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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
//! The [Bits] type is used to capture values with arbitrarily large (but known) bit length
//!
//! One significant difference between hardware design and software programming is the need
//! (and indeed ability) to easily manipulate collections of bits that are of various lengths.
//! While Rust has built in types to represent 8, 16, 32, 64, and 128 bits (at the time of this
//! writing, anyway), it is difficult to represent a 5 bit type.  Or a 256 bit type.  Or indeed
//! any bit length that differs from one of the supported values.
//!
//! In hardware design, the bit size is nearly always unusual, as bits occupy physical space,
//! and as a result, as a logic designer, you will intentionally use the smallest number of
//! bits needed to capture a value.  For example, if you are reading a single nibble at a
//! time from a bus, this is clearly a 4 bit value, and storing it in a `u8` is a waste of
//! space and resources.
//!
//! To model this behavior in RustHDL, we have the [Bits] type, which attempts to be as close
//! as possible to a hardware bit vector.  The size must be known at compile time, and there is
//! some internal optimization for short bitvectors being represented efficiently, but ideally
//! you should be able to think of it as a bit of arbitrary length.  Note that the [Bits]
//! type is `Copy`, which is quite important.  This means in your RustHDL code, you can freely
//! copy and assign bitvectors without worrying about the borrow checker or trying to call
//! `clone` in the midst of your HDL.
//!
//! For the most part, the [Bits] type is meant to act like a `u32` or `u128` type as far
//! as your code is concerned.  But the emulation of built-in types is not perfect, and
//! you may struggle with them a bit.
//!
//! # Constructing [Bits]
//! There are several ways to construct a [Bits] type.  It includes an implementation of
//! the [Default](std::default::Default), trait, so if you need a zero value, you can use
//! that form:
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<50> = Default::default();
//! ```
//! This will construct a length 50 bit vector that is initialized to all `0`.
//!
//! You can also convert from literals into bit vectors using the [From] and [Into] traits,
//! provided the literals are of the `u64` type.
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<50> = 0xBEEF.into();
//! ```
//!
//! In some cases, Rust complains about literals, and you may need to provide a suffix:
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<50> = 0xDEAD_BEEF_u64.into();
//! ```
//! However, in most cases, you can leave literals suffix-free, and Rust will automatically
//! determine the type from the context.
//!
//! You can construct a larger constant using the [bits] function.  If you have a literal of up to
//! 128 bits, it provides a functional form
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<200> = bits(0xDEAD_BEEE); // Works for up to 128 bit constants.
//! ```
//!
//! There is also the [ToBits] trait, which is implemented on the basic unsigned integer types.
//! This trait allows you to handily convert from different integer values
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<10> = 32_u8.to_bits();
//! ```
//!
//!
//! # Operations
//! Only a subset of operations are defined for [Bits].  These are the operations that can
//! be synthesized in hardware without surprises (generally speaking).  In Rust, you can
//! operate between [Bits] types and other [Bits] of the _same width_, or you can
//! use integer literals.  *Be careful!* Especially when manipulating signed quantities.  Use the
//! [Signed](crate::core::signed::Signed) type for those.
//!
//! ## Addition
//! You can perform wrapping addition using the `+` operator.
//! Here are some simple examples of addition. First the version using a literal
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<200> = bits(0xDEAD_BEEE);
//! let y: Bits<200> = x + 1;
//! assert_eq!(y, bits(0xDEAD_BEEF));
//! ```
//!
//! And now a second example that uses two [Bits] values
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<40> = bits(0xDEAD_0000);
//! let y: Bits<40> = bits(0x0000_CAFE);
//! let z = x + y;
//! assert_eq!(z, bits(0xDEAD_CAFE));
//! ```
//!
//! Note that the addition operator is _silently wrapping_.  In other words the carry
//! bit is discarded silently (again - this is what hardware typically does).  So you
//! may find this result surprising:
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<40> = bits(0xFF_FFFF_FFFF);
//! let y = x + 1;
//! assert_eq!(y, bits(0));
//! ```
//!
//! In this case, the addition of 1 caused [x] to wrap to all zeros.  This is totally normal,
//! and what one would expect from hardware addition (without a carry).  If you _need_ the
//! carry bit, then the solution is to first cast to 1 higher bit, and then add, or alternately,
//! to compute the carry directly.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<40> = bits(0xFF_FFFF_FFFF);
//! let y = bit_cast::<41, 40>(x) + 1;
//! assert_eq!(y, bits(0x100_0000_0000));
//! ```
//!
//! The order of the arguments does not matter.  The bit width of the calculation will be
//! determined by the [Bits] width.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x : Bits<25> = bits(0xCAFD);
//! let y = 1 + x;
//! assert_eq!(y, bits(0xCAFE));
//! ```
//!
//! However, you cannot combine two different width [Bits] values in a single expression.
//! ```compile_fail
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<20> = bits(0x1234);
//! let y: Bits<21> = bits(0x5123);
//! let z = x + y; // Won't compile!
//! ```
//!
//! ## Subtraction
//! Hardware subtraction is defined using 2-s complement representation for negative numbers.
//! This is pretty much a universal standard for representing negative numbers in binary, and
//! has the added advantage that a hardware subtractor can be built from an adder and some basic
//! gates.  Subtraction operates much like the [Wrapping] class.  Note that overflow and underflow
//! are _not_ detected in RustHDL (nor are they detected in most hardware implementations either).
//!
//! Here is a simple example with a literal and subtraction that does not cause udnerflow
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<40> = bits(0xDEAD_BEF0);
//! let y = x - 1;
//! assert_eq!(y, bits(0xDEAD_BEEF));
//! ```
//!
//! When values underflow, the representation is still valid as a 2-s complement number.  For
//! example,
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(0x40);
//! let y: Bits<16> = bits(0x60);
//! let z = x - y;
//! assert_eq!(z, bits(0xFFFF-0x20+1));
//! ```
//!
//! Here, we compare the value of `z` with `0xFFFF-0x20+1` which is the 2-s complement
//! representation of `-0x20`.
//!
//! You can also put the literal on the left side of the subtraction expression, as expected.  The
//! bitwidth of the computation will be driven by the width of the [Bits] in the expression.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x = bits::<32>(0xBABE);
//! let z = 0xB_BABE - x;
//! assert_eq!(z, bits(0xB_0000));
//! ```
//!
//! ## Bitwise And
//!
//! You can combine [Bits] using the and operator `&`.  In general, avoid using the shortcut
//! logical operator `&&`, since this operator is really only defined for logical (scalar) values
//! of type `bool`.
//!
//! ```
//! # use  rust_hdl::core::prelude::*;
//! let x: Bits<32> = bits(0xDEAD_BEEF);
//! let y: Bits<32> = bits(0xFFFF_0000);
//! let z = x & y;
//! assert_eq!(z, bits(0xDEAD_0000));
//! ```
//!
//! Of course, you can also use a literal value in the `and` operation.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<32> = bits(0xDEAD_BEEF);
//! let z = x & 0x0000_FFFF;
//! assert_eq!(z, bits(0xBEEF))
//! ```
//!
//! and similarly, the literal can appear on the left of the `and` expression.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<32> = bits(0xCAFE_BEEF);
//! let z = 0xFFFF_0000 & x;
//! assert_eq!(z, bits(0xCAFE_0000));
//! ```
//!
//! Just like all other binary operations, you cannot mix widths (unless one of the
//! values is a literal).
//! ```compile_fail
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(0xFEED_FACE);
//! let y: Bits<17> = bits(0xABCE);
//! let z = x & y; // Won't compile!
//! ```
//!
//! ## Bitwise Or
//!
//! There is also a bitwise-OR operation using the `|` symbol.  Note that the logical OR
//! (or shortcut OR) operator `||` is not supported for [Bits], as it is only defined for
//! scalar boolean values.
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x : Bits<32> = bits(0xBEEF_0000);
//! let y : Bits<32> = bits(0x0000_CAFE);
//! let z = x | y;
//! assert_eq!(z, bits(0xBEEF_CAFE));
//! ```
//!
//! You can also use literals
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x : Bits<32> = bits(0xBEEF_0000);
//! let z = x | 0x0000_CAFE;
//! assert_eq!(z, bits(0xBEEF_CAFE));
//! ```
//!
//! The caveat about mixing [Bits] of different widths still applies.
//!
//! ## Bitwise Xor
//!
//! There is a bitwise-Xor operation using the `^` operator.  This will compute the
//! bitwise exclusive OR of the two values.
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x : Bits<32> = bits(0xCAFE_BABE);
//! let y : Bits<32> = bits(0xFF00_00FF);
//! let z = y ^ x;
//! let w = z ^ y; // XOR applied twice is a null-op
//! assert_eq!(w, x);
//! ```
//!
//! ## Bitwise comparison
//!
//! The equality operator `==` can compare two [Bits] for bit-wise equality.
//!
//!```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(0x5ea1);
//! let y: Bits<16> = bits(0xbadb);
//! assert_eq!(x == y, false)
//!```
//!
//! Again, it is a compile time failure to attempt to compare [Bits] of different
//! widths.
//!
//!```compile_fail
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<15> = bits(52);
//! let y: Bits<16> = bits(32);
//! let z = x == y; // Won't compile - bit widths must match
//!```
//!
//! You can compare to literals, as they will automatically extended (or truncated) to match the
//! bitwidth of the [Bits] value.
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x : Bits<16> = bits(32);
//! let z = x == 32;
//! let y = 32 == x;
//! assert!(z);
//! assert!(y);
//! ```
//!
//! ## Unsigned comparison
//!
//! The [Bits] type only supports unsigned comparisons.  If you compare a [Bits] value
//! to a signed integer, it will first convert the signed integer into 2s complement
//! representation and then perform an unsigned comparison.  That is most likely _not_ what
//! you want.  However, until there is full support for signed integer computations, that is
//! the behavior you get.  Hardware signed comparisons require more circuitry and logic
//! than unsigned comparisons, so the rationale is to not inadvertently bloat your hardware
//! designs with sign-aware circuitry when you don't explicitly invoke it.  If you want signed
//! values, use [Signed].
//!
//! Here are some simple examples.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(52);
//! let y: Bits<16> = bits(13);
//! assert!(y < x)
//! ```
//!
//! We can also compare with literals, which RustHDL will expand out to match the bit width
//! of the [Bits] being compared to.
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(52);
//! let y = x < 135;  // Converts the 135 to a Bits<16> and then compares
//! assert!(y)
//! ```
//!
//! ## Shift Left
//!
//! RustHDl supports left shift bit operations using the `<<` operator.
//! Bits that shift off the left end of
//! the bit vector (most significant bits).
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(0xDEAD);
//! let y = x << 8;
//! assert_eq!(y, bits(0xAD00));
//! ```
//!
//! ## Shift Right
//!
//! Right shifting is also supported using the `>>` operator.
//! Bits that shift off the right end of the
//! the bit vector (least significant bits).
//!
//! ```
//! # use rust_hdl::core::prelude::*;
//! let x: Bits<16> = bits(0xDEAD);
//! let y = x >> 8;
//! assert_eq!(y, bits(0x00DE));
//! ```

use crate::core::bitvec::BitVec;
use crate::core::short_bit_vec::{ShortBitVec, ShortType, SHORT_BITS};
use crate::core::synth::VCDValue;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use std::cmp::Ordering;
use std::fmt::{Binary, Debug, Formatter, LowerHex, UpperHex};
use std::hash::Hasher;
use std::num::Wrapping;

// This comes with a few invariants that must be maintained for short representation
// The short value must be less than 2^N
// N <= SHORT_BITS --> Short repr, otherwise Long repr

/// The [LiteralType] is used to set the type for literals that appear in RustHDL
/// expressions.  Because of how Rust's type inference currently works, an expression
/// like
/// ```
/// # use rust_hdl::core::prelude::*;
///
/// let x: Bits<32> = 0xDEADBEEF.into();
/// let y : Bits<32> = x + 1;
/// ```
/// only works if Rust can unambiguously assign a type to the literal (either `DEADBEEF` or `1`).
/// In earlier versions of RustHDL, this required adding a suffix to the literal like `0xDEADBEEF_u32`,
/// but those suffixes in turn littered the code and made it difficult to read.  Now, only one type
/// of literal is supported [LiteralType], which is an alias for [u64].  As such, any un-suffixed
/// number is assumed to be at most a 64 bit integer.  This does not limit you in any way from
/// using suffixed literals.  You can express, for example, up to 128 bit constants using standard
/// Rust notation, and using the [to_bits] trait to convert it to a [Bits] type.
/// ```
/// # use rust_hdl::core::prelude::*;
///
/// let x: Bits<128> = 0xDEADBEEF_CAFE_1234_u128.to_bits();  // Works!
/// ```
/// However, the following will fail, since the [From] trait is only implemented on [LiteralType]
/// to make the conversion unambiguous.
/// ```compile_fail
/// # use rust_hdl::core::prelude::*;
///
/// let x: Bits<128> = 0xDEADBEEF_CAFE_1234_u128.into();  // Fails to compile, since conversion from [u128] is not defined
/// ```
pub type LiteralType = u64;
/// [LITERAL_BITS] is set to the number of bits in the [LiteralType].  I.e., it is guaranteed that
/// the number of bits in [LiteralType] is [LITERAL_BITS].
pub const LITERAL_BITS: usize = 64;

/// Compute the minimum number of bits to represent a container with t items.
/// This is basically `ceil(log2(t))` as a constant (compile time computable) function.
/// You can use it where a const generic (bit width) argument is required.
///
/// Example
///
/// Unfortunately, with stable Rust, this function is not of much use.
/// For now, const generic arguments cannot be used in expressions yet.
/// Suppose we want to design a simple state machine that counts from
/// from 0 to some maximum number N-1, and then cycles again.  We
/// want to specify the maximum number, not the number of bits needed
/// to represent it.  In this case, we would like to use the
/// compile time `clog2` function to compute the bit width of
/// the signal that holds the count.
///
/// ```rust, compile_fail
/// # use rust_hdl::core::prelude::*;
///
/// #[derive(LogicBlock, Default)]
/// struct CountToN<const N: usize> {
///     signal_out: Signal<Out, Bits<{clog2({N})}>>,
/// }
/// ```
///
///
pub const fn clog2(t: usize) -> usize {
    let mut p = 0;
    let mut b = 1;
    while b < t {
        p += 1;
        b *= 2;
    }
    p
}

#[test]
fn test_clog2_is_correct() {
    assert_eq!(clog2(1024), 10);
}

/// The [Bits] type holds a bit array of size [N].
#[derive(Clone, Debug, Copy)]
pub enum Bits<const N: usize> {
    #[doc(hidden)]
    Short(ShortBitVec<N>),
    #[doc(hidden)]
    Long(BitVec<N>),
}

/// Convert from a [BigUint] to a [Bits].  Will panic if the number of bits
/// needed to represent the value are greater than the width of the [Bits].
/// ```
/// # use num_bigint::BigUint;
/// # use rust_hdl::core::bits::Bits;
/// let x = BigUint::parse_bytes(b"10111000101", 2).unwrap();
/// let y : Bits<16> = x.into();
/// println!("y = {:x}", y); // Prints y = 02c5
/// ```
/// The following will panic, because the value cannot be represented in the
/// given number of bits.
/// ```
/// # use rust_hdl::core::prelude::*;
/// # use num_bigint::BigUint;
/// let x = BigUint::parse_bytes(b"10111000101", 2).unwrap();
/// let y : Bits<12> = x.into(); // Panics
/// ```
impl<const N: usize> From<BigUint> for Bits<N> {
    fn from(x: BigUint) -> Self {
        assert!(
            x.bits() <= N as u64,
            "cannot fit value from BigUInt with {} bits into Bits<{}>",
            x.bits(),
            N
        );
        if N <= SHORT_BITS {
            x.to_u64().unwrap().into()
        } else {
            let mut ret = [false; N];
            for i in 0..N {
                ret[i] = x.bit(i as u64)
            }
            Bits::Long(ret.into())
        }
    }
}

/// Convert from a [Bits] to a [BigUint].
/// ```
/// # use rust_hdl::core::prelude::*;
/// # use num_bigint::BigUint;
/// let x : Bits<128> = 0xDEAD_BEEF_CAFE_BABE_1234_5678_u128.to_bits();
/// let y : BigUint = x.into();
/// println!("y = {:x}", y); // Prints 0xDEAD_BEEF_CAFE_BABE_1234_5678
/// ```
impl<const N: usize> From<Bits<N>> for BigUint {
    fn from(y: Bits<N>) -> Self {
        let mut x = BigUint::default();
        for i in 0..N {
            x.set_bit(i as u64, y.get_bit(i));
        }
        x
    }
}

#[cfg(test)]
fn random_bits<const N: usize>() -> Bits<N> {
    use rand::random;
    let mut x = Bits::default();
    for bit in 0..N {
        if random::<bool>() {
            x = x.replace_bit(bit, true);
        }
    }
    x
}

#[test]
fn test_biguint_roundtrip() {
    use rand::random;
    use seq_macro::seq;

    seq!(N in 5..150 {
        for _iters in 0..10 {
            let y: Bits<N> = random_bits();
            let z: BigUint = y.into();
            let h: Bits<N> = z.into();
            assert_eq!(h, y);
        }
    });
    seq!(N in 5..150 {
        for _iters in 0..10 {
            let bits = (0..N).map(|_| if random::<bool>() {
                b"1"[0]
            } else {
                b"0"[0]
            }).collect::<Vec<u8>>();
            let y = BigUint::parse_bytes(&bits, 2).unwrap();
            let z : Bits<N> = y.clone().into();
            let h : BigUint = z.into();
            assert_eq!(h, y);
        }
    });
}

#[test]
fn test_cast_from_biguint() {
    let x = BigUint::parse_bytes(b"1011000101", 2).unwrap();
    let y: Bits<16> = x.into();
    let p = format!("y = {:x}", y);
    assert_eq!(p, "y = 02c5");
    println!("y = {:x}", y);
}

/// Allows you to format a [Bits] as a binary string
/// ```
/// # use rust_hdl::core::bits::Bits;
/// let y = Bits::<16>::from(0b1011_0100_0010_0000);
/// println!("y = {:b}", y); // Prints y = 1011010000100000
/// ```
impl<const N: usize> Binary for Bits<N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for i in 0..N {
            if self.get_bit(N - 1 - i) {
                write!(f, "1")?;
            } else {
                write!(f, "0")?;
            }
        }
        Ok(())
    }
}

#[test]
fn test_print_as_binary() {
    let x = Bits::<16>::from(0b_1011_0100_1000_0000);
    let p = format!("x = {:b}", x);
    assert_eq!(p, "x = 1011010010000000")
}

/// Allows you to format a [Bits] as a lowercase hex string
/// ```
/// # use rust_hdl::core::bits::Bits;
/// let y = Bits::<16>::from(0xcafe);
/// println!("y = {:x}", y); // Prints y = cafe
/// ```
impl<const N: usize> LowerHex for Bits<N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let m: usize = N + (4 - (N % 4)) % 4; // Round up to an integer number of nibbles
        let digits: usize = m / 4;
        for digit in 0..digits {
            let nibble: Bits<4> = self.get_bits(4 * (digits - 1 - digit));
            let nibble_u8: LiteralType = nibble.into();
            std::fmt::LowerHex::fmt(&nibble_u8, f)?;
        }
        Ok(())
    }
}

#[test]
fn test_print_as_lowercase_hex() {
    let x = Bits::<16>::from(0xcafe);
    let p = format!("x = {:x}", x);
    assert_eq!(p, "x = cafe");
}

/// Allows you to format a [Bits] as an uppercase hex string
/// ```
/// # use rust_hdl::core::bits::Bits;
/// let y = Bits::<16>::from(0xcafe);
/// println!("y = {:X}", y); // Prints y = CAFE
/// ```
impl<const N: usize> UpperHex for Bits<N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let m: usize = N + (4 - (N % 4)) % 4; // Round up to an integer number of nibbles
        let digits: usize = m / 4;
        for digit in 0..digits {
            let nibble: Bits<4> = self.get_bits(4 * (digits - 1 - digit));
            let nibble_u8: LiteralType = nibble.into();
            std::fmt::UpperHex::fmt(&nibble_u8, f)?;
        }
        Ok(())
    }
}

#[test]
fn test_print_as_uppercase_hex() {
    let x = Bits::<16>::from(0xcafe);
    let p = format!("x = {:X}", x);
    assert_eq!(p, "x = CAFE");
}

/// Convenience function to construct [Bits] from an unsigned literal
/// Sometimes, you know you will be working with a value that is smaller than
/// 128 bits (the current maximum sized built-in unsigned integer in Rust).
/// In those cases, the [bits] function can make construction slightly
/// simpler.
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x : Bits<14> = bits(0xDEA);
/// assert_eq!("0dea", format!("{:x}", x))
/// ```
/// In most cases, it's easier to use `into`:
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x: Bits<14> = 0xDEA.into();
/// assert_eq!("0dea", format!("{:x}", x))
/// ```
pub fn bits<const N: usize>(x: LiteralType) -> Bits<N> {
    let t: Bits<N> = x.into();
    t
}

/// The [ToBits] trait is used to provide a way to convert Rust standard unsigned
/// types (currently `u8, u16, u32, u64, u128`) into [Bits] of different lengths.
/// Note that RustHDL will panic if you attempt to convert an unsigned type into
/// a [Bits] that is too small to hold the value.
pub trait ToBits {
    /// Convert the underlying type to a [Bits] of the specified size.  Invoked
    /// using
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<4> = 0xF_u8.to_bits();
    ///```
    fn to_bits<const N: usize>(self) -> Bits<N>;
}

impl ToBits for u8 {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        (self as LiteralType).into()
    }
}

impl ToBits for u16 {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        (self as LiteralType).into()
    }
}

impl ToBits for u32 {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        (self as LiteralType).into()
    }
}

impl ToBits for u64 {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        (self as LiteralType).into()
    }
}

impl ToBits for usize {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        (self as LiteralType).into()
    }
}

impl ToBits for u128 {
    fn to_bits<const N: usize>(self) -> Bits<N> {
        Bits::<N>::from(BigUint::from(self))
    }
}

/// Cast from one bit width to another with truncation or zero padding
/// The [bit_cast] function allows you to convert from one bit width
/// to another.  It handles the different widths in the following simplified
/// manner:
///    - if casting to a narrower bit width, the most significant bits are
///      discarded until the new value fits into the specified bits
///    - if casting to a wider bit width, the most significant bits are
///      padded with zeros until the new value occupies the specified bits
/// This may seem a bit counterintuitive, but it fits logical circuitry
/// behavior.  Narrowing is usually done by preserving the least significant
/// bits (so that the carry bits are discarded when adding, for example).
/// Widening is also usually done (for unsigned values) by zero extending
/// the most significant bits.  The [bit_cast] operation does both of
/// these operations depending on the arguments.
///
/// First, an example of widening, in this case, an extra nibble is
/// added to the most significant bits, and is set to zero.
///```
/// # use rust_hdl::core::prelude::*;
/// let x : Bits<12> = bits(0xEAF);
/// let y : Bits<16> = bit_cast(x); // M = 16, N = 12
/// assert_eq!(y, bits::<16>(0x0EAF));
///```
///
/// In the second example, we downcast, this time, discarding the most
/// significant nibble.
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x : Bits<16> = bits(0xDEAF);
/// let y : Bits<12> = bit_cast(x); // M = 12, N = 16
/// assert_eq!(y, bits::<12>(0xEAF));
/// ```
///
/// Note that internally, you can [bit_cast] from an arbitrary bit length
/// to another arbitrary bit length without losing information because of
/// any internal Rust limit.
///
/// Note also that bit-casting does _not_ preserve signedness.  Generally,
/// RustHDL follows hardware conventions that values are unsigned.  If you
/// want to work with signed bit vectors, use [Signed] instead.
pub fn bit_cast<const M: usize, const N: usize>(x: Bits<N>) -> Bits<M> {
    match x {
        Bits::Short(t) => {
            let t: ShortType = t.into();
            let t = if M < N {
                t & ShortBitVec::<M>::mask().short()
            } else {
                t
            };
            let k: Bits<M> = (t as LiteralType).into();
            k
        }
        Bits::Long(t) => {
            if M > SHORT_BITS {
                Bits::Long(t.resize())
            } else {
                let k: ShortType = t.into();
                Bits::Short(k.into())
            }
        }
    }
}

#[doc(hidden)]
impl<const N: usize> Into<VCDValue> for Bits<N> {
    fn into(self) -> VCDValue {
        if N == 1 {
            if self.get_bit(0) {
                VCDValue::Single(vcd::Value::V1)
            } else {
                VCDValue::Single(vcd::Value::V0)
            }
        } else {
            let mut x = vec![];
            for i in 0..N {
                if self.get_bit(N - 1 - i) {
                    x.push(vcd::Value::V1)
                } else {
                    x.push(vcd::Value::V0)
                }
            }
            VCDValue::Vector(x)
        }
    }
}

#[test]
fn test_bits_from_int_via_bits() {
    let x: Bits<23> = bits(23);
    let u: LiteralType = x.into();
    assert_eq!(u, 23);
}

impl<const N: usize> Bits<N> {
    #[inline(always)]
    /// The [any] function returns true if any of the
    /// individual bits are true, and false otherwise.
    /// This reduction operation is equivalent to a logical
    /// OR of all the bits in the vector.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<14> = bits(0xDEA);
    /// assert_eq!(x.any(), true);
    /// let y : Bits<14> = Bits::default();
    /// assert_eq!(y.any(), false);
    /// ```
    pub fn any(&self) -> bool {
        match self {
            Bits::Short(x) => x.any(),
            Bits::Long(x) => x.any(),
        }
    }

    #[inline(always)]
    /// The [all] function returns true if all of the individual
    /// bits are true, and false otherwise.  This reduction
    /// operation is equivalent to a logical AND of all the bits
    /// in the vector.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<14> = bits(0xDEA);
    /// assert_eq!(x.all(), false);
    /// let y : Bits<14> = bits(0x3FFF);
    /// assert_eq!(y.all(), true);
    /// ```
    pub fn all(&self) -> bool {
        match self {
            Bits::Short(x) => x.all(),
            Bits::Long(x) => x.all(),
        }
    }

    #[inline(always)]
    /// The [xor] function computes the exclusive OR of all
    /// the bits in the vector.  This is equivalent to counting
    /// the number of ones.  If the number is odd, the XOR will
    /// be true.  If even, it will be false.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<12> = bits(0b1100_0100_1100);
    /// assert_eq!(x.xor(), true);
    /// let y: Bits<12> = bits(0b1100_0110_1100);
    /// assert_eq!(y.xor(), false);
    /// ```
    pub fn xor(&self) -> bool {
        match self {
            Bits::Short(x) => x.xor(),
            Bits::Long(x) => x.xor(),
        }
    }

    /// The [index] function is used when a [Bits] is going
    /// to be used to index into an array or some other bit vector.
    /// This is typically a very specialized hardware operation,
    /// so there are limited cases in which it can be used.  Also,
    /// there is an assumption that the [Bits] being used as
    /// an index is sufficiently small to fit in a natural word (assume 32 bits, here
    /// for safety).  In practice, that means, that if you are
    /// indexing into a register using some other register/value,
    /// the _length_ of the register is limited to a few billion bits.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<12> = bits(0b1100_0100_1100);
    /// assert_eq!(x.index(), 0b1100_0100_1100_usize);
    /// ```
    pub fn index(&self) -> usize {
        match self {
            Bits::Short(x) => x.short() as usize,
            Bits::Long(_x) => panic!("Cannot map long bit vector to index type"),
        }
    }

    #[inline(always)]
    /// Return the number of bits in the current [Bits].
    /// Because this is determined at compile time, it is
    /// of limited use as a runtime function, but is there
    /// nonetheless.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<14> = Bits::default();
    /// assert_eq!(x.len(), 14);
    /// ```
    pub fn len(&self) -> usize {
        N
    }

    /// Compute the number of possible values that a [Bits]
    /// can take.  This is basically 2 raised to the Nth
    /// power.  Because the result is returned as a [usize],
    /// you must be careful, since this can easily overflow.
    /// A [Bits<256>] for example, cannot represent [count]
    /// on a normal 64 bit machine.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// assert_eq!(Bits::<16>::count(), 1 << 16);
    /// ```
    pub fn count() -> u128 {
        1 << N
    }

    #[inline(always)]
    /// Extract the [index] bit from the given [Bits]. This will
    /// cause a runtime panic if the [index] bit is out of range
    /// of the width of the bitvector.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<14> = bits(0b10110);
    /// assert_eq!(x.get_bit(0), false);
    /// assert_eq!(x.get_bit(1), true);
    /// assert_eq!(x.get_bit(2), true); // You get the idea
    /// ```
    pub fn get_bit(&self, index: usize) -> bool {
        assert!(index < N);
        match self {
            Bits::Short(x) => x.get_bit(index),
            Bits::Long(x) => x.get_bit(index),
        }
    }

    /// Replace the given bit of a [Bits] with a new bit value.
    /// This method leaves the original value alone, and returns
    /// a new [Bits] with all bits except the designated one left
    /// alone.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<16> = bits(0b1100_0000);
    /// let x = x.replace_bit(0, true);
    /// let x = x.replace_bit(7, false);
    /// assert_eq!(x, bits(0b0100_0001));
    /// ```
    pub fn replace_bit(&self, index: usize, val: bool) -> Self {
        assert!(index < N);
        match self {
            Bits::Short(x) => Bits::Short(x.replace_bit(index, val)),
            Bits::Long(x) => Bits::Long(x.replace_bit(index, val)),
        }
    }

    #[inline(always)]
    /// Return a subset of bits from a [Bits] value, with a given offset.
    /// To preserve the feasibility of representing this in hardware, the width
    /// of the result must be fixed (the argument [M]), and only the offset
    /// can be computed.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<40> = bits(0xDEAD_BEEF_CA);
    /// let y = x.get_bits::<32>(8);
    /// assert_eq!(y, bits(0xDEAD_BEEF))
    /// ```
    pub fn get_bits<const M: usize>(&self, index: usize) -> Bits<M> {
        assert!(index <= N);
        bit_cast::<M, N>(*self >> index as LiteralType)
    }

    #[inline(always)]
    /// Set a group of bits in a value.  This operation modifies the
    /// value in place.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let mut x: Bits<40> = bits(0xDEAD_BEEF_CA);
    /// x.set_bits::<16>(8, bits(0xCAFE));
    /// assert_eq!(x, bits(0xDEAD_CAFE_CA));
    /// ```
    pub fn set_bits<const M: usize>(&mut self, index: usize, rhs: Bits<M>) {
        assert!(index <= N);
        assert!(index + M <= N);
        let mask = !(bit_cast::<N, M>(Bits::<M>::mask()) << index as LiteralType);
        let masked = *self & mask;
        let replace = bit_cast::<N, M>(rhs) << index as LiteralType;
        *self = masked | replace
    }

    #[inline(always)]
    /// Returns a [Bits] value that contains [N] ones.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x = Bits::<40>::mask();
    /// assert_eq!(x, bits(0xFF_FFFF_FFFF));
    /// ```
    pub fn mask() -> Bits<N> {
        if N <= SHORT_BITS {
            Bits::Short(ShortBitVec::<N>::mask())
        } else {
            Bits::Long([true; N].into())
        }
    }

    /// Returns the width in bits of the [BitVec].
    /// Note that this is the number of bits allocated.
    /// It does not depend on the value at all.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// assert_eq!(Bits::<40>::width(), 40);
    /// ```
    pub const fn width() -> usize {
        N
    }

    /// Convert [Bits] to an [u8].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<6> = 12.into();
    /// let y = x.to_u8();
    /// assert_eq!(y, 12_u8);
    /// ```
    /// Note that this will panic if the width of the
    /// bitvector is larger than 8 bits.
    /// ```should_panic
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<12> = 0xADE.into();
    /// let y = x.to_u8(); // Panics - too many bits
    /// ```
    pub fn to_u8(self) -> u8 {
        assert!(N <= 8, "Cannot convert Bits::<{}> to u8 - too many bits", N);
        let x: LiteralType = self.into();
        x as u8
    }

    /// Convert [Bits] to an [u16].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<12> = 12.into();
    /// let y = x.to_u16();
    /// assert_eq!(y, 12_u16);
    /// ```
    /// Note that this will panic if the width of the
    /// bitvector is larger than 16 bits.
    /// ```should_panic
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<20> = 0xADE.into();
    /// let y = x.to_u16(); // Panics - too many bits
    /// ```
    pub fn to_u16(self) -> u16 {
        assert!(
            N <= 16,
            "Cannot convert Bits::<{}> to u16 - too many bits",
            N
        );
        let x: LiteralType = self.into();
        x as u16
    }

    /// Convert [Bits] to an [u32].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<24> = 12.into();
    /// let y = x.to_u32();
    /// assert_eq!(y, 12_u32);
    /// ```
    /// Note that this will panic if the width of the
    /// bitvector is larger than 32 bits.
    /// ```should_panic
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<40> = 0xADE.into();
    /// let y = x.to_u32(); // Panics - too many bits
    /// ```
    pub fn to_u32(self) -> u32 {
        assert!(
            N <= 32,
            "Cannot convert Bits::<{}> to u32 - too many bits",
            N
        );
        let x: LiteralType = self.into();
        x as u32
    }

    /// Convert [Bits] to an [u64].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<40> = 12.into();
    /// let y = x.to_u64();
    /// assert_eq!(y, 12_u64);
    /// ```
    /// Note that this will panic if the width of the
    /// bitvector is larger than 64 bits.
    /// ```should_panic
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<80> = 0xADE.into();
    /// let y = x.to_u64(); // Panics - too many bits
    /// ```
    pub fn to_u64(self) -> u64 {
        assert!(
            N <= 64,
            "Cannot convert Bits::<{}> to u64 - too many bits",
            N
        );
        let x: LiteralType = self.into();
        x as u64
    }

    /// Convert [Bits] to an [u128].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<80> = 12.into();
    /// let y = x.to_u128();
    /// assert_eq!(y, 12_u128);
    /// ```
    /// Note that this will panic if the width of the
    /// bitvector is larger than 128 bits.
    /// ```should_panic
    /// # use rust_hdl::core::prelude::*;
    /// let x: Bits<140> = 0xADE.into();
    /// let y = x.to_u128(); // Panics - too many bits
    /// ```
    pub fn to_u128(self) -> u128 {
        match self {
            Bits::Short(x) => x.to_u128(),
            Bits::Long(x) => x.to_u128(),
        }
    }
}

impl From<bool> for Bits<1> {
    #[inline(always)]
    /// Convenience method that allows you to convert
    /// a boolean into a single bit-width [Bits].
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<1> = true.into();
    /// assert_eq!(x, bits(1))
    /// ```
    fn from(x: bool) -> Self {
        if x {
            1.into()
        } else {
            0.into()
        }
    }
}

impl Into<bool> for Bits<1> {
    #[inline(always)]
    /// Convenience method for converting a 1-bit
    /// width [Bits] value into a boolean value.
    /// ```
    /// # use rust_hdl::core::prelude::*;
    /// let x : Bits<1> = bits(1);
    /// let y : bool = x.into();
    /// assert!(y)
    /// ```
    fn into(self) -> bool {
        self.get_bit(0)
    }
}

/// Convert from [LiteralType] to [Bits].  Because of some restrictions on
/// how Rust's type inference works, when you work with unsized
/// literals (e.g., `x = 3`), there must either be a unique integer type
/// that can fit the expression, or it will default to `i32`.  Unfortunately,
/// in general, [Bits] are used to represent unsigned types.  The upshot
/// of all this is that RustHDL selects one unsigned integer type to represent
/// literals.  Although not ideal, RustHDL uses a [LiteralType] (currently 'u64') to represent literals
/// so as to make HDL expressions close to Verilog or VHDL.  This choice should not affect
/// any hardware implementation, as hardware registers need to be of [Bits] type.
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x: Bits<16> = 0xDEAD.into(); // This is interpreteed as a 128 bit constant by Rust
/// ```
/// This example is the largest bit width literal you can express using current
/// edition Rust:
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x: Bits<128> = 0xDEADBEEF_CAFEBABE_1234ABCD_00005EA1_u128.to_bits();
/// ```
/// From a safety perspective, RustHDL will panic if the argument is too large to fit
/// into the bit vector.  Thus, this example will panic, since the literal cannot be
/// fit into 16 bits without truncation:
/// ```should_panic
/// # use rust_hdl::core::prelude::*;
/// let x: Bits<16> = 0xDEADBEEF.into(); // This will panic!
/// ```
impl<const N: usize> From<LiteralType> for Bits<N> {
    fn from(x: LiteralType) -> Self {
        if N > SHORT_BITS {
            let y: BitVec<N> = x.into();
            Bits::Long(y)
        } else {
            assert!(
                x <= ShortBitVec::<N>::max_legal(),
                "Value 0x{:x} does not fit into bitvector of length {}",
                x,
                N
            );
            Bits::Short((x as ShortType).into())
        }
    }
}

impl<const N: usize> From<Wrapping<LiteralType>> for Bits<N> {
    fn from(x: Wrapping<LiteralType>) -> Self {
        x.0.into()
    }
}

/// Convert a [Bits] back to [u128].  Until Rust supports larger integers, the [u128] is
/// the largest integer type you can use without resorting to [BigUint].  RustHDL will panic
/// if you try to convert a [Bits] that is more than 128 bits to a literal.  Even if the
/// value in the bitvector would fit.
///```
///# use rust_hdl::core::prelude::*;
/// let x: Bits<16> = 0xDEAD.into();
/// let y: u128 = x.to_u128();
/// assert_eq!(y, 0xDEAD);
///```
///The following will panic even through the literal value stored in the 256 bit vector
///is less than 128 bits.
///```should_panic
///# use rust_hdl::core::prelude::*;
///let x : Bits<256> = 42.into();
///let y: u128 = x.to_u128(); // Panics!
/// ```
impl<const N: usize> From<Bits<N>> for LiteralType {
    fn from(x: Bits<N>) -> Self {
        assert!(N <= LITERAL_BITS);
        match x {
            Bits::Short(t) => {
                let p: ShortType = t.into();
                p as LiteralType
            }
            Bits::Long(t) => t.into(),
        }
    }
}

#[inline(always)]
#[doc(hidden)]
fn binop<Tshort, TLong, const N: usize>(
    a: Bits<N>,
    b: Bits<N>,
    short_op: Tshort,
    long_op: TLong,
) -> Bits<N>
where
    Tshort: Fn(ShortBitVec<N>, ShortBitVec<N>) -> ShortBitVec<N>,
    TLong: Fn(BitVec<N>, BitVec<N>) -> BitVec<N>,
{
    match a {
        Bits::Short(x) => match b {
            Bits::Short(y) => Bits::Short(short_op(x, y)),
            _ => {
                unreachable!()
            }
        },
        Bits::Long(x) => match b {
            Bits::Long(y) => Bits::Long(long_op(x, y)),
            _ => {
                unreachable!()
            }
        },
    }
}

macro_rules! op {
    ($func: ident, $method: ident, $op: tt) => {
        #[doc(hidden)]
        impl<const N: usize> std::ops::$method<Bits<N>> for Bits<N> {
            type Output = Bits<N>;

            #[inline(always)]
            fn $func(self, rhs: Bits<N>) -> Self::Output {
                binop(self, rhs, |a, b| a $op b, |a, b| a $op b)
            }
        }

        impl<const N: usize> std::ops::$method<LiteralType> for Bits<N> {
            type Output = Bits<N>;

            fn $func(self, rhs: LiteralType) -> Self::Output {
                binop(self, rhs.into(), |a, b| a $op b, |a, b| a $op b)
            }
        }

        impl<const N: usize> std::ops::$method<Bits<N>> for LiteralType {
            type Output = Bits<N>;

            #[inline(always)]
            fn $func(self, rhs: Bits<N>) -> Self::Output {
                binop(self.into(), rhs.into(), |a, b| a $op b, |a, b| a $op b)
            }
        }
    }
}

op!(add, Add, +);
op!(sub, Sub, -);
op!(bitor, BitOr, |);
op!(bitand, BitAnd, &);
op!(bitxor, BitXor, ^);
op!(shr, Shr, >>);
op!(shl, Shl, <<);

/// Construct a default [Bits] - i.e., a zero bit vector of length N.
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x : Bits<200> = Default::default();
/// assert_eq!(x, bits(0));
/// ```
impl<const N: usize> Default for Bits<N> {
    fn default() -> Bits<N> {
        bits::<N>(0)
    }
}

/// Bitwise inversion of a [Bits] vector
/// The `!` operator will invert each bit in a [Bits] vector.
/// ```
/// # use rust_hdl::core::prelude::*;
/// let x : Bits<16> = bits(0xAAAA);
/// let y = !x;
/// assert_eq!(y, bits(0x5555))
/// ```
impl<const N: usize> std::ops::Not for Bits<N> {
    type Output = Bits<N>;

    fn not(self) -> Self::Output {
        match self {
            Bits::Short(x) => Bits::Short(!x),
            Bits::Long(x) => Bits::Long(!x),
        }
    }
}

#[doc(hidden)]
impl<const N: usize> Ord for Bits<N> {
    fn cmp(&self, other: &Bits<N>) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

#[doc(hidden)]
impl<const N: usize> PartialOrd<Bits<N>> for LiteralType {
    fn partial_cmp(&self, other: &Bits<N>) -> Option<Ordering> {
        let self_as_bits: Bits<N> = (*self).into();
        self_as_bits.partial_cmp(other)
    }
}

#[doc(hidden)]
impl<const N: usize> PartialOrd<LiteralType> for Bits<N> {
    fn partial_cmp(&self, other: &LiteralType) -> Option<Ordering> {
        let other_as_bits: Bits<N> = (*other).into();
        self.partial_cmp(&other_as_bits)
    }
}

#[doc(hidden)]
impl<const N: usize> PartialOrd<Bits<N>> for Bits<N> {
    #[inline(always)]
    fn partial_cmp(&self, other: &Bits<N>) -> Option<Ordering> {
        match self {
            Bits::Short(x) => match other {
                Bits::Short(y) => x.partial_cmp(y),
                _ => panic!("Short Long case"),
            },
            Bits::Long(x) => match other {
                Bits::Long(y) => x.partial_cmp(y),
                _ => panic!("Long short case"),
            },
        }
    }
}

#[doc(hidden)]
impl<const N: usize> PartialEq<Bits<N>> for Bits<N> {
    #[inline(always)]
    fn eq(&self, other: &Bits<N>) -> bool {
        match self {
            Bits::Short(x) => match other {
                Bits::Short(y) => x == y,
                _ => panic!("Short Long case"),
            },
            Bits::Long(x) => match other {
                Bits::Long(y) => x == y,
                _ => panic!("Long Short case"),
            },
        }
    }
}

#[doc(hidden)]
impl<const N: usize> PartialEq<LiteralType> for Bits<N> {
    fn eq(&self, other: &LiteralType) -> bool {
        let other_as_bits: Bits<N> = (*other).into();
        self.eq(&other_as_bits)
    }
}

#[doc(hidden)]
impl<const N: usize> PartialEq<Bits<N>> for LiteralType {
    fn eq(&self, other: &Bits<N>) -> bool {
        let self_as_bits: Bits<N> = (*self).into();
        self_as_bits.eq(other)
    }
}

#[doc(hidden)]
impl PartialEq<bool> for Bits<1> {
    #[inline(always)]
    fn eq(&self, other: &bool) -> bool {
        self.get_bit(0) == *other
    }
}

#[doc(hidden)]
impl PartialEq<Bits<1>> for bool {
    fn eq(&self, other: &Bits<1>) -> bool {
        *self == other.get_bit(0)
    }
}

#[doc(hidden)]
impl<const N: usize> Eq for Bits<N> {}

#[doc(hidden)]
impl<const N: usize> std::hash::Hash for Bits<N> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Bits::Short(t) => t.hash(state),
            Bits::Long(t) => t.hash(state),
        }
    }
}

#[doc(hidden)]
impl<const N: usize> std::ops::Add<bool> for Bits<N> {
    type Output = Bits<N>;

    fn add(self, rhs: bool) -> Self::Output {
        if rhs {
            self + Bits::<N>::from(1)
        } else {
            self
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{bit_cast, clog2, Bits};
    use crate::core::bits::random_bits;
    use crate::core::bits::{LiteralType, ToBits};
    use num_bigint::BigUint;
    use num_traits::One;
    use seq_macro::seq;
    use std::num::Wrapping;

    #[test]
    fn test_get_bits_section() {
        let x: Bits<40> = 0xD_ADBE_EFCA.into();
        let y = x.get_bits::<32>(8).to_u32();
        let answer = 0xDAD_BEEF;
        assert_eq!(y, answer);
    }

    #[test]
    fn test_short_from_u8() {
        let x: Bits<4> = 15.into();
        let y: LiteralType = x.into();
        assert_eq!(y, 15 & (0x0F));
    }

    #[test]
    fn test_short_from_u16() {
        let x: Bits<12> = 1432.into();
        let y: LiteralType = x.into();
        assert_eq!(y, 1432 & (0x0FFF));
    }

    #[test]
    fn test_short_from_u32() {
        let x: Bits<64> = 12434234.into();
        let y: LiteralType = x.into();
        assert_eq!(y, 12434234);
    }

    #[test]
    fn test_from_u32() {
        let x: Bits<64> = 0xFFFF_FFFF.into();
        let y: LiteralType = x.into();
        assert_eq!(y, 0xFFFF_FFFF);
    }

    #[test]
    fn or_test() {
        let a: Bits<32> = 45.into();
        let b: Bits<32> = 10395.into();
        let c = a | b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 45 | 10395)
    }
    #[test]
    fn and_test() {
        let a: Bits<32> = 45.into();
        let b: Bits<32> = 10395.into();
        let c = a & b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 45 & 10395)
    }
    #[test]
    fn xor_test() {
        let a: Bits<32> = 45.into();
        let b: Bits<32> = 10395.into();
        let c = a ^ b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 45 ^ 10395)
    }
    #[test]
    fn not_test() {
        let a: Bits<32> = 45.into();
        let c = !a;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, (!45_u32) as LiteralType);
    }
    #[test]
    fn shr_test() {
        let a: Bits<32> = 10395.into();
        let c: Bits<32> = a >> 4;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 10395 >> 4);
    }
    #[test]
    fn shr_test_pair() {
        let a: Bits<32> = 10395.into();
        let b: Bits<32> = 4.into();
        let c = a >> b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 10395 >> 4);
    }
    #[test]
    fn shl_test() {
        let a: Bits<32> = 10395.into();
        let c = a << 24;
        let c_u32 = c.to_u32();
        assert_eq!(c_u32, 10395 << 24);
    }
    #[test]
    fn shl_test_pair() {
        let a: Bits<32> = 10395.into();
        let b: Bits<32> = 4.into();
        let c = a << b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 10395 << 4);
    }
    #[test]
    fn add_works() {
        let a: Bits<32> = 10234.into();
        let b: Bits<32> = 19423.into();
        let c = a + b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 10234 + 19423);
    }
    #[test]
    fn add_int_works() {
        let a: Bits<32> = 10234.into();
        let b = 19423;
        let c: Bits<32> = a + b;
        let c_u32: LiteralType = c.into();
        assert_eq!(c_u32, 10234 + 19423);
    }
    #[test]
    fn add_works_with_overflow() {
        let x = 2_042_102_334_u32;
        let y = 2_942_142_512_u32;
        let a: Bits<32> = x.to_bits();
        let b: Bits<32> = y.to_bits();
        let c = a + b;
        let c_u32 = c.to_u32();
        assert_eq!(Wrapping(c_u32), Wrapping(x) + Wrapping(y));
    }
    #[test]
    fn sub_works() {
        let x = 2_042_102_334_u32;
        let y = 2_942_142_512_u32;
        let a: Bits<32> = x.to_bits();
        let b: Bits<32> = y.to_bits();
        let c = a - b;
        let c_u32 = c.to_u32();
        assert_eq!(Wrapping(c_u32), Wrapping(x) - Wrapping(y));
    }
    #[test]
    fn sub_int_works() {
        let x = 2_042_102_334_u32;
        let y = 2_942_142_512;
        let a: Bits<32> = x.to_bits();
        let c = a - y;
        let c_u32 = c.to_u32();
        assert_eq!(Wrapping(c_u32), Wrapping(x) - Wrapping(y as u32));
    }
    #[test]
    fn eq_works() {
        let x = 2_032_142_351;
        let y = 2_942_142_512;
        let a: Bits<32> = x.into();
        let b: Bits<32> = x.into();
        let c: Bits<32> = y.into();
        assert_eq!(a, b);
        assert_ne!(a, c)
    }
    #[test]
    fn mask_works() {
        let a: Bits<48> = 0xFFFF_FFFF_FFFF.into();
        let b = Bits::<48>::mask();
        assert_eq!(a, b);
        let a: Bits<16> = 0xFFFF.into();
        let b = Bits::<16>::mask();
        assert_eq!(a, b)
    }
    #[test]
    fn get_bit_works() {
        // 0101 = 5
        let a: Bits<48> = 0xFFFF_FFFF_FFF5.into();
        assert!(a.get_bit(0));
        assert!(!a.get_bit(1));
        assert!(a.get_bit(2));
        assert!(!a.get_bit(3));
        let c: Bits<5> = 3.into();
        assert!(!a.get_bit(c.index()));
    }
    #[test]
    fn test_bit_cast_short() {
        let a: Bits<8> = 0xFF.into();
        let b: Bits<16> = bit_cast(a);
        assert_eq!(b, 0xFF);
        let c: Bits<4> = bit_cast(a);
        assert_eq!(c, 0xF);
    }
    #[test]
    fn test_bit_cast_long() {
        let a: Bits<48> = 0xdead_cafe_babe.into();
        let b: Bits<44> = bit_cast(a);
        assert_eq!(b, 0xead_cafe_babe);
        let b: Bits<32> = bit_cast(a);
        assert_eq!(b, 0xcafe_babe);
    }
    #[test]
    fn test_bit_extract_long() {
        let a: Bits<48> = 0xdead_cafe_babe.into();
        let b: Bits<44> = a.get_bits(4);
        assert_eq!(b, 0xdead_cafe_bab);
        let b: Bits<32> = a.get_bits(16);
        assert_eq!(b, 0xdead_cafe);
    }
    #[test]
    fn test_set_bit() {
        let a: Bits<48> = 0xdead_cafe_babe.into();
        let mut b = a;
        for i in 4..8 {
            b = b.replace_bit(i, false)
        }
        assert_eq!(b, 0xdead_cafe_ba0e);
    }
    #[test]
    fn test_set_bits() {
        let a: Bits<16> = 0xdead.into();
        let b: Bits<4> = 0xf.into();
        let mut c = a.clone();
        c.set_bits(4, b);
        assert_eq!(c, 0xdefd);
        let a: Bits<48> = 0xdead_cafe_babe.into();
        let b: Bits<8> = 0xde.into();
        let mut c = a.clone();
        c.set_bits(16, b);
        assert_eq!(c, 0xdead_cade_babe);
    }

    #[test]
    fn test_constants_and_bits() {
        let a: Bits<16> = 0xdead.into();
        let b = a + 1;
        let c = 1 + a;
        println!("{:x}", b);
        assert_eq!(b, 0xdeae);
        assert_eq!(b, c);
    }
    #[test]
    fn test_clog2() {
        const A_WIDTH: usize = clog2(250);
        let a: Bits<{ A_WIDTH }> = 153.into();
        println!("{:x}", a);
        assert_eq!(a.len(), 8);
        assert_eq!(clog2(1024), 10);
    }

    #[test]
    fn test_clog2_inline() {
        const A_WIDTH: usize = clog2(1000);
        let a: Bits<A_WIDTH> = 1023.into();
        assert_eq!(a.len(), 10);
    }

    #[test]
    fn test_default() {
        const N: usize = 128;
        let a = Bits::<N>::default();
        assert_eq!(a, 0);
    }

    #[test]
    fn test_get_bits() {
        fn get_bits_test<const N: usize, const M: usize>() {
            for offset in 0_usize..N {
                let y: Bits<N> = random_bits();
                let z = y.get_bits::<M>(offset);
                let yb: BigUint = y.into();
                let yb = (yb >> offset) & ((BigUint::one() << M) - BigUint::one());
                let zb: BigUint = z.into();
                assert_eq!(zb, yb);
            }
        }
        seq!(N in 0..16 {
            get_bits_test::<8, N>();
        });
        seq!(N in 0..64 {
            get_bits_test::<32, N>();
        });
        seq!(N in 0..65 {
            get_bits_test::<64, N>();
        });
        seq!(N in 0..300 {
            get_bits_test::<256, N>();
        });
        seq!(N in 0..150 {
            get_bits_test::<125, N>();
        });
    }

    #[test]
    fn test_bitcast() {
        fn bitcast_test<const N: usize, const M: usize>() {
            let y: Bits<N> = random_bits();
            let z = bit_cast::<M, N>(y);
            let yb: BigUint = y.into();
            let zb = yb & ((BigUint::one() << M) - BigUint::one());
            let zc: BigUint = z.into();
            assert_eq!(zb, zc);
        }
        fn bitcast_test_set<const M: usize>() {
            bitcast_test::<M, 1>();
            bitcast_test::<M, 8>();
            bitcast_test::<M, 16>();
            bitcast_test::<M, 32>();
            bitcast_test::<M, 64>();
            bitcast_test::<M, 128>();
            bitcast_test::<M, 256>();
        }
        bitcast_test_set::<1>();
        bitcast_test_set::<8>();
        bitcast_test_set::<16>();
        bitcast_test_set::<32>();
        bitcast_test_set::<64>();
        bitcast_test_set::<128>();
        bitcast_test_set::<256>();
    }

    #[test]
    fn test_any() {
        seq!(N in 1..150 {
            for _rep in 0..10 {
                let y: Bits<N> = random_bits();
                let z : BigUint = y.into();
                let y_any = y.any();
                let z_any = z.count_ones() != 0;
                assert_eq!(y_any, z_any)
            }
            let y = Bits::<N>::default();
            assert_eq!(y.any(), false);
        });
    }

    #[test]
    fn test_all() {
        seq!(N in 1..150 {
            for _rep in 0..10 {
                let y: Bits<N> = random_bits();
                let z : BigUint = y.into();
                let y_all = y.all();
                let z_all = z.count_ones() == N as u64;
                assert_eq!(y_all, z_all)
            }
            let y = Bits::<N>::mask();
            assert_eq!(y.all(), true);
        });
    }

    #[test]
    fn test_shl() {
        seq!(N in 1..150 {
            for l in 0..N {
                let y: Bits<N> = random_bits();
                let z: Bits<N> = y << l;
                let y1 : BigUint = y.into();
                let mask : BigUint = (BigUint::one() << N) - BigUint::one();
                let z1 = (y1 << l) & mask;
                let convert : BigUint = z.into();
                assert_eq!(z1, convert)
            }
        });
    }

    #[test]
    fn test_shr() {
        seq!(N in 1..150 {
            for l in 0..N {
                let y: Bits<N> = random_bits();
                let z: Bits<N> = y >> l;
                let y1 : BigUint = y.into();
                let mask : BigUint = (BigUint::one() << N) - BigUint::one();
                let z1 = (y1 >> l) & mask;
                let convert : BigUint = z.into();
                assert_eq!(z1, convert)
            }
        });
    }

    macro_rules! test_op_with_values {
        ($func: ident) => {
            seq!(N in 1..150 {
                for _iters in 0..10 {
                    let y: Bits<N> = random_bits();
                    let z: Bits<N> = random_bits();
                    let v1_as_bint : BigUint = y.into();
                    let v2_as_bint : BigUint = z.into();
                    let mask : BigUint = (BigUint::one() << N) - BigUint::one();
                    let (lib_answer, biguint_answer) = $func(y, z, v1_as_bint, v2_as_bint, mask);
                    let convert : BigUint = lib_answer.into();
                    assert_eq!(biguint_answer, convert)
                }
            });
        }
    }

    #[test]
    fn test_add() {
        fn add<const N: usize>(
            y: Bits<N>,
            z: Bits<N>,
            y1: BigUint,
            z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            (y + z, (y1 + z1) & mask)
        }
        test_op_with_values!(add);
    }

    #[test]
    fn test_sub() {
        fn sub<const N: usize>(
            y: Bits<N>,
            z: Bits<N>,
            y1: BigUint,
            z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            if z1 <= y1 {
                (y - z, (y1 - z1))
            } else {
                (y - z, mask + BigUint::one() + y1 - z1)
            }
        }
        test_op_with_values!(sub);
    }

    #[test]
    fn test_bitor() {
        fn bor<const N: usize>(
            y: Bits<N>,
            z: Bits<N>,
            y1: BigUint,
            z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            (y | z, (y1 | z1) & mask)
        }
        test_op_with_values!(bor);
    }

    #[test]
    fn test_bitand() {
        fn band<const N: usize>(
            y: Bits<N>,
            z: Bits<N>,
            y1: BigUint,
            z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            (y & z, (y1 & z1) & mask)
        }
        test_op_with_values!(band);
    }

    #[test]
    fn test_bitxor() {
        fn bxor<const N: usize>(
            y: Bits<N>,
            z: Bits<N>,
            y1: BigUint,
            z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            (y ^ z, (y1 ^ z1) & mask)
        }
        test_op_with_values!(bxor);
    }

    #[test]
    fn test_not() {
        fn not<const N: usize>(
            y: Bits<N>,
            _z: Bits<N>,
            y1: BigUint,
            _z1: BigUint,
            mask: BigUint,
        ) -> (Bits<N>, BigUint) {
            (!y, (y1 ^ mask))
        }
        test_op_with_values!(not);
    }

    macro_rules! test_cmp_with_values {
        ($func: ident) => {
            seq!(N in 1..256 {
                for _iters in 0..10 {
                    let y: Bits<N> = random_bits();
                    let z: Bits<N> = random_bits();
                    let v1_as_bint : BigUint = y.into();
                    let v2_as_bint : BigUint = z.into();
                    let (lib_answer, biguint_answer) = $func(y, z, v1_as_bint, v2_as_bint);
                    assert_eq!(lib_answer, biguint_answer)
                }
            });
        }
    }

    #[test]
    fn test_lt() {
        fn lt<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y < z, y1 < z1)
        }
        test_cmp_with_values!(lt);
    }

    #[test]
    fn test_le() {
        fn le<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y <= z, y1 <= z1)
        }
        test_cmp_with_values!(le);
    }

    #[test]
    fn test_eq() {
        fn eq<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y == z, y1 == z1)
        }
        test_cmp_with_values!(eq);
    }

    #[test]
    fn test_neq() {
        fn neq<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y != z, y1 != z1)
        }
        test_cmp_with_values!(neq);
    }

    #[test]
    fn test_ge() {
        fn ge<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y >= z, y1 >= z1)
        }
        test_cmp_with_values!(ge);
    }

    #[test]
    fn test_gt() {
        fn gt<const N: usize>(y: Bits<N>, z: Bits<N>, y1: BigUint, z1: BigUint) -> (bool, bool) {
            (y > z, y1 > z1)
        }
        test_cmp_with_values!(gt);
    }
}

/// A type alias for a simple bool.  You can use them interchangeably.
pub type Bit = bool;

/// Multipliers are special, so we only implement multipliers that we think are
/// synthesizable.  In this case, we implement a 16 x 16 bit multiplier
/// which yields a 32 bit result.
impl std::ops::Mul<Bits<16>> for Bits<16> {
    type Output = Bits<32>;

    fn mul(self, rhs: Bits<16>) -> Self::Output {
        let x = match self {
            Bits::Short(x) => x.short(),
            Bits::Long(_) => {
                panic!("unreachable!")
            }
        };
        let y = match rhs {
            Bits::Short(x) => x.short(),
            Bits::Long(_) => {
                panic!("unreachable!")
            }
        };
        Bits::Short(ShortBitVec::from(x * y))
    }
}