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.