https://bugs.llvm.org/show_bug.cgi?id=47393

            Bug ID: 47393
           Summary: Conversion from uint64_t to double produces wrong
                    result for rounding downward
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangb...@nondot.org
          Reporter: chf...@gmail.com
                CC: llvm-bugs@lists.llvm.org, neeil...@live.com,
                    richard-l...@metafoo.co.uk

When floating-point rounding direction is set to "towards negative infinity"
(FE_DOWNWARD) and conversion from uint64_t(0) to double is performed the result
is -0.0 instead of +0.0.

Architecture: x86_64
Clang version: reproducible in all known versions, including trunk
Options: happens for all combinations of -O0, -O2, -frounding-math,
-fsigned-zeros.

Code (-lm is needed)

enum { FE_DOWNWARD = 0x400 };

extern int fesetround(int rounding_direction);

__attribute__((noinline))
double ui64_to_f64(unsigned long x)
{
    return (double)x;
}

int main()
{
    fesetround(FE_DOWNWARD);
    double z = ui64_to_f64(0);
    return __builtin_signbit(z) != 0;  // expected 0
}

Can be played with at https://godbolt.org/z/jvPGqW.

I believe the main issue is in LLVM.

Without -frounding-math the Clang uses uitofp instruction. The reference says
this should be fine in this context as "it is rounded using the default
rounding mode".

With -frounding-math the 
call double @llvm.experimental.constrained.uitofp.f64.i64(i64 %3, metadata
!"round.dynamic", metadata !"fpexcept.ignore")
is used, but the result is wrong nevertheless.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to