| Issue |
235266
|
| Summary |
[clang][ExpandIRInsts] `[su]itofp iN -> x86_fp80` (N > 128) converts via fp128 and then truncates, so it rounds twice: `(long double)(unsigned _BitInt(129))(2^127 + 2^63 + 1)` gives 2^127 instead of 2^127 + 2^64
|
| Labels |
backend:X86,
llvm:codegen,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [ExpandIRInsts] [su]itofp iN -> x86_fp80 (N > 128) is computed as an fp128 conversion followed by
* fptrunc fp128 -> x86_fp80: two roundings (113-bit, then 64-bit significand).
* x = 2^127 + 2^63 + 1 lies just above the midpoint 2^127 + 2^63 between the x86_fp80 neighbours 2^127 and
* 2^127 + 2^64, so the correctly rounded result is 2^127 + 2^64. The fp128 step rounds x to 2^127 + 2^63 (the +1
* is below fp128's ulp 2^15 there), and the fptrunc then ties-to-even down to 2^127.
*
* Godbolt: compiler "x86-64 clang (trunk)", language C, flags: -O2 (also -O0)
* "Execute the code" works (plain x86-64; long double = x86_fp80).
* GCC 11 has no _BitInt(129); the expected value is the exact round-to-nearest-even result.
* EXPECTED (correct compiler):
* conv = 0x8.000000000000001p+124 (...), expected 0x8.000000000000001p+124
* OK (exit 0)
* BUGGY (vanilla clang trunk c4a7814f8bc3, -O0 and -O2):
* conv = 0x8p+124 (mantissa 8000000000000000), expected 0x8.000000000000001p+124
* MISMATCH (exit 1)
*/
#include <stdio.h>
#include <string.h>
typedef unsigned _BitInt(129) u129;
__attribute__((noinline)) long double conv(u129 x) { return (long double)x; }
volatile unsigned long long lo = (1ull << 63) | 1; /* x = 2^127 + 2^63 + 1 */
int main(void) {
u129 x = ((u129)1 << 127) | (u129)lo;
long double r = conv(x);
long double e = 0x1p127L + 0x1p64L; /* correctly rounded result */
unsigned long long m; memcpy(&m, &r, 8);
printf("conv = %La (mantissa %016llx), expected %La\n", r, m, e);
if (r != e) { puts("MISMATCH"); return 1; }
puts("OK"); return 0;
}
```
Resulted in this output:
```
$ clang -O2 repro.c && ./a.out # same at -O0
conv = 0x8p+124 (mantissa 8000000000000000), expected 0x8.000000000000001p+124
MISMATCH
(exit 1)
```
| input x | expected (long double)x | actual |
|---------|-------------------------|--------|
| 2^127 + 2^63 + 1 | 2^127 + 2^64 (0x8.000000000000001p+124) | 2^127 (0x8p+124) |
Trunk: https://godbolt.org/z/Gqnqqdb87 (x86-64 clang trunk, -O2, compiles and executes; prints MISMATCH as of 2026-10-23)
Root cause: For integer-to-FP conversions wider than 128 bits, `expandIToFP` in llvm/lib/CodeGen/ExpandIRInsts.cpp builds the result with integer operations. For `x86_fp80` it switches the destination to fp128 (`FPMantissaWidth == 63 ? 112`), builds a correctly rounded fp128 value, and then emits `fptrunc fp128 -> x86_fp80`. That rounds twice, first to a 113-bit and then to a 64-bit significand. Since 113 < 2*64 + 2, the result is not always correctly rounded. For x = 2^127 + 2^63 + 1, the fp128 step drops the +1 and lands exactly on the x86_fp80 midpoint 2^127 + 2^63, and the fptrunc then ties to even, down to 2^127. Both `uitofp` and `sitofp` are affected, at every optimization level. This is a separate path from the known bfloat-via-float double rounding in the same function (#234750).
Suggested fix: Avoid the second rounding. Before the fp128 step, reduce the magnitude to its top 66 bits with a sticky bit (round to odd; then the final fptrunc rounds correctly), or round directly at 64-bit precision and assemble the x86_fp80 bit pattern with its explicit integer bit. Not validated.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs