Gcc -Ofast - complete list of restrictions

I am using the -Ofast

gcc parameter in my program to cause delays. I wrote a simple test program:

#include <iostream>
#include <math.h>

static double quiet_NaN = std::numeric_limits<double>::quiet_NaN();

int main()
{
    double newValue = 130000; 
    double curValue = quiet_NaN; 
    printf("newValue = %f\n", newValue); 
    printf("curValue = %f\n", curValue); 
    printf("isnan(newValue) = %d\n", isnan(newValue)); 
    printf("isnan(curValue) = %d\n", isnan(curValue)); 
    printf("newValue == curValue %d\n", (newValue == curValue)); 
    printf("newValue != curValue %d\n", (newValue != curValue)); 
}

      

I tried to run it with the default flags and with -Ofast:

$ g++ TestPointer.cpp 
$./a.out 
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 0
newValue != curValue 1

$ g++ -Ofast TestPointer.cpp 
$ ./a.out 
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 1
newValue != curValue 0

      

Thus, the result !=

and ==

can not be trusted. Does this mean that I have to ==

and !=

only when it is known that both values ​​are not nan, otherwise I have to check with isnan

before?

Is it guaranteed to isnan

work correctly with -Ofast

? What is correct ==

and !=

working for double with -Ofast

? Can anyone provide a complete list of the restrictions added -Ofast

?

+3


source to share


1 answer


You are seeing the effects -ffast-math

.

From the docs :

-Ofast

Do not follow strict adherence to standards. -Ofast allows you to optimize all -O3. This also provides optimizations that are not valid for all standard programs. It includes -ffast-math and Fortran-specific -fno-protect-parens and -fstack-arrays.

and

-ffast mathematician

Installs -fno-math-errno, -funsafe-math-optimizations, -fno-trapping-math, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans, and fcx-limited-range.

and

-ffinite-math only

Allow optimizations for floating point arithmetic that assume arguments and results are not NaN or + -Infs.



There are several gcc error reports for this flagged invalid.

Problems with -ffast-math and isnan

Also, IEEE strict floating point comparison always fails.

Checking if double (or float) is NaN in C ++

It doesn't necessarily apply to -ffast-math

, but explains what you're showing.

gcc does not provide a formal standard for how -ffast-math

floating point works, so you just need to work out the details empirically if you should and shouldn't be consistent between gcc versions. Better yet, avoid the combination of NaN

and completely -ffast-math

.

+7


source







All Articles