Evaluate the square root function in Raytheon hammer III

Source: Internet
Author: User
Tags float number
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;
}

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.