Mercurial > vec
comparison gen/genlib.c @ 45:7955bed1d169 default tip
*: add preliminary floating point support
no x86 intrinsics just yet, but I did add altivec since it's
(arguably) the simplest :)
author | Paper <paper@tflc.us> |
---|---|
date | Wed, 30 Apr 2025 18:36:38 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
44:b0a3f0248ecc | 45:7955bed1d169 |
---|---|
1 /** | |
2 * vec - a tiny SIMD vector library in C99 | |
3 * | |
4 * Copyright (c) 2024-2025 Paper | |
5 * | |
6 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 * of this software and associated documentation files (the "Software"), to deal | |
8 * in the Software without restriction, including without limitation the rights | |
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 * copies of the Software, and to permit persons to whom the Software is | |
11 * furnished to do so, subject to the following conditions: | |
12 * | |
13 * The above copyright notice and this permission notice shall be included in all | |
14 * copies or substantial portions of the 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 THE | |
19 * 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 "genlib.h" | |
26 | |
27 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) | |
28 | |
29 struct strs type_str[] = { | |
30 [TYPE_INT] = {"int", "INT"}, | |
31 [TYPE_UINT] = {"uint", "UINT"}, | |
32 [TYPE_FLOAT] = {"f", "F"}, | |
33 }; | |
34 | |
35 void gen_print_vtype(int type, int bits, int size) | |
36 { | |
37 printf("v%s%dx%d", type_str[type].l, bits, size); | |
38 } | |
39 | |
40 void gen_print_stype(int type, int bits) | |
41 { | |
42 printf("vec_%s%d", type_str[type].l, bits); | |
43 } | |
44 | |
45 static void vret(int op, int type, int bits, int size) | |
46 { | |
47 gen_print_vtype(type, bits, size); | |
48 | |
49 (void)op; | |
50 } | |
51 | |
52 static void nret(int op, int type, int bits, int size) | |
53 { | |
54 printf("void"); | |
55 | |
56 (void)op, (void)type, (void)bits, (void)size; | |
57 } | |
58 | |
59 static void voneparam(int op, int type, int bits, int size) | |
60 { | |
61 gen_print_vtype(type, bits, size); | |
62 printf(" vec"); | |
63 | |
64 (void)op; | |
65 } | |
66 | |
67 static void vtwoparam(int op, int type, int bits, int size) | |
68 { | |
69 gen_print_vtype(type, bits, size); | |
70 printf(" vec1, "); | |
71 gen_print_vtype(type, bits, size); | |
72 printf(" vec2"); | |
73 | |
74 (void)op; | |
75 } | |
76 | |
77 static void vshiftparam(int op, int type, int bits, int size) | |
78 { | |
79 gen_print_vtype(type, bits, size); | |
80 printf(" vec1, "); | |
81 gen_print_vtype(TYPE_UINT, bits, size); | |
82 printf(" vec2"); | |
83 | |
84 (void)op; | |
85 } | |
86 | |
87 static void vloadparam(int op, int type, int bits, int size) | |
88 { | |
89 printf("const "); | |
90 gen_print_stype(type, bits); | |
91 printf(" x[%d]", size); | |
92 | |
93 (void)op; | |
94 } | |
95 | |
96 static void vsplatparam(int op, int type, int bits, int size) | |
97 { | |
98 gen_print_stype(type, bits); | |
99 printf(" x"); | |
100 | |
101 (void)op, (void)size; | |
102 } | |
103 | |
104 static void vstoreparam(int op, int type, int bits, int size) | |
105 { | |
106 gen_print_vtype(type, bits, size); | |
107 printf(" vec, "); | |
108 gen_print_stype(type, bits); | |
109 printf(" x[%d]", size); | |
110 | |
111 (void)op; | |
112 } | |
113 | |
114 struct op_info ops[] = { | |
115 [OP_SPLAT] = {"SPLAT", "splat", vret, vsplatparam}, | |
116 [OP_LOAD_ALIGNED] = {"LOAD_ALIGNED", "load_aligned", vret, vloadparam}, | |
117 [OP_LOAD] = {"LOAD", "load", vret, vloadparam}, | |
118 [OP_STORE_ALIGNED] = {"STORE_ALIGNED", "store_aligned", nret, vstoreparam}, | |
119 [OP_STORE] = {"STORE", "store", nret, vstoreparam}, | |
120 [OP_ADD] = {"ADD", "add", vret, vtwoparam}, | |
121 [OP_SUB] = {"SUB", "sub", vret, vtwoparam}, | |
122 [OP_MUL] = {"MUL", "mul", vret, vtwoparam}, | |
123 [OP_DIV] = {"DIV", "div", vret, vtwoparam}, | |
124 [OP_MOD] = {"MOD", "mod", vret, vtwoparam}, | |
125 [OP_AVG] = {"AVG", "avg", vret, vtwoparam}, | |
126 [OP_AND] = {"AND", "and", vret, vtwoparam}, | |
127 [OP_OR] = {"OR", "or", vret, vtwoparam}, | |
128 [OP_XOR] = {"XOR", "xor", vret, vtwoparam}, | |
129 [OP_NOT] = {"NOT", "not", vret, voneparam}, | |
130 [OP_CMPLT] = {"CMPLT", "cmplt", vret, vtwoparam}, | |
131 [OP_CMPEQ] = {"CMPEQ", "cmpeq", vret, vtwoparam}, | |
132 [OP_CMPGT] = {"CMPGT", "cmpgt", vret, vtwoparam}, | |
133 [OP_CMPLE] = {"CMPLE", "cmple", vret, vtwoparam}, | |
134 [OP_CMPGE] = {"CMPGE", "cmpge", vret, vtwoparam}, | |
135 [OP_MIN] = {"MIN", "min", vret, vtwoparam}, | |
136 [OP_MAX] = {"MAX", "max", vret, vtwoparam}, | |
137 [OP_RSHIFT] = {"RSHIFT", "rshift", vret, vshiftparam}, | |
138 [OP_LRSHIFT] = {"LRSHIFT", "lrshift", vret, vshiftparam}, | |
139 [OP_LSHIFT] = {"LSHIFT", "lshift", vret, vshiftparam}, | |
140 }; | |
141 | |
142 struct op_info *gen_op_info(int op) | |
143 { | |
144 return &ops[op]; | |
145 } | |
146 | |
147 /* okay */ | |
148 extern int (*genlib_test(void))[(ARRAY_SIZE(ops) == OP_FINAL_) ? 1 : -2]; | |
149 | |
150 int op_impl_check_always(int op, int type, int bits, int size) | |
151 { | |
152 return 1; | |
153 | |
154 (void)op, (void)type, (void)bits, (void)size; | |
155 } | |
156 | |
157 static inline int verify_op(int op, int type) | |
158 { | |
159 switch (op) { | |
160 case OP_AND: | |
161 case OP_XOR: | |
162 case OP_OR: | |
163 case OP_NOT: | |
164 case OP_RSHIFT: | |
165 case OP_LSHIFT: | |
166 case OP_LRSHIFT: | |
167 /* these operations make no sense for floating point */ | |
168 if (type == TYPE_FLOAT) | |
169 return 0; | |
170 break; | |
171 } | |
172 | |
173 return 1; | |
174 } | |
175 | |
176 /* XXX: would it be faster to unroll literally everything instead of defining everything, | |
177 * and then unpacking it all? */ | |
178 static const char *header_tmpl = | |
179 "/**\n" | |
180 " * vec - a tiny SIMD vector library in C99\n" | |
181 " * \n" | |
182 " * Copyright (c) 2024-2025 Paper\n" | |
183 " * \n" | |
184 " * Permission is hereby granted, free of charge, to any person obtaining a copy\n" | |
185 " * of this software and associated documentation files (the \"Software\"), to deal\n" | |
186 " * in the Software without restriction, including without limitation the rights\n" | |
187 " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n" | |
188 " * copies of the Software, and to permit persons to whom the Software is\n" | |
189 " * furnished to do so, subject to the following conditions:\n" | |
190 " * \n" | |
191 " * The above copyright notice and this permission notice shall be included in all\n" | |
192 " * copies or substantial portions of the Software.\n" | |
193 " * \n" | |
194 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" | |
195 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" | |
196 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n" | |
197 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" | |
198 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" | |
199 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" | |
200 " * SOFTWARE.\n" | |
201 "**/\n" | |
202 "\n" | |
203 "/* This file is automatically generated! Do not edit it directly!\n" | |
204 " * Edit the code that generates it in utils/gen%s.c --paper */\n" | |
205 "\n" | |
206 "/* ------------------------------------------------------------------------ */\n" | |
207 "/* PREPROCESSOR HELL INCOMING */\n\n"; | |
208 | |
209 int gen(struct op_impl op_impl[OP_FINAL_], const char *name) | |
210 { | |
211 static struct { | |
212 int type, bits, size; | |
213 } types[] = { | |
214 #define INT_TYPE(bits, size) {TYPE_INT, bits, size}, {TYPE_UINT, bits, size} | |
215 | |
216 INT_TYPE(8, 2), | |
217 INT_TYPE(8, 4), | |
218 INT_TYPE(8, 8), | |
219 INT_TYPE(8, 16), | |
220 INT_TYPE(8, 32), | |
221 INT_TYPE(8, 64), | |
222 | |
223 INT_TYPE(16, 2), | |
224 INT_TYPE(16, 4), | |
225 INT_TYPE(16, 8), | |
226 INT_TYPE(16, 16), | |
227 INT_TYPE(16, 32), | |
228 | |
229 INT_TYPE(32, 2), | |
230 INT_TYPE(32, 4), | |
231 INT_TYPE(32, 8), | |
232 INT_TYPE(32, 16), | |
233 | |
234 INT_TYPE(64, 2), | |
235 INT_TYPE(64, 4), | |
236 INT_TYPE(64, 8), | |
237 | |
238 #undef INT_TYPE | |
239 | |
240 /* float */ | |
241 {TYPE_FLOAT, 32, 2}, | |
242 {TYPE_FLOAT, 32, 4}, | |
243 {TYPE_FLOAT, 32, 8}, | |
244 {TYPE_FLOAT, 32, 16}, | |
245 | |
246 /* double */ | |
247 {TYPE_FLOAT, 64, 2}, | |
248 {TYPE_FLOAT, 64, 4}, | |
249 {TYPE_FLOAT, 64, 8}, | |
250 }; | |
251 int op; | |
252 size_t s; | |
253 | |
254 printf(header_tmpl, name); | |
255 | |
256 for (s = 0; s < ARRAY_SIZE(types); s++) { | |
257 for (op = 0; op < OP_FINAL_; op++) { | |
258 if (!op_impl[op].pbody) | |
259 continue; /* What? */ | |
260 | |
261 if (op_impl[op].check && !op_impl[op].check(op, types[s].type, types[s].bits, types[s].size)) | |
262 continue; | |
263 | |
264 if (!verify_op(op, types[s].type)) | |
265 continue; | |
266 | |
267 printf("#if !defined(V%s%dx%d_%s_DEFINED)", type_str[types[s].type].u, types[s].bits, types[s].size, ops[op].u); | |
268 | |
269 if (op_impl[op].ppcheck) { | |
270 printf(" \\\n\t && ("); | |
271 op_impl[op].ppcheck(op, types[s].type, types[s].bits, types[s].size); | |
272 printf(")"); | |
273 } | |
274 | |
275 puts(""); | |
276 | |
277 printf("VEC_FUNC_IMPL "); | |
278 ops[op].pret(op, types[s].type, types[s].bits, types[s].size); | |
279 printf(" "); | |
280 gen_print_vtype(types[s].type, types[s].bits, types[s].size); | |
281 printf("_%s(", ops[op].l); | |
282 ops[op].pparam(op, types[s].type, types[s].bits, types[s].size); | |
283 puts(")\n{"); | |
284 | |
285 op_impl[op].pbody(op, types[s].type, types[s].bits, types[s].size); | |
286 | |
287 puts("}"); | |
288 | |
289 printf("# define V%s%dx%d_%s_DEFINED\n", type_str[types[s].type].u, types[s].bits, types[s].size, ops[op].u); | |
290 puts("#endif"); | |
291 } | |
292 } | |
293 | |
294 } |