aboutsummaryrefslogtreecommitdiffstats
path: root/src/video-math.h
blob: 69cc83d11eaa75306439d1f45f5afeb844fb64d1 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include <math.h>

static inline double
nnpow_d(double a, double b)
{
	int neg = a < 0;
	a = pow(neg ? -a : a, b);
	return neg ? -a : a;
}

static inline float
nnpow_f(float a, float b)
{
	int neg = a < 0;
	a = powf(neg ? -a : a, b);
	return neg ? -a : a;
}

static inline double
posmod_d(double a, double b)
{
	double x = fmod(a, b);
	return x < 0 ? x + b : x;
}

static inline float
posmod_f(float a, float b)
{
	float x = fmodf(a, b);
	return x < 0 ? x + b : x;
}

static inline double
degsin_d(double u)
{
	if (!fmod(u, 90)) {
		int64_t v = (int64_t)u;
		v = ((v / 90) % 4 + 4) % 4;
		return ((double[]){0, 1, 0, -1})[v];
	}
	return sin(u * (M_PI / 180));
}

static inline float
degsin_f(float u)
{
	if (!fmodf(u, 90)) {
		int64_t v = (int64_t)u;
		v = ((v / 90) % 4 + 4) % 4;
		return ((float[]){0, 1, 0, -1})[v];
	}
	return sinf(u * (float)(M_PI / 180));
}

static inline double
degcos_d(double u)
{
	if (!fmod(u, 90)) {
		int64_t v = (int64_t)u;
		v = ((v / 90) % 4 + 4) % 4;
		return ((double[]){1, 0, -1, 0})[v];
	}
	return cos(u * (M_PI / 180));
}

static inline float
degcos_f(float u)
{
	if (!fmodf(u, 90)) {
		int64_t v = (int64_t)u;
		v = ((v / 90) % 4 + 4) % 4;
		return ((float[]){1, 0, -1, 0})[v];
	}
	return cosf(u * (float)(M_PI / 180));
}

#define GENERIC(TYPE, FUNC, ...)\
	TYPE:           FUNC(__VA_ARGS__),\
	TYPE *:         FUNC(__VA_ARGS__),\
	TYPE **:        FUNC(__VA_ARGS__),\
	TYPE ***:       FUNC(__VA_ARGS__),\
	const TYPE:     FUNC(__VA_ARGS__),\
	const TYPE *:   FUNC(__VA_ARGS__),\
	const TYPE **:  FUNC(__VA_ARGS__),\
	const TYPE ***: FUNC(__VA_ARGS__)

#define MATH_GENERIC_1(FUNC, A)       (_Generic((A),\
						GENERIC(double, FUNC,    A),\
						GENERIC(float,  FUNC##f, A)))

#define MATH_GENERIC_N(FUNC, A, ...)  (_Generic((A),\
						GENERIC(double, FUNC,    A, __VA_ARGS__),\
						GENERIC(float,  FUNC##f, A, __VA_ARGS__)))

#define BLIND_GENERIC_1(FUNC, A)      (_Generic((A),\
						GENERIC(double, FUNC##_d, A),\
						GENERIC(float,  FUNC##_f, A)))

#define BLIND_GENERIC_N(FUNC, A, ...) (_Generic((A),\
						GENERIC(double, FUNC##_d, A, __VA_ARGS__), \
						GENERIC(float,  FUNC##_f, A, __VA_ARGS__)))

#define pow(...)        MATH_GENERIC_N(pow,      __VA_ARGS__)
#define log2(...)       MATH_GENERIC_1(log2,     __VA_ARGS__)
#define log(...)        MATH_GENERIC_1(log,      __VA_ARGS__)
#define abs(...)        MATH_GENERIC_1(fabs,     __VA_ARGS__)
#define sqrt(...)       MATH_GENERIC_1(sqrt,     __VA_ARGS__)
#define exp(...)        MATH_GENERIC_1(exp,      __VA_ARGS__)
#define g_isnan(...)    MATH_GENERIC_1(isnan,    __VA_ARGS__)
#define g_isinf(...)    MATH_GENERIC_1(isinf,    __VA_ARGS__)
#define g_isfinite(...) MATH_GENERIC_1(isfinite, __VA_ARGS__)
#define mod(...)        MATH_GENERIC_N(fmod,     __VA_ARGS__)
#define cos(...)        MATH_GENERIC_1(cos,      __VA_ARGS__)
#define sin(...)        MATH_GENERIC_1(sin,      __VA_ARGS__)
#define tan(...)        MATH_GENERIC_1(tan,      __VA_ARGS__)
#define atan2(...)      MATH_GENERIC_N(atan2,    __VA_ARGS__)

#define nnpow(...)       BLIND_GENERIC_N(nnpow,       __VA_ARGS__)
#define posmod(...)      BLIND_GENERIC_N(posmod,      __VA_ARGS__)
#define degcos(...)      BLIND_GENERIC_1(degcos,      __VA_ARGS__)
#define degsin(...)      BLIND_GENERIC_1(degsin,      __VA_ARGS__)
#define srgb_encode(...) BLIND_GENERIC_1(srgb_encode, __VA_ARGS__)
#define srgb_decode(...) BLIND_GENERIC_1(srgb_decode, __VA_ARGS__)

#define yuv_to_srgb(a, b, c, d, e, f)\
	BLIND_GENERIC_N(yuv_to_srgb, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))
#define srgb_to_yuv(a, b, c, d, e, f)\
	BLIND_GENERIC_N(srgb_to_yuv, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))
#define ciexyz_to_srgb(a, b, c, d, e, f)\
	BLIND_GENERIC_N(ciexyz_to_srgb, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))
#define srgb_to_ciexyz(a, b, c, d, e, f)\
	BLIND_GENERIC_N(srgb_to_ciexyz, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))
#define scaled_yuv_to_ciexyz(a, b, c, d, e, f)\
	BLIND_GENERIC_N(scaled_yuv_to_ciexyz, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))
#define ciexyz_to_scaled_yuv(a, b, c, d, e, f)\
	BLIND_GENERIC_N(ciexyz_to_scaled_yuv, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f))

#define htole(A) (_Generic((A),\
			   uint8_t: (A),\
			    int8_t: (uint8_t)(A),\
			   uint16_t: htole16((uint16_t)(A)),\
			    int16_t: (uint16_t)htole16((uint16_t)(A)),\
			   uint32_t: htole32((uint32_t)(A)),\
			    int32_t: (uint32_t)htole32((uint32_t)(A)),\
			   uint64_t: htole64((uint64_t)(A)),\
			    int64_t: (uint64_t)htole64((uint64_t)(A))))

#define letoh(A) (_Generic((A),\
			   uint8_t: (A),\
			    int8_t: (uint8_t)(A),\
			   uint16_t: le16toh((uint16_t)(A)),\
			    int16_t: (uint16_t)le16toh((uint16_t)(A)),\
			   uint32_t: le32toh((uint32_t)(A)),\
			    int32_t: (uint32_t)le32toh((uint32_t)(A)),\
			   uint64_t: le64toh((uint64_t)(A)),\
			    int64_t: (uint64_t)le64toh((uint64_t)(A))))