From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796461393; b=UMUqbuXQfelLeN2rdwEelsvS0xjJd6CqlLfG53G0ezu0fosWotHC0IAZb+nV/5AC587z y5pVJPPM3a4E2fyTSTZrhDCyXpwLpuwMXqHNyoWqtoKMe/DlWkdiLIvRgmRN2iLeU20Nk WmEUF09Y4jAQ+TObZTanUmgYRtfgBo3FZt/pCfHRAopBLVbL5I5eOgFlMjHcikppbDrf5 JZo3V/LJ4QjV3SqkaPFvGeO+7KKihyujWPIlVTA9IoXhHSc+7r77dyzJxH/E0YXngLP7o GMaC2PXWMv5mwpR/+HgeOx9vxNzxi/YDPgv4nOeDVCw7qOLHxjN8EJZAWu3pvlPJqUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796461393; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key; bh=8+24KPD2IPM6HPiwiqdQJdcHIloVOcXz3we5HzeXvmM=; b=mZ6hEot6ZIswJRywY36FE9cn0mnidL3kDZq0d0xaBH86ISxZdRA1jxmDU8SlqMR42qJk xG3VURFrClx/mBHGduU2K73ZTSBS655tjSeEneIZYyjbPtsMTY6ECvu35wsLVO58l0RcU SyV5ENXwD8FYmMOruJF6E9fUowBAo1dE3QXN5mhpxoF0Ek8hNUkxdsCy5D9nXqRkhPgH3 T+sPed5/sP4UwC5BgWLOAjREQRquhWqx+paqxJAMJ6HwNUSpi3M4p2A+GEm8Z3lroamcs 9IUeMot1tvPwsQd2HOoc1Mq40nRKg9dVHuPVpCMMQhnJ8wFWRlZvsf/YmHZsMh5JLhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GK3jphrc; arc=none smtp.remote-ip=209.85.210.174 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAD61606A32F for ; Sat, 5 Dec 2026 09:03:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AAD61606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GK3jphrc Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-8bddb1172a4so185721b3a.1 for ; Sat, 05 Dec 2026 01:03:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796461392; x=1797066192; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=8+24KPD2IPM6HPiwiqdQJdcHIloVOcXz3we5HzeXvmM=; b=GK3jphrcmIt7Vl7klAuSYFEbQsnVLA4u8XPerUzu3xAYGnj1KAyg0CPokLeD3wRBT1 m4OguB+bK17paw22C0k6vG0o2kfDUjRzXLZ4Yq8f538ZLHdwDDTfdTgggQ/fNzR9PIhq /e8utkIHmmfTnskao5FgKmyNuycwSBCo/YbgFUrCNm74bTqPxN1LAVrfcV81P2MWMU8o 4NEctZOUUah/gR1AQ51mW9krb5V8CoHx1sCpHrPE04nF0+3VL75DHTIK1fKeWJh7u2d8 tDGTFu026OI9H+DTXbAd8demzm0bDIryGQlqQZn8hEeSqNyUBPlfPG9uPw61MR2Eq1ZU 8xXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796461392; x=1797066192; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=8+24KPD2IPM6HPiwiqdQJdcHIloVOcXz3we5HzeXvmM=; b=C0jJAinq9RKJ6fibIO0pF80E83YjUG6KrtYvXMN0b2i2BcoqWiUSZKMXXMx1hdhymC KMXfI84zwOXuvyqtbCBo09NOPIQxR14AJZmqlgJizjytuIAQ6VCR+teM5ENciUAUZgX3 1dGVCygzx/lzf90AX7iu1kNQHy+MIyWspbGHA/zR5rg3bur/msI75NY/+gjyy7XnicXO FT1bCsJryXdPJuWq1xiPNnJRgbUGB3xAMul4NRFuzAE8PzCveFLhulZKTpUeW/cUx21J vCZXdlPJa5IXcT82GNPnaxCle1YuSnR+vrbK2fqlaOA7HIZ8AxBNxCDcOAGObJu97Wkx KSnw== X-Forwarded-Encrypted: i=1; AL+hErGsINKijuMKebyb7OKcC2QsDTEfOv+m0MbcBoMD7DQjca0EbZBhk/YLt6TyXix8pXD5iicfvnRxN3mYZNk=@vger.kernel.org X-Gm-Message-State: AB4q+uGE0554n4+BB3r69qr7jyFyl/ntYmH9NAAqn5MSiiD3+zIkFWmP 4s3QyRM4Ph39PvJZF6Q9HJzASyul9W5Bh/7WR7ecD1w9+B3pUinCuQZS X-Gm-Gg: AZFdTZcexf4xUuwA7tUMY5Pwugu80PNr4LgCBbnEwA3vpS03H7QeuUT3UxZrG4/WfDl J67RY4FJErX1eqgjHE6vuqca83wU8eCQvvZQMNZ7QZSxDhGjpiH6YUozKmyBpg2CQbpFjQeuLIQ XgxsL4q9w0JB4kh2nO5+b1IVBXtzdbhPAXTqu4R3CCQ/SjbbsQfTh5RLSCvred0f84vnCYi4tM3 WB4bqAHBcfVIvjbL6ICRcfkA1sKo2wgmUl4NX0tfkV1Lpn9lelysS2M37plhGS5QTAajXUzbryz eD4WGKCdKkns6I4f8isQuJapZF+gryUPcwOc8AlWuuk9ujZg6lcUltND46PznRsmIeJ5kpZpYuF mVX4siGtJSVqtt6YH3AHwCxEXGQThWocNMKOvPo3siZ7Gm3KsN8LWsZHf0ZE7y9fgencI/E3IpN tyfQORYUTbRvdBIOKZgojmDVv+aDW5MdNpVTEeAxCIHEgXDN6Om3Ydand7ag92Q8E/d/9opFoau 6DbquUhFqJ3u6JdeGDvVsnKz2569s0f41VZngPntDyx9m72C2DpANKXkiuizgIkw5P9uftQRRLu f5aAQUTxI/4OxU+kL9/nh9SXsBFqWJs9v+ul X-Received: by 2002:a17:90b:1cc5:b0:3b0:9e2c:9b55 with SMTP id 98e67ed59e1d1-3b60e055076mr3582807a91.2.1796461391895; Sat, 05 Dec 2026 01:03:11 -0800 (PST) Received: from [192.168.20.112] ([2400:4050:d860:9700:75bf:9e2e:8ac9:3001]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddc73566sm8041218a91.17.2026.12.05.01.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 01:03:11 -0800 (PST) From: Masaharu Noguchi Date: Sat, 05 Dec 2026 18:02:50 +0900 Subject: [PATCH] lib/tests: add KUnit tests for bitrev X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261205-bitrev-kunit-v1-1-f8b712691e04@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAA/x3MYQpAQBAG0KtM329ba2rRXkV+LAaTWtpFSu6uv AO8B1mSSoanB0kuzbpFeCoLwrCEOIvREZ7AlquSrTO9Hkkus55RD2NDcBxqGZueURD2JJPef9d 27/sBOEv6I14AAAA= X-Change-ID: 20261205-bitrev-kunit-0aa52a7ed8b2 To: Andrew Morton , Yury Norov , Rasmus Villemoes Cc: Yury Norov , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Masaharu Noguchi X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796461390; l=9165; i=nogunix@gmail.com; s=20261202; h=from:subject:message-id; bh=gQSJLZtoPbF96f9X3HS1VaCDJ7FtfXRka+5ZwdrRuH0=; b=J43Yg35gg2axCUmeDWXQJxj9blZh97TY+VISbsGYJ659viq4oLJQhp8UQZWysSsbt9OvR+eH7 P/F8cYZPmJEDwW1zdO6Rj5082+CigQyPi1020lf2EIfBnK/TsVa15yM X-Developer-Key: i=nogunix@gmail.com; a=ed25519; pk=NgtNsZ+zeVmdqlocewi1nv2/pD3VYxk5S++pnPHKDf8= 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 --- 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 + +#include +#include +#include +#include +#include + +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