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.

80 lines
2.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #ifndef _MATH_PFNS_H_
  7. #define _MATH_PFNS_H_
  8. #if defined( _X360 )
  9. #include <xboxmath.h>
  10. #endif
  11. #if !defined( _X360 )
  12. // These globals are initialized by mathlib and redirected based on available fpu features
  13. extern float (*pfSqrt)(float x);
  14. extern float (*pfRSqrt)(float x);
  15. extern float (*pfRSqrtFast)(float x);
  16. extern void (*pfFastSinCos)(float x, float *s, float *c);
  17. extern float (*pfFastCos)(float x);
  18. // The following are not declared as macros because they are often used in limiting situations,
  19. // and sometimes the compiler simply refuses to inline them for some reason
  20. #define FastSqrt(x) (*pfSqrt)(x)
  21. #define FastRSqrt(x) (*pfRSqrt)(x)
  22. #define FastRSqrtFast(x) (*pfRSqrtFast)(x)
  23. #define FastSinCos(x,s,c) (*pfFastSinCos)(x,s,c)
  24. #define FastCos(x) (*pfFastCos)(x)
  25. #if defined(__i386__) || defined(_M_IX86)
  26. // On x86, the inline FPU or SSE sqrt instruction is faster than
  27. // the overhead of setting up a function call and saving/restoring
  28. // the FPU or SSE register state and can be scheduled better, too.
  29. #undef FastSqrt
  30. #define FastSqrt(x) ::sqrtf(x)
  31. #endif
  32. #endif // !_X360
  33. #if defined( _X360 )
  34. FORCEINLINE float _VMX_Sqrt( float x )
  35. {
  36. return __fsqrts( x );
  37. }
  38. FORCEINLINE float _VMX_RSqrt( float x )
  39. {
  40. float rroot = __frsqrte( x );
  41. // Single iteration NewtonRaphson on reciprocal square root estimate
  42. return (0.5f * rroot) * (3.0f - (x * rroot) * rroot);
  43. }
  44. FORCEINLINE float _VMX_RSqrtFast( float x )
  45. {
  46. return __frsqrte( x );
  47. }
  48. FORCEINLINE void _VMX_SinCos( float a, float *pS, float *pC )
  49. {
  50. XMScalarSinCos( pS, pC, a );
  51. }
  52. FORCEINLINE float _VMX_Cos( float a )
  53. {
  54. return XMScalarCos( a );
  55. }
  56. // the 360 has fixed hw and calls directly
  57. #define FastSqrt(x) _VMX_Sqrt(x)
  58. #define FastRSqrt(x) _VMX_RSqrt(x)
  59. #define FastRSqrtFast(x) _VMX_RSqrtFast(x)
  60. #define FastSinCos(x,s,c) _VMX_SinCos(x,s,c)
  61. #define FastCos(x) _VMX_Cos(x)
  62. #endif // _X360
  63. #endif // _MATH_PFNS_H_