public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-4764] middle-end/57245 - honor -frounding-math in real truncation
Date: Thu, 28 Oct 2021 09:29:01 +0000 (GMT)	[thread overview]
Message-ID: <20211028092901.43ED43858404@sourceware.org> (raw)

https://gcc.gnu.org/g:a84b9d5373c7e67fd0ab2a412c22162cdf969c91

commit r12-4764-ga84b9d5373c7e67fd0ab2a412c22162cdf969c91
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:
---
 gcc/fold-const.c                                   |  6 ++++
 gcc/simplify-rtx.c                                 |  5 +++
 .../gcc.dg/torture/fp-double-convert-float-1.c     | 41 ++++++++++++++++++++++
 3 files changed, 52 insertions(+)

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;
+}


                 reply	other threads:[~2021-10-28  9:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211028092901.43ED43858404@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).