Fast invsqrt () (square root inverse rate algorithm)

Source: Internet
Author: User
Tags square root

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.