[The same cure is needed for __builtin_trunc on 32bit SSE targets.] 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 <ubiz...@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. 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 f838112783c..c856489c046 100644 --- a/gcc/config/i386/i386-expand.c +++ b/gcc/config/i386/i386-expand.c @@ -16534,7 +16534,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. @@ -16557,28 +16557,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; +}