public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-9166] i386: Fix __builtin_trunc with FE_DOWNWARD rounding direction [PR96793]
@ 2020-12-23 17:33 Uros Bizjak
  0 siblings, 0 replies; only message in thread
From: Uros Bizjak @ 2020-12-23 17:33 UTC (permalink / raw)
  To: gcc-cvs

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

commit r10-9166-ga5bcb9487dd62b853a6813387309e5f83a47ce0d
Author: Uros Bizjak <ubizjak@gmail.com>
Date:   Wed Dec 23 18:32:28 2020 +0100

    i386: Fix __builtin_trunc with FE_DOWNWARD rounding direction [PR96793]
    
    x86_expand_truncdf_32 expander uses x86_sse_copysign_to_positive, which
    is unable to change the sign from - to +.  When FE_DOWNWARD rounding
    direction is in effect, the expanded sequence that involves subtraction
    can trigger x - x = -0.0 special rule.  x86_sse_copysign_to_positive
    fails to change the sign of the intermediate value, assumed to always
    be positive, back to positive.
    
    The patch adds one extra fabs that strips the sign from the intermediate
    value when flag_rounding_math is in effect.
    
    2020-12-23  Uroš Bizjak  <ubizjak@gmail.com>
    
    gcc/
            PR target/96793
            * config/i386/i386-expand.c (ix86_expand_truncdf_32):
            Remove the sign of the intermediate value for flag_rounding_math.
    
    gcc/testsuite/
            PR target/96793
            * gcc.target/i386/pr96793-1.c: New test.

Diff:
---
 gcc/config/i386/i386-expand.c             | 27 ++++++++++++++-------------
 gcc/testsuite/gcc.target/i386/pr96793-1.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c
index 55474fbfa21..dd710809be2 100644
--- a/gcc/config/i386/i386-expand.c
+++ b/gcc/config/i386/i386-expand.c
@@ -16035,7 +16035,7 @@ void
 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
 {
   machine_mode mode = GET_MODE (operand0);
-  rtx xa, mask, TWO52, one, res, smask, tmp;
+  rtx xa, xa2, TWO52, tmp, one, res, mask;
   rtx_code_label *label;
 
   /* C code for SSE variant we expand below.
@@ -16058,28 +16058,29 @@ ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
   emit_move_insn (res, operand1);
 
   /* xa = abs (operand1) */
-  xa = ix86_expand_sse_fabs (res, &smask);
+  xa = ix86_expand_sse_fabs (res, &mask);
 
   /* if (!isless (xa, TWO52)) goto label; */
   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
 
-  /* res = xa + TWO52 - TWO52; */
-  tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
-  tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
-  emit_move_insn (res, tmp);
+  /* xa2 = xa + TWO52 - TWO52; */
+  xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
+  xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
 
   /* generate 1.0 */
   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
 
-  /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
-  mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
-  emit_insn (gen_rtx_SET (mask, gen_rtx_AND (mode, mask, one)));
+  /* Compensate: xa2 = xa2 - (xa2 > xa ? 1 : 0)  */
+  tmp = ix86_expand_sse_compare_mask (UNGT, xa2, xa, false);
+  emit_insn (gen_rtx_SET (tmp, gen_rtx_AND (mode, one, tmp)));
   tmp = expand_simple_binop (mode, MINUS,
-			     res, mask, NULL_RTX, 0, OPTAB_DIRECT);
-  emit_move_insn (res, tmp);
+			     xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
+  /* Remove the sign with FE_DOWNWARD, where x - x = -0.0.  */
+  if (flag_rounding_math)
+    tmp = ix86_expand_sse_fabs (tmp, NULL);
 
-  /* res = copysign (res, operand1) */
-  ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
+  /* res = copysign (xa2, operand1) */
+  ix86_sse_copysign_to_positive (res, tmp, res, mask);
 
   emit_label (label);
   LABEL_NUSES (label) = 1;
diff --git a/gcc/testsuite/gcc.target/i386/pr96793-1.c b/gcc/testsuite/gcc.target/i386/pr96793-1.c
new file mode 100644
index 00000000000..b205d39f63c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr96793-1.c
@@ -0,0 +1,28 @@
+/* PR target/96793 */
+/* { dg-do run { target sse2_runtime } } */
+/* { dg-require-effective-target fenv } */
+/* { dg-options "-O2 -frounding-math -msse2 -mno-sse4 -mfpmath=sse" } */
+
+#include <fenv.h>
+
+double
+__attribute__((noinline))
+test (double value)
+{
+  return __builtin_trunc (value);
+}
+
+int
+main ()
+{
+  double result;
+
+  fesetround (FE_DOWNWARD);
+
+  result = test (0.25);
+
+  if (__builtin_signbit (result) != 0)
+    __builtin_abort ();
+
+  return 0;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-23 17:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-23 17:33 [gcc r10-9166] i386: Fix __builtin_trunc with FE_DOWNWARD rounding direction [PR96793] Uros Bizjak

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).