>From 7a2cffb8802e955ede85fb907e94a7172b4a0fa6 Mon Sep 17 00:00:00 2001 From: Li Zhijian Date: Fri, 13 Nov 2015 15:51:48 +0800 Subject: [PATCH 1/2] bitmap: add bitmap_weight() api count the number of bit set in bitmap Signed-off-by: Li Zhijian --- include/qemu/bitmap.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h index 86dd9cd..6e48429 100644 --- a/include/qemu/bitmap.h +++ b/include/qemu/bitmap.h @@ -43,6 +43,7 @@ * bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area + * bitmap_weight(src, nbits) Hamming Weight: number set bits */ /* @@ -227,6 +228,23 @@ static inline int bitmap_intersects(const unsigned long *src1, } } +static inline int bitmap_weight(const unsigned long *src, long nbits) +{ + int i, count = 0, nlong = nbits / BITS_PER_LONG; + + if (small_nbits(nbits)) { + return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); + } + for (i = 0; i < nlong; i++) { + count += hweight_long(src[i]); + } + if (nbits % BITS_PER_LONG) { + count += hweight_long(src[i] & BITMAP_LAST_WORD_MASK(nbits)); + } + + return count; +} + void bitmap_set(unsigned long *map, long i, long len); void bitmap_set_atomic(unsigned long *map, long i, long len); void bitmap_clear(unsigned long *map, long start, long nr); -- 2.1.4