2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "util/half_float.h"
29 #include "midgard_ops.h"
32 mir_print_constant_component(FILE *fp
, const midgard_constants
*consts
, unsigned c
,
33 midgard_reg_mode reg_mode
, bool half
,
34 unsigned mod
, midgard_alu_op op
)
36 bool is_sint
= false, is_uint
= false, is_hex
= false;
37 const char *opname
= alu_opcode_props
[op
].name
;
39 /* Add a sentinel name to prevent crashing */
43 if (opname
[0] == 'u') {
44 /* If the opcode starts with a 'u' we are sure we deal with an
45 * unsigned int operation
48 } else if (opname
[0] == 'i') {
49 /* Bit ops are easier to follow when the constant is printed in
50 * hexadecimal. Other operations starting with a 'i' are
51 * considered to operate on signed integers. That might not
52 * be true for all of them, but it's good enough for traces.
54 if (op
>= midgard_alu_op_iand
&&
55 op
<= midgard_alu_op_ibitcount8
)
65 case midgard_reg_mode_64
:
67 fprintf(fp
, "%"PRIi64
, consts
->i64
[c
]);
69 fprintf(fp
, "%"PRIu64
, consts
->u64
[c
]);
71 fprintf(fp
, "0x%"PRIX64
, consts
->u64
[c
]);
73 double v
= consts
->f64
[c
];
75 if (mod
& MIDGARD_FLOAT_MOD_ABS
) v
= fabs(v
);
76 if (mod
& MIDGARD_FLOAT_MOD_NEG
) v
= -v
;
82 case midgard_reg_mode_32
:
86 if (half
&& mod
== midgard_int_zero_extend
)
88 else if (half
&& mod
== midgard_int_shift
)
89 v
= (uint64_t)consts
->u32
[c
] << 32;
93 fprintf(fp
, "%"PRIi64
, v
);
94 } else if (is_uint
|| is_hex
) {
97 if (half
&& mod
== midgard_int_shift
)
98 v
= (uint64_t)consts
->u32
[c
] << 32;
102 fprintf(fp
, is_uint
? "%"PRIu64
: "0x%"PRIX64
, v
);
104 float v
= consts
->f32
[c
];
106 if (mod
& MIDGARD_FLOAT_MOD_ABS
) v
= fabsf(v
);
107 if (mod
& MIDGARD_FLOAT_MOD_NEG
) v
= -v
;
109 fprintf(fp
, "%g", v
);
113 case midgard_reg_mode_16
:
117 if (half
&& mod
== midgard_int_zero_extend
)
119 else if (half
&& mod
== midgard_int_shift
)
120 v
= (uint32_t)consts
->u16
[c
] << 16;
124 fprintf(fp
, "%d", v
);
125 } else if (is_uint
|| is_hex
) {
128 if (half
&& mod
== midgard_int_shift
)
129 v
= (uint32_t)consts
->u16
[c
] << 16;
133 fprintf(fp
, is_uint
? "%u" : "0x%X", v
);
135 float v
= _mesa_half_to_float(consts
->f16
[c
]);
137 if (mod
& MIDGARD_FLOAT_MOD_ABS
) v
= fabsf(v
);
138 if (mod
& MIDGARD_FLOAT_MOD_NEG
) v
= -v
;
140 fprintf(fp
, "%g", v
);
144 case midgard_reg_mode_8
:
145 fprintf(fp
, "0x%X", consts
->u8
[c
]);
148 fprintf(fp
, " /* %u */", mod
);
150 assert(!half
); /* No 4-bit */