math_util.h 4.9 KB

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