Skip to content

Commit

Permalink
uses math/bits for trailing zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Fitzgerald committed Aug 28, 2017
1 parent 332dbec commit e50f633
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 0 additions & 11 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,6 @@ func (b *BitSet) Count() uint {
return 0
}

var deBruijn = [...]byte{
0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
}

func trailingZeroes64(v uint64) uint {
return uint(deBruijn[((v&-v)*0x03f79d71b4ca8b09)>>58])
}

// Equal tests the equvalence of two BitSets.
// False if they are of different sizes, otherwise true
// only if all the same bits are set
Expand Down
14 changes: 14 additions & 0 deletions trailing_zeros_18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !go1.9

package bitset

var deBruijn = [...]byte{
0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
}

func trailingZeroes64(v uint64) uint {
return uint(deBruijn[((v&-v)*0x03f79d71b4ca8b09)>>58])
}
9 changes: 9 additions & 0 deletions trailing_zeros_19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build go1.9

package bitset

import "math/bits"

func trailingZeroes64(v uint64) uint {
return uint(bits.TrailingZeros64(v))
}

0 comments on commit e50f633

Please sign in to comment.