Skip to contentSkip to conversation

target/i386: Clear C1 for FCOM/FUCOM/FICOM

PATCH New Simon Scherer 1 participant 1 message Sep 2

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 target/i386: Clear C1 for FCOM/FUCOM/FICOM

The SDM specifies that FCOM/FCOMP/FCOMPP/FUCOM/FUCOMP/FUCOMPP/FICOM/FICOMP unconditionally clear C1 in the FPU status word, regardless of the comparison result.

helper_fcom_ST0_FT0/helper_fucom_ST0_FT0 only cleared C3, C2, C0 (mask 0x4500) before OR-ing in the comparison result, leaving C1 (bit 9) at whatever value it already had. FICOM/FICOMP dispatch through the same helpers after converting their integer operand, so they inherited the same bug.

This patch folds the C1 clear into the existing fpus mask for both helpers, since fcom_ccval never sets bit 9 itself.

The SDM also lists "C1 Set to 0" for FCOMI/FCOMIP/FUCOMI/FUCOMIP. Testing against real x86-64 hardware shows this instruction group does not actually clear C1, unlike FCOM/FUCOM/FICOM. QEMU's existing behavior for those four is already correct, so they are intentionally left unchanged.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4378

---
v3: drop the C1 clear in helper_fcomi_ST0_FT0/helper_fucomi_ST0_FT0.
Hardware testing shows FCOMI/FCOMIP/FUCOMI/FUCOMIP do not actually clear C1, unlike FCOM/FUCOM/FICOM.
v2: merge the C1 clear into the existing fpus mask for
fcom_ST0_FT0/fucom_ST0_FT0 (Richard Henderson)

 target/i386/tcg/fpu_helper.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/i386/tcg/fpu_helper.c +4 -2
index b812125efa..34670a1c2b 100644
@@ -531,7 +531,8 @@ void helper_fcom_ST0_FT0(CPUX86State *env)
     FloatRelation ret;
 
     ret = floatx80_compare(ST0, FT0, &env->fp_status);
-    env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
+    /* C1 is unconditionally cleared to 0 */
+    env->fpus = (env->fpus & ~0x4700) | fcom_ccval[ret + 1];
     merge_exception_flags(env, old_flags);
 }
 
@@ -541,7 +542,8 @@ void helper_fucom_ST0_FT0(CPUX86State *env)
     FloatRelation ret;
 
     ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
-    env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
+    /* C1 is unconditionally cleared to 0 */
+    env->fpus = (env->fpus & ~0x4700) | fcom_ccval[ret + 1];
     merge_exception_flags(env, old_flags);
 }
 
Signature · 1 line
2.53.0