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
|
#pragma once
//
// Configuration macros:
//
// MATH_USE_FLOAT
// - Use floats instead of doubles for scalar values.
#include <float.h>
#include <math.h>
#ifdef MATH_USE_DOUBLE
typedef double R;
#define R_MIN DBL_MIN
#define R_MAX DBL_MAX
#else // floats
typedef float R;
#define R_MIN FLT_MIN
#define R_MAX FLT_MAX
#endif
#define PI 3.14159265359
#define INV_PI 0.31830988618
/// Radians per degree.
#define TO_RAD (PI / 180.0)
/// Degrees per radian.
#define TO_DEG (180.0 / PI)
#ifdef MATH_USE_DOUBLE
static inline double min(R a, R b) { return fmin(a, b); }
static inline double max(R a, R b) { return fmax(a, b); }
static inline double rlog2(R a) { return log2(a); }
#else // floats
static inline float min(R a, R b) { return fminf(a, b); }
static inline float max(R a, R b) { return fmaxf(a, b); }
static inline float rlog2(R a) { return log2f(a); }
#endif
static inline R rabs(R x) { return x >= 0.0 ? x : -x; }
static inline R clamp(R x, R low, R high) { return max(low, min(high, x)); }
static inline R sq(R x) { return x * x; }
static inline R sign(R x) {
if (x < 0) {
return -1;
} else if (x > 0) {
return 1;
} else {
return 0;
}
}
static inline R lerp(R a, R b, R t) { return a + (b - a) * t; }
|