Test CLO, CLZ and bitswap.

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

diff --git a/tests/tcg/mipsel/test_mips32r6_bitops.c 
b/tests/tcg/mipsel/test_mips32r6_bitops.c
new file mode 100644
index 000000000000..dc55530b3b18
--- /dev/null
+++ b/tests/tcg/mipsel/test_mips32r6_bitops.c
@@ -0,0 +1,82 @@
+/*
+ * MIPS32R6 tests: bit operations 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 (*unary_op_32)(const uint32_t *, uint32_t *);
+typedef uint32_t (*ref_op_32)(uint32_t);
+
+static uint32_t ref_clz(uint32_t a) { return a ? __builtin_clz(a) : 32; }
+static uint32_t ref_clo(uint32_t a) { return ref_clz(~a); }
+
+/* BITSWAP reverses the bit order inside each byte. */
+static uint32_t ref_bitswap(uint32_t a)
+{
+    uint32_t res = 0;
+
+    for (int byte = 0; byte < 4; byte++) {
+        for (int bit = 0; bit < 8; bit++) {
+            if (a & (1u << (byte * 8 + bit))) {
+                res |= 1u << (byte * 8 + 7 - bit);
+            }
+        }
+    }
+    return res;
+}
+
+static int check_all(const char *name, unary_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++) {
+        uint32_t a = input[i], res = 0;
+
+        op(&a, &res);
+        if (res != ref(a)) {
+            printf("# %s: %08x: got %08x expected %08x\n",
+                   name, a, res, ref(a));
+            fail = 1;
+        }
+    }
+    return fail;
+}
+
+static int run(const char *name, unary_op_32 op, ref_op_32 ref)
+{
+    static int count;
+    int ret = 0;
+
+    ret |= check_all(name, op, ref, b32_pattern, PATTERN_INPUTS_32_COUNT);
+    ret |= check_all(name, op, ref, b32_random, RANDOM_INPUTS_32_COUNT);
+    /* Every leading-run length, for both polarities. */
+    for (int n = 0; n < 32; n++) {
+        uint32_t v[2] = { 0xffffffffu >> n, ~(0xffffffffu >> n) };
+
+        ret |= check_all(name, op, ref, v, 2);
+        v[0] = 1u << n;
+        v[1] = ~(1u << n);
+        ret |= check_all(name, op, ref, v, 2);
+    }
+    printf("%s %d - MIPS32R6 bitops %s\n", ret ? "not ok" : "ok", ++count, 
name);
+    return ret;
+}
+
+int main(void)
+{
+    int ret = 0;
+
+    printf("TAP version 14\n1..3\n");
+    ret |= run("CLO", do_mips32r6_CLO, ref_clo);
+    ret |= run("CLZ", do_mips32r6_CLZ, ref_clz);
+    ret |= run("BITSWAP", do_mips32r6_BITSWAP, ref_bitswap);
+
+    return ret ? 1 : 0;
+}
diff --git a/tests/tcg/mipsel/meson.build b/tests/tcg/mipsel/meson.build
index 7a6dff32cbf4..837b26698e06 100644
--- a/tests/tcg/mipsel/meson.build
+++ b/tests/tcg/mipsel/meson.build
@@ -88,6 +88,7 @@ endif
 tests = []
 
 tests_mips32r6 = [
+  'test_mips32r6_bitops.c',
   'test_mips32r6_logic.c',
   'test_mips32r6_shift.c',
 ]
-- 
2.55.0


Reply via email to