mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-02-01 23:11:02 +00:00
bitmap: introduce bitmap_count_one()
Count how many bits set in the bitmap. Backports commit fc7deeea26af3d08f45bad85b8bd3fc3d790a090 from qemu
This commit is contained in:
parent
296c30aaca
commit
3d5fa79305
|
@ -48,6 +48,8 @@
|
|||
#define DECLARE_BITMAP(name,bits) \
|
||||
unsigned long name[BITS_TO_LONGS(bits)]
|
||||
|
||||
long slow_bitmap_count_one(const unsigned long *bitmap, long nbits);
|
||||
|
||||
static inline unsigned long *bitmap_try_new(long nbits)
|
||||
{
|
||||
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||
|
@ -63,6 +65,15 @@ static inline unsigned long *bitmap_new(long nbits)
|
|||
return ptr;
|
||||
}
|
||||
|
||||
static inline long bitmap_count_one(const unsigned long *bitmap, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits));
|
||||
} else {
|
||||
return slow_bitmap_count_one(bitmap, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
|
@ -148,3 +148,18 @@ void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src,
|
|||
nr -= BITS_PER_LONG;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue