Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions roaring64/bsi64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1859,19 +1859,36 @@ func (b *BSI) addPlanes(planes []*Bitmap) {
b.bA[n-1].Or(&b.bA[n-2])
}
}
carry := NewBitmap()
// carry is nil when there is no carry into plane i.
var carry *Bitmap
for i := 0; i < width; i++ {
x := &b.bA[i]
y := planes[len(planes)-1]
if i < len(planes) {
y = planes[i]
}
xy := Xor(x, y)
if carry == nil && y.IsEmpty() {
if i >= len(planes)-1 {
// y is the empty sign plane of a non-negative operand from
// here on, and there is no carry: no plane above i changes.
break
}
continue
}
x := &b.bA[i]
nextCarry := And(x, y)
nextCarry.Or(And(carry, xy))
x.Xor(y)
x.Xor(carry)
x.Xor(y) // x now holds x^y
if carry != nil {
// Xor may install carry's own containers into x when carry has a
// high key x does not. Mutating carry after that clears the sum
// bit, so take carry&(x^y) first and leave carry alone.
term := And(carry, x)
x.Xor(carry) // x now holds the sum bit x^y^carry
nextCarry.Or(term)
}
carry = nextCarry
if carry.IsEmpty() {
carry = nil
}
}
// Drop sign planes that only repeat the one below them.
if minLen < 2 {
Expand Down
108 changes: 108 additions & 0 deletions roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,54 @@ func BenchmarkBSIClone(b *testing.B) {
}
}

func benchmarkBSIAddSetup(n int, bits uint, signed bool, seed int64) *BSI {
r := rand.New(rand.NewSource(seed))
bsi := NewDefaultBSI()
for i := 0; i < n; i++ {
v := r.Int63n(1 << bits)
if signed {
v -= 1 << (bits - 1)
}
bsi.SetValue(uint64(i), v)
}
return bsi
}

func BenchmarkBSIAdd(b *testing.B) {
cases := []struct {
name string
bits, oBits uint
signed bool
increment bool
}{
{name: "same-width-signed", bits: 30, oBits: 30, signed: true},
{name: "same-width-unsigned", bits: 30, oBits: 30},
{name: "narrow-other", bits: 40, oBits: 8},
{name: "increment", bits: 30, increment: true},
}
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
base := benchmarkBSIAddSetup(100000, c.bits, c.signed, 1)
var other *BSI
if !c.increment {
other = benchmarkBSIAddSetup(100000, c.oBits, c.signed, 2)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
bsi := base.Clone()
b.StartTimer()
if c.increment {
bsi.IncrementAll()
} else {
bsi.Add(other)
}
}
})
}
}

func TestClone(t *testing.T) {
bsi := NewDefaultBSI()
// Setup values
Expand Down Expand Up @@ -689,6 +737,66 @@ func TestIncrementNegative(t *testing.T) {
}
}

// Column IDs in different roaring64 high halves make the carry bitmap hold
// a key that the value plane does not.
func TestAddAcrossHighKeys(t *testing.T) {
const hi = uint64(1) << 32

t.Run("increment", func(t *testing.T) {
bsi := NewDefaultBSI()
bsi.SetValue(0, 1)
bsi.SetValue(hi, 2)
bsi.IncrementAll()
got, ok := bsi.GetValue(0)
assert.True(t, ok)
assert.Equal(t, int64(2), got)
got, ok = bsi.GetValue(hi)
assert.True(t, ok)
assert.Equal(t, int64(3), got)
})

t.Run("add one", func(t *testing.T) {
bsi := NewDefaultBSI()
bsi.SetValue(0, 1)
bsi.SetValue(hi, 2)
one := NewDefaultBSI()
one.SetValue(0, 1)
one.SetValue(hi, 1)
bsi.Add(one)
got, ok := bsi.GetValue(0)
assert.True(t, ok)
assert.Equal(t, int64(2), got)
got, ok = bsi.GetValue(hi)
assert.True(t, ok)
assert.Equal(t, int64(3), got)
got, ok = one.GetValue(0)
assert.True(t, ok)
assert.Equal(t, int64(1), got)
got, ok = one.GetValue(hi)
assert.True(t, ok)
assert.Equal(t, int64(1), got)
})

t.Run("add minus one", func(t *testing.T) {
bsi := NewDefaultBSI()
bsi.SetValue(0, -1)
bsi.SetValue(hi, 4)
minus := NewDefaultBSI()
minus.SetValue(0, -1)
minus.SetValue(hi, -1)
bsi.Add(minus)
got, ok := bsi.GetValue(0)
assert.True(t, ok)
assert.Equal(t, int64(-2), got)
got, ok = bsi.GetValue(hi)
assert.True(t, ok)
assert.Equal(t, int64(3), got)
got, ok = minus.GetValue(0)
assert.True(t, ok)
assert.Equal(t, int64(-1), got)
})
}

func TestTransposeWithCounts(t *testing.T) {
bsi := setup()
bsi.SetValue(101, 50)
Expand Down
Loading