Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
8.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #ifndef _MATH_PFNS_H_
  7. #define _MATH_PFNS_H_
  8. #include <limits>
  9. // YUP_ACTIVE is from Source2. It's (obviously) not supported on this branch, just including it here to help merge camera.cpp/.h and the CSM shadow code.
  10. //#define YUP_ACTIVE 1
  11. enum MatrixAxisType_t
  12. {
  13. #ifdef YUP_ACTIVE
  14. FORWARD_AXIS = 2,
  15. LEFT_AXIS = 0,
  16. UP_AXIS = 1,
  17. #else
  18. FORWARD_AXIS = 0,
  19. LEFT_AXIS = 1,
  20. UP_AXIS = 2,
  21. #endif
  22. X_AXIS = 0,
  23. Y_AXIS = 1,
  24. Z_AXIS = 2,
  25. ORIGIN = 3,
  26. PROJECTIVE = 3,
  27. };
  28. #if defined( _X360 )
  29. #include <xboxmath.h>
  30. #elif defined(_PS3)
  31. #ifdef SPU
  32. #include <vectormath/c/vectormath_aos.h>
  33. #include <spu_intrinsics.h>
  34. #else
  35. #include <ppu_asm_intrinsics.h>
  36. #endif
  37. // Note that similar defines exist in ssemath.h
  38. // Maybe we should consolidate in one place for all platforms.
  39. #define _VEC_0x7ff (vec_int4){0x7ff,0x7ff,0x7ff,0x7ff}
  40. #define _VEC_0x3ff (vec_int4){0x3ff,0x3ff,0x3ff,0x3ff}
  41. #define _VEC_22L (vector unsigned int){22,22,22,22}
  42. #define _VEC_11L (vector unsigned int){11,11,11,11}
  43. #define _VEC_0L (vector unsigned int){0,0,0,0}
  44. #define _VEC_255F (vector float){255.0f,255.0f,255.0f,255.0f}
  45. #define _VEC_NEGONEF (vector float){-1.0f,-1.0f,-1.0f,-1.0f}
  46. #define _VEC_ONEF (vector float){1.0f,1.0f,1.0f,1.0f}
  47. #define _VEC_ZEROF (vector float){0.0f,0.0f,0.0f,0.0f}
  48. #define _VEC_ZEROxyzONEwF (vector float){0.0f,0.0f,0.0f,1.0f}
  49. #define _VEC_HALFF (vector float){0.5f,0.5f,0.5f,0.5f}
  50. #define _VEC_HALFxyzZEROwF (vector float){0.5f,0.5f,0.5f,0.0f}
  51. #define _VEC_PERMUTE_XYZ0W1 (vector unsigned char){0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x1c,0x1d,0x1e,0x1f}
  52. #define _VEC_IEEEHACK (vector float){(float)(1 << 23),(float)(1 << 23),(float)(1 << 23),(float)(1 << 23)}
  53. #define _VEC_PERMUTE_FASTFTOC (vector unsigned char){0,0,0,0,0,0,0,0,0,0,0,0,0x03,0x07,0x0b,0x0f}
  54. // AngleQuaternion
  55. #define _VEC_PERMUTE_AQsxsxcxcx (vector unsigned char) {0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,0x10,0x11,0x12,0x13,0x10,0x11,0x12,0x13}
  56. #define _VEC_PERMUTE_AQczszszcz (vector unsigned char) {0x18,0x19,0x1a,0x1b,0x08,0x09,0x0a,0x0b,0x08,0x09,0x0a,0x0b,0x18,0x19,0x1a,0x1b}
  57. #define _VEC_PERMUTE_AQcxcxsxsx (vector unsigned char) {0x10,0x11,0x12,0x13,0x10,0x11,0x12,0x13,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03}
  58. #define _VEC_PERMUTE_AQszczczsz (vector unsigned char) {0x08,0x09,0x0a,0x0b,0x18,0x19,0x1a,0x1b,0x18,0x19,0x1a,0x1b,0x08,0x09,0x0a,0x0b}
  59. #define _VEC_PERMUTE_ANGLEQUAT (vector unsigned char) {0x10,0x11,0x12,0x13,0x04,0x05,0x06,0x07,0x18,0x19,0x1a,0x1b,0x0c,0x0d,0x0e,0x0f}
  60. #define _VEC_EPSILONF (__vector float) {FLT_EPSILON,FLT_EPSILON,FLT_EPSILON,FLT_EPSILON}
  61. #endif
  62. #if !(defined( PLATFORM_PPC ) || defined(SPU))
  63. // If we are not PPC based or SPU based, then assumes it is SSE2. We should make this code cleaner.
  64. #include <xmmintrin.h>
  65. // These globals are initialized by mathlib and redirected based on available fpu features
  66. // The following are not declared as macros because they are often used in limiting situations,
  67. // and sometimes the compiler simply refuses to inline them for some reason
  68. FORCEINLINE float VECTORCALL FastSqrt( float x )
  69. {
  70. __m128 root = _mm_sqrt_ss( _mm_load_ss( &x ) );
  71. return *( reinterpret_cast<float *>( &root ) );
  72. }
  73. FORCEINLINE float VECTORCALL FastRSqrtFast( float x )
  74. {
  75. // use intrinsics
  76. __m128 rroot = _mm_rsqrt_ss( _mm_load_ss( &x ) );
  77. return *( reinterpret_cast<float *>( &rroot ) );
  78. }
  79. // Single iteration NewtonRaphson reciprocal square root:
  80. // 0.5 * rsqrtps * (3 - x * rsqrtps(x) * rsqrtps(x))
  81. // Very low error, and fine to use in place of 1.f / sqrtf(x).
  82. FORCEINLINE float VECTORCALL FastRSqrt( float x )
  83. {
  84. float rroot = FastRSqrtFast( x );
  85. return (0.5f * rroot) * (3.f - (x * rroot) * rroot);
  86. }
  87. void FastSinCos( float x, float* s, float* c ); // any x
  88. float FastCos( float x );
  89. inline float FastRecip(float x) {return 1.0f / x;}
  90. // Simple SSE rsqrt. Usually accurate to around 6 (relative) decimal places
  91. // or so, so ok for closed transforms. (ie, computing lighting normals)
  92. inline float FastSqrtEst(float x) { return FastRSqrtFast(x) * x; }
  93. #else // !defined( PLATFORM_PPC ) && !defined(_SPU)
  94. #ifndef SPU
  95. // We may not need this for SPU, so let's not bother for now
  96. FORCEINLINE float _VMX_Sqrt( float x )
  97. {
  98. return __fsqrts( x );
  99. }
  100. FORCEINLINE double _VMX_RSqrt( double x )
  101. {
  102. double rroot = __frsqrte( x );
  103. // Single iteration NewtonRaphson on reciprocal square root estimate
  104. return (0.5f * rroot) * (3.0f - (x * rroot) * rroot);
  105. }
  106. FORCEINLINE double _VMX_RSqrtFast( double x )
  107. {
  108. return __frsqrte( x );
  109. }
  110. #ifdef _X360
  111. FORCEINLINE void _VMX_SinCos( float a, float *pS, float *pC )
  112. {
  113. XMScalarSinCos( pS, pC, a );
  114. }
  115. FORCEINLINE float _VMX_Cos( float a )
  116. {
  117. return XMScalarCos( a );
  118. }
  119. #endif
  120. // the 360 has fixed hw and calls directly
  121. #define FastSqrt(x) _VMX_Sqrt(x)
  122. #define FastRSqrt(x) _VMX_RSqrt(x)
  123. #define FastRSqrtFast(x) _VMX_RSqrtFast(x)
  124. #define FastSinCos(x,s,c) _VMX_SinCos(x,s,c)
  125. #define FastCos(x) _VMX_Cos(x)
  126. inline double FastRecip(double x) {return __fres(x);}
  127. inline double FastSqrtEst(double x) { return __frsqrte(x) * x; }
  128. #endif // !defined( PLATFORM_PPC ) && !defined(_SPU)
  129. // if x is infinite, return FLT_MAX
  130. inline float FastClampInfinity( float x )
  131. {
  132. #ifdef PLATFORM_PPC
  133. return fsel( std::numeric_limits<float>::infinity() - x, x, FLT_MAX );
  134. #else
  135. return ( x > FLT_MAX ? FLT_MAX : x );
  136. #endif
  137. }
  138. #if defined (_PS3)
  139. #if defined(__SPU__)
  140. inline int _rotl( int a, int count )
  141. {
  142. vector signed int vi;
  143. vi = spu_promote(a, 0);
  144. vi = spu_rl(vi, count);
  145. return spu_extract(vi, 0);
  146. }
  147. #else
  148. // extern float cosvf(float); /* single precision cosine */
  149. // extern float sinvf(float); /* single precision sine */
  150. // TODO: need a faster single precision equivalent
  151. #define cosvf cosf
  152. #define sinvf sinf
  153. inline int _rotl( int x, int c )
  154. {
  155. return __rlwimi(x,x,c,0,31);
  156. }
  157. inline int64 _rotl64( int64 x, int c )
  158. {
  159. return __rldicl( x, c, 0 );
  160. }
  161. /*
  162. FORCEINLINE float _VMX_Sqrt( float x )
  163. {
  164. vector_float_union vIn, vOut;
  165. vIn.f[0] = x;
  166. vOut.vf = sqrtf4(vIn.vf);
  167. return vOut.f[0];
  168. }
  169. FORCEINLINE float _VMX_RSqrt( float x )
  170. {
  171. vector_float_union vIn, vOut;
  172. vIn.f[0] = x;
  173. vOut.vf = rsqrtf4(vIn.vf);
  174. return vOut.f[0];
  175. }
  176. FORCEINLINE float _VMX_RSqrtFast( float x )
  177. {
  178. vector_float_union vIn, vOut;
  179. vIn.f[0] = x;
  180. vOut.vf = rsqrtf4fast(vIn.vf);
  181. return vOut.f[0];
  182. }
  183. */
  184. FORCEINLINE void _VMX_SinCos( float a, float *pS, float *pC )
  185. {
  186. *pS=sinvf(a);
  187. *pC=cosvf(a);
  188. }
  189. FORCEINLINE float _VMX_Cos( float a )
  190. {
  191. return cosvf(a);
  192. }
  193. // the 360 has fixed hw and calls directly
  194. /*
  195. #define FastSqrt(x) _VMX_Sqrt(x)
  196. #define FastRSqrt(x) _VMX_RSqrt(x)
  197. #define FastRSqrtFast(x) _VMX_RSqrtFast(x)
  198. #define FastSinCos(x,s,c) _VMX_SinCos(x,s,c)
  199. #define FastCos(x) _VMX_Cos(x)
  200. */
  201. #endif
  202. #if defined(__SPU__)
  203. // do we need these optimized yet?
  204. FORCEINLINE float FastSqrt( float x )
  205. {
  206. return sqrtf( x );
  207. }
  208. FORCEINLINE float FastRSqrt( float x )
  209. {
  210. float rroot = 1.f / (sqrtf(x) + FLT_EPSILON);
  211. return rroot;
  212. }
  213. #define FastRSqrtFast(x) FastRSqrt(x)
  214. #endif
  215. //-----------------------------------------------------------------
  216. // Vector Unions
  217. //-----------------------------------------------------------------
  218. //-----------------------------------------------------------------
  219. // Floats
  220. //-----------------------------------------------------------------
  221. typedef union
  222. {
  223. vector float vf;
  224. float f[4];
  225. } vector_float_union;
  226. #if !defined(__SPU__)
  227. //-----------------------------------------------------------------
  228. // Ints
  229. //-----------------------------------------------------------------
  230. typedef union
  231. {
  232. vector int vi;
  233. int i[4];
  234. } vector_int4_union;
  235. typedef union
  236. {
  237. vector unsigned int vui;
  238. unsigned int ui[4];
  239. } vector_uint4_union;
  240. //-----------------------------------------------------------------
  241. // Shorts
  242. //-----------------------------------------------------------------
  243. typedef union
  244. {
  245. vector signed short vs;
  246. signed short s[8];
  247. } vector_short8_union;
  248. typedef union
  249. {
  250. vector unsigned short vus;
  251. unsigned short us[8];
  252. } vector_ushort8_union;
  253. //-----------------------------------------------------------------
  254. // Chars
  255. //-----------------------------------------------------------------
  256. typedef union
  257. {
  258. vector signed char vc;
  259. signed char c[16];
  260. } vector_char16_union;
  261. typedef union
  262. {
  263. vector unsigned char vuc;
  264. unsigned char uc[16];
  265. } vector_uchar16_union;
  266. #endif
  267. #endif // _PS3
  268. #endif // #ifndef SPU
  269. #endif // _MATH_PFNS_H_