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

            Bug ID: 102549
           Summary: Incorrect optimization if argument is SNAN
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Serge.Pavlov.at.gnu at gmail dot com
  Target Milestone: ---

GCC was taken at https://godbolt.org, the reported version is:

gcc
(Compiler-Explorer-Build-gcc-d6a87d96d7473cbd2404d5dcc7eef36a7f53b2b2-binutils-2.36.1)
12.0.0 20210929 (experimental)

The program is:

extern int printf(const char *fmt, ...);
union {
    double f;
    long long i;
} val;
double add_zero(double x) {
    return x + -0.0;
}
int main(int arc, char *argv[]) {
    val.i = 0x7ff4000000000000LL; // SNaN
    val.f = add_zero(val.f);
    printf("%llx\n", val.i);
    return 0;
}

Compiled with options "-O2" this program prints
(https://godbolt.org/z/YE13qEhf9):
7ff4000000000000

This is incorrect as this is SNaN, but according to IEEE 754-2008, 6.2:

Under default exception handling, any operation signaling an invalid operation
exception and for which a
floating-point result is to be delivered shall deliver a quiet NaN.
Signaling NaNs shall be reserved operands that, under default exception
handling, signal the invalid
operation exception (see 7.2) for every general-computational and
signaling-computational operation except
for the conversions described in 5.12.

And according to the latest standard draft
(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf), F.3p1:

C operators, functions, and function-like macros provide operations specified
by IEC 60559 as shown
in the following table. ... The C specifications are
intended to match IEC 60559, unless stated otherwise.

So a QNaN is expected here.

If "-0.0" is replaced with the positive zero:

double add_zero(double x) {
    return x + 0.0;
}

the behavior is as expected, the program prints:
7ffc000000000000

which is QNaN.

Reply via email to