]> git.lizzy.rs Git - dragonstd.git/blob - number.h
Cleanup number code
[dragonstd.git] / number.h
1 #ifndef _DRAGONTYPE_NUMBER_H_
2 #define _DRAGONTYPE_NUMBER_H_
3
4 #include <stdint.h>
5 #include <stdbool.h>
6
7 #define DEFINE_CLAMP(type) \
8         type type ## _clamp(type v, type min, type max);
9
10 #define DEFINE_MIX(scalar, type) \
11         type type ## _mix(type a, type b, scalar f);
12
13 #define DEFINE_VECTOR(scalar, vector, box) vector; \
14         typedef struct {vector min; vector max;} box; \
15         bool   vector ## _equals(vector a, vector b); \
16         vector vector ## _add(vector a, vector b); \
17         vector vector ## _clamp(vector v, vector min, vector max); \
18         DEFINE_CLAMP(vector)
19
20 #define DEFINE_VECTORS(scalar) \
21         typedef struct {scalar x, y      ;} DEFINE_VECTOR(scalar, v2 ## scalar, aabb2 ## scalar) \
22         typedef struct {scalar x, y, z   ;} DEFINE_VECTOR(scalar, v3 ## scalar, aabb3 ## scalar) \
23         typedef struct {scalar x, y, z, w;} DEFINE_VECTOR(scalar, v4 ## scalar, aabb4 ## scalar)
24
25 #define DEFINE_SCALAR(scalar, origin) \
26         typedef origin scalar; \
27         scalar scalar ## _max(scalar a, scalar b); \
28         scalar scalar ## _min(scalar a, scalar b); \
29         DEFINE_CLAMP(scalar) \
30         DEFINE_VECTORS(scalar)
31
32 #define DEFINE_INTEGER(bits) \
33         DEFINE_SCALAR(s ## bits,  int ## bits ## _t) \
34         DEFINE_SCALAR(u ## bits, uint ## bits ## _t)
35
36 #define DEFINE_FLOAT(type, origin) \
37         DEFINE_SCALAR(type, origin) \
38         DEFINE_MIX(type, type) \
39         DEFINE_MIX(type, v2 ## type) \
40         DEFINE_MIX(type, v3 ## type) \
41         DEFINE_MIX(type, v4 ## type)
42
43 DEFINE_INTEGER(8)
44 DEFINE_INTEGER(16)
45 DEFINE_INTEGER(32)
46 DEFINE_INTEGER(64)
47
48 DEFINE_FLOAT(f32, float)
49 DEFINE_FLOAT(f64, double)
50
51 #undef DEFINE_CLAMP
52 #undef DEFINE_MIX
53 #undef DEFINE_VECTOR
54 #undef DEFINE_VECTORS
55 #undef DEFINE_SCALAR
56 #undef DEFINE_INTEGER
57 #undef DEFINE_FLOAT
58
59 #endif