view gen/gendouble.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
line wrap: on
line source

/**
 * vec - a tiny SIMD vector library in C99
 * 
 * Copyright (c) 2024-2025 Paper
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

#include "genlib.h"

#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))

/* XXX: would it be faster to unroll literally everything instead of defining everything,
 * and then unpacking it all? */
static const char *header =
	"/**\n"
	" * vec - a tiny SIMD vector library in C99\n"
	" * \n"
	" * Copyright (c) 2024-2025 Paper\n"
	" * \n"
	" * Permission is hereby granted, free of charge, to any person obtaining a copy\n"
	" * of this software and associated documentation files (the \"Software\"), to deal\n"
	" * in the Software without restriction, including without limitation the rights\n"
	" * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
	" * copies of the Software, and to permit persons to whom the Software is\n"
	" * furnished to do so, subject to the following conditions:\n"
	" * \n"
	" * The above copyright notice and this permission notice shall be included in all\n"
	" * copies or substantial portions of the Software.\n"
	" * \n"
	" * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
	" * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
	" * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
	" * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
	" * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
	" * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
	" * SOFTWARE.\n"
	"**/\n"
	"\n"
	"/* This file is automatically generated! Do not edit it directly!\n"
	" * Edit the code that generates it in utils/gendbl.c  --paper */\n"
	"\n"
	"#ifndef VEC_IMPL_DOUBLE_H_\n"
	"#define VEC_IMPL_DOUBLE_H_\n"
	"\n"
	"#define VEC_DOUBLE_SPLAT(sign, bits, size, halfsize) \\\n"
	"	VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_splat(vec_##sign##int##bits x) \\\n"
	"	{ \\\n"
	"		v##sign##int##bits##x##size vec; \\\n"
	"	\\\n"
	"		vec.dbl[0] = v##sign##int##bits##x##halfsize##_splat(x); \\\n"
	"		vec.dbl[1] = v##sign##int##bits##x##halfsize##_splat(x); \\\n"
	"	\\\n"
	"		return vec; \\\n"
	"	}\n"
	"\n"
	"#define VEC_DOUBLE_LOAD_EX(name, sign, bits, size, halfsize) \\\n"
	"	VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_##name(const vec_##sign##int##bits x[size]) \\\n"
	"	{ \\\n"
	"		v##sign##int##bits##x##size vec; \\\n"
	"	\\\n"
	"		vec.dbl[0] = v##sign##int##bits##x##halfsize##_##name(x); \\\n"
	"		vec.dbl[1] = v##sign##int##bits##x##halfsize##_##name(x + halfsize); \\\n"
	"	\\\n"
	"		return vec; \\\n"
	"	}\n"
	"\n"
	"#define VEC_DOUBLE_LOAD(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load, sign, bits, size, halfsize)\n"
	"#define VEC_DOUBLE_LOAD_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_LOAD_EX(load_aligned, sign, bits, size, halfsize)\n"
	"\n"
	"#define VEC_DOUBLE_STORE_EX(name, sign, bits, size, halfsize) \\\n"
	"	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"
	"	{ \\\n"
	"		v##sign##int##bits##x##halfsize##_##name(vec.dbl[0], x); \\\n"
	"		v##sign##int##bits##x##halfsize##_##name(vec.dbl[1], x + halfsize); \\\n"
	"	}\n"
	"\n"
	"#define VEC_DOUBLE_STORE(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store, sign, bits, size, halfsize)\n"
	"#define VEC_DOUBLE_STORE_ALIGNED(sign, bits, size, halfsize) VEC_DOUBLE_STORE_EX(store_aligned, sign, bits, size, halfsize)\n"
	"\n"
	"#define VEC_DOUBLE_OP(name, sign, bits, size, halfsize, secondsign) \\\n"
	"	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"
	"	{ \\\n"
	"		vec1.dbl[0] = v##sign##int##bits##x##halfsize##_##name(vec1.dbl[0], vec2.dbl[0]); \\\n"
	"		vec1.dbl[1] = v##sign##int##bits##x##halfsize##_##name(vec1.dbl[1], vec2.dbl[1]); \\\n"
	"	\\\n"
	"		return vec1; \\\n"
	"	}\n"
	"\n"
	"#define VEC_DOUBLE_ADD(sign, bits, size, halfsize) VEC_DOUBLE_OP(add, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_SUB(sign, bits, size, halfsize) VEC_DOUBLE_OP(sub, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_MUL(sign, bits, size, halfsize) VEC_DOUBLE_OP(mul, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_DIV(sign, bits, size, halfsize) VEC_DOUBLE_OP(div, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_MOD(sign, bits, size, halfsize) VEC_DOUBLE_OP(mod, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_AVG(sign, bits, size, halfsize) VEC_DOUBLE_OP(avg, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_LSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lshift, sign, bits, size, halfsize, u)\n"
	"#define VEC_DOUBLE_RSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(rshift, sign, bits, size, halfsize, u)\n"
	"#define VEC_DOUBLE_LRSHIFT(sign, bits, size, halfsize) VEC_DOUBLE_OP(lrshift, sign, bits, size, halfsize, u)\n"
	"#define VEC_DOUBLE_AND(sign, bits, size, halfsize) VEC_DOUBLE_OP(and, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_OR(sign, bits, size, halfsize) VEC_DOUBLE_OP(or, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_XOR(sign, bits, size, halfsize) VEC_DOUBLE_OP(xor, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_MIN(sign, bits, size, halfsize) VEC_DOUBLE_OP(min, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_MAX(sign, bits, size, halfsize) VEC_DOUBLE_OP(max, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_CMPLT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmplt, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_CMPLE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmple, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_CMPEQ(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpeq, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_CMPGE(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpge, sign, bits, size, halfsize, sign)\n"
	"#define VEC_DOUBLE_CMPGT(sign, bits, size, halfsize) VEC_DOUBLE_OP(cmpgt, sign, bits, size, halfsize, sign)\n"
	"\n"
	"#define VEC_DOUBLE_NOT(sign, bits, size, halfsize) \\\n"
	"	VEC_FUNC_IMPL v##sign##int##bits##x##size v##sign##int##bits##x##size##_not(v##sign##int##bits##x##size vec) \\\n"
	"	{ \\\n"
	"		vec.dbl[0] = v##sign##int##bits##x##halfsize##_not(vec.dbl[0]); \\\n"
	"		vec.dbl[1] = v##sign##int##bits##x##halfsize##_not(vec.dbl[1]); \\\n"
	"	\\\n"
	"		return vec; \\\n"
	"	}\n"
	"\n"
	"#endif /* VEC_IMPL_DOUBLE_H_ */ \n"
	"\n"
	"/* ------------------------------------------------------------------------ */\n"
	"/* PREPROCESSOR HELL INCOMING */\n"
	"";

static const char *footer = 
	"" /* nothing */;

/* ------------------------------------------------------------------------ */

static void op_print_pp_halfsize(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("defined(V%s%dx%d_%s_DEFINED)", type_str[type].u, bits, size / 2, op_info->u);
}

static void op_print_twoop(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("\tvec1.dbl[0] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec1.dbl[0], vec2.dbl[0]);\n", op_info->l);

	printf("\tvec1.dbl[1] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec1.dbl[1], vec2.dbl[1]);\n", op_info->l);

	printf("\treturn vec1;\n");
}

static void op_print_unoop(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("\tvec.dbl[0] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec.dbl[0]);\n", op_info->l);

	printf("\tvec1.dbl[1] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec.dbl[1]);\n", op_info->l);

	printf("\treturn vec;\n");
}

static inline void op_print_load(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("\t");
	gen_print_vtype(type, bits, size);
	printf(" vec;\n");

	printf("\tvec.dbl[0] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(x);\n", op_info->l);

	printf("\tvec.dbl[1] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(x + %d);\n", op_info->l, size / 2);

	printf("\treturn vec;\n");
}

static inline void op_print_splat(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("\t");
	gen_print_vtype(type, bits, size);
	printf(" vec;\n");

	printf("\tvec.dbl[0] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(x);\n", op_info->l);

	printf("\tvec.dbl[1] = ");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(x);\n", op_info->l);

	printf("\treturn vec;\n");
}

static inline void op_print_store(int op, int type, int bits, int size)
{
	struct op_info *op_info = gen_op_info(op);

	printf("\t");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec.dbl[0], x);\n", op_info->l);

	printf("\t");
	gen_print_vtype(type, bits, size / 2);
	printf("_%s(vec.dbl[1], x + %d);\n", op_info->l, size / 2);
}

static struct op_impl op_impl[OP_FINAL_] = {
	[OP_SPLAT] = {NULL, op_print_pp_halfsize, op_print_splat},
	[OP_LOAD_ALIGNED] = {NULL, op_print_pp_halfsize, op_print_load},
	[OP_LOAD] = {NULL, op_print_pp_halfsize, op_print_load},
	[OP_STORE_ALIGNED] = {NULL, op_print_pp_halfsize, op_print_store},
	[OP_STORE] = {NULL, op_print_pp_halfsize, op_print_store},

	/* arithmetic */
	[OP_ADD] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_SUB] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_MUL] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_DIV] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_MOD] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_AVG] = {NULL, op_print_pp_halfsize, op_print_twoop},

	/* bitwise */
	[OP_AND] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_OR] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_XOR] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_NOT] = {NULL, op_print_pp_halfsize, op_print_unoop},

	/* min/max */
	[OP_MIN] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_MAX] = {NULL, op_print_pp_halfsize, op_print_twoop},

	/* bitshift */
	[OP_LSHIFT] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_LRSHIFT] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_RSHIFT] = {NULL, op_print_pp_halfsize, op_print_twoop},

	/* comparison */
	[OP_CMPLT] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_CMPLE] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_CMPEQ] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_CMPGE] = {NULL, op_print_pp_halfsize, op_print_twoop},
	[OP_CMPGT] = {NULL, op_print_pp_halfsize, op_print_twoop},
};

int main(void)
{
	gen(op_impl, "double");
}