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.

100 lines
1.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=====================================================================================//
  6. #include "mathlib/ssemath.h"
  7. // NOTE: This has to be the last file included!
  8. #include "tier0/memdbgon.h"
  9. fltx4 Pow_FixedPoint_Exponent_SIMD( const fltx4 & x, int exponent)
  10. {
  11. fltx4 rslt=Four_Ones; // x^0=1.0
  12. int xp=abs(exponent);
  13. if (xp & 3) // fraction present?
  14. {
  15. fltx4 sq_rt=SqrtEstSIMD(x);
  16. if (xp & 1) // .25?
  17. rslt=SqrtEstSIMD(sq_rt); // x^.25
  18. if (xp & 2)
  19. rslt=MulSIMD(rslt,sq_rt);
  20. }
  21. xp>>=2; // strip fraction
  22. fltx4 curpower=x; // curpower iterates through x,x^2,x^4,x^8,x^16...
  23. while(1)
  24. {
  25. if (xp & 1)
  26. rslt=MulSIMD(rslt,curpower);
  27. xp>>=1;
  28. if (xp)
  29. curpower=MulSIMD(curpower,curpower);
  30. else
  31. break;
  32. }
  33. if (exponent<0)
  34. return ReciprocalEstSaturateSIMD(rslt); // pow(x,-b)=1/pow(x,b)
  35. else
  36. return rslt;
  37. }
  38. #ifndef _PS3 // these aren't fast (or correct) on the PS3
  39. /*
  40. * (c) Ian Stephenson
  41. *
  42. * ian@dctsystems.co.uk
  43. *
  44. * Fast pow() reference implementation
  45. */
  46. static float shift23=(1<<23);
  47. static float OOshift23=1.0/(1<<23);
  48. float FastLog2(float i)
  49. {
  50. float LogBodge=0.346607f;
  51. float x;
  52. float y;
  53. x=*(int *)&i;
  54. x*= OOshift23; //1/pow(2,23);
  55. x=x-127;
  56. y=x-floorf(x);
  57. y=(y-y*y)*LogBodge;
  58. return x+y;
  59. }
  60. float FastPow2(float i)
  61. {
  62. float PowBodge=0.33971f;
  63. float x;
  64. float y=i-floorf(i);
  65. y=(y-y*y)*PowBodge;
  66. x=i+127-y;
  67. x*= shift23; //pow(2,23);
  68. *(int*)&x=(int)x;
  69. return x;
  70. }
  71. float FastPow(float a, float b)
  72. {
  73. if (a <= OOshift23)
  74. {
  75. return 0.0f;
  76. }
  77. return FastPow2(b*FastLog2(a));
  78. }
  79. float FastPow10( float i )
  80. {
  81. return FastPow2( i * 3.321928f );
  82. }
  83. #else
  84. #pragma message("TODO: revisit fast logs on all PPC hardware")
  85. #endif