1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // From Hacker's Delight, fig 5.2.
8 func popcountHD(x uint32) int {
9 x -= (x >> 1) & 0x55555555
10 x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
11 x = (x + (x >> 4)) & 0x0f0f0f0f
14 return int(x & 0x0000003f)
22 for x := i; x != 0; x >>= 1 {
31 func popcountTable(x word) int {
32 return int(a[byte(x>>(0*8))] +
42 // nlz returns the number of leading zeros of x.
43 // From Hacker's Delight, fig 5.11.
44 func nlz(x word) int {
54 // ntz returns the number of trailing zeros of x.
55 // From Hacker's Delight, fig 5.13.
56 func ntz(x word) int {
61 if bitsPerWord == 64 {
62 if (x & 0xffffffff) == 0 {
67 if (x & 0x0000ffff) == 0 {
71 if (x & 0x000000ff) == 0 {
75 if (x & 0x0000000f) == 0 {
79 if (x & 0x00000003) == 0 {