bitset: add range counting and parallel bit extract/deposit operations #184
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add three new operations to BitSet:
OnesBetween operates at the word level where possible, handling edge words with masks. It returns 0 for invalid ranges where from >= to.
Extract takes bits from positions where mask is set and packs them into consecutive positions starting at 0. For example, if mask has bits set at positions 1,4,5 then Extract will take bits at those positions from the source and pack them into positions 0,1,2.
Deposit is the inverse of Extract - it spreads bits from consecutive positions into positions specified by mask.
The parallel bit operations are implemented using byte-wise lookup tables rather than CPU-specific instructions like PEXT/PDEP, making them portable while maintaining good performance.
Added extensive testing:
These operations are useful for:
The implementation aims to be both correct and fast, operating at the word level where possible while properly handling edges.
Benchmarks