forked from tsoding/la
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lag.c
704 lines (639 loc) · 23.8 KB
/
lag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define VECTOR_MIN_SIZE 2
#define VECTOR_MAX_SIZE 4
static_assert(VECTOR_MIN_SIZE <= VECTOR_MAX_SIZE, "Max vector size may not be less than the min vector size, c'mon");
typedef enum {
STMT_DECL,
STMT_IMPL,
} Stmt;
typedef enum {
TYPE_FLOAT = 0,
TYPE_DOUBLE,
TYPE_INT,
TYPE_UNSIGNED_INT,
COUNT_TYPES,
} Type;
typedef struct {
const char *name;
const char *suffix;
const char *fmt;
const char *zero_lit;
} Type_Def;
static_assert(COUNT_TYPES == 4, "The amount of type definitions have changed. Please update the array bellow accordingly");
static Type_Def type_defs[COUNT_TYPES] = {
[TYPE_FLOAT] = {.name = "float", .suffix = "f", .fmt = "f", .zero_lit = "0.0f"},
[TYPE_DOUBLE] = {.name = "double", .suffix = "d", .fmt = "lf", .zero_lit = "0.0"},
[TYPE_INT] = {.name = "int", .suffix = "i", .fmt = "d", .zero_lit = "0"},
[TYPE_UNSIGNED_INT] = {.name = "unsigned int", .suffix = "u", .fmt = "u", .zero_lit = "0u"},
};
typedef enum {
OP_SUM = 0,
OP_SUB,
OP_MUL,
OP_DIV,
COUNT_OPS,
} Op_Type;
typedef struct {
const char *suffix;
const char *op;
} Op_Def;
static_assert(COUNT_OPS == 4, "The amount of operator definitions have changed. Please update the array below accordingly");
static Op_Def op_defs[COUNT_OPS] = {
[OP_SUM] = {.suffix = "sum", .op = "+="},
[OP_SUB] = {.suffix = "sub", .op = "-="},
[OP_MUL] = {.suffix = "mul", .op = "*="},
[OP_DIV] = {.suffix = "div", .op = "/="},
};
char *op_arg_names[] = {"a", "b"};
#define OP_ARITY (sizeof(op_arg_names) / sizeof(op_arg_names[0]))
typedef struct {
char cstr[128];
} Short_String;
#if defined(__GNUC__) || defined(__clang__)
#define CHECK_PRINTF_FMT(a, b) __attribute__ ((format (printf, a, b)))
#else
#define CHECK_PRINTF_FMT(...)
#endif
CHECK_PRINTF_FMT(1, 2) Short_String shortf(const char *fmt, ...)
{
Short_String result = {0};
va_list args;
va_start(args, fmt);
int n = vsnprintf(result.cstr, sizeof(result.cstr), fmt, args);
assert(n >= 0);
assert((size_t) n + 1 <= sizeof(result.cstr));
va_end(args);
return result;
}
Short_String make_vector_type(size_t n, Type_Def type_def)
{
return shortf("V%zu%s", n, type_def.suffix);
}
Short_String make_vector_prefix(size_t n, Type_Def type_def)
{
return shortf("v%zu%s", n, type_def.suffix);
}
static_assert(VECTOR_MAX_SIZE == 4, "We defined only 4 vector component names. Please update this list accordingly");
static char *vector_comps[VECTOR_MAX_SIZE] = {"x", "y", "z", "w"};
void gen_vector_def(FILE *stream, size_t n, Type_Def type_def)
{
Short_String vector_type = make_vector_type(n, type_def);
fprintf(stream, "typedef struct { %s ", type_def.name);
assert(n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < n; ++i) {
if (i > 0) fprintf(stream, ", ");
fprintf(stream, "%s", vector_comps[i]);
}
fprintf(stream, "; } %s;\n", vector_type.cstr);
}
// Generates function signatures of the following form:
// ret_type name(arg_type arg_names[0], arg_type arg_names[1], ..., arg_type arg_names[arity - 1])
// All arguments have the same type.
void gen_func_sig(FILE *stream, const char *ret_type, const char *name, const char *arg_type, char **arg_names, size_t arity)
{
fprintf(stream, "LADEF %s %s(", ret_type, name);
if (arity > 0) fprintf(stream, "%s %s", arg_type, arg_names[0]);
for (size_t arg_index = 1; arg_index < arity; ++arg_index) {
fprintf(stream, ", ");
fprintf(stream, "%s %s", arg_type, arg_names[arg_index]);
}
fprintf(stream, ")");
}
void gen_vector_ctor(FILE *stream, Stmt stmt, size_t n, Type_Def type_def)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
assert(n <= VECTOR_MAX_SIZE);
gen_func_sig(stream, vector_type.cstr, vector_prefix.cstr, type_def.name, vector_comps, n);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
fprintf(stream, " %s v;\n", vector_type.cstr);
assert(n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < n; ++i) {
fprintf(stream, " v.%s = %s;\n", vector_comps[i], vector_comps[i]);
}
fprintf(stream, " return v;\n");
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
void gen_vector_scalar_ctor(FILE *stream, Stmt stmt, size_t n, Type_Def type_def)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
Short_String name = shortf("%s%s", vector_prefix.cstr, type_def.suffix);
static_assert(VECTOR_MAX_SIZE >= 1, "The vector size is too short for this code");
gen_func_sig(stream, vector_type.cstr, name.cstr, type_def.name, vector_comps, 1);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
fprintf(stream, " return %s(", make_vector_prefix(n, type_def).cstr);
for (size_t i = 0; i < n; ++i) {
if (i > 0) fprintf(stream, ", ");
static_assert(VECTOR_MAX_SIZE >= 1, "The vector size is too short for this code");
fprintf(stream, "%s", vector_comps[0]);
}
fprintf(stream, ");\n");
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
void gen_vector_op(FILE *stream, Stmt stmt, size_t n, Type_Def type_def, Op_Def op_def)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
Short_String name = shortf("%s_%s", vector_prefix.cstr, op_def.suffix);
gen_func_sig(stream, vector_type.cstr, name.cstr, vector_type.cstr, op_arg_names, OP_ARITY);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
assert(n <= VECTOR_MAX_SIZE);
static_assert(OP_ARITY >= 2, "This code assumes that operation's arity is at least 2");
for (size_t i = 0; i < n; ++i) {
fprintf(stream, " %s.%s %s %s.%s;\n",
op_arg_names[0],
vector_comps[i],
op_def.op,
op_arg_names[1],
vector_comps[i]);
}
fprintf(stream, " return %s;\n", op_arg_names[0]);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
typedef enum {
FUN_SQRT = 0,
FUN_POW,
FUN_SIN,
FUN_COS,
FUN_MIN,
FUN_MAX,
FUN_LERP,
FUN_FLOOR,
FUN_CEIL,
FUN_CLAMP,
COUNT_FUNS,
} Fun_Type;
#define FUN_DEF_MAX_ARITY 5
typedef struct {
const char *suffix;
// NOTE: NULL means the function is not supported for this type
const char *name_for_type[COUNT_TYPES];
size_t arity;
char *args[FUN_DEF_MAX_ARITY];
} Fun_Def;
static_assert(COUNT_FUNS == 10, "The amount of functions have changed. Please update the array below accordingly");
static_assert(COUNT_TYPES == 4, "The amount of type definitions have changed. Please update the array bellow accordingly");
Fun_Def fun_defs[COUNT_FUNS] = {
[FUN_SQRT] = {
.suffix = "sqrt",
.name_for_type = {
[TYPE_FLOAT] = "sqrtf",
[TYPE_DOUBLE] = "sqrt",
},
.arity = 1,
.args = {"a"}
},
[FUN_POW] = {
.suffix = "pow",
.name_for_type = {
[TYPE_FLOAT] = "powf",
[TYPE_DOUBLE] = "pow",
},
.arity = 2,
.args = {"base", "exp"},
},
[FUN_SIN] = {
.suffix = "sin",
.name_for_type = {
[TYPE_FLOAT] = "sinf",
[TYPE_DOUBLE] = "sin",
},
.arity = 1,
.args = {"a"},
},
[FUN_COS] = {
.suffix = "cos",
.name_for_type = {
[TYPE_FLOAT] = "cosf",
[TYPE_DOUBLE] = "cos",
},
.arity = 1,
.args = {"a"},
},
[FUN_MIN] = {
.suffix = "min",
.name_for_type = {
[TYPE_FLOAT] = "fminf",
[TYPE_DOUBLE] = "fmin",
[TYPE_INT] = "mini",
[TYPE_UNSIGNED_INT] = "minu",
},
.arity = 2,
.args = {"a", "b"},
},
[FUN_MAX] = {
.suffix = "max",
.name_for_type = {
[TYPE_FLOAT] = "fmaxf",
[TYPE_DOUBLE] = "fmax",
[TYPE_INT] = "maxi",
[TYPE_UNSIGNED_INT] = "maxu",
},
.arity = 2,
.args = {"a", "b"},
},
[FUN_LERP] = {
.suffix = "lerp",
.name_for_type = {
[TYPE_FLOAT] = "lerpf",
[TYPE_DOUBLE] = "lerp",
},
.arity = 3,
.args = {"a", "b", "t"},
},
[FUN_FLOOR] = {
.suffix = "floor",
.name_for_type = {
[TYPE_FLOAT] = "floorf",
[TYPE_DOUBLE] = "floor",
},
.arity = 1,
.args = {"a"},
},
[FUN_CEIL] = {
.suffix = "ceil",
.name_for_type = {
[TYPE_FLOAT] = "ceilf",
[TYPE_DOUBLE] = "ceil",
},
.arity = 1,
.args = {"a"},
},
[FUN_CLAMP] = {
.suffix = "clamp",
.name_for_type = {
[TYPE_FLOAT] = "clampf",
[TYPE_DOUBLE] = "clampd",
[TYPE_INT] = "clampi",
[TYPE_UNSIGNED_INT] = "clampu",
},
.arity = 3,
.args = {"x", "a", "b"}
}
};
void gen_vector_fun(FILE *stream, Stmt stmt, size_t n, Type type, Fun_Type fun)
{
Fun_Def fun_def = fun_defs[fun];
Type_Def type_def = type_defs[type];
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
Short_String name = shortf("%s_%s", vector_prefix.cstr, fun_def.suffix);
gen_func_sig(stream, vector_type.cstr, name.cstr, vector_type.cstr, fun_def.args, fun_def.arity);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
Fun_Def fun_def = fun_defs[fun];
assert(fun_def.name_for_type[type]);
assert(fun_def.arity >= 1);
assert(n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < n; ++i) {
fprintf(stream, " %s.%s = %s(", fun_def.args[0], vector_comps[i], fun_def.name_for_type[type]);
for (size_t arg_num = 0; arg_num < fun_def.arity; ++arg_num) {
if (arg_num > 0) fprintf(stream, ", ");
fprintf(stream, "%s.%s", fun_def.args[arg_num], vector_comps[i]);
}
fprintf(stream, ");\n");
}
fprintf(stream, " return %s;\n", fun_def.args[0]);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
#define LERP_ARITY 3
static char *lerp_args[LERP_ARITY] = {"a", "b", "t"};
void gen_lerp(FILE *stream, Stmt stmt, const char *name, const char *type)
{
gen_func_sig(stream, type, name, type, lerp_args, LERP_ARITY);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
char *a = lerp_args[0];
char *b = lerp_args[1];
char *t = lerp_args[2];
fprintf(stream, " return %s + (%s - %s) * %s;\n", a, b, a, t);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
char *minmax_args[] = {"a", "b"};
#define MINMAX_ARITY (sizeof(minmax_args) / sizeof(minmax_args[0]))
void gen_min(FILE *stream, Stmt stmt, Type_Def type_def)
{
Short_String name = shortf("min%s", type_def.suffix);
gen_func_sig(stream, type_def.name, name.cstr, type_def.name, minmax_args, MINMAX_ARITY);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
static_assert(MINMAX_ARITY == 2, "Unexpected arity of min/max functions");
fprintf(stream, " return %s < %s ? %s : %s;\n",
minmax_args[0], minmax_args[1], minmax_args[0], minmax_args[1]);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
void gen_max(FILE *stream, Stmt stmt, Type_Def type_def)
{
Short_String name = shortf("max%s", type_def.suffix);
gen_func_sig(stream, type_def.name, name.cstr, type_def.name, minmax_args, MINMAX_ARITY);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
static_assert(MINMAX_ARITY == 2, "Unexpected arity of min/max functions");
fprintf(stream, " return %s < %s ? %s : %s;\n",
minmax_args[0], minmax_args[1], minmax_args[1], minmax_args[0]);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
char *clamp_args[] = {"x", "a", "b"};
#define CLAMP_ARITY (sizeof(clamp_args) / sizeof(clamp_args[0]))
void gen_clamp(FILE *stream, Stmt stmt, Type type, Fun_Def min_def, Fun_Def max_def)
{
Type_Def type_def = type_defs[type];
Short_String name = shortf("clamp%s", type_def.suffix);
gen_func_sig(stream, type_def.name, name.cstr, type_def.name, clamp_args, CLAMP_ARITY);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
static_assert(CLAMP_ARITY == 3, "Unexpected clamp arity");
const char *min_name = min_def.name_for_type[type];
assert(min_name != NULL);
const char *max_name = max_def.name_for_type[type];
assert(max_name != NULL);
fprintf(stream, " return %s(%s(%s, %s), %s);\n",
min_name, max_name, clamp_args[1], clamp_args[0], clamp_args[2]);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
static char *sqrlen_arg_name = "a";
void gen_vector_sqrlen(FILE *stream, Stmt stmt, size_t n, Type_Def type_def)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
Short_String name = shortf("%s_sqrlen", vector_prefix.cstr);
gen_func_sig(stream, type_def.name, name.cstr, vector_type.cstr, &sqrlen_arg_name, 1);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
fprintf(stream, " return ");
assert(n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < n; ++i) {
if (i > 0) fprintf(stream, " + ");
fprintf(stream, "%s.%s*%s.%s", sqrlen_arg_name, vector_comps[i], sqrlen_arg_name, vector_comps[i]);
}
fprintf(stream, ";\n");
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
void gen_vector_printf_macros(FILE *stream, size_t n, Type_Def type_def)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
fprintf(stream, "#define %s_Fmt \"%s(", vector_type.cstr, vector_prefix.cstr);
for (size_t i = 0; i < n; ++i) {
if (i > 0) fprintf(stream, ", ");
fprintf(stream, "%%%s", type_def.fmt);
}
fprintf(stream, ")\"\n");
fprintf(stream, "#define %s_Arg(v) ", vector_type.cstr);
assert(n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < n; ++i) {
if (i > 0) fprintf(stream, ", ");
fprintf(stream, "(v).%s", vector_comps[i]);
}
fprintf(stream, "\n");
}
static_assert(COUNT_TYPES == 4, "Amount of types have changed");
const char *funcs_sqrt_defined_for[COUNT_TYPES] = {
[TYPE_FLOAT] = "sqrtf",
[TYPE_DOUBLE] = "sqrt",
};
void gen_vector_len(FILE *stream, Stmt stmt, size_t n, Type_Def type_def, const char *sqrt_name)
{
Short_String vector_type = make_vector_type(n, type_def);
Short_String vector_prefix = make_vector_prefix(n, type_def);
Short_String func_name = shortf("%s_len", vector_prefix.cstr);
gen_func_sig(stream, type_def.name, func_name.cstr, vector_type.cstr, &sqrlen_arg_name, 1);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
Short_String sqrlen_name = shortf("%s_sqrlen", vector_prefix.cstr);
fprintf(stream, "\n");
fprintf(stream, "{\n");
fprintf(stream, " return %s(%s(%s));\n", sqrt_name, sqrlen_name.cstr, sqrlen_arg_name);
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
char *vector_convert_arg = "a";
void gen_vector_convert(FILE *stream, Stmt stmt,
size_t dst_n, Type_Def dst_type_def,
size_t src_n, Type_Def src_type_def)
{
Short_String dst_type = make_vector_type(dst_n, dst_type_def);
Short_String src_type = make_vector_type(src_n, src_type_def);
Short_String name = shortf("v%zu%s%zu%s", dst_n, dst_type_def.suffix, src_n, src_type_def.suffix);
gen_func_sig(stream, dst_type.cstr, name.cstr, src_type.cstr, &vector_convert_arg, 1);
if (stmt == STMT_DECL) {
fprintf(stream, ";\n");
} else if (stmt == STMT_IMPL) {
fprintf(stream, "\n");
fprintf(stream, "{\n");
fprintf(stream, " %s result;\n", dst_type.cstr);
assert(dst_n <= VECTOR_MAX_SIZE);
for (size_t i = 0; i < dst_n; ++i) {
if (i < src_n) {
fprintf(stream, " result.%s = (%s) %s.%s;\n", vector_comps[i], dst_type_def.name, vector_convert_arg, vector_comps[i]);
} else {
fprintf(stream, " result.%s = %s;\n", vector_comps[i], dst_type_def.zero_lit);
}
}
fprintf(stream, " return result;\n");
fprintf(stream, "}\n");
} else {
assert(0 && "unreachable");
exit(69);
}
}
// TODO: matrices
// TODO: documentation
// TODO: I'm not sure if different size conversions of the vectors are that useful
// Maybe only the same size casting?
// TODO: Would be interesting to introduce some sort of swizzling, like: V4f v2f_xxyy(V2f v)
int main()
{
FILE *stream = stdout;
// Header Part
{
fprintf(stream, "#ifndef LA_H_\n");
fprintf(stream, "#define LA_H_\n");
fprintf(stream, "\n");
fprintf(stream, "#include <math.h>\n");
fprintf(stream, "\n");
fprintf(stream, "#ifndef LADEF\n");
fprintf(stream, "#define LADEF static inline\n");
fprintf(stream, "#endif // LADEF\n");
fprintf(stream, "\n");
gen_lerp(stream, STMT_DECL, "lerpf", "float");
gen_lerp(stream, STMT_DECL, "lerp", "double");
// TODO: more robust generation of min/max functions
// kinda similar to how we do that for sqrt ones
gen_min(stream, STMT_DECL, type_defs[TYPE_INT]);
gen_max(stream, STMT_DECL, type_defs[TYPE_INT]);
gen_min(stream, STMT_DECL, type_defs[TYPE_UNSIGNED_INT]);
gen_max(stream, STMT_DECL, type_defs[TYPE_UNSIGNED_INT]);
for (Type type = 0; type < COUNT_TYPES; ++type) {
gen_clamp(stream, STMT_DECL, type, fun_defs[FUN_MIN], fun_defs[FUN_MAX]);
}
fprintf(stream, "\n");
for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
for (Type type = 0; type < COUNT_TYPES; ++type) {
gen_vector_def(stream, n, type_defs[type]);
}
}
fprintf(stream, "\n");
for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
for (Type type = 0; type < COUNT_TYPES; ++type) {
gen_vector_printf_macros(stream, n, type_defs[type]);
gen_vector_ctor(stream, STMT_DECL, n, type_defs[type]);
gen_vector_scalar_ctor(stream, STMT_DECL, n, type_defs[type]);
for (size_t src_n = VECTOR_MIN_SIZE; src_n <= VECTOR_MAX_SIZE; ++src_n) {
for (Type src_type = 0; src_type < COUNT_TYPES; ++src_type) {
if (src_n != n || src_type != type) {
gen_vector_convert(stream, STMT_DECL, n, type_defs[type], src_n, type_defs[src_type]);
}
}
}
for (Op_Type op = 0; op < COUNT_OPS; ++op) {
gen_vector_op(stream, STMT_DECL, n, type_defs[type], op_defs[op]);
}
for (Fun_Type fun = 0; fun < COUNT_FUNS; ++fun) {
if (fun_defs[fun].name_for_type[type]) {
gen_vector_fun(stream, STMT_DECL, n, type, fun);
}
}
gen_vector_sqrlen(stream, STMT_DECL, n, type_defs[type]);
if (funcs_sqrt_defined_for[type]) {
gen_vector_len(stream, STMT_DECL, n, type_defs[type], funcs_sqrt_defined_for[type]);
}
fprintf(stream, "\n");
}
}
fprintf(stream, "#endif // LA_H_\n");
fprintf(stream, "\n");
}
// C part
{
fprintf(stream, "#ifdef LA_IMPLEMENTATION\n");
fprintf(stream, "\n");
gen_lerp(stream, STMT_IMPL, "lerpf", "float");
fputc('\n', stream);
gen_lerp(stream, STMT_IMPL, "lerp", "double");
fputc('\n', stream);
gen_min(stream, STMT_IMPL, type_defs[TYPE_INT]);
fputc('\n', stream);
gen_max(stream, STMT_IMPL, type_defs[TYPE_INT]);
fputc('\n', stream);
gen_min(stream, STMT_IMPL, type_defs[TYPE_UNSIGNED_INT]);
fputc('\n', stream);
gen_max(stream, STMT_IMPL, type_defs[TYPE_UNSIGNED_INT]);
fputc('\n', stream);
for (Type type = 0; type < COUNT_TYPES; ++type) {
gen_clamp(stream, STMT_IMPL, type, fun_defs[FUN_MIN], fun_defs[FUN_MAX]);
fputc('\n', stream);
}
for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
for (Type type = 0; type < COUNT_TYPES; ++type) {
gen_vector_ctor(stream, STMT_IMPL, n, type_defs[type]);
fputc('\n', stream);
gen_vector_scalar_ctor(stream, STMT_IMPL, n, type_defs[type]);
fputc('\n', stream);
for (size_t src_n = VECTOR_MIN_SIZE; src_n <= VECTOR_MAX_SIZE; ++src_n) {
for (Type src_type = 0; src_type < COUNT_TYPES; ++src_type) {
if (src_n != n || src_type != type) {
gen_vector_convert(stream, STMT_IMPL, n, type_defs[type], src_n, type_defs[src_type]);
fputc('\n', stream);
}
}
}
for (Op_Type op = 0; op < COUNT_OPS; ++op) {
gen_vector_op(stream, STMT_IMPL, n, type_defs[type], op_defs[op]);
fputc('\n', stream);
}
for (Fun_Type fun = 0; fun < COUNT_FUNS; ++fun) {
if (fun_defs[fun].name_for_type[type]) {
gen_vector_fun(stream, STMT_IMPL, n, type, fun);
fputc('\n', stream);
}
}
gen_vector_sqrlen(stream, STMT_IMPL, n, type_defs[type]);
fputc('\n', stream);
if (funcs_sqrt_defined_for[type]) {
gen_vector_len(stream, STMT_IMPL, n, type_defs[type], funcs_sqrt_defined_for[type]);
fputc('\n', stream);
}
}
}
fprintf(stream, "#endif // LA_IMPLEMENTATION\n");
}
// TODO: print stats on how many things were generated
// TODO: v2*_add alias to v2*_sum
// 'Cause I keep confusing them
return 0;
}