1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package pmath
- const (
- bitsize = 32 << (^uint(0) >> 63)
- maxint = int(1<<(bitsize-1) - 1)
- maxintHeadBit = 1 << (bitsize - 2)
- )
- // LogarithmicRange iterates from ceiled to power of two min to max,
- // calling cb on each iteration.
- func LogarithmicRange(min, max int, cb func(int)) {
- if min == 0 {
- min = 1
- }
- for n := CeilToPowerOfTwo(min); n <= max; n <<= 1 {
- cb(n)
- }
- }
- // IsPowerOfTwo reports whether given integer is a power of two.
- func IsPowerOfTwo(n int) bool {
- return n&(n-1) == 0
- }
- // Identity is identity.
- func Identity(n int) int {
- return n
- }
- // CeilToPowerOfTwo returns the least power of two integer value greater than
- // or equal to n.
- func CeilToPowerOfTwo(n int) int {
- if n&maxintHeadBit != 0 && n > maxintHeadBit {
- panic("argument is too large")
- }
- if n <= 2 {
- return n
- }
- n--
- n = fillBits(n)
- n++
- return n
- }
- // FloorToPowerOfTwo returns the greatest power of two integer value less than
- // or equal to n.
- func FloorToPowerOfTwo(n int) int {
- if n <= 2 {
- return n
- }
- n = fillBits(n)
- n >>= 1
- n++
- return n
- }
- func fillBits(n int) int {
- n |= n >> 1
- n |= n >> 2
- n |= n >> 4
- n |= n >> 8
- n |= n >> 16
- n |= n >> 32
- return n
- }
|