[PATCH] xen/bitmap: Deduplicate __bitmap_weight() implementations

Andrew Cooper posted 1 patch 2 months, 4 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20240202153816.772594-1-andrew.cooper3@citrix.com
xen/common/bitmap.c      | 21 +++------------------
xen/include/xen/bitops.h |  2 +-
2 files changed, 4 insertions(+), 19 deletions(-)
[PATCH] xen/bitmap: Deduplicate __bitmap_weight() implementations
Posted by Andrew Cooper 2 months, 4 weeks ago
We have two copies of __bitmap_weight() that differ by whether they make
hweight32() or hweight64() calls, yet we already have hweight_long() which is
the form that __bitmap_weight() wants.

Fix hweight_long() to return unsigned int like all the other hweight helpers,
and fix __bitmap_weight() to used unsigned integers.

No functional change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: George Dunlap <George.Dunlap@citrix.com>
CC: Jan Beulich <JBeulich@suse.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Wei Liu <wl@xen.org>
CC: Julien Grall <julien@xen.org>
---
 xen/common/bitmap.c      | 21 +++------------------
 xen/include/xen/bitops.h |  2 +-
 2 files changed, 4 insertions(+), 19 deletions(-)

diff --git a/xen/common/bitmap.c b/xen/common/bitmap.c
index c57b35f0042c..9d9ff09f3dde 100644
--- a/xen/common/bitmap.c
+++ b/xen/common/bitmap.c
@@ -186,33 +186,18 @@ int __bitmap_subset(const unsigned long *bitmap1,
 }
 EXPORT_SYMBOL(__bitmap_subset);
 
-#if BITS_PER_LONG == 32
 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
 {
-	int k, w = 0, lim = bits/BITS_PER_LONG;
+	unsigned int k, w = 0, lim = bits / BITS_PER_LONG;
 
 	for (k = 0; k < lim; k++)
-		w += hweight32(bitmap[k]);
+		w += hweight_long(bitmap[k]);
 
 	if (bits % BITS_PER_LONG)
-		w += hweight32(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
+		w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
 
 	return w;
 }
-#else
-unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
-{
-	int k, w = 0, lim = bits/BITS_PER_LONG;
-
-	for (k = 0; k < lim; k++)
-		w += hweight64(bitmap[k]);
-
-	if (bits % BITS_PER_LONG)
-		w += hweight64(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
-
-	return w;
-}
-#endif
 EXPORT_SYMBOL(__bitmap_weight);
 
 void __bitmap_set(unsigned long *map, unsigned int start, int len)
diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h
index a88d45475c40..2cb7892bcca0 100644
--- a/xen/include/xen/bitops.h
+++ b/xen/include/xen/bitops.h
@@ -199,7 +199,7 @@ static inline unsigned int generic_hweight64(uint64_t w)
     return (w + (w >> 32)) & 0xFF;
 }
 
-static inline unsigned long hweight_long(unsigned long w)
+static inline unsigned int hweight_long(unsigned long w)
 {
     return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
 }
-- 
2.30.2
Re: [PATCH] xen/bitmap: Deduplicate __bitmap_weight() implementations
Posted by Jan Beulich 2 months, 3 weeks ago
On 02.02.2024 16:38, Andrew Cooper wrote:
> We have two copies of __bitmap_weight() that differ by whether they make
> hweight32() or hweight64() calls, yet we already have hweight_long() which is
> the form that __bitmap_weight() wants.
> 
> Fix hweight_long() to return unsigned int like all the other hweight helpers,
> and fix __bitmap_weight() to used unsigned integers.
> 
> No functional change.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>