A lot of research has been done to optimize the invSqrt calculus. One of the most famous was discovered in Quake source code and is commonly refered as "fast inverse square root". It's written in C language as the following function :

I'll not enter into technical details on how and why this is working, but you will notice two operations that are converting float-to-int and int-to-float using pointer calculus. This is used to get the internal representation of the float-bits as an integer, and vice-versa.

In Flash, you can do that by using a ByteArray :

write a Number by using the writeFloat() method

reset the position to 0

use readInt() to read the float bits

Sadly, calling these functions is very slow, so using a ByteArray to implement invSqrt is actually slower than using 1.0 / Math.sqrt...

You'll notice that by using this, the inverse-square-root takes around the same time as as Float division ! Again very good news for haxe3D, physaXe and all other Haxe Flash applications that will use this trick !

The paper linked from there, http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf, suggests that there are better "magic" numbers; the one that author has found was 0x5f375a86

Ian Liu

Nov 23, 2008 at 03:00

Ha! Thanks for the links.Why they don't teach us these things on the college? =P

Tim Knip

Nov 24, 2008 at 00:44

Wow, that's cool.... I'm one of the core-devs of Papervision3D, so alas can't use it... Couldn't resist checking how Alchemy itself performs on it. The swc is here: http://www.assembla.com/spaces/floorplanner-alchemy/documents

As to be expected its very slow... So slow in fact that its unusable (like a 1000 times slower then 1/Math.sqrt()).

Could you maybe review invsqrt.c to see if something's wrong with the code itself? (apart from some weird double-to-float casts it seems ok to me).

Kind of reminds me of the optimizations for Sin/Cos that can be seen at http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/ and only seem easy to implement in Haxe (because of it's inline functions). I'm so close to biting the bullet now and making the jump ... very soon.