diff options
Diffstat (limited to 'src/util/bitset.h')
| -rw-r--r-- | src/util/bitset.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util/bitset.h b/src/util/bitset.h index 29de65e839c..b9e968293b1 100644 --- a/src/util/bitset.h +++ b/src/util/bitset.h @@ -80,6 +80,25 @@ ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \ (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0)) +static inline unsigned +__bitset_prefix_sum(const BITSET_WORD *x, unsigned b, unsigned n) +{ + unsigned prefix = 0; + + for (unsigned i = 0; i < n; i++) { + if ((i + 1) * BITSET_WORDBITS <= b) { + prefix += util_bitcount(x[i]); + } else { + prefix += util_bitcount(x[i] & BITFIELD_MASK(b - i * BITSET_WORDBITS)); + break; + } + } + return prefix; +} + +#define BITSET_PREFIX_SUM(x, b) \ + __bitset_prefix_sum(x, b, ARRAY_SIZE(x)) + /* Get first bit set in a bitset. */ static inline int |