-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitops.go
393 lines (320 loc) · 12.2 KB
/
bitops.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Package bitops provide a set of bit operations for uint32/uint64. They are similar to
what you can find in conventional C library.
*/
package bitops
import "fmt"
// real implementation for Extract32 and GetField32
func extract32(value uint32, start uint, length uint) (uint32, error) {
return (value >> start) & ( ^uint32(0) >> (32 - length)), nil
}
// real implementation for Extract64 and GetField64
func extract64(value uint64, start uint, length uint) (uint64, error) {
return (value >> start) & ( ^uint64(0) >> (64 - length)), nil
}
// Extract32 specify field from uint32 by staring position and length
// LSB/MSB are 0/31 and return original value if error occurs
func Extract32(value uint32, start uint, length uint) (uint32, error) {
if (start > 31 || length > 32 - start ) {
return value, fmt.Errorf("invalid start(%v) or length(%v)", start, length);
}
return extract32(value, start, length)
}
// Extract64 specify field from uint64 by starting position and length
// LSB/MSB are 0/63 and return original value if error occurs
func Extract64(value uint64, start uint, length uint) (uint64, error) {
if (start > 63 || length > 64 - start ) {
return value, fmt.Errorf("invalid start(%v) or length(%v)", start, length);
}
return extract64(value, start, length)
}
// GetField32 specify field between high and low bit from uint32
// LSB/MSB are 0/31 and return original value if error occurs
func GetField32(value uint32, high uint, low uint) (uint32, error) {
if (high > 31 || low > 31 || high < low) {
return value, fmt.Errorf("invalid high(%v) or low(%v)", high, low);
}
return extract32(value, low, high - low + 1)
}
// GetField64 specify field between high and low bit from uint64
// LSB/MSB are 0/63 and return original value if error occurs
func GetField64(value uint64, high uint, low uint) (uint64, error) {
if (high > 63 || low > 63 || high < low) {
return value, fmt.Errorf("invalid high(%v) or low(%v)", high, low);
}
return extract64(value, low, high - low + 1)
}
// real implementation for Depoit32 and SetField32
func deposit32(value uint32, start uint, length uint, field uint32) (uint32, error) {
mask := (^uint32(0) >> (32 - length)) << start;
return (value & ^mask) | ((field << start) & mask), nil;
}
// real implementation for Depoit64 and SetField64
func deposit64(value uint64, start uint, length uint, field uint64) (uint64, error) {
mask := (^uint64(0) >> (64 - length)) << start;
return (value & ^mask) | ((field << start) & mask), nil;
}
// Deposit32 specified field to uint32 variable by staring position and length
// LSB/MSB are 0/31 and return original value if error occurs
func Deposit32(value uint32, start uint, length uint, field uint32) (uint32, error) {
if start > 31 || length > 32 - start {
return value, fmt.Errorf("invalid start(%v) or length(%v)", start, length);
}
return deposit32(value, start, length, field)
}
// Deposit64 specified field to uint64 variable by staring position and length
// LSB/MSB are 0/63 and return original value if error occurs
func Deposit64(value uint64, start uint, length uint, field uint64) (uint64, error) {
if start > 63 || length > 64 - start {
return value, fmt.Errorf("invalid start(%v) or length(%v)", start, length);
}
return deposit64(value, start, length, field)
}
// SetField32 specified field to uint32 variable by staring position and length
// LSB/MSB are 0/31 and return original value if error occurs
func SetField32(value uint32, high uint, low uint, field uint32) (uint32, error) {
if high > 31 || low > 31 || high < low {
return value, fmt.Errorf("invalid high(%v) or low(%v)", high, low);
}
return deposit32(value, low, high - low + 1, field)
}
// SetField64 specified field to uint64 variable by staring position and length
// LSB/MSB are 0/63 and return original value if error occurs
func SetField64(value uint64, high uint, low uint, field uint64) (uint64, error) {
if high > 63 || low > 63 || high < low {
return value, fmt.Errorf("invalid high(%v) or low(%v)", high, low);
}
return deposit64(value, low, high - low + 1, field)
}
// CountOne8 return number of 1 in uint8 variable
func CountOne8(value uint8) (uint) {
value = (value & 0x55) + ((value >> 1) & 0x55);
value = (value & 0x33) + ((value >> 2) & 0x33);
value = (value & 0x0f) + ((value >> 4) & 0x0f);
return uint(value);
}
// CountOne16 return number of 1 in uint16 variable
func CountOne16(value uint16) (uint) {
value = (value & 0x5555) + ((value >> 1) & 0x5555);
value = (value & 0x3333) + ((value >> 2) & 0x3333);
value = (value & 0x0f0f) + ((value >> 4) & 0x0f0f);
value = (value & 0x00ff) + ((value >> 8) & 0x00ff);
return uint(value);
}
// CountOne32 return number of 1 in uint32 variable
func CountOne32(value uint32) (uint) {
value = (value & 0x55555555) + ((value >> 1) & 0x55555555);
value = (value & 0x33333333) + ((value >> 2) & 0x33333333);
value = (value & 0x0f0f0f0f) + ((value >> 4) & 0x0f0f0f0f);
value = (value & 0x00ff00ff) + ((value >> 8) & 0x00ff00ff);
value = (value & 0x0000ffff) + ((value >> 16) & 0x0000ffff);
return uint(value);
}
// CountOne64 return number of 1 in uint64 variable
func CountOne64(value uint64) (uint) {
value = (value & 0x5555555555555555) + ((value >> 1) & 0x5555555555555555);
value = (value & 0x3333333333333333) + ((value >> 2) & 0x3333333333333333);
value = (value & 0x0f0f0f0f0f0f0f0f) + ((value >> 4) & 0x0f0f0f0f0f0f0f0f);
value = (value & 0x00ff00ff00ff00ff) + ((value >> 8) & 0x00ff00ff00ff00ff);
value = (value & 0x0000ffff0000ffff) + ((value >> 16) & 0x0000ffff0000ffff);
value = (value & 0x00000000ffffffff) + ((value >> 32) & 0x00000000ffffffff);
return uint(value);
}
// CountOne8 return number of 0 in uint8 variable
func CountZero8(value uint8) (uint) {
return 8 - CountOne8(value)
}
// CountOne16 return number of 0 in uint16 variable
func CountZero16(value uint16) (uint) {
return 16 - CountOne16(value)
}
// CountOne32 return number of 0 in uint32 variable
func CountZero32(value uint32) (uint) {
return 32 - CountOne32(value)
}
// CountOne64 return number of 0 in uint64 variable
func CountZero64(value uint64) (uint) {
return 64 - CountOne64(value)
}
// CountTrailZero32 return number of trailing zero in a 32-bit value
func CountTrailZero32(value uint32) (uint) {
var count uint = 0
if (value & 0x0000FFFF) == 0 {
count += 16;
value >>= 16;
}
if (value & 0x000000FF) == 0 {
count += 8;
value >>= 8;
}
if (value & 0x0000000F) == 0 {
count += 4;
value >>= 4;
}
if (value & 0x00000003) == 0 {
count += 2;
value >>= 2;
}
if (value & 0x00000001) == 0 {
count++;
value >>= 1;
}
if (value & 0x00000001) == 0 {
count++;
}
return count
}
// CountTrailZero64 return number of trailing zero in a 32-bit value
func CountTrailZero64(value uint64) (uint) {
var count uint = 0
if uint32(value) == 0 {
count += 32;
value >>= 32;
}
return count + CountTrailZero32(uint32(value))
}
// CountTrailOne32 return number of trailing 1 in a 32-bit value
func CountTrailOne32(value uint32) (uint) {
return CountTrailZero32(^value)
}
// CountTrailOne64 return number of trailing 1 in a 32-bit value
func CountTrailOne64(value uint64) (uint) {
return CountTrailZero64(^value)
}
// CountLeadZero32 return number of leading 0 in a 32-bit value
func CountLeadZero32(value uint32) (uint) {
var count uint = 0
if (value & 0xFFFF0000) == 0 {
count += 16;
value <<= 16;
}
if (value & 0xFF000000) == 0 {
count += 8;
value <<= 8;
}
if (value & 0xF0000000) == 0 {
count += 4;
value <<= 4;
}
if (value & 0x30000000) == 0 {
count += 2;
value <<= 2;
}
if (value & 0x10000000) == 0 {
count++;
value <<= 1;
}
if (value & 0x10000000) == 0 {
count++;
}
return count
}
// CountLeadZero64 return number of leading 0 in a 32-bit value
func CountLeadZero64(value uint64) (uint) {
var count uint = 0
if (value >> 32) == 0 {
count += 32
} else {
value >>= 32;
}
return count + CountLeadZero32(uint32(value))
}
// CountLeadOne32 return number of leading 1 in a 32-bit value
func CountLeadOne32(value uint32) (uint) {
return CountLeadZero32(^value)
}
// CountLeadOne64 return number of leading 1 in a 32-bit value
func CountLeadOne64(value uint64) (uint) {
return CountLeadZero64(^value)
}
// SetBit32 set the specified bit to 1 for 32-bit value and return the new value
func SetBit32(value uint32, pos uint) (uint32, error) {
if pos >= 32 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value | (uint32(1) << pos)), nil
}
// SetBit64 set the specified bit to 1 for 64-bit value and return the new value
func SetBit64(value uint64, pos uint) (uint64, error) {
if pos >= 64 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value | (uint64(1) << pos)), nil
}
// ToggleBit32 set the specified bit to 1 for 32-bit value and return the new value
func ToggleBit32(value uint32, pos uint) (uint32, error) {
if pos >= 32 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value ^ (uint32(1) << pos)), nil
}
// ToggleBit64 set the specified bit to 1 for 64-bit value and return the new value
func ToggleBit64(value uint64, pos uint) (uint64, error) {
if pos >= 64 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value ^ (uint64(1) << pos)), nil
}
// ClearBit32 set the specified bit to 1 for 32-bit value and return the new value
func ClearBit32(value uint32, pos uint) (uint32, error) {
if pos >= 32 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value &^ (uint32(1) << pos)), nil
}
// ClearBit64 set the specified bit to 1 for 64-bit value and return the new value
func ClearBit64(value uint64, pos uint) (uint64, error) {
if pos >= 64 {
return value, fmt.Errorf("invalid position(%v)", pos)
}
return (value &^ (uint64(1) << pos)), nil
}
// TestBit32 set the specified bit to 1 for 32-bit value and return the new value
func TestBit32(value uint32, pos uint) (bool, error) {
if pos >= 32 {
return false, fmt.Errorf("invalid position(%v)", pos)
}
return (value & (uint32(1) << pos)) != 0, nil
}
// TestBit64 set the specified bit to 1 for 64-bit value and return the new value
func TestBit64(value uint64, pos uint) (bool, error) {
if pos >= 64 {
return false, fmt.Errorf("invalid position(%v)", pos)
}
return (value & (uint64(1) << pos)) != 0, nil
}
// Reverse32 set reverse the bit order for 32-bit variable
func Reverse32(value uint32) (uint32) {
value = ((value >> 1) & 0x55555555) | ((value & 0x55555555) << 1)
value = ((value >> 2) & 0x33333333) | ((value & 0x33333333) << 2)
value = ((value >> 4) & 0x0F0F0F0F) | ((value & 0x0F0F0F0F) << 4)
value = ((value >> 8) & 0x00FF00FF) | ((value & 0x00FF00FF) << 8)
value = ( value >> 16 ) | ( value << 16)
return value
}
// Reverse64 set reverse the bit order for 64-bit variable
func Reverse64(value uint64) (uint64) {
high := Reverse32(uint32(value))
low := Reverse32(uint32(value >> 32))
return (uint64(high) << 32) | uint64(low)
}
// RotateRight32 rotate an 32-bit value right
func RotateRight32(value uint32, shift uint) (uint32) {
shift = shift & 0x1F
return (value >> shift) | (value << (32 - shift))
}
// RotateLeft32 rotate an 32-bit value left
func RotateLeft32(value uint32, shift uint) (uint32){
shift = shift & 0x1F
return (value << shift) | (value >> (32 - shift))
}
// RotateRight64 rotate an 64-bit value right
func RotateRight64(value uint64, shift uint) (uint64){
shift = shift & 0x3F
return (value >> shift) | (value << (64 - shift))
}
// RotateLeft64 rotate an 64-bit value left
func RotateLeft64(value uint64, shift uint) (uint64){
shift = shift & 0x3F
return (value << shift) | (value >> (64 - shift))
}