From: Ingo Molnar <mingo@kernel.org>
To: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
Cc: "Ingo Molnar" <mingo@redhat.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Tony Luck" <tony.luck@intel.com>,
"Xin Li (Intel)" <xin@zytor.com>,
"Chang S. Bae" <chang.seok.bae@intel.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] bitmap: add bitmap_weight_from()
Date: Mon, 15 Dec 2025 07:59:57 +0100 [thread overview]
Message-ID: <aT-x7f4EFFEgd1T4@gmail.com> (raw)
In-Reply-To: <20251214235437.244125-2-yury.norov@gmail.com>
* Yury Norov (NVIDIA) <yury.norov@gmail.com> wrote:
> The function calculates a Hamming weight of a bitmap starting from an
> arbitrary bit.
>
> Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> ---
> include/linux/bitmap.h | 25 +++++++++++++++++++++++++
> lib/bitmap.c | 28 ++++++++++++++++++++++++++++
> lib/test_bitmap.c | 29 +++++++++++++++++++++++++++++
> 3 files changed, 82 insertions(+)
>
> diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> index b0395e4ccf90..0f4789e1f7cb 100644
> --- a/include/linux/bitmap.h
> +++ b/include/linux/bitmap.h
> @@ -57,6 +57,7 @@ struct device;
> * bitmap_weight(src, nbits) Hamming Weight: number set bits
> * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap
> * bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap
> + * bitmap_weight_from(src, start, nbits) Hamming Weight starting from @start
> * bitmap_set(dst, pos, nbits) Set specified bit area
> * bitmap_clear(dst, pos, nbits) Clear specified bit area
> * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
> @@ -184,6 +185,8 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
> const unsigned long *bitmap2, unsigned int nbits);
> unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
> const unsigned long *bitmap2, unsigned int nbits);
> +unsigned long __bitmap_weight_from(const unsigned long *bitmap,
> + unsigned int start, unsigned int nbits);
> void __bitmap_set(unsigned long *map, unsigned int start, int len);
> void __bitmap_clear(unsigned long *map, unsigned int start, int len);
>
> @@ -479,6 +482,28 @@ unsigned long bitmap_weight_andnot(const unsigned long *src1,
> return __bitmap_weight_andnot(src1, src2, nbits);
> }
>
> +/**
> + * bitmap_weight_from - Hamming weight for a memory region
> + * @bitmap: The base address
> + * @start: The bitnumber to starts weighting
> + * @nbits: the bitmap size in bits
> + *
> + * Returns the number of set bits in the region, or > @nbits in case of error.
> + */
> +static __always_inline
> +unsigned long bitmap_weight_from(const unsigned long *bitmap,
> + unsigned int start, unsigned int nbits)
> +{
> + if (small_const_nbits(nbits)) {
> + if (unlikely(start >= nbits))
> + return nbits + 1;
> +
> + return hweight_long(*bitmap & GENMASK(nbits - 1, start));
> + }
> +
> + return __bitmap_weight_from(bitmap, start, nbits);
The 'nbits' name and description is actively misleading: it suggests
the number of bits searched, like bitmap_write() has nbits for
the number of bits written, but in reality it's the *end* index,
not the size of the area to search.
Note how it contrasts with how bitmap_write(..,start,nbits)
works.
So please rename it to something more suitable, like 'end', or so.
Thanks,
Ingo
next prev parent reply other threads:[~2025-12-15 7:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-14 23:54 [PATCH 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
2025-12-14 23:54 ` [PATCH 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
2025-12-15 6:59 ` Ingo Molnar [this message]
2025-12-14 23:54 ` [PATCH 2/2] x86/topology: use bitmap_weight_from() Yury Norov (NVIDIA)
-- strict thread matches above, loose matches on Subject: below --
2025-07-20 1:41 [PATCH 0/2] bitmap: introduce bitmap_weight_from() Yury Norov
2025-07-20 1:41 ` [PATCH 1/2] bitmap: add bitmap_weight_from() Yury Norov
2025-07-20 10:44 ` Thomas Gleixner
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=aT-x7f4EFFEgd1T4@gmail.com \
--to=mingo@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=bp@alien8.de \
--cc=chang.seok.bae@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=tony.luck@intel.com \
--cc=x86@kernel.org \
--cc=xin@zytor.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.