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 <ubiz...@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. Bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. Pushed to mainline, will be backported to other release branches. Uros.
diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c index 7c31cc7daac..f838112783c 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 00000000000..4a96478e31e --- /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; +}