1bf215546Sopenharmony_ci/* Copyright (c) 2018-2019 Alyssa Rosenzweig (alyssa@rosenzweig.io)
2bf215546Sopenharmony_ci * Copyright (C) 2019-2020 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights
7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
9bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17bf215546Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20bf215546Sopenharmony_ci * THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#ifndef __MIDGARD_OPS
24bf215546Sopenharmony_ci#define __MIDGARD_OPS
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "helpers.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/* Forward declare */
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ciextern struct mir_op_props alu_opcode_props[256];
31bf215546Sopenharmony_ciextern struct mir_ldst_op_props load_store_opcode_props[256];
32bf215546Sopenharmony_ciextern struct mir_tex_op_props tex_opcode_props[16];
33bf215546Sopenharmony_ciextern struct mir_tag_props midgard_tag_props[16];
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#define OP_IS_ATOMIC(op) (load_store_opcode_props[op].props & LDST_ATOMIC)
36bf215546Sopenharmony_ci#define OP_USES_ATTRIB(op) (load_store_opcode_props[op].props & LDST_ATTRIB)
37bf215546Sopenharmony_ci#define OP_IS_STORE(op) (load_store_opcode_props[op].props & LDST_STORE)
38bf215546Sopenharmony_ci#define OP_HAS_ADDRESS(op) (load_store_opcode_props[op].props & LDST_ADDRESS)
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci/* Is this opcode that of an integer (regardless of signedness)? Instruction
41bf215546Sopenharmony_ci * names authoritatively determine types */
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistatic inline bool
44bf215546Sopenharmony_cimidgard_is_integer_op(int op)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci        return (op >= 0x40 && op <= 0x7E) || (op >= 0xA0 && op <= 0xC1);
47bf215546Sopenharmony_ci}
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistatic inline bool
50bf215546Sopenharmony_cimidgard_is_unsigned_op(int op)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci        assert(midgard_is_integer_op(op));
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci        switch (op) {
55bf215546Sopenharmony_ci        case midgard_alu_op_uaddsat:
56bf215546Sopenharmony_ci        case midgard_alu_op_usubsat:
57bf215546Sopenharmony_ci        case midgard_alu_op_uwmul:
58bf215546Sopenharmony_ci        case midgard_alu_op_umin:
59bf215546Sopenharmony_ci        case midgard_alu_op_umax:
60bf215546Sopenharmony_ci        case midgard_alu_op_uavg:
61bf215546Sopenharmony_ci        case midgard_alu_op_uravg:
62bf215546Sopenharmony_ci        case midgard_alu_op_ushlsat:
63bf215546Sopenharmony_ci        case midgard_alu_op_uabsdiff:
64bf215546Sopenharmony_ci        case midgard_alu_op_ult:
65bf215546Sopenharmony_ci        case midgard_alu_op_ule:
66bf215546Sopenharmony_ci        case midgard_alu_op_uball_lt:
67bf215546Sopenharmony_ci        case midgard_alu_op_uball_lte:
68bf215546Sopenharmony_ci        case midgard_alu_op_ubany_lt:
69bf215546Sopenharmony_ci        case midgard_alu_op_ubany_lte:
70bf215546Sopenharmony_ci        case midgard_alu_op_u2f_rte:
71bf215546Sopenharmony_ci        case midgard_alu_op_u2f_rtz:
72bf215546Sopenharmony_ci        case midgard_alu_op_u2f_rtn:
73bf215546Sopenharmony_ci        case midgard_alu_op_u2f_rtp:
74bf215546Sopenharmony_ci                return true;
75bf215546Sopenharmony_ci        default:
76bf215546Sopenharmony_ci                return false;
77bf215546Sopenharmony_ci        }
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci/* Does this opcode *write* an integer? Same as is_integer_op, unless it's a
81bf215546Sopenharmony_ci * conversion between int<->float in which case we do the opposite */
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic inline bool
84bf215546Sopenharmony_cimidgard_is_integer_out_op(int op)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci        bool is_int = midgard_is_integer_op(op);
87bf215546Sopenharmony_ci        bool is_conversion = alu_opcode_props[op].props & OP_TYPE_CONVERT;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci        return is_int ^ is_conversion;
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci/* Determines effective writemask, taking quirks and expansion into account */
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic inline unsigned
95bf215546Sopenharmony_cieffective_writemask(midgard_alu_op op, unsigned existing_mask)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci        /* Channel count is off-by-one to fit in two-bits (0 channel makes no
98bf215546Sopenharmony_ci         * sense) */
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci        unsigned channel_count = GET_CHANNEL_COUNT(alu_opcode_props[op].props);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci        /* If there is a fixed channel count, construct the appropriate mask */
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci        if (channel_count)
105bf215546Sopenharmony_ci                return (1 << channel_count) - 1;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci        return existing_mask;
108bf215546Sopenharmony_ci};
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci#endif
111