C Square Root Functions

(1/1)

Richard Marks:
I do not take credit for this code snippet. I'm posting it here for my own reference, and others.
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.
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!

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!
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.

kingthomas:
This one comes from the Taylor Series for √(1 + x):

Code:

// calculates the square root based on the Taylor series
// higher precision is slower, but more accurate
// default is somewhat arbitrary

float sqrt(float operand, int precision = 5) {
// can't square root a negative (though this series might give you an interesting answer)
if (operand < 0)
return operand;
// x = operand - 1, since the sum is for x + 1
int x = operand - 1;
// the first is always 1
float result = 1;
for (int n = 1; n < precision; n++) {
result += (pow(-1, n) * factorial(2 * n)) / ((1 - 2 * n) * pow(factorial(n), 2) * pow(4, n)) * pow(x, n);
}
return result;
}


By the way, it only works for 0 - 2. HA HA HA!

Navigation

[0] Message Index