Archived

This topic is now archived and is closed to further replies.

vaneger

i need a fast sqrt or Inverse sqrt for unsigned longs

Recommended Posts

Palidine    1315
usually you can get away with not using the square root for most game involved things. comparing distance squared is just as good as comparing distance, as long as you are just worried about relative distances. but otherwise just use the square root function. you can use a tailor series for the square root, but AFAIK that''s what the processor already uses. either that or a lookup table. either way, the processor will most likely be better at getting the square root faster than what you can come up with

-me

Share this post


Link to post
Share on other sites
iMalc    2466
Give this a go, I think it''s out of the Decent source code.

inline const long isqrt(register unsigned long x) {
register unsigned long r, nr, m;
r= 0;
m= 0x40000000;
do {
nr= r + m;
if (nr<=x) {
x-= nr;
r= nr + m;
}
r>>= 1;
m>>= 2;
} while (m!=0);
if (x>r) r+= 1;
return r;
}

Share this post


Link to post
Share on other sites