math_util.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #include "common/math_util.h"
  6. #include <numeric> // Necessary on OS X, but not Linux
  7. namespace MathUtil
  8. {
  9. u32 ClassifyDouble(double dvalue)
  10. {
  11. // TODO: Optimize the below to be as fast as possible.
  12. IntDouble value;
  13. value.d = dvalue;
  14. u64 sign = value.i & DOUBLE_SIGN;
  15. u64 exp = value.i & DOUBLE_EXP;
  16. if (exp > DOUBLE_ZERO && exp < DOUBLE_EXP)
  17. {
  18. // Nice normalized number.
  19. return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
  20. }
  21. else
  22. {
  23. u64 mantissa = value.i & DOUBLE_FRAC;
  24. if (mantissa)
  25. {
  26. if (exp)
  27. {
  28. return PPC_FPCLASS_QNAN;
  29. }
  30. else
  31. {
  32. // Denormalized number.
  33. return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
  34. }
  35. }
  36. else if (exp)
  37. {
  38. //Infinite
  39. return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
  40. }
  41. else
  42. {
  43. //Zero
  44. return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
  45. }
  46. }
  47. }
  48. u32 ClassifyFloat(float fvalue)
  49. {
  50. // TODO: Optimize the below to be as fast as possible.
  51. IntFloat value;
  52. value.f = fvalue;
  53. u32 sign = value.i & FLOAT_SIGN;
  54. u32 exp = value.i & FLOAT_EXP;
  55. if (exp > FLOAT_ZERO && exp < FLOAT_EXP)
  56. {
  57. // Nice normalized number.
  58. return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
  59. }
  60. else
  61. {
  62. u32 mantissa = value.i & FLOAT_FRAC;
  63. if (mantissa)
  64. {
  65. if (exp)
  66. {
  67. return PPC_FPCLASS_QNAN; // Quiet NAN
  68. }
  69. else
  70. {
  71. // Denormalized number.
  72. return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
  73. }
  74. }
  75. else if (exp)
  76. {
  77. // Infinite
  78. return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
  79. }
  80. else
  81. {
  82. //Zero
  83. return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
  84. }
  85. }
  86. }
  87. } // namespace
  88. inline void MatrixMul(int n, const float *a, const float *b, float *result)
  89. {
  90. for (int i = 0; i < n; ++i)
  91. {
  92. for (int j = 0; j < n; ++j)
  93. {
  94. float temp = 0;
  95. for (int k = 0; k < n; ++k)
  96. {
  97. temp += a[i * n + k] * b[k * n + j];
  98. }
  99. result[i * n + j] = temp;
  100. }
  101. }
  102. }
  103. // Calculate sum of a float list
  104. float MathFloatVectorSum(const std::vector<float>& Vec)
  105. {
  106. return std::accumulate(Vec.begin(), Vec.end(), 0.0f);
  107. }
  108. void Matrix33::LoadIdentity(Matrix33 &mtx)
  109. {
  110. memset(mtx.data, 0, sizeof(mtx.data));
  111. mtx.data[0] = 1.0f;
  112. mtx.data[4] = 1.0f;
  113. mtx.data[8] = 1.0f;
  114. }
  115. void Matrix33::RotateX(Matrix33 &mtx, float rad)
  116. {
  117. float s = sin(rad);
  118. float c = cos(rad);
  119. memset(mtx.data, 0, sizeof(mtx.data));
  120. mtx.data[0] = 1;
  121. mtx.data[4] = c;
  122. mtx.data[5] = -s;
  123. mtx.data[7] = s;
  124. mtx.data[8] = c;
  125. }
  126. void Matrix33::RotateY(Matrix33 &mtx, float rad)
  127. {
  128. float s = sin(rad);
  129. float c = cos(rad);
  130. memset(mtx.data, 0, sizeof(mtx.data));
  131. mtx.data[0] = c;
  132. mtx.data[2] = s;
  133. mtx.data[4] = 1;
  134. mtx.data[6] = -s;
  135. mtx.data[8] = c;
  136. }
  137. void Matrix33::Multiply(const Matrix33 &a, const Matrix33 &b, Matrix33 &result)
  138. {
  139. MatrixMul(3, a.data, b.data, result.data);
  140. }
  141. void Matrix33::Multiply(const Matrix33 &a, const float vec[3], float result[3])
  142. {
  143. for (int i = 0; i < 3; ++i) {
  144. result[i] = 0;
  145. for (int k = 0; k < 3; ++k) {
  146. result[i] += a.data[i * 3 + k] * vec[k];
  147. }
  148. }
  149. }
  150. void Matrix44::LoadIdentity(Matrix44 &mtx)
  151. {
  152. memset(mtx.data, 0, sizeof(mtx.data));
  153. mtx.data[0] = 1.0f;
  154. mtx.data[5] = 1.0f;
  155. mtx.data[10] = 1.0f;
  156. mtx.data[15] = 1.0f;
  157. }
  158. void Matrix44::LoadMatrix33(Matrix44 &mtx, const Matrix33 &m33)
  159. {
  160. for (int i = 0; i < 3; ++i)
  161. {
  162. for (int j = 0; j < 3; ++j)
  163. {
  164. mtx.data[i * 4 + j] = m33.data[i * 3 + j];
  165. }
  166. }
  167. for (int i = 0; i < 3; ++i)
  168. {
  169. mtx.data[i * 4 + 3] = 0;
  170. mtx.data[i + 12] = 0;
  171. }
  172. mtx.data[15] = 1.0f;
  173. }
  174. void Matrix44::Set(Matrix44 &mtx, const float mtxArray[16])
  175. {
  176. for(int i = 0; i < 16; ++i) {
  177. mtx.data[i] = mtxArray[i];
  178. }
  179. }
  180. void Matrix44::Translate(Matrix44 &mtx, const float vec[3])
  181. {
  182. LoadIdentity(mtx);
  183. mtx.data[3] = vec[0];
  184. mtx.data[7] = vec[1];
  185. mtx.data[11] = vec[2];
  186. }
  187. void Matrix44::Multiply(const Matrix44 &a, const Matrix44 &b, Matrix44 &result)
  188. {
  189. MatrixMul(4, a.data, b.data, result.data);
  190. }