Commit d7210e568f for qemu.org
commit d7210e568f948f4a877e03d6434598f49e3e9014
Author: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Date: Thu Oct 29 11:18:18 2026 +0100
tests/tcg/mipsel: Add MIPS32R6 bit operation tests
Test CLO, CLZ and bitswap.
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Message-ID: <20261030182546.2508-6-philmd@oss.qualcomm.com>
diff --git a/tests/tcg/mipsel/meson.build b/tests/tcg/mipsel/meson.build
index 3c40a556ad..0c43c31e73 100644
--- a/tests/tcg/mipsel/meson.build
+++ b/tests/tcg/mipsel/meson.build
@@ -67,6 +67,7 @@ endif
tests = []
tests_mips32r6 = [
+ 'test_mips32r6_bitops.c',
'test_mips32r6_logic.c',
'test_mips32r6_shift.c',
]
diff --git a/tests/tcg/mipsel/test_mips32r6_bitops.c b/tests/tcg/mipsel/test_mips32r6_bitops.c
new file mode 100644
index 0000000000..b2d5ab2468
--- /dev/null
+++ b/tests/tcg/mipsel/test_mips32r6_bitops.c
@@ -0,0 +1,82 @@
+/*
+ * MIPS32R6 tests: bit operations instructions
+ *
+ * Author:
+ * Philippe Mathieu-Daudé
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdint.h>
+
+#include "../include/tap.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)) {
+ TAP_INFO("%s: %08x: got %08x expected %08x",
+ name, a, res, ref(a));
+ fail = 1;
+ }
+ }
+ return fail;
+}
+
+static int run(const char *name, unary_op_32 op, ref_op_32 ref)
+{
+ 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);
+ }
+ tap_report(!ret, "MIPS32R6 bitops %s", name);
+ return ret;
+}
+
+int main(void)
+{
+ TAP_START();
+ run("CLO", do_mips32r6_CLO, ref_clo);
+ run("CLZ", do_mips32r6_CLZ, ref_clz);
+ run("BITSWAP", do_mips32r6_BITSWAP, ref_bitswap);
+
+ return tap_done();
+}