/DiscreteDataAnalysis/q_rsqrt.h

https://github.com/DingSoung/DSP · C++ Header · 20 lines · 15 code · 3 blank · 2 comment · 0 complexity · 5eaf73bd0232c33349d93915a83cdd73 MD5 · raw file

  1. #ifndef q_rsqrt_H
  2. #define q_rsqrt_H
  3. //wiki http://en.wikipedia.org/wiki/Fast_inverse_square_root
  4. static float q_rsqrt(float number) {
  5. long i;
  6. float x2, y;
  7. const float threehalfs = 1.5F;
  8. x2 = number * 0.5F;
  9. y = number;
  10. i = *(long *)&y;// evil floating point bit level hacking(对浮点数的邪恶位级hack)
  11. i = 0x5f3759df - (i >> 1);// what the fuck?(这他妈的是怎么回事?)
  12. y = *(float *)&i;
  13. y = y * (threehalfs - (x2 * y * y)); // 1st iteration (第一次牛顿迭代)
  14. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed(第二次迭代,可以删除)
  15. return y;
  16. }
  17. #endif