[Bug c++/97965] New: constexpr evaluation vs. NaNs inconsistency

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Nov 24 10:21:52 GMT 2020


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

            Bug ID: 97965
           Summary: constexpr evaluation vs. NaNs inconsistency
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jakub at gcc dot gnu.org
  Target Milestone: ---

constexpr bool a = __builtin_nan ("") > 0.0;
constexpr bool b = __builtin_nans ("") > 0.0;
constexpr bool c = __builtin_nan ("") < 0.0;
constexpr bool d = __builtin_nans ("") < 0.0;

strangely accepts the < 0.0 comparisons and rejects the > 0.0 comparisons.
clang++ accepts all of them.  IMHO either we should accept all of them, or just
the ones not involving SNaNs, or reject all of them, it is unclear what
exceptions appart from division by zero (and does that apply to floating
point?) should cause constexpr evaluation to fail (I'd hope inexact exception
doesn't count, another question is underflow/overflow, another one is invalid
operations that from non-NaN operands create NaN, another one are operations
with NaNs, another one are operations with SNaNs).

Seems the reason why < 0.0 is accepted is fold_binary_loc uses
tree_expr_nonnegative_warnv_p on the NaN REAL_CST which in the end uses
tree_single_nonnegative_warnv_p which uses !REAL_VALUE_NEGATIVE.
While NaNs have a sign in the representation, it shouldn't affect behavior of
the comparisons, so I think we should never treat NaNs with the sign bit clear
as non-negative.


More information about the Gcc-bugs mailing list