forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clz_test.go
48 lines (44 loc) · 873 Bytes
/
clz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package roaring
import (
"github.com/stretchr/testify/assert"
"testing"
)
func numberOfLeadingZeros(i uint64) int {
if i == 0 {
return 64
}
n := 1
x := uint32(i >> 32)
if x == 0 {
n += 32
x = uint32(i)
}
if (x >> 16) == 0 {
n += 16
x <<= 16
}
if (x >> 24) == 0 {
n += 8
x <<= 8
}
if x>>28 == 0 {
n += 4
x <<= 4
}
if x>>30 == 0 {
n += 2
x <<= 2
}
n -= int(x >> 31)
return n
}
func TestCountLeadingZeros072(t *testing.T) {
assert.Equal(t, 64, numberOfLeadingZeros(0))
assert.Equal(t, 60, numberOfLeadingZeros(8))
assert.Equal(t, 64-17-1, numberOfLeadingZeros(1<<17))
assert.Equal(t, 0, numberOfLeadingZeros(0xFFFFFFFFFFFFFFFF))
assert.Equal(t, 64, countLeadingZeros(0))
assert.Equal(t, 60, countLeadingZeros(8))
assert.Equal(t, 64-17-1, countLeadingZeros(1<<17))
assert.Equal(t, 0, countLeadingZeros(0xFFFFFFFFFFFFFFFF))
}