├── clz_asm.go ├── ctz_asm.go ├── popcnt_asm.go ├── ctz_amd64.s ├── clz_amd64.s ├── popcnt_amd64.s ├── popcnt.go ├── clz.go ├── ctz.go ├── bits_test.go └── LICENSE /clz_asm.go: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine 2 | 3 | package bits 4 | 5 | // Clz counts leading zeroes 6 | func Clz(x uint64) uint64 7 | -------------------------------------------------------------------------------- /ctz_asm.go: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine 2 | 3 | package bits 4 | 5 | // Ctz counts trailing zeroes 6 | func Ctz(x uint64) uint64 7 | -------------------------------------------------------------------------------- /popcnt_asm.go: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine,!popcntgo 2 | 3 | package bits 4 | 5 | // Popcnt counts the number of bits set 6 | func Popcnt(x uint64) uint64 7 | -------------------------------------------------------------------------------- /ctz_amd64.s: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine 2 | 3 | // func Ctz(x uint64) uint64 4 | TEXT ·Ctz(SB),4,$0-16 5 | BSFQ x+0(FP), AX 6 | JZ zero 7 | MOVQ AX, ret+8(FP) 8 | RET 9 | zero: 10 | MOVQ $64, ret+8(FP) 11 | RET 12 | -------------------------------------------------------------------------------- /clz_amd64.s: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine 2 | 3 | // func Clz(x uint64) uint64 4 | TEXT ·Clz(SB),4,$0-16 5 | BSRQ x+0(FP), AX 6 | JZ zero 7 | SUBQ $63, AX 8 | NEGQ AX 9 | MOVQ AX, ret+8(FP) 10 | RET 11 | zero: 12 | MOVQ $64, ret+8(FP) 13 | RET 14 | -------------------------------------------------------------------------------- /popcnt_amd64.s: -------------------------------------------------------------------------------- 1 | // +build amd64,!appengine,!popcntgo 2 | 3 | #define POPCNTQ_DX_DX BYTE $0xf3; BYTE $0x48; BYTE $0x0f; BYTE $0xb8; BYTE $0xd2 4 | 5 | // func Popcnt(x uint64) uint64 6 | 7 | TEXT ·Popcnt(SB),4,$0-16 8 | MOVQ x+0(FP), DX 9 | POPCNTQ_DX_DX 10 | MOVQ DX, ret+8(FP) 11 | RET 12 | -------------------------------------------------------------------------------- /popcnt.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 appengine popcntgo 2 | 3 | package bits 4 | 5 | // Popcnt counts the number of bits set 6 | func Popcnt(x uint64) uint64 { 7 | // bit population count, see 8 | // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 9 | x -= (x >> 1) & 0x5555555555555555 10 | x = (x>>2)&0x3333333333333333 + x&0x3333333333333333 11 | x += x >> 4 12 | x &= 0x0f0f0f0f0f0f0f0f 13 | x *= 0x0101010101010101 14 | return x >> 56 15 | } 16 | -------------------------------------------------------------------------------- /clz.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 appengine 2 | 3 | package bits 4 | 5 | // Clz counts leading zeroes 6 | func Clz(x uint64) uint64 { 7 | var n uint64 8 | 9 | n = 1 10 | 11 | if (x >> 32) == 0 { 12 | n = n + 32 13 | x = x << 32 14 | } 15 | if (x >> (32 + 16)) == 0 { 16 | n = n + 16 17 | x = x << 16 18 | } 19 | 20 | if (x >> (32 + 16 + 8)) == 0 { 21 | n = n + 8 22 | x = x << 8 23 | } 24 | 25 | if (x >> (32 + 16 + 8 + 4)) == 0 { 26 | n = n + 4 27 | x = x << 4 28 | } 29 | 30 | if (x >> (32 + 16 + 8 + 4 + 2)) == 0 { 31 | n = n + 2 32 | x = x << 2 33 | } 34 | 35 | n = n - (x >> 63) 36 | return uint64(n) 37 | } 38 | -------------------------------------------------------------------------------- /ctz.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 appengine 2 | 3 | package bits 4 | 5 | // Ctz counts trailing zeroes 6 | func Ctz(x uint64) uint64 { 7 | 8 | if x == 0 { 9 | return 64 10 | } 11 | 12 | var n uint64 13 | 14 | if (x & 0x00000000FFFFFFFF) == 0 { 15 | n = n + 32 16 | x = x >> 32 17 | } 18 | if (x & 0x000000000000FFFF) == 0 { 19 | n = n + 16 20 | x = x >> 16 21 | } 22 | if (x & 0x00000000000000FF) == 0 { 23 | n = n + 8 24 | x = x >> 8 25 | } 26 | if (x & 0x000000000000000F) == 0 { 27 | n = n + 4 28 | x = x >> 4 29 | } 30 | if (x & 0x0000000000000003) == 0 { 31 | n = n + 2 32 | x = x >> 2 33 | } 34 | if (x & 0x0000000000000001) == 0 { 35 | n = n + 1 36 | } 37 | 38 | return n 39 | } 40 | -------------------------------------------------------------------------------- /bits_test.go: -------------------------------------------------------------------------------- 1 | package bits 2 | 3 | import ( 4 | "testing" 5 | "testing/quick" 6 | ) 7 | 8 | func testQuick(t *testing.T, which string, ffast, fslow func(x uint64) uint64) { 9 | f := func(x uint64) bool { 10 | return ffast(x) == fslow(x) 11 | } 12 | if err := quick.Check(f, nil); err != nil { 13 | t.Errorf("fast%v != slow%v: %v: ", which, which, err) 14 | } 15 | } 16 | 17 | func ctzSlow(x uint64) uint64 { 18 | var n uint64 19 | for x&1 == 0 { 20 | n++ 21 | x >>= 1 22 | } 23 | return n 24 | } 25 | 26 | func TestQuickCtz(t *testing.T) { testQuick(t, "ctz", Ctz, ctzSlow) } 27 | 28 | func clzSlow(x uint64) uint64 { 29 | var n uint64 30 | for x&0x8000000000000000 == 0 { 31 | n++ 32 | x <<= 1 33 | } 34 | return n 35 | } 36 | 37 | func TestQuickClz(t *testing.T) { testQuick(t, "clz", Clz, clzSlow) } 38 | 39 | func popcntSlow(x uint64) uint64 { 40 | var n uint64 41 | for x != 0 { 42 | if x&1 == 1 { 43 | n++ 44 | } 45 | x >>= 1 46 | } 47 | return n 48 | } 49 | 50 | func TestQuickPopcnt(t *testing.T) { testQuick(t, "popcnt", Popcnt, popcntSlow) } 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Damian Gryski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------