public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
@ 2021-04-16 15:32 chfast at gmail dot com
  2021-04-19  7:04 ` [Bug target/100119] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: chfast at gmail dot com @ 2021-04-16 15:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

            Bug ID: 100119
           Summary: [x86] Conversion unsigned int -> double produces -0
                    (-m32 -msse2 -mfpmath=sse)
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chfast at gmail dot com
  Target Milestone: ---

When building for 32-bit x86 but with SSE2 floating-point enabled:
-m32 -msse2 -mfpmath=sse

the conversion from unsigned int 0 to double produces the result of -0.0 when
floating-point rounding mode is set to FE_DOWNWARD.

I used -frounding-math and #pragma STDC FENV_ACCESS ON.

This bug is not present on x87 nor x86_64 builds.

The bug seems to be present at least since GCC 5.


#include <fenv.h>

#pragma STDC FENV_ACCESS ON

__attribute__((noinline)) double u32_to_f64(unsigned x) {
  return static_cast<double>(x);
}

int main() {
  fesetround(FE_DOWNWARD);

  double d = u32_to_f64(0);

  return __builtin_signbit(d) != 0;  // signbit should be 0
}


The assembly:

u32_to_f64(unsigned int):
        sub     esp, 12
        pxor    xmm0, xmm0
        mov     eax, DWORD PTR [esp+16]
        add     eax, -2147483648
        cvtsi2sd        xmm0, eax
        addsd   xmm0, QWORD PTR .LC0
        movsd   QWORD PTR [esp], xmm0
        fld     QWORD PTR [esp]
        add     esp, 12
        ret
main:
        lea     ecx, [esp+4]
        and     esp, -16
        push    DWORD PTR [ecx-4]
        push    ebp
        mov     ebp, esp
        push    ecx
        sub     esp, 32
        push    1024
        call    fesetround
        mov     DWORD PTR [esp], 0
        call    u32_to_f64(unsigned int)
        mov     ecx, DWORD PTR [ebp-4]
        add     esp, 16
        fstp    QWORD PTR [ebp-16]
        movsd   xmm0, QWORD PTR [ebp-16]
        leave
        lea     esp, [ecx-4]
        movmskpd        eax, xmm0
        and     eax, 1
        ret
.LC0:
        .long   0
        .long   1105199104


https://godbolt.org/z/rrMWY9jsG

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug target/100119] [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
  2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
@ 2021-04-19  7:04 ` rguenth at gcc dot gnu.org
  2021-04-19  9:14 ` ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-19  7:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-04-19
             Target|x86_64-*-* i?86-*-*         |i?86-*-*
      Known to fail|                            |10.3.0, 11.0, 7.5.0

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug target/100119] [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
  2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
  2021-04-19  7:04 ` [Bug target/100119] " rguenth at gcc dot gnu.org
@ 2021-04-19  9:14 ` ubizjak at gmail dot com
  2021-04-22 14:32 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-04-19  9:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

--- Comment #2 from Uroš Bizjak <ubizjak at gmail dot com> ---
diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c
index dda08ff67f2..5a7a00c13bd 100644
--- a/gcc/config/i386/i386-expand.c
+++ b/gcc/config/i386/i386-expand.c
@@ -1550,6 +1550,8 @@ ix86_expand_convert_uns_sixf_sse (rtx, rtx)
   gcc_unreachable ();
 }

+static rtx ix86_expand_sse_fabs (rtx op0, rtx *smask);
+
 /* Convert an unsigned SImode value into a DFmode.  Only currently used
    for SSE, but applicable anywhere.  */

@@ -1569,6 +1571,11 @@ ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
   x = const_double_from_real_value (TWO31r, DFmode);

   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
+
+  /* Remove the sign with FE_DOWNWARD, where x - x = -0.0.  */
+  if (HONOR_SIGNED_ZEROS (DFmode) && flag_rounding_math)
+    x = ix86_expand_sse_fabs (x, NULL);
+
   if (x != target)
     emit_move_insn (target, x);
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug target/100119] [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
  2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
  2021-04-19  7:04 ` [Bug target/100119] " rguenth at gcc dot gnu.org
  2021-04-19  9:14 ` ubizjak at gmail dot com
@ 2021-04-22 14:32 ` cvs-commit at gcc dot gnu.org
  2021-10-28 13:53 ` rguenth at gcc dot gnu.org
  2021-10-28 16:48 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-22 14:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Uros Bizjak <uros@gcc.gnu.org>:

https://gcc.gnu.org/g:0cda606d08d6196b76524c7b6ad51d87fed0d54b

commit r12-61-g0cda606d08d6196b76524c7b6ad51d87fed0d54b
Author: Uros Bizjak <ubizjak@gmail.com>
Date:   Thu Apr 22 16:30:38 2021 +0200

    i386: Fix unsigned int -> double conversion on i386 w/ -mfpmath=sse
[PR100119]

    2021-04-22  Uroš Bizjak  <ubizjak@gmail.com>

    gcc/
            PR target/100119
            * config/i386/i386-expand.c (ix86_expand_convert_uns_sidf_sse):
            Remove the sign with FE_DOWNWARD, where x - x = -0.0.

    gcc/testsuite/

            PR target/100119
            * gcc.target/i386/pr100119.c: New test.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug target/100119] [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
  2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
                   ` (2 preceding siblings ...)
  2021-04-22 14:32 ` cvs-commit at gcc dot gnu.org
@ 2021-10-28 13:53 ` rguenth at gcc dot gnu.org
  2021-10-28 16:48 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-10-28 13:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |12.0

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
So fixed for GCC 12?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug target/100119] [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse)
  2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
                   ` (3 preceding siblings ...)
  2021-10-28 13:53 ` rguenth at gcc dot gnu.org
@ 2021-10-28 16:48 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-10-28 16:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100119

Uroš Bizjak <ubizjak at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |12.0
             Status|NEW                         |RESOLVED

--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Richard Biener from comment #4)
> So fixed for GCC 12?

Yes.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-10-28 16:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-16 15:32 [Bug target/100119] New: [x86] Conversion unsigned int -> double produces -0 (-m32 -msse2 -mfpmath=sse) chfast at gmail dot com
2021-04-19  7:04 ` [Bug target/100119] " rguenth at gcc dot gnu.org
2021-04-19  9:14 ` ubizjak at gmail dot com
2021-04-22 14:32 ` cvs-commit at gcc dot gnu.org
2021-10-28 13:53 ` rguenth at gcc dot gnu.org
2021-10-28 16:48 ` ubizjak at gmail dot com

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