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

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #5 from liu zhenyu <lzy20000419 at outlook dot com> ---
Without -fsignaling-nans GCC does not promise to preserve sNaN semantics, but
the same thing happens with -fsignaling-nans, where it does. These all return
the sNaN unchanged and raise no FE_INVALID, at -O0 as well as -O2:

    #define _GNU_SOURCE
    #include <math.h>
    #include <fenv.h>
    #include <stdio.h>

    __attribute__((noipa)) float  f1 (float x)  { return (float) (double) x; }
    __attribute__((noipa)) float  f2 (float x)  { return (float) -(double) x; }
    __attribute__((noipa)) double f3 (double x) { return (double) (long double)
x; }

    int main (void)
    {
      volatile float s = __builtin_nansf ("");
      volatile double sd = __builtin_nans ("");
      feclearexcept (FE_ALL_EXCEPT);
      float r1 = f1 (s), r2 = f2 (s); double r3 = f3 (sd);
      printf ("%d %d %d FE_INVALID=%d\n", issignaling (r1), issignaling (r2),
issignaling (r3),
              fetestexcept (FE_INVALID) != 0);
      return 0;
    }

    $ gcc -O2 -fsignaling-nans t.c -lm && ./a.out
    1 1 1 FE_INVALID=0

Expected 0 0 0 FE_INVALID=1. x86_64 4.8.5, 5.4, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5,
12.5, 13.4, 14.4, 15.3, 16.2 and trunk (17.0.0 20261119) all print 1 1 1
FE_INVALID=0 at -O0 and -O2, so this is not a regression.

The identity is applied in three places, none of which checks HONOR_SNANS:

1. match.pd, the nested conversion simplifier: the arm "converting something to
its own type via an object of identical or wider precision" (inter_float &&
final_float && inter_prec >= final_prec gives @0), and the following arm for an
intermediate type wider than the initial one. It already fires in GENERIC: with
-O0 -fsignaling-nans the original dump of f1 and f3 is "return x;". It fires
again in GIMPLE: float g (float x) { double d = x; return (float) d; } returns
the sNaN at -O1 and above but not at -O0.

2. convert_to_real_1 (gcc/convert.cc) moves the narrowing conversion inside
NEGATE_EXPR and ABS_EXPR, which turns f2 into "return -x;" in the original
dump.

3. simplify-rtx.cc, FLOAT_TRUNCATE in simplify_unary_operation_1:
(float_truncate:SF (float_extend:DF x:SF)) is simplified to x, and the
(float_truncate (float_extend)) case right after it reaches the same result
through simplify_gen_unary.

So a fix probably needs all three to keep the round trip when HONOR_SNANS is
true for the mode involved.

Godbolt (trunk -O2 -fsignaling-nans, with execution):
https://godbolt.org/z/YfocezTf3

Suggested fields: Known to fail 4.8.5, 5.4.0, 6.5.0, 7.5.0, 8.5.0, 9.5.0,
10.5.0, 11.5.0, 12.5.0, 13.4.0, 14.4.0, 15.3.0, 16.2.0, 17.0.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to