[PATCH] lib/tests: add KUnit tests for bitrev
From: Masaharu Noguchi
Date: Sat Dec 05 2026 - 04:03:16 EST
Add a KUnit test suite (lib/tests/bitrev_kunit.c) for the bit reversal
APIs in include/linux/bitrev.h:
- bitrev8() (exhaustive over all 256 u8 values plus involution check)
- bitrev16(), bitrev32(), and bitrev8x4() (boundary patterns,
involution checks, and single-bit / inverted single-bit sweeps)
- compile-time constant evaluation via BUILD_BUG_ON() and runtime
verification of constant expressions
Runtime tests use OPTIMIZER_HIDE_VAR() to ensure the runtime
architecture/generic table implementation is exercised rather than
folded into the compile-time constant path.
Tested under ARCH=um with:
./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_BITREV_KUNIT_TEST=y 'bitrev'
Sample KUnit output:
[PASSED] test_bitrev8
[PASSED] test_bitrev16
[PASSED] test_bitrev32
[PASSED] test_bitrev8x4
[PASSED] test_bitrev_const
===================== [PASSED] bitrev ======================
Testing complete. Ran 5 tests: passed: 5
Also verified W=1 compilation of lib/tests/bitrev_kunit.o with no
warnings.
Signed-off-by: Masaharu Noguchi <nogunix@xxxxxxxxx>
---
Note: An LLM (Gemini) was used to help draft the initial test cases and
commit message; all changes were reviewed, verified, and signed off by
the submitter.
---
MAINTAINERS | 1 +
lib/Kconfig.debug | 15 ++++
lib/tests/Makefile | 1 +
lib/tests/bitrev_kunit.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 232 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..596ecc59a139 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4847,6 +4847,7 @@ F: lib/bitrev.c
F: lib/hweight.c
F: lib/test_bitops.c
F: lib/tests/bitops_kunit.c
+F: lib/tests/bitrev_kunit.c
F: tools/*/bitops*
BITOPS API BINDINGS [RUST]
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec13a..e9cbdd50e437 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2736,6 +2736,21 @@ config BITOPS_KUNIT
If unsure, say N.
+config BITREV_KUNIT_TEST
+ tristate "KUnit test for bitrev" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ select BITREVERSE
+ default KUNIT_ALL_TESTS
+ help
+ This builds the bitrev KUnit test suite.
+ It tests bitrev8(), bitrev16(), bitrev32(), and bitrev8x4(),
+ covering both runtime and compile-time constant evaluation paths.
+
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config BITFIELD_KUNIT
tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
depends on KUNIT
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index df0537e1b91f..bda89cb3d278 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -7,6 +7,7 @@ CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
obj-$(CONFIG_842_DECOMPRESS_KUNIT_TEST) += 842_decompress_kunit.o
obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
+obj-$(CONFIG_BITREV_KUNIT_TEST) += bitrev_kunit.o
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
obj-$(CONFIG_BITS_TEST) += test_bits.o
obj-$(CONFIG_SHDI3_KUNIT_TEST) += shdi3_kunit.o
diff --git a/lib/tests/bitrev_kunit.c b/lib/tests/bitrev_kunit.c
new file mode 100644
index 000000000000..83d9ec7a2a26
--- /dev/null
+++ b/lib/tests/bitrev_kunit.c
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * KUnit tests for bitrev8(), bitrev16(), bitrev32(), and bitrev8x4().
+ *
+ * Exercises both the runtime implementation (architecture-specific or
+ * generic table lookup) and the compile-time constant evaluation paths
+ * defined in include/linux/bitrev.h.
+ */
+#include <kunit/test.h>
+
+#include <linux/bitops.h>
+#include <linux/bitrev.h>
+#include <linux/build_bug.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+
+static u32 reference_bitrev(u32 val, unsigned int bits)
+{
+ u32 res = 0;
+ unsigned int i;
+
+ for (i = 0; i < bits; i++) {
+ res = (res << 1) | (val & 1U);
+ val >>= 1;
+ }
+
+ return res;
+}
+
+static void test_bitrev8(struct kunit *test)
+{
+ unsigned int i;
+
+ for (i = 0; i <= U8_MAX; i++) {
+ u8 val = i;
+ u8 rev;
+
+ OPTIMIZER_HIDE_VAR(val);
+ rev = bitrev8(val);
+ KUNIT_EXPECT_EQ(test, rev, (u8)reference_bitrev(i, 8));
+
+ OPTIMIZER_HIDE_VAR(rev);
+ KUNIT_EXPECT_EQ(test, bitrev8(rev), (u8)i);
+ }
+}
+
+static void test_bitrev16(struct kunit *test)
+{
+ static const struct {
+ u16 val;
+ u16 expected;
+ } cases[] = {
+ { 0x0000, 0x0000 },
+ { 0x0001, 0x8000 },
+ { 0x8000, 0x0001 },
+ { 0x00ff, 0xff00 },
+ { 0xff00, 0x00ff },
+ { 0x5555, 0xaaaa },
+ { 0xaaaa, 0x5555 },
+ { 0x0f0f, 0xf0f0 },
+ { 0x1234, 0x2c48 },
+ { U16_MAX, U16_MAX },
+ };
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ u16 val = cases[i].val;
+ u16 rev;
+
+ OPTIMIZER_HIDE_VAR(val);
+ rev = bitrev16(val);
+ KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+ OPTIMIZER_HIDE_VAR(rev);
+ KUNIT_EXPECT_EQ(test, bitrev16(rev), cases[i].val);
+ }
+
+ for (i = 0; i < 16; i++) {
+ u16 single = BIT(i);
+ u16 expected = BIT(15 - i);
+ u16 inverted = ~single;
+
+ OPTIMIZER_HIDE_VAR(single);
+ OPTIMIZER_HIDE_VAR(inverted);
+ KUNIT_EXPECT_EQ(test, bitrev16(single), expected);
+ KUNIT_EXPECT_EQ(test, bitrev16(inverted), (u16)~expected);
+ }
+}
+
+static void test_bitrev32(struct kunit *test)
+{
+ static const struct {
+ u32 val;
+ u32 expected;
+ } cases[] = {
+ { 0x00000000U, 0x00000000U },
+ { 0x00000001U, 0x80000000U },
+ { 0x80000000U, 0x00000001U },
+ { 0x0000ffffU, 0xffff0000U },
+ { 0xffff0000U, 0x0000ffffU },
+ { 0x55555555U, 0xaaaaaaaaU },
+ { 0xaaaaaaaaU, 0x55555555U },
+ { 0x12345678U, 0x1e6a2c48U },
+ { U32_MAX, U32_MAX },
+ };
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ u32 val = cases[i].val;
+ u32 rev;
+
+ OPTIMIZER_HIDE_VAR(val);
+ rev = bitrev32(val);
+ KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+ OPTIMIZER_HIDE_VAR(rev);
+ KUNIT_EXPECT_EQ(test, bitrev32(rev), cases[i].val);
+ }
+
+ for (i = 0; i < 32; i++) {
+ u32 single = BIT(i);
+ u32 expected = BIT(31 - i);
+ u32 inverted = ~single;
+
+ OPTIMIZER_HIDE_VAR(single);
+ OPTIMIZER_HIDE_VAR(inverted);
+ KUNIT_EXPECT_EQ(test, bitrev32(single), expected);
+ KUNIT_EXPECT_EQ(test, bitrev32(inverted), ~expected);
+ }
+}
+
+static void test_bitrev8x4(struct kunit *test)
+{
+ static const struct {
+ u32 val;
+ u32 expected;
+ } cases[] = {
+ { 0x00000000U, 0x00000000U },
+ { 0x01020408U, 0x80402010U },
+ { 0x80402010U, 0x01020408U },
+ { 0x55555555U, 0xaaaaaaaaU },
+ { 0xaaaaaaaaU, 0x55555555U },
+ { 0x0f0f0f0fU, 0xf0f0f0f0U },
+ { 0x12345678U, 0x482c6a1eU },
+ { U32_MAX, U32_MAX },
+ };
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ u32 val = cases[i].val;
+ u32 rev;
+
+ OPTIMIZER_HIDE_VAR(val);
+ rev = bitrev8x4(val);
+ KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+ OPTIMIZER_HIDE_VAR(rev);
+ KUNIT_EXPECT_EQ(test, bitrev8x4(rev), cases[i].val);
+ }
+
+ for (i = 0; i < 32; i++) {
+ unsigned int byte_shift = (i / 8) * 8;
+ unsigned int bit_in_byte = i % 8;
+ u32 single = BIT(i);
+ u32 expected = BIT(byte_shift + (7 - bit_in_byte));
+
+ OPTIMIZER_HIDE_VAR(single);
+ KUNIT_EXPECT_EQ(test, bitrev8x4(single), expected);
+ }
+}
+
+static void test_bitrev_const(struct kunit *test)
+{
+ BUILD_BUG_ON(bitrev8(0x00) != 0x00);
+ BUILD_BUG_ON(bitrev8(0x01) != 0x80);
+ BUILD_BUG_ON(bitrev8(0xa5) != 0xa5);
+ BUILD_BUG_ON(bitrev8(0x12) != 0x48);
+ BUILD_BUG_ON(bitrev8(0xff) != 0xff);
+
+ BUILD_BUG_ON(bitrev16(0x0000) != 0x0000);
+ BUILD_BUG_ON(bitrev16(0x1234) != 0x2c48);
+ BUILD_BUG_ON(bitrev16(0xffff) != 0xffff);
+
+ BUILD_BUG_ON(bitrev32(0x00000000U) != 0x00000000U);
+ BUILD_BUG_ON(bitrev32(0x12345678U) != 0x1e6a2c48U);
+ BUILD_BUG_ON(bitrev32(0xffffffffU) != 0xffffffffU);
+
+ BUILD_BUG_ON(bitrev8x4(0x00000000U) != 0x00000000U);
+ BUILD_BUG_ON(bitrev8x4(0x12345678U) != 0x482c6a1eU);
+ BUILD_BUG_ON(bitrev8x4(0xffffffffU) != 0xffffffffU);
+
+ KUNIT_EXPECT_EQ(test, bitrev8(0x12), (u8)0x48);
+ KUNIT_EXPECT_EQ(test, bitrev16(0x1234), (u16)0x2c48);
+ KUNIT_EXPECT_EQ(test, bitrev32(0x12345678U), 0x1e6a2c48U);
+ KUNIT_EXPECT_EQ(test, bitrev8x4(0x12345678U), 0x482c6a1eU);
+}
+
+static struct kunit_case bitrev_test_cases[] = {
+ KUNIT_CASE(test_bitrev8),
+ KUNIT_CASE(test_bitrev16),
+ KUNIT_CASE(test_bitrev32),
+ KUNIT_CASE(test_bitrev8x4),
+ KUNIT_CASE(test_bitrev_const),
+ {},
+};
+
+static struct kunit_suite bitrev_test_suite = {
+ .name = "bitrev",
+ .test_cases = bitrev_test_cases,
+};
+
+kunit_test_suite(bitrev_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for bitrev");
+MODULE_LICENSE("GPL");
---
base-commit: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3
change-id: 20261205-bitrev-kunit-0aa52a7ed8b2
Best regards,
--
Masaharu Noguchi <nogunix@xxxxxxxxx>