summaryrefslogtreecommitdiffstats
path: root/src/math/bits/bits_test.go
blob: 23b4539fcd6ca4d374b96f4d372b777cb09e8219 (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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
// Copyright 2017 The Go 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 bits_test

import (
	. "math/bits"
	"runtime"
	"testing"
	"unsafe"
)

func TestUintSize(t *testing.T) {
	var x uint
	if want := unsafe.Sizeof(x) * 8; UintSize != want {
		t.Fatalf("UintSize = %d; want %d", UintSize, want)
	}
}

func TestLeadingZeros(t *testing.T) {
	for i := 0; i < 256; i++ {
		nlz := tab[i].nlz
		for k := 0; k < 64-8; k++ {
			x := uint64(i) << uint(k)
			if x <= 1<<8-1 {
				got := LeadingZeros8(uint8(x))
				want := nlz - k + (8 - 8)
				if x == 0 {
					want = 8
				}
				if got != want {
					t.Fatalf("LeadingZeros8(%#02x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<16-1 {
				got := LeadingZeros16(uint16(x))
				want := nlz - k + (16 - 8)
				if x == 0 {
					want = 16
				}
				if got != want {
					t.Fatalf("LeadingZeros16(%#04x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<32-1 {
				got := LeadingZeros32(uint32(x))
				want := nlz - k + (32 - 8)
				if x == 0 {
					want = 32
				}
				if got != want {
					t.Fatalf("LeadingZeros32(%#08x) == %d; want %d", x, got, want)
				}
				if UintSize == 32 {
					got = LeadingZeros(uint(x))
					if got != want {
						t.Fatalf("LeadingZeros(%#08x) == %d; want %d", x, got, want)
					}
				}
			}

			if x <= 1<<64-1 {
				got := LeadingZeros64(uint64(x))
				want := nlz - k + (64 - 8)
				if x == 0 {
					want = 64
				}
				if got != want {
					t.Fatalf("LeadingZeros64(%#016x) == %d; want %d", x, got, want)
				}
				if UintSize == 64 {
					got = LeadingZeros(uint(x))
					if got != want {
						t.Fatalf("LeadingZeros(%#016x) == %d; want %d", x, got, want)
					}
				}
			}
		}
	}
}

// Exported (global) variable serving as input for some
// of the benchmarks to ensure side-effect free calls
// are not optimized away.
var Input uint64 = DeBruijn64

// Exported (global) variable to store function results
// during benchmarking to ensure side-effect free calls
// are not optimized away.
var Output int

func BenchmarkLeadingZeros(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += LeadingZeros(uint(Input) >> (uint(i) % UintSize))
	}
	Output = s
}

func BenchmarkLeadingZeros8(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += LeadingZeros8(uint8(Input) >> (uint(i) % 8))
	}
	Output = s
}

func BenchmarkLeadingZeros16(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += LeadingZeros16(uint16(Input) >> (uint(i) % 16))
	}
	Output = s
}

func BenchmarkLeadingZeros32(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += LeadingZeros32(uint32(Input) >> (uint(i) % 32))
	}
	Output = s
}

func BenchmarkLeadingZeros64(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += LeadingZeros64(uint64(Input) >> (uint(i) % 64))
	}
	Output = s
}

func TestTrailingZeros(t *testing.T) {
	for i := 0; i < 256; i++ {
		ntz := tab[i].ntz
		for k := 0; k < 64-8; k++ {
			x := uint64(i) << uint(k)
			want := ntz + k
			if x <= 1<<8-1 {
				got := TrailingZeros8(uint8(x))
				if x == 0 {
					want = 8
				}
				if got != want {
					t.Fatalf("TrailingZeros8(%#02x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<16-1 {
				got := TrailingZeros16(uint16(x))
				if x == 0 {
					want = 16
				}
				if got != want {
					t.Fatalf("TrailingZeros16(%#04x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<32-1 {
				got := TrailingZeros32(uint32(x))
				if x == 0 {
					want = 32
				}
				if got != want {
					t.Fatalf("TrailingZeros32(%#08x) == %d; want %d", x, got, want)
				}
				if UintSize == 32 {
					got = TrailingZeros(uint(x))
					if got != want {
						t.Fatalf("TrailingZeros(%#08x) == %d; want %d", x, got, want)
					}
				}
			}

			if x <= 1<<64-1 {
				got := TrailingZeros64(uint64(x))
				if x == 0 {
					want = 64
				}
				if got != want {
					t.Fatalf("TrailingZeros64(%#016x) == %d; want %d", x, got, want)
				}
				if UintSize == 64 {
					got = TrailingZeros(uint(x))
					if got != want {
						t.Fatalf("TrailingZeros(%#016x) == %d; want %d", x, got, want)
					}
				}
			}
		}
	}
}

func BenchmarkTrailingZeros(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += TrailingZeros(uint(Input) << (uint(i) % UintSize))
	}
	Output = s
}

func BenchmarkTrailingZeros8(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += TrailingZeros8(uint8(Input) << (uint(i) % 8))
	}
	Output = s
}

func BenchmarkTrailingZeros16(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += TrailingZeros16(uint16(Input) << (uint(i) % 16))
	}
	Output = s
}

func BenchmarkTrailingZeros32(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += TrailingZeros32(uint32(Input) << (uint(i) % 32))
	}
	Output = s
}

func BenchmarkTrailingZeros64(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += TrailingZeros64(uint64(Input) << (uint(i) % 64))
	}
	Output = s
}

func TestOnesCount(t *testing.T) {
	var x uint64
	for i := 0; i <= 64; i++ {
		testOnesCount(t, x, i)
		x = x<<1 | 1
	}

	for i := 64; i >= 0; i-- {
		testOnesCount(t, x, i)
		x = x << 1
	}

	for i := 0; i < 256; i++ {
		for k := 0; k < 64-8; k++ {
			testOnesCount(t, uint64(i)<<uint(k), tab[i].pop)
		}
	}
}

func testOnesCount(t *testing.T, x uint64, want int) {
	if x <= 1<<8-1 {
		got := OnesCount8(uint8(x))
		if got != want {
			t.Fatalf("OnesCount8(%#02x) == %d; want %d", uint8(x), got, want)
		}
	}

	if x <= 1<<16-1 {
		got := OnesCount16(uint16(x))
		if got != want {
			t.Fatalf("OnesCount16(%#04x) == %d; want %d", uint16(x), got, want)
		}
	}

	if x <= 1<<32-1 {
		got := OnesCount32(uint32(x))
		if got != want {
			t.Fatalf("OnesCount32(%#08x) == %d; want %d", uint32(x), got, want)
		}
		if UintSize == 32 {
			got = OnesCount(uint(x))
			if got != want {
				t.Fatalf("OnesCount(%#08x) == %d; want %d", uint32(x), got, want)
			}
		}
	}

	if x <= 1<<64-1 {
		got := OnesCount64(uint64(x))
		if got != want {
			t.Fatalf("OnesCount64(%#016x) == %d; want %d", x, got, want)
		}
		if UintSize == 64 {
			got = OnesCount(uint(x))
			if got != want {
				t.Fatalf("OnesCount(%#016x) == %d; want %d", x, got, want)
			}
		}
	}
}

func BenchmarkOnesCount(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += OnesCount(uint(Input))
	}
	Output = s
}

func BenchmarkOnesCount8(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += OnesCount8(uint8(Input))
	}
	Output = s
}

func BenchmarkOnesCount16(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += OnesCount16(uint16(Input))
	}
	Output = s
}

func BenchmarkOnesCount32(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += OnesCount32(uint32(Input))
	}
	Output = s
}

func BenchmarkOnesCount64(b *testing.B) {
	var s int
	for i := 0; i < b.N; i++ {
		s += OnesCount64(uint64(Input))
	}
	Output = s
}

func TestRotateLeft(t *testing.T) {
	var m uint64 = DeBruijn64

	for k := uint(0); k < 128; k++ {
		x8 := uint8(m)
		got8 := RotateLeft8(x8, int(k))
		want8 := x8<<(k&0x7) | x8>>(8-k&0x7)
		if got8 != want8 {
			t.Fatalf("RotateLeft8(%#02x, %d) == %#02x; want %#02x", x8, k, got8, want8)
		}
		got8 = RotateLeft8(want8, -int(k))
		if got8 != x8 {
			t.Fatalf("RotateLeft8(%#02x, -%d) == %#02x; want %#02x", want8, k, got8, x8)
		}

		x16 := uint16(m)
		got16 := RotateLeft16(x16, int(k))
		want16 := x16<<(k&0xf) | x16>>(16-k&0xf)
		if got16 != want16 {
			t.Fatalf("RotateLeft16(%#04x, %d) == %#04x; want %#04x", x16, k, got16, want16)
		}
		got16 = RotateLeft16(want16, -int(k))
		if got16 != x16 {
			t.Fatalf("RotateLeft16(%#04x, -%d) == %#04x; want %#04x", want16, k, got16, x16)
		}

		x32 := uint32(m)
		got32 := RotateLeft32(x32, int(k))
		want32 := x32<<(k&0x1f) | x32>>(32-k&0x1f)
		if got32 != want32 {
			t.Fatalf("RotateLeft32(%#08x, %d) == %#08x; want %#08x", x32, k, got32, want32)
		}
		got32 = RotateLeft32(want32, -int(k))
		if got32 != x32 {
			t.Fatalf("RotateLeft32(%#08x, -%d) == %#08x; want %#08x", want32, k, got32, x32)
		}
		if UintSize == 32 {
			x := uint(m)
			got := RotateLeft(x, int(k))
			want := x<<(k&0x1f) | x>>(32-k&0x1f)
			if got != want {
				t.Fatalf("RotateLeft(%#08x, %d) == %#08x; want %#08x", x, k, got, want)
			}
			got = RotateLeft(want, -int(k))
			if got != x {
				t.Fatalf("RotateLeft(%#08x, -%d) == %#08x; want %#08x", want, k, got, x)
			}
		}

		x64 := uint64(m)
		got64 := RotateLeft64(x64, int(k))
		want64 := x64<<(k&0x3f) | x64>>(64-k&0x3f)
		if got64 != want64 {
			t.Fatalf("RotateLeft64(%#016x, %d) == %#016x; want %#016x", x64, k, got64, want64)
		}
		got64 = RotateLeft64(want64, -int(k))
		if got64 != x64 {
			t.Fatalf("RotateLeft64(%#016x, -%d) == %#016x; want %#016x", want64, k, got64, x64)
		}
		if UintSize == 64 {
			x := uint(m)
			got := RotateLeft(x, int(k))
			want := x<<(k&0x3f) | x>>(64-k&0x3f)
			if got != want {
				t.Fatalf("RotateLeft(%#016x, %d) == %#016x; want %#016x", x, k, got, want)
			}
			got = RotateLeft(want, -int(k))
			if got != x {
				t.Fatalf("RotateLeft(%#08x, -%d) == %#08x; want %#08x", want, k, got, x)
			}
		}
	}
}

func BenchmarkRotateLeft(b *testing.B) {
	var s uint
	for i := 0; i < b.N; i++ {
		s += RotateLeft(uint(Input), i)
	}
	Output = int(s)
}

func BenchmarkRotateLeft8(b *testing.B) {
	var s uint8
	for i := 0; i < b.N; i++ {
		s += RotateLeft8(uint8(Input), i)
	}
	Output = int(s)
}

func BenchmarkRotateLeft16(b *testing.B) {
	var s uint16
	for i := 0; i < b.N; i++ {
		s += RotateLeft16(uint16(Input), i)
	}
	Output = int(s)
}

func BenchmarkRotateLeft32(b *testing.B) {
	var s uint32
	for i := 0; i < b.N; i++ {
		s += RotateLeft32(uint32(Input), i)
	}
	Output = int(s)
}

func BenchmarkRotateLeft64(b *testing.B) {
	var s uint64
	for i := 0; i < b.N; i++ {
		s += RotateLeft64(uint64(Input), i)
	}
	Output = int(s)
}

func TestReverse(t *testing.T) {
	// test each bit
	for i := uint(0); i < 64; i++ {
		testReverse(t, uint64(1)<<i, uint64(1)<<(63-i))
	}

	// test a few patterns
	for _, test := range []struct {
		x, r uint64
	}{
		{0, 0},
		{0x1, 0x8 << 60},
		{0x2, 0x4 << 60},
		{0x3, 0xc << 60},
		{0x4, 0x2 << 60},
		{0x5, 0xa << 60},
		{0x6, 0x6 << 60},
		{0x7, 0xe << 60},
		{0x8, 0x1 << 60},
		{0x9, 0x9 << 60},
		{0xa, 0x5 << 60},
		{0xb, 0xd << 60},
		{0xc, 0x3 << 60},
		{0xd, 0xb << 60},
		{0xe, 0x7 << 60},
		{0xf, 0xf << 60},
		{0x5686487, 0xe12616a000000000},
		{0x0123456789abcdef, 0xf7b3d591e6a2c480},
	} {
		testReverse(t, test.x, test.r)
		testReverse(t, test.r, test.x)
	}
}

func testReverse(t *testing.T, x64, want64 uint64) {
	x8 := uint8(x64)
	got8 := Reverse8(x8)
	want8 := uint8(want64 >> (64 - 8))
	if got8 != want8 {
		t.Fatalf("Reverse8(%#02x) == %#02x; want %#02x", x8, got8, want8)
	}

	x16 := uint16(x64)
	got16 := Reverse16(x16)
	want16 := uint16(want64 >> (64 - 16))
	if got16 != want16 {
		t.Fatalf("Reverse16(%#04x) == %#04x; want %#04x", x16, got16, want16)
	}

	x32 := uint32(x64)
	got32 := Reverse32(x32)
	want32 := uint32(want64 >> (64 - 32))
	if got32 != want32 {
		t.Fatalf("Reverse32(%#08x) == %#08x; want %#08x", x32, got32, want32)
	}
	if UintSize == 32 {
		x := uint(x32)
		got := Reverse(x)
		want := uint(want32)
		if got != want {
			t.Fatalf("Reverse(%#08x) == %#08x; want %#08x", x, got, want)
		}
	}

	got64 := Reverse64(x64)
	if got64 != want64 {
		t.Fatalf("Reverse64(%#016x) == %#016x; want %#016x", x64, got64, want64)
	}
	if UintSize == 64 {
		x := uint(x64)
		got := Reverse(x)
		want := uint(want64)
		if got != want {
			t.Fatalf("Reverse(%#08x) == %#016x; want %#016x", x, got, want)
		}
	}
}

func BenchmarkReverse(b *testing.B) {
	var s uint
	for i := 0; i < b.N; i++ {
		s += Reverse(uint(i))
	}
	Output = int(s)
}

func BenchmarkReverse8(b *testing.B) {
	var s uint8
	for i := 0; i < b.N; i++ {
		s += Reverse8(uint8(i))
	}
	Output = int(s)
}

func BenchmarkReverse16(b *testing.B) {
	var s uint16
	for i := 0; i < b.N; i++ {
		s += Reverse16(uint16(i))
	}
	Output = int(s)
}

func BenchmarkReverse32(b *testing.B) {
	var s uint32
	for i := 0; i < b.N; i++ {
		s += Reverse32(uint32(i))
	}
	Output = int(s)
}

func BenchmarkReverse64(b *testing.B) {
	var s uint64
	for i := 0; i < b.N; i++ {
		s += Reverse64(uint64(i))
	}
	Output = int(s)
}

func TestReverseBytes(t *testing.T) {
	for _, test := range []struct {
		x, r uint64
	}{
		{0, 0},
		{0x01, 0x01 << 56},
		{0x0123, 0x2301 << 48},
		{0x012345, 0x452301 << 40},
		{0x01234567, 0x67452301 << 32},
		{0x0123456789, 0x8967452301 << 24},
		{0x0123456789ab, 0xab8967452301 << 16},
		{0x0123456789abcd, 0xcdab8967452301 << 8},
		{0x0123456789abcdef, 0xefcdab8967452301 << 0},
	} {
		testReverseBytes(t, test.x, test.r)
		testReverseBytes(t, test.r, test.x)
	}
}

func testReverseBytes(t *testing.T, x64, want64 uint64) {
	x16 := uint16(x64)
	got16 := ReverseBytes16(x16)
	want16 := uint16(want64 >> (64 - 16))
	if got16 != want16 {
		t.Fatalf("ReverseBytes16(%#04x) == %#04x; want %#04x", x16, got16, want16)
	}

	x32 := uint32(x64)
	got32 := ReverseBytes32(x32)
	want32 := uint32(want64 >> (64 - 32))
	if got32 != want32 {
		t.Fatalf("ReverseBytes32(%#08x) == %#08x; want %#08x", x32, got32, want32)
	}
	if UintSize == 32 {
		x := uint(x32)
		got := ReverseBytes(x)
		want := uint(want32)
		if got != want {
			t.Fatalf("ReverseBytes(%#08x) == %#08x; want %#08x", x, got, want)
		}
	}

	got64 := ReverseBytes64(x64)
	if got64 != want64 {
		t.Fatalf("ReverseBytes64(%#016x) == %#016x; want %#016x", x64, got64, want64)
	}
	if UintSize == 64 {
		x := uint(x64)
		got := ReverseBytes(x)
		want := uint(want64)
		if got != want {
			t.Fatalf("ReverseBytes(%#016x) == %#016x; want %#016x", x, got, want)
		}
	}
}

func BenchmarkReverseBytes(b *testing.B) {
	var s uint
	for i := 0; i < b.N; i++ {
		s += ReverseBytes(uint(i))
	}
	Output = int(s)
}

func BenchmarkReverseBytes16(b *testing.B) {
	var s uint16
	for i := 0; i < b.N; i++ {
		s += ReverseBytes16(uint16(i))
	}
	Output = int(s)
}

func BenchmarkReverseBytes32(b *testing.B) {
	var s uint32
	for i := 0; i < b.N; i++ {
		s += ReverseBytes32(uint32(i))
	}
	Output = int(s)
}

func BenchmarkReverseBytes64(b *testing.B) {
	var s uint64
	for i := 0; i < b.N; i++ {
		s += ReverseBytes64(uint64(i))
	}
	Output = int(s)
}

func TestLen(t *testing.T) {
	for i := 0; i < 256; i++ {
		len := 8 - tab[i].nlz
		for k := 0; k < 64-8; k++ {
			x := uint64(i) << uint(k)
			want := 0
			if x != 0 {
				want = len + k
			}
			if x <= 1<<8-1 {
				got := Len8(uint8(x))
				if got != want {
					t.Fatalf("Len8(%#02x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<16-1 {
				got := Len16(uint16(x))
				if got != want {
					t.Fatalf("Len16(%#04x) == %d; want %d", x, got, want)
				}
			}

			if x <= 1<<32-1 {
				got := Len32(uint32(x))
				if got != want {
					t.Fatalf("Len32(%#08x) == %d; want %d", x, got, want)
				}
				if UintSize == 32 {
					got := Len(uint(x))
					if got != want {
						t.Fatalf("Len(%#08x) == %d; want %d", x, got, want)
					}
				}
			}

			if x <= 1<<64-1 {
				got := Len64(uint64(x))
				if got != want {
					t.Fatalf("Len64(%#016x) == %d; want %d", x, got, want)
				}
				if UintSize == 64 {
					got := Len(uint(x))
					if got != want {
						t.Fatalf("Len(%#016x) == %d; want %d", x, got, want)
					}
				}
			}
		}
	}
}

const (
	_M   = 1<<UintSize - 1
	_M32 = 1<<32 - 1
	_M64 = 1<<64 - 1
)

func TestAddSubUint(t *testing.T) {
	test := func(msg string, f func(x, y, c uint) (z, cout uint), x, y, c, z, cout uint) {
		z1, cout1 := f(x, y, c)
		if z1 != z || cout1 != cout {
			t.Errorf("%s: got z:cout = %#x:%#x; want %#x:%#x", msg, z1, cout1, z, cout)
		}
	}
	for _, a := range []struct{ x, y, c, z, cout uint }{
		{0, 0, 0, 0, 0},
		{0, 1, 0, 1, 0},
		{0, 0, 1, 1, 0},
		{0, 1, 1, 2, 0},
		{12345, 67890, 0, 80235, 0},
		{12345, 67890, 1, 80236, 0},
		{_M, 1, 0, 0, 1},
		{_M, 0, 1, 0, 1},
		{_M, 1, 1, 1, 1},
		{_M, _M, 0, _M - 1, 1},
		{_M, _M, 1, _M, 1},
	} {
		test("Add", Add, a.x, a.y, a.c, a.z, a.cout)
		test("Add symmetric", Add, a.y, a.x, a.c, a.z, a.cout)
		test("Sub", Sub, a.z, a.x, a.c, a.y, a.cout)
		test("Sub symmetric", Sub, a.z, a.y, a.c, a.x, a.cout)
		// The above code can't test intrinsic implementation, because the passed function is not called directly.
		// The following code uses a closure to test the intrinsic version in case the function is intrinsified.
		test("Add intrinsic", func(x, y, c uint) (uint, uint) { return Add(x, y, c) }, a.x, a.y, a.c, a.z, a.cout)
		test("Add intrinsic symmetric", func(x, y, c uint) (uint, uint) { return Add(x, y, c) }, a.y, a.x, a.c, a.z, a.cout)
		test("Sub intrinsic", func(x, y, c uint) (uint, uint) { return Sub(x, y, c) }, a.z, a.x, a.c, a.y, a.cout)
		test("Sub intrinsic symmetric", func(x, y, c uint) (uint, uint) { return Sub(x, y, c) }, a.z, a.y, a.c, a.x, a.cout)

	}
}

func TestAddSubUint32(t *testing.T) {
	test := func(msg string, f func(x, y, c uint32) (z, cout uint32), x, y, c, z, cout uint32) {
		z1, cout1 := f(x, y, c)
		if z1 != z || cout1 != cout {
			t.Errorf("%s: got z:cout = %#x:%#x; want %#x:%#x", msg, z1, cout1, z, cout)
		}
	}
	for _, a := range []struct{ x, y, c, z, cout uint32 }{
		{0, 0, 0, 0, 0},
		{0, 1, 0, 1, 0},
		{0, 0, 1, 1, 0},
		{0, 1, 1, 2, 0},
		{12345, 67890, 0, 80235, 0},
		{12345, 67890, 1, 80236, 0},
		{_M32, 1, 0, 0, 1},
		{_M32, 0, 1, 0, 1},
		{_M32, 1, 1, 1, 1},
		{_M32, _M32, 0, _M32 - 1, 1},
		{_M32, _M32, 1, _M32, 1},
	} {
		test("Add32", Add32, a.x, a.y, a.c, a.z, a.cout)
		test("Add32 symmetric", Add32, a.y, a.x, a.c, a.z, a.cout)
		test("Sub32", Sub32, a.z, a.x, a.c, a.y, a.cout)
		test("Sub32 symmetric", Sub32, a.z, a.y, a.c, a.x, a.cout)
	}
}

func TestAddSubUint64(t *testing.T) {
	test := func(msg string, f func(x, y, c uint64) (z, cout uint64), x, y, c, z, cout uint64) {
		z1, cout1 := f(x, y, c)
		if z1 != z || cout1 != cout {
			t.Errorf("%s: got z:cout = %#x:%#x; want %#x:%#x", msg, z1, cout1, z, cout)
		}
	}
	for _, a := range []struct{ x, y, c, z, cout uint64 }{
		{0, 0, 0, 0, 0},
		{0, 1, 0, 1, 0},
		{0, 0, 1, 1, 0},
		{0, 1, 1, 2, 0},
		{12345, 67890, 0, 80235, 0},
		{12345, 67890, 1, 80236, 0},
		{_M64, 1, 0, 0, 1},
		{_M64, 0, 1, 0, 1},
		{_M64, 1, 1, 1, 1},
		{_M64, _M64, 0, _M64 - 1, 1},
		{_M64, _M64, 1, _M64, 1},
	} {
		test("Add64", Add64, a.x, a.y, a.c, a.z, a.cout)
		test("Add64 symmetric", Add64, a.y, a.x, a.c, a.z, a.cout)
		test("Sub64", Sub64, a.z, a.x, a.c, a.y, a.cout)
		test("Sub64 symmetric", Sub64, a.z, a.y, a.c, a.x, a.cout)
		// The above code can't test intrinsic implementation, because the passed function is not called directly.
		// The following code uses a closure to test the intrinsic version in case the function is intrinsified.
		test("Add64 intrinsic", func(x, y, c uint64) (uint64, uint64) { return Add64(x, y, c) }, a.x, a.y, a.c, a.z, a.cout)
		test("Add64 intrinsic symmetric", func(x, y, c uint64) (uint64, uint64) { return Add64(x, y, c) }, a.y, a.x, a.c, a.z, a.cout)
		test("Sub64 intrinsic", func(x, y, c uint64) (uint64, uint64) { return Sub64(x, y, c) }, a.z, a.x, a.c, a.y, a.cout)
		test("Sub64 intrinsic symmetric", func(x, y, c uint64) (uint64, uint64) { return Sub64(x, y, c) }, a.z, a.y, a.c, a.x, a.cout)
	}
}

func TestAdd64OverflowPanic(t *testing.T) {
	// Test that 64-bit overflow panics fire correctly.
	// These are designed to improve coverage of compiler intrinsics.
	tests := []func(uint64, uint64) uint64{
		func(a, b uint64) uint64 {
			x, c := Add64(a, b, 0)
			if c > 0 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Add64(a, b, 0)
			if c != 0 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Add64(a, b, 0)
			if c == 1 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Add64(a, b, 0)
			if c != 1 {
				return x
			}
			panic("overflow")
		},
		func(a, b uint64) uint64 {
			x, c := Add64(a, b, 0)
			if c == 0 {
				return x
			}
			panic("overflow")
		},
	}
	for _, test := range tests {
		shouldPanic := func(f func()) {
			defer func() {
				if err := recover(); err == nil {
					t.Fatalf("expected panic")
				}
			}()
			f()
		}

		// overflow
		shouldPanic(func() { test(_M64, 1) })
		shouldPanic(func() { test(1, _M64) })
		shouldPanic(func() { test(_M64, _M64) })

		// no overflow
		test(_M64, 0)
		test(0, 0)
		test(1, 1)
	}
}

func TestSub64OverflowPanic(t *testing.T) {
	// Test that 64-bit overflow panics fire correctly.
	// These are designed to improve coverage of compiler intrinsics.
	tests := []func(uint64, uint64) uint64{
		func(a, b uint64) uint64 {
			x, c := Sub64(a, b, 0)
			if c > 0 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Sub64(a, b, 0)
			if c != 0 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Sub64(a, b, 0)
			if c == 1 {
				panic("overflow")
			}
			return x
		},
		func(a, b uint64) uint64 {
			x, c := Sub64(a, b, 0)
			if c != 1 {
				return x
			}
			panic("overflow")
		},
		func(a, b uint64) uint64 {
			x, c := Sub64(a, b, 0)
			if c == 0 {
				return x
			}
			panic("overflow")
		},
	}
	for _, test := range tests {
		shouldPanic := func(f func()) {
			defer func() {
				if err := recover(); err == nil {
					t.Fatalf("expected panic")
				}
			}()
			f()
		}

		// overflow
		shouldPanic(func() { test(0, 1) })
		shouldPanic(func() { test(1, _M64) })
		shouldPanic(func() { test(_M64-1, _M64) })

		// no overflow
		test(_M64, 0)
		test(0, 0)
		test(1, 1)
	}
}

func TestMulDiv(t *testing.T) {
	testMul := func(msg string, f func(x, y uint) (hi, lo uint), x, y, hi, lo uint) {
		hi1, lo1 := f(x, y)
		if hi1 != hi || lo1 != lo {
			t.Errorf("%s: got hi:lo = %#x:%#x; want %#x:%#x", msg, hi1, lo1, hi, lo)
		}
	}
	testDiv := func(msg string, f func(hi, lo, y uint) (q, r uint), hi, lo, y, q, r uint) {
		q1, r1 := f(hi, lo, y)
		if q1 != q || r1 != r {
			t.Errorf("%s: got q:r = %#x:%#x; want %#x:%#x", msg, q1, r1, q, r)
		}
	}
	for _, a := range []struct {
		x, y      uint
		hi, lo, r uint
	}{
		{1 << (UintSize - 1), 2, 1, 0, 1},
		{_M, _M, _M - 1, 1, 42},
	} {
		testMul("Mul", Mul, a.x, a.y, a.hi, a.lo)
		testMul("Mul symmetric", Mul, a.y, a.x, a.hi, a.lo)
		testDiv("Div", Div, a.hi, a.lo+a.r, a.y, a.x, a.r)
		testDiv("Div symmetric", Div, a.hi, a.lo+a.r, a.x, a.y, a.r)
		// The above code can't test intrinsic implementation, because the passed function is not called directly.
		// The following code uses a closure to test the intrinsic version in case the function is intrinsified.
		testMul("Mul intrinsic", func(x, y uint) (uint, uint) { return Mul(x, y) }, a.x, a.y, a.hi, a.lo)
		testMul("Mul intrinsic symmetric", func(x, y uint) (uint, uint) { return Mul(x, y) }, a.y, a.x, a.hi, a.lo)
		testDiv("Div intrinsic", func(hi, lo, y uint) (uint, uint) { return Div(hi, lo, y) }, a.hi, a.lo+a.r, a.y, a.x, a.r)
		testDiv("Div intrinsic symmetric", func(hi, lo, y uint) (uint, uint) { return Div(hi, lo, y) }, a.hi, a.lo+a.r, a.x, a.y, a.r)
	}
}

func TestMulDiv32(t *testing.T) {
	testMul := func(msg string, f func(x, y uint32) (hi, lo uint32), x, y, hi, lo uint32) {
		hi1, lo1 := f(x, y)
		if hi1 != hi || lo1 != lo {
			t.Errorf("%s: got hi:lo = %#x:%#x; want %#x:%#x", msg, hi1, lo1, hi, lo)
		}
	}
	testDiv := func(msg string, f func(hi, lo, y uint32) (q, r uint32), hi, lo, y, q, r uint32) {
		q1, r1 := f(hi, lo, y)
		if q1 != q || r1 != r {
			t.Errorf("%s: got q:r = %#x:%#x; want %#x:%#x", msg, q1, r1, q, r)
		}
	}
	for _, a := range []struct {
		x, y      uint32
		hi, lo, r uint32
	}{
		{1 << 31, 2, 1, 0, 1},
		{0xc47dfa8c, 50911, 0x98a4, 0x998587f4, 13},
		{_M32, _M32, _M32 - 1, 1, 42},
	} {
		testMul("Mul32", Mul32, a.x, a.y, a.hi, a.lo)
		testMul("Mul32 symmetric", Mul32, a.y, a.x, a.hi, a.lo)
		testDiv("Div32", Div32, a.hi, a.lo+a.r, a.y, a.x, a.r)
		testDiv("Div32 symmetric", Div32, a.hi, a.lo+a.r, a.x, a.y, a.r)
	}
}

func TestMulDiv64(t *testing.T) {
	testMul := func(msg string, f func(x, y uint64) (hi, lo uint64), x, y, hi, lo uint64) {
		hi1, lo1 := f(x, y)
		if hi1 != hi || lo1 != lo {
			t.Errorf("%s: got hi:lo = %#x:%#x; want %#x:%#x", msg, hi1, lo1, hi, lo)
		}
	}
	testDiv := func(msg string, f func(hi, lo, y uint64) (q, r uint64), hi, lo, y, q, r uint64) {
		q1, r1 := f(hi, lo, y)
		if q1 != q || r1 != r {
			t.Errorf("%s: got q:r = %#x:%#x; want %#x:%#x", msg, q1, r1, q, r)
		}
	}
	for _, a := range []struct {
		x, y      uint64
		hi, lo, r uint64
	}{
		{1 << 63, 2, 1, 0, 1},
		{0x3626229738a3b9, 0xd8988a9f1cc4a61, 0x2dd0712657fe8, 0x9dd6a3364c358319, 13},
		{_M64, _M64, _M64 - 1, 1, 42},
	} {
		testMul("Mul64", Mul64, a.x, a.y, a.hi, a.lo)
		testMul("Mul64 symmetric", Mul64, a.y, a.x, a.hi, a.lo)
		testDiv("Div64", Div64, a.hi, a.lo+a.r, a.y, a.x, a.r)
		testDiv("Div64 symmetric", Div64, a.hi, a.lo+a.r, a.x, a.y, a.r)
		// The above code can't test intrinsic implementation, because the passed function is not called directly.
		// The following code uses a closure to test the intrinsic version in case the function is intrinsified.
		testMul("Mul64 intrinsic", func(x, y uint64) (uint64, uint64) { return Mul64(x, y) }, a.x, a.y, a.hi, a.lo)
		testMul("Mul64 intrinsic symmetric", func(x, y uint64) (uint64, uint64) { return Mul64(x, y) }, a.y, a.x, a.hi, a.lo)
		testDiv("Div64 intrinsic", func(hi, lo, y uint64) (uint64, uint64) { return Div64(hi, lo, y) }, a.hi, a.lo+a.r, a.y, a.x, a.r)
		testDiv("Div64 intrinsic symmetric", func(hi, lo, y uint64) (uint64, uint64) { return Div64(hi, lo, y) }, a.hi, a.lo+a.r, a.x, a.y, a.r)
	}
}

const (
	divZeroError  = "runtime error: integer divide by zero"
	overflowError = "runtime error: integer overflow"
)

func TestDivPanicOverflow(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div should have panicked when y<=hi")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != overflowError {
			t.Errorf("Div expected panic: %q, got: %q ", overflowError, e.Error())
		}
	}()
	q, r := Div(1, 0, 1)
	t.Errorf("undefined q, r = %v, %v calculated when Div should have panicked", q, r)
}

func TestDiv32PanicOverflow(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div32 should have panicked when y<=hi")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != overflowError {
			t.Errorf("Div32 expected panic: %q, got: %q ", overflowError, e.Error())
		}
	}()
	q, r := Div32(1, 0, 1)
	t.Errorf("undefined q, r = %v, %v calculated when Div32 should have panicked", q, r)
}

func TestDiv64PanicOverflow(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div64 should have panicked when y<=hi")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != overflowError {
			t.Errorf("Div64 expected panic: %q, got: %q ", overflowError, e.Error())
		}
	}()
	q, r := Div64(1, 0, 1)
	t.Errorf("undefined q, r = %v, %v calculated when Div64 should have panicked", q, r)
}

func TestDivPanicZero(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div should have panicked when y==0")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != divZeroError {
			t.Errorf("Div expected panic: %q, got: %q ", divZeroError, e.Error())
		}
	}()
	q, r := Div(1, 1, 0)
	t.Errorf("undefined q, r = %v, %v calculated when Div should have panicked", q, r)
}

func TestDiv32PanicZero(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div32 should have panicked when y==0")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != divZeroError {
			t.Errorf("Div32 expected panic: %q, got: %q ", divZeroError, e.Error())
		}
	}()
	q, r := Div32(1, 1, 0)
	t.Errorf("undefined q, r = %v, %v calculated when Div32 should have panicked", q, r)
}

func TestDiv64PanicZero(t *testing.T) {
	// Expect a panic
	defer func() {
		if err := recover(); err == nil {
			t.Error("Div64 should have panicked when y==0")
		} else if e, ok := err.(runtime.Error); !ok || e.Error() != divZeroError {
			t.Errorf("Div64 expected panic: %q, got: %q ", divZeroError, e.Error())
		}
	}()
	q, r := Div64(1, 1, 0)
	t.Errorf("undefined q, r = %v, %v calculated when Div64 should have panicked", q, r)
}

func TestRem32(t *testing.T) {
	// Sanity check: for non-oveflowing dividends, the result is the
	// same as the rem returned by Div32
	hi, lo, y := uint32(510510), uint32(9699690), uint32(510510+1) // ensure hi < y
	for i := 0; i < 1000; i++ {
		r := Rem32(hi, lo, y)
		_, r2 := Div32(hi, lo, y)
		if r != r2 {
			t.Errorf("Rem32(%v, %v, %v) returned %v, but Div32 returned rem %v", hi, lo, y, r, r2)
		}
		y += 13
	}
}

func TestRem32Overflow(t *testing.T) {
	// To trigger a quotient overflow, we need y <= hi
	hi, lo, y := uint32(510510), uint32(9699690), uint32(7)
	for i := 0; i < 1000; i++ {
		r := Rem32(hi, lo, y)
		_, r2 := Div64(0, uint64(hi)<<32|uint64(lo), uint64(y))
		if r != uint32(r2) {
			t.Errorf("Rem32(%v, %v, %v) returned %v, but Div64 returned rem %v", hi, lo, y, r, r2)
		}
		y += 13
	}
}

func TestRem64(t *testing.T) {
	// Sanity check: for non-oveflowing dividends, the result is the
	// same as the rem returned by Div64
	hi, lo, y := uint64(510510), uint64(9699690), uint64(510510+1) // ensure hi < y
	for i := 0; i < 1000; i++ {
		r := Rem64(hi, lo, y)
		_, r2 := Div64(hi, lo, y)
		if r != r2 {
			t.Errorf("Rem64(%v, %v, %v) returned %v, but Div64 returned rem %v", hi, lo, y, r, r2)
		}
		y += 13
	}
}

func TestRem64Overflow(t *testing.T) {
	Rem64Tests := []struct {
		hi, lo, y uint64
		rem       uint64
	}{
		// Testcases computed using Python 3, as:
		//   >>> hi = 42; lo = 1119; y = 42
		//   >>> ((hi<<64)+lo) % y
		{42, 1119, 42, 27},
		{42, 1119, 38, 9},
		{42, 1119, 26, 23},
		{469, 0, 467, 271},
		{469, 0, 113, 58},
		{111111, 111111, 1171, 803},
		{3968194946088682615, 3192705705065114702, 1000037, 56067},
	}

	for _, rt := range Rem64Tests {
		if rt.hi < rt.y {
			t.Fatalf("Rem64(%v, %v, %v) is not a test with quo overflow", rt.hi, rt.lo, rt.y)
		}
		rem := Rem64(rt.hi, rt.lo, rt.y)
		if rem != rt.rem {
			t.Errorf("Rem64(%v, %v, %v) returned %v, wanted %v",
				rt.hi, rt.lo, rt.y, rem, rt.rem)
		}
	}
}

func BenchmarkAdd(b *testing.B) {
	var z, c uint
	for i := 0; i < b.N; i++ {
		z, c = Add(uint(Input), uint(i), c)
	}
	Output = int(z + c)
}

func BenchmarkAdd32(b *testing.B) {
	var z, c uint32
	for i := 0; i < b.N; i++ {
		z, c = Add32(uint32(Input), uint32(i), c)
	}
	Output = int(z + c)
}

func BenchmarkAdd64(b *testing.B) {
	var z, c uint64
	for i := 0; i < b.N; i++ {
		z, c = Add64(uint64(Input), uint64(i), c)
	}
	Output = int(z + c)
}

func BenchmarkAdd64multiple(b *testing.B) {
	var z0 = uint64(Input)
	var z1 = uint64(Input)
	var z2 = uint64(Input)
	var z3 = uint64(Input)
	for i := 0; i < b.N; i++ {
		var c uint64
		z0, c = Add64(z0, uint64(i), c)
		z1, c = Add64(z1, uint64(i), c)
		z2, c = Add64(z2, uint64(i), c)
		z3, _ = Add64(z3, uint64(i), c)
	}
	Output = int(z0 + z1 + z2 + z3)
}

func BenchmarkSub(b *testing.B) {
	var z, c uint
	for i := 0; i < b.N; i++ {
		z, c = Sub(uint(Input), uint(i), c)
	}
	Output = int(z + c)
}

func BenchmarkSub32(b *testing.B) {
	var z, c uint32
	for i := 0; i < b.N; i++ {
		z, c = Sub32(uint32(Input), uint32(i), c)
	}
	Output = int(z + c)
}

func BenchmarkSub64(b *testing.B) {
	var z, c uint64
	for i := 0; i < b.N; i++ {
		z, c = Sub64(uint64(Input), uint64(i), c)
	}
	Output = int(z + c)
}

func BenchmarkSub64multiple(b *testing.B) {
	var z0 = uint64(Input)
	var z1 = uint64(Input)
	var z2 = uint64(Input)
	var z3 = uint64(Input)
	for i := 0; i < b.N; i++ {
		var c uint64
		z0, c = Sub64(z0, uint64(i), c)
		z1, c = Sub64(z1, uint64(i), c)
		z2, c = Sub64(z2, uint64(i), c)
		z3, _ = Sub64(z3, uint64(i), c)
	}
	Output = int(z0 + z1 + z2 + z3)
}

func BenchmarkMul(b *testing.B) {
	var hi, lo uint
	for i := 0; i < b.N; i++ {
		hi, lo = Mul(uint(Input), uint(i))
	}
	Output = int(hi + lo)
}

func BenchmarkMul32(b *testing.B) {
	var hi, lo uint32
	for i := 0; i < b.N; i++ {
		hi, lo = Mul32(uint32(Input), uint32(i))
	}
	Output = int(hi + lo)
}

func BenchmarkMul64(b *testing.B) {
	var hi, lo uint64
	for i := 0; i < b.N; i++ {
		hi, lo = Mul64(uint64(Input), uint64(i))
	}
	Output = int(hi + lo)
}

func BenchmarkDiv(b *testing.B) {
	var q, r uint
	for i := 0; i < b.N; i++ {
		q, r = Div(1, uint(i), uint(Input))
	}
	Output = int(q + r)
}

func BenchmarkDiv32(b *testing.B) {
	var q, r uint32
	for i := 0; i < b.N; i++ {
		q, r = Div32(1, uint32(i), uint32(Input))
	}
	Output = int(q + r)
}

func BenchmarkDiv64(b *testing.B) {
	var q, r uint64
	for i := 0; i < b.N; i++ {
		q, r = Div64(1, uint64(i), uint64(Input))
	}
	Output = int(q + r)
}

// ----------------------------------------------------------------------------
// Testing support

type entry = struct {
	nlz, ntz, pop int
}

// tab contains results for all uint8 values
var tab [256]entry

func init() {
	tab[0] = entry{8, 8, 0}
	for i := 1; i < len(tab); i++ {
		// nlz
		x := i // x != 0
		n := 0
		for x&0x80 == 0 {
			n++
			x <<= 1
		}
		tab[i].nlz = n

		// ntz
		x = i // x != 0
		n = 0
		for x&1 == 0 {
			n++
			x >>= 1
		}
		tab[i].ntz = n

		// pop
		x = i // x != 0
		n = 0
		for x != 0 {
			n += int(x & 1)
			x >>= 1
		}
		tab[i].pop = n
	}
}