1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
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
13 * Software.
14 *
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 THE
18 * 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
21 * SOFTWARE.
22 */
23
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27
28 #include "rogue.h"
29 #include "util/macros.h"
30
31 /**
32 * \file rogue_constreg.c
33 *
34 * \brief Contains functions to find and allocate constant register values.
35 */
36
37 /**
38 * \brief Mapping of constant register values and their indices.
39 */
40 typedef struct rogue_constreg_map {
41 uint32_t value;
42 unsigned index;
43 } rogue_constreg_map;
44
45 #define CONSTREG(VALUE, INDEX) \
46 { \
47 .value = (VALUE), .index = (INDEX), \
48 }
49
50 /**
51 * \brief Constant register values (sorted for bsearch).
52 */
53 static const rogue_constreg_map const_regs[] = {
54 CONSTREG(0x00000000U, 0U), /* 0 (INT32) / 0.0 (Float) */
55 CONSTREG(0x00000001U, 1U), /* 1 (INT32) */
56 CONSTREG(0x00000002U, 2U), /* 2 (INT32) */
57 CONSTREG(0x00000003U, 3U), /* 3 (INT32) */
58 CONSTREG(0x00000004U, 4U), /* 4 (INT32) */
59 CONSTREG(0x00000005U, 5U), /* 5 (INT32) */
60 CONSTREG(0x00000006U, 6U), /* 6 (INT32) */
61 CONSTREG(0x00000007U, 7U), /* 7 (INT32) */
62 CONSTREG(0x00000008U, 8U), /* 8 (INT32) */
63 CONSTREG(0x00000009U, 9U), /* 9 (INT32) */
64 CONSTREG(0x0000000aU, 10U), /* 10 (INT32) */
65 CONSTREG(0x0000000bU, 11U), /* 11 (INT32) */
66 CONSTREG(0x0000000cU, 12U), /* 12 (INT32) */
67 CONSTREG(0x0000000dU, 13U), /* 13 (INT32) */
68 CONSTREG(0x0000000eU, 14U), /* 14 (INT32) */
69 CONSTREG(0x0000000fU, 15U), /* 15 (INT32) */
70 CONSTREG(0x00000010U, 16U), /* 16 (INT32) */
71 CONSTREG(0x00000011U, 17U), /* 17 (INT32) */
72 CONSTREG(0x00000012U, 18U), /* 18 (INT32) */
73 CONSTREG(0x00000013U, 19U), /* 19 (INT32) */
74 CONSTREG(0x00000014U, 20U), /* 20 (INT32) */
75 CONSTREG(0x00000015U, 21U), /* 21 (INT32) */
76 CONSTREG(0x00000016U, 22U), /* 22 (INT32) */
77 CONSTREG(0x00000017U, 23U), /* 23 (INT32) */
78 CONSTREG(0x00000018U, 24U), /* 24 (INT32) */
79 CONSTREG(0x00000019U, 25U), /* 25 (INT32) */
80 CONSTREG(0x0000001aU, 26U), /* 26 (INT32) */
81 CONSTREG(0x0000001bU, 27U), /* 27 (INT32) */
82 CONSTREG(0x0000001cU, 28U), /* 28 (INT32) */
83 CONSTREG(0x0000001dU, 29U), /* 29 (INT32) */
84 CONSTREG(0x0000001eU, 30U), /* 30 (INT32) */
85 CONSTREG(0x0000001fU, 31U), /* 31 (INT32) */
86 CONSTREG(0x0000007fU, 147U), /* 127 (INT32) */
87
88 CONSTREG(0x37800000U, 134U), /* 1.0f/65536f */
89 CONSTREG(0x38000000U, 135U), /* 1.0f/32768f */
90 CONSTREG(0x38800000U, 88U), /* float(2^-14) */
91 CONSTREG(0x39000000U, 87U), /* float(2^-13) */
92 CONSTREG(0x39800000U, 86U), /* float(2^-12) */
93 CONSTREG(0x3a000000U, 85U), /* float(2^-11) */
94 CONSTREG(0x3a800000U, 84U), /* float(2^-10) */
95 CONSTREG(0x3b000000U, 83U), /* float(2^-9) */
96 CONSTREG(0x3b4d2e1cU, 136U), /* 0.0031308f */
97 CONSTREG(0x3b800000U, 82U), /* float(2^-8) */
98 CONSTREG(0x3c000000U, 81U), /* float(2^-7) */
99 CONSTREG(0x3c800000U, 80U), /* float(2^-6) */
100 CONSTREG(0x3d000000U, 79U), /* float(2^-5) */
101 CONSTREG(0x3d25aee6U, 156U), /* 0.04045f */
102 CONSTREG(0x3d6147aeU, 140U), /* 0.055f */
103 CONSTREG(0x3d800000U, 78U), /* float(2^-4) */
104 CONSTREG(0x3d9e8391U, 157U), /* 1.0f/12.92f */
105 CONSTREG(0x3e000000U, 77U), /* float(2^-3) */
106 CONSTREG(0x3e2aaaabU, 153U), /* 1/6 */
107 CONSTREG(0x3e800000U, 76U), /* float(2^-2) */
108 CONSTREG(0x3e9a209bU, 145U), /* Log_10(2) */
109 CONSTREG(0x3ea2f983U, 128U), /* Float 1/PI */
110 CONSTREG(0x3eaaaaabU, 152U), /* 1/3 */
111 CONSTREG(0x3ebc5ab2U, 90U), /* 1/e */
112 CONSTREG(0x3ed55555U, 138U), /* 1.0f/2.4f */
113 CONSTREG(0x3f000000U, 75U), /* float(2^-1) */
114 CONSTREG(0x3f22f983U, 129U), /* Float 2/PI */
115 CONSTREG(0x3f317218U, 146U), /* Log_e(2) */
116 CONSTREG(0x3f3504f3U, 92U), /* Float 1/SQRT(2) */
117 CONSTREG(0x3f490fdbU, 93U), /* Float PI/4 */
118 CONSTREG(0x3f72a76fU, 158U), /* 1.0f/1.055f */
119 CONSTREG(0x3f800000U, 64U), /* 1.0f */
120 CONSTREG(0x3f860a92U, 151U), /* Pi/3 */
121 CONSTREG(0x3f870a3dU, 139U), /* 1.055f */
122 CONSTREG(0x3fa2f983U, 130U), /* Float 4/PI */
123 CONSTREG(0x3fb504f3U, 91U), /* Float SQRT(2) */
124 CONSTREG(0x3fb8aa3bU, 155U), /* Log_2(e) */
125 CONSTREG(0x3fc90fdbU, 94U), /* Float PI/2 */
126 CONSTREG(0x40000000U, 65U), /* float(2^1) */
127 CONSTREG(0x4019999aU, 159U), /* 2.4f */
128 CONSTREG(0x402df854U, 89U), /* e */
129 CONSTREG(0x40490fdbU, 95U), /* Float PI */
130 CONSTREG(0x40549a78U, 154U), /* Log_2(10) */
131 CONSTREG(0x40800000U, 66U), /* float(2^2) */
132 CONSTREG(0x40c90fdbU, 131U), /* Float 2*PI */
133 CONSTREG(0x41000000U, 67U), /* float(2^3) */
134 CONSTREG(0x41490fdbU, 132U), /* Float 4*PI */
135 CONSTREG(0x414eb852U, 137U), /* 12.92f */
136 CONSTREG(0x41800000U, 68U), /* float(2^4) */
137 CONSTREG(0x41c90fdbU, 133U), /* Float 8*PI */
138 CONSTREG(0x42000000U, 69U), /* float(2^5) */
139 CONSTREG(0x42800000U, 70U), /* float(2^6) */
140 CONSTREG(0x43000000U, 71U), /* float(2^7) */
141 CONSTREG(0x43800000U, 72U), /* float(2^8) */
142 CONSTREG(0x44000000U, 73U), /* float(2^9) */
143 CONSTREG(0x44800000U, 74U), /* float(2^10) */
144 CONSTREG(0x4b000000U, 149U), /* 2^23 */
145 CONSTREG(0x4b800000U, 150U), /* 2^24 */
146 CONSTREG(0x7f7fffffU, 148U), /* FLT_MAX */
147 CONSTREG(0x7f800000U, 142U), /* Infinity */
148 CONSTREG(0x7fff7fffU, 144U), /* ARGB1555 mask */
149 CONSTREG(0x80000000U, 141U), /* -0.0f */
150 CONSTREG(0xffffffffU, 143U), /* -1 */
151 };
152
153 #undef CONSTREG
154
155 /**
156 * \brief Comparison function for bsearch() to support rogue_constreg_map.
157 *
158 * \param[in] lhs The left hand side of the comparison.
159 * \param[in] rhs The right hand side of the comparison.
160 * \return 0 if (lhs == rhs), -1 if (lhs < rhs), 1 if (lhs > rhs).
161 */
constreg_cmp(const void * lhs,const void * rhs)162 static int constreg_cmp(const void *lhs, const void *rhs)
163 {
164 const rogue_constreg_map *l = lhs;
165 const rogue_constreg_map *r = rhs;
166
167 if (l->value < r->value)
168 return -1;
169 else if (l->value > r->value)
170 return 1;
171
172 return 0;
173 }
174
175 /**
176 * \brief Determines whether a given value exists in a constant register.
177 *
178 * \param[in] imm The immediate value required.
179 * \return The index of the constant register containing the value, or
180 * ROGUE_NO_CONST_REG if the value is not found.
181 */
182 PUBLIC
rogue_constreg_lookup(rogue_imm_t imm)183 unsigned rogue_constreg_lookup(rogue_imm_t imm)
184 {
185 rogue_constreg_map constreg_target = {
186 .value = imm.u32,
187 };
188 const rogue_constreg_map *constreg;
189
190 constreg = bsearch(&constreg_target,
191 const_regs,
192 ARRAY_SIZE(const_regs),
193 sizeof(rogue_constreg_map),
194 constreg_cmp);
195 if (!constreg)
196 return ROGUE_NO_CONST_REG;
197
198 return constreg->index;
199 }
200