Test DIV[U] and MOD[U]. A zero divisor and INT_MIN / -1
are skipped: their result is UNPREDICTABLE.

Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
---
 tests/tcg/mips/include/wrappers_mips32r6.h |  5 +++
 tests/tcg/mipsel/test_mips32r6_multiply.c  | 45 ++++++++++++++++++----
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/tests/tcg/mips/include/wrappers_mips32r6.h 
b/tests/tcg/mips/include/wrappers_mips32r6.h
index aaa8afcae95b..cbff8450b656 100644
--- a/tests/tcg/mips/include/wrappers_mips32r6.h
+++ b/tests/tcg/mips/include/wrappers_mips32r6.h
@@ -53,6 +53,11 @@ DO_MIPS32R6__RD__RS_RT(MUH, muh)
 DO_MIPS32R6__RD__RS_RT(MULU, mulu)
 DO_MIPS32R6__RD__RS_RT(MUHU, muhu)
 
+DO_MIPS32R6__RD__RS_RT(DIV, div)
+DO_MIPS32R6__RD__RS_RT(MOD, mod)
+DO_MIPS32R6__RD__RS_RT(DIVU, divu)
+DO_MIPS32R6__RD__RS_RT(MODU, modu)
+
 DO_MIPS32R6__RD__RS_RT(AND, and)
 DO_MIPS32R6__RD__RS_RT(NOR, nor)
 DO_MIPS32R6__RD__RS_RT(OR, or)
diff --git a/tests/tcg/mipsel/test_mips32r6_multiply.c 
b/tests/tcg/mipsel/test_mips32r6_multiply.c
index 17106391107a..a58f757b24c0 100644
--- a/tests/tcg/mipsel/test_mips32r6_multiply.c
+++ b/tests/tcg/mipsel/test_mips32r6_multiply.c
@@ -12,6 +12,7 @@
 
 typedef void (*binary_op_32)(const uint32_t *, const uint32_t *, uint32_t *);
 typedef uint32_t (*ref_op_32)(uint32_t, uint32_t);
+typedef int (*valid_op_32)(uint32_t, uint32_t);
 
 static uint32_t ref_mul(uint32_t a, uint32_t b)
 {
@@ -33,8 +34,28 @@ static uint32_t ref_muhu(uint32_t a, uint32_t b)
     return (uint32_t)(((uint64_t)a * b) >> 32);
 }
 
+/* Division by zero and INT_MIN / -1 have UNPREDICTABLE results. */
+static int valid_udiv(uint32_t a, uint32_t b) { return b != 0; }
+static int valid_sdiv(uint32_t a, uint32_t b)
+{
+    return b != 0 && !(a == 0x80000000u && b == 0xffffffffu);
+}
+
+static uint32_t ref_div(uint32_t a, uint32_t b)
+{
+    return (uint32_t)((int32_t)a / (int32_t)b);
+}
+
+static uint32_t ref_mod(uint32_t a, uint32_t b)
+{
+    return (uint32_t)((int32_t)a % (int32_t)b);
+}
+
+static uint32_t ref_divu(uint32_t a, uint32_t b) { return a / b; }
+static uint32_t ref_modu(uint32_t a, uint32_t b) { return a % b; }
+
 static int check_op(const char *name, binary_op_32 op, ref_op_32 ref,
-                    const uint32_t *input, size_t count)
+                    valid_op_32 valid, const uint32_t *input, size_t count)
 {
     int fail = 0;
 
@@ -42,6 +63,9 @@ static int check_op(const char *name, binary_op_32 op, 
ref_op_32 ref,
         for (size_t j = 0; j < count; j++) {
             uint32_t a = input[i], b = input[j], res = 0;
 
+            if (valid && !valid(a, b)) {
+                continue;
+            }
             op(&a, &b, &res);
             if (res != ref(a, b)) {
                 TAP_INFO("%s: %08x, %08x: got %08x expected %08x",
@@ -53,12 +77,13 @@ static int check_op(const char *name, binary_op_32 op, 
ref_op_32 ref,
     return fail;
 }
 
-static int run(const char *name, binary_op_32 op, ref_op_32 ref)
+static int run(const char *name, binary_op_32 op, ref_op_32 ref,
+               valid_op_32 valid)
 {
     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);
+    ret |= check_op(name, op, ref, valid, b32_pattern, 
PATTERN_INPUTS_32_COUNT);
+    ret |= check_op(name, op, ref, valid, b32_random, RANDOM_INPUTS_32_COUNT);
     tap_report(!ret, "MIPS32R6 multiply %s", name);
     return ret;
 }
@@ -66,10 +91,14 @@ static int run(const char *name, binary_op_32 op, ref_op_32 
ref)
 int main(void)
 {
     TAP_START();
-    run("MUL", do_mips32r6_MUL, ref_mul);
-    run("MUH", do_mips32r6_MUH, ref_muh);
-    run("MULU", do_mips32r6_MULU, ref_mulu);
-    run("MUHU", do_mips32r6_MUHU, ref_muhu);
+    run("MUL", do_mips32r6_MUL, ref_mul, NULL);
+    run("MUH", do_mips32r6_MUH, ref_muh, NULL);
+    run("MULU", do_mips32r6_MULU, ref_mulu, NULL);
+    run("MUHU", do_mips32r6_MUHU, ref_muhu, NULL);
+    run("DIV", do_mips32r6_DIV, ref_div, valid_sdiv);
+    run("MOD", do_mips32r6_MOD, ref_mod, valid_sdiv);
+    run("DIVU", do_mips32r6_DIVU, ref_divu, valid_udiv);
+    run("MODU", do_mips32r6_MODU, ref_modu, valid_udiv);
 
     return tap_done();
 }
-- 
2.55.0


Reply via email to