Skip to contentSkip to conversation

target/m68k: Implement fmove.p

PATCH Superseded Richard Henderson 1 participant 5 messages Aug 11, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/m68k: Implement fmove.p

Implement packed decimal real conversions.

This is perhaps too trivial, as it does not produce correctly rounded results compared to the compiler. I'm not sure how else to implement this without using gmp, which seems a bit heavy-weight.

Perhaps we could do better with a compile-time table of powers of 10, rather that the pile of runtime roundings that I use in this patch set. If the host libc supports float128, we could probably leverage scanf to produce properly rounded results, but that ignores other hosts.

r~

Richard Henderson (4):
target/m68k: Introduce M68K_FEATURE_FPU_PACKED_DECIMAL target/m68k: Implement packed decimal real loads target/m68k: Implement packed decimal real stores tests/tcg/m68k: Add packed decimal tests

 target/m68k/cpu.h                |   2 +
 target/m68k/helper.h             |   3 +
 target/m68k/cpu.c                |   2 +
 target/m68k/fpu_helper.c         | 237 +++++++++++++++++++++++++++++++
 target/m68k/translate.c          |  49 +++++--
 tests/tcg/m68k/packeddecimal-1.c |  45 ++++++
 tests/tcg/m68k/packeddecimal-2.c |  42 ++++++
 tests/tcg/m68k/Makefile.target   |   2 +-
 8 files changed, 368 insertions(+), 14 deletions(-)
 create mode 100644 tests/tcg/m68k/packeddecimal-1.c
 create mode 100644 tests/tcg/m68k/packeddecimal-2.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 target/m68k: Introduce M68K_FEATURE_FPU_PACKED_DECIMAL

Set for 68020 and 68030, but does nothing so far.

---
 target/m68k/cpu.h | 2 ++
 target/m68k/cpu.c | 2 ++
 2 files changed, 4 insertions(+)
target/m68k/cpu.h +2 -0
index b5bbeedb7a..7114324e4d 100644
@@ -553,6 +553,8 @@ enum m68k_features {
     M68K_FEATURE_MOVEFROMSR_PRIV,
     /* Exception frame with format+vector (from 68010) */
     M68K_FEATURE_EXCEPTION_FORMAT_VEC,
+    /* FPU supports packed decimal real format */
+    M68K_FEATURE_FPU_PACKED_DECIMAL,
 };
 
 static inline bool m68k_feature(CPUM68KState *env, int feature)
target/m68k/cpu.c +2 -0
index 1d49f4cb23..65058dd052 100644
@@ -182,6 +182,7 @@ static void m68020_cpu_initfn(Object *obj)
     m68k_set_feature(env, M68K_FEATURE_MSP);
     m68k_set_feature(env, M68K_FEATURE_UNALIGNED_DATA);
     m68k_set_feature(env, M68K_FEATURE_TRAPCC);
+    m68k_set_feature(env, M68K_FEATURE_FPU_PACKED_DECIMAL);
 }
 
 /*
@@ -224,6 +225,7 @@ static void m68040_cpu_initfn(Object *obj)
 
     m68030_cpu_initfn(obj);
     m68k_unset_feature(env, M68K_FEATURE_M68030);
+    m68k_unset_feature(env, M68K_FEATURE_FPU_PACKED_DECIMAL);
     m68k_set_feature(env, M68K_FEATURE_M68040);
 }
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 target/m68k: Implement packed decimal real loads

In reply to Richard Henderson
---
 target/m68k/helper.h     |   2 +
 target/m68k/fpu_helper.c | 137 +++++++++++++++++++++++++++++++++++++++
 target/m68k/translate.c  |  22 ++++---
 3 files changed, 151 insertions(+), 10 deletions(-)
target/m68k/helper.h +2 -0
index 95aa5e53bb..2c71361451 100644
@@ -126,6 +126,8 @@ DEF_HELPER_FLAGS_4(bfffo_mem, TCG_CALL_NO_WG, i64, env, i32, s32, i32)
 DEF_HELPER_3(chk, void, env, s32, s32)
 DEF_HELPER_4(chk2, void, env, s32, s32, s32)
 
+DEF_HELPER_FLAGS_3(load_pdr_to_fx80, TCG_CALL_NO_RWG, void, env, fp, tl)
+
 #if !defined(CONFIG_USER_ONLY)
 DEF_HELPER_3(ptest, void, env, i32, i32)
 DEF_HELPER_3(pflush, void, env, i32, i32)
target/m68k/fpu_helper.c +137 -0

140 lines not on this page · raw message

target/m68k/translate.c +12 -10
index 445966fb6a..59e7d27393 100644
@@ -963,11 +963,11 @@ static void gen_load_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp,
         tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
         break;
     case OS_PACKED:
-        /*
-         * unimplemented data type on 68040/ColdFire
-         * FIXME if needed for another FPU
-         */
-        gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+        if (!m68k_feature(s->env, M68K_FEATURE_FPU_PACKED_DECIMAL)) {
+            gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+            break;
+        }
+        gen_helper_load_pdr_to_fx80(tcg_env, fp, addr);
         break;
     default:
         g_assert_not_reached();
@@ -1142,11 +1142,13 @@ static int gen_ea_mode_fp(CPUM68KState *env, DisasContext *s, int mode,
                 tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
                 break;
             case OS_PACKED:
-                /*
-                 * unimplemented data type on 68040/ColdFire
-                 * FIXME if needed for another FPU
-                 */
-                gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+                if (!m68k_feature(s->env, M68K_FEATURE_FPU_PACKED_DECIMAL)) {
+                    gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+                    break;
+                }
+                tmp = tcg_constant_tl(s->pc);
+                s->pc += 12;
+                gen_helper_load_pdr_to_fx80(tcg_env, fp, tmp);
                 break;
             default:
                 g_assert_not_reached();
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 target/m68k: Implement packed decimal real stores

In reply to Richard Henderson

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2488

---
 target/m68k/helper.h     |   1 +
 target/m68k/fpu_helper.c | 100 +++++++++++++++++++++++++++++++++++++++
 target/m68k/translate.c  |  27 +++++++++--
 3 files changed, 125 insertions(+), 3 deletions(-)
target/m68k/helper.h +1 -0
index 2c71361451..21af6adb39 100644
@@ -127,6 +127,7 @@ DEF_HELPER_3(chk, void, env, s32, s32)
 DEF_HELPER_4(chk2, void, env, s32, s32, s32)
 
 DEF_HELPER_FLAGS_3(load_pdr_to_fx80, TCG_CALL_NO_RWG, void, env, fp, tl)
+DEF_HELPER_FLAGS_4(store_fx80_to_pdr, TCG_CALL_NO_RWG, void, env, tl, fp, int)
 
 #if !defined(CONFIG_USER_ONLY)
 DEF_HELPER_3(ptest, void, env, i32, i32)
target/m68k/fpu_helper.c +100 -0

103 lines not on this page · raw message

target/m68k/translate.c +24 -3
index 59e7d27393..fb5ecce0c3 100644
@@ -119,6 +119,8 @@ typedef struct DisasContext {
     int done_mac;
     int writeback_mask;
     TCGv writeback[8];
+    uint16_t insn;
+    uint16_t ext;
     bool ss_active;
 } DisasContext;
 
@@ -671,6 +673,7 @@ static inline int ext_opsize(int ext, int pos)
     case 4: return OS_WORD;
     case 5: return OS_DOUBLE;
     case 6: return OS_BYTE;
+    case 7: return OS_PACKED; /* store, dynamic k-factor */
     default:
         g_assert_not_reached();
     }
@@ -1010,11 +1013,25 @@ static void gen_store_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp,
         tcg_gen_qemu_st_i64(t64, tmp, index, MO_TEUQ);
         break;
     case OS_PACKED:
+        if (!m68k_feature(s->env, M68K_FEATURE_FPU_PACKED_DECIMAL)) {
+            gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+            break;
+        }
         /*
-         * unimplemented data type on 68040/ColdFire
-         * FIXME if needed for another FPU
+         * For stores we must recover k-factor, either from an
+         * immediate or the low 7 bits of a D register.
          */
-        gen_exception(s, s->base.pc_next, EXCP_FP_UNIMP);
+        switch ((s->ext >> 10) & 7) {
+        case 3:
+            tcg_gen_movi_i32(tmp, sextract32(s->ext, 0, 7));
+            break;
+        case 7:
+            tcg_gen_sextract_i32(tmp, DREG(s->ext, 4), 0, 7);
+            break;
+        default:
+            g_assert_not_reached();
+        }
+        gen_helper_store_fx80_to_pdr(tcg_env, addr, fp, tmp);
         break;
     default:
         g_assert_not_reached();
@@ -4940,6 +4957,8 @@ DISAS_INSN(fpu)
     TCGv_ptr cpu_src, cpu_dest;
 
     ext = read_im16(env, s);
+    s->ext = ext;
+
     opmode = ext & 0x7f;
     switch ((ext >> 13) & 7) {
     case 0:
@@ -6042,6 +6061,8 @@ static void m68k_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     CPUM68KState *env = cpu_env(cpu);
     uint16_t insn = read_im16(env, dc);
 
+    dc->insn = insn;
+    dc->ext = 0;
     opcode_table[insn](env, dc, insn);
     do_writebacks(dc);
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 tests/tcg/m68k: Add packed decimal tests

In reply to Richard Henderson
---
 tests/tcg/m68k/packeddecimal-1.c | 45 ++++++++++++++++++++++++++++++++
 tests/tcg/m68k/packeddecimal-2.c | 42 +++++++++++++++++++++++++++++
 tests/tcg/m68k/Makefile.target   |  2 +-
 3 files changed, 88 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/m68k/packeddecimal-1.c
 create mode 100644 tests/tcg/m68k/packeddecimal-2.c
tests/tcg/m68k/packeddecimal-1.c new file +45 -0
new file mode 100644
index 0000000000..812307402a
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Test packed decimal real conversion to long double. */
+
+#include <stdio.h>
+
+struct T {
+    unsigned int d[3];
+    double f;
+};
+
+static const struct T tests[] = {
+    { { 0x00000001, 0x00000000, 0x00000000 }, 1.0e0l },
+    { { 0x01000001, 0x00000000, 0x00000000 }, 1.0e1l },
+    { { 0x00100001, 0x00000000, 0x00000000 }, 1.0e10l },
+    { { 0x00000000, 0x10000000, 0x00000000 }, 0.1e0l },
+    { { 0x41000001, 0x00000000, 0x00000000 }, 1.0e-1l },
+    { { 0x85000005, 0x55550000, 0x00000000 }, -5.5555e5l },
+    { { 0x09990009, 0x99999999, 0x99999999 }, 9.9999999999999999e999l },
+    { { 0x03210001, 0x23456789, 0x12345678 }, 1.2345678912345678e123l },
+    { { 0x00000000, 0x00000000, 0x00000000 }, 0.0l },
+    { { 0x80000000, 0x00000000, 0x00000000 }, -0.0l },
+    { { 0x09990000, 0x00000000, 0x00000000 }, 0.0e999l },
+};
+
+int main()
+{
+    int ret = 0;
+
+    for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+        const struct T *t = &tests[i];
+        long double lf;
+        volatile double f;
+
+        asm("fmove.p (%1),%0" : "=f"(lf) : "a"(t->d));
+
+        /* Round to double; as yet we cannot promise perfect rounding. */
+        f = lf;
+
+        if (f != t->f) {
+            fprintf(stderr, "Mismatch at %d: %.17e != %.17e\n", i, f, t->f);
+            ret = 1;
+        }
+    }
+    return ret;
+}
tests/tcg/m68k/packeddecimal-2.c new file +42 -0

42 lines not on this page · raw message

tests/tcg/m68k/Makefile.target +1 -1
index 33f7b1b127..7587b8f249 100644
@@ -4,4 +4,4 @@
 #
 
 VPATH += $(SRC_PATH)/tests/tcg/m68k
-TESTS += trap denormal
+TESTS += trap denormal packeddecimal-1 packeddecimal-2
Signature · 1 line
2.43.0