File: popcnt.go

package info (click to toggle)
golang-github-dgryski-go-bits 0.0~git20151205.0.86c69b3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88 kB
  • ctags: 26
  • sloc: asm: 24; makefile: 2
file content (15 lines) | stat: -rw-r--r-- 400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// +build !amd64 appengine popcntgo

package bits

// Popcnt counts the number of bits set
func Popcnt(x uint64) uint64 {
	// bit population count, see
	// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
	x -= (x >> 1) & 0x5555555555555555
	x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
	x += x >> 4
	x &= 0x0f0f0f0f0f0f0f0f
	x *= 0x0101010101010101
	return x >> 56
}