First, it is the qsort of CREAMK:
- float q_rsqrt (float number)
- {
- Long I;
- float x2, y;
- const float Threehalfs = 1.5F;
- x2 = number * 0.5F;
- y = number;
- i = * (long *) &y; Evil floating point bit level hacking
- i = 0x5f3759df-(i >> 1); What's the fuck?
- y = * (float *) &i;
- y = y * (Threehalfs-(x2 * y * y)); 1st Iteration
- y = y * (Threehalfs-(x2 * y * y)); 2nd iteration, this can removed
- #ifndef Q3_VM
- #ifdef __linux__
- Assert (!isnan (y)); Bk010122-fpe?
- #endif
- #endif
- return y;
- }
This code solves the 1.0/SQRT (x);
and the simple implementation code in C + +:
| 123456789101112 |
staticfloatCarmackSqrt (floatx){ floatxhalf = 0.5f * x; inti = *(int*)&x; // get bits for floating VALUE i = 0x5f3759df - (i>>1); // gives initial guess y0 x = *(float*)&i; // convert bits BACK to float x = x*(1.5f - xhalf*x*x); // Newton step, repeating increases accuracy x = x*(1.5f - xhalf*x*x); // Newton step, repeating increases accuracy x = x*(1.5f - xhalf*x*x); // Newton step, repeating increases accuracy return(1 / x);} |
After testing, this code is 4 times times the efficiency of sqrt in STL. Why is this code so efficient when it comes to the problem of spicy?
First, CREAMK uses the general method of solving the square root: Newton iterative method, the principle is as follows:
Set R is the root, select x0 as the initial approximate value of R, over point The equation for the tangent l,l of the curve is to find the horizontal axis of the intersection of L and X axis, which is called X1 as an approximate value of R. Point the tangent of the curve and ask the horizontal axis of the intersection of the tangent and the x-axis, saying Two approximate values for R. Repeat the above process to get a sequence of approximate values of R, where, Called R.The quadratic approximation, which is called the Newton iterative formula. using Newton iterative method to solve the nonlinear equation is an approximate method to linearization the nonlinear equation. PutExpand the expansions series in a neighborhood of a point x0, take its linear part (that is, the first two of Taylor's expansions), and make it equal to 0, i.e., as the approximate equation of the nonlinear equation
, then its solution
, so that an iterative formula for Newton's iterative method is obtained:
. It has been shown that if it is continuous and the 0 points to be asked to be isolated, there is an area around 0 points, so long as the initial value is in the adjacent region, then Newton's method must converge. And, if it is not 0, then Newton's method will have a performance of square convergence. Roughly speaking, this means that the effective number of Newton's results will increase by one more times per iteration. After multiple iterations, the number must converge to the root of the equation. The 2nd is that he used the magic number 0X5F3759DF, making the iterative method of efficiency greatly increased, can only use three steps even out of sqrt. The process of finding this number is CREAMK, but even the Purdue mathematician Chris Lomont failed to figure out how much more efficient he was:
Chris Lomon, a mathematician at Purdue University, thought it was interesting to see Creamk's quick sqrt and decided to look at the mystery of what Creamk had to say about this guess. Lomont, after all, was a mathematician, and after careful study, he deduced from the theory that a
The best guess value, and the CREAMK number is very close to the 0x5f37642f. Lomont calculated the results are very satisfied, so take their own calculated starting value and CREAMK mysterious numbers to do the game, to see whose numbers can be faster and more accurate to obtain square root. The result is that Creamk won. No one knows how Creamk found the number.
Finally Lomont, using violence method A number a number to try, and finally found a higher than the number of CREAMK digital efficiency figures, although in fact, the results of these two numbers are very similar, the violence of the figure is 0x5f375a86.
Lomont wrote the next paper, "Fast inverse Square Root".
in the case of sqrt operations that require large amounts of data, the CREAMK qsqrt will be more efficient than sqrt in the STL library stint.
So when you feel the need to use it, enjoy it!
Qsqrt and its principle of cream