Commit 661e2179 authored by Robert Griesemer's avatar Robert Griesemer

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/36315Reviewed-by: 's avatarKeith Randall <khr@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 1693e7b6
// 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)) }
// 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
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment