The code was made available in André LaMothe's book:

**Tricks of the 3D Game Programming Gurus (ISBN 0-672-31835-0)**

Code:

float Sqrt(float value)

{

float result;

_asm

{

mov eax, value

sub eax, 0x3F800000

sar eax, 1

add eax, 0x3F800000

mov result, eax

}

return result;

}

It is at least 3x faster than the standard sqrt() function.{

float result;

_asm

{

mov eax, value

sub eax, 0x3F800000

sar eax, 1

add eax, 0x3F800000

mov result, eax

}

return result;

}

The code depends on the format of the float data type.

I assume this code will only work on Intel/AMD PCs.

I've tried to understand HOW it works to no avail. I just will use it as is.

The following implementation of a square root function is my own design.

Code:

unsigned int Sqrt(unsigned int value)

{

unsigned int result = 0;

for (unsigned int i = 128; i != 0; i >>= 1)

{

result += i;

if (result * result > value)

{

result -= i;

}

}

return result;

}

This code executes about 2x faster than the standard sqrt() function!{

unsigned int result = 0;

for (unsigned int i = 128; i != 0; i >>= 1)

{

result += i;

if (result * result > value)

{

result -= i;

}

}

return result;

}

The following implementation is by an unnamed member in the NDS homebrew development scene.

Code:

unsigned int Sqrt(unsigned int n)

{

unsigned int a;

for (a = 0; n >= (2 * a) + 1; n -= (2 * a++) + 1);

return a;

}

I was disappointed to find that their code executes 4x SLOWER than the standard sqrt() function!{

unsigned int a;

for (a = 0; n >= (2 * a) + 1; n -= (2 * a++) + 1);

return a;

}

No soup for you!

If you've got an implementation of calculating the square root of a number, then post it here.

I tested 1,000,000 calculations using each function and timed the calculations using a high precision timer.