github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/math/bits/bits.gno (about) 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. 9 // 10 // Functions in this package may be implemented directly by 11 // the compiler, for better performance. For those functions 12 // the code in this package will not be used. Which 13 // functions are implemented by the compiler depends on the 14 // architecture and the Go release. 15 package bits 16 17 const uintSize = 32 << (^uint(0) >> 63) // 32 or 64 18 19 // UintSize is the size of a uint in bits. 20 const UintSize = uintSize 21 22 // --- LeadingZeros --- 23 24 // LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0. 25 func LeadingZeros(x uint) int { return UintSize - Len(x) } 26 27 // LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0. 28 func LeadingZeros8(x uint8) int { return 8 - Len8(x) } 29 30 // LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0. 31 func LeadingZeros16(x uint16) int { return 16 - Len16(x) } 32 33 // LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0. 34 func LeadingZeros32(x uint32) int { return 32 - Len32(x) } 35 36 // LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0. 37 func LeadingZeros64(x uint64) int { return 64 - Len64(x) } 38 39 // --- TrailingZeros --- 40 41 // See http://supertech.csail.mit.edu/papers/debruijn.pdf 42 const deBruijn32 = 0x077CB531 43 44 var deBruijn32tab = [32]byte{ 45 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 46 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, 47 } 48 49 const deBruijn64 = 0x03f79d71b4ca8b09 50 51 var deBruijn64tab = [64]byte{ 52 0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 53 62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 54 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11, 55 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6, 56 } 57 58 // TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0. 59 func TrailingZeros(x uint) int { 60 if UintSize == 32 { 61 return TrailingZeros32(uint32(x)) 62 } 63 return TrailingZeros64(uint64(x)) 64 } 65 66 // TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0. 67 func TrailingZeros8(x uint8) int { 68 return int(ntz8tab[x]) 69 } 70 71 // TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0. 72 func TrailingZeros16(x uint16) int { 73 if x == 0 { 74 return 16 75 } 76 // see comment in TrailingZeros64 77 return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)]) 78 } 79 80 // TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0. 81 func TrailingZeros32(x uint32) int { 82 if x == 0 { 83 return 32 84 } 85 // see comment in TrailingZeros64 86 return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)]) 87 } 88 89 // TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0. 90 func TrailingZeros64(x uint64) int { 91 if x == 0 { 92 return 64 93 } 94 // If popcount is fast, replace code below with return popcount(^x & (x - 1)). 95 // 96 // x & -x leaves only the right-most bit set in the word. Let k be the 97 // index of that bit. Since only a single bit is set, the value is two 98 // to the power of k. Multiplying by a power of two is equivalent to 99 // left shifting, in this case by k bits. The de Bruijn (64 bit) constant 100 // is such that all six bit, consecutive substrings are distinct. 101 // Therefore, if we have a left shifted version of this constant we can 102 // find by how many bits it was shifted by looking at which six bit 103 // substring ended up at the top of the word. 104 // (Knuth, volume 4, section 7.3.1) 105 return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)]) 106 } 107 108 // --- OnesCount --- 109 110 const ( 111 m0 = 0x5555555555555555 // 01010101 ... 112 m1 = 0x3333333333333333 // 00110011 ... 113 m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ... 114 m3 = 0x00ff00ff00ff00ff // etc. 115 m4 = 0x0000ffff0000ffff 116 ) 117 118 // OnesCount returns the number of one bits ("population count") in x. 119 func OnesCount(x uint) int { 120 if UintSize == 32 { 121 return OnesCount32(uint32(x)) 122 } 123 return OnesCount64(uint64(x)) 124 } 125 126 // OnesCount8 returns the number of one bits ("population count") in x. 127 func OnesCount8(x uint8) int { 128 return int(pop8tab[x]) 129 } 130 131 // OnesCount16 returns the number of one bits ("population count") in x. 132 func OnesCount16(x uint16) int { 133 return int(pop8tab[x>>8] + pop8tab[x&0xff]) 134 } 135 136 // OnesCount32 returns the number of one bits ("population count") in x. 137 func OnesCount32(x uint32) int { 138 return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff]) 139 } 140 141 // OnesCount64 returns the number of one bits ("population count") in x. 142 func OnesCount64(x uint64) int { 143 // Implementation: Parallel summing of adjacent bits. 144 // See "Hacker's Delight", Chap. 5: Counting Bits. 145 // The following pattern shows the general approach: 146 // 147 // x = x>>1&(m0&m) + x&(m0&m) 148 // x = x>>2&(m1&m) + x&(m1&m) 149 // x = x>>4&(m2&m) + x&(m2&m) 150 // x = x>>8&(m3&m) + x&(m3&m) 151 // x = x>>16&(m4&m) + x&(m4&m) 152 // x = x>>32&(m5&m) + x&(m5&m) 153 // return int(x) 154 // 155 // Masking (& operations) can be left away when there's no 156 // danger that a field's sum will carry over into the next 157 // field: Since the result cannot be > 64, 8 bits is enough 158 // and we can ignore the masks for the shifts by 8 and up. 159 // Per "Hacker's Delight", the first line can be simplified 160 // more, but it saves at best one instruction, so we leave 161 // it alone for clarity. 162 const m = 1<<64 - 1 163 x = x>>1&(m0&m) + x&(m0&m) 164 x = x>>2&(m1&m) + x&(m1&m) 165 x = (x>>4 + x) & (m2 & m) 166 x += x >> 8 167 x += x >> 16 168 x += x >> 32 169 return int(x) & (1<<7 - 1) 170 } 171 172 // --- RotateLeft --- 173 174 // RotateLeft returns the value of x rotated left by (k mod UintSize) bits. 175 // To rotate x right by k bits, call RotateLeft(x, -k). 176 // 177 // This function's execution time does not depend on the inputs. 178 func RotateLeft(x uint, k int) uint { 179 if UintSize == 32 { 180 return uint(RotateLeft32(uint32(x), k)) 181 } 182 return uint(RotateLeft64(uint64(x), k)) 183 } 184 185 // RotateLeft8 returns the value of x rotated left by (k mod 8) bits. 186 // To rotate x right by k bits, call RotateLeft8(x, -k). 187 // 188 // This function's execution time does not depend on the inputs. 189 func RotateLeft8(x uint8, k int) uint8 { 190 const n = 8 191 s := uint(k) & (n - 1) 192 return x<<s | x>>(n-s) 193 } 194 195 // RotateLeft16 returns the value of x rotated left by (k mod 16) bits. 196 // To rotate x right by k bits, call RotateLeft16(x, -k). 197 // 198 // This function's execution time does not depend on the inputs. 199 func RotateLeft16(x uint16, k int) uint16 { 200 const n = 16 201 s := uint(k) & (n - 1) 202 return x<<s | x>>(n-s) 203 } 204 205 // RotateLeft32 returns the value of x rotated left by (k mod 32) bits. 206 // To rotate x right by k bits, call RotateLeft32(x, -k). 207 // 208 // This function's execution time does not depend on the inputs. 209 func RotateLeft32(x uint32, k int) uint32 { 210 const n = 32 211 s := uint(k) & (n - 1) 212 return x<<s | x>>(n-s) 213 } 214 215 // RotateLeft64 returns the value of x rotated left by (k mod 64) bits. 216 // To rotate x right by k bits, call RotateLeft64(x, -k). 217 // 218 // This function's execution time does not depend on the inputs. 219 func RotateLeft64(x uint64, k int) uint64 { 220 const n = 64 221 s := uint(k) & (n - 1) 222 return x<<s | x>>(n-s) 223 } 224 225 // --- Reverse --- 226 227 // Reverse returns the value of x with its bits in reversed order. 228 func Reverse(x uint) uint { 229 if UintSize == 32 { 230 return uint(Reverse32(uint32(x))) 231 } 232 return uint(Reverse64(uint64(x))) 233 } 234 235 // Reverse8 returns the value of x with its bits in reversed order. 236 func Reverse8(x uint8) uint8 { 237 return rev8tab[x] 238 } 239 240 // Reverse16 returns the value of x with its bits in reversed order. 241 func Reverse16(x uint16) uint16 { 242 return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&0xff])<<8 243 } 244 245 // Reverse32 returns the value of x with its bits in reversed order. 246 func Reverse32(x uint32) uint32 { 247 const m = 1<<32 - 1 248 x = x>>1&(m0&m) | x&(m0&m)<<1 249 x = x>>2&(m1&m) | x&(m1&m)<<2 250 x = x>>4&(m2&m) | x&(m2&m)<<4 251 return ReverseBytes32(x) 252 } 253 254 // Reverse64 returns the value of x with its bits in reversed order. 255 func Reverse64(x uint64) uint64 { 256 const m = 1<<64 - 1 257 x = x>>1&(m0&m) | x&(m0&m)<<1 258 x = x>>2&(m1&m) | x&(m1&m)<<2 259 x = x>>4&(m2&m) | x&(m2&m)<<4 260 return ReverseBytes64(x) 261 } 262 263 // --- ReverseBytes --- 264 265 // ReverseBytes returns the value of x with its bytes in reversed order. 266 // 267 // This function's execution time does not depend on the inputs. 268 func ReverseBytes(x uint) uint { 269 if UintSize == 32 { 270 return uint(ReverseBytes32(uint32(x))) 271 } 272 return uint(ReverseBytes64(uint64(x))) 273 } 274 275 // ReverseBytes16 returns the value of x with its bytes in reversed order. 276 // 277 // This function's execution time does not depend on the inputs. 278 func ReverseBytes16(x uint16) uint16 { 279 return x>>8 | x<<8 280 } 281 282 // ReverseBytes32 returns the value of x with its bytes in reversed order. 283 // 284 // This function's execution time does not depend on the inputs. 285 func ReverseBytes32(x uint32) uint32 { 286 const m = 1<<32 - 1 287 x = x>>8&(m3&m) | x&(m3&m)<<8 288 return x>>16 | x<<16 289 } 290 291 // ReverseBytes64 returns the value of x with its bytes in reversed order. 292 // 293 // This function's execution time does not depend on the inputs. 294 func ReverseBytes64(x uint64) uint64 { 295 const m = 1<<64 - 1 296 x = x>>8&(m3&m) | x&(m3&m)<<8 297 x = x>>16&(m4&m) | x&(m4&m)<<16 298 return x>>32 | x<<32 299 } 300 301 // --- Len --- 302 303 // Len returns the minimum number of bits required to represent x; the result is 0 for x == 0. 304 func Len(x uint) int { 305 if UintSize == 32 { 306 return Len32(uint32(x)) 307 } 308 return Len64(uint64(x)) 309 } 310 311 // Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 312 func Len8(x uint8) int { 313 return int(len8tab[x]) 314 } 315 316 // Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 317 func Len16(x uint16) (n int) { 318 if x >= 1<<8 { 319 x >>= 8 320 n = 8 321 } 322 return n + int(len8tab[x]) 323 } 324 325 // Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 326 func Len32(x uint32) (n int) { 327 if x >= 1<<16 { 328 x >>= 16 329 n = 16 330 } 331 if x >= 1<<8 { 332 x >>= 8 333 n += 8 334 } 335 return n + int(len8tab[x]) 336 } 337 338 // Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0. 339 func Len64(x uint64) (n int) { 340 if x >= 1<<32 { 341 x >>= 32 342 n = 32 343 } 344 if x >= 1<<16 { 345 x >>= 16 346 n += 16 347 } 348 if x >= 1<<8 { 349 x >>= 8 350 n += 8 351 } 352 return n + int(len8tab[x]) 353 } 354 355 // --- Add with carry --- 356 357 // Add returns the sum with carry of x, y and carry: sum = x + y + carry. 358 // The carry input must be 0 or 1; otherwise the behavior is undefined. 359 // The carryOut output is guaranteed to be 0 or 1. 360 // 361 // This function's execution time does not depend on the inputs. 362 func Add(x, y, carry uint) (sum, carryOut uint) { 363 if UintSize == 32 { 364 s32, c32 := Add32(uint32(x), uint32(y), uint32(carry)) 365 return uint(s32), uint(c32) 366 } 367 s64, c64 := Add64(uint64(x), uint64(y), uint64(carry)) 368 return uint(s64), uint(c64) 369 } 370 371 // Add32 returns the sum with carry of x, y and carry: sum = x + y + carry. 372 // The carry input must be 0 or 1; otherwise the behavior is undefined. 373 // The carryOut output is guaranteed to be 0 or 1. 374 // 375 // This function's execution time does not depend on the inputs. 376 func Add32(x, y, carry uint32) (sum, carryOut uint32) { 377 sum64 := uint64(x) + uint64(y) + uint64(carry) 378 sum = uint32(sum64) 379 carryOut = uint32(sum64 >> 32) 380 return 381 } 382 383 // Add64 returns the sum with carry of x, y and carry: sum = x + y + carry. 384 // The carry input must be 0 or 1; otherwise the behavior is undefined. 385 // The carryOut output is guaranteed to be 0 or 1. 386 // 387 // This function's execution time does not depend on the inputs. 388 func Add64(x, y, carry uint64) (sum, carryOut uint64) { 389 sum = x + y + carry 390 // The sum will overflow if both top bits are set (x & y) or if one of them 391 // is (x | y), and a carry from the lower place happened. If such a carry 392 // happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum). 393 carryOut = ((x & y) | ((x | y) &^ sum)) >> 63 394 return 395 } 396 397 // --- Subtract with borrow --- 398 399 // Sub returns the difference of x, y and borrow: diff = x - y - borrow. 400 // The borrow input must be 0 or 1; otherwise the behavior is undefined. 401 // The borrowOut output is guaranteed to be 0 or 1. 402 // 403 // This function's execution time does not depend on the inputs. 404 func Sub(x, y, borrow uint) (diff, borrowOut uint) { 405 if UintSize == 32 { 406 d32, b32 := Sub32(uint32(x), uint32(y), uint32(borrow)) 407 return uint(d32), uint(b32) 408 } 409 d64, b64 := Sub64(uint64(x), uint64(y), uint64(borrow)) 410 return uint(d64), uint(b64) 411 } 412 413 // Sub32 returns the difference of x, y and borrow, diff = x - y - borrow. 414 // The borrow input must be 0 or 1; otherwise the behavior is undefined. 415 // The borrowOut output is guaranteed to be 0 or 1. 416 // 417 // This function's execution time does not depend on the inputs. 418 func Sub32(x, y, borrow uint32) (diff, borrowOut uint32) { 419 diff = x - y - borrow 420 // The difference will underflow if the top bit of x is not set and the top 421 // bit of y is set (^x & y) or if they are the same (^(x ^ y)) and a borrow 422 // from the lower place happens. If that borrow happens, the result will be 423 // 1 - 1 - 1 = 0 - 0 - 1 = 1 (& diff). 424 borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 31 425 return 426 } 427 428 // Sub64 returns the difference of x, y and borrow: diff = x - y - borrow. 429 // The borrow input must be 0 or 1; otherwise the behavior is undefined. 430 // The borrowOut output is guaranteed to be 0 or 1. 431 // 432 // This function's execution time does not depend on the inputs. 433 func Sub64(x, y, borrow uint64) (diff, borrowOut uint64) { 434 diff = x - y - borrow 435 // See Sub32 for the bit logic. 436 borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 63 437 return 438 } 439 440 // --- Full-width multiply --- 441 442 // Mul returns the full-width product of x and y: (hi, lo) = x * y 443 // with the product bits' upper half returned in hi and the lower 444 // half returned in lo. 445 // 446 // This function's execution time does not depend on the inputs. 447 func Mul(x, y uint) (hi, lo uint) { 448 if UintSize == 32 { 449 h, l := Mul32(uint32(x), uint32(y)) 450 return uint(h), uint(l) 451 } 452 h, l := Mul64(uint64(x), uint64(y)) 453 return uint(h), uint(l) 454 } 455 456 // Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y 457 // with the product bits' upper half returned in hi and the lower 458 // half returned in lo. 459 // 460 // This function's execution time does not depend on the inputs. 461 func Mul32(x, y uint32) (hi, lo uint32) { 462 tmp := uint64(x) * uint64(y) 463 hi, lo = uint32(tmp>>32), uint32(tmp) 464 return 465 } 466 467 // Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y 468 // with the product bits' upper half returned in hi and the lower 469 // half returned in lo. 470 // 471 // This function's execution time does not depend on the inputs. 472 func Mul64(x, y uint64) (hi, lo uint64) { 473 const mask32 = 1<<32 - 1 474 x0 := x & mask32 475 x1 := x >> 32 476 y0 := y & mask32 477 y1 := y >> 32 478 w0 := x0 * y0 479 t := x1*y0 + w0>>32 480 w1 := t & mask32 481 w2 := t >> 32 482 w1 += x0 * y1 483 hi = x1*y1 + w2 + w1>>32 484 lo = x * y 485 return 486 } 487 488 // --- Full-width divide --- 489 490 // Div returns the quotient and remainder of (hi, lo) divided by y: 491 // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper 492 // half in parameter hi and the lower half in parameter lo. 493 // Div panics for y == 0 (division by zero) or y <= hi (quotient overflow). 494 func Div(hi, lo, y uint) (quo, rem uint) { 495 if UintSize == 32 { 496 q, r := Div32(uint32(hi), uint32(lo), uint32(y)) 497 return uint(q), uint(r) 498 } 499 q, r := Div64(uint64(hi), uint64(lo), uint64(y)) 500 return uint(q), uint(r) 501 } 502 503 // Div32 returns the quotient and remainder of (hi, lo) divided by y: 504 // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper 505 // half in parameter hi and the lower half in parameter lo. 506 // Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow). 507 func Div32(hi, lo, y uint32) (quo, rem uint32) { 508 if y != 0 && y <= hi { 509 panic(overflowError) 510 } 511 if y == 0 { 512 panic(divideError) 513 } 514 z := uint64(hi)<<32 | uint64(lo) 515 quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y)) 516 return 517 } 518 519 // Div64 returns the quotient and remainder of (hi, lo) divided by y: 520 // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper 521 // half in parameter hi and the lower half in parameter lo. 522 // Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow). 523 func Div64(hi, lo, y uint64) (quo, rem uint64) { 524 if y == 0 { 525 panic(divideError) 526 } 527 if y <= hi { 528 panic(overflowError) 529 } 530 531 // If high part is zero, we can directly return the results. 532 if hi == 0 { 533 return lo / y, lo % y 534 } 535 536 s := uint(LeadingZeros64(y)) 537 y <<= s 538 539 const ( 540 two32 = 1 << 32 541 mask32 = two32 - 1 542 ) 543 yn1 := y >> 32 544 yn0 := y & mask32 545 un32 := hi<<s | lo>>(64-s) 546 un10 := lo << s 547 un1 := un10 >> 32 548 un0 := un10 & mask32 549 q1 := un32 / yn1 550 rhat := un32 - q1*yn1 551 552 for q1 >= two32 || q1*yn0 > two32*rhat+un1 { 553 q1-- 554 rhat += yn1 555 if rhat >= two32 { 556 break 557 } 558 } 559 560 un21 := un32*two32 + un1 - q1*y 561 q0 := un21 / yn1 562 rhat = un21 - q0*yn1 563 564 for q0 >= two32 || q0*yn0 > two32*rhat+un0 { 565 q0-- 566 rhat += yn1 567 if rhat >= two32 { 568 break 569 } 570 } 571 572 return q1*two32 + q0, (un21*two32 + un0 - q0*y) >> s 573 } 574 575 // Rem returns the remainder of (hi, lo) divided by y. Rem panics for 576 // y == 0 (division by zero) but, unlike Div, it doesn't panic on a 577 // quotient overflow. 578 func Rem(hi, lo, y uint) uint { 579 if UintSize == 32 { 580 return uint(Rem32(uint32(hi), uint32(lo), uint32(y))) 581 } 582 return uint(Rem64(uint64(hi), uint64(lo), uint64(y))) 583 } 584 585 // Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics 586 // for y == 0 (division by zero) but, unlike Div32, it doesn't panic 587 // on a quotient overflow. 588 func Rem32(hi, lo, y uint32) uint32 { 589 return uint32((uint64(hi)<<32 | uint64(lo)) % uint64(y)) 590 } 591 592 // Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics 593 // for y == 0 (division by zero) but, unlike Div64, it doesn't panic 594 // on a quotient overflow. 595 func Rem64(hi, lo, y uint64) uint64 { 596 // We scale down hi so that hi < y, then use Div64 to compute the 597 // rem with the guarantee that it won't panic on quotient overflow. 598 // Given that 599 // hi ≡ hi%y (mod y) 600 // we have 601 // hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y) 602 _, rem := Div64(hi%y, lo, y) 603 return rem 604 }