Skip to content

roaring64: fix BSI Add and Increment for negative values - #609

Merged
lemire merged 1 commit into
RoaringBitmap:masterfrom
amalsaidov:bsi64-signed-add
Dec 4, 2026
Merged

lemire merged 1 commit into
RoaringBitmap:masterfrom
amalsaidov:bsi64-signed-add

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

Description

roaring64.BSI.Add and Increment give wrong results when any value involved is negative:

a := roaring64.NewDefaultBSI()
a.SetValue(1, 5)
b := roaring64.NewDefaultBSI()
b.SetValue(1, -3)
a.Add(b)
a.GetValue(1) // 10, expected 2

More examples: 1 + -1 = 4, -1 + -1 = 6, -3 + 5 = -6, and IncrementAll turns -1 into 4. The 32-bit BitSliceIndexing.BSI gets all of these right. Non-negative values were already correct.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Changes Made

What was changed?

  • roaring64.BSI.Add and Increment now share an addPlanes helper that adds in two's complement. The recursive addDigit is removed.

Why was it changed?

  • addDigit added the bit planes as unsigned numbers. In roaring64 the last plane is the sign plane, and its position depends on the BSI's width. Two things went wrong:
    • The planes of other were not sign-extended to the width of b.
    • A carry out of the top plane was appended as a new plane, which then became the sign plane.

How was it changed?

  • Both operands are sign-extended to one plane wider than the wider of the two, so the sum cannot overflow. The planes are then added with a ripple carry, and the carry out of the sign plane is dropped.
  • Afterwards, top planes that only repeat the plane below them are trimmed, but never below the BSI's original width. This means repeated Add calls don't keep widening the index, and BSIs created with NewBSI(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), TestAddToItself and TestIncrementNegative. TestAddNegativeValues and TestAddMixedSigns fail on master. I also compared Add against plain int64 addition on random data, using the 32-bit BSI as a reference.

go test ./... and go vet pass, and gofmt is 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:

before  BenchmarkZZAdd-8   328   3666129 ns/op   9020949 B/op   8610 allocs/op
after   BenchmarkZZAdd-8  1426    897227 ns/op   3250637 B/op   2242 allocs/op

Breaking Changes

None.

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.
@lemire
lemire merged commit a38a939 into RoaringBitmap:master Dec 4, 2026
8 checks passed
@amalsaidov

Copy link
Copy Markdown
Contributor Author

Thanks for merging 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants