github.com/mitghi/x@v0.0.0-20191206171256-71e86edf750d/bit/math.go (about)

     1  /* MIT License
     2  *
     3  * Copyright (c) 2018 Mike Taghavi <mitghi[at]gmail.com>
     4  *
     5  * Permission is hereby granted, free of charge, to any person obtaining a copy
     6  * of this software and associated documentation files (the "Software"), to deal
     7  * in the Software without restriction, including without limitation the rights
     8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  * copies of the Software, and to permit persons to whom the Software is
    10  * furnished to do so, subject to the following conditions:
    11  * The above copyright notice and this permission notice shall be included in all
    12  * copies or substantial portions of the Software.
    13  *
    14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20  * SOFTWARE.
    21   */
    22  
    23  package bit
    24  
    25  type blktable []int
    26  
    27  var (
    28  	mDeBruijnBitPosition [32]int = [32]int{
    29  		0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
    30  		8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31,
    31  	}
    32  
    33  	blocks blktable = blktable{
    34  		8, 8, 8,
    35  		16, 32, 64,
    36  		128, 256, 512,
    37  		1024, 2048, 4096, 8192,
    38  		16384, 32768, 65536, 131072,
    39  		262144, 524288, 1048576, 2097152,
    40  		4194304, 8388608, 16777216,
    41  		33554432, 67108864, 134217728,
    42  		268435456, 536870912, 1073741824,
    43  		2147483648, 4294967296,
    44  	}
    45  )
    46  
    47  func (b blktable) bin(num int) int {
    48  	return int(b.lgb2(uint64(num)))
    49  }
    50  
    51  func (b blktable) size(num int) int {
    52  	return b[int(b.bin(num))]
    53  }
    54  
    55  func (b blktable) lgb2(num uint64) int {
    56  	return mDeBruijnBitPosition[int(uint64(uint32(RoundPrevP2(num)-1)*(uint32)(0x07C4ACDD))>>27)]
    57  }
    58  
    59  func (b blktable) nbalgn(num uint64) int {
    60  	if num == 0 {
    61  		return blocks[0]
    62  	}
    63  	return blocks[b.lgb2(num)]
    64  }
    65  
    66  func RoundNextP2(num uint64) uint64 {
    67  	num--
    68  	num |= num >> 1
    69  	num |= num >> 2
    70  	num |= num >> 4
    71  	num |= num >> 8
    72  	num |= num >> 16
    73  	num |= num >> 32
    74  	num++
    75  	return num
    76  }
    77  
    78  func RoundPrevP2(num uint64) uint64 {
    79  	num--
    80  	num |= num >> 1
    81  	num |= num >> 2
    82  	num |= num >> 4
    83  	num |= num >> 8
    84  	num |= num >> 16
    85  	num |= num >> 32
    86  	num++
    87  	return num - (num >> 1)
    88  }
    89  
    90  func NAlignBits(num uint64) int {
    91  	return blocks.nbalgn(num)
    92  }
    93  
    94  func Lgb2(num uint64) int {
    95  	return blocks.lgb2(num)
    96  }
    97  
    98  // DEV SECTION
    99  
   100  func findslot(n int, wsize int, capacity int) int {
   101  	return ((n * wsize) / capacity) / 2
   102  }