/**
 * 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"

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

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("\tvec.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},

	[OP_FLOOR] = {NULL, op_print_pp_halfsize, op_print_unoop},
	[OP_CEIL]  = {NULL, op_print_pp_halfsize, op_print_unoop},
	[OP_ROUND] = {NULL, op_print_pp_halfsize, op_print_unoop},
	[OP_TRUNC] = {NULL, op_print_pp_halfsize, op_print_unoop},
};

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