diff options
Diffstat (limited to 'src/common/math_util.h')
| -rw-r--r-- | src/common/math_util.h | 174 |
1 files changed, 3 insertions, 171 deletions
diff --git a/src/common/math_util.h b/src/common/math_util.h index 52f579cf7..0b1400b41 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h | |||
| @@ -4,11 +4,9 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include "common/common_types.h" | ||
| 8 | |||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | #include <cstdlib> | ||
| 10 | #include <type_traits> | 9 | #include <type_traits> |
| 11 | #include <vector> | ||
| 12 | 10 | ||
| 13 | namespace MathUtil | 11 | namespace MathUtil |
| 14 | { | 12 | { |
| @@ -19,83 +17,6 @@ inline T Clamp(const T val, const T& min, const T& max) | |||
| 19 | return std::max(min, std::min(max, val)); | 17 | return std::max(min, std::min(max, val)); |
| 20 | } | 18 | } |
| 21 | 19 | ||
| 22 | static const u64 DOUBLE_SIGN = 0x8000000000000000ULL, | ||
| 23 | DOUBLE_EXP = 0x7FF0000000000000ULL, | ||
| 24 | DOUBLE_FRAC = 0x000FFFFFFFFFFFFFULL, | ||
| 25 | DOUBLE_ZERO = 0x0000000000000000ULL; | ||
| 26 | |||
| 27 | static const u32 FLOAT_SIGN = 0x80000000, | ||
| 28 | FLOAT_EXP = 0x7F800000, | ||
| 29 | FLOAT_FRAC = 0x007FFFFF, | ||
| 30 | FLOAT_ZERO = 0x00000000; | ||
| 31 | |||
| 32 | union IntDouble { | ||
| 33 | double d; | ||
| 34 | u64 i; | ||
| 35 | }; | ||
| 36 | union IntFloat { | ||
| 37 | float f; | ||
| 38 | u32 i; | ||
| 39 | }; | ||
| 40 | |||
| 41 | inline bool IsNAN(double d) | ||
| 42 | { | ||
| 43 | IntDouble x; x.d = d; | ||
| 44 | return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && | ||
| 45 | ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) ); | ||
| 46 | } | ||
| 47 | |||
| 48 | inline bool IsQNAN(double d) | ||
| 49 | { | ||
| 50 | IntDouble x; x.d = d; | ||
| 51 | return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && | ||
| 52 | ((x.i & 0x0007fffffffffffULL) == 0x000000000000000ULL) && | ||
| 53 | ((x.i & 0x000800000000000ULL) == 0x000800000000000ULL) ); | ||
| 54 | } | ||
| 55 | |||
| 56 | inline bool IsSNAN(double d) | ||
| 57 | { | ||
| 58 | IntDouble x; x.d = d; | ||
| 59 | return( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && | ||
| 60 | ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) && | ||
| 61 | ((x.i & 0x0008000000000000ULL) == DOUBLE_ZERO) ); | ||
| 62 | } | ||
| 63 | |||
| 64 | inline float FlushToZero(float f) | ||
| 65 | { | ||
| 66 | IntFloat x; x.f = f; | ||
| 67 | if ((x.i & FLOAT_EXP) == 0) | ||
| 68 | x.i &= FLOAT_SIGN; // turn into signed zero | ||
| 69 | return x.f; | ||
| 70 | } | ||
| 71 | |||
| 72 | inline double FlushToZeroAsFloat(double d) | ||
| 73 | { | ||
| 74 | IntDouble x; x.d = d; | ||
| 75 | if ((x.i & DOUBLE_EXP) < 0x3800000000000000ULL) | ||
| 76 | x.i &= DOUBLE_SIGN; // turn into signed zero | ||
| 77 | return x.d; | ||
| 78 | } | ||
| 79 | |||
| 80 | enum PPCFpClass | ||
| 81 | { | ||
| 82 | PPC_FPCLASS_QNAN = 0x11, | ||
| 83 | PPC_FPCLASS_NINF = 0x9, | ||
| 84 | PPC_FPCLASS_NN = 0x8, | ||
| 85 | PPC_FPCLASS_ND = 0x18, | ||
| 86 | PPC_FPCLASS_NZ = 0x12, | ||
| 87 | PPC_FPCLASS_PZ = 0x2, | ||
| 88 | PPC_FPCLASS_PD = 0x14, | ||
| 89 | PPC_FPCLASS_PN = 0x4, | ||
| 90 | PPC_FPCLASS_PINF = 0x5, | ||
| 91 | }; | ||
| 92 | |||
| 93 | // Uses PowerPC conventions for the return value, so it can be easily | ||
| 94 | // used directly in CPU emulation. | ||
| 95 | u32 ClassifyDouble(double dvalue); | ||
| 96 | // More efficient float version. | ||
| 97 | u32 ClassifyFloat(float fvalue); | ||
| 98 | |||
| 99 | template<class T> | 20 | template<class T> |
| 100 | struct Rectangle | 21 | struct Rectangle |
| 101 | { | 22 | { |
| @@ -104,101 +25,12 @@ struct Rectangle | |||
| 104 | T right; | 25 | T right; |
| 105 | T bottom; | 26 | T bottom; |
| 106 | 27 | ||
| 107 | Rectangle() | 28 | Rectangle() {} |
| 108 | { } | ||
| 109 | 29 | ||
| 110 | Rectangle(T theLeft, T theTop, T theRight, T theBottom) | 30 | Rectangle(T left, T top, T right, T bottom) : left(left), top(top), right(right), bottom(bottom) {} |
| 111 | : left(theLeft), top(theTop), right(theRight), bottom(theBottom) | ||
| 112 | { } | ||
| 113 | |||
| 114 | bool operator==(const Rectangle& r) { return left==r.left && top==r.top && right==r.right && bottom==r.bottom; } | ||
| 115 | 31 | ||
| 116 | T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); } | 32 | T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); } |
| 117 | T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); } | 33 | T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); } |
| 118 | |||
| 119 | // If the rectangle is in a coordinate system with a lower-left origin, use | ||
| 120 | // this Clamp. | ||
| 121 | void ClampLL(T x1, T y1, T x2, T y2) | ||
| 122 | { | ||
| 123 | if (left < x1) left = x1; | ||
| 124 | if (right > x2) right = x2; | ||
| 125 | if (top > y1) top = y1; | ||
| 126 | if (bottom < y2) bottom = y2; | ||
| 127 | } | ||
| 128 | |||
| 129 | // If the rectangle is in a coordinate system with an upper-left origin, | ||
| 130 | // use this Clamp. | ||
| 131 | void ClampUL(T x1, T y1, T x2, T y2) | ||
| 132 | { | ||
| 133 | if (left < x1) left = x1; | ||
| 134 | if (right > x2) right = x2; | ||
| 135 | if (top < y1) top = y1; | ||
| 136 | if (bottom > y2) bottom = y2; | ||
| 137 | } | ||
| 138 | }; | 34 | }; |
| 139 | 35 | ||
| 140 | } // namespace MathUtil | 36 | } // namespace MathUtil |
| 141 | |||
| 142 | inline float pow2f(float x) {return x * x;} | ||
| 143 | inline double pow2(double x) {return x * x;} | ||
| 144 | |||
| 145 | float MathFloatVectorSum(const std::vector<float>&); | ||
| 146 | |||
| 147 | #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) | ||
| 148 | #define ROUND_DOWN(x, a) ((x) & ~((a) - 1)) | ||
| 149 | |||
| 150 | // Rounds down. 0 -> undefined | ||
| 151 | inline u64 Log2(u64 val) | ||
| 152 | { | ||
| 153 | #if defined(__GNUC__) | ||
| 154 | return 63 - __builtin_clzll(val); | ||
| 155 | |||
| 156 | #elif defined(_MSC_VER) && defined(_M_X64) | ||
| 157 | unsigned long result = -1; | ||
| 158 | _BitScanReverse64(&result, val); | ||
| 159 | return result; | ||
| 160 | |||
| 161 | #else | ||
| 162 | u64 result = -1; | ||
| 163 | while (val != 0) | ||
| 164 | { | ||
| 165 | val >>= 1; | ||
| 166 | ++result; | ||
| 167 | } | ||
| 168 | return result; | ||
| 169 | #endif | ||
| 170 | } | ||
| 171 | |||
| 172 | // Tiny matrix/vector library. | ||
| 173 | // Used for things like Free-Look in the gfx backend. | ||
| 174 | |||
| 175 | class Matrix33 | ||
| 176 | { | ||
| 177 | public: | ||
| 178 | static void LoadIdentity(Matrix33 &mtx); | ||
| 179 | |||
| 180 | // set mtx to be a rotation matrix around the x axis | ||
| 181 | static void RotateX(Matrix33 &mtx, float rad); | ||
| 182 | // set mtx to be a rotation matrix around the y axis | ||
| 183 | static void RotateY(Matrix33 &mtx, float rad); | ||
| 184 | |||
| 185 | // set result = a x b | ||
| 186 | static void Multiply(const Matrix33 &a, const Matrix33 &b, Matrix33 &result); | ||
| 187 | static void Multiply(const Matrix33 &a, const float vec[3], float result[3]); | ||
| 188 | |||
| 189 | float data[9]; | ||
| 190 | }; | ||
| 191 | |||
| 192 | class Matrix44 | ||
| 193 | { | ||
| 194 | public: | ||
| 195 | static void LoadIdentity(Matrix44 &mtx); | ||
| 196 | static void LoadMatrix33(Matrix44 &mtx, const Matrix33 &m33); | ||
| 197 | static void Set(Matrix44 &mtx, const float mtxArray[16]); | ||
| 198 | |||
| 199 | static void Translate(Matrix44 &mtx, const float vec[3]); | ||
| 200 | |||
| 201 | static void Multiply(const Matrix44 &a, const Matrix44 &b, Matrix44 &result); | ||
| 202 | |||
| 203 | float data[16]; | ||
| 204 | }; | ||