aboutsummaryrefslogtreecommitdiff
path: root/include/math/defs.h
blob: f572d8fdb68fa22a298fbfd247ca13930f0bc6ff (plain)
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
#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 R min(R a, R b) { return fmin(a, b); }
static inline R max(R a, R b) { return fmax(a, b); }
static inline R rlog2(R a) { return log2(a); }
static inline R rmod(R a, R m) { return fmodf(a, m); }
#else // floats
static inline R min(R a, R b) { return fminf(a, b); }
static inline R max(R a, R b) { return fmaxf(a, b); }
static inline R rlog2(R a) { return log2f(a); }
static inline R rmod(R a, R m) { return fmod(a, m); }
#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; }
static inline R R_eq(R a, R b, R eps) { return rabs(a - b) <= eps; }