From e07a2a0f18398a9d55b8b2580ccfd5bc1f18725b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sun, 2 May 2021 13:16:17 -0400 Subject: [PATCH] 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 Reviewed-by: Mike Blumenkrants Part-of: --- src/util/bitset.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util/bitset.h b/src/util/bitset.h index b9e968293b1..4807709edb3 100644 --- a/src/util/bitset.h +++ b/src/util/bitset.h @@ -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