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

            Bug ID: 127785
           Summary: !__builtin_isunordered (a, b) & (a != b) folded to
                    LTGT raises FE_INVALID for a quiet NaN
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: igor at tachyum dot com
  Target Milestone: ---

Created attachment 65787
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65787&action=edit
ordered-ne.c

With the default -ftrapping-math, neither !__builtin_isunordered (a, b) nor
a != b raises FE_INVALID for a quiet NaN operand, but match.pd folds their
conjunction to a <> b (LTGT_EXPR), which is a signaling comparison. The
attached testcase aborts at every optimization level, -O0 included, on
x86_64 trunk (r17-4942) and on GCC 16.2:

$ gcc -O0 ordered-ne.c -lm && ./a.out
Aborted

  int ord_and_ne (double a, double b)
  { return !__builtin_isunordered (a, b) & (a != b); }

ord_and_ne (NaN, 1.0) returns 0 as expected, but FE_INVALID is set.

The fold should only be done when trapping math is not honored (or the
operands cannot be NaN); with -fno-trapping-math LTGT is fine. Patch to
follow.

Reply via email to