-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathpopcnt.go
More file actions
52 lines (46 loc) · 1.33 KB
/
popcnt.go
File metadata and controls
52 lines (46 loc) · 1.33 KB
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
49
50
51
52
package bitset
import "math/bits"
func popcntSlice(s []uint64) (cnt uint64) {
for _, x := range s {
cnt += uint64(bits.OnesCount64(x))
}
return
}
func popcntMaskSlice(s, m []uint64) (cnt uint64) {
// The next line is to help the bounds checker, it matters!
_ = m[len(s)-1] // BCE
for i := range s {
cnt += uint64(bits.OnesCount64(s[i] &^ m[i]))
}
return
}
// popcntAndSlice computes the population count of the AND of two slices.
// It assumes that len(m) >= len(s) > 0.
func popcntAndSlice(s, m []uint64) (cnt uint64) {
// The next line is to help the bounds checker, it matters!
_ = m[len(s)-1] // BCE
for i := range s {
cnt += uint64(bits.OnesCount64(s[i] & m[i]))
}
return
}
// popcntOrSlice computes the population count of the OR of two slices.
// It assumes that len(m) >= len(s) > 0.
func popcntOrSlice(s, m []uint64) (cnt uint64) {
// The next line is to help the bounds checker, it matters!
_ = m[len(s)-1] // BCE
for i := range s {
cnt += uint64(bits.OnesCount64(s[i] | m[i]))
}
return
}
// popcntXorSlice computes the population count of the XOR of two slices.
// It assumes that len(m) >= len(s) > 0.
func popcntXorSlice(s, m []uint64) (cnt uint64) {
// The next line is to help the bounds checker, it matters!
_ = m[len(s)-1] // BCE
for i := range s {
cnt += uint64(bits.OnesCount64(s[i] ^ m[i]))
}
return
}