question about some code



 DEVELOP > c-Plus-Plus > question about some code

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "lrbtav"
Date: 20 Dec 2006 08:39:07 PM
Object: question about some code
Saw this on beyond 3d's website earlier today:

float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}

I don't know what it is. OK, I know it's the inverse of the square root
of x, but I'm having difficulty relating how this segment of code and
Quake 3 are related.
.

User: "Gianni Mariani"

Title: Re: question about some code 20 Dec 2006 08:54:21 PM
lrbtav wrote:

Saw this on beyond 3d's website earlier today:

float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}


I don't know what it is. OK, I know it's the inverse of the square root
of x, but I'm having difficulty relating how this segment of code and
Quake 3 are related.

There was a discussion about it on slashdot a few weeks ago.
It's basically assuming that the floating point number is in IEEE format
and it's doing an iteration of a successive approximation scheme. The
downshift also does a downshift of the exponent which is like a
squareroot. I don't know the exact details, but needless to say, it is
platform specific.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER