Test MUL[U] and MUH[U].

Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
---
 tests/tcg/mipsel/test_mips32r6_multiply.c | 79 +++++++++++++++++++++++
 tests/tcg/mipsel/meson.build              |  1 +
 2 files changed, 80 insertions(+)
 create mode 100644 tests/tcg/mipsel/test_mips32r6_multiply.c

diff --git a/tests/tcg/mipsel/test_mips32r6_multiply.c 
b/tests/tcg/mipsel/test_mips32r6_multiply.c
new file mode 100644
index 000000000000..94c4a3426ba2
--- /dev/null
+++ b/tests/tcg/mipsel/test_mips32r6_multiply.c
@@ -0,0 +1,79 @@
+/*
+ * MIPS32R6 tests: multiply instructions
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include "../mips/include/wrappers_mips32r6.h"
+#include "../mips/include/test_inputs_32.h"
+
+typedef void (*binary_op_32)(const uint32_t *, const uint32_t *, uint32_t *);
+typedef uint32_t (*ref_op_32)(uint32_t, uint32_t);
+
+static uint32_t ref_mul(uint32_t a, uint32_t b)
+{
+    return (uint32_t)((int64_t)(int32_t)a * (int32_t)b);
+}
+
+static uint32_t ref_muh(uint32_t a, uint32_t b)
+{
+    return (uint32_t)(((int64_t)(int32_t)a * (int32_t)b) >> 32);
+}
+
+static uint32_t ref_mulu(uint32_t a, uint32_t b)
+{
+    return (uint32_t)((uint64_t)a * b);
+}
+
+static uint32_t ref_muhu(uint32_t a, uint32_t b)
+{
+    return (uint32_t)(((uint64_t)a * b) >> 32);
+}
+
+static int check_op(const char *name, binary_op_32 op, ref_op_32 ref,
+                    const uint32_t *input, size_t count)
+{
+    int fail = 0;
+
+    for (size_t i = 0; i < count; i++) {
+        for (size_t j = 0; j < count; j++) {
+            uint32_t a = input[i], b = input[j], res = 0;
+
+            op(&a, &b, &res);
+            if (res != ref(a, b)) {
+                printf("# %s: %08x, %08x: got %08x expected %08x\n",
+                       name, a, b, res, ref(a, b));
+                fail = 1;
+            }
+        }
+    }
+    return fail;
+}
+
+static int run(const char *name, binary_op_32 op, ref_op_32 ref)
+{
+    static int count;
+    int ret = 0;
+
+    ret |= check_op(name, op, ref, b32_pattern, PATTERN_INPUTS_32_COUNT);
+    ret |= check_op(name, op, ref, b32_random, RANDOM_INPUTS_32_COUNT);
+    printf("%s %d - MIPS32R6 multiply %s\n", ret ? "not ok" : "ok", ++count,
+           name);
+    return ret;
+}
+
+int main(void)
+{
+    int ret = 0;
+
+    printf("TAP version 14\n1..4\n");
+    ret |= run("MUL", do_mips32r6_MUL, ref_mul);
+    ret |= run("MUH", do_mips32r6_MUH, ref_muh);
+    ret |= run("MULU", do_mips32r6_MULU, ref_mulu);
+    ret |= run("MUHU", do_mips32r6_MUHU, ref_muhu);
+
+    return ret ? 1 : 0;
+}
diff --git a/tests/tcg/mipsel/meson.build b/tests/tcg/mipsel/meson.build
index 837b26698e06..9d7cfcf1c6a0 100644
--- a/tests/tcg/mipsel/meson.build
+++ b/tests/tcg/mipsel/meson.build
@@ -90,6 +90,7 @@ tests = []
 tests_mips32r6 = [
   'test_mips32r6_bitops.c',
   'test_mips32r6_logic.c',
+  'test_mips32r6_multiply.c',
   'test_mips32r6_shift.c',
 ]
 
-- 
2.55.0


Reply via email to