Debian Codegraph @

src/panfrost/compiler/midgard/midgard_print_constant.c

raw
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include <inttypes.h>
8 #include <math.h>
9 #include "util/half_float.h"
10 #include "helpers.h"
11 #include "midgard.h"
12 #include "midgard_ops.h"
13
14 void
15 mir_print_constant_component(FILE *fp, const midgard_constants *consts,
16 unsigned c, midgard_reg_mode reg_mode, bool half,
17 unsigned mod, midgard_alu_op op)
18 {
19 bool is_sint = false, is_uint = false, is_hex = false;
20 const char *opname = alu_opcode_props[op].name;
21
22 bool is_int = midgard_is_integer_op(op);
23
24 /* Add a sentinel name to prevent crashing */
25 if (!opname)
26 opname = "unknown";
27
28 if (is_int) {
29 is_uint = midgard_is_unsigned_op(op);
30
31 if (!is_uint) {
32 /* Bit ops are easier to follow when the constant is printed in
33 * hexadecimal. Other operations starting with a 'i' are
34 * considered to operate on signed integers. That might not
35 * be true for all of them, but it's good enough for traces.
36 */
37 if (op >= midgard_alu_op_iand && op <= midgard_alu_op_ipopcnt)
38 is_hex = true;
39 else
40 is_sint = true;
41 }
42 }
43
44 if (half)
45 reg_mode--;
46
47 switch (reg_mode) {
48 case midgard_reg_mode_64:
49 if (is_sint) {
50 fprintf(fp, "%" PRIi64, consts->i64[c]);
51 } else if (is_uint) {
52 fprintf(fp, "%" PRIu64, consts->u64[c]);
53 } else if (is_hex) {
54 fprintf(fp, "0x%" PRIX64, consts->u64[c]);
55 } else {
56 double v = consts->f64[c];
57
58 if (mod & MIDGARD_FLOAT_MOD_ABS)
59 v = fabs(v);
60 if (mod & MIDGARD_FLOAT_MOD_NEG)
61 v = -v;
62
63 printf("%g", v);
64 }
65 break;
66
67 case midgard_reg_mode_32:
68 if (is_sint) {
69 int64_t v;
70
71 if (half && mod == midgard_int_zero_extend)
72 v = consts->u32[c];
73 else if (half && mod == midgard_int_left_shift)
74 v = (uint64_t)consts->u32[c] << 32;
75 else
76 v = consts->i32[c];
77
78 fprintf(fp, "%" PRIi64, v);
79 } else if (is_uint || is_hex) {
80 uint64_t v;
81
82 if (half && mod == midgard_int_left_shift)
83 v = (uint64_t)consts->u32[c] << 32;
84 else
85 v = consts->u32[c];
86
87 fprintf(fp, is_uint ? "%" PRIu64 : "0x%" PRIX64, v);
88 } else {
89 float v = consts->f32[c];
90
91 if (mod & MIDGARD_FLOAT_MOD_ABS)
92 v = fabsf(v);
93 if (mod & MIDGARD_FLOAT_MOD_NEG)
94 v = -v;
95
96 fprintf(fp, "%g", v);
97 }
98 break;
99
100 case midgard_reg_mode_16:
101 if (is_sint) {
102 int32_t v;
103
104 if (half && mod == midgard_int_zero_extend)
105 v = consts->u16[c];
106 else if (half && mod == midgard_int_left_shift)
107 v = (uint32_t)consts->u16[c] << 16;
108 else
109 v = consts->i16[c];
110
111 fprintf(fp, "%d", v);
112 } else if (is_uint || is_hex) {
113 uint32_t v;
114
115 if (half && mod == midgard_int_left_shift)
116 v = (uint32_t)consts->u16[c] << 16;
117 else
118 v = consts->u16[c];
119
120 fprintf(fp, is_uint ? "%u" : "0x%X", v);
121 } else {
122 float v = _mesa_half_to_float(consts->f16[c]);
123
124 if (mod & MIDGARD_FLOAT_MOD_ABS)
125 v = fabsf(v);
126 if (mod & MIDGARD_FLOAT_MOD_NEG)
127 v = -v;
128
129 fprintf(fp, "%g", v);
130 }
131 break;
132
133 case midgard_reg_mode_8:
134 fprintf(fp, "0x%X", consts->u8[c]);
135
136 if (mod)
137 fprintf(fp, " /* %u */", mod);
138
139 assert(!half); /* No 4-bit */
140
141 break;
142 }
143 }
144
145 static char *outmod_names_float[4] = {"", ".clamp_0_inf", ".clamp_m1_1",
146 ".clamp_0_1"};
147
148 static char *outmod_names_int[4] = {".ssat", ".usat", ".keeplo", ".keephi"};
149
150 void
151 mir_print_outmod(FILE *fp, unsigned outmod, bool is_int)
152 {
153 fprintf(fp, "%s",
154 is_int ? outmod_names_int[outmod] : outmod_names_float[outmod]);
155 }