util/bitset: Add BITSET_COUNT helper

Expressible as a prefix sum but that's a bit unnatural, so add a
convenience helper.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Mike Blumenkrants <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10581>
This commit is contained in:
Alyssa Rosenzweig 2021-05-02 13:16:17 -04:00 committed by Marge Bot
parent 231651fd89
commit e07a2a0f18
1 changed files with 14 additions and 0 deletions

View File

@ -96,9 +96,23 @@ __bitset_prefix_sum(const BITSET_WORD *x, unsigned b, unsigned n)
return prefix;
}
/* Count set bits in the bitset (compute the size/cardinality of the bitset).
* This is a special case of prefix sum, but this convenience method is more
* natural when applicable.
*/
static inline unsigned
__bitset_count(const BITSET_WORD *x, unsigned n)
{
return __bitset_prefix_sum(x, ~0, n);
}
#define BITSET_PREFIX_SUM(x, b) \
__bitset_prefix_sum(x, b, ARRAY_SIZE(x))
#define BITSET_COUNT(x) \
__bitset_count(x, ARRAY_SIZE(x))
/* Get first bit set in a bitset.
*/
static inline int