Math.H and sqrt()
I was looking in math.h trying to find the function for square root (i wanted to see how it calculated it and see why it is so taxing and if there was any way i could make it faster)
I found this:
extern __inline__ double sqrt (double x)
{
double res;
__asm__ ("fsqrt;" : "=t" (res) : "0" (x));
return res;
}
I don''t understand what this __asm__ ("fsqrt;" : "=t" (res) : "0" (x)); line is...
what does it do?
or how does it find the square root?
That code calls the processor sqrt instruction, and pretty much no, you cant make it any faster. You can make calcs faster by not using sqrt at all. There are lots of tricks. for example, in distance comparison, just use the squared values for comparison instead of square rootifying (thats the proper terminology :-P) hope that helps
Brian
Brian
Brian J
If you''re div''ing by sqrt(), you can use this:
Is is a modified version of a function to find a reciprocal square root. I forgot where I originally found the code, it was on a message board, but it''s supposedly from Quake 3. Here''s the version from the text at magic-software.com:
I made a few very miniscule alterations.
Anyway, both versions will find the reciprocal square root of x, although it''s not very precise, you can get a lot of optimization with it.
__inline float RSqrt (float a) { const float x2 = a * 0.5f; int i = * (int*) &a i = 0x5F3759DF - (i >> 1); // compute initial guess a = * (float*) &i return a * (1.5f - (x2 * a * a)); // refine once}
Is is a modified version of a function to find a reciprocal square root. I forgot where I originally found the code, it was on a message board, but it''s supposedly from Quake 3. Here''s the version from the text at magic-software.com:
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 made a few very miniscule alterations.
Anyway, both versions will find the reciprocal square root of x, although it''s not very precise, you can get a lot of optimization with it.
use sqrtf instead of sqrt... my testing has shown on my pc it''s a good 2x faster than sqrt... But, it''s a good 3x slower in debug builds, so adding debug macros to change the call to just (float)sqrt(). (it''s the same with sin/sinf, cos/tan/atan, etc)
| - Project-X - my mega project.. yup, still cracking along
- | - adDeath - an ad blocker I made - | - email me - |
| - Project-X - my mega project.. yup, still cracking along

ohh, and don''t assume that making your own taylor series approximations will be fast, cpus use these anyway (I believe). a while ago I wrote a 4 stage taylor sqrtf approximation. it was very very simple, and produced fairly good results (sqrt 16 was 4.003 or something like that), but it was some 3x slower than standard sqrtf, even though it was a very, very small bit of code.
| - Project-X - my mega project.. yup, still cracking along
- | - adDeath - an ad blocker I made - | - email me - |
| - Project-X - my mega project.. yup, still cracking along

This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement