Reading through The Tricks of the 3D Game Programming Gurus, I came across this sort function written in inline assembly:
inline float FastSqrt(float Value)
{
float Result;
_asm
{
mov eax, Value
sub eax, 0x3F800000
sar eax, 1
add eax, 0x3F800000
mov Result, eax
}
return(Result);
}
It is an approximation of the actual square-root, but the accuracy is enough for my needs.
How does this actually work? What is this magical 0x3F800000
value? How are we achieving square root by subtracting, rotating and adding?
Here's how it looks in C/C++ code:
inline float FastSqrt_C(float Value)
{
float Result;
long Magic = *((long *)&Value);
Magic -= 0x3F800000;
Magic >>= 1;
Magic += 0x3F800000;
Result = *((float *)&Magic);
return(Result);
}