Code: Select all
int sqrt_int(int x)
{
unsigned int ret = 44; // starting point is relatively unimportant
for (int i = 0; i < 6; i++)
{
ret = (ret + x / ret) / 2;
}
// almost exact for x ~ 1 .. 500000
return ret;
}
Code: Select all
int sqrt_int(int x)
{
unsigned int ret = 44; // starting point is relatively unimportant
for (int i = 0; i < 6; i++)
{
ret = (ret + x / ret) / 2;
}
// almost exact for x ~ 1 .. 500000
return ret;
}
Code: Select all
#include <stdint.h>
float Q_rsqrt( float number )
{
const float x2 = number * 0.5F;
const float threehalfs = 1.5F;
union {
float f;
uint32_t i;
} conv = {number}; // member 'f' set to value of 'number'.
conv.i = 0x5f3759df - ( conv.i >> 1 );
conv.f *= threehalfs - x2 * conv.f * conv.f;
return conv.f;
}
Code: Select all
float inv_sqrt(float x)
{
union { float f; uint32 u; }
y = {x};
y.u = 0x5F1FFFF9ul - (y.u >> 1);
return 0.703952253f * y.f * (2.38924456f - x * y.f * y.f);
}