[Bug c++/95351] New: Comparison with NAN optimizes incorrectly with -ffast-math disabled

chris.dahlberg at cytovale dot com gcc-bugzilla@gcc.gnu.org
Tue May 26 21:39:30 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95351

            Bug ID: 95351
           Summary: Comparison with NAN optimizes incorrectly with
                    -ffast-math disabled
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chris.dahlberg at cytovale dot com
  Target Milestone: ---

Created attachment 48610
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48610&action=edit
nan_bug.ii:  preprocessed output from --save-temps

The function

bool Foo(double possiblyNAN, double b, double c)
{
    return (possiblyNAN <= 2.0) || ((possiblyNAN  > 2.0) && (b > c));
}


is optimized incorrectly with -O1.  It gets reduced to just '(b > c)', ignoring
the possibility that 'possiblyNAN' is NAN and both comparisons vs 2.0 are
false.  -ffinite-math-only is off (both by default and explicitly, no
difference)

The command line I'm using is:

"g++ -Wall -Wextra -O -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fno-finite-math-only nan_bug.cpp"

No warnings are output

Looking at the assembly, 'possiblyNAN' is never accessed at all

Tried this on these gcc versions and all have the same behavior:
- gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
- gcc version 8.4.0 (Ubuntu 8.4.0-1ubuntu1~18.04)
- gcc version 10.1.0 (Compiler-Explorer-Build)
- gcc version 11.0.0 20200525 (experimental) (Compiler-Explorer-Build)


More information about the Gcc-bugs mailing list