math_util.h 4.7 KB

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