target/i386: Emulate ftz and denormal flag bits correctly
v2 cover target/i386: Emulate ftz and denormal flag bits correctly
This is a set of four patches to target/i386 which use the core fpu functionality we just landed for Arm FEAT_AFP to correct the emulation of some corner cases of x86 floating point:
* when we are flushing denormal outputs to zero, this should be done after rounding, not before
* the MXCSR.DE and FPUS.DE bits indicate when a denormal input was not flushed and was used in an fp operation; we previously weren't implementing these semantics
All four patches have been reviewed. Testing is a bit light (make check-tcg and make check-functional), because AFAIK there is no freely available comprehensive FP testsuite for x86, and risu doesn't support x86 currently so I can't do the same kind of random-instruction-testing I could for Arm.
Changes v1->v2: just a rebase to apply on current head of git.
thanks
-- PMM
Peter Maydell (4):
target/i386: Detect flush-to-zero after rounding target/i386: Use correct type for get_float_exception_flags() values target/i386: Wire up MXCSR.DE and FPUS.DE correctly tests/tcg/x86_64/fma: add test for exact-denormal output
target/i386/ops_sse.h | 16 +++--- target/i386/tcg/fpu_helper.c | 101 +++++++++++++++++------------------ tests/tcg/x86_64/fma.c | 17 ++++-- 3 files changed, 68 insertions(+), 66 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/4 target/i386: Detect flush-to-zero after rounding
The Intel SDM section 10.2.3.3 on the MXCSR.FTZ bit says that we flush outputs to zero when we detect underflow, which is after rounding. Set the detect_ftz flag accordingly.
This allows us to enable the test in fma.c which checks this behaviour.
--- target/i386/tcg/fpu_helper.c | 8 ++++---- tests/tcg/x86_64/fma.c | 5 ----- 2 files changed, 4 insertions(+), 9 deletions(-)
target/i386/tcg/fpu_helper.c +4 -4
@@ -189,13 +189,13 @@ void cpu_init_fp_statuses(CPUX86State *env)set_float_default_nan_pattern (0b11000000 , &env->mmx_status);set_float_default_nan_pattern (0b11000000 , &env->sse_status);/* -* TODO: x86 does flush-to-zero detection after rounding (the SDM +* x86 does flush-to-zero detection after rounding (the SDM * section 10.2.3.3 on the FTZ bit of MXCSR says that we flush * when we detect underflow, which x86 does after rounding). */ -set_float_ftz_detection (float_ftz_before_rounding, &env->fp_status); -set_float_ftz_detection (float_ftz_before_rounding, &env->mmx_status); -set_float_ftz_detection (float_ftz_before_rounding, &env->sse_status); +set_float_ftz_detection (float_ftz_after_rounding, &env->fp_status); +set_float_ftz_detection (float_ftz_after_rounding, &env->mmx_status); +set_float_ftz_detection (float_ftz_after_rounding, &env->sse_status); }static inline uint8_t save_exception_flags (CPUX86State *env)
tests/tcg/x86_64/fma.c +0 -5
@@ -79,14 +79,9 @@ static testdata tests[] = {/* * Flushing of denormal outputs to zero should also happen after * rounding, so setting FTZ should not affect the result or the flags. -* QEMU currently does not emulate this correctly because we do the -* flush-to-zero check before rounding, so we incorrectly produce a -* zero result and set Underflow as well as Precision. */ -#ifdef ENABLE_FAILING_TESTS {0x3fdfffffffffffff ,0x001fffffffffffff ,0x801fffffffffffff , true, 0x8010000000000000, 0x20 }, /* Enabling FTZ shouldn't change flags */ -#endif };int main (void )
Signature · 1 line
2.43.0
PATCH v2 2/4 target/i386: Use correct type for get_float_exception_flags() values
In reply to Peter MaydellThe softfloat get_float_exception_flags() function returns 'int', but in various places in target/i386 we incorrectly store the returned value into a uint8_t. This currently has no ill effects because i386 doesn't care about any of the float_flag enum values above 0x40. However, we want to start using float_flag_input_denormal_used, which is 0x4000.
Switch to using 'int' so that we can handle all the possible valid float_flag_* values. This includes changing the return type of save_exception_flags() and the argument to merge_exception_flags().
--- target/i386/ops_sse.h | 16 +++---- target/i386/tcg/fpu_helper.c | 82 ++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 49 deletions(-)
target/i386/ops_sse.h +8 -8
@@ -842,7 +842,7 @@ int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s)void glue (helper_rsqrtps ,SUFFIX )(CPUX86State *env,ZMMReg *d,ZMMReg *s) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);int i;for (i =0 ; i <2 <<SHIFT ; i++) { d->ZMM_S(i) = float32_div(float32_one,
@@ -855,7 +855,7 @@ void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)#if SHIFT ==1 void helper_rsqrtss (CPUX86State *env,ZMMReg *d,ZMMReg *v,ZMMReg *s) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);int i; d->ZMM_S(0) = float32_div(float32_one, float32_sqrt(s->ZMM_S(0), &env->sse_status),
@@ -869,7 +869,7 @@ void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *v, ZMMReg *s)void glue (helper_rcpps ,SUFFIX )(CPUX86State *env,ZMMReg *d,ZMMReg *s) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);int i;for (i =0 ; i <2 <<SHIFT ; i++) { d->ZMM_S (i) =float32_div (float32_one, s->ZMM_S (i), &env->sse_status);
@@ -880,7 +880,7 @@ void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s)#if SHIFT ==1 void helper_rcpss (CPUX86State *env,ZMMReg *d,ZMMReg *v,ZMMReg *s) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);int i; d->ZMM_S (0 ) =float32_div (float32_one, s->ZMM_S (0 ), &env->sse_status);for (i =1 ; i <2 <<SHIFT ; i++) {
@@ -1714,7 +1714,7 @@ void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)void glue (helper_roundps ,SUFFIX )(CPUX86State *env,Reg *d,Reg *s,uint32_t mode) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);signed char prev_rounding_mode;int i;
@@ -1738,7 +1738,7 @@ void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,void glue (helper_roundpd ,SUFFIX )(CPUX86State *env,Reg *d,Reg *s,uint32_t mode) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);signed char prev_rounding_mode;int i;
@@ -1763,7 +1763,7 @@ void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s,void glue (helper_roundss ,SUFFIX )(CPUX86State *env,Reg *d,Reg *v,Reg *s,uint32_t mode) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);signed char prev_rounding_mode;int i;
@@ -1788,7 +1788,7 @@ void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *v, Reg *s,void glue (helper_roundsd ,SUFFIX )(CPUX86State *env,Reg *d,Reg *v,Reg *s,uint32_t mode) { -uint8_t old_flags =get_float_exception_flags (&env->sse_status); +int old_flags =get_float_exception_flags (&env->sse_status);signed char prev_rounding_mode;int i;
target/i386/tcg/fpu_helper.c +41 -41
316 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] target/i386: Use correct type for get_float_exception_flags() values
Quoted 17 lines · Peter Maydell
Date: Mon, 19 May 2025 15:51:12 +0100
From: Peter Maydell <[email protected]>
Subject: [PATCH v2 2/4] target/i386: Use correct type for
get_float_exception_flags()values
X-Mailer: git-send-email 2.43.0The softfloat
get_float_exception_flags()function returns 'int', but in various places in target/i386 we incorrectly store the returned value into auint8_t. This currently has no ill effects because i386 doesn't care about any of thefloat_flagenum values above0x40. However, we want to start usingfloat_flag_input_denormal_used, which is0x4000.Switch to using 'int' so that we can handle all the possible valid float_flag_* values. This includes changing the return type of
save_exception_flags()and the argument tomerge_exception_flags().
Quoted 7 lines
Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>--- target/i386/ops_sse.h | 16 +++---- target/i386/tcg/fpu_helper.c | 82 ++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 49 deletions(-)
PATCH v2 3/4 target/i386: Wire up MXCSR.DE and FPUS.DE correctly
In reply to Peter MaydellThe x86 DE bit in the FPU and MXCSR status is supposed to be set when an input denormal is consumed. We didn't previously report this from softfloat, so the x86 code either simply didn't set the DE bit or else incorrectly wired it up to denormal_flushed, depending on which register you looked at.
Now we have input_denormal_used we can wire up these DE bits with the semantics they are supposed to have.
--- target/i386/tcg/fpu_helper.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
target/i386/tcg/fpu_helper.c +3 -8
@@ -215,7 +215,7 @@ static void merge_exception_flags(CPUX86State *env, int old_flags) (new_flags & float_flag_overflow ?FPUS_OE :0 ) | (new_flags & float_flag_underflow ?FPUS_UE :0 ) | (new_flags & float_flag_inexact ?FPUS_PE :0 ) | - (new_flags & float_flag_input_denormal_flushed ? FPUS_DE : 0))); + (new_flags & float_flag_input_denormal_used ? FPUS_DE : 0))); }static inline floatx80 helper_fdiv (CPUX86State *env,floatx80 a,floatx80 b)
@@ -3254,6 +3254,7 @@ void update_mxcsr_status(CPUX86State *env)/* Set exception flags. */ set_float_exception_flags((mxcsr & FPUS_IE ? float_flag_invalid : 0) | + (mxcsr & FPUS_DE ? float_flag_input_denormal_used : 0) | (mxcsr & FPUS_ZE ? float_flag_divbyzero : 0) | (mxcsr & FPUS_OE ? float_flag_overflow : 0) | (mxcsr & FPUS_UE ? float_flag_underflow : 0) |
@@ -3270,14 +3271,8 @@ void update_mxcsr_status(CPUX86State *env)void update_mxcsr_from_sse_status (CPUX86State *env) {int flags =get_float_exception_flags (&env->sse_status); -/* -* The MXCSR denormal flag has opposite semantics to -* float_flag_input_denormal_flushed (the softfloat code sets that flag -* only when flushing input denormals to zero, but SSE sets it -* only when not flushing them to zero), so is not converted -* here. -*/ env->mxcsr |= ((flags & float_flag_invalid ? FPUS_IE : 0) | + (flags & float_flag_input_denormal_used ? FPUS_DE : 0) | (flags & float_flag_divbyzero ? FPUS_ZE : 0) | (flags & float_flag_overflow ? FPUS_OE : 0) | (flags & float_flag_underflow ? FPUS_UE : 0) |
Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] target/i386: Wire up MXCSR.DE and FPUS.DE correctly
Quoted 11 lines · Peter Maydell
Date: Mon, 19 May 2025 15:51:13 +0100
From: Peter Maydell <[email protected]>
Subject: [PATCH v2 3/4] target/i386: Wire up MXCSR.DE and FPUS.DE correctly
X-Mailer: git-send-email 2.43.0The x86 DE bit in the FPU and MXCSR status is supposed to be set when an input denormal is consumed. We didn't previously report this from softfloat, so the x86 code either simply didn't set the DE bit or else incorrectly wired it up to
denormal_flushed, depending on which register you looked at.
Quoted 8 lines
Now we have
input_denormal_usedwe can wire up these DE bits with the semantics they are supposed to have.Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- target/i386/tcg/fpu_helper.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
PATCH v2 4/4 tests/tcg/x86_64/fma: add test for exact-denormal output
In reply to Peter MaydellAdd some fma test cases that check for correct handling of FTZ and for the flag that indicates that the input denormal was consumed.
--- tests/tcg/x86_64/fma.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tests/tcg/x86_64/fma.c +12 -0
@@ -82,6 +82,18 @@ static testdata tests[] = {*/ {0x3fdfffffffffffff ,0x001fffffffffffff ,0x801fffffffffffff , true, 0x8010000000000000, 0x20 }, /* Enabling FTZ shouldn't change flags */ +/* +* normal * 0 + a denormal. With FTZ disabled this gives an exact +* result (equal to the input denormal) that has consumed the denormal. +*/ + { 0x3cc8000000000000, 0x0000000000000000, 0x8008000000000000, false, + 0x8008000000000000, 0x2 }, /* Denormal */ +/* +* With FTZ enabled, this consumes the denormal, returns zero (because +* flushed) and indicates also Underflow and Precision. +*/ + { 0x3cc8000000000000, 0x0000000000000000, 0x8008000000000000, true, + 0x8000000000000000, 0x32 }, /* Precision, Underflow, Denormal */ };int main (void )
Signature · 1 line
2.43.0
Paolo Bonzini
AppliedQueued, thanks.
Paolo