Quake fast square root for cpp target?

137 views
Skip to first unread message

Damian Pope

unread,
May 13, 2015, 12:06:15 AM5/13/15
to haxe...@googlegroups.com
Hello!

I just learnt about the "Quake" trick for calculating inverse square roots quickly. Nicholas wrote a great article on it back in 2008. Does anyone know how I can implement it in Haxe for the cpp target?

Nicholas showed how to do it for the Flash target. But there are four lines that rely on a Flash-only class called Flash.memory:
   flash.Memory.setFloat(0,x);
   var i = flash.Memory.getI32(0);
   ...
   flash.Memory.setI32(0,i);
   x = flash.Memory.getFloat(0);

Is there similar code for the cpp target? Can I just use the corresponding methods in haxe.io.Bytes? If so, will the method still be substantially faster than Math.sqrt()?

I've searched, but I haven't been able to find code anywhere.

Thanks!

Damian.

Hugh

unread,
May 13, 2015, 1:01:00 AM5/13/15
to haxe...@googlegroups.com
I mentioned this in my wwx talk last year:

You can use the cpp.Pointer "reinterpret" to switch representations.

Hugh

Luca

unread,
May 13, 2015, 8:37:54 AM5/13/15
to haxe...@googlegroups.com
Nowadays, whether the quack inv-sqrt is faster or not is no longer clear, sometimes just doing 1 / sqrt will be faster.

You won't be able to use the quake inv-sqrt at all though if you are just using Haxe's Float's since they are 64bit and the quake trick only works for 32bit floats.

Damian Pope

unread,
May 13, 2015, 9:48:29 AM5/13/15
to haxe...@googlegroups.com
Thanks Hugh! This is exactly what I was after. Cheers,

Damian

Damian Pope

unread,
May 13, 2015, 10:00:24 AM5/13/15
to haxe...@googlegroups.com
Thanks Luca!

Very helpful insights. I guess I should do some testing/benchmarking to check which is faster. Maybe Haxe's math.sqrt() is faster today than back in 2008?

Great point about 32 vs. 64-bit floats - I am using Haxe's standard 64-bit floats. But I assume I can just convert to 32-bit floats as Hugh seems to do in his code:

var y:Float32  = number;

(Here, number is a standard Haxe 64-bit float)

I don't need much precision in my calculations. So, I guess I could just the new Float32 class throughout my program.

Thanks again!

Damian.

Luca

unread,
May 13, 2015, 12:21:56 PM5/13/15
to haxe...@googlegroups.com
Yes, you can convert to 32bit float and back, but that conversion is not free.

Justin Donaldson

unread,
May 13, 2015, 4:47:41 PM5/13/15
to Haxe
There's different "magic" constants for 64 bit numbers, wikipedia gives more info:
http://en.wikipedia.org/wiki/Fast_inverse_square_root

On Wed, May 13, 2015 at 9:21 AM, Luca <delta...@hotmail.com> wrote:
Yes, you can convert to 32bit float and back, but that conversion is not free.

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Damian Pope

unread,
May 13, 2015, 7:48:19 PM5/13/15
to haxe...@googlegroups.com
Thanks Justin! Great to know. Damian.
Reply all
Reply to author
Forward
0 new messages