[PATCH] D141074: [X86] Avoid converting 64-bit integers to floating point using x87 on Windows

icedrocket via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 15 01:29:48 PST 2023


icedrocket added a comment.

In D141074#4054359 <https://reviews.llvm.org/D141074#4054359>, @craig.topper wrote:

> In D141074#4054355 <https://reviews.llvm.org/D141074#4054355>, @icedrocket wrote:
>
>> I'm not sure why the result depends on the x87 control word, but anyway, there is a possibility that this issue is because of the rounding mode. The conversion from f64 to f32 is typically done using round to nearest, and the target to round can differ between u64 and u64 to f64. We should round over u64, not u64 to f64.
>
> The algorithm we use for u64 to f32 looks like this
>
> bitcast u64 to i64
> convert to i64 to 80-bit fp using FILD instruction (this conversion does not depend on the value of PC).
> if bits 63 was set in the integer this conversion produces a negative number.
> if bit 63 was set in the integer, add 18446744073709551616 to the negative floating point value to give it the correct positive value. With PC=64 this shouldn't result in any rounding.
> round 80-bit fp value to f32 using FST.
>
> The bug occurs because the addition of 18446744073709551616 ends up rounding unexpectedly because of PC=53.

The value I tried to convert in summary's example code is 18446744073709551616, which is representable in i64 range and bit 63 is not set.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141074/new/

https://reviews.llvm.org/D141074



More information about the llvm-commits mailing list