aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorUros Bizjak <ubizjak@gmail.com>2020-12-22 18:13:24 +0100
committerUros Bizjak <ubizjak@gmail.com>2020-12-22 18:14:51 +0100
commit337ed0eb490b14899f4049bc4c8922eb1d8a2e67 (patch)
treecd68fc8e6ed92d585b4baa8b6c3f898d0c6d0c58 /gcc
parent7e63d383b89cd75b7775883d71ed09724b9ee73f (diff)
downloadgcc-337ed0eb490b14899f4049bc4c8922eb1d8a2e67.zip
gcc-337ed0eb490b14899f4049bc4c8922eb1d8a2e67.tar.gz
gcc-337ed0eb490b14899f4049bc4c8922eb1d8a2e67.tar.bz2
i386: Fix __builtin_floor with FE_DOWNWARD rounding direction [PR96793]
x86_expand_floorceil 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-22 Uroš Bizjak <ubizjak@gmail.com> gcc/ PR target/96793 * config/i386/i386-expand.c (ix86_expand_floorceil): Remove the sign of the intermediate value for flag_rounding_math. (ix86_expand_floorceildf_32): Ditto. gcc/testsuite/ PR target/96793 * gcc.target/i386/pr96793.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/config/i386/i386-expand.c25
-rw-r--r--gcc/testsuite/gcc.target/i386/pr96793.c28
2 files changed, 48 insertions, 5 deletions
diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c
index 7c31cc7..f838112 100644
--- a/gcc/config/i386/i386-expand.c
+++ b/gcc/config/i386/i386-expand.c
@@ -16352,12 +16352,14 @@ ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
if (!isless (xa, TWO52))
return x;
x2 = (double)(long)x;
+
Compensate. Floor:
if (x2 > x)
x2 -= 1;
Compensate. Ceil:
if (x2 < x)
x2 += 1;
+
if (HONOR_SIGNED_ZEROS (mode))
return copysign (x2, x);
return x2;
@@ -16392,10 +16394,15 @@ ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
emit_insn (gen_rtx_SET (tmp, gen_rtx_AND (mode, one, tmp)));
tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
- emit_move_insn (res, tmp);
-
if (HONOR_SIGNED_ZEROS (mode))
- ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
+ {
+ /* Remove the sign with FE_DOWNWARD, where x - x = -0.0. */
+ if (do_floor && flag_rounding_math)
+ tmp = ix86_expand_sse_fabs (tmp, NULL);
+
+ ix86_sse_copysign_to_positive (tmp, tmp, res, mask);
+ }
+ emit_move_insn (res, tmp);
emit_label (label);
LABEL_NUSES (label) = 1;
@@ -16415,12 +16422,14 @@ ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
return x;
xa = xa + TWO52 - TWO52;
x2 = copysign (xa, x);
+
Compensate. Floor:
if (x2 > x)
x2 -= 1;
Compensate. Ceil:
if (x2 < x)
x2 += 1;
+
if (HONOR_SIGNED_ZEROS (mode))
x2 = copysign (x2, x);
return x2;
@@ -16457,8 +16466,14 @@ ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
emit_insn (gen_rtx_SET (tmp, gen_rtx_AND (mode, one, tmp)));
tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
- if (!do_floor && HONOR_SIGNED_ZEROS (mode))
- ix86_sse_copysign_to_positive (tmp, tmp, res, mask);
+ if (HONOR_SIGNED_ZEROS (mode))
+ {
+ /* Remove the sign with FE_DOWNWARD, where x - x = -0.0. */
+ if (do_floor && flag_rounding_math)
+ tmp = ix86_expand_sse_fabs (tmp, NULL);
+
+ ix86_sse_copysign_to_positive (tmp, tmp, res, mask);
+ }
emit_move_insn (res, tmp);
emit_label (label);
diff --git a/gcc/testsuite/gcc.target/i386/pr96793.c b/gcc/testsuite/gcc.target/i386/pr96793.c
new file mode 100644
index 0000000..4a96478
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr96793.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_floor (value);
+}
+
+int
+main ()
+{
+ double result;
+
+ fesetround (FE_DOWNWARD);
+
+ result = test (0.25);
+
+ if (__builtin_signbit (result) != 0)
+ __builtin_abort ();
+
+ return 0;
+}