1// Copyright 2017 The Go 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
5//go:generate go run make_tables.go
6
7// Package bits implements bit counting and manipulation
8// functions for the predeclared unsigned integer types.
9package bits
10
11const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
12
13// UintSize is the size of a uint in bits.
14const UintSize = uintSize
15
16// --- LeadingZeros ---
17
18// LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
19func LeadingZeros(x uint) int { return UintSize - Len(x) }
20
21// LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
22func LeadingZeros8(x uint8) int { return 8 - Len8(x) }
23
24// LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
25func LeadingZeros16(x uint16) int { return 16 - Len16(x) }
26
27// LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
28func LeadingZeros32(x uint32) int { return 32 - Len32(x) }
29
30// LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
31func LeadingZeros64(x uint64) int { return 64 - Len64(x) }
32
33// --- TrailingZeros ---
34
35// See http://supertech.csail.mit.edu/papers/debruijn.pdf
36const deBruijn32 = 0x077CB531
37
38var deBruijn32tab = [32]byte{
39	0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
40	31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
41}
42
43const deBruijn64 = 0x03f79d71b4ca8b09
44
45var deBruijn64tab = [64]byte{
46	0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
47	62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
48	63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
49	54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
50}
51
52// TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
53func TrailingZeros(x uint) int {
54	if UintSize == 32 {
55		return TrailingZeros32(uint32(x))
56	}
57	return TrailingZeros64(uint64(x))
58}
59
60// TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
61func TrailingZeros8(x uint8) int {
62	return int(ntz8tab[x])
63}
64
65// TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
66func TrailingZeros16(x uint16) int {
67	if x == 0 {
68		return 16
69	}
70	// see comment in TrailingZeros64
71	return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)])
72}
73
74// TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
75func TrailingZeros32(x uint32) int {
76	if x == 0 {
77		return 32
78	}
79	// see comment in TrailingZeros64
80	return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)])
81}
82
83// TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
84func TrailingZeros64(x uint64) int {
85	if x == 0 {
86		return 64
87	}
88	// If popcount is fast, replace code below with return popcount(^x & (x - 1)).
89	//
90	// x & -x leaves only the right-most bit set in the word. Let k be the
91	// index of that bit. Since only a single bit is set, the value is two
92	// to the power of k. Multiplying by a power of two is equivalent to
93	// left shifting, in this case by k bits. The de Bruijn (64 bit) constant
94	// is such that all six bit, consecutive substrings are distinct.
95	// Therefore, if we have a left shifted version of this constant we can
96	// find by how many bits it was shifted by looking at which six bit
97	// substring ended up at the top of the word.
98	// (Knuth, volume 4, section 7.3.1)
99	return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)])
100}
101
102// --- OnesCount ---
103
104const m0 = 0x5555555555555555 // 01010101 ...
105const m1 = 0x3333333333333333 // 00110011 ...
106const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
107const m3 = 0x00ff00ff00ff00ff // etc.
108const m4 = 0x0000ffff0000ffff
109
110// OnesCount returns the number of one bits ("population count") in x.
111func OnesCount(x uint) int {
112	if UintSize == 32 {
113		return OnesCount32(uint32(x))
114	}
115	return OnesCount64(uint64(x))
116}
117
118// OnesCount8 returns the number of one bits ("population count") in x.
119func OnesCount8(x uint8) int {
120	return int(pop8tab[x])
121}
122
123// OnesCount16 returns the number of one bits ("population count") in x.
124func OnesCount16(x uint16) int {
125	return int(pop8tab[x>>8] + pop8tab[x&0xff])
126}
127
128// OnesCount32 returns the number of one bits ("population count") in x.
129func OnesCount32(x uint32) int {
130	return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff])
131}
132
133// OnesCount64 returns the number of one bits ("population count") in x.
134func OnesCount64(x uint64) int {
135	// Implementation: Parallel summing of adjacent bits.
136	// See "Hacker's Delight", Chap. 5: Counting Bits.
137	// The following pattern shows the general approach:
138	//
139	//   x = x>>1&(m0&m) + x&(m0&m)
140	//   x = x>>2&(m1&m) + x&(m1&m)
141	//   x = x>>4&(m2&m) + x&(m2&m)
142	//   x = x>>8&(m3&m) + x&(m3&m)
143	//   x = x>>16&(m4&m) + x&(m4&m)
144	//   x = x>>32&(m5&m) + x&(m5&m)
145	//   return int(x)
146	//
147	// Masking (& operations) can be left away when there's no
148	// danger that a field's sum will carry over into the next
149	// field: Since the result cannot be > 64, 8 bits is enough
150	// and we can ignore the masks for the shifts by 8 and up.
151	// Per "Hacker's Delight", the first line can be simplified
152	// more, but it saves at best one instruction, so we leave
153	// it alone for clarity.
154	const m = 1<<64 - 1
155	x = x>>1&(m0&m) + x&(m0&m)
156	x = x>>2&(m1&m) + x&(m1&m)
157	x = (x>>4 + x) & (m2 & m)
158	x += x >> 8
159	x += x >> 16
160	x += x >> 32
161	return int(x) & (1<<7 - 1)
162}
163
164// --- RotateLeft ---
165
166// RotateLeft returns the value of x rotated left by (k mod UintSize) bits.
167// To rotate x right by k bits, call RotateLeft(x, -k).
168//
169// This function's execution time does not depend on the inputs.
170func RotateLeft(x uint, k int) uint {
171	if UintSize == 32 {
172		return uint(RotateLeft32(uint32(x), k))
173	}
174	return uint(RotateLeft64(uint64(x), k))
175}
176
177// RotateLeft8 returns the value of x rotated left by (k mod 8) bits.
178// To rotate x right by k bits, call RotateLeft8(x, -k).
179//
180// This function's execution time does not depend on the inputs.
181func RotateLeft8(x uint8, k int) uint8 {
182	const n = 8
183	s := uint(k) & (n - 1)
184	return x<<s | x>>(n-s)
185}
186
187// RotateLeft16 returns the value of x rotated left by (k mod 16) bits.
188// To rotate x right by k bits, call RotateLeft16(x, -k).
189//
190// This function's execution time does not depend on the inputs.
191func RotateLeft16(x uint16, k int) uint16 {
192	const n = 16
193	s := uint(k) & (n - 1)
194	return x<<s | x>>(n-s)
195}
196
197// RotateLeft32 returns the value of x rotated left by (k mod 32) bits.
198// To rotate x right by k bits, call RotateLeft32(x, -k).
199//
200// This function's execution time does not depend on the inputs.
201func RotateLeft32(x uint32, k int) uint32 {
202	const n = 32
203	s := uint(k) & (n - 1)
204	return x<<s | x>>(n-s)
205}
206
207// RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
208// To rotate x right by k bits, call RotateLeft64(x, -k).
209//
210// This function's execution time does not depend on the inputs.
211func RotateLeft64(x uint64, k int) uint64 {
212	const n = 64
213	s := uint(k) & (n - 1)
214	return x<<s | x>>(n-s)
215}
216
217// --- Reverse ---
218
219// Reverse returns the value of x with its bits in reversed order.
220func Reverse(x uint) uint {
221	if UintSize == 32 {
222		return uint(Reverse32(uint32(x)))
223	}
224	return uint(Reverse64(uint64(x)))
225}
226
227// Reverse8 returns the value of x with its bits in reversed order.
228func Reverse8(x uint8) uint8 {
229	return rev8tab[x]
230}
231
232// Reverse16 returns the value of x with its bits in reversed order.
233func Reverse16(x uint16) uint16 {
234	return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&0xff])<<8
235}
236
237// Reverse32 returns the value of x with its bits in reversed order.
238func Reverse32(x uint32) uint32 {
239	const m = 1<<32 - 1
240	x = x>>1&(m0&m) | x&(m0&m)<<1
241	x = x>>2&(m1&m) | x&(m1&m)<<2
242	x = x>>4&(m2&m) | x&(m2&m)<<4
243	return ReverseBytes32(x)
244}
245
246// Reverse64 returns the value of x with its bits in reversed order.
247func Reverse64(x uint64) uint64 {
248	const m = 1<<64 - 1
249	x = x>>1&(m0&m) | x&(m0&m)<<1
250	x = x>>2&(m1&m) | x&(m1&m)<<2
251	x = x>>4&(m2&m) | x&(m2&m)<<4
252	return ReverseBytes64(x)
253}
254
255// --- ReverseBytes ---
256
257// ReverseBytes returns the value of x with its bytes in reversed order.
258//
259// This function's execution time does not depend on the inputs.
260func ReverseBytes(x uint) uint {
261	if UintSize == 32 {
262		return uint(ReverseBytes32(uint32(x)))
263	}
264	return uint(ReverseBytes64(uint64(x)))
265}
266
267// ReverseBytes16 returns the value of x with its bytes in reversed order.
268//
269// This function's execution time does not depend on the inputs.
270func ReverseBytes16(x uint16) uint16 {
271	return x>>8 | x<<8
272}
273
274// ReverseBytes32 returns the value of x with its bytes in reversed order.
275//
276// This function's execution time does not depend on the inputs.
277func ReverseBytes32(x uint32) uint32 {
278	const m = 1<<32 - 1
279	x = x>>8&(m3&m) | x&(m3&m)<<8
280	return x>>16 | x<<16
281}
282
283// ReverseBytes64 returns the value of x with its bytes in reversed order.
284//
285// This function's execution time does not depend on the inputs.
286func ReverseBytes64(x uint64) uint64 {
287	const m = 1<<64 - 1
288	x = x>>8&(m3&m) | x&(m3&m)<<8
289	x = x>>16&(m4&m) | x&(m4&m)<<16
290	return x>>32 | x<<32
291}
292
293// --- Len ---
294
295// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
296func Len(x uint) int {
297	if UintSize == 32 {
298		return Len32(uint32(x))
299	}
300	return Len64(uint64(x))
301}
302
303// Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
304func Len8(x uint8) int {
305	return int(len8tab[x])
306}
307
308// Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
309func Len16(x uint16) (n int) {
310	if x >= 1<<8 {
311		x >>= 8
312		n = 8
313	}
314	return n + int(len8tab[x])
315}
316
317// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
318func Len32(x uint32) (n int) {
319	if x >= 1<<16 {
320		x >>= 16
321		n = 16
322	}
323	if x >= 1<<8 {
324		x >>= 8
325		n += 8
326	}
327	return n + int(len8tab[x])
328}
329
330// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
331func Len64(x uint64) (n int) {
332	if x >= 1<<32 {
333		x >>= 32
334		n = 32
335	}
336	if x >= 1<<16 {
337		x >>= 16
338		n += 16
339	}
340	if x >= 1<<8 {
341		x >>= 8
342		n += 8
343	}
344	return n + int(len8tab[x])
345}
346
347// --- Add with carry ---
348
349// Add returns the sum with carry of x, y and carry: sum = x + y + carry.
350// The carry input must be 0 or 1; otherwise the behavior is undefined.
351// The carryOut output is guaranteed to be 0 or 1.
352//
353// This function's execution time does not depend on the inputs.
354func Add(x, y, carry uint) (sum, carryOut uint) {
355	if UintSize == 32 {
356		s32, c32 := Add32(uint32(x), uint32(y), uint32(carry))
357		return uint(s32), uint(c32)
358	}
359	s64, c64 := Add64(uint64(x), uint64(y), uint64(carry))
360	return uint(s64), uint(c64)
361}
362
363// Add32 returns the sum with carry of x, y and carry: sum = x + y + carry.
364// The carry input must be 0 or 1; otherwise the behavior is undefined.
365// The carryOut output is guaranteed to be 0 or 1.
366//
367// This function's execution time does not depend on the inputs.
368func Add32(x, y, carry uint32) (sum, carryOut uint32) {
369	sum64 := uint64(x) + uint64(y) + uint64(carry)
370	sum = uint32(sum64)
371	carryOut = uint32(sum64 >> 32)
372	return
373}
374
375// Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.
376// The carry input must be 0 or 1; otherwise the behavior is undefined.
377// The carryOut output is guaranteed to be 0 or 1.
378//
379// This function's execution time does not depend on the inputs.
380func Add64(x, y, carry uint64) (sum, carryOut uint64) {
381	sum = x + y + carry
382	// The sum will overflow if both top bits are set (x & y) or if one of them
383	// is (x | y), and a carry from the lower place happened. If such a carry
384	// happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum).
385	carryOut = ((x & y) | ((x | y) &^ sum)) >> 63
386	return
387}
388
389// --- Subtract with borrow ---
390
391// Sub returns the difference of x, y and borrow: diff = x - y - borrow.
392// The borrow input must be 0 or 1; otherwise the behavior is undefined.
393// The borrowOut output is guaranteed to be 0 or 1.
394//
395// This function's execution time does not depend on the inputs.
396func Sub(x, y, borrow uint) (diff, borrowOut uint) {
397	if UintSize == 32 {
398		d32, b32 := Sub32(uint32(x), uint32(y), uint32(borrow))
399		return uint(d32), uint(b32)
400	}
401	d64, b64 := Sub64(uint64(x), uint64(y), uint64(borrow))
402	return uint(d64), uint(b64)
403}
404
405// Sub32 returns the difference of x, y and borrow, diff = x - y - borrow.
406// The borrow input must be 0 or 1; otherwise the behavior is undefined.
407// The borrowOut output is guaranteed to be 0 or 1.
408//
409// This function's execution time does not depend on the inputs.
410func Sub32(x, y, borrow uint32) (diff, borrowOut uint32) {
411	diff = x - y - borrow
412	// The difference will underflow if the top bit of x is not set and the top
413	// bit of y is set (^x & y) or if they are the same (^(x ^ y)) and a borrow
414	// from the lower place happens. If that borrow happens, the result will be
415	// 1 - 1 - 1 = 0 - 0 - 1 = 1 (& diff).
416	borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 31
417	return
418}
419
420// Sub64 returns the difference of x, y and borrow: diff = x - y - borrow.
421// The borrow input must be 0 or 1; otherwise the behavior is undefined.
422// The borrowOut output is guaranteed to be 0 or 1.
423//
424// This function's execution time does not depend on the inputs.
425func Sub64(x, y, borrow uint64) (diff, borrowOut uint64) {
426	diff = x - y - borrow
427	// See Sub32 for the bit logic.
428	borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 63
429	return
430}
431
432// --- Full-width multiply ---
433
434// Mul returns the full-width product of x and y: (hi, lo) = x * y
435// with the product bits' upper half returned in hi and the lower
436// half returned in lo.
437//
438// This function's execution time does not depend on the inputs.
439func Mul(x, y uint) (hi, lo uint) {
440	if UintSize == 32 {
441		h, l := Mul32(uint32(x), uint32(y))
442		return uint(h), uint(l)
443	}
444	h, l := Mul64(uint64(x), uint64(y))
445	return uint(h), uint(l)
446}
447
448// Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y
449// with the product bits' upper half returned in hi and the lower
450// half returned in lo.
451//
452// This function's execution time does not depend on the inputs.
453func Mul32(x, y uint32) (hi, lo uint32) {
454	tmp := uint64(x) * uint64(y)
455	hi, lo = uint32(tmp>>32), uint32(tmp)
456	return
457}
458
459// Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y
460// with the product bits' upper half returned in hi and the lower
461// half returned in lo.
462//
463// This function's execution time does not depend on the inputs.
464func Mul64(x, y uint64) (hi, lo uint64) {
465	const mask32 = 1<<32 - 1
466	x0 := x & mask32
467	x1 := x >> 32
468	y0 := y & mask32
469	y1 := y >> 32
470	w0 := x0 * y0
471	t := x1*y0 + w0>>32
472	w1 := t & mask32
473	w2 := t >> 32
474	w1 += x0 * y1
475	hi = x1*y1 + w2 + w1>>32
476	lo = x * y
477	return
478}
479
480// --- Full-width divide ---
481
482// Div returns the quotient and remainder of (hi, lo) divided by y:
483// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
484// half in parameter hi and the lower half in parameter lo.
485// Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
486func Div(hi, lo, y uint) (quo, rem uint) {
487	if UintSize == 32 {
488		q, r := Div32(uint32(hi), uint32(lo), uint32(y))
489		return uint(q), uint(r)
490	}
491	q, r := Div64(uint64(hi), uint64(lo), uint64(y))
492	return uint(q), uint(r)
493}
494
495// Div32 returns the quotient and remainder of (hi, lo) divided by y:
496// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
497// half in parameter hi and the lower half in parameter lo.
498// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
499func Div32(hi, lo, y uint32) (quo, rem uint32) {
500	if y != 0 && y <= hi {
501		panic(getOverflowError())
502	}
503	z := uint64(hi)<<32 | uint64(lo)
504	quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
505	return
506}
507
508// Div64 returns the quotient and remainder of (hi, lo) divided by y:
509// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
510// half in parameter hi and the lower half in parameter lo.
511// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
512func Div64(hi, lo, y uint64) (quo, rem uint64) {
513	const (
514		two32  = 1 << 32
515		mask32 = two32 - 1
516	)
517	if y == 0 {
518		panic(getDivideError())
519	}
520	if y <= hi {
521		panic(getOverflowError())
522	}
523
524	s := uint(LeadingZeros64(y))
525	y <<= s
526
527	yn1 := y >> 32
528	yn0 := y & mask32
529	un32 := hi<<s | lo>>(64-s)
530	un10 := lo << s
531	un1 := un10 >> 32
532	un0 := un10 & mask32
533	q1 := un32 / yn1
534	rhat := un32 - q1*yn1
535
536	for q1 >= two32 || q1*yn0 > two32*rhat+un1 {
537		q1--
538		rhat += yn1
539		if rhat >= two32 {
540			break
541		}
542	}
543
544	un21 := un32*two32 + un1 - q1*y
545	q0 := un21 / yn1
546	rhat = un21 - q0*yn1
547
548	for q0 >= two32 || q0*yn0 > two32*rhat+un0 {
549		q0--
550		rhat += yn1
551		if rhat >= two32 {
552			break
553		}
554	}
555
556	return q1*two32 + q0, (un21*two32 + un0 - q0*y) >> s
557}
558
559// Rem returns the remainder of (hi, lo) divided by y. Rem panics for
560// y == 0 (division by zero) but, unlike Div, it doesn't panic on a
561// quotient overflow.
562func Rem(hi, lo, y uint) uint {
563	if UintSize == 32 {
564		return uint(Rem32(uint32(hi), uint32(lo), uint32(y)))
565	}
566	return uint(Rem64(uint64(hi), uint64(lo), uint64(y)))
567}
568
569// Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics
570// for y == 0 (division by zero) but, unlike Div32, it doesn't panic
571// on a quotient overflow.
572func Rem32(hi, lo, y uint32) uint32 {
573	return uint32((uint64(hi)<<32 | uint64(lo)) % uint64(y))
574}
575
576// Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics
577// for y == 0 (division by zero) but, unlike Div64, it doesn't panic
578// on a quotient overflow.
579func Rem64(hi, lo, y uint64) uint64 {
580	// We scale down hi so that hi < y, then use Div64 to compute the
581	// rem with the guarantee that it won't panic on quotient overflow.
582	// Given that
583	//   hi ≡ hi%y    (mod y)
584	// we have
585	//   hi<<64 + lo ≡ (hi%y)<<64 + lo    (mod y)
586	_, rem := Div64(hi%y, lo, y)
587	return rem
588}
589