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.

96 lines
1.7 KiB

  1. //========= Copyright 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. /*
  39. * (c) Ian Stephenson
  40. *
  41. * ian@dctsystems.co.uk
  42. *
  43. * Fast pow() reference implementation
  44. */
  45. static float shift23=(1<<23);
  46. static float OOshift23=1.0/(1<<23);
  47. float FastLog2(float i)
  48. {
  49. float LogBodge=0.346607f;
  50. float x;
  51. float y;
  52. x=*(int *)&i;
  53. x*= OOshift23; //1/pow(2,23);
  54. x=x-127;
  55. y=x-floorf(x);
  56. y=(y-y*y)*LogBodge;
  57. return x+y;
  58. }
  59. float FastPow2(float i)
  60. {
  61. float PowBodge=0.33971f;
  62. float x;
  63. float y=i-floorf(i);
  64. y=(y-y*y)*PowBodge;
  65. x=i+127-y;
  66. x*= shift23; //pow(2,23);
  67. *(int*)&x=(int)x;
  68. return x;
  69. }
  70. float FastPow(float a, float b)
  71. {
  72. if (a <= OOshift23)
  73. {
  74. return 0.0f;
  75. }
  76. return FastPow2(b*FastLog2(a));
  77. }
  78. float FastPow10( float i )
  79. {
  80. return FastPow2( i * 3.321928f );
  81. }