Mercurial > vec
comparison gen/gendouble.c @ 46:31cee67540b5
f32/f64: add floor, ceil, round, and trunc operations
we also need saturated add sub mul etc
| author | Paper <paper@tflc.us> |
|---|---|
| date | Sat, 09 Aug 2025 15:55:59 -0400 |
| parents | 7955bed1d169 |
| children |
comparison
equal
deleted
inserted
replaced
| 45:7955bed1d169 | 46:31cee67540b5 |
|---|---|
| 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 * SOFTWARE. | 22 * SOFTWARE. |
| 23 **/ | 23 **/ |
| 24 | 24 |
| 25 #include "genlib.h" | 25 #include "genlib.h" |
| 26 | |
| 27 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) | |
| 28 | |
| 29 /* XXX: would it be faster to unroll literally everything instead of defining everything, | |
| 30 * and then unpacking it all? */ | |
| 31 static const char *header = | |
| 32 "/**\n" | |
| 33 " * vec - a tiny SIMD vector library in C99\n" | |
| 34 " * \n" | |
| 35 " * Copyright (c) 2024-2025 Paper\n" | |
| 36 " * \n" | |
| 37 " * Permission is hereby granted, free of charge, to any person obtaining a copy\n" | |
| 38 " * of this software and associated documentation files (the \"Software\"), to deal\n" | |
| 39 " * in the Software without restriction, including without limitation the rights\n" | |
| 40 " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n" | |
| 41 " * copies of the Software, and to permit persons to whom the Software is\n" | |
| 42 " * furnished to do so, subject to the following conditions:\n" | |
| 43 " * \n" | |
| 44 " * The above copyright notice and this permission notice shall be included in all\n" | |
| 45 " * copies or substantial portions of the Software.\n" | |
| 46 " * \n" | |
| 47 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" | |
| 48 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" | |
| 49 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n" | |
| 50 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" | |
| 51 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" | |
| 52 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" | |
| 53 " * SOFTWARE.\n" | |
| 54 "**/\n" | |
| 55 "\n" | |
| 56 "/* This file is automatically generated! Do not edit it directly!\n" | |
| 57 " * Edit the code that generates it in utils/gendbl.c --paper */\n" | |
| 58 "\n" | |
| 59 "#ifndef VEC_IMPL_DOUBLE_H_\n" | |
| 60 "#define VEC_IMPL_DOUBLE_H_\n" | |
| 61 "\n" | |
| 62 "#define VEC_DOUBLE_SPLAT(sign, bits, size, halfsize) \\\n" | |
| 63 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_splat(vec_##sign##int##bits x) \\\n" | |
| 64 " { \\\n" | |
| 65 " v##sign##int##bits##x##size vec; \\\n" | |
| 66 " \\\n" | |
| 67 " vec.dbl[0] = v##sign##int##bits##x##halfsize##_splat(x); \\\n" | |
| 68 " vec.dbl[1] = v##sign##int##bits##x##halfsize##_splat(x); \\\n" | |
| 69 " \\\n" | |
| 70 " return vec; \\\n" | |
| 71 " }\n" | |
| 72 "\n" | |
| 73 "#define VEC_DOUBLE_LOAD_EX(name, sign, bits, size, halfsize) \\\n" | |
| 74 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_##name(const vec_##sign##int##bits x[size]) \\\n" | |
| 75 " { \\\n" | |
| 76 " v##sign##int##bits##x##size vec; \\\n" | |
| 77 " \\\n" | |
| 78 " vec.dbl[0] = v##sign##int##bits##x##halfsize##_##name(x); \\\n" | |
| 79 " vec.dbl[1] = v##sign##int##bits##x##halfsize##_##name(x + halfsize); \\\n" | |
| 80 " \\\n" | |
| 81 " return vec; \\\n" | |
| 82 " }\n" | |
| 83 "\n" | |
| 84 "#define VEC_DOUBLE_LOAD(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load, sign, bits, size, halfsize)\n" | |
| 85 "#define VEC_DOUBLE_LOAD_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load_aligned, sign, bits, size, halfsize)\n" | |
| 86 "\n" | |
| 87 "#define VEC_DOUBLE_STORE_EX(name, sign, bits, size, halfsize) \\\n" | |
| 88 " 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" | |
| 89 " { \\\n" | |
| 90 " v##sign##int##bits##x##halfsize##_##name(vec.dbl[0], x); \\\n" | |
| 91 " v##sign##int##bits##x##halfsize##_##name(vec.dbl[1], x + halfsize); \\\n" | |
| 92 " }\n" | |
| 93 "\n" | |
| 94 "#define VEC_DOUBLE_STORE(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store, sign, bits, size, halfsize)\n" | |
| 95 "#define VEC_DOUBLE_STORE_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store_aligned, sign, bits, size, halfsize)\n" | |
| 96 "\n" | |
| 97 "#define VEC_DOUBLE_OP(name, sign, bits, size, halfsize, secondsign) \\\n" | |
| 98 " 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" | |
| 99 " { \\\n" | |
| 100 " vec1.dbl[0] = v##sign##int##bits##x##halfsize##_##name(vec1.dbl[0], vec2.dbl[0]); \\\n" | |
| 101 " vec1.dbl[1] = v##sign##int##bits##x##halfsize##_##name(vec1.dbl[1], vec2.dbl[1]); \\\n" | |
| 102 " \\\n" | |
| 103 " return vec1; \\\n" | |
| 104 " }\n" | |
| 105 "\n" | |
| 106 "#define VEC_DOUBLE_ADD(sign, bits, size, halfsize) VEC_DOUBLE_OP(add, sign, bits, size, halfsize, sign)\n" | |
| 107 "#define VEC_DOUBLE_SUB(sign, bits, size, halfsize) VEC_DOUBLE_OP(sub, sign, bits, size, halfsize, sign)\n" | |
| 108 "#define VEC_DOUBLE_MUL(sign, bits, size, halfsize) VEC_DOUBLE_OP(mul, sign, bits, size, halfsize, sign)\n" | |
| 109 "#define VEC_DOUBLE_DIV(sign, bits, size, halfsize) VEC_DOUBLE_OP(div, sign, bits, size, halfsize, sign)\n" | |
| 110 "#define VEC_DOUBLE_MOD(sign, bits, size, halfsize) VEC_DOUBLE_OP(mod, sign, bits, size, halfsize, sign)\n" | |
| 111 "#define VEC_DOUBLE_AVG(sign, bits, size, halfsize) VEC_DOUBLE_OP(avg, sign, bits, size, halfsize, sign)\n" | |
| 112 "#define VEC_DOUBLE_LSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lshift, sign, bits, size, halfsize, u)\n" | |
| 113 "#define VEC_DOUBLE_RSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(rshift, sign, bits, size, halfsize, u)\n" | |
| 114 "#define VEC_DOUBLE_LRSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lrshift, sign, bits, size, halfsize, u)\n" | |
| 115 "#define VEC_DOUBLE_AND(sign, bits, size, halfsize) VEC_DOUBLE_OP(and, sign, bits, size, halfsize, sign)\n" | |
| 116 "#define VEC_DOUBLE_OR(sign, bits, size, halfsize) VEC_DOUBLE_OP(or, sign, bits, size, halfsize, sign)\n" | |
| 117 "#define VEC_DOUBLE_XOR(sign, bits, size, halfsize) VEC_DOUBLE_OP(xor, sign, bits, size, halfsize, sign)\n" | |
| 118 "#define VEC_DOUBLE_MIN(sign, bits, size, halfsize) VEC_DOUBLE_OP(min, sign, bits, size, halfsize, sign)\n" | |
| 119 "#define VEC_DOUBLE_MAX(sign, bits, size, halfsize) VEC_DOUBLE_OP(max, sign, bits, size, halfsize, sign)\n" | |
| 120 "#define VEC_DOUBLE_CMPLT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmplt, sign, bits, size, halfsize, sign)\n" | |
| 121 "#define VEC_DOUBLE_CMPLE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmple, sign, bits, size, halfsize, sign)\n" | |
| 122 "#define VEC_DOUBLE_CMPEQ(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpeq, sign, bits, size, halfsize, sign)\n" | |
| 123 "#define VEC_DOUBLE_CMPGE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpge, sign, bits, size, halfsize, sign)\n" | |
| 124 "#define VEC_DOUBLE_CMPGT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpgt, sign, bits, size, halfsize, sign)\n" | |
| 125 "\n" | |
| 126 "#define VEC_DOUBLE_NOT(sign, bits, size, halfsize) \\\n" | |
| 127 " VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_not(v##sign##int##bits##x##size vec) \\\n" | |
| 128 " { \\\n" | |
| 129 " vec.dbl[0] = v##sign##int##bits##x##halfsize##_not(vec.dbl[0]); \\\n" | |
| 130 " vec.dbl[1] = v##sign##int##bits##x##halfsize##_not(vec.dbl[1]); \\\n" | |
| 131 " \\\n" | |
| 132 " return vec; \\\n" | |
| 133 " }\n" | |
| 134 "\n" | |
| 135 "#endif /* VEC_IMPL_DOUBLE_H_ */ \n" | |
| 136 "\n" | |
| 137 "/* ------------------------------------------------------------------------ */\n" | |
| 138 "/* PREPROCESSOR HELL INCOMING */\n" | |
| 139 ""; | |
| 140 | |
| 141 static const char *footer = | |
| 142 "" /* nothing */; | |
| 143 | 26 |
| 144 /* ------------------------------------------------------------------------ */ | 27 /* ------------------------------------------------------------------------ */ |
| 145 | 28 |
| 146 static void op_print_pp_halfsize(int op, int type, int bits, int size) | 29 static void op_print_pp_halfsize(int op, int type, int bits, int size) |
| 147 { | 30 { |
| 171 | 54 |
| 172 printf("\tvec.dbl[0] = "); | 55 printf("\tvec.dbl[0] = "); |
| 173 gen_print_vtype(type, bits, size / 2); | 56 gen_print_vtype(type, bits, size / 2); |
| 174 printf("_%s(vec.dbl[0]);\n", op_info->l); | 57 printf("_%s(vec.dbl[0]);\n", op_info->l); |
| 175 | 58 |
| 176 printf("\tvec1.dbl[1] = "); | 59 printf("\tvec.dbl[1] = "); |
| 177 gen_print_vtype(type, bits, size / 2); | 60 gen_print_vtype(type, bits, size / 2); |
| 178 printf("_%s(vec.dbl[1]);\n", op_info->l); | 61 printf("_%s(vec.dbl[1]);\n", op_info->l); |
| 179 | 62 |
| 180 printf("\treturn vec;\n"); | 63 printf("\treturn vec;\n"); |
| 181 } | 64 } |
| 265 [OP_CMPLT] = {NULL, op_print_pp_halfsize, op_print_twoop}, | 148 [OP_CMPLT] = {NULL, op_print_pp_halfsize, op_print_twoop}, |
| 266 [OP_CMPLE] = {NULL, op_print_pp_halfsize, op_print_twoop}, | 149 [OP_CMPLE] = {NULL, op_print_pp_halfsize, op_print_twoop}, |
| 267 [OP_CMPEQ] = {NULL, op_print_pp_halfsize, op_print_twoop}, | 150 [OP_CMPEQ] = {NULL, op_print_pp_halfsize, op_print_twoop}, |
| 268 [OP_CMPGE] = {NULL, op_print_pp_halfsize, op_print_twoop}, | 151 [OP_CMPGE] = {NULL, op_print_pp_halfsize, op_print_twoop}, |
| 269 [OP_CMPGT] = {NULL, op_print_pp_halfsize, op_print_twoop}, | 152 [OP_CMPGT] = {NULL, op_print_pp_halfsize, op_print_twoop}, |
| 153 | |
| 154 [OP_FLOOR] = {NULL, op_print_pp_halfsize, op_print_unoop}, | |
| 155 [OP_CEIL] = {NULL, op_print_pp_halfsize, op_print_unoop}, | |
| 156 [OP_ROUND] = {NULL, op_print_pp_halfsize, op_print_unoop}, | |
| 157 [OP_TRUNC] = {NULL, op_print_pp_halfsize, op_print_unoop}, | |
| 270 }; | 158 }; |
| 271 | 159 |
| 272 int main(void) | 160 int main(void) |
| 273 { | 161 { |
| 274 gen(op_impl, "double"); | 162 gen(op_impl, "double"); |
