bits.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright (c) 2014 The mathutil 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. package mathutil
  5. import (
  6. "math/big"
  7. )
  8. // BitLenByte returns the bit width of the non zero part of n.
  9. func BitLenByte(n byte) int {
  10. return log2[n] + 1
  11. }
  12. // BitLenUint16 returns the bit width of the non zero part of n.
  13. func BitLenUint16(n uint16) int {
  14. if b := n >> 8; b != 0 {
  15. return log2[b] + 8 + 1
  16. }
  17. return log2[n] + 1
  18. }
  19. // BitLenUint32 returns the bit width of the non zero part of n.
  20. func BitLenUint32(n uint32) int {
  21. if b := n >> 24; b != 0 {
  22. return log2[b] + 24 + 1
  23. }
  24. if b := n >> 16; b != 0 {
  25. return log2[b] + 16 + 1
  26. }
  27. if b := n >> 8; b != 0 {
  28. return log2[b] + 8 + 1
  29. }
  30. return log2[n] + 1
  31. }
  32. // BitLen returns the bit width of the non zero part of n.
  33. func BitLen(n int) int { // Should handle correctly [future] 64 bit Go ints
  34. if IntBits == 64 {
  35. return BitLenUint64(uint64(n))
  36. }
  37. if b := byte(n >> 24); b != 0 {
  38. return log2[b] + 24 + 1
  39. }
  40. if b := byte(n >> 16); b != 0 {
  41. return log2[b] + 16 + 1
  42. }
  43. if b := byte(n >> 8); b != 0 {
  44. return log2[b] + 8 + 1
  45. }
  46. return log2[byte(n)] + 1
  47. }
  48. // BitLenUint returns the bit width of the non zero part of n.
  49. func BitLenUint(n uint) int { // Should handle correctly [future] 64 bit Go uints
  50. if IntBits == 64 {
  51. return BitLenUint64(uint64(n))
  52. }
  53. if b := n >> 24; b != 0 {
  54. return log2[b] + 24 + 1
  55. }
  56. if b := n >> 16; b != 0 {
  57. return log2[b] + 16 + 1
  58. }
  59. if b := n >> 8; b != 0 {
  60. return log2[b] + 8 + 1
  61. }
  62. return log2[n] + 1
  63. }
  64. // BitLenUint64 returns the bit width of the non zero part of n.
  65. func BitLenUint64(n uint64) int {
  66. if b := n >> 56; b != 0 {
  67. return log2[b] + 56 + 1
  68. }
  69. if b := n >> 48; b != 0 {
  70. return log2[b] + 48 + 1
  71. }
  72. if b := n >> 40; b != 0 {
  73. return log2[b] + 40 + 1
  74. }
  75. if b := n >> 32; b != 0 {
  76. return log2[b] + 32 + 1
  77. }
  78. if b := n >> 24; b != 0 {
  79. return log2[b] + 24 + 1
  80. }
  81. if b := n >> 16; b != 0 {
  82. return log2[b] + 16 + 1
  83. }
  84. if b := n >> 8; b != 0 {
  85. return log2[b] + 8 + 1
  86. }
  87. return log2[n] + 1
  88. }
  89. // BitLenUintptr returns the bit width of the non zero part of n.
  90. func BitLenUintptr(n uintptr) int {
  91. if b := n >> 56; b != 0 {
  92. return log2[b] + 56 + 1
  93. }
  94. if b := n >> 48; b != 0 {
  95. return log2[b] + 48 + 1
  96. }
  97. if b := n >> 40; b != 0 {
  98. return log2[b] + 40 + 1
  99. }
  100. if b := n >> 32; b != 0 {
  101. return log2[b] + 32 + 1
  102. }
  103. if b := n >> 24; b != 0 {
  104. return log2[b] + 24 + 1
  105. }
  106. if b := n >> 16; b != 0 {
  107. return log2[b] + 16 + 1
  108. }
  109. if b := n >> 8; b != 0 {
  110. return log2[b] + 8 + 1
  111. }
  112. return log2[n] + 1
  113. }
  114. // PopCountByte returns population count of n (number of bits set in n).
  115. func PopCountByte(n byte) int {
  116. return int(popcnt[n])
  117. }
  118. // PopCountUint16 returns population count of n (number of bits set in n).
  119. func PopCountUint16(n uint16) int {
  120. return int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
  121. }
  122. // PopCountUint32 returns population count of n (number of bits set in n).
  123. func PopCountUint32(n uint32) int {
  124. return int(popcnt[byte(n>>24)]) + int(popcnt[byte(n>>16)]) +
  125. int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
  126. }
  127. // PopCount returns population count of n (number of bits set in n).
  128. func PopCount(n int) int { // Should handle correctly [future] 64 bit Go ints
  129. if IntBits == 64 {
  130. return PopCountUint64(uint64(n))
  131. }
  132. return PopCountUint32(uint32(n))
  133. }
  134. // PopCountUint returns population count of n (number of bits set in n).
  135. func PopCountUint(n uint) int { // Should handle correctly [future] 64 bit Go uints
  136. if IntBits == 64 {
  137. return PopCountUint64(uint64(n))
  138. }
  139. return PopCountUint32(uint32(n))
  140. }
  141. // PopCountUintptr returns population count of n (number of bits set in n).
  142. func PopCountUintptr(n uintptr) int {
  143. if UintPtrBits == 64 {
  144. return PopCountUint64(uint64(n))
  145. }
  146. return PopCountUint32(uint32(n))
  147. }
  148. // PopCountUint64 returns population count of n (number of bits set in n).
  149. func PopCountUint64(n uint64) int {
  150. return int(popcnt[byte(n>>56)]) + int(popcnt[byte(n>>48)]) +
  151. int(popcnt[byte(n>>40)]) + int(popcnt[byte(n>>32)]) +
  152. int(popcnt[byte(n>>24)]) + int(popcnt[byte(n>>16)]) +
  153. int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
  154. }
  155. // PopCountBigInt returns population count of |n| (number of bits set in |n|).
  156. func PopCountBigInt(n *big.Int) (r int) {
  157. for _, v := range n.Bits() {
  158. r += PopCountUintptr(uintptr(v))
  159. }
  160. return
  161. }