File: popcnt.go

package info (click to toggle)
golang-github-bits-and-blooms-bitset 1.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,361 bytes parent folder | download
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
}