1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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 <math.h>
26 #include <inttypes.h>
27 #include "util/half_float.h"
28 #include "midgard.h"
29 #include "helpers.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, unsigned c,
34 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 /* Add a sentinel name to prevent crashing */
41 if (!opname)
42 opname = "unknown";
43
44 if (opname[0] == 'u') {
45 /* If the opcode starts with a 'u' we are sure we deal with an
46 * unsigned int operation
47 */
48 is_uint = true;
49 } else if (opname[0] == 'i') {
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 &&
56 op <= midgard_alu_op_ibitcount8)
57 is_hex = true;
58 else
59 is_sint = true;
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) v = fabs(v);
77 if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
78
79 printf("%g", v);
80 }
81 break;
82
83 case midgard_reg_mode_32:
84 if (is_sint) {
85 int64_t v;
86
87 if (half && mod == midgard_int_zero_extend)
88 v = consts->u32[c];
89 else if (half && mod == midgard_int_shift)
90 v = (uint64_t)consts->u32[c] << 32;
91 else
92 v = consts->i32[c];
93
94 fprintf(fp, "%"PRIi64, v);
95 } else if (is_uint || is_hex) {
96 uint64_t v;
97
98 if (half && mod == midgard_int_shift)
99 v = (uint64_t)consts->u32[c] << 32;
100 else
101 v = consts->u32[c];
102
103 fprintf(fp, is_uint ? "%"PRIu64 : "0x%"PRIX64, v);
104 } else {
105 float v = consts->f32[c];
106
107 if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);
108 if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
109
110 fprintf(fp, "%g", v);
111 }
112 break;
113
114 case midgard_reg_mode_16:
115 if (is_sint) {
116 int32_t v;
117
118 if (half && mod == midgard_int_zero_extend)
119 v = consts->u16[c];
120 else if (half && mod == midgard_int_shift)
121 v = (uint32_t)consts->u16[c] << 16;
122 else
123 v = consts->i16[c];
124
125 fprintf(fp, "%d", v);
126 } else if (is_uint || is_hex) {
127 uint32_t v;
128
129 if (half && mod == midgard_int_shift)
130 v = (uint32_t)consts->u16[c] << 16;
131 else
132 v = consts->u16[c];
133
134 fprintf(fp, is_uint ? "%u" : "0x%X", v);
135 } else {
136 float v = _mesa_half_to_float(consts->f16[c]);
137
138 if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);
139 if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
140
141 fprintf(fp, "%g", v);
142 }
143 break;
144
145 case midgard_reg_mode_8:
146 fprintf(fp, "0x%X", consts->u8[c]);
147
148 if (mod)
149 fprintf(fp, " /* %u */", mod);
150
151 assert(!half); /* No 4-bit */
152
153 break;
154 }
155 }
156
157
158