target-i386: Fix regressions introduced by the switch to softfloat
cover target-i386: Fix regressions introduced by the switch to softfloat
Since commit 347ac8e35661eff1c2b5ec74d11ee152f2a61856 which switched target-i386 to softfloat, a few SSE instructions are not working correctly anymore. It's especially noticeable on linux/x86-64 as SSE is used default for floating point computation. For example GDM from Debian Lenny is not usable anymore, it displays all the graphical elements at the wrong place.
This patch series is an attempt to fix that, and it's probably a good idea to apply it to the stable branch.
Aurelien Jarno (4):
target-i386: fix {min,max}{pd,ps,sd,ss} SSE2 instructions
target-i386: fix round{pd,ps,sd,ss} SSE2 instructions
target-i386: fix dpps and dppd SSE2 instructions
target-i386: fix SSE rounding and flush to zero
target-i386/TODO | 1 - target-i386/helper.h | 1 + target-i386/op_helper.c | 64 +++++++++++++++++++++++++++++++++++++++------- target-i386/ops_sse.h | 48 +++++++++++++++++----------------- target-i386/translate.c | 2 +- 5 files changed, 80 insertions(+), 36 deletions(-)
Signature · 1 line
1.7.7.3
PATCH 1/4 target-i386: fix {min, max}{pd, ps, sd, ss} SSE2 instructions
minpd, minps, minsd, minss and maxpd, maxps, maxsd, maxss SSE2 instructions have been broken when switching target-i386 to softfloat. It's not possible to use comparison instructions on float types anymore to softfloat, so use the floatXX_min anf floatXX_max functions instead.
As a bonus it implements the correct NaNs behaviour, so let's remove this from the TODO.
It fixes GDM screen display on Debian Lenny.
--- target-i386/TODO | 1 - target-i386/ops_sse.h | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-)
target-i386/TODO +0 -1
@@ -15,7 +15,6 @@ Correctness issues: - DRx register support - CR0.AC emulation - SSE alignment checks -- fix SSE min/max with nans Optimizations/Features:
target-i386/ops_sse.h +2 -2
@@ -584,8 +584,8 @@ void helper_ ## name ## sd (Reg *d, Reg *s)\#define FPU_SUB (size, a, b) float ## size ## _sub(a, b, &env->sse_status)#define FPU_MUL (size, a, b) float ## size ## _mul(a, b, &env->sse_status)#define FPU_DIV (size, a, b) float ## size ## _div(a, b, &env->sse_status) -#define FPU_MIN (size, a, b) (a) < (b) ? (a) : (b) -#define FPU_MAX (size, a, b) (a) > (b) ? (a) : (b) +#define FPU_MIN (size, a, b) float ## size ## _min(a, b, &env->sse_status) +#define FPU_MAX (size, a, b) float ## size ## _max(a, b, &env->sse_status)#define FPU_SQRT (size, a, b) float ## size ## _sqrt(b, &env->sse_status)SSE_HELPER_S (add,FPU_ADD )
Signature · 1 line
1.7.7.3
Re: [Qemu-devel] [PATCH 1/4] target-i386: fix {min, max}{pd, ps, sd, ss} SSE2 instructions
Quoted 4 lines · Aurelien Jarno
minpd, minps, minsd, minss and maxpd, maxps, maxsd, maxss SSE2 instructions have been broken when switching target-i386 to softfloat. It's not possible to use comparison instructions on float types anymore to softfloat, so use the
floatXX_minanffloatXX_maxfunctions instead.
Nope, this gets the x86 special cases wrong. This has been discussed here before:
http://www.mail-archive.com/[email protected]/msg85557.html
has the right implementation (from Jason Wessell) and a comment (from me) about why it's right.
-- PMM
PATCH v2 1/4 target-i386: fix {min, max}{pd, ps, sd, ss} SSE2 instructions
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Aurelien Jarno
minpd, minps, minsd, minss and maxpd, maxps, maxsd, maxss SSE2 instructions have been broken when switching target-i386 to softfloat. It's not possible to use comparison instructions on float types anymore to softfloat, so use the
floatXX_minanffloatXX_maxfunctions instead.
Quoted 7 lines
Nope, this gets the x86 special cases wrong. This has been discussed here before:
http://www.mail-archive.com/[email protected]/msg85557.html
has the right implementation (from Jason Wessell) and a comment (from me) about why it's right.
Good catch, the patch below should implement the correct behaviour.
target-i386: fix {min,max}{pd,ps,sd,ss} SSE2 instructions
minpd, minps, minsd, minss and maxpd, maxps, maxsd, maxss SSE2 instructions have been broken when switching target-i386 to softfloat. It's not possible to use comparison instructions on float types anymore to softfloat, so use the floatXX_lt function instead, as the float_XX_min and float_XX_max functions can't be used due to the Intel specific behaviour.
As it implements the correct NaNs behaviour, let's remove the corresponding entry from the TODO.
It fixes GDM screen display on Debian Lenny.
Thanks to Peter Maydell and Jason Wessel for their analysis of the problem.
--- target-i386/TODO | 1 - target-i386/ops_sse.h | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-)
target-i386/TODO +0 -1
@@ -15,7 +15,6 @@ Correctness issues: - DRx register support - CR0.AC emulation - SSE alignment checks -- fix SSE min/max with nans Optimizations/Features:
target-i386/ops_sse.h +7 -2
@@ -584,10 +584,15 @@ void helper_ ## name ## sd (Reg *d, Reg *s)\#define FPU_SUB (size, a, b) float ## size ## _sub(a, b, &env->sse_status)#define FPU_MUL (size, a, b) float ## size ## _mul(a, b, &env->sse_status)#define FPU_DIV (size, a, b) float ## size ## _div(a, b, &env->sse_status) -#define FPU_MIN (size, a, b) (a) < (b) ? (a) : (b) -#define FPU_MAX (size, a, b) (a) > (b) ? (a) : (b)#define FPU_SQRT (size, a, b) float ## size ## _sqrt(b, &env->sse_status) +/* Note that the choice of comparison op here is important to get the +* special cases right: for min and max Intel specifies that (-0,0), +* (NaN, anything) and (anything, NaN) return the second argument. +*/ +#define FPU_MIN (size, a, b) float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b) +#define FPU_MAX (size, a, b) float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b) + SSE_HELPER_S(add, FPU_ADD) SSE_HELPER_S(sub, FPU_SUB)SSE_HELPER_S (mul,FPU_MUL )
Signature · 6 lines
1.7.7.3 -- Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
PATCH 2/4 target-i386: fix round{pd, ps, sd, ss} SSE2 instructions
In reply to Aurelien Jarnoroundps and roundss SSE2 instructions have been broken when switching target-i386 to softfloat. They use float64_round_to_int to convert a float32, and while the implicit conversion from float32 to float64 was correct for softfloat-native, it is not for pure softfloat. Fix that by using the correct registers and correct functions.
Also fix roundpd and roundsd implementation at the same time, even if these functions are behaving correctly.
--- target-i386/ops_sse.h | 16 ++++++++-------- 1 files changed, 8 insertions(+), 8 deletions(-)
target-i386/ops_sse.h +8 -8
@@ -1648,10 +1648,10 @@ void glue(helper_roundps, SUFFIX) (Reg *d, Reg *s, uint32_t mode)break ; } - d->L (0 ) =float64_round_to_int (s->L (0 ), &env->sse_status); - d->L (1 ) =float64_round_to_int (s->L (1 ), &env->sse_status); - d->L (2 ) =float64_round_to_int (s->L (2 ), &env->sse_status); - d->L (3 ) =float64_round_to_int (s->L (3 ), &env->sse_status); + d->XMM_S (0 ) =float32_round_to_int (s->XMM_S (0 ), &env->sse_status); + d->XMM_S (1 ) =float32_round_to_int (s->XMM_S (1 ), &env->sse_status); + d->XMM_S (2 ) =float32_round_to_int (s->XMM_S (2 ), &env->sse_status); + d->XMM_S (3 ) =float32_round_to_int (s->XMM_S (3 ), &env->sse_status);#if 0 /* TODO */ if (mode & (1 <<3 ))
@@ -1684,8 +1684,8 @@ void glue(helper_roundpd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)break ; } - d->Q (0 ) =float64_round_to_int (s->Q (0 ), &env->sse_status); - d->Q (1 ) =float64_round_to_int (s->Q (1 ), &env->sse_status); + d->XMM_D (0 ) =float64_round_to_int (s->XMM_D (0 ), &env->sse_status); + d->XMM_D (1 ) =float64_round_to_int (s->XMM_D (1 ), &env->sse_status);#if 0 /* TODO */ if (mode & (1 <<3 ))
@@ -1718,7 +1718,7 @@ void glue(helper_roundss, SUFFIX) (Reg *d, Reg *s, uint32_t mode)break ; } - d->L (0 ) =float64_round_to_int (s->L (0 ), &env->sse_status); + d->XMM_S (0 ) =float32_round_to_int (s->XMM_S (0 ), &env->sse_status);#if 0 /* TODO */ if (mode & (1 <<3 ))
@@ -1751,7 +1751,7 @@ void glue(helper_roundsd, SUFFIX) (Reg *d, Reg *s, uint32_t mode)break ; } - d->Q (0 ) =float64_round_to_int (s->Q (0 ), &env->sse_status); + d->XMM_D (0 ) =float64_round_to_int (s->XMM_D (0 ), &env->sse_status);#if 0 /* TODO */ if (mode & (1 <<3 ))
Signature · 1 line
1.7.7.3
PATCH 3/4 target-i386: fix dpps and dppd SSE2 instructions
In reply to Aurelien JarnoThe helpers implemented dpps and dppd SSE instructions are not passing the correct argument types to the softfloat functions. While they do work anyway providing a correct behaviour, this patch fixes that.
--- target-i386/ops_sse.h | 28 ++++++++++++++-------------- 1 files changed, 14 insertions(+), 14 deletions(-)
target-i386/ops_sse.h +14 -14
@@ -1770,44 +1770,44 @@ SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP)void glue (helper_dpps ,SUFFIX ) (Reg *d,Reg *s,uint32_t mask) { -float32 iresult =0 /*float32_zero*/ ; +float32 iresult = float32_zero;if (mask & (1 <<4 )) iresult =float32_add (iresult, -float32_mul (d->L (0 ), s->L (0 ), &env->sse_status), +float32_mul (d->XMM_S (0 ), s->XMM_S (0 ), &env->sse_status), &env->sse_status);if (mask & (1 <<5 )) iresult =float32_add (iresult, -float32_mul (d->L (1 ), s->L (1 ), &env->sse_status), +float32_mul (d->XMM_S (1 ), s->XMM_S (1 ), &env->sse_status), &env->sse_status);if (mask & (1 <<6 )) iresult =float32_add (iresult, -float32_mul (d->L (2 ), s->L (2 ), &env->sse_status), +float32_mul (d->XMM_S (2 ), s->XMM_S (2 ), &env->sse_status), &env->sse_status);if (mask & (1 <<7 )) iresult =float32_add (iresult, -float32_mul (d->L (3 ), s->L (3 ), &env->sse_status), +float32_mul (d->XMM_S (3 ), s->XMM_S (3 ), &env->sse_status), &env->sse_status); - d->L (0 ) = (mask & (1 <<0 )) ? iresult :0 /*float32_zero*/ ; - d->L (1 ) = (mask & (1 <<1 )) ? iresult :0 /*float32_zero*/ ; - d->L (2 ) = (mask & (1 <<2 )) ? iresult :0 /*float32_zero*/ ; - d->L (3 ) = (mask & (1 <<3 )) ? iresult :0 /*float32_zero*/ ; + d->XMM_S (0 ) = (mask & (1 <<0 )) ? iresult : float32_zero; + d->XMM_S (1 ) = (mask & (1 <<1 )) ? iresult : float32_zero; + d->XMM_S (2 ) = (mask & (1 <<2 )) ? iresult : float32_zero; + d->XMM_S (3 ) = (mask & (1 <<3 )) ? iresult : float32_zero; }void glue (helper_dppd ,SUFFIX ) (Reg *d,Reg *s,uint32_t mask) { -float64 iresult =0 /*float64_zero*/ ; +float64 iresult = float64_zero;if (mask & (1 <<4 )) iresult =float64_add (iresult, -float64_mul (d->Q (0 ), s->Q (0 ), &env->sse_status), +float64_mul (d->XMM_D (0 ), s->XMM_D (0 ), &env->sse_status), &env->sse_status);if (mask & (1 <<5 )) iresult =float64_add (iresult, -float64_mul (d->Q (1 ), s->Q (1 ), &env->sse_status), +float64_mul (d->XMM_D (1 ), s->XMM_D (1 ), &env->sse_status), &env->sse_status); - d->Q (0 ) = (mask & (1 <<0 )) ? iresult :0 /*float64_zero*/ ; - d->Q (1 ) = (mask & (1 <<1 )) ? iresult :0 /*float64_zero*/ ; + d->XMM_D (0 ) = (mask & (1 <<0 )) ? iresult : float64_zero; + d->XMM_D (1 ) = (mask & (1 <<1 )) ? iresult : float64_zero; }void glue (helper_mpsadbw ,SUFFIX ) (Reg *d,Reg *s,uint32_t offset)
Signature · 1 line
1.7.7.3
PATCH 4/4 target-i386: fix SSE rounding and flush to zero
In reply to Aurelien JarnoSSE rounding and flush to zero control has never been implemented. However given that softfloat-native was using a single state for FPU and SSE and given that glibc is setting both FPU and SSE state in fesetround(), this was working correctly up to the switch to softfloat.
Fix that by adding an update_sse_status() function similar to update_fpu_status(), and callin git on write to mxcsr.
--- target-i386/helper.h | 1 + target-i386/op_helper.c | 64 +++++++++++++++++++++++++++++++++++++++------- target-i386/translate.c | 2 +- 3 files changed, 56 insertions(+), 11 deletions(-)
target-i386/helper.h +1 -0
@@ -197,6 +197,7 @@ DEF_HELPER_2(lzcnt, tl, tl, int)/* MMX/SSE */ +DEF_HELPER_1(ldmxcsr, void, i32) DEF_HELPER_0(enter_mmx, void) DEF_HELPER_0(emms, void)DEF_HELPER_2 (movq, void, ptr, ptr)
target-i386/op_helper.c +54 -10
89 lines not on this page · raw message
target-i386/translate.c +1 -1
@@ -7544,7 +7544,7 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)gen_lea_modrm (s, modrm, ®_addr, &offset_addr);if (op ==2 ) {gen_op_ld_T0_A0 (OT_LONG + s->mem_index); -tcg_gen_st32_tl (cpu_T[0 ], cpu_env, offsetof(CPUX86State , mxcsr)); +gen_helper_ldmxcsr (cpu_T[0 ]); }else {tcg_gen_ld32u_tl (cpu_T[0 ], cpu_env, offsetof(CPUX86State , mxcsr));gen_op_st_T0_A0 (OT_LONG + s->mem_index);
Signature · 1 line
1.7.7.3
Re: [Qemu-devel] [PATCH 4/4] target-i386: fix SSE rounding and flush to zero
After applied this patch, while I was compiling on my lap, there will be an error:
./configure --enable-kvm --target-list=x86_64-softmmu && make
CC x86_64-softmmu/translate.o
/qemu/target-i386/translate.c: In function ‘disas_insn’:
/qemu/target-i386/translate.c:7547:17: error: incompatible type for argument 1 of ‘gen_helper_ldmxcsr’ /qemu/target-i386/helper.h:200:1: note: expected ‘TCGv_i32’ but argument is of type ‘TCGv_i64’
make[1]: *** [translate.o] Error 1
make: *** [subdir-x86_64-softmmu] Error 2
Quoted 140 lines · Aurelien Jarno
SSE rounding and flush to zero control has never been implemented. However given that softfloat-native was using a single state for FPU and SSE and given that glibc is setting both FPU and SSE state in
fesetround(), this was working correctly up to the switch to softfloat.Fix that by adding an
update_sse_status()function similar toupdate_fpu_status(), and callin git on write to mxcsr.Signed-off-by Aurelien Jarno <[email protected]>---
target-i386/helper.h | 1 +
target-i386/op_helper.c | 64 +++++++++++++++++++++++++++++++++++++++-------target-i386/translate.c| 2 +-3 files changed, 56 insertions(+), 11 deletions(-)target-i386/helper.h +1
-0@@ -197,6 +197,7 @@ DEF_HELPER_2(lzcnt, tl, tl, int) /* MMX/SSE */ +DEF_HELPER_1(ldmxcsr, void, i32) DEF_HELPER_0(enter_mmx, void) DEF_HELPER_0(emms, void) DEF_HELPER_2(movq, void, ptr, ptr)target-i386/op_helper.c +6
-6@@ -52,11 +52,11 @@ static inline target_long lshift(target_long x, int n) } } -#define RC_MASK 0xc00 -#define RC_NEAR 0x000 -#define RC_DOWN 0x400 -#define RC_UP 0x800 -#define RC_CHOP 0xc00 +#define FPU_RC_MASK 0xc00 +#define FPU_RC_NEAR 0x000 +#define FPU_RC_DOWN 0x400 +#define FPU_RC_UP 0x800 +#define FPU_RC_CHOP 0xc00 #define MAXTAN 9223372036854775808.0@@ -4024,18 +4024,18 @@ static void update_fp_status(void) int rnd_type; /* set rounding mode */ - switch(env->fpuc & RC_MASK) { + switch(env->fpuc & FPU_RC_MASK) {default:
- caseRC_NEAR:
+ caseFPU_RC_NEAR:
rnd_type=float_round_nearest_even;
break;
- caseRC_DOWN:
+ caseFPU_RC_DOWN:
rnd_type=float_round_down;
break;
- caseRC_UP:
+ caseFPU_RC_UP:
rnd_type=float_round_up;
break;
- caseRC_CHOP:
+ caseFPU_RC_CHOP:
rnd_type=float_round_to_zero;
break;
}target-i386/op_helper.c +44
-0@@ -5629,6 +5629,50 @@ void helper_vmexit(uint32_t exit_code, uint64_t exit_info_1) /* MMX/SSE */ /* XXX: optimize by storing fptt and fptags in the static cpu state */ + +#define SSE_DAZ 0x0040 +#define SSE_RC_MASK 0x6000 +#define SSE_RC_NEAR 0x0000 +#define SSE_RC_DOWN 0x2000 +#define SSE_RC_UP 0x4000 +#define SSE_RC_CHOP 0x6000 +#define SSE_FZ 0x8000 + +static void update_sse_status (void ) +{ + int rnd_type; + + /* set rounding mode */ + switch(env->mxcsr & SSE_RC_MASK) { + default: + case SSE_RC_NEAR: + rnd_type = float_round_nearest_even; + break; + case SSE_RC_DOWN: + rnd_type = float_round_down; + break; + case SSE_RC_UP: + rnd_type = float_round_up; + break; + case SSE_RC_CHOP: + rnd_type = float_round_to_zero; + break; + } + set_float_rounding_mode(rnd_type, &env->sse_status); + + /* set denormals are zero */ + set_flush_inputs_to_zero((env->mxcsr & SSE_DAZ) ? 1 : 0, &env->sse_status); + + /* set flush to zero */ + set_flush_to_zero((env->mxcsr & SSE_FZ) ? 1 : 0, &env->fp_status); +} + +void helper_ldmxcsr (uint32_t val) +{ + env->mxcsr = val; + update_sse_status(); +} + void helper_enter_mmx(void) { env->fpstt = 0;target-i386/translate.c +1
-2@@ -7544,7 +7544,7 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start) gen_lea_modrm(s, modrm, ®_addr, &offset_addr); if (op == 2) { gen_op_ld_T0_A0(OT_LONG + s->mem_index); - tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr)); + gen_helper_ldmxcsr(cpu_T[0]); } else { tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr)); gen_op_st_T0_A0(OT_LONG + s->mem_index); --1.7.7.3
Re: [Qemu-devel] [PATCH 4/4] target-i386: fix SSE rounding and flush to zero
Quoted 12 lines · Dong Xu Wang
After applied this patch, while I was compiling on my lap, there will be an error:
./configure --enable-kvm --target-list=x86_64-softmmu && make
CCx86_64-softmmu/translate.o
/qemu/target-i386/translate.c: In function ‘disas_insn’:
/qemu/target-i386/translate.c:7547:17: error: incompatible type for argument 1 of ‘gen_helper_ldmxcsr’ /qemu/target-i386/helper.h:200:1: note: expected ‘TCGv_i32’ but argument is of type ‘TCGv_i64’
make[1]: *** [translate.o] Error 1
make: *** [subdir-x86_64-softmmu] Error 2
I see this, too.
Re: [Qemu-devel] [Qemu-stable] [PATCH 4/4] target-i386: fix SSE rounding and flush to zero
Quoted 15 lines · Markus Armbruster
Quoted 12 lines · Dong Xu Wang
After applied this patch, while I was compiling on my lap, there will be an error:
./configure --enable-kvm --target-list=x86_64-softmmu && make
CCx86_64-softmmu/translate.o
/qemu/target-i386/translate.c: In function ‘disas_insn’:
/qemu/target-i386/translate.c:7547:17: error: incompatible type for argument 1 of ‘gen_helper_ldmxcsr’ /qemu/target-i386/helper.h:200:1: note: expected ‘TCGv_i32’ but argument is of type ‘TCGv_i64’
make[1]: *** [translate.o] Error 1
make: *** [subdir-x86_64-softmmu] Error 2
Quoted 1 line
I see this, too.
I will take a look, I am not seeing it right now with the full stable tree, though I am doing a much larger config.
Justin
Re: [Qemu-devel] [PATCH 4/4] target-i386: fix SSE rounding and flush to zero
In reply to Dong Xu WangQuoted 12 lines · Dong Xu Wang
After applied this patch, while I was compiling on my lap, there will be an error:
./configure --enable-kvm --target-list=x86_64-softmmu && make
CCx86_64-softmmu/translate.o
/qemu/target-i386/translate.c: In function ‘disas_insn’:
/qemu/target-i386/translate.c:7547:17: error: incompatible type for argument 1 of ‘gen_helper_ldmxcsr’ /qemu/target-i386/helper.h:200:1: note: expected ‘TCGv_i32’ but argument is of type ‘TCGv_i64’
make[1]: *** [translate.o] Error 1
make: *** [subdir-x86_64-softmmu] Error 2
Sorry about that, I have pushed the following patch which solves the problem.
target-i386: fix compilation with --enable-debug-tcg
Commit 2355c16e74ffa4d14e7fc2b4a23b055565ac0221 introduced a new ldmxcsr helper taking an i32 argument, but the helper is actually passed a long. Fix that by truncating the long to i32.
--- target-i386/translate.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
target-i386/translate.c +2 -1
@@ -7544,7 +7544,8 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)gen_lea_modrm (s, modrm, ®_addr, &offset_addr);if (op ==2 ) {gen_op_ld_T0_A0 (OT_LONG + s->mem_index); -gen_helper_ldmxcsr (cpu_T[0 ]); +tcg_gen_trunc_tl_i32 (cpu_tmp2_i32, cpu_T[0 ]); +gen_helper_ldmxcsr (cpu_tmp2_i32); }else {tcg_gen_ld32u_tl (cpu_T[0 ], cpu_env, offsetof(CPUX86State , mxcsr));gen_op_st_T0_A0 (OT_LONG + s->mem_index);
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net