By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,432 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,432 IT Pros & Developers. It's quick & easy.

question about some code

P: n/a
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.

Dec 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Dec 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.