NOGDUS $1670.00 has been donated to NOGDUS!
September 21, 2017, 03:49:45 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: C Square Root Functions  (Read 15031 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: May 06, 2010, 08:55:02 PM »

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

kingthomas
Offline Offline

Respect: 10
« Reply #1 on: May 06, 2010, 09:48:41 PM »

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!
Logged
Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!