Updated: Someone asked this question.Algorithm. In fact, the principle is very simple. It is the root of Newton iteration. The place where the cockmark algorithm is ox X is that he chooses a constant as the starting value. This starting value makes it enough for him to use only one iteration.
From this point of view. Quakeiii is naturally one of the masterpieces of the legendary capma. On Some CPUs, this function is four times faster than ordinary (float) (1.0/SQRT (x! One of the reasons is that a mysterious constant, 0x5f3759df, is used. Chris lomont of Pudu University discussed the significance of this constant in this paper, try to use a strict method to derive this constant (he also mentioned that someone thought this function was written by Gary tarolli, which worked in NVIDIA ). The constant introduced by Chris is 0x5f37642f. It is slightly different from q_rsqrt, and the actual performance is slightly inferior. It is a mystery how kakmark releases this constant. It is a pity that such experts do not write books.
Float q_rsqrt (float number)
{
Long I;
Float X2, Y;
Const float threehalfs = 1.5f;
X2 = Number * 0.5f;
Y = number;
I = * (long *) & Y; // edevil floating point bit level hacking
I = 0x5f3759df-(I> 1); // What is the fuck?
Y = * (float *) & I;
Y = y * (threehalfs-(X2 * y); // 1st Iteration
// Y = y * (threehalfs-(X2 * y); // 2nd iteration, this can be removed
# Ifndef q3_vm
# Ifdef _ Linux __
Assert (! Isnan (y); // bk010122-FPE?
# Endif
# Endif
Return y;
}