Repository navigation
roaring64: fix BSI Add and Increment for negative values - #609
Merged
Merged
Conversation
Add summed the bit planes as unsigned numbers. The value from other was not sign-extended to the width of b, and a carry out of the top plane became a new sign plane, so any negative operand gave a wrong result (5 + -3 = 10, -1 + -1 = 6, and Increment turned -1 into 4). Add the planes in two's complement instead: sign-extend both operands to one plane wider than the wider of the two, drop the final carry, and trim sign planes that only repeat the one below (never below the original width). Adding a BSI to itself now works too.
This was referenced Dec 4, 2026
Contributor
Author
|
Thanks for merging 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
roaring64.BSI.AddandIncrementgive wrong results when any value involved is negative:More examples:
1 + -1 = 4,-1 + -1 = 6,-3 + 5 = -6, andIncrementAllturns-1into4. The 32-bitBitSliceIndexing.BSIgets all of these right. Non-negative values were already correct.Type of Change
Changes Made
What was changed?
roaring64.BSI.AddandIncrementnow share anaddPlaneshelper that adds in two's complement. The recursiveaddDigitis removed.Why was it changed?
addDigitadded the bit planes as unsigned numbers. Inroaring64the last plane is the sign plane, and its position depends on the BSI's width. Two things went wrong:otherwere not sign-extended to the width ofb.How was it changed?
Addcalls don't keep widening the index, and BSIs created withNewBSI(max, min)keep their width.b.Add(b)used to modify its own input while reading it. It now clones first.Testing
New tests:
TestAddNegativeValues,TestAddMixedSigns(random mixed-sign values, including columns that only one side has),TestAddToItselfandTestIncrementNegative.TestAddNegativeValuesandTestAddMixedSignsfail on master. I also comparedAddagainst plainint64addition on random data, using the 32-bit BSI as a reference.go test ./...andgo vetpass, andgofmtis clean. I didn't run the smat fuzzer, because it doesn't exercise the BSI code.Performance Impact
The new adder does a fixed number of bitmap operations per plane and has no recursive carries. A quick local benchmark (not included in the PR) cloned a 100k-column BSI with 30-bit values and added another:
Breaking Changes
None.