summaryrefslogtreecommitdiff
path: root/common/utility.hpp
blob: 5f5c186cf689e7afdb0771d0ad77782594653d56 (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
#pragma once

#ifdef _MANAGED
#include <math.h>
#else
#include <emmintrin.h>
#endif

namespace rawaccel {

#ifdef _MANAGED
    inline double sqrtsd(double val) { return sqrt(val); }
#else
    inline double sqrtsd(double val) {
        __m128d src = _mm_load_sd(&val);
        __m128d dst = _mm_sqrt_sd(src, src);
        _mm_store_sd(&val, dst);
        return val;
    }
#endif

    constexpr double minsd(double a, double b) 
    {
        return (a < b) ? a : b;
    }

    constexpr double maxsd(double a, double b) 
    {
        return (b < a) ? a : b;
    }

    constexpr double clampsd(double v, double lo, double hi) 
    {
        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)
    {
	    union { double f; unsigned long long i; } u = { x };
	    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;
    }

    struct noop { 
        template <typename... Ts> 
        constexpr void operator()(Ts&&...) const noexcept {}
    };
}