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)
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))
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);
}
--- /dev/null
+/* 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;
+}
--- /dev/null
+/* 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;
+}