/* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
-(for minmax (min max FMIN_ALL FMAX_ALL)
+(for minmax (min max)
(simplify
(minmax @0 @0)
@0))
+/* For fmin() and fmax(), skip folding when both are sNaN. */
+(for minmax (FMIN_ALL FMAX_ALL)
+ (simplify
+ (minmax @0 @0)
+ (if (!tree_expr_maybe_signaling_nan_p (@0))
+ @0)))
/* min(max(x,y),y) -> y. */
(simplify
(min:c (max:c @0 @1) @1)
(minmax @1 (convert @2)))))
(for minmax (FMIN_ALL FMAX_ALL)
- /* If either argument is NaN, return the other one. Avoid the
- transformation if we get (and honor) a signalling NaN. */
+ /* If either argument is NaN and other one is not sNaN, return the other
+ one. Avoid the transformation if we get (and honor) a signalling NaN. */
(simplify
(minmax:c @0 REAL_CST@1)
- (if (real_isnan (TREE_REAL_CST_PTR (@1))
- && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
+ (if (real_isnan (TREE_REAL_CST_PTR (@1))
+ && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
+ && !tree_expr_maybe_signaling_nan_p (@0))
@0)))
/* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
functions to return the numeric arg if the other one is NaN.
--- /dev/null
+/* { dg-do run { target { *-*-linux* *-*-gnu* } } } */
+/* { dg-options "-O1 -fsignaling-nans -lm" } */
+/* { dg-add-options ieee } */
+/* { dg-require-effective-target issignaling } */
+
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <math.h>
+
+int main()
+{
+ double a = __builtin_nans ("");
+
+ if (issignaling (fmin (a, a)))
+ __builtin_abort ();
+
+ if (issignaling (fmax (a, a)))
+ __builtin_abort ();
+
+ double b = __builtin_nan ("");
+
+ if (issignaling (fmin (a, b)))
+ __builtin_abort ();
+
+ if (issignaling (fmax (a, b)))
+ __builtin_abort ();
+
+ return 0;
+}