Team Fortress 2 Source Code as on 22/4/2020
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.

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