about summary refs log tree commit diff
path: root/vendor/modernc.org/mathutil/bits.go
blob: ab5185442d43f736234d46e9311a001eae010856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2014 The mathutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mathutil // import "modernc.org/mathutil"

import (
	"math/big"
)

// BitLenByte returns the bit width of the non zero part of n.
func BitLenByte(n byte) int {
	return log2[n] + 1
}

// BitLenUint16 returns the bit width of the non zero part of n.
func BitLenUint16(n uint16) int {
	if b := n >> 8; b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[n] + 1
}

// BitLenUint32 returns the bit width of the non zero part of n.
func BitLenUint32(n uint32) int {
	if b := n >> 24; b != 0 {
		return log2[b] + 24 + 1
	}

	if b := n >> 16; b != 0 {
		return log2[b] + 16 + 1
	}

	if b := n >> 8; b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[n] + 1
}

// BitLen returns the bit width of the non zero part of n.
func BitLen(n int) int { // Should handle correctly [future] 64 bit Go ints
	if IntBits == 64 {
		return BitLenUint64(uint64(n))
	}

	if b := byte(n >> 24); b != 0 {
		return log2[b] + 24 + 1
	}

	if b := byte(n >> 16); b != 0 {
		return log2[b] + 16 + 1
	}

	if b := byte(n >> 8); b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[byte(n)] + 1
}

// BitLenUint returns the bit width of the non zero part of n.
func BitLenUint(n uint) int { // Should handle correctly [future] 64 bit Go uints
	if IntBits == 64 {
		return BitLenUint64(uint64(n))
	}

	if b := n >> 24; b != 0 {
		return log2[b] + 24 + 1
	}

	if b := n >> 16; b != 0 {
		return log2[b] + 16 + 1
	}

	if b := n >> 8; b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[n] + 1
}

// BitLenUint64 returns the bit width of the non zero part of n.
func BitLenUint64(n uint64) int {
	if b := n >> 56; b != 0 {
		return log2[b] + 56 + 1
	}

	if b := n >> 48; b != 0 {
		return log2[b] + 48 + 1
	}

	if b := n >> 40; b != 0 {
		return log2[b] + 40 + 1
	}

	if b := n >> 32; b != 0 {
		return log2[b] + 32 + 1
	}

	if b := n >> 24; b != 0 {
		return log2[b] + 24 + 1
	}

	if b := n >> 16; b != 0 {
		return log2[b] + 16 + 1
	}

	if b := n >> 8; b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[n] + 1
}

// BitLenUintptr returns the bit width of the non zero part of n.
func BitLenUintptr(n uintptr) int {
	if b := n >> 56; b != 0 {
		return log2[b] + 56 + 1
	}

	if b := n >> 48; b != 0 {
		return log2[b] + 48 + 1
	}

	if b := n >> 40; b != 0 {
		return log2[b] + 40 + 1
	}

	if b := n >> 32; b != 0 {
		return log2[b] + 32 + 1
	}

	if b := n >> 24; b != 0 {
		return log2[b] + 24 + 1
	}

	if b := n >> 16; b != 0 {
		return log2[b] + 16 + 1
	}

	if b := n >> 8; b != 0 {
		return log2[b] + 8 + 1
	}

	return log2[n] + 1
}

// PopCountByte returns population count of n (number of bits set in n).
func PopCountByte(n byte) int {
	return int(popcnt[n])
}

// PopCountUint16 returns population count of n (number of bits set in n).
func PopCountUint16(n uint16) int {
	return int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
}

// PopCountUint32 returns population count of n (number of bits set in n).
func PopCountUint32(n uint32) int {
	return int(popcnt[byte(n>>24)]) + int(popcnt[byte(n>>16)]) +
		int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
}

// PopCount returns population count of n (number of bits set in n).
func PopCount(n int) int { // Should handle correctly [future] 64 bit Go ints
	if IntBits == 64 {
		return PopCountUint64(uint64(n))
	}

	return PopCountUint32(uint32(n))
}

// PopCountUint returns population count of n (number of bits set in n).
func PopCountUint(n uint) int { // Should handle correctly [future] 64 bit Go uints
	if IntBits == 64 {
		return PopCountUint64(uint64(n))
	}

	return PopCountUint32(uint32(n))
}

// PopCountUintptr returns population count of n (number of bits set in n).
func PopCountUintptr(n uintptr) int {
	if UintPtrBits == 64 {
		return PopCountUint64(uint64(n))
	}

	return PopCountUint32(uint32(n))
}

// PopCountUint64 returns population count of n (number of bits set in n).
func PopCountUint64(n uint64) int {
	return int(popcnt[byte(n>>56)]) + int(popcnt[byte(n>>48)]) +
		int(popcnt[byte(n>>40)]) + int(popcnt[byte(n>>32)]) +
		int(popcnt[byte(n>>24)]) + int(popcnt[byte(n>>16)]) +
		int(popcnt[byte(n>>8)]) + int(popcnt[byte(n)])
}

// PopCountBigInt returns population count of |n| (number of bits set in |n|).
func PopCountBigInt(n *big.Int) (r int) {
	for _, v := range n.Bits() {
		r += PopCountUintptr(uintptr(v))
	}
	return
}