680x0 mul and div instructions
v4 cover 680x0 mul and div instructions
This series is another subset of the series I sent in May:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00501.html
It must be applied on top of series:
"target-m68k: 680x0 instruction set, part 2"
This subset contains reworked patches of mul and div instructions:
- "add 64bit mull": correctly set QREG_CC_V,
- "inline divu/divs": don't inline divu/divs, but update existing functions to manage 680x0 div instructions
I've checked it doesn't break coldfire support:
http://wiki.qemu.org/download/coldfire-test-0.1.tar.bz2
but it can't boot a 680x0 processor kernel.
v4:
- divull/divsll: build correctly the 64bit num value check overflow as it is done in divsw
v3:
- mull: manage the case where Dl == Dh
- divu/divs: move all the mechanic to helpers,
and pass the register number to directly set
the result
v2:
- Free "rem" and "quot" in "divl".
Laurent Vivier (2):
target-m68k: add 64bit mull
target-m68k: add 680x0 divu/divs variants
linux-user/main.c | 7 ++ target-m68k/cpu.h | 4 -- target-m68k/helper.h | 8 ++- target-m68k/op_helper.c | 182 +++++++++++++++++++++++++++++++++++++++++------- target-m68k/qregs.def | 2 - target-m68k/translate.c | 146 +++++++++++++++++++++++++------------- 6 files changed, 267 insertions(+), 82 deletions(-)
Signature · 1 line
2.7.4
PATCH v4 1/2 target-m68k: add 64bit mull
--- target-m68k/translate.c | 62 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-)
target-m68k/translate.c +50 -12
@@ -1812,24 +1812,62 @@ DISAS_INSN(tas)DISAS_INSN (mull) {uint16_t ext; -TCGv reg;TCGv src1; -TCGv dest; +int sign; -/* The upper 32 bits of the product are discarded, so -muls.l and mulu.l are functionally equivalent. */ ext =read_im16 (env, s); -if (ext &0x87ff ) { -gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); + + sign = ext &0x800 ; + +if (ext &0x400 ) { +if (!m68k_feature (s->env,M68K_FEATURE_QUAD_MULDIV )) { +gen_exception (s, s->pc -4 ,EXCP_UNSUPPORTED ); +return ; + } + +SRC_EA (env, src1,OS_LONG ,0 ,NULL ); + +if (sign) { +tcg_gen_muls2_i32 (QREG_CC_Z ,QREG_CC_N , src1,DREG (ext,12 )); + }else { +tcg_gen_mulu2_i32 (QREG_CC_Z ,QREG_CC_N , src1,DREG (ext,12 )); + } +/* if Dl == Dh, 68040 returns low word */ +tcg_gen_mov_i32 (DREG (ext,0 ),QREG_CC_N ); +tcg_gen_mov_i32 (DREG (ext,12 ),QREG_CC_Z ); +tcg_gen_or_i32 (QREG_CC_Z ,QREG_CC_Z ,QREG_CC_N ); + +tcg_gen_movi_i32 (QREG_CC_V ,0 ); +tcg_gen_movi_i32 (QREG_CC_C ,0 ); + +set_cc_op (s,CC_OP_FLAGS );return ; } - reg =DREG (ext,12 );SRC_EA (env, src1,OS_LONG ,0 ,NULL ); - dest =tcg_temp_new (); -tcg_gen_mul_i32 (dest, src1, reg); -tcg_gen_mov_i32 (reg, dest); -/* Unlike m68k, coldfire always clears the overflow bit. */ -gen_logic_cc (s, dest,OS_LONG ); +if (m68k_feature (s->env,M68K_FEATURE_M68000 )) { +tcg_gen_movi_i32 (QREG_CC_C ,0 ); +if (sign) { +tcg_gen_muls2_i32 (QREG_CC_N ,QREG_CC_V , src1,DREG (ext,12 )); +/* QREG_CC_V is -(QREG_CC_V != (QREG_CC_N >> 31)) */ +tcg_gen_sari_i32 (QREG_CC_Z ,QREG_CC_N ,31 ); +tcg_gen_setcond_i32 (TCG_COND_NE ,QREG_CC_V ,QREG_CC_V ,QREG_CC_Z ); + }else { +tcg_gen_mulu2_i32 (QREG_CC_N ,QREG_CC_V , src1,DREG (ext,12 )); +/* QREG_CC_V is -(QREG_CC_V != 0), use QREG_CC_C as 0 */ +tcg_gen_setcond_i32 (TCG_COND_NE ,QREG_CC_V ,QREG_CC_V ,QREG_CC_C ); + } +tcg_gen_neg_i32 (QREG_CC_V ,QREG_CC_V ); +tcg_gen_mov_i32 (DREG (ext,12 ),QREG_CC_N ); + +tcg_gen_mov_i32 (QREG_CC_Z ,QREG_CC_N ); + +set_cc_op (s,CC_OP_FLAGS ); + }else { +/* The upper 32 bits of the product are discarded, so +muls.l and mulu.l are functionally equivalent. */ +tcg_gen_mul_i32 (DREG (ext,12 ), src1,DREG (ext,12 )); +gen_logic_cc (s,DREG (ext,12 ),OS_LONG ); + } }static void gen_link (DisasContext *s,uint16_t insn,int32_t offset)
Signature · 1 line
2.7.4
PATCH v4 2/2 target-m68k: add 680x0 divu/divs variants
In reply to Laurent VivierUpdate helper to set the throwing location in case of div-by-0. Cleanup divX.w and add quad word variants of divX.l.
--- linux-user/main.c | 7 ++ target-m68k/cpu.h | 4 -- target-m68k/helper.h | 8 ++- target-m68k/op_helper.c | 182 +++++++++++++++++++++++++++++++++++++++++------- target-m68k/qregs.def | 2 - target-m68k/translate.c | 84 ++++++++++++---------- 6 files changed, 217 insertions(+), 70 deletions(-)
linux-user/main.c +7 -0
@@ -2864,6 +2864,13 @@ void cpu_loop(CPUM68KState *env) info._sifields._sigfault._addr = env->pc;queue_signal (env, info.si_signo,QEMU_SI_FAULT , &info);break ; +case EXCP_DIV0 : + info.si_signo =TARGET_SIGFPE ; + info.si_errno =0 ; + info.si_code =TARGET_FPE_INTDIV ; + info._sifields._sigfault._addr = env->pc; +queue_signal (env, info.si_signo,QEMU_SI_FAULT , &info); +break ;case EXCP_TRAP0 : {abi_long ret;
target-m68k/cpu.h +0 -4
@@ -95,10 +95,6 @@ typedef struct CPUM68KState {uint32_t macsr;uint32_t mac_mask; -/* Temporary storage for DIV helpers. */ -uint32_t div1; -uint32_t div2; -/* MMU status. */ struct {uint32_t ar;
target-m68k/helper.h +6 -2
@@ -1,8 +1,12 @@ DEF_HELPER_1(bitrev, i32, i32) DEF_HELPER_1(ff1, i32, i32) DEF_HELPER_FLAGS_2(sats, TCG_CALL_NO_RWG_SE, i32, i32, i32) -DEF_HELPER_2(divu, void, env, i32) -DEF_HELPER_2(divs, void, env, i32) +DEF_HELPER_3(divuw, void, env, int, i32) +DEF_HELPER_3(divsw, void, env, int, s32) +DEF_HELPER_4(divul, void, env, int, int, i32) +DEF_HELPER_4(divsl, void, env, int, int, s32) +DEF_HELPER_4(divull, void, env, int, int, i32) +DEF_HELPER_4(divsll, void, env, int, int, s32) DEF_HELPER_2(set_sr, void, env, i32)DEF_HELPER_3 (movec, void, env, i32, i32)
target-m68k/op_helper.c +157 -25
220 lines not on this page · raw message
target-m68k/qregs.def +0 -2
@@ -7,7 +7,5 @@ DEFO32(CC_C, cc_c) DEFO32(CC_N, cc_n) DEFO32(CC_V, cc_v) DEFO32(CC_Z, cc_z) -DEFO32(DIV1, div1) -DEFO32(DIV2, div2) DEFO32(MACSR, macsr) DEFO32(MAC_MASK, mac_mask)
target-m68k/translate.c +47 -37
111 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v4 2/2] target-m68k: add 680x0 divu/divs variants
Quoted 4 lines · Laurent Vivier
Update helper to set the throwing location in case of div-by-0. Cleanup divX.w and add quad word variants of divX.l.
Signed-off-by Laurent Vivier <[email protected]>
Quoted 8 lines
--- linux-user/main.c | 7 ++ target-m68k/cpu.h | 4 -- target-m68k/helper.h | 8 ++- target-m68k/op_helper.c | 182 +++++++++++++++++++++++++++++++++++++++++------- target-m68k/qregs.def | 2 - target-m68k/translate.c | 84 ++++++++++++---------- 6 files changed, 217 insertions(+), 70 deletions(-)
r~
Re: [Qemu-devel] [PATCH v4 2/2] target-m68k: add 680x0 divu/divs variants
In reply to Laurent VivierHi,
I come back on some patches as I've been able to test some instructions using RISU.
Le 01/11/2016 à 21:03, Laurent Vivier a écrit :
...
Quoted 29 lines
target-m68k/op_helper.c +6
-10@@ -179,51 +184,178 @@ void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)raise_exception (env, tt); } -void HELPER (divu )(CPUM68KState *env,uint32_t word) +void HELPER (divuw )(CPUM68KState *env,int destr,uint32_t den) { -uint32_t num; -uint32_t den; -uint32_t quot; -uint32_t rem; +uint32_t num = env->dregs[destr]; +uint32_t quot, rem; - num = env->div1; - den = env->div2; -/* ??? This needs to make sure the throwing location is accurate. */ if (den ==0 ) { -raise_exception (env,EXCP_DIV0 ); +raise_exception_ra (env,EXCP_DIV0 ,GETPC ()); } quot = num / den; rem = num % den; - env->cc_v = (word && quot >0xffff ?-1 :0 ); + env->cc_c =0 ;/* always cleared, even if overflow */ +if (quot >0xffff ) {
target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 209
Quoted 10 lines
target-m68k/op_helper.c +8
-0+ env->cc_v =-1 ; +/* nothing else is modified */ +/* real 68040 keeps Z and N on overflow, +* whereas documentation says "undefined" +*/ +return ; + } + env->dregs[destr] =deposit32 (quot,16 ,16 , rem); env->cc_z = quot; env->cc_n = quot;
quot is here a 32bit, but the result is only the 16 lower bits, so I think we should have
env->cc_z = (int16_t)quot;
env->cc_n = (int16_t)quot;Quoted 16 lines
target-m68k/op_helper.c +16
-0+ env->cc_v =0 ; +} + +void HELPER (divsw )(CPUM68KState *env,int destr,int32_t den) +{ +int32_t num = env->dregs[destr]; +uint32_t quot, rem; + +if (den ==0 ) { +raise_exception_ra (env,EXCP_DIV0 ,GETPC ()); + } + quot = num / den; + rem = num % den; + + env->cc_c =0 ;/* always cleared, even if overflow */ +if (quot != (int16_t )quot) {
target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 235
Quoted 10 lines
target-m68k/op_helper.c +10
-0+ env->cc_v =-1 ; +/* nothing else is modified */ +/* real 68040 keeps Z and N on overflow, +* whereas documentation says "undefined" +*/ +return ; + } + env->dregs[destr] =deposit32 (quot,16 ,16 , rem); + env->cc_z = quot; + env->cc_n = quot;
Same here:
env->cc_z = (int16_t)quot;
env->cc_n = (int16_t)quot;Thanks,
Laurent
Re: [Qemu-devel] [PATCH v4 2/2] target-m68k: add 680x0 divu/divs variants
Quoted 9 lines · Laurent Vivier
Quoted 3 lines
Quoted 3 lines
+ env->dregs[destr] = deposit32(quot, 16, 16, rem); env->cc_z = quot; env->cc_n = quot;quot is here a 32bit, but the result is only the 16 lower bits, so I think we should have
env->cc_z = (int16_t)quot; env->cc_n = (int16_t)quot;
Yep, you're right.
r~
Re: [Qemu-devel] [PATCH v4 2/2] target-m68k: add 680x0 divu/divs variants
In reply to Laurent VivierQuoted 24 lines · Laurent Vivier
target-m68k/op_helper.c +21
-0+void HELPER (divull )(CPUM68KState *env,int numr,int regr,uint32_t den) +{ +uint64_t num =deposit64 (env->dregs[numr],32 ,32 , env->dregs[regr]); +uint64_t quot; +uint32_t rem; + +if (den ==0 ) { +raise_exception_ra (env,EXCP_DIV0 ,GETPC ()); + } + quot = num / den; + rem = num % den; + + env->cc_c =0 ;/* always cleared, even if overflow */ +if (quot >0xffffffffULL ) { + env->cc_v =-1 ; +/* nothing else is modified */ +/* real 68040 keeps Z and N on overflow, +* whereas documentation says "undefined" +*/ +return ; + }
...
Quoted 16 lines
target-m68k/op_helper.c +6
-3+ +void HELPER (divsll )(CPUM68KState *env,int numr,int regr,int32_t den) +{ +int64_t num =deposit64 (env->dregs[numr],32 ,32 , env->dregs[regr]); +int64_t quot;int32_t rem; - num = env->div1; - den = env->div2;if (den ==0 ) { -raise_exception (env,EXCP_DIV0 ); +raise_exception_ra (env,EXCP_DIV0 ,GETPC ()); } quot = num / den; rem = num % den;
target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 349
Quoted 10 lines
target-m68k/op_helper.c +9
-1- env->cc_v = (word && quot != (int16_t )quot ?-1 :0 ); + env->cc_c =0 ;/* always cleared, even if overflow */ +if (quot != (int32_t )quot) { + env->cc_v =-1 ; +/* nothing else is modified */ +/* real 68040 keeps Z and N on overflow, +* whereas documentation says "undefined" +*/ +return ; + }
On a real 68040, for divsll and divull, Z is unset. (while divsw/divuw don't modify it as we do).
So I will update the patch with this behavior.
Laurent