Skip to content

Commit 0dd24ab

Browse files
committed
bitmap: use bits.LeadingZeros64 intrinsic in maximum()
bitmapContainer.maximum() relied on a hand-rolled clz() to find the highest set bit in the most significant non-empty word. That helper reimplemented, in portable shift/compare arithmetic, what the standard library already exposes as math/bits.LeadingZeros64, which the Go compiler lowers to a single hardware instruction (LZCNT on amd64, CLZ on arm64). The package already wraps it as countLeadingZeros (clz.go), so maximum() now calls that and the redundant clz() helper is deleted.
1 parent 9e2afa4 commit 0dd24ab

1 file changed

Lines changed: 1 addition & 29 deletions

File tree

bitmapcontainer.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,39 +69,11 @@ func (bc *bitmapContainer) safeMinimum() (uint16, error) {
6969
return val, nil
7070
}
7171

72-
// i should be non-zero
73-
func clz(i uint64) int {
74-
n := 1
75-
x := uint32(i >> 32)
76-
if x == 0 {
77-
n += 32
78-
x = uint32(i)
79-
}
80-
if x>>16 == 0 {
81-
n += 16
82-
x = x << 16
83-
}
84-
if x>>24 == 0 {
85-
n += 8
86-
x = x << 8
87-
}
88-
if x>>28 == 0 {
89-
n += 4
90-
x = x << 4
91-
}
92-
if x>>30 == 0 {
93-
n += 2
94-
x = x << 2
95-
}
96-
return n - int(x>>31)
97-
}
98-
9972
func (bc *bitmapContainer) maximum() uint16 {
10073
for i := len(bc.bitmap); i > 0; i-- {
10174
w := bc.bitmap[i-1]
10275
if w != 0 {
103-
r := clz(w)
104-
return uint16((i-1)*64 + 63 - r)
76+
return uint16((i-1)*64 + 63 - countLeadingZeros(w))
10577
}
10678
}
10779
return uint16(0)

0 commit comments

Comments
 (0)