1// Copyright 2017 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5//go:generate go run make_tables.go 6 7// Package bits implements bit counting and manipulation 8// functions for the predeclared unsigned integer types. 9package bits 10 11const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 12 13// UintSize is the size of a uint in bits. 14const UintSize = uintSize 15 16// --- LeadingZeros --- 17 18// LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0. 19func LeadingZeros(x uint) int { return UintSize - Len(x) } 20 21// LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0. 22func LeadingZeros8(x uint8) int { return 8 - Len8(x) } 23 24// LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0. 25func LeadingZeros16(x uint16) int { return 16 - Len16(x) } 26 27// LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0. 28func LeadingZeros32(x uint32) int { return 32 - Len32(x) } 29 30// LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0. 31func LeadingZeros64(x uint64) int { return 64 - Len64(x) } 32 33// --- TrailingZeros --- 34 35// See http://supertech.csail.mit.edu/papers/debruijn.pdf 36const deBruijn32 = 0x077CB531 37 38var deBruijn32tab = [32]byte{ 39 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 40 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, 41} 42 43const deBruijn64 = 0x03f79d71b4ca8b09 44 45var deBruijn64tab = [64]byte{ 46 0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 47 62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 48 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11, 49 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6, 50} 51 52// TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0. 53func TrailingZeros(x uint) int { 54 if UintSize == 32 { 55 return TrailingZeros32(uint32(x)) 56 } 57 return TrailingZeros64(uint64(x)) 58} 59 60// TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0. 61func TrailingZeros8(x uint8) int { 62 return int(ntz8tab[x]) 63} 64 65// TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0. 66func TrailingZeros16(x uint16) (n int) { 67 if x == 0 { 68 return 16 69 } 70 // see comment in TrailingZeros64 71 return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)]) 72} 73 74// TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0. 75func TrailingZeros32(x uint32) int { 76 if x == 0 { 77 return 32 78 } 79 // see comment in TrailingZeros64 80 return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)]) 81} 82 83// TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0. 84func TrailingZeros64(x uint64) int { 85 if x == 0 { 86 return 64 87 } 88 // If popcount is fast, replace code below with return popcount(^x & (x - 1)). 89 // 90 // x & -x leaves only the right-most bit set in the word. Let k be the 91 // index of that bit. Since only a single bit is set, the value is two 92 // to the power of k. Multiplying by a power of two is equivalent to 93 // left shifting, in this case by k bits. The de Bruijn (64 bit) constant 94 // is such that all six bit, consecutive substrings are distinct. 95 // Therefore, if we have a left shifted version of this constant we can 96 // find by how many bits it was shifted by looking at which six bit 97 // substring ended up at the top of the word. 98 // (Knuth, volume 4, section 7.3.1) 99 return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)]) 100} 101 102// --- OnesCount --- 103 104const m0 = 0x5555555555555555 // 01010101 ... 105const m1 = 0x3333333333333333 // 00110011 ... 106const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ... 107const m3 = 0x00ff00ff00ff00ff // etc. 108const m4 = 0x0000ffff0000ffff 109 110// OnesCount returns the number of one bits ("population count") in x. 111func OnesCount(x uint) int { 112 if UintSize == 32 { 113 return OnesCount32(uint32(x)) 114 } 115 return OnesCount64(uint64(x)) 116} 117 118// OnesCount8 returns the number of one bits ("population count") in x. 119func OnesCount8(x uint8) int { 120 return int(pop8tab[x]) 121} 122 123// OnesCount16 returns the number of one bits ("population count") in x. 124func OnesCount16(x uint16) int { 125 return int(pop8tab[x>>8] + pop8tab[x&0xff]) 126} 127 128// OnesCount32 returns the number of one bits ("population count") in x. 129func OnesCount32(x uint32) int { 130 return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff]) 131} 132 133// OnesCount64 returns the number of one bits ("population count") in x. 134func OnesCount64(x uint64) int { 135 // Implementation: Parallel summing of adjacent bits. 136 // See "Hacker's Delight", Chap. 5: Counting Bits. 137 // The following pattern shows the general approach: 138 // 139 // x = x>>1&(m0&m) + x&(m0&m) 140 // x = x>>2&(m1&m) + x&(m1&m) 141 // x = x>>4&(m2&m) + x&(m2&m) 142 // x = x>>8&(m3&m) + x&(m3&m) 143 // x = x>>16&(m4&m) + x&(m4&m) 144 // x = x>>32&(m5&m) + x&(m5&m) 145 // return int(x) 146 // 147 // Masking (& operations) can be left away when there's no 148 // danger that a field's sum will carry over into the next 149 // field: Since the result cannot be > 64, 8 bits is enough 150 // and we can ignore the masks for the shifts by 8 and up. 151 // Per "Hacker's Delight", the first line can be simplified 152 // more, but it saves at best one instruction, so we leave 153 // it alone for clarity. 154 const m = 1<<64 - 1 155 x = x>>1&(m0&m) + x&(m0&m) 156 x = x>>2&(m1&m) + x&(m1&m) 157 x = (x>>4 + x) & (m2 & m) 158 x += x >> 8 159 x += x >> 16 160 x += x >> 32 161 return int(x) & (1<<7 - 1) 162} 163 164// --- RotateLeft --- 165 166// RotateLeft returns the value of x rotated left by (k mod UintSize) bits. 167// To rotate x right by k bits, call RotateLeft(x, -k). 168func RotateLeft(x uint, k int) uint { 169 if UintSize == 32 { 170 return uint(RotateLeft32(uint32(x), k)) 171 } 172 return uint(RotateLeft64(uint64(x), k)) 173} 174 175// RotateLeft8 returns the value of x rotated left by (k mod 8) bits. 176// To rotate x right by k bits, call RotateLeft8(x, -k). 177func RotateLeft8(x uint8, k int) uint8 { 178 const n = 8 179 s := uint(k) & (n - 1) 180 return x<<s | x>>(n-s) 181} 182 183// RotateLeft16 returns the value of x rotated left by (k mod 16) bits. 184// To rotate x right by k bits, call RotateLeft16(x, -k). 185func RotateLeft16(x uint16, k int) uint16 { 186 const n = 16 187 s := uint(k) & (n - 1) 188 return x<<s | x>>(n-s) 189} 190 191// RotateLeft32 returns the value of x rotated left by (k mod 32) bits. 192// To rotate x right by k bits, call RotateLeft32(x, -k). 193func RotateLeft32(x uint32, k int) uint32 { 194 const n = 32 195 s := uint(k) & (n - 1) 196 return x<<s | x>>(n-s) 197} 198 199// RotateLeft64 returns the value of x rotated left by (k mod 64) bits. 200// To rotate x right by k bits, call RotateLeft64(x, -k). 201func RotateLeft64(x uint64, k int) uint64 { 202 const n = 64 203 s := uint(k) & (n - 1) 204 return x<<s | x>>(n-s) 205} 206 207// --- Reverse --- 208 209// Reverse returns the value of x with its bits in reversed order. 210func Reverse(x uint) uint { 211 if UintSize == 32 { 212 return uint(Reverse32(uint32(x))) 213 } 214 return uint(Reverse64(uint64(x))) 215} 216 217// Reverse8 returns the value of x with its bits in reversed order. 218func Reverse8(x uint8) uint8 { 219 return rev8tab[x] 220} 221 222// Reverse16 returns the value of x with its bits in reversed order. 223func Reverse16(x uint16) uint16 { 224 return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&0xff])<<8 225} 226 227// Reverse32 returns the value of x with its bits in reversed order. 228func Reverse32(x uint32) uint32 { 229 const m = 1<<32 - 1 230 x = x>>1&(m0&m) | x&(m0&m)<<1 231 x = x>>2&(m1&m) | x&(m1&m)<<2 232 x = x>>4&(m2&m) | x&(m2&m)<<4 233 x = x>>8&(m3&m) | x&(m3&m)<<8 234 return x>>16 | x<<16 235} 236 237// Reverse64 returns the value of x with its bits in reversed order. 238func Reverse64(x uint64) uint64 { 239 const m = 1<<64 - 1 240 x = x>>1&(m0&m) | x&(m0&m)<<1 241 x = x>>2&(m1&m) | x&(m1&m)<<2 242 x = x>>4&(m2&m) | x&(m2&m)<<4 243 x = x>>8&(m3&m) | x&(m3&m)<<8 244 x = x>>16&(m4&m) | x&(m4&m)<<16 245 return x>>32 | x<<32 246} 247 248// --- ReverseBytes --- 249 250// ReverseBytes returns the value of x with its bytes in reversed order. 251func ReverseBytes(x uint) uint { 252 if UintSize == 32 { 253 return uint(ReverseBytes32(uint32(x))) 254 } 255 return uint(ReverseBytes64(uint64(x))) 256} 257 258// ReverseBytes16 returns the value of x with its bytes in reversed order. 259func ReverseBytes16(x uint16) uint16 { 260 return x>>8 | x<<8 261} 262 263// ReverseBytes32 returns the value of x with its bytes in reversed order. 264func ReverseBytes32(x uint32) uint32 { 265 const m = 1<<32 - 1 266 x = x>>8&(m3&m) | x&(m3&m)<<8 267 return x>>16 | x<<16 268} 269 270// ReverseBytes64 returns the value of x with its bytes in reversed order. 271func ReverseBytes64(x uint64) uint64 { 272 const m = 1<<64 - 1 273 x = x>>8&(m3&m) | x&(m3&m)<<8 274 x = x>>16&(m4&m) | x&(m4&m)<<16 275 return x>>32 | x<<32 276} 277 278// --- Len --- 279 280// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0. 281func Len(x uint) int { 282 if UintSize == 32 { 283 return Len32(uint32(x)) 284 } 285 return Len64(uint64(x)) 286} 287 288// Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 289func Len8(x uint8) int { 290 return int(len8tab[x]) 291} 292 293// Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 294func Len16(x uint16) (n int) { 295 if x >= 1<<8 { 296 x >>= 8 297 n = 8 298 } 299 return n + int(len8tab[x]) 300} 301 302// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 303func Len32(x uint32) (n int) { 304 if x >= 1<<16 { 305 x >>= 16 306 n = 16 307 } 308 if x >= 1<<8 { 309 x >>= 8 310 n += 8 311 } 312 return n + int(len8tab[x]) 313} 314 315// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 316func Len64(x uint64) (n int) { 317 if x >= 1<<32 { 318 x >>= 32 319 n = 32 320 } 321 if x >= 1<<16 { 322 x >>= 16 323 n += 16 324 } 325 if x >= 1<<8 { 326 x >>= 8 327 n += 8 328 } 329 return n + int(len8tab[x]) 330} 331