diff options
| author | Alyssa Rosenzweig <a@rosenzweig.io> | 2021-05-02 13:16:17 -0400 |
|---|---|---|
| committer | Marge Bot <eric+marge@anholt.net> | 2021-05-02 20:38:28 +0000 |
| commit | e07a2a0f18398a9d55b8b2580ccfd5bc1f18725b (patch) | |
| tree | 0fdab23ca00bed4123ff88c9d3a11033d12eae9b /src/util/bitset.h | |
| parent | 231651fd89fb007610568b3ca76837253e7683ff (diff) | |
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>
Diffstat (limited to 'src/util/bitset.h')
| -rw-r--r-- | src/util/bitset.h | 14 |
1 files changed, 14 insertions, 0 deletions
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 |