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 __MDG_HELPERS_H
24bf215546Sopenharmony_ci#define __MDG_HELPERS_H
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/macros.h"
27bf215546Sopenharmony_ci#include <stdio.h>
28bf215546Sopenharmony_ci#include <string.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#define OP_IS_LOAD_VARY_F(op) (\
31bf215546Sopenharmony_ci                op == midgard_op_ld_vary_16 || \
32bf215546Sopenharmony_ci                op == midgard_op_ld_vary_32 \
33bf215546Sopenharmony_ci        )
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#define OP_IS_PROJECTION(op) ( \
36bf215546Sopenharmony_ci                op == midgard_op_ldst_perspective_div_y || \
37bf215546Sopenharmony_ci                op == midgard_op_ldst_perspective_div_z || \
38bf215546Sopenharmony_ci                op == midgard_op_ldst_perspective_div_w \
39bf215546Sopenharmony_ci        )
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#define OP_IS_VEC4_ONLY(op) ( \
42bf215546Sopenharmony_ci                OP_IS_PROJECTION(op) || \
43bf215546Sopenharmony_ci                op == midgard_op_ld_cubemap_coords \
44bf215546Sopenharmony_ci        )
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci#define OP_IS_MOVE(op) ( \
47bf215546Sopenharmony_ci                (op >= midgard_alu_op_fmov && op <= midgard_alu_op_fmov_rtp) || \
48bf215546Sopenharmony_ci                op == midgard_alu_op_imov \
49bf215546Sopenharmony_ci        )
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#define OP_IS_UBO_READ(op) ( \
52bf215546Sopenharmony_ci                op >= midgard_op_ld_ubo_u8 && \
53bf215546Sopenharmony_ci                op <= midgard_op_ld_ubo_128_bswap8 \
54bf215546Sopenharmony_ci        )
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci#define OP_IS_CSEL_V(op) ( \
57bf215546Sopenharmony_ci                op == midgard_alu_op_icsel_v || \
58bf215546Sopenharmony_ci                op == midgard_alu_op_fcsel_v \
59bf215546Sopenharmony_ci        )
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci#define OP_IS_CSEL(op) ( \
62bf215546Sopenharmony_ci                OP_IS_CSEL_V(op) || \
63bf215546Sopenharmony_ci                op == midgard_alu_op_icsel || \
64bf215546Sopenharmony_ci                op == midgard_alu_op_fcsel \
65bf215546Sopenharmony_ci        )
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci#define OP_IS_UNSIGNED_CMP(op) ( \
68bf215546Sopenharmony_ci                op == midgard_alu_op_ult || \
69bf215546Sopenharmony_ci                op == midgard_alu_op_ule \
70bf215546Sopenharmony_ci        )
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci#define OP_IS_INTEGER_CMP(op) ( \
73bf215546Sopenharmony_ci                op == midgard_alu_op_ieq || \
74bf215546Sopenharmony_ci                op == midgard_alu_op_ine || \
75bf215546Sopenharmony_ci                op == midgard_alu_op_ilt || \
76bf215546Sopenharmony_ci                op == midgard_alu_op_ile || \
77bf215546Sopenharmony_ci                OP_IS_UNSIGNED_CMP(op) \
78bf215546Sopenharmony_ci        )
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci#define OP_IS_COMMON_STORE(op) ( \
81bf215546Sopenharmony_ci                op >= midgard_op_st_u8 && \
82bf215546Sopenharmony_ci                op <= midgard_op_st_128_bswap8 \
83bf215546Sopenharmony_ci        )
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci#define OP_IS_IMAGE(op) ( \
86bf215546Sopenharmony_ci                (op >= midgard_op_ld_image_32f && op <= midgard_op_ld_image_32i) || \
87bf215546Sopenharmony_ci                (op >= midgard_op_st_image_32f && op <= midgard_op_st_image_32i) || \
88bf215546Sopenharmony_ci                op == midgard_op_lea_image \
89bf215546Sopenharmony_ci        )
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci#define OP_IS_SPECIAL(op) ( \
92bf215546Sopenharmony_ci                (op >= midgard_op_ld_special_32f && op <= midgard_op_ld_special_32i) || \
93bf215546Sopenharmony_ci                (op >= midgard_op_st_special_32f && op <= midgard_op_st_special_32i) \
94bf215546Sopenharmony_ci        )
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci#define OP_IS_PACK_COLOUR(op) ( \
97bf215546Sopenharmony_ci                (op >= midgard_op_pack_colour_f32 && op <= midgard_op_pack_colour_s32) \
98bf215546Sopenharmony_ci        )
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci#define OP_IS_UNPACK_COLOUR(op) ( \
101bf215546Sopenharmony_ci                (op >= midgard_op_unpack_colour_f32 && op <= midgard_op_unpack_colour_s32) \
102bf215546Sopenharmony_ci        )
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci/* Instructions that are on the load/store unit but don't access memory */
105bf215546Sopenharmony_ci#define OP_IS_REG2REG_LDST(op) ( \
106bf215546Sopenharmony_ci                op >= midgard_op_unpack_colour_f32 && \
107bf215546Sopenharmony_ci                op <= midgard_op_ldst_perspective_div_w \
108bf215546Sopenharmony_ci        )
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci/* ALU control words are single bit fields with a lot of space */
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci#define ALU_ENAB_VEC_MUL  (1 << 17)
113bf215546Sopenharmony_ci#define ALU_ENAB_SCAL_ADD  (1 << 19)
114bf215546Sopenharmony_ci#define ALU_ENAB_VEC_ADD  (1 << 21)
115bf215546Sopenharmony_ci#define ALU_ENAB_SCAL_MUL  (1 << 23)
116bf215546Sopenharmony_ci#define ALU_ENAB_VEC_LUT  (1 << 25)
117bf215546Sopenharmony_ci#define ALU_ENAB_BR_COMPACT (1 << 26)
118bf215546Sopenharmony_ci#define ALU_ENAB_BRANCH   (1 << 27)
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci/* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci/* Denotes an opcode that takes a vector input with a fixed-number of
123bf215546Sopenharmony_ci * channels, but outputs to only a single output channel, like dot products.
124bf215546Sopenharmony_ci * For these, to determine the effective mask, this quirk can be set. We have
125bf215546Sopenharmony_ci * an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no
126bf215546Sopenharmony_ci * sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't
127bf215546Sopenharmony_ci * make sense (since then why are we quirked?), so that corresponds to "no
128bf215546Sopenharmony_ci * count set" */
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci#define OP_CHANNEL_COUNT(c) ((c - 1) << 0)
131bf215546Sopenharmony_ci#define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci/* For instructions that take a single argument, normally the first argument
134bf215546Sopenharmony_ci * slot is used for the argument and the second slot is a dummy #0 constant.
135bf215546Sopenharmony_ci * However, there are exceptions: instructions like fmov store their argument
136bf215546Sopenharmony_ci * in the _second_ slot and store a dummy r24 in the first slot, designated by
137bf215546Sopenharmony_ci * QUIRK_FLIPPED_R24 */
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci#define QUIRK_FLIPPED_R24 (1 << 2)
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci/* Is the op commutative? */
142bf215546Sopenharmony_ci#define OP_COMMUTES (1 << 3)
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci/* Does the op convert types between int- and float- space (i2f/f2u/etc) */
145bf215546Sopenharmony_ci#define OP_TYPE_CONVERT (1 << 4)
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci/* Is this opcode the first in a f2x (rte, rtz, rtn, rtp) sequence? If so,
148bf215546Sopenharmony_ci * takes a roundmode argument in the IR. This has the semantic of rounding the
149bf215546Sopenharmony_ci * source (it's all fused in), which is why it doesn't necessarily make sense
150bf215546Sopenharmony_ci * for i2f (though folding there might be necessary for OpenCL reasons). Comes
151bf215546Sopenharmony_ci * up in format conversion, i.e. f2u_rte */
152bf215546Sopenharmony_ci#define MIDGARD_ROUNDS (1 << 5)
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci/* Vector-independant shorthands for the above; these numbers are arbitrary and
155bf215546Sopenharmony_ci * not from the ISA. Convert to the above with unit_enum_to_midgard */
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci#define UNIT_MUL 0
158bf215546Sopenharmony_ci#define UNIT_ADD 1
159bf215546Sopenharmony_ci#define UNIT_LUT 2
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci#define IS_ALU(tag) (tag >= TAG_ALU_4)
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci/* Special register aliases */
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci#define MAX_WORK_REGISTERS 16
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci/* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */
168bf215546Sopenharmony_ci#define REGISTER_UNIFORMS 24
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci/* r24 and r25 are special registers that only exist during the pipeline,
171bf215546Sopenharmony_ci * by using them when we don't care about the register we skip a roundtrip
172bf215546Sopenharmony_ci * to the register file. */
173bf215546Sopenharmony_ci#define REGISTER_UNUSED 24
174bf215546Sopenharmony_ci#define REGISTER_CONSTANT 26
175bf215546Sopenharmony_ci#define REGISTER_LDST_BASE 26
176bf215546Sopenharmony_ci#define REGISTER_TEXTURE_BASE 28
177bf215546Sopenharmony_ci#define REGISTER_SELECT 31
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci/* The following registers are read-only */
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci/* XY is Program Counter, ZW is Stack Pointer */
182bf215546Sopenharmony_ci#define REGISTER_LDST_PC_SP 2
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci/* XY is Thread Local Storage pointer, ZW is Workgroup Local Storage pointer */
185bf215546Sopenharmony_ci#define REGISTER_LDST_LOCAL_STORAGE_PTR 3
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci#define REGISTER_LDST_LOCAL_THREAD_ID 4
188bf215546Sopenharmony_ci#define REGISTER_LDST_GROUP_ID 5
189bf215546Sopenharmony_ci#define REGISTER_LDST_GLOBAL_THREAD_ID 6
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci/* This register is always zeroed when read. */
192bf215546Sopenharmony_ci#define REGISTER_LDST_ZERO 7
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci/* SSA helper aliases to mimic the registers. */
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci#define SSA_FIXED_SHIFT 24
197bf215546Sopenharmony_ci#define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1)
198bf215546Sopenharmony_ci#define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1)
199bf215546Sopenharmony_ci#define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci#define COMPONENT_X 0x0
202bf215546Sopenharmony_ci#define COMPONENT_Y 0x1
203bf215546Sopenharmony_ci#define COMPONENT_Z 0x2
204bf215546Sopenharmony_ci#define COMPONENT_W 0x3
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci#define SWIZZLE_IDENTITY { \
207bf215546Sopenharmony_ci        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \
208bf215546Sopenharmony_ci        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \
209bf215546Sopenharmony_ci        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \
210bf215546Sopenharmony_ci        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } \
211bf215546Sopenharmony_ci}
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci#define SWIZZLE_IDENTITY_4 { \
214bf215546Sopenharmony_ci        { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0 }, \
215bf215546Sopenharmony_ci        { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0 }, \
216bf215546Sopenharmony_ci        { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0 }, \
217bf215546Sopenharmony_ci        { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0 }, \
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_cistatic inline unsigned
221bf215546Sopenharmony_cimask_of(unsigned nr_comp)
222bf215546Sopenharmony_ci{
223bf215546Sopenharmony_ci        return (1 << nr_comp) - 1;
224bf215546Sopenharmony_ci}
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci/* See ISA notes */
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci#define LDST_NOP (3)
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci/* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is
231bf215546Sopenharmony_ci * implemented on some subset of these units (or occassionally all of them).
232bf215546Sopenharmony_ci * This table encodes a bit mask of valid units for each opcode, so the
233bf215546Sopenharmony_ci * scheduler can figure where to plonk the instruction. */
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci/* Shorthands for each unit */
236bf215546Sopenharmony_ci#define UNIT_VMUL ALU_ENAB_VEC_MUL
237bf215546Sopenharmony_ci#define UNIT_SADD ALU_ENAB_SCAL_ADD
238bf215546Sopenharmony_ci#define UNIT_VADD ALU_ENAB_VEC_ADD
239bf215546Sopenharmony_ci#define UNIT_SMUL ALU_ENAB_SCAL_MUL
240bf215546Sopenharmony_ci#define UNIT_VLUT ALU_ENAB_VEC_LUT
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci/* Shorthands for usual combinations of units */
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci#define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)
245bf215546Sopenharmony_ci#define UNITS_ADD (UNIT_VADD | UNIT_SADD)
246bf215546Sopenharmony_ci#define UNITS_MOST (UNITS_MUL | UNITS_ADD)
247bf215546Sopenharmony_ci#define UNITS_ALL (UNITS_MOST | UNIT_VLUT)
248bf215546Sopenharmony_ci#define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)
249bf215546Sopenharmony_ci#define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)
250bf215546Sopenharmony_ci#define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_cistruct mir_op_props {
253bf215546Sopenharmony_ci        const char *name;
254bf215546Sopenharmony_ci        unsigned props;
255bf215546Sopenharmony_ci};
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci/* For load/store */
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_cistruct mir_ldst_op_props {
260bf215546Sopenharmony_ci        const char *name;
261bf215546Sopenharmony_ci        unsigned props;
262bf215546Sopenharmony_ci};
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_cistruct mir_tex_op_props {
265bf215546Sopenharmony_ci        const char *name;
266bf215546Sopenharmony_ci        unsigned props;
267bf215546Sopenharmony_ci};
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_cistruct mir_tag_props {
270bf215546Sopenharmony_ci        const char *name;
271bf215546Sopenharmony_ci        unsigned size;
272bf215546Sopenharmony_ci};
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci/* Lower 2-bits are a midgard_reg_mode */
275bf215546Sopenharmony_ci#define GET_LDST_SIZE(c) (c & 3)
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci/* Store (so the primary register is a source, not a destination */
278bf215546Sopenharmony_ci#define LDST_STORE (1 << 2)
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci/* Mask has special meaning and should not be manipulated directly */
281bf215546Sopenharmony_ci#define LDST_SPECIAL_MASK (1 << 3)
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci/* Non-store operation has side effects and should not be eliminated even if
284bf215546Sopenharmony_ci * its mask is 0 */
285bf215546Sopenharmony_ci#define LDST_SIDE_FX (1 << 4)
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci/* Computes an address according to indirects/zext/shift/etc */
288bf215546Sopenharmony_ci#define LDST_ADDRESS (1 << 5)
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci/* Some fields such swizzle and address have special meanings */
291bf215546Sopenharmony_ci#define LDST_ATOMIC (1 << 6)
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci/* Operates on attributes/varyings (including images) */
294bf215546Sopenharmony_ci#define LDST_ATTRIB (1 << 7)
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci/* This file is common, so don't define the tables themselves. #include
297bf215546Sopenharmony_ci * midgard_op.h if you need that, or edit midgard_ops.c directly */
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci/* Duplicate bits to convert a per-component to duplicated 8-bit format,
300bf215546Sopenharmony_ci * which is used for vector units */
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_cistatic inline unsigned
303bf215546Sopenharmony_ciexpand_writemask(unsigned mask, unsigned log2_channels)
304bf215546Sopenharmony_ci{
305bf215546Sopenharmony_ci        unsigned o = 0;
306bf215546Sopenharmony_ci        unsigned factor = 8 >> log2_channels;
307bf215546Sopenharmony_ci        unsigned expanded = (1 << factor) - 1;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci        for (unsigned i = 0; i < (1 << log2_channels); ++i)
310bf215546Sopenharmony_ci                if (mask & (1 << i))
311bf215546Sopenharmony_ci                        o |= (expanded << (factor * i));
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci        return o;
314bf215546Sopenharmony_ci}
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci/* Coerce structs to integer */
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_cistatic inline unsigned
319bf215546Sopenharmony_civector_alu_srco_unsigned(midgard_vector_alu_src src)
320bf215546Sopenharmony_ci{
321bf215546Sopenharmony_ci        unsigned u;
322bf215546Sopenharmony_ci        memcpy(&u, &src, sizeof(src));
323bf215546Sopenharmony_ci        return u;
324bf215546Sopenharmony_ci}
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_cistatic inline midgard_vector_alu_src
327bf215546Sopenharmony_civector_alu_from_unsigned(unsigned u)
328bf215546Sopenharmony_ci{
329bf215546Sopenharmony_ci        midgard_vector_alu_src s;
330bf215546Sopenharmony_ci        memcpy(&s, &u, sizeof(s));
331bf215546Sopenharmony_ci        return s;
332bf215546Sopenharmony_ci}
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_cistatic inline void
335bf215546Sopenharmony_cimir_compose_swizzle(unsigned *left, unsigned *right, unsigned *final_out)
336bf215546Sopenharmony_ci{
337bf215546Sopenharmony_ci        unsigned out[16];
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci        for (unsigned c = 0; c < 16; ++c)
340bf215546Sopenharmony_ci                out[c] = right[left[c]];
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci        memcpy(final_out, out, sizeof(out));
343bf215546Sopenharmony_ci}
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci/* Checks for an xyzw.. swizzle, given a mask */
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_cistatic inline bool
348bf215546Sopenharmony_cimir_is_simple_swizzle(unsigned *swizzle, unsigned mask)
349bf215546Sopenharmony_ci{
350bf215546Sopenharmony_ci        for (unsigned i = 0; i < 16; ++i) {
351bf215546Sopenharmony_ci                if (!(mask & (1 << i))) continue;
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci                if (swizzle[i] != i)
354bf215546Sopenharmony_ci                        return false;
355bf215546Sopenharmony_ci        }
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci        return true;
358bf215546Sopenharmony_ci}
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci/* Packs a load/store argument */
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_cistatic inline uint8_t
363bf215546Sopenharmony_cimidgard_ldst_comp(unsigned reg, unsigned component, unsigned size)
364bf215546Sopenharmony_ci{
365bf215546Sopenharmony_ci        assert((reg & ~1) == 0);
366bf215546Sopenharmony_ci        assert(size == 16 || size == 32 || size == 64);
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci        /* Shift so everything is in terms of 32-bit units */
369bf215546Sopenharmony_ci        if (size == 64) {
370bf215546Sopenharmony_ci                assert(component < 2);
371bf215546Sopenharmony_ci                component <<= 1;
372bf215546Sopenharmony_ci        } else if (size == 16) {
373bf215546Sopenharmony_ci                assert((component & 1) == 0);
374bf215546Sopenharmony_ci                component >>= 1;
375bf215546Sopenharmony_ci        }
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci        return component;
378bf215546Sopenharmony_ci}
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci/* Packs/unpacks a ubo index immediate. The unpack must be defined here so it
381bf215546Sopenharmony_ci * can be used with the disassembler, which need not be linked with the main
382bf215546Sopenharmony_ci * compiler.
383bf215546Sopenharmony_ci */
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_civoid midgard_pack_ubo_index_imm(midgard_load_store_word *word, unsigned index);
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_cistatic inline unsigned
388bf215546Sopenharmony_cimidgard_unpack_ubo_index_imm(midgard_load_store_word word)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci        unsigned ubo = word.arg_comp |
391bf215546Sopenharmony_ci                       (word.arg_reg << 2)  |
392bf215546Sopenharmony_ci                       (word.bitsize_toggle << 5) |
393bf215546Sopenharmony_ci                       (word.index_format << 6);
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci        return ubo;
396bf215546Sopenharmony_ci}
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci/* Packs/unpacks varying parameters.
400bf215546Sopenharmony_ci * FIXME: IMPORTANT: We currently handle varying mode weirdly, by passing all
401bf215546Sopenharmony_ci * parameters via an offset and using REGISTER_LDST_ZERO as base. This works
402bf215546Sopenharmony_ci * for most parameters, but does not allow us to encode/decode direct sample
403bf215546Sopenharmony_ci * position. */
404bf215546Sopenharmony_civoid midgard_pack_varying_params(midgard_load_store_word *word, midgard_varying_params p);
405bf215546Sopenharmony_cimidgard_varying_params midgard_unpack_varying_params(midgard_load_store_word word);
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci/* Load/store ops' displacement helpers.
408bf215546Sopenharmony_ci * This is useful because different types of load/store ops have different
409bf215546Sopenharmony_ci * displacement bitsize. */
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci#define UNPACK_LDST_ATTRIB_OFS(a) ((a) >> 9)
412bf215546Sopenharmony_ci#define UNPACK_LDST_VERTEX_OFS(a) util_sign_extend((a) & 0x1FF, 9)
413bf215546Sopenharmony_ci#define UNPACK_LDST_SELECTOR_OFS(a) ((a) >> 9)
414bf215546Sopenharmony_ci#define UNPACK_LDST_UBO_OFS(a) ((a) >> 2)
415bf215546Sopenharmony_ci#define UNPACK_LDST_MEM_OFS(a) ((a))
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci#define PACK_LDST_ATTRIB_OFS(a) ((a) << 9)
418bf215546Sopenharmony_ci#define PACK_LDST_VERTEX_OFS(a) ((a) & 0x1FF)
419bf215546Sopenharmony_ci#define PACK_LDST_SELECTOR_OFS(a) ((a) << 9)
420bf215546Sopenharmony_ci#define PACK_LDST_UBO_OFS(a) ((a) << 2)
421bf215546Sopenharmony_ci#define PACK_LDST_MEM_OFS(a) ((a))
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_cistatic inline bool
424bf215546Sopenharmony_cimidgard_is_branch_unit(unsigned unit)
425bf215546Sopenharmony_ci{
426bf215546Sopenharmony_ci        return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT);
427bf215546Sopenharmony_ci}
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci/* Packs ALU mod argument */
430bf215546Sopenharmony_cistruct midgard_instruction;
431bf215546Sopenharmony_ciunsigned mir_pack_mod(struct midgard_instruction *ins, unsigned i, bool scalar);
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_civoid
434bf215546Sopenharmony_cimir_print_constant_component(FILE *fp, const midgard_constants *consts,
435bf215546Sopenharmony_ci                             unsigned c, midgard_reg_mode reg_mode, bool half,
436bf215546Sopenharmony_ci                             unsigned mod, midgard_alu_op op);
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_civoid
439bf215546Sopenharmony_cimir_print_outmod(FILE *fp, unsigned outmod, bool is_int);
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci#endif
442