The trap of carmark's square root Function Code

Source: Internet
Author: User
First, thanks to xq; lj for locating the problem!

 

The problem lies in the code of the square root function that I checked in from carmark.

Code
Double InvSqrt (double number)
{
_ Int64 I;
Double x2, y;
Const double threehalfs = 1.5F;

X2 = number * 0.5F;
Y = number;
I = * (_ int64 *) & y;
I = 0x5fe6ec85e7de30da-(I> 1 );
Y = * (double *) & I;
Y = y * (threehalfs-(x2 * y); // 1st iteration
Y = y * (threehalfs-(x2 * y); // 2nd iteration, this can be removed
Return y;
}

The red part of the code will get the wrong code after gcc enables the-fstrict-aliasing option. Because type-punned pointer is used, strict-aliasing rules are broken.

Because the-fstrict-aliasing option is enabled in-O2,-O3,-OS and other optimization modes (dev is not optimized currently, main with-O3, so this problem only occurs on main. Therefore, it is recommended to generate
Warning: dereferencing type-punned pointer will break strict-aliasing rule
The warning is used as a compilation failure. To prevent similar problems.
The code above should be rewritten using a consortium:

Code
Double InvSqrt (double number)
{
Double x2, y;
Const double threehalfs = 1.5F;
Union
{
Double d;
_ Int64 I;
} D;
X2 = number * 0.5F;
Y = number;
D. d = y;
D. I = 0x5fe6ec85e7de30da-(d. I> 1 );
Y = d. d;
Y = y * (threehalfs-(x2 * y); // 1st iteration
Y = y * (threehalfs-(x2 * y); // 2nd iteration, this can be removed
Return y;
}

In this way, the rule will not be broken.

What is Strict Aliasing? Please see: http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html?

In C99, it isIllegalTo create an alias of a different type than the original.

In our current code, you can press the regular expression \ ([^ ()] + \ *: B * \): B * & find the approximate180Matching rows, involving60Files.
Some of them do not meet the strict-aliasing requirements.
Solution
1. temporarily disable strict-aliasing: Use-fno-strict-aliasing to disable strict-aliasing when compiling with-O2,-O3, and-OS.
2. You need to modify this type of code that does not meet this restriction and open it after the modification is complete.
Note that the strict-aliasing option prompts the compiler code to meet this restriction, so it can provide the compiler with the opportunity to perform additional code optimization.

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.