From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Yury Norov <yury.norov@gmail.com>, linux-kernel@vger.kernel.org
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: Re: [PATCH 02/12] bitmap: add bitmap_weight_from()
Date: Tue, 29 Aug 2023 09:22:52 +0200 [thread overview]
Message-ID: <945c1c2e-03ab-e531-bbba-6e69e2bfff4f@rasmusvillemoes.dk> (raw)
In-Reply-To: <20230828184353.5145-3-yury.norov@gmail.com>
On 28/08/2023 20.43, Yury Norov wrote:
> Add a _from flavor for bitmap_weight(). It's useful when traversing
> bitmaps in a loop to not count bits from the beginning every time.
>
> The test for bitmap_weight{,_from}() is added as well.
>
> Signed-off-by: Yury Norov <yury.norov@gmail.com>
> ---
> include/linux/bitmap.h | 14 ++++++++++++++
> lib/bitmap.c | 25 +++++++++++++++++++++++++
> lib/test_bitmap.c | 18 ++++++++++++++++++
> 3 files changed, 57 insertions(+)
>
> diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> index 692d0a1dbe92..6acbdd2abd0c 100644
> --- a/include/linux/bitmap.h
> +++ b/include/linux/bitmap.h
> @@ -168,6 +168,8 @@ bool __bitmap_subset(const unsigned long *bitmap1,
> unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
> unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
> const unsigned long *bitmap2, unsigned int nbits);
> +unsigned int __bitmap_weight_from(const unsigned long *bitmap,
> + unsigned int bits, unsigned int start);
> void __bitmap_set(unsigned long *map, unsigned int start, int len);
> void __bitmap_clear(unsigned long *map, unsigned int start, int len);
>
> @@ -446,6 +448,18 @@ unsigned long bitmap_weight_and(const unsigned long *src1,
> return __bitmap_weight_and(src1, src2, nbits);
> }
>
> +static __always_inline
> +unsigned int bitmap_weight_from(const unsigned long *src, unsigned int nbits, unsigned int start)
> +{
> + if (unlikely(start >= nbits))
> + return 0;
> +
> + if (small_const_nbits(nbits - start) && nbits / BITS_PER_LONG == start / BITS_PER_LONG)
> + return hweight_long(*src & GENMASK(nbits-1, start));
This must be buggy. If I pass compile-time constants nbits==96 and
start==64, the whole if() is true, and we call GENMASK with total
garbage arguments, and access src[0] and not src[1] as that start-value
would suggest.
Don't optimize for irrelevant cases. The expected use of this function
must be that start is some runtime thing. So just make the whole if()
just test whether nbits is small_const, and if so, I think the
hweight_long() line is actually right as-is (though I can never remember
GENMASK argument order).
>
> +#define BITMAP_WEIGHT_FROM(FETCH, bits, start) \
> +({ \
> + unsigned long __bits = (bits), val, idx, w = 0, __start = (start), mask;\
> + \
> + mask = BITMAP_FIRST_WORD_MASK(__start); \
> + idx = __start / BITS_PER_LONG; \
> + \
> + for (val = (FETCH) & mask; idx < __bits / BITS_PER_LONG; val = (FETCH)) {\
> + w += hweight_long(val); \
> + idx++; \
> + } \
> + \
> + if (__bits % BITS_PER_LONG) \
> + w += hweight_long((val) & BITMAP_LAST_WORD_MASK(__bits)); \
> + \
> + w; \
> +})
>
Why does the entire function body need to be an unholy statement expression?
Rasmus
next prev parent reply other threads:[~2023-08-29 7:24 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-28 18:43 [PATCH 00/12] bitmap: rework bitmap_{bit,}remap() Yury Norov
2023-08-28 18:43 ` [PATCH 01/12] bitmap: add find_nth_bit_from() Yury Norov
2023-08-28 18:43 ` [PATCH 02/12] bitmap: add bitmap_weight_from() Yury Norov
2023-08-29 7:22 ` Rasmus Villemoes [this message]
2023-08-28 18:43 ` [PATCH 03/12] bitmap: add test for bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 04/12] bitmap: add test for bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 05/12] bitmap: update comment for bitmap_{bit,}remap() Yury Norov
2023-08-28 18:43 ` [PATCH 06/12] bitmap: add small_cont_nbits() optimization for bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 07/12] bitmap: add small_const_nbits() optimization for bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 08/12] bitmap: optiimze bitmap_bitremap() Yury Norov
2023-08-28 18:43 ` [PATCH 09/12] bitmap: optimize bitmap_remap() when 'new' is empty map Yury Norov
2023-08-28 18:43 ` [PATCH 10/12] bitmap: separate handling of identity and remapping parts in bitmap_remap() Yury Norov
2023-08-28 18:43 ` [PATCH 11/12] bitmap: defer calculating weight of 'new' " Yury Norov
2023-08-28 18:43 ` [PATCH 12/12] bitmap: don't count bits from the beginning " Yury Norov
2023-08-29 7:33 ` [PATCH 00/12] bitmap: rework bitmap_{bit,}remap() Rasmus Villemoes
2023-08-29 13:38 ` Andy Shevchenko
2023-08-29 13:50 ` Yury Norov
2023-08-29 14:56 ` Andy Shevchenko
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=945c1c2e-03ab-e531-bbba-6e69e2bfff4f@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=andriy.shevchenko@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®