Quake 3: Arena

 float InvSqrt(float x){

    float xhalf=0.5f*x;

    int i=*(int*)&x;    

    i=0×5f3759df - (i>>1);    

    x=*(float *)&i;

    x=x*(1.5f-xhalf*x*x);    

    return x;
}

Analysis coming soon.

A little bit Google searching got me the usage of this Inverse Square function in Quake 3. Mind you it was written prior 2000.

Finding the inverse square root of a number has many applications in 3D graphics, not least of all the normalisation of 3D vectors. Without something like the nrm instruction in a modern fragment processor where you can get normalisation of an fp16 3-channel vector for free on certain NVIDIA hardware if you’re (or the compiler is!) careful, or if you need to do it outside of a shader program for whatever reason, inverse square root is your friend.

The magic of the code stands out as the i=0×5f3759df - (i>>1); line. That is, the mysterious number is 0×5f3759df which is 1597463007 in decimal notation.  Now why is it here and what’s it doing here?

 

[Read: FAST INVERSE SQUARE ROOT ]

[Read: Beyond3D]



⊕ Related Posts

  • Wall·E directing animator Angus MacLane discusses the making of the movie

  • ContextFree.js & Algorithm Ink: Making Art with Javascript

  • The Future of Prosthetics

  • Thoughts on “SoftBank is going to carry the iPhone in Japan”


  • ⊕ Article Tags → | | | | |

    Leave a Reply