| // Copyright 2013 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // From Hacker's Delight, fig 5.2. |
| func popcountHD(x uint32) int { |
| x -= (x >> 1) & 0x55555555 |
| x = (x & 0x33333333) + ((x >> 2) & 0x33333333) |
| x = (x + (x >> 4)) & 0x0f0f0f0f |
| return int(x & 0x0000003f) |
| for x := i; x != 0; x >>= 1 { |
| func popcountTable(x word) int { |
| return int(a[byte(x>>(0*8))] + |
| // nlz returns the number of leading zeros of x. |
| // From Hacker's Delight, fig 5.11. |
| // ntz returns the number of trailing zeros of x. |
| // From Hacker's Delight, fig 5.13. |
| if (x & 0xffffffff) == 0 { |
| if (x & 0x0000ffff) == 0 { |
| if (x & 0x000000ff) == 0 { |
| if (x & 0x0000000f) == 0 { |
| if (x & 0x00000003) == 0 { |