Honor -frounding-math in real truncation (PR middle-end/57245)

Resolves: RHEL-73749
This commit is contained in:
Marek Polacek 2025-01-21 14:10:48 -05:00
parent 33c7fdbe53
commit 961c747afd
2 changed files with 105 additions and 1 deletions

View File

@ -4,7 +4,7 @@
%global gcc_major 11
# Note, gcc_release must be integer, if you want to add suffixes to
# %%{release}, append them after %%{gcc_release} on Release: line.
%global gcc_release 2
%global gcc_release 3
%global nvptx_tools_gitrev 5f6f343a302d620b0868edab376c00b15741e39e
%global newlib_cygwin_gitrev 50e2a63b04bdd018484605fbb954fd1bd5147fa0
%global _unpackaged_files_terminate_build 0
@ -299,6 +299,7 @@ Patch36: gcc11-libgfortran-flush.patch
Patch37: gcc11-pr113960.patch
Patch38: gcc11-pr105157.patch
Patch39: gcc11-testsuite-fixes-4.patch
Patch40: gcc11-pr57245.patch
Patch100: gcc11-fortran-fdec-duplicates.patch
Patch101: gcc11-fortran-flogical-as-integer.patch
@ -903,6 +904,7 @@ mark them as cross compiled.
%patch37 -p1 -b .pr113960~
%patch38 -p1 -b .pr105157~
%patch39 -p1 -b .testsuite4~
%patch40 -p1 -b .pr57245~
%if 0%{?rhel} >= 9
%patch100 -p1 -b .fortran-fdec-duplicates~
@ -3596,6 +3598,9 @@ end
%endif
%changelog
* Tue Jan 21 2025 Marek Polacek <polacek@redhat.com> 11.5.0-3
- honor -frounding-math in real truncation (PR middle-end/57245, RHEL-73749)
* Mon Jul 22 2024 Marek Polacek <polacek@redhat.com> 11.5.0-2
- fix TARGET_CPU_DEFAULT (PR target/105157, RHEL-50037)
- libstdc++: Workaround kernel-headers on s390x-linux (RHEL-50054)

99
gcc11-pr57245.patch Normal file
View File

@ -0,0 +1,99 @@
commit a84b9d5373c7e67fd0ab2a412c22162cdf969c91
Author: Richard Biener <rguenther@suse.de>
Date: Wed Oct 27 14:27:40 2021 +0200
middle-end/57245 - honor -frounding-math in real truncation
The following honors -frounding-math when converting a FP constant
to another FP type.
2021-10-27 Richard Biener <rguenther@suse.de>
PR middle-end/57245
* fold-const.c (fold_convert_const_real_from_real): Honor
-frounding-math if the conversion is not exact.
* simplify-rtx.c (simplify_const_unary_operation): Do not
simplify FLOAT_TRUNCATE with sign dependent rounding.
* gcc.dg/torture/fp-double-convert-float-1.c: New testcase.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index ff23f12f33c..18950aeb760 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -2139,6 +2139,12 @@ fold_convert_const_real_from_real (tree type, const_tree arg1)
&& REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
return NULL_TREE;
+ /* With flag_rounding_math we should respect the current rounding mode
+ unless the conversion is exact. */
+ if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
+ && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
+ return NULL_TREE;
+
real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
t = build_real (type, value);
diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c
index bbbd6b74942..f38b6d7d31c 100644
--- a/gcc/simplify-rtx.c
+++ b/gcc/simplify-rtx.c
@@ -2068,6 +2068,11 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
and the operand is a signaling NaN. */
if (HONOR_SNANS (mode) && REAL_VALUE_ISSIGNALING_NAN (d))
return NULL_RTX;
+ /* Or if flag_rounding_math is on and the truncation is not
+ exact. */
+ if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)
+ && !exact_real_truncate (mode, &d))
+ return NULL_RTX;
d = real_value_truncate (mode, d);
break;
case FLOAT_EXTEND:
diff --git a/gcc/testsuite/gcc.dg/torture/fp-double-convert-float-1.c b/gcc/testsuite/gcc.dg/torture/fp-double-convert-float-1.c
new file mode 100644
index 00000000000..ec23274ea98
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/fp-double-convert-float-1.c
@@ -0,0 +1,41 @@
+/* PR57245 */
+/* { dg-do run } */
+/* { dg-require-effective-target fenv } */
+/* { dg-additional-options "-frounding-math" } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+#if __DBL_MANT_DIG__ == 53 && __FLT_MANT_DIG__ == 24
+#ifdef FE_UPWARD
+ fesetround (FE_UPWARD);
+ float f = 1.3;
+ if (f != 0x1.4ccccep+0f)
+ __builtin_abort ();
+#endif
+#ifdef FE_TONEAREST
+ fesetround (FE_TONEAREST);
+ /* Use different actual values so the bogus CSE we perform does not
+ break things. */
+ f = 1.33;
+ if (f != 0x1.547ae2p+0f)
+ abort ();
+#endif
+#ifdef FE_DOWNWARD
+ fesetround (FE_DOWNWARD);
+ f = 1.333;
+ if (f != 0x1.553f7cp+0f)
+ abort ();
+#endif
+#ifdef FE_TOWARDZERO
+ fesetround (FE_TOWARDZERO);
+ f = 1.3333;
+ if (f != 0x1.555326p+0f)
+ abort ();
+#endif
+#endif
+ return 0;
+}