1 #pragma once
2 
3 #include <algorithm>
4 #include <limits>
5 #include <cmath>
6 #include <cstdint>
7 
8 namespace lhdrengine
9 {
10 
11 constexpr int MAXVAL = 0xffff;
12 constexpr float MAXVALF = static_cast<float>(MAXVAL);  // float version of MAXVAL
13 constexpr double MAXVALD = static_cast<double>(MAXVAL); // double version of MAXVAL
14 
15 constexpr double RT_PI = 3.14159265358979323846; // pi
16 constexpr double RT_PI_2 = 1.57079632679489661923; // pi/2
17 constexpr double RT_1_PI = 0.31830988618379067154; // 1/pi
18 constexpr double RT_2_PI = 0.63661977236758134308; // 2/pi
19 constexpr double RT_SQRT1_2 = 0.70710678118654752440; // 1/sqrt(2)
20 
21 constexpr double RT_INFINITY = std::numeric_limits<double>::infinity();
22 constexpr double RT_NAN = std::numeric_limits<double>::quiet_NaN();
23 
24 constexpr float RT_PI_F = RT_PI;
25 constexpr float RT_PI_F_2 = RT_PI_2;
26 
27 constexpr float RT_INFINITY_F = std::numeric_limits<float>::infinity();
28 constexpr float RT_NAN_F = std::numeric_limits<float>::quiet_NaN();
29 
30 template<typename T>
SQR(T x)31 constexpr T SQR(T x)
32 {
33     return x * x;
34 }
35 
36 template<typename T>
min(const T & a)37 constexpr const T& min(const T& a)
38 {
39     return a;
40 }
41 
42 template<typename T>
min(const T & a,const T & b)43 constexpr const T& min(const T& a, const T& b)
44 {
45     return b < a ? b : a;
46 }
47 
48 template<typename T, typename... ARGS>
min(const T & a,const T & b,const ARGS &...args)49 constexpr const T& min(const T& a, const T& b, const ARGS&... args)
50 {
51     return min(min(a, b), min(args...));
52 }
53 
54 template<typename T>
max(const T & a)55 constexpr const T& max(const T& a)
56 {
57     return a;
58 }
59 
60 template<typename T>
max(const T & a,const T & b)61 constexpr const T& max(const T& a, const T& b)
62 {
63     return a < b ? b : a;
64 }
65 
66 template<typename T, typename... ARGS>
max(const T & a,const T & b,const ARGS &...args)67 constexpr const T& max(const T& a, const T& b, const ARGS&... args)
68 {
69     return max(max(a, b), max(args...));
70 }
71 
72 template<typename T>
LIM(const T & a,const T & b,const T & c)73 constexpr const T& LIM(const T& a, const T& b, const T& c)
74 {
75     return max(b, min(a, c));
76 }
77 
78 template<typename T>
LIM01(const T & a)79 constexpr T LIM01(const T& a)
80 {
81     return max(T(0), min(a, T(1)));
82 }
83 
84 template<typename T>
CLIP(const T & a)85 constexpr T CLIP(const T& a)
86 {
87     return LIM(a, static_cast<T>(0), static_cast<T>(MAXVAL));
88 }
89 
90 template <typename T>
SGN(const T & a)91 constexpr T SGN(const T& a)
92 {
93     // returns -1 for a < 0, 0 for a = 0 and +1 for a > 0
94     return (T(0) < a) - (a < T(0));
95 }
96 
97 template<typename T>
intp(T a,T b,T c)98 constexpr T intp(T a, T b, T c)
99 {
100     // calculate a * b + (1 - a) * c
101     // following is valid:
102     // intp(a, b+x, c+x) = intp(a, b, c) + x
103     // intp(a, b*x, c*x) = intp(a, b, c) * x
104     return a * (b - c) + c;
105 }
106 
107 template<typename T>
norm1(const T & x,const T & y)108 inline T norm1(const T& x, const T& y)
109 {
110     return std::abs(x) + std::abs(y);
111 }
112 
113 template<typename T>
norm2(const T & x,const T & y)114 inline T norm2(const T& x, const T& y)
115 {
116     return std::sqrt(x * x + y * y);
117 }
118 
119 template< typename T >
norminf(const T & x,const T & y)120 inline T norminf(const T& x, const T& y)
121 {
122     return max(std::abs(x), std::abs(y));
123 }
124 
float2uint16range(float d)125 constexpr int float2uint16range(float d)
126 {
127     // clips input to [0;65535] and rounds
128     return CLIP(d) + 0.5f;
129 }
130 
uint16ToUint8Rounded(std::uint16_t i)131 constexpr std::uint8_t uint16ToUint8Rounded(std::uint16_t i)
132 {
133     return ((i + 128) - ((i + 128) >> 8)) >> 8;
134 }
135 
136 }
137