The inverse of the square root of a floating-point number is often used to calculate a normalized vector. 3D graphics programs need to use a normalized vector to achieve the lighting and projection effect, so every second to do the million square root of the reciprocal operation, and in the processing of coordinate conversion and light source of specialized hardware equipment, these calculations are completed by the software, the calculation speed is also quite slow. When this code was developed in the 1990 's, the speed of most floating-point operations was far behind the integer operation. Therefore, it is very important to optimize the regularization vector algorithm. The following statement calculates the principle of normalized vectors:
To standardize a vector, it is necessary to calculate its Euclidean norm in order to obtain the length of the vector, for which the sum of squares of the vectors and the square root of each component of the vector is required, and when the length is obtained and each component of the vector is obtained, the new vector is the same unit vector as the original vector.
When the normalized vector is visible, it is necessary to calculate the square root of the vector component, so the optimization of the inverse square root algorithm is also helpful to calculate the normalized vector.
To speed up the calculation of the image processing unit, the "Hammer III Arena" uses the square root inverse rate algorithm.
floatQ_RSQRT (floatNumber ) { Longi; floatx2, y; Const floatThreehalfs =1.5F; X2= number *0.5F; Y=Number ; I= * (Long*) &y;//Evil floating point bit levels hacking (evil bit level hack for floating points)i =0X5F3759DF-(I >>1);//What's the fuck? (what the fuck is going on?) )y = * (float*) &i; Y= y * (Threehalfs-(x2 * y * y));//1st Iteration (first Newton iteration)//y = y * (Threehalfs-(x2 * y * y)); //2nd iteration, this can is removed (second iteration, can be deleted) returny;}
For more reading on this code, please refer to Wikipedia.
Fast invsqrt () (square root inverse rate algorithm)