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

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