1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <[email protected]>
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <inttypes.h>
26 #include <math.h>
27 #include "util/half_float.h"
28 #include "helpers.h"
29 #include "midgard.h"
30 #include "midgard_ops.h"
31
32 void
mir_print_constant_component(FILE * fp,const midgard_constants * consts,unsigned c,midgard_reg_mode reg_mode,bool half,unsigned mod,midgard_alu_op op)33 mir_print_constant_component(FILE *fp, const midgard_constants *consts,
34 unsigned c, midgard_reg_mode reg_mode, bool half,
35 unsigned mod, midgard_alu_op op)
36 {
37 bool is_sint = false, is_uint = false, is_hex = false;
38 const char *opname = alu_opcode_props[op].name;
39
40 bool is_int = midgard_is_integer_op(op);
41
42 /* Add a sentinel name to prevent crashing */
43 if (!opname)
44 opname = "unknown";
45
46 if (is_int) {
47 is_uint = midgard_is_unsigned_op(op);
48
49 if (!is_uint) {
50 /* Bit ops are easier to follow when the constant is printed in
51 * hexadecimal. Other operations starting with a 'i' are
52 * considered to operate on signed integers. That might not
53 * be true for all of them, but it's good enough for traces.
54 */
55 if (op >= midgard_alu_op_iand && op <= midgard_alu_op_ipopcnt)
56 is_hex = true;
57 else
58 is_sint = true;
59 }
60 }
61
62 if (half)
63 reg_mode--;
64
65 switch (reg_mode) {
66 case midgard_reg_mode_64:
67 if (is_sint) {
68 fprintf(fp, "%" PRIi64, consts->i64[c]);
69 } else if (is_uint) {
70 fprintf(fp, "%" PRIu64, consts->u64[c]);
71 } else if (is_hex) {
72 fprintf(fp, "0x%" PRIX64, consts->u64[c]);
73 } else {
74 double v = consts->f64[c];
75
76 if (mod & MIDGARD_FLOAT_MOD_ABS)
77 v = fabs(v);
78 if (mod & MIDGARD_FLOAT_MOD_NEG)
79 v = -v;
80
81 printf("%g", v);
82 }
83 break;
84
85 case midgard_reg_mode_32:
86 if (is_sint) {
87 int64_t v;
88
89 if (half && mod == midgard_int_zero_extend)
90 v = consts->u32[c];
91 else if (half && mod == midgard_int_left_shift)
92 v = (uint64_t)consts->u32[c] << 32;
93 else
94 v = consts->i32[c];
95
96 fprintf(fp, "%" PRIi64, v);
97 } else if (is_uint || is_hex) {
98 uint64_t v;
99
100 if (half && mod == midgard_int_left_shift)
101 v = (uint64_t)consts->u32[c] << 32;
102 else
103 v = consts->u32[c];
104
105 fprintf(fp, is_uint ? "%" PRIu64 : "0x%" PRIX64, v);
106 } else {
107 float v = consts->f32[c];
108
109 if (mod & MIDGARD_FLOAT_MOD_ABS)
110 v = fabsf(v);
111 if (mod & MIDGARD_FLOAT_MOD_NEG)
112 v = -v;
113
114 fprintf(fp, "%g", v);
115 }
116 break;
117
118 case midgard_reg_mode_16:
119 if (is_sint) {
120 int32_t v;
121
122 if (half && mod == midgard_int_zero_extend)
123 v = consts->u16[c];
124 else if (half && mod == midgard_int_left_shift)
125 v = (uint32_t)consts->u16[c] << 16;
126 else
127 v = consts->i16[c];
128
129 fprintf(fp, "%d", v);
130 } else if (is_uint || is_hex) {
131 uint32_t v;
132
133 if (half && mod == midgard_int_left_shift)
134 v = (uint32_t)consts->u16[c] << 16;
135 else
136 v = consts->u16[c];
137
138 fprintf(fp, is_uint ? "%u" : "0x%X", v);
139 } else {
140 float v = _mesa_half_to_float(consts->f16[c]);
141
142 if (mod & MIDGARD_FLOAT_MOD_ABS)
143 v = fabsf(v);
144 if (mod & MIDGARD_FLOAT_MOD_NEG)
145 v = -v;
146
147 fprintf(fp, "%g", v);
148 }
149 break;
150
151 case midgard_reg_mode_8:
152 fprintf(fp, "0x%X", consts->u8[c]);
153
154 if (mod)
155 fprintf(fp, " /* %u */", mod);
156
157 assert(!half); /* No 4-bit */
158
159 break;
160 }
161 }
162
163 static char *outmod_names_float[4] = {"", ".clamp_0_inf", ".clamp_m1_1",
164 ".clamp_0_1"};
165
166 static char *outmod_names_int[4] = {".ssat", ".usat", ".keeplo", ".keephi"};
167
168 void
mir_print_outmod(FILE * fp,unsigned outmod,bool is_int)169 mir_print_outmod(FILE *fp, unsigned outmod, bool is_int)
170 {
171 fprintf(fp, "%s",
172 is_int ? outmod_names_int[outmod] : outmod_names_float[outmod]);
173 }
174