Mercurial > vec
comparison utils/gendouble.c @ 41:c6e0df09b86f
*: performance improvements with old GCC, reimplement altivec
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 28 Apr 2025 16:31:59 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 40:55cadb1fac4b | 41:c6e0df09b86f |
|---|---|
| 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 /* Use this file to generate include/vec/impl/double.h !! | |
| 26 * | |
| 27 * `gcc -o gendouble gendouble.c` */ | |
| 28 | |
| 29 #include <stdio.h> | |
| 30 #include <stdlib.h> | |
| 31 #include <string.h> | |
| 32 #include <ctype.h> | |
| 33 | |
| 34 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) | |
| 35 | |
| 36 /* XXX: would it be faster to unroll literally everything instead of defining everything, | |
| 37 * and then unpacking it all? */ | |
| 38 static const char *header = | |
| 39 "/**\n" | |
| 40 " * vec - a tiny SIMD vector library in C99\n" | |
| 41 " * \n" | |
| 42 " * Copyright (c) 2024-2025 Paper\n" | |
| 43 " * \n" | |
| 44 " * Permission is hereby granted, free of charge, to any person obtaining a copy\n" | |
| 45 " * of this software and associated documentation files (the \"Software\"), to deal\n" | |
| 46 " * in the Software without restriction, including without limitation the rights\n" | |
| 47 " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n" | |
| 48 " * copies of the Software, and to permit persons to whom the Software is\n" | |
| 49 " * furnished to do so, subject to the following conditions:\n" | |
| 50 " * \n" | |
| 51 " * The above copyright notice and this permission notice shall be included in all\n" | |
| 52 " * copies or substantial portions of the Software.\n" | |
| 53 " * \n" | |
| 54 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" | |
| 55 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" | |
| 56 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n" | |
| 57 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" | |
| 58 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" | |
| 59 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" | |
| 60 " * SOFTWARE.\n" | |
| 61 "**/\n" | |
| 62 "\n" | |
| 63 "/* This file is automatically generated! Do not edit it directly!\n" | |
| 64 " * Edit the code that generates it in utils/gengeneric.c --paper */\n" | |
| 65 "\n" | |
| 66 "#ifndef VEC_IMPL_DOUBLE_H_\n" | |
| 67 "#define VEC_IMPL_DOUBLE_H_\n" | |
| 68 "\n" | |
| 69 "#define VEC_DOUBLE_SPLAT(sign, bits, size, halfsize) \\\n" | |
| 70 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_splat(vec_##sign##int##bits x) \\\n" | |
| 71 " { \\\n" | |
| 72 " v##sign##int##bits##x##size vec; \\\n" | |
| 73 " \\\n" | |
| 74 " vec.generic[0] = v##sign##int##bits##x##halfsize##_splat(x); \\\n" | |
| 75 " vec.generic[1] = v##sign##int##bits##x##halfsize##_splat(x); \\\n" | |
| 76 " \\\n" | |
| 77 " return vec; \\\n" | |
| 78 " }\n" | |
| 79 "\n" | |
| 80 "#define VEC_DOUBLE_LOAD_EX(name, sign, bits, size, halfsize) \\\n" | |
| 81 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_##name(const vec_##sign##int##bits x[size]) \\\n" | |
| 82 " { \\\n" | |
| 83 " v##sign##int##bits##x##size vec; \\\n" | |
| 84 " \\\n" | |
| 85 " vec.generic[0] = v##sign##int##bits##x##halfsize##_##name(x); \\\n" | |
| 86 " vec.generic[1] = v##sign##int##bits##x##halfsize##_##name(x + halfsize); \\\n" | |
| 87 " \\\n" | |
| 88 " return vec; \\\n" | |
| 89 " }\n" | |
| 90 "\n" | |
| 91 "#define VEC_DOUBLE_LOAD(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load, sign, bits, size, halfsize)\n" | |
| 92 "#define VEC_DOUBLE_LOAD_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load_aligned, sign, bits, size, halfsize)\n" | |
| 93 "\n" | |
| 94 "#define VEC_DOUBLE_STORE_EX(name, sign, bits, size, halfsize) \\\n" | |
| 95 " VEC_FUNC_IMPL void v##sign##int##bits##x##size##_##name(v##sign##int##bits##x##size vec, vec_##sign##int##bits x[size]) \\\n" | |
| 96 " { \\\n" | |
| 97 " v##sign##int##bits##x##halfsize##_##name(vec.generic[0], x); \\\n" | |
| 98 " v##sign##int##bits##x##halfsize##_##name(vec.generic[1], x + halfsize); \\\n" | |
| 99 " }\n" | |
| 100 "\n" | |
| 101 "#define VEC_DOUBLE_STORE(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store, sign, bits, size, halfsize)\n" | |
| 102 "#define VEC_DOUBLE_STORE_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store_aligned, sign, bits, size, halfsize)\n" | |
| 103 "\n" | |
| 104 "#define VEC_DOUBLE_OP(name, sign, bits, size, halfsize, secondsign) \\\n" | |
| 105 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_##name(v##sign##int##bits##x##size vec1, v##secondsign##int##bits##x##size vec2) \\\n" | |
| 106 " { \\\n" | |
| 107 " vec1.generic[0] = v##sign##int##bits##x##halfsize##_##name(vec1.generic[0], vec2.generic[0]); \\\n" | |
| 108 " vec1.generic[1] = v##sign##int##bits##x##halfsize##_##name(vec1.generic[1], vec2.generic[1]); \\\n" | |
| 109 " \\\n" | |
| 110 " return vec1; \\\n" | |
| 111 " }\n" | |
| 112 "\n" | |
| 113 "#define VEC_DOUBLE_ADD(sign, bits, size, halfsize) VEC_DOUBLE_OP(add, sign, bits, size, halfsize, sign)\n" | |
| 114 "#define VEC_DOUBLE_SUB(sign, bits, size, halfsize) VEC_DOUBLE_OP(sub, sign, bits, size, halfsize, sign)\n" | |
| 115 "#define VEC_DOUBLE_MUL(sign, bits, size, halfsize) VEC_DOUBLE_OP(mul, sign, bits, size, halfsize, sign)\n" | |
| 116 "#define VEC_DOUBLE_DIV(sign, bits, size, halfsize) VEC_DOUBLE_OP(div, sign, bits, size, halfsize, sign)\n" | |
| 117 "#define VEC_DOUBLE_MOD(sign, bits, size, halfsize) VEC_DOUBLE_OP(mod, sign, bits, size, halfsize, sign)\n" | |
| 118 "#define VEC_DOUBLE_AVG(sign, bits, size, halfsize) VEC_DOUBLE_OP(avg, sign, bits, size, halfsize, sign)\n" | |
| 119 "#define VEC_DOUBLE_LSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lshift, sign, bits, size, halfsize, u)\n" | |
| 120 "#define VEC_DOUBLE_RSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(rshift, sign, bits, size, halfsize, u)\n" | |
| 121 "#define VEC_DOUBLE_LRSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lrshift, sign, bits, size, halfsize, u)\n" | |
| 122 "#define VEC_DOUBLE_AND(sign, bits, size, halfsize) VEC_DOUBLE_OP(and, sign, bits, size, halfsize, sign)\n" | |
| 123 "#define VEC_DOUBLE_OR(sign, bits, size, halfsize) VEC_DOUBLE_OP(or, sign, bits, size, halfsize, sign)\n" | |
| 124 "#define VEC_DOUBLE_XOR(sign, bits, size, halfsize) VEC_DOUBLE_OP(xor, sign, bits, size, halfsize, sign)\n" | |
| 125 "#define VEC_DOUBLE_MIN(sign, bits, size, halfsize) VEC_DOUBLE_OP(min, sign, bits, size, halfsize, sign)\n" | |
| 126 "#define VEC_DOUBLE_MAX(sign, bits, size, halfsize) VEC_DOUBLE_OP(max, sign, bits, size, halfsize, sign)\n" | |
| 127 "#define VEC_DOUBLE_CMPLT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmplt, sign, bits, size, halfsize, sign)\n" | |
| 128 "#define VEC_DOUBLE_CMPLE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmple, sign, bits, size, halfsize, sign)\n" | |
| 129 "#define VEC_DOUBLE_CMPEQ(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpeq, sign, bits, size, halfsize, sign)\n" | |
| 130 "#define VEC_DOUBLE_CMPGE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpge, sign, bits, size, halfsize, sign)\n" | |
| 131 "#define VEC_DOUBLE_CMPGT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpgt, sign, bits, size, halfsize, sign)\n" | |
| 132 "\n" | |
| 133 "#define VEC_DOUBLE_NOT(sign, bits, size, halfsize) \\\n" | |
| 134 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_not(v##sign##int##bits##x##size vec) \\\n" | |
| 135 " { \\\n" | |
| 136 " vec.generic[0] = v##sign##int##bits##x##halfsize##_not(vec.generic[0]); \\\n" | |
| 137 " vec.generic[1] = v##sign##int##bits##x##halfsize##_not(vec.generic[1]); \\\n" | |
| 138 " \\\n" | |
| 139 " return vec; \\\n" | |
| 140 " }\n" | |
| 141 "\n" | |
| 142 "#endif /* VEC_IMPL_DOUBLE_H_ */ \n" | |
| 143 "\n" | |
| 144 "/* ------------------------------------------------------------------------ */\n" | |
| 145 "/* PREPROCESSOR HELL INCOMING */\n" | |
| 146 ""; | |
| 147 | |
| 148 static const char *footer = | |
| 149 "" /* nothing */; | |
| 150 | |
| 151 /* ------------------------------------------------------------------------ */ | |
| 152 | |
| 153 static void print_generic_dbl_op(const char *op, int is_signed, int bits, int size) | |
| 154 { | |
| 155 printf( | |
| 156 "#if !defined(V%sINT%dx%d_%s_DEFINED) && defined(V%sINT%dx%d_%s_DEFINED)\n" | |
| 157 "VEC_DOUBLE_%s(%s, %d, %d, %d)\n" | |
| 158 "# define V%sINT%dx%d_%s_DEFINED\n" | |
| 159 "#endif\n\n", | |
| 160 (is_signed ? "" : "U"), bits, size, op, (is_signed ? "" : "U"), bits, size / 2, op, | |
| 161 op, (is_signed ? "/* nothing */" : "u"), bits, size, size / 2, | |
| 162 (is_signed ? "" : "U"), bits, size, op); | |
| 163 } | |
| 164 | |
| 165 typedef void (*print_op_spec)(const char *op, int is_signed, int bits, int size); | |
| 166 | |
| 167 static inline void print_ops(int is_signed, int bits, int size, print_op_spec print_op) | |
| 168 { | |
| 169 /* all supported operations here */ | |
| 170 static const char *ops[] = { | |
| 171 "SPLAT", | |
| 172 "LOAD_ALIGNED", | |
| 173 "LOAD", | |
| 174 "STORE_ALIGNED", | |
| 175 "STORE", | |
| 176 "ADD", | |
| 177 "SUB", | |
| 178 "MUL", | |
| 179 "DIV", | |
| 180 "MOD", | |
| 181 "AVG", | |
| 182 "AND", | |
| 183 "OR", | |
| 184 "XOR", | |
| 185 "NOT", | |
| 186 "CMPLT", | |
| 187 "CMPEQ", | |
| 188 "CMPGT", | |
| 189 "CMPLE", /* these two must be after CMPLT and CMPGT respectfully, */ | |
| 190 "CMPGE", /* because their definitions call those functions */ | |
| 191 "MIN", | |
| 192 "MAX", | |
| 193 "RSHIFT", | |
| 194 "LRSHIFT", | |
| 195 "LSHIFT", | |
| 196 NULL, | |
| 197 }; | |
| 198 int i; | |
| 199 | |
| 200 printf("\n\n/* v%sint%dx%d */\n\n", (is_signed ? "u" : ""), bits, size); | |
| 201 | |
| 202 for (i = 0; ops[i]; i++) | |
| 203 print_op(ops[i], is_signed, bits, size); | |
| 204 } | |
| 205 | |
| 206 int main(void) | |
| 207 { | |
| 208 static struct { | |
| 209 int bits, size; | |
| 210 print_op_spec print_op; | |
| 211 } defs[] = { | |
| 212 /* -- 8-bit */ | |
| 213 {8, 4, print_generic_dbl_op}, | |
| 214 {8, 8, print_generic_dbl_op}, | |
| 215 {8, 16, print_generic_dbl_op}, | |
| 216 {8, 32, print_generic_dbl_op}, | |
| 217 {8, 64, print_generic_dbl_op}, | |
| 218 | |
| 219 /* -- 16-bit */ | |
| 220 {16, 4, print_generic_dbl_op}, | |
| 221 {16, 8, print_generic_dbl_op}, | |
| 222 {16, 16, print_generic_dbl_op}, | |
| 223 {16, 32, print_generic_dbl_op}, | |
| 224 | |
| 225 /* -- 32-bit */ | |
| 226 {32, 4, print_generic_dbl_op}, | |
| 227 {32, 8, print_generic_dbl_op}, | |
| 228 {32, 16, print_generic_dbl_op}, | |
| 229 | |
| 230 /* -- 64-bit */ | |
| 231 {64, 4, print_generic_dbl_op}, | |
| 232 {64, 8, print_generic_dbl_op}, | |
| 233 }; | |
| 234 int i; | |
| 235 | |
| 236 puts(header); | |
| 237 | |
| 238 for (i = 0; i < ARRAY_SIZE(defs); i++) { | |
| 239 print_ops(1, defs[i].bits, defs[i].size, defs[i].print_op); | |
| 240 print_ops(0, defs[i].bits, defs[i].size, defs[i].print_op); | |
| 241 } | |
| 242 | |
| 243 puts(footer); | |
| 244 } |
