We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Inverse Square Root
Page Index Toggle Pages: 1
Inverse Square Root (Read 605 times)
Inverse Square Root
Jul 22nd, 2007, 5:19pm
 
I looked up the Inverse Square Root method that Traer Physics uses because I'm developing a physics engine of my own for use at work.

I converted it to Java and for a moment I thought it was slower until I realised why having the inverse square root is useful. It means you can calculate normals by multiplication instead of division.

So here is the speed proof:

Code:

float dx = 0;
float dy = 0;
int timer = 0;
void setup(){
}
void draw(){
timer = millis();
for(int i = 0; i < 10000000; i++){
float d = myDist1();
dx = mouseX / d;
dy = mouseY / d;
}
println("dx:"+dx+" dy:"+dy+" 1:"+(millis()-timer));
timer = millis();
for(int i = 0; i < 10000000; i++){
float d = myDist2();
dx = mouseX * d;
dy = mouseY * d;
}
println("dx:"+dx+" dy:"+dy+" 2:"+(millis()-timer));
}

float myDist1(){
return (float)Math.sqrt((mouseX - 0) * (mouseX - 0) + (mouseY - 0) * (mouseY - 0));
}
float myDist2(){
return invSqrt((mouseX - 0) * (mouseX - 0) + (mouseY - 0) * (mouseY - 0));
}

static float invSqrt(float x){
float xhalf = 0.5f*x;
int i = Float.floatToIntBits(x); // get bits for floating value
i = 0x5f375a86- (i>>1); // gives initial guess y0
x = Float.intBitsToFloat(i); // convert bits back to float
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
return x;
}

/*
// Original C code by Chris Lomont <http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf>
static float InvSqrt(float x){
float xhalf = 0.5f*x;
int i = (int)&x; // get bits for floating value
i = 0x5f375a86- (i>>1); // gives initial guess y0
x = (float)&i; // convert bits back to float
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
return x;
}
*/


I'd like to port this to Flash though, does anyone know how the float to ints and back is done? Seeing as the speed boost is so slight with inverse square root I'm thinking it might also help this example to inline that method.
Re: Inverse Square Root
Reply #1 - Jul 22nd, 2007, 6:03pm
 
Ah wait, looking at the Java reference I found the equations for the int bits back into floats:

Code:

float x = 0.2;
int bits = Float.floatToIntBits(x);
int s = ((bits >> 31) == 0) ? 1 : -1;
int e = ((bits >> 23) & 0xff);
int m = (e == 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
println(s*m*pow(2, e-150));
println(Float.intBitsToFloat(bits));


But that method is 60 times slower than Float.intBitsToFloat so that can't be how it's done inside Java.

I guess I'll only be able to have super fast physics in Java. Sad
Page Index Toggle Pages: 1