Issue 239346
Summary [SPARC] unsigned integer to floating-point conversion of 0 gives -0.0 in round-downward mode
Labels new issue
Assignees
Reporter shalseth
    On sparcv9, converting an unsigned integer 0 to double, or an unsigned 32-bit 0 to float, gives -0.0 when the rounding mode is FE_DOWNWARD. It should be +0.0.

```c
#include <fenv.h>
#include <math.h>
#include <stdio.h>

__attribute__((noinline)) float u2f (unsigned int x) { return x; }
__attribute__((noinline)) double u2d (unsigned int x) { return x; }
__attribute__((noinline)) float ul2f (unsigned long x) { return x; }
__attribute__((noinline)) double ul2d (unsigned long x) { return x; }

int main (void)
{
  volatile unsigned int z = 0;
  volatile unsigned long zl = 0;
  fesetround (FE_DOWNWARD);
  float a = u2f (z); double b = u2d (z); float c = ul2f (zl); double d = ul2d (zl);
  fesetround (FE_TONEAREST);
  printf ("u32->float %s0, u32->double %s0, u64->float %s0, u64->double %s0\n",
          signbit (a) ? "-" : "+", signbit (b) ? "-" : "+",
          signbit (c) ? "-" : "+", signbit (d) ? "-" : "+");
  return 0;
}
```

```
$ clang -O2 t.c -lm && ./a.out
u32->float -0, u32->double -0, u64->float +0, u64->double -0
$ gcc -O2 t.c -lm && ./a.out
u32->float +0, u32->double +0, u64->float +0, u64->double +0
```

Same at -O0 and with -frounding-math. `uitofp i32` is expanded with the 0x43300000 high word and an `fsubd` of 2^52, and `uitofp i64` to double with the 2^84/2^52 pair, so for 0 the result is 2^52 - 2^52, which is -0.0 when rounding down:

```llvm
define double @f(i32 %x) {
 %r = uitofp i32 %x to double
  ret double %r
}
```

```
$ llc -mtriple=sparcv9 -O2 t.ll -o -
        ...
        sethi 1100800, %o0
 st %o0, [%sp+2183]
        ldd [%sp+2183], %f0
        ...
        ldd [%o0+%l44(.LCPI0_0)], %f2
        fsubd %f0, %f2, %f0
```

GCC zero-extends a 32-bit value and uses `fxtos`/`fxtod`, and for a 64-bit value uses `fxtod` directly when the sign bit is clear, so it never subtracts.

Without -frounding-math this is mostly hidden, since a uitofp of a value known to be non-negative becomes `fitos`/`fitod`. With strict FP (https://github.com/brad0/llvm-project/commit/64cc436bc8f4066ab7ea98cdae1fa5dc2423d079) the constrained uitofp keeps the expansion, and glibc's float getpayload and log10 tests fail in round-downward because of it (https://github.com/sparclinux/issues/issues/96).

LLVM main bbdbaa776dc8, SPARC M7, Linux sparc64.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to