From fc7deeea26af3d08f45bad85b8bd3fc3d790a090 Mon Sep 17 00:00:00 2001 From: Peter Xu Date: Wed, 30 Aug 2017 16:31:59 +0800 Subject: bitmap: introduce bitmap_count_one() Count how many bits set in the bitmap. Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Peter Xu Reviewed-by: Juan Quintela Signed-off-by: Juan Quintela --- util/bitmap.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'util/bitmap.c') diff --git a/util/bitmap.c b/util/bitmap.c index efced9a..90a42ff 100644 --- a/util/bitmap.c +++ b/util/bitmap.c @@ -355,3 +355,18 @@ int slow_bitmap_intersects(const unsigned long *bitmap1, } return 0; } + +long slow_bitmap_count_one(const unsigned long *bitmap, long nbits) +{ + long k, lim = nbits / BITS_PER_LONG, result = 0; + + for (k = 0; k < lim; k++) { + result += ctpopl(bitmap[k]); + } + + if (nbits % BITS_PER_LONG) { + result += ctpopl(bitmap[k] & BITMAP_LAST_WORD_MASK(nbits)); + } + + return result; +} -- cgit v1.1