From: Yury Norov <ynorov@nvidia.com>
To: Masaharu Noguchi <nogunix@gmail.com>
Cc: Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH] lib/tests: extend KUnit test for bitops with hweight APIs
Date: Wed, 2 Dec 2026 15:10:09 -0500 [thread overview]
Message-ID: <axB7IWY2TIKivJOF@yury> (raw)
In-Reply-To: <20261202-bitops-hweight-v1-1-d0232905d1c0@gmail.com>
On Wed, Dec 02, 2026 at 07:24:19PM +0900, Masaharu Noguchi wrote:
> Extend the bitops KUnit test suite to cover the Hamming weight APIs:
>
> - hweight8() (exhaustive over all 256 u8 values)
> - hweight16(), hweight32(), hweight64(), and hweight_long()
> (boundary patterns, single-bit and inverted single-bit sweeps)
> - compile-time constant evaluation via hweightN() and HWEIGHT*()
>
> Runtime tests use OPTIMIZER_HIDE_VAR() to ensure the runtime
> architecture/software 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_BITOPS_KUNIT=y 'bitops'
>
> Sample KUnit output:
>
> [PASSED] test_hweight8
> [PASSED] test_hweight16
> [PASSED] test_hweight32
> [PASSED] test_hweight64
> [PASSED] test_hweight_long
> [PASSED] test_hweight_const
> ===================== [PASSED] bitops ======================
> Testing complete. Ran 40 tests: passed: 40
>
> Also verified W=1 compilation of lib/tests/bitops_kunit.o with no
> warnings.
>
> Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
Added in bitmap-for-next. Thanks!
> ---
> 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.
> ---
> lib/tests/bitops_kunit.c | 219 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 219 insertions(+)
>
> diff --git a/lib/tests/bitops_kunit.c b/lib/tests/bitops_kunit.c
> index 7fd9d697f131..d2d906c4d240 100644
> --- a/lib/tests/bitops_kunit.c
> +++ b/lib/tests/bitops_kunit.c
> @@ -179,6 +179,219 @@ static void test_get_count_order_long(struct kunit *test)
> }
> #endif
>
> +static unsigned int reference_hweight64(u64 val)
> +{
> + unsigned int count = 0;
> +
> + while (val) {
> + count += val & 1;
> + val >>= 1;
> + }
> +
> + return count;
> +}
> +
> +static void test_hweight8(struct kunit *test)
> +{
> + unsigned int i;
> +
> + for (i = 0; i <= U8_MAX; i++) {
> + u8 val = i;
> +
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight8(val), reference_hweight64(i));
> + }
> +}
> +
> +static void test_hweight16(struct kunit *test)
> +{
> + static const struct {
> + u16 val;
> + unsigned int expected;
> + } cases[] = {
> + { 0x0000, 0 },
> + { 0x0001, 1 },
> + { 0x8000, 1 },
> + { 0x00ff, 8 },
> + { 0xff00, 8 },
> + { 0x5555, 8 },
> + { 0xaaaa, 8 },
> + { 0x0f0f, 8 },
> + { 0x1234, 5 },
> + { U16_MAX, 16 },
> + };
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(cases); i++) {
> + u16 val = cases[i].val;
> +
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight16(val), cases[i].expected);
> + }
> +
> + for (i = 0; i < 16; i++) {
> + u16 single = BIT(i);
> + u16 inverted = ~single;
> +
> + OPTIMIZER_HIDE_VAR(single);
> + OPTIMIZER_HIDE_VAR(inverted);
> + KUNIT_EXPECT_EQ(test, hweight16(single), 1);
> + KUNIT_EXPECT_EQ(test, hweight16(inverted), 15);
> + }
> +}
> +
> +static void test_hweight32(struct kunit *test)
> +{
> + static const struct {
> + u32 val;
> + unsigned int expected;
> + } cases[] = {
> + { 0x00000000U, 0 },
> + { 0x00000001U, 1 },
> + { 0x80000000U, 1 },
> + { 0x0000ffffU, 16 },
> + { 0xffff0000U, 16 },
> + { 0x55555555U, 16 },
> + { 0xaaaaaaaaU, 16 },
> + { 0x12345678U, 13 },
> + { U32_MAX, 32 },
> + };
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(cases); i++) {
> + u32 val = cases[i].val;
> +
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight32(val), cases[i].expected);
> + }
> +
> + for (i = 0; i < 32; i++) {
> + u32 single = BIT(i);
> + u32 inverted = ~single;
> +
> + OPTIMIZER_HIDE_VAR(single);
> + OPTIMIZER_HIDE_VAR(inverted);
> + KUNIT_EXPECT_EQ(test, hweight32(single), 1);
> + KUNIT_EXPECT_EQ(test, hweight32(inverted), 31);
> + }
> +}
> +
> +static void test_hweight64(struct kunit *test)
> +{
> + static const struct {
> + u64 val;
> + unsigned int expected;
> + } cases[] = {
> + { 0x0000000000000000ULL, 0 },
> + { 0x0000000000000001ULL, 1 },
> + { 0x0000000080000000ULL, 1 },
> + { 0x0000000100000000ULL, 1 },
> + { 0x8000000000000000ULL, 1 },
> + { 0x00000000ffffffffULL, 32 },
> + { 0xffffffff00000000ULL, 32 },
> + { 0x5555555555555555ULL, 32 },
> + { 0xaaaaaaaaaaaaaaaaULL, 32 },
> + { 0x0123456789abcdefULL, 32 },
> + { U64_MAX, 64 },
> + };
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(cases); i++) {
> + u64 val = cases[i].val;
> +
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight64(val), cases[i].expected);
> + }
> +
> + for (i = 0; i < 64; i++) {
> + u64 single = BIT_ULL(i);
> + u64 inverted = ~single;
> +
> + OPTIMIZER_HIDE_VAR(single);
> + OPTIMIZER_HIDE_VAR(inverted);
> + KUNIT_EXPECT_EQ(test, hweight64(single), 1);
> + KUNIT_EXPECT_EQ(test, hweight64(inverted), 63);
> + }
> +}
> +
> +static void test_hweight_long(struct kunit *test)
> +{
> + const unsigned long alt_low = ~0UL / 3; /* 0x5555... */
> + const unsigned long alt_high = alt_low << 1; /* 0xaaaa... */
> + const unsigned long half_mask = GENMASK(BITS_PER_LONG / 2 - 1, 0);
> + unsigned long val;
> + unsigned int i;
> +
> + val = 0UL;
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight_long(val), 0UL);
> +
> + val = ~0UL;
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight_long(val), (unsigned long)BITS_PER_LONG);
> +
> + val = alt_low;
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight_long(val), (unsigned long)(BITS_PER_LONG / 2));
> +
> + val = alt_high;
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight_long(val), (unsigned long)(BITS_PER_LONG / 2));
> +
> + val = half_mask;
> + OPTIMIZER_HIDE_VAR(val);
> + KUNIT_EXPECT_EQ(test, hweight_long(val), (unsigned long)(BITS_PER_LONG / 2));
> +
> + for (i = 0; i < BITS_PER_LONG; i++) {
> + unsigned long single = BIT(i);
> + unsigned long inverted = ~single;
> +
> + OPTIMIZER_HIDE_VAR(single);
> + OPTIMIZER_HIDE_VAR(inverted);
> + KUNIT_EXPECT_EQ(test, hweight_long(single), 1UL);
> + KUNIT_EXPECT_EQ(test, hweight_long(inverted),
> + (unsigned long)(BITS_PER_LONG - 1));
> + }
> +}
> +
> +static void test_hweight_const(struct kunit *test)
> +{
> + static_assert(HWEIGHT8(0x00) == 0);
> + static_assert(HWEIGHT8(0xa5) == 4);
> + static_assert(HWEIGHT8(0xff) == 8);
> + static_assert(HWEIGHT16(0x0000) == 0);
> + static_assert(HWEIGHT16(0x5a5a) == 8);
> + static_assert(HWEIGHT16(0xffff) == 16);
> + static_assert(HWEIGHT32(0x00000000U) == 0);
> + static_assert(HWEIGHT32(0x12345678U) == 13);
> + static_assert(HWEIGHT32(0xffffffffU) == 32);
> + static_assert(HWEIGHT64(0x0000000000000000ULL) == 0);
> + static_assert(HWEIGHT64(0x0123456789abcdefULL) == 32);
> + static_assert(HWEIGHT64(0xffffffffffffffffULL) == 64);
> + static_assert(HWEIGHT(0x0123456789abcdefULL) == 32);
> +
> + KUNIT_EXPECT_EQ(test, hweight8(0x00), 0U);
> + KUNIT_EXPECT_EQ(test, hweight8(0xa5), 4U);
> + KUNIT_EXPECT_EQ(test, hweight8(0xff), 8U);
> + KUNIT_EXPECT_EQ(test, HWEIGHT8(0xa5), 4U);
> +
> + KUNIT_EXPECT_EQ(test, hweight16(0x0000), 0U);
> + KUNIT_EXPECT_EQ(test, hweight16(0x5a5a), 8U);
> + KUNIT_EXPECT_EQ(test, hweight16(0xffff), 16U);
> + KUNIT_EXPECT_EQ(test, HWEIGHT16(0x5a5a), 8U);
> +
> + KUNIT_EXPECT_EQ(test, hweight32(0x00000000U), 0U);
> + KUNIT_EXPECT_EQ(test, hweight32(0x12345678U), 13U);
> + KUNIT_EXPECT_EQ(test, hweight32(0xffffffffU), 32U);
> + KUNIT_EXPECT_EQ(test, HWEIGHT32(0x12345678U), 13U);
> +
> + KUNIT_EXPECT_EQ(test, hweight64(0x0000000000000000ULL), 0UL);
> + KUNIT_EXPECT_EQ(test, hweight64(0x0123456789abcdefULL), 32UL);
> + KUNIT_EXPECT_EQ(test, hweight64(0xffffffffffffffffULL), 64UL);
> + KUNIT_EXPECT_EQ(test, HWEIGHT64(0x0123456789abcdefULL), 32U);
> + KUNIT_EXPECT_EQ(test, HWEIGHT(0x0123456789abcdefULL), 32U);
> +}
> +
> static struct kunit_case bitops_test_cases[] = {
> KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
> KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
> @@ -188,6 +401,12 @@ static struct kunit_case bitops_test_cases[] = {
> #ifdef CONFIG_64BIT
> KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
> #endif
> + KUNIT_CASE(test_hweight8),
> + KUNIT_CASE(test_hweight16),
> + KUNIT_CASE(test_hweight32),
> + KUNIT_CASE(test_hweight64),
> + KUNIT_CASE(test_hweight_long),
> + KUNIT_CASE(test_hweight_const),
> {},
> };
>
>
> ---
> base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
> change-id: 20261202-bitops-hweight-b04ab21f2122
>
> Best regards,
> --
> Masaharu Noguchi <nogunix@gmail.com>
prev parent reply other threads:[~2026-12-02 20:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 10:24 [PATCH] lib/tests: extend KUnit test for bitops with hweight APIs Masaharu Noguchi
2026-12-02 20:10 ` Yury Norov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axB7IWY2TIKivJOF@yury \
--to=ynorov@nvidia.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=nogunix@gmail.com \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.