Source File
bits.go
Belonging Package
math/bits
// 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.//go:generate go run make_tables.go// Package bits implements bit counting and manipulation// functions for the predeclared unsigned integer types.package bitsconst uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64// UintSize is the size of a uint in bits.const UintSize = uintSize// --- LeadingZeros ---// LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.func ( uint) int { return UintSize - Len() }// LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.func ( uint8) int { return 8 - Len8() }// LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.func ( uint16) int { return 16 - Len16() }// LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.func ( uint32) int { return 32 - Len32() }// LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.func ( uint64) int { return 64 - Len64() }// --- TrailingZeros ---// See http://supertech.csail.mit.edu/papers/debruijn.pdfconst deBruijn32 = 0x077CB531var 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,}const deBruijn64 = 0x03f79d71b4ca8b09var 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,}// TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.func ( uint) int {if UintSize == 32 {return TrailingZeros32(uint32())}return TrailingZeros64(uint64())}// TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.func ( uint8) int {return int(ntz8tab[])}// TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.func ( uint16) int {if == 0 {return 16}// see comment in TrailingZeros64return int(deBruijn32tab[uint32(&-)*deBruijn32>>(32-5)])}// TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.func ( uint32) int {if == 0 {return 32}// see comment in TrailingZeros64return int(deBruijn32tab[(&-)*deBruijn32>>(32-5)])}// TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.func ( uint64) int {if == 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[(&-)*deBruijn64>>(64-6)])}// --- OnesCount ---const m0 = 0x5555555555555555 // 01010101 ...const m1 = 0x3333333333333333 // 00110011 ...const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...const m3 = 0x00ff00ff00ff00ff // etc.const m4 = 0x0000ffff0000ffff// OnesCount returns the number of one bits ("population count") in x.func ( uint) int {if UintSize == 32 {return OnesCount32(uint32())}return OnesCount64(uint64())}// OnesCount8 returns the number of one bits ("population count") in x.func ( uint8) int {return int(pop8tab[])}// OnesCount16 returns the number of one bits ("population count") in x.func ( uint16) int {return int(pop8tab[>>8] + pop8tab[&0xff])}// OnesCount32 returns the number of one bits ("population count") in x.func ( uint32) int {return int(pop8tab[>>24] + pop8tab[>>16&0xff] + pop8tab[>>8&0xff] + pop8tab[&0xff])}// OnesCount64 returns the number of one bits ("population count") in x.func ( uint64) int {// Implementation: Parallel summing of adjacent bits.// See "Hacker's Delight", Chap. 5: Counting Bits.// The following pattern shows the general approach://// x = x>>1&(m0&m) + x&(m0&m)// x = x>>2&(m1&m) + x&(m1&m)// x = x>>4&(m2&m) + x&(m2&m)// x = x>>8&(m3&m) + x&(m3&m)// x = x>>16&(m4&m) + x&(m4&m)// x = x>>32&(m5&m) + x&(m5&m)// return int(x)//// Masking (& operations) can be left away when there's no// danger that a field's sum will carry over into the next// field: Since the result cannot be > 64, 8 bits is enough// and we can ignore the masks for the shifts by 8 and up.// Per "Hacker's Delight", the first line can be simplified// more, but it saves at best one instruction, so we leave// it alone for clarity.const = 1<<64 - 1= >>1&(m0&) + &(m0&)= >>2&(m1&) + &(m1&)= (>>4 + ) & (m2 & )+= >> 8+= >> 16+= >> 32return int() & (1<<7 - 1)}// --- RotateLeft ---// RotateLeft returns the value of x rotated left by (k mod UintSize) bits.// To rotate x right by k bits, call RotateLeft(x, -k).//// This function's execution time does not depend on the inputs.func ( uint, int) uint {if UintSize == 32 {return uint(RotateLeft32(uint32(), ))}return uint(RotateLeft64(uint64(), ))}// RotateLeft8 returns the value of x rotated left by (k mod 8) bits.// To rotate x right by k bits, call RotateLeft8(x, -k).//// This function's execution time does not depend on the inputs.func ( uint8, int) uint8 {const = 8:= uint() & ( - 1)return << | >>(-)}// RotateLeft16 returns the value of x rotated left by (k mod 16) bits.// To rotate x right by k bits, call RotateLeft16(x, -k).//// This function's execution time does not depend on the inputs.func ( uint16, int) uint16 {const = 16:= uint() & ( - 1)return << | >>(-)}// RotateLeft32 returns the value of x rotated left by (k mod 32) bits.// To rotate x right by k bits, call RotateLeft32(x, -k).//// This function's execution time does not depend on the inputs.func ( uint32, int) uint32 {const = 32:= uint() & ( - 1)return << | >>(-)}// RotateLeft64 returns the value of x rotated left by (k mod 64) bits.// To rotate x right by k bits, call RotateLeft64(x, -k).//// This function's execution time does not depend on the inputs.func ( uint64, int) uint64 {const = 64:= uint() & ( - 1)return << | >>(-)}// --- Reverse ---// Reverse returns the value of x with its bits in reversed order.func ( uint) uint {if UintSize == 32 {return uint(Reverse32(uint32()))}return uint(Reverse64(uint64()))}// Reverse8 returns the value of x with its bits in reversed order.func ( uint8) uint8 {return rev8tab[]}// Reverse16 returns the value of x with its bits in reversed order.func ( uint16) uint16 {return uint16(rev8tab[>>8]) | uint16(rev8tab[&0xff])<<8}// Reverse32 returns the value of x with its bits in reversed order.func ( uint32) uint32 {const = 1<<32 - 1= >>1&(m0&) | &(m0&)<<1= >>2&(m1&) | &(m1&)<<2= >>4&(m2&) | &(m2&)<<4return ReverseBytes32()}// Reverse64 returns the value of x with its bits in reversed order.func ( uint64) uint64 {const = 1<<64 - 1= >>1&(m0&) | &(m0&)<<1= >>2&(m1&) | &(m1&)<<2= >>4&(m2&) | &(m2&)<<4return ReverseBytes64()}// --- ReverseBytes ---// ReverseBytes returns the value of x with its bytes in reversed order.//// This function's execution time does not depend on the inputs.func ( uint) uint {if UintSize == 32 {return uint(ReverseBytes32(uint32()))}return uint(ReverseBytes64(uint64()))}// ReverseBytes16 returns the value of x with its bytes in reversed order.//// This function's execution time does not depend on the inputs.func ( uint16) uint16 {return >>8 | <<8}// ReverseBytes32 returns the value of x with its bytes in reversed order.//// This function's execution time does not depend on the inputs.func ( uint32) uint32 {const = 1<<32 - 1= >>8&(m3&) | &(m3&)<<8return >>16 | <<16}// ReverseBytes64 returns the value of x with its bytes in reversed order.//// This function's execution time does not depend on the inputs.func ( uint64) uint64 {const = 1<<64 - 1= >>8&(m3&) | &(m3&)<<8= >>16&(m4&) | &(m4&)<<16return >>32 | <<32}// --- Len ---// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.func ( uint) int {if UintSize == 32 {return Len32(uint32())}return Len64(uint64())}// Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.func ( uint8) int {return int(len8tab[])}// Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.func ( uint16) ( int) {if >= 1<<8 {>>= 8= 8}return + int(len8tab[])}// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.func ( uint32) ( int) {if >= 1<<16 {>>= 16= 16}if >= 1<<8 {>>= 8+= 8}return + int(len8tab[])}// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.func ( uint64) ( int) {if >= 1<<32 {>>= 32= 32}if >= 1<<16 {>>= 16+= 16}if >= 1<<8 {>>= 8+= 8}return + int(len8tab[])}// --- Add with carry ---// Add returns the sum with carry of x, y and carry: sum = x + y + carry.// The carry input must be 0 or 1; otherwise the behavior is undefined.// The carryOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint) (, uint) {if UintSize == 32 {, := Add32(uint32(), uint32(), uint32())return uint(), uint()}, := Add64(uint64(), uint64(), uint64())return uint(), uint()}// Add32 returns the sum with carry of x, y and carry: sum = x + y + carry.// The carry input must be 0 or 1; otherwise the behavior is undefined.// The carryOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint32) (, uint32) {:= uint64() + uint64() + uint64()= uint32()= uint32( >> 32)return}// Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.// The carry input must be 0 or 1; otherwise the behavior is undefined.// The carryOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint64) (, uint64) {= + +// The sum will overflow if both top bits are set (x & y) or if one of them// is (x | y), and a carry from the lower place happened. If such a carry// happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum).= (( & ) | (( | ) &^ )) >> 63return}// --- Subtract with borrow ---// Sub returns the difference of x, y and borrow: diff = x - y - borrow.// The borrow input must be 0 or 1; otherwise the behavior is undefined.// The borrowOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint) (, uint) {if UintSize == 32 {, := Sub32(uint32(), uint32(), uint32())return uint(), uint()}, := Sub64(uint64(), uint64(), uint64())return uint(), uint()}// Sub32 returns the difference of x, y and borrow, diff = x - y - borrow.// The borrow input must be 0 or 1; otherwise the behavior is undefined.// The borrowOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint32) (, uint32) {= - -// The difference will underflow if the top bit of x is not set and the top// bit of y is set (^x & y) or if they are the same (^(x ^ y)) and a borrow// from the lower place happens. If that borrow happens, the result will be// 1 - 1 - 1 = 0 - 0 - 1 = 1 (& diff).= ((^ & ) | (^( ^ ) & )) >> 31return}// Sub64 returns the difference of x, y and borrow: diff = x - y - borrow.// The borrow input must be 0 or 1; otherwise the behavior is undefined.// The borrowOut output is guaranteed to be 0 or 1.//// This function's execution time does not depend on the inputs.func (, , uint64) (, uint64) {= - -// See Sub32 for the bit logic.= ((^ & ) | (^( ^ ) & )) >> 63return}// --- Full-width multiply ---// Mul returns the full-width product of x and y: (hi, lo) = x * y// with the product bits' upper half returned in hi and the lower// half returned in lo.//// This function's execution time does not depend on the inputs.func (, uint) (, uint) {if UintSize == 32 {, := Mul32(uint32(), uint32())return uint(), uint()}, := Mul64(uint64(), uint64())return uint(), uint()}// Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y// with the product bits' upper half returned in hi and the lower// half returned in lo.//// This function's execution time does not depend on the inputs.func (, uint32) (, uint32) {:= uint64() * uint64(), = uint32(>>32), uint32()return}// Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y// with the product bits' upper half returned in hi and the lower// half returned in lo.//// This function's execution time does not depend on the inputs.func (, uint64) (, uint64) {const = 1<<32 - 1:= &:= >> 32:= &:= >> 32:= *:= * + >>32:= &:= >> 32+= *= * + + >>32= *return}// --- Full-width divide ---// Div returns the quotient and remainder of (hi, lo) divided by y:// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper// half in parameter hi and the lower half in parameter lo.// Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).func (, , uint) (, uint) {if UintSize == 32 {, := Div32(uint32(), uint32(), uint32())return uint(), uint()}, := Div64(uint64(), uint64(), uint64())return uint(), uint()}// Div32 returns the quotient and remainder of (hi, lo) divided by y:// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper// half in parameter hi and the lower half in parameter lo.// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).func (, , uint32) (, uint32) {if != 0 && <= {panic(overflowError)}:= uint64()<<32 | uint64(), = uint32(/uint64()), uint32(%uint64())return}// Div64 returns the quotient and remainder of (hi, lo) divided by y:// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper// half in parameter hi and the lower half in parameter lo.// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).func (, , uint64) (, uint64) {const (= 1 << 32= - 1)if == 0 {panic(divideError)}if <= {panic(overflowError)}:= uint(LeadingZeros64())<<=:= >> 32:= &:= << | >>(64-):= <<:= >> 32:= &:= /:= - *for >= || * > *+ {--+=if >= {break}}:= * + - *:= /= - *for >= || * > *+ {--+=if >= {break}}return * + , (* + - *) >>}// Rem returns the remainder of (hi, lo) divided by y. Rem panics for// y == 0 (division by zero) but, unlike Div, it doesn't panic on a// quotient overflow.func (, , uint) uint {if UintSize == 32 {return uint(Rem32(uint32(), uint32(), uint32()))}return uint(Rem64(uint64(), uint64(), uint64()))}// Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics// for y == 0 (division by zero) but, unlike Div32, it doesn't panic// on a quotient overflow.func (, , uint32) uint32 {return uint32((uint64()<<32 | uint64()) % uint64())}// Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics// for y == 0 (division by zero) but, unlike Div64, it doesn't panic// on a quotient overflow.func (, , uint64) uint64 {// We scale down hi so that hi < y, then use Div64 to compute the// rem with the guarantee that it won't panic on quotient overflow.// Given that// hi ≡ hi%y (mod y)// we have// hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y), := Div64(%, , )return}