diff options
| author | a1xd <[email protected]> | 2021-03-30 18:27:02 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-03-30 18:27:02 -0400 |
| commit | fa3ebfb1eb054ba88824a908c996094bb98e85c5 (patch) | |
| tree | bf52cf6d5de16714dba11e96719ce1434a686779 /common/utility.hpp | |
| parent | put utility in namespace (diff) | |
| download | rawaccel-fa3ebfb1eb054ba88824a908c996094bb98e85c5.tar.xz rawaccel-fa3ebfb1eb054ba88824a908c996094bb98e85c5.zip | |
refactor lut/motivity
Diffstat (limited to 'common/utility.hpp')
| -rw-r--r-- | common/utility.hpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/common/utility.hpp b/common/utility.hpp index ae14b48..de90d44 100644 --- a/common/utility.hpp +++ b/common/utility.hpp @@ -34,6 +34,33 @@ namespace rawaccel { return minsd(maxsd(v, lo), hi); } + template <typename T> + constexpr const T& min(const T& a, const T& b) + { + return (b < a) ? b : a; + } + + template <typename T> + constexpr const T& max(const T& a, const T& b) + { + return (b < a) ? a : b; + } + + template <typename T> + constexpr const T& clamp(const T& v, const T& lo, const T& hi) + { + return (v < lo) ? lo : (hi < v) ? hi : v; + } + + constexpr double lerp(double a, double b, double t) + { + double x = a + t * (b - a); + if ((t > 1) == (a < b)) { + return maxsd(x, b); + } + return minsd(x, b); + } + // returns the unbiased exponent of x if x is normal inline int ilogb(double x) { @@ -41,6 +68,14 @@ namespace rawaccel { return static_cast<int>((u.i >> 52) & 0x7ff) - 0x3ff; } + // returns x * 2^n if n is in [-1022, 1023] + inline double scalbn(double x, int n) + { + union { double f; unsigned long long i; } u; + u.i = static_cast<unsigned long long>(0x3ff + n) << 52; + return x * u.f; + } + inline bool infnan(double x) { return ilogb(x) == 0x400; |