From 32d1c0a5614b9979768b3711726db3d601750ed3 Mon Sep 17 00:00:00 2001 From: Stefan Date: Wed, 4 Nov 2026 13:25:31 +0100 Subject: [PATCH] x86_64: Fix long double to int cast x86_64-gen.c: - gen_cvt_ftoi() Use TOK___fixxfdi for VT_LDOUBLE independently of the size of the target integer type, avoiding a wrong conversion to doube. This solves: int main(void) { long double x = 0.999999999999999999l; return (int)x; } Before for VT_INT a long double was first converted to double with rounding, possibly leading to a wrong result when truncating with cvttsd2si. --- x86_64-gen.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/x86_64-gen.c b/x86_64-gen.c index c62c8a0c..ac5c50a9 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -2121,17 +2121,14 @@ void gen_cvt_ftoi(int t) ft = vtop->type.t; bt = ft & VT_BTYPE; if (bt == VT_LDOUBLE) { - if (t != VT_INT) { - vpush_helper_func(TOK___fixxfdi); - vswap(); - gfunc_call(1); - vpushi(0); - vtop->r = REG_IRET; + vpush_helper_func(TOK___fixxfdi); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r = REG_IRET; + if (t != VT_INT) vtop->r2 = REG_IRE2; - return; - } - gen_cvt_ftof(VT_DOUBLE); - bt = VT_DOUBLE; + return; } gv(RC_FLOAT);