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
32void
33mir_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        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 &&
56                            op <= midgard_alu_op_ipopcnt)
57                                is_hex = true;
58                        else
59                                is_sint = true;
60                }
61	}
62
63        if (half)
64                reg_mode--;
65
66        switch (reg_mode) {
67        case midgard_reg_mode_64:
68                if (is_sint) {
69                        fprintf(fp, "%"PRIi64, consts->i64[c]);
70                } else if (is_uint) {
71                        fprintf(fp, "%"PRIu64, consts->u64[c]);
72                } else if (is_hex) {
73                        fprintf(fp, "0x%"PRIX64, consts->u64[c]);
74                } else {
75                        double v = consts->f64[c];
76
77                        if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabs(v);
78                        if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
79
80                        printf("%g", v);
81                }
82                break;
83
84        case midgard_reg_mode_32:
85                if (is_sint) {
86                        int64_t v;
87
88                        if (half && mod == midgard_int_zero_extend)
89                                v = consts->u32[c];
90                        else if (half && mod == midgard_int_left_shift)
91                                v = (uint64_t)consts->u32[c] << 32;
92                        else
93                                v = consts->i32[c];
94
95                        fprintf(fp, "%"PRIi64, v);
96                } else if (is_uint || is_hex) {
97                        uint64_t v;
98
99                        if (half && mod == midgard_int_left_shift)
100                                v = (uint64_t)consts->u32[c] << 32;
101                        else
102                                v = consts->u32[c];
103
104                        fprintf(fp, is_uint ? "%"PRIu64 : "0x%"PRIX64, v);
105                } else {
106                        float v = consts->f32[c];
107
108                        if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);
109                        if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
110
111                        fprintf(fp, "%g", v);
112                }
113                break;
114
115        case midgard_reg_mode_16:
116                if (is_sint) {
117                        int32_t v;
118
119                        if (half && mod == midgard_int_zero_extend)
120                                v = consts->u16[c];
121                        else if (half && mod == midgard_int_left_shift)
122                                v = (uint32_t)consts->u16[c] << 16;
123                        else
124                                v = consts->i16[c];
125
126                        fprintf(fp, "%d", v);
127                } else if (is_uint || is_hex) {
128                        uint32_t v;
129
130                        if (half && mod == midgard_int_left_shift)
131                                v = (uint32_t)consts->u16[c] << 16;
132                        else
133                                v = consts->u16[c];
134
135                        fprintf(fp, is_uint ? "%u" : "0x%X", v);
136                } else {
137                        float v = _mesa_half_to_float(consts->f16[c]);
138
139                        if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);
140                        if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;
141
142                        fprintf(fp, "%g", v);
143                }
144                break;
145
146        case midgard_reg_mode_8:
147                fprintf(fp, "0x%X", consts->u8[c]);
148
149                if (mod)
150                        fprintf(fp, " /* %u */", mod);
151
152                assert(!half); /* No 4-bit */
153
154                break;
155        }
156}
157
158static char *outmod_names_float[4] = {
159        "",
160        ".clamp_0_inf",
161        ".clamp_m1_1",
162        ".clamp_0_1"
163};
164
165static char *outmod_names_int[4] = {
166        ".ssat",
167        ".usat",
168        ".keeplo",
169        ".keephi"
170};
171
172void
173mir_print_outmod(FILE *fp, unsigned outmod, bool is_int)
174{
175        fprintf(fp, "%s", is_int ? outmod_names_int[outmod] :
176                outmod_names_float[outmod]);
177}
178