1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00

math/bits: added package for bit-level counting and manipulation

Initial platform-independent implementation.

For #18616.

Change-Id: I4585c55b963101af9059c06c1b8a866cb384754c
Reviewed-on: https://go-review.googlesource.com/36315
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Robert Griesemer 2017-02-02 16:59:34 -08:00
parent 1693e7b6f2
commit 661e2179e5
3 changed files with 727 additions and 0 deletions

77
src/math/bits/bits.go Normal file
View File

@ -0,0 +1,77 @@
// Copyright 2017 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.
// Package bits implements bit counting and manipulation
// functions for the predeclared unsigned integer types.
package bits
// UintSize is the size of a uint in bits.
const UintSize = uintSize
// LeadingZerosN returns the number of leading zero bits in x.
// N is absent for uint, or one of 8, 16, 32, 64.
// The result is the size of x in bits for x == 0.
func LeadingZeros(x uint) int { return UintSize - blen(uint64(x)) }
func LeadingZeros8(x uint8) int { return 8 - blen(uint64(x)) }
func LeadingZeros16(x uint16) int { return 16 - blen(uint64(x)) }
func LeadingZeros32(x uint32) int { return 32 - blen(uint64(x)) }
func LeadingZeros64(x uint64) int { return 64 - blen(uint64(x)) }
// TrailingZerosN returns the number of trailing zero bits in x.
// N is absent for uint, or one of 8, 16, 32, 64.
// The result is the size of x in bits for x == 0.
func TrailingZeros(x uint) int { return ntz(x) }
func TrailingZeros8(x uint8) int { return ntz8(x) }
func TrailingZeros16(x uint16) int { return ntz16(x) }
func TrailingZeros32(x uint32) int { return ntz32(x) }
func TrailingZeros64(x uint64) int { return ntz64(x) }
// OnesCountN returns the number of one bits ("population count") in x.
// N is absent for uint, or one of 8, 16, 32, 64.
func OnesCount(x uint) int { return pop(uint64(x)) }
func OnesCount8(x uint8) int { return pop(uint64(x)) }
func OnesCount16(x uint16) int { return pop(uint64(x)) }
func OnesCount32(x uint32) int { return pop(uint64(x)) }
func OnesCount64(x uint64) int { return pop(uint64(x)) }
// RotateLeftN returns the value of x rotated left by k bits; k must not be negative.
// N is absent for uint, or one of 8, 16, 32, 64.
func RotateLeft(x uint, k int) uint { return uint(rot(uint64(x), UintSize, pos(k)%UintSize)) }
func RotateLeft8(x uint8, k int) uint8 { return uint8(rot(uint64(x), 8, pos(k)%8)) }
func RotateLeft16(x uint16, k int) uint16 { return uint16(rot(uint64(x), 16, pos(k)%16)) }
func RotateLeft32(x uint32, k int) uint32 { return uint32(rot(uint64(x), 32, pos(k)%32)) }
func RotateLeft64(x uint64, k int) uint64 { return uint64(rot(uint64(x), 64, pos(k)%64)) }
// RotateRightN returns the value of x rotated right by k bits; k must not be negative.
// N is absent for uint, or one of 8, 16, 32, 64.
func RotateRight(x uint, k int) uint { return uint(rot(uint64(x), UintSize, UintSize-pos(k)%UintSize)) }
func RotateRight8(x uint8, k int) uint8 { return uint8(rot(uint64(x), 8, 8-pos(k)%8)) }
func RotateRight16(x uint16, k int) uint16 { return uint16(rot(uint64(x), 16, 16-pos(k)%16)) }
func RotateRight32(x uint32, k int) uint32 { return uint32(rot(uint64(x), 32, 32-pos(k)%32)) }
func RotateRight64(x uint64, k int) uint64 { return uint64(rot(uint64(x), 64, 64-pos(k)%64)) }
// ReverseN returns the value of x with its bits in reversed order.
// N is absent for uint, or one of 8, 16, 32, 64.
func Reverse(x uint) uint { return uint(rev(uint64(x), UintSize)) }
func Reverse8(x uint8) uint8 { return uint8(rev(uint64(x), 8)) }
func Reverse16(x uint16) uint16 { return uint16(rev(uint64(x), 16)) }
func Reverse32(x uint32) uint32 { return uint32(rev(uint64(x), 32)) }
func Reverse64(x uint64) uint64 { return uint64(rev(uint64(x), 64)) }
// ReverseBytesN returns the value of x with its bytes in reversed order.
// N is absent for uint, or one of 8, 16, 32, 64.
func ReverseBytes(x uint) uint { return uint(swap(uint64(x), UintSize)) }
func ReverseBytes16(x uint16) uint16 { return uint16(swap(uint64(x), 16)) }
func ReverseBytes32(x uint32) uint32 { return uint32(swap(uint64(x), 32)) }
func ReverseBytes64(x uint64) uint64 { return uint64(swap(uint64(x), 64)) }
// LenN returns the minimum number of bits required to represent x.
// LenN(x) - 1 is the index of the most significant bit of x.
// N is absent for uint, or one of 8, 16, 32, 64.
// The result is 0 for x == 0.
func Len(x uint) int { return blen(uint64(x)) }
func Len8(x uint8) int { return blen(uint64(x)) }
func Len16(x uint16) int { return blen(uint64(x)) }
func Len32(x uint32) int { return blen(uint64(x)) }
func Len64(x uint64) int { return blen(uint64(x)) }

132
src/math/bits/bits_impl.go Normal file
View File

@ -0,0 +1,132 @@
// Copyright 2017 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.
// This file provides basic implementations of the bits functions.
package bits
const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
func ntz(x uint) (n int) {
if UintSize == 32 {
return ntz32(uint32(x))
}
return ntz64(uint64(x))
}
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
const deBruijn32 = 0x077CB531
var deBruijn32tab = [32]byte{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
}
func ntz8(x uint8) (n int) {
if x == 0 {
return 8
}
// see comment in ntz64
return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)])
}
func ntz16(x uint16) (n int) {
if x == 0 {
return 16
}
// see comment in ntz64
return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)])
}
func ntz32(x uint32) int {
if x == 0 {
return 32
}
// see comment in ntz64
return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)])
}
const deBruijn64 = 0x03f79d71b4ca8b09
var deBruijn64tab = [64]byte{
0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
}
func ntz64(x uint64) int {
if x == 0 {
return 64
}
// If popcount is fast, replace code below with return popcount(^x & (x - 1)).
//
// x & -x leaves only the right-most bit set in the word. Let k be the
// index of that bit. Since only a single bit is set, the value is two
// to the power of k. Multiplying by a power of two is equivalent to
// left shifting, in this case by k bits. The de Bruijn (64 bit) constant
// is such that all six bit, consecutive substrings are distinct.
// Therefore, if we have a left shifted version of this constant we can
// find by how many bits it was shifted by looking at which six bit
// substring ended up at the top of the word.
// (Knuth, volume 4, section 7.3.1)
return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)])
}
func pop(x uint64) (n int) {
for x != 0 {
n++
x &= x - 1
}
return
}
func pos(k int) uint {
if k < 0 {
panic("negative rotation count")
}
return uint(k)
}
func rot(x uint64, size, k uint) uint64 {
return x<<k | x>>(size-k)&(1<<k-1)
}
func rev(x uint64, size uint) (r uint64) {
for i := size; i > 0; i-- {
r = r<<1 | x&1
x >>= 1
}
return
}
func swap(x uint64, size uint) (r uint64) {
for i := size / 8; i > 0; i-- {
r = r<<8 | x&0xff
x >>= 8
}
return
}
func blen(x uint64) (i int) {
for ; x >= 1<<(16-1); x >>= 16 {
i += 16
}
if x >= 1<<(8-1) {
x >>= 8
i += 8
}
if x >= 1<<(4-1) {
x >>= 4
i += 4
}
if x >= 1<<(2-1) {
x >>= 2
i += 2
}
if x >= 1<<(1-1) {
i++
}
return
}

518
src/math/bits/bits_test.go Normal file
View File

@ -0,0 +1,518 @@
// Copyright 2017 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.
package bits
import (
"testing"
"unsafe"
)
func TestUintSize(t *testing.T) {
var x uint
if want := unsafe.Sizeof(x) * 8; UintSize != want {
t.Fatalf("UintSize = %d; want %d", UintSize, want)
}
}
func TestLeadingZeros(t *testing.T) {
for i := 0; i < 256; i++ {
nlz := tab[i].nlz
for k := 0; k < 64-8; k++ {
x := uint64(i) << uint(k)
if x <= 1<<8-1 {
got := LeadingZeros8(uint8(x))
want := nlz - k + (8 - 8)
if x == 0 {
want = 8
}
if got != want {
t.Fatalf("LeadingZeros8(%#02x) == %d; want %d", x, got, want)
}
}
if x <= 1<<16-1 {
got := LeadingZeros16(uint16(x))
want := nlz - k + (16 - 8)
if x == 0 {
want = 16
}
if got != want {
t.Fatalf("LeadingZeros16(%#04x) == %d; want %d", x, got, want)
}
}
if x <= 1<<32-1 {
got := LeadingZeros32(uint32(x))
want := nlz - k + (32 - 8)
if x == 0 {
want = 32
}
if got != want {
t.Fatalf("LeadingZeros32(%#08x) == %d; want %d", x, got, want)
}
if UintSize == 32 {
got = LeadingZeros(uint(x))
if got != want {
t.Fatalf("LeadingZeros(%#08x) == %d; want %d", x, got, want)
}
}
}
if x <= 1<<64-1 {
got := LeadingZeros64(uint64(x))
want := nlz - k + (64 - 8)
if x == 0 {
want = 64
}
if got != want {
t.Fatalf("LeadingZeros64(%#016x) == %d; want %d", x, got, want)
}
if UintSize == 64 {
got = LeadingZeros(uint(x))
if got != want {
t.Fatalf("LeadingZeros(%#016x) == %d; want %d", x, got, want)
}
}
}
}
}
}
func TestTrailingZeros(t *testing.T) {
for i := 0; i < 256; i++ {
ntz := tab[i].ntz
for k := 0; k < 64-8; k++ {
x := uint64(i) << uint(k)
want := ntz + k
if x <= 1<<8-1 {
got := TrailingZeros8(uint8(x))
if x == 0 {
want = 8
}
if got != want {
t.Fatalf("TrailingZeros8(%#02x) == %d; want %d", x, got, want)
}
}
if x <= 1<<16-1 {
got := TrailingZeros16(uint16(x))
if x == 0 {
want = 16
}
if got != want {
t.Fatalf("TrailingZeros16(%#04x) == %d; want %d", x, got, want)
}
}
if x <= 1<<32-1 {
got := TrailingZeros32(uint32(x))
if x == 0 {
want = 32
}
if got != want {
t.Fatalf("TrailingZeros32(%#08x) == %d; want %d", x, got, want)
}
if UintSize == 32 {
got = TrailingZeros(uint(x))
if got != want {
t.Fatalf("TrailingZeros(%#08x) == %d; want %d", x, got, want)
}
}
}
if x <= 1<<64-1 {
got := TrailingZeros64(uint64(x))
if x == 0 {
want = 64
}
if got != want {
t.Fatalf("TrailingZeros64(%#016x) == %d; want %d", x, got, want)
}
if UintSize == 64 {
got = TrailingZeros(uint(x))
if got != want {
t.Fatalf("TrailingZeros(%#016x) == %d; want %d", x, got, want)
}
}
}
}
}
}
func TestOnesCount(t *testing.T) {
for i := 0; i < 256; i++ {
want := tab[i].pop
for k := 0; k < 64-8; k++ {
x := uint64(i) << uint(k)
if x <= 1<<8-1 {
got := OnesCount8(uint8(x))
if got != want {
t.Fatalf("OnesCount8(%#02x) == %d; want %d", x, got, want)
}
}
if x <= 1<<16-1 {
got := OnesCount16(uint16(x))
if got != want {
t.Fatalf("OnesCount16(%#04x) == %d; want %d", x, got, want)
}
}
if x <= 1<<32-1 {
got := OnesCount32(uint32(x))
if got != want {
t.Fatalf("OnesCount32(%#08x) == %d; want %d", x, got, want)
}
if UintSize == 32 {
got = OnesCount(uint(x))
if got != want {
t.Fatalf("OnesCount(%#08x) == %d; want %d", x, got, want)
}
}
}
if x <= 1<<64-1 {
got := OnesCount64(uint64(x))
if got != want {
t.Fatalf("OnesCount64(%#016x) == %d; want %d", x, got, want)
}
if UintSize == 64 {
got = OnesCount(uint(x))
if got != want {
t.Fatalf("OnesCount(%#016x) == %d; want %d", x, got, want)
}
}
}
}
}
}
func TestRotateLeft(t *testing.T) {
var m uint64 = deBruijn64
for k := uint(0); k < 128; k++ {
x8 := uint8(m)
got8 := RotateLeft8(x8, int(k))
want8 := x8<<(k&0x7) | x8>>(8-k&0x7)
if got8 != want8 {
t.Fatalf("RotateLeft8(%#02x, %d) == %#02x; want %#02x", x8, k, got8, want8)
}
x16 := uint16(m)
got16 := RotateLeft16(x16, int(k))
want16 := x16<<(k&0xf) | x16>>(16-k&0xf)
if got16 != want16 {
t.Fatalf("RotateLeft16(%#04x, %d) == %#04x; want %#04x", x16, k, got16, want16)
}
x32 := uint32(m)
got32 := RotateLeft32(x32, int(k))
want32 := x32<<(k&0x1f) | x32>>(32-k&0x1f)
if got32 != want32 {
t.Fatalf("RotateLeft32(%#08x, %d) == %#08x; want %#08x", x32, k, got32, want32)
}
if UintSize == 32 {
x := uint(m)
got := RotateLeft(x, int(k))
want := x<<(k&0x1f) | x>>(32-k&0x1f)
if got != want {
t.Fatalf("RotateLeft(%#08x, %d) == %#08x; want %#08x", x, k, got, want)
}
}
x64 := uint64(m)
got64 := RotateLeft64(x64, int(k))
want64 := x64<<(k&0x3f) | x64>>(64-k&0x3f)
if got64 != want64 {
t.Fatalf("RotateLeft64(%#016x, %d) == %#016x; want %#016x", x64, k, got64, want64)
}
if UintSize == 64 {
x := uint(m)
got := RotateLeft(x, int(k))
want := x<<(k&0x3f) | x>>(64-k&0x3f)
if got != want {
t.Fatalf("RotateLeft(%#016x, %d) == %#016x; want %#016x", x, k, got, want)
}
}
}
}
func TestRotateRight(t *testing.T) {
var m uint64 = deBruijn64
for k := uint(0); k < 128; k++ {
x8 := uint8(m)
got8 := RotateRight8(x8, int(k))
want8 := x8>>(k&0x7) | x8<<(8-k&0x7)
if got8 != want8 {
t.Fatalf("RotateRight8(%#02x, %d) == %#02x; want %#02x", x8, k, got8, want8)
}
x16 := uint16(m)
got16 := RotateRight16(x16, int(k))
want16 := x16>>(k&0xf) | x16<<(16-k&0xf)
if got16 != want16 {
t.Fatalf("RotateRight16(%#04x, %d) == %#04x; want %#04x", x16, k, got16, want16)
}
x32 := uint32(m)
got32 := RotateRight32(x32, int(k))
want32 := x32>>(k&0x1f) | x32<<(32-k&0x1f)
if got32 != want32 {
t.Fatalf("RotateRight32(%#08x, %d) == %#08x; want %#08x", x32, k, got32, want32)
}
if UintSize == 32 {
x := uint(m)
got := RotateRight(x, int(k))
want := x>>(k&0x1f) | x<<(32-k&0x1f)
if got != want {
t.Fatalf("RotateRight(%#08x, %d) == %#08x; want %#08x", x, k, got, want)
}
}
x64 := uint64(m)
got64 := RotateRight64(x64, int(k))
want64 := x64>>(k&0x3f) | x64<<(64-k&0x3f)
if got64 != want64 {
t.Fatalf("RotateRight64(%#016x, %d) == %#016x; want %#016x", x64, k, got64, want64)
}
if UintSize == 64 {
x := uint(m)
got := RotateRight(x, int(k))
want := x>>(k&0x3f) | x<<(64-k&0x3f)
if got != want {
t.Fatalf("RotateRight(%#016x, %d) == %#016x; want %#016x", x, k, got, want)
}
}
}
}
func TestReverse(t *testing.T) {
// test each bit
for i := uint(0); i < 64; i++ {
testReverse(t, uint64(1)<<i, uint64(1)<<(63-i))
}
// test a few patterns
for _, test := range []struct {
x, r uint64
}{
{0, 0},
{0x1, 0x8 << 60},
{0x2, 0x4 << 60},
{0x3, 0xc << 60},
{0x4, 0x2 << 60},
{0x5, 0xa << 60},
{0x6, 0x6 << 60},
{0x7, 0xe << 60},
{0x8, 0x1 << 60},
{0x9, 0x9 << 60},
{0xa, 0x5 << 60},
{0xb, 0xd << 60},
{0xc, 0x3 << 60},
{0xd, 0xb << 60},
{0xe, 0x7 << 60},
{0xf, 0xf << 60},
{0x5686487, 0xe12616a000000000},
{0x0123456789abcdef, 0xf7b3d591e6a2c480},
} {
testReverse(t, test.x, test.r)
testReverse(t, test.r, test.x)
}
}
func testReverse(t *testing.T, x64, want64 uint64) {
x8 := uint8(x64)
got8 := Reverse8(x8)
want8 := uint8(want64 >> (64 - 8))
if got8 != want8 {
t.Fatalf("Reverse8(%#02x) == %#02x; want %#02x", x8, got8, want8)
}
x16 := uint16(x64)
got16 := Reverse16(x16)
want16 := uint16(want64 >> (64 - 16))
if got16 != want16 {
t.Fatalf("Reverse16(%#04x) == %#04x; want %#04x", x16, got16, want16)
}
x32 := uint32(x64)
got32 := Reverse32(x32)
want32 := uint32(want64 >> (64 - 32))
if got32 != want32 {
t.Fatalf("Reverse32(%#08x) == %#08x; want %#08x", x32, got32, want32)
}
if UintSize == 32 {
x := uint(x32)
got := Reverse(x)
want := uint(want32)
if got != want {
t.Fatalf("Reverse(%#08x) == %#08x; want %#08x", x, got, want)
}
}
got64 := Reverse64(x64)
if got64 != want64 {
t.Fatalf("Reverse64(%#016x) == %#016x; want %#016x", x64, got64, want64)
}
if UintSize == 64 {
x := uint(x64)
got := Reverse(x)
want := uint(want64)
if got != want {
t.Fatalf("Reverse(%#08x) == %#016x; want %#016x", x, got, want)
}
}
}
func TestReverseBytes(t *testing.T) {
for _, test := range []struct {
x, r uint64
}{
{0, 0},
{0x01, 0x01 << 56},
{0x0123, 0x2301 << 48},
{0x012345, 0x452301 << 40},
{0x01234567, 0x67452301 << 32},
{0x0123456789, 0x8967452301 << 24},
{0x0123456789ab, 0xab8967452301 << 16},
{0x0123456789abcd, 0xcdab8967452301 << 8},
{0x0123456789abcdef, 0xefcdab8967452301 << 0},
} {
testReverseBytes(t, test.x, test.r)
testReverseBytes(t, test.r, test.x)
}
}
func testReverseBytes(t *testing.T, x64, want64 uint64) {
x16 := uint16(x64)
got16 := ReverseBytes16(x16)
want16 := uint16(want64 >> (64 - 16))
if got16 != want16 {
t.Fatalf("ReverseBytes16(%#04x) == %#04x; want %#04x", x16, got16, want16)
}
x32 := uint32(x64)
got32 := ReverseBytes32(x32)
want32 := uint32(want64 >> (64 - 32))
if got32 != want32 {
t.Fatalf("ReverseBytes32(%#08x) == %#08x; want %#08x", x32, got32, want32)
}
if UintSize == 32 {
x := uint(x32)
got := ReverseBytes(x)
want := uint(want32)
if got != want {
t.Fatalf("ReverseBytes(%#08x) == %#08x; want %#08x", x, got, want)
}
}
got64 := ReverseBytes64(x64)
if got64 != want64 {
t.Fatalf("ReverseBytes64(%#016x) == %#016x; want %#016x", x64, got64, want64)
}
if UintSize == 64 {
x := uint(x64)
got := ReverseBytes(x)
want := uint(want64)
if got != want {
t.Fatalf("ReverseBytes(%#016x) == %#016x; want %#016x", x, got, want)
}
}
}
func TestLen(t *testing.T) {
for i := 0; i < 256; i++ {
len := 8 - tab[i].nlz
for k := 0; k < 64-8; k++ {
x := uint64(i) << uint(k)
want := 0
if x != 0 {
want = len + k
}
if x <= 1<<8-1 {
got := Len8(uint8(x))
if got != want {
t.Fatalf("Len8(%#02x) == %d; want %d", x, got, want)
}
}
if x <= 1<<16-1 {
got := Len16(uint16(x))
if got != want {
t.Fatalf("Len16(%#04x) == %d; want %d", x, got, want)
}
}
if x <= 1<<32-1 {
got := Len32(uint32(x))
if got != want {
t.Fatalf("Len32(%#08x) == %d; want %d", x, got, want)
}
if UintSize == 32 {
got := Len(uint(x))
if got != want {
t.Fatalf("Len(%#08x) == %d; want %d", x, got, want)
}
}
}
if x <= 1<<64-1 {
got := Len64(uint64(x))
if got != want {
t.Fatalf("Len64(%#016x) == %d; want %d", x, got, want)
}
if UintSize == 64 {
got := Len(uint(x))
if got != want {
t.Fatalf("Len(%#016x) == %d; want %d", x, got, want)
}
}
}
}
}
}
// ----------------------------------------------------------------------------
// Testing support
type entry = struct {
nlz, ntz, pop int
}
// tab contains results for all uint8 values
var tab [256]entry
func init() {
tab[0] = entry{8, 8, 0}
for i := 1; i < len(tab); i++ {
// nlz
x := i // x != 0
n := 0
for x&0x80 == 0 {
n++
x <<= 1
}
tab[i].nlz = n
// ntz
x = i // x != 0
n = 0
for x&1 == 0 {
n++
x >>= 1
}
tab[i].ntz = n
// pop
x = i // x != 0
n = 0
for x != 0 {
n += int(x & 1)
x >>= 1
}
tab[i].pop = n
}
}