/m/x/carmack.c

https://github.com/seydar/icling · C · 17 lines · 12 code · 2 blank · 3 comment · 0 complexity · b0cd3943152ceecd0e465bbb0b800e79 MD5 · raw file

  1. /* FILE: carmack.c
  2. * PURPOSE: IEEE sqrt trickery
  3. */
  4. 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
  11. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  12. y = * ( float * ) &i;
  13. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  14. return y * number;
  15. }