]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/84407 - honor -frounding-math for int to float conversion
authorRichard Biener <rguenther@suse.de>
Thu, 28 Oct 2021 09:38:32 +0000 (11:38 +0200)
committerRichard Biener <rguenther@suse.de>
Thu, 28 Oct 2021 13:45:22 +0000 (15:45 +0200)
This makes us honor -frounding-math for integer to float conversions
and avoid constant folding when such conversion is not exact.

2021-10-28  Richard Biener  <rguenther@suse.de>

PR middle-end/84407
* fold-const.c (fold_convert_const): Avoid int to float
constant folding with -frounding-math and inexact result.
* simplify-rtx.c (simplify_const_unary_operation): Likewise
for both float and unsigned_float.

* gcc.dg/torture/fp-uint64-convert-double-1.c: New testcase.
* gcc.dg/torture/fp-uint64-convert-double-2.c: Likewise.

gcc/fold-const.c
gcc/simplify-rtx.c
gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-2.c [new file with mode: 0644]

index 18950aeb760d6a4baaefd5c53ab3be9466c7c21c..c7daf8711256674a14981c4ca3711cb663cfc4cc 100644 (file)
@@ -2290,7 +2290,20 @@ fold_convert_const (enum tree_code code, tree type, tree arg1)
   else if (TREE_CODE (type) == REAL_TYPE)
     {
       if (TREE_CODE (arg1) == INTEGER_CST)
-       return build_real_from_int_cst (type, arg1);
+       {
+         tree res = build_real_from_int_cst (type, arg1);
+         /* Avoid the folding if flag_rounding_math is on and the
+            conversion is not exact.  */
+         if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
+           {
+             bool fail = false;
+             wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
+                                           TYPE_PRECISION (TREE_TYPE (arg1)));
+             if (fail || wi::ne_p (w, wi::to_wide (arg1)))
+               return NULL_TREE;
+           }
+         return res;
+       }
       else if (TREE_CODE (arg1) == REAL_CST)
        return fold_convert_const_real_from_real (type, arg1);
       else if (TREE_CODE (arg1) == FIXED_CST)
index f38b6d7d31c7d54a224af51cb045b9a4e6d88f06..a060f1bbce01f4afb4ffa01ccc808c07975ba81c 100644 (file)
@@ -1917,6 +1917,19 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
         return 0;
 
       d = real_value_truncate (mode, d);
+
+      /* Avoid the folding if flag_rounding_math is on and the
+        conversion is not exact.  */
+      if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
+       {
+         bool fail = false;
+         wide_int w = real_to_integer (&d, &fail,
+                                       GET_MODE_PRECISION
+                                         (as_a <scalar_int_mode> (op_mode)));
+         if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
+           return 0;
+       }
+
       return const_double_from_real_value (d, mode);
     }
   else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op))
@@ -1941,6 +1954,19 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
         return 0;
 
       d = real_value_truncate (mode, d);
+
+      /* Avoid the folding if flag_rounding_math is on and the
+        conversion is not exact.  */
+      if (HONOR_SIGN_DEPENDENT_ROUNDING (mode))
+       {
+         bool fail = false;
+         wide_int w = real_to_integer (&d, &fail,
+                                       GET_MODE_PRECISION
+                                         (as_a <scalar_int_mode> (op_mode)));
+         if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode))))
+           return 0;
+       }
+
       return const_double_from_real_value (d, mode);
     }
 
diff --git a/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c b/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c
new file mode 100644 (file)
index 0000000..b40a16a
--- /dev/null
@@ -0,0 +1,74 @@
+/* PR84407 */
+/* { dg-do run } */
+/* { dg-require-effective-target fenv } */
+/* { dg-additional-options "-frounding-math" } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+void __attribute__((noipa))
+fooa ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_TONEAREST
+  fesetround(FE_TONEAREST);
+  __UINT64_TYPE__ x = 0x7fffffffffffffff;
+  double f = x;
+  if (f != 0x1p+63)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+foob ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_DOWNWARD
+  fesetround(FE_DOWNWARD);
+  __UINT64_TYPE__ x = 0x7fffffffffffffff;
+  double f = x;
+  if (f != 0x1.fffffffffffffp+62)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+fooc ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_UPWARD
+  fesetround(FE_UPWARD);
+  __UINT64_TYPE__ x = 0x7fffffffffffffff;
+  double f = x;
+  if (f != 0x1p+63)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+food ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_TOWARDZERO
+  fesetround(FE_TOWARDZERO);
+  __UINT64_TYPE__ x = 0x7fffffffffffffff;
+  double f = x;
+  if (f != 0x1.fffffffffffffp+62)
+    abort ();
+#endif
+#endif
+}
+
+
+int
+main ()
+{
+  fooa ();
+  foob ();
+  fooc ();
+  food ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-2.c b/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-2.c
new file mode 100644 (file)
index 0000000..952f96b
--- /dev/null
@@ -0,0 +1,75 @@
+/* PR84407 */
+/* { dg-do run } */
+/* { dg-require-effective-target fenv } */
+/* { dg-additional-options "-frounding-math" } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+void __attribute__((noipa))
+fooa ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_TONEAREST
+  fesetround(FE_TONEAREST);
+  /* Large enough constant to trigger unsigned_float.  */
+  __UINT64_TYPE__ x = 0x8000000000000001;
+  double f = x;
+  if (f != 0x1p+63)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+foob ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_DOWNWARD
+  fesetround(FE_DOWNWARD);
+  __UINT64_TYPE__ x = 0x8000000000000001;
+  double f = x;
+  if (f != 0x1p+63)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+fooc ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_UPWARD
+  fesetround(FE_UPWARD);
+  __UINT64_TYPE__ x = 0x8000000000000001;
+  double f = x;
+  if (f != 0x1.0000000000001p+63)
+    abort ();
+#endif
+#endif
+}
+
+void __attribute__((noipa))
+food ()
+{
+#if __DBL_MANT_DIG__ == 53
+#ifdef FE_TOWARDZERO
+  fesetround(FE_TOWARDZERO);
+  __UINT64_TYPE__ x = 0x8000000000000001;
+  double f = x;
+  if (f != 0x1p+63)
+    abort ();
+#endif
+#endif
+}
+
+
+int
+main ()
+{
+  fooa ();
+  foob ();
+  fooc ();
+  food ();
+  return 0;
+}