Skip to contentSkip to conversation

680x0 mul and div instructions

PATCH Superseded Laurent Vivier 2 participants 5 messages Oct 31, 2016 – Nov 1, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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.

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
#1 lore raw

PATCH v3 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
index c00978d..93e201c 100644
@@ -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
#3 lore raw

PATCH v3 2/2 target-m68k: add 680x0 divu/divs variants

In reply to 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.

---
 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
index 75b199f..c1d5eb4 100644
@@ -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
index 6dfb54e..0b4ed7b 100644
@@ -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
index aae01f9..d863e55 100644
@@ -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
index 156c0f5..51ff43b 100644
@@ -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
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] target-m68k: add 680x0 divu/divs variants

target-m68k/op_helper.c void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) · line 297

Quoted 3 lines · Laurent Vivier
target-m68k/op_helper.c +3 -0
+void HELPER(divull)(CPUM68KState *env, int numr, int regr, uint32_t den)
+{
+    uint32_t num = env->dregs[numr];
  uint64_t num = deposit64(env->dregs[numr], 32, 32, env->dregs[regr]);

Similarly in divsll with int64_t num.

Quoted 1 line

+ if (!((quot >> 31) == 0 || (quot >> 31) == -1)) {

Maybe better as

if (quot != (int32_t)quot)

as in divsw.

Otherwise it looks good.

r~