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
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- U I N T P --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Output; use Output;
with Tree_IO; use Tree_IO;
with GNAT.HTable; use GNAT.HTable;
package body Uintp is
------------------------
-- Local Declarations --
------------------------
Uint_Int_First : Uint := Uint_0;
-- Uint value containing Int'First value, set by Initialize. The initial
-- value of Uint_0 is used for an assertion check that ensures that this
-- value is not used before it is initialized. This value is used in the
-- UI_Is_In_Int_Range predicate, and it is right that this is a host value,
-- since the issue is host representation of integer values.
Uint_Int_Last : Uint;
-- Uint value containing Int'Last value set by Initialize
UI_Power_2 : array (Int range 0 .. 64) of Uint;
-- This table is used to memoize exponentiations by powers of 2. The Nth
-- entry, if set, contains the Uint value 2**N. Initially UI_Power_2_Set
-- is zero and only the 0'th entry is set, the invariant being that all
-- entries in the range 0 .. UI_Power_2_Set are initialized.
UI_Power_2_Set : Nat;
-- Number of entries set in UI_Power_2;
UI_Power_10 : array (Int range 0 .. 64) of Uint;
-- This table is used to memoize exponentiations by powers of 10 in the
-- same manner as described above for UI_Power_2.
UI_Power_10_Set : Nat;
-- Number of entries set in UI_Power_10;
Uints_Min : Uint;
Udigits_Min : Int;
-- These values are used to make sure that the mark/release mechanism does
-- not destroy values saved in the U_Power tables or in the hash table used
-- by UI_From_Int. Whenever an entry is made in either of these tables,
-- Uints_Min and Udigits_Min are updated to protect the entry, and Release
-- never cuts back beyond these minimum values.
Int_0 : constant Int := 0;
Int_1 : constant Int := 1;
Int_2 : constant Int := 2;
-- These values are used in some cases where the use of numeric literals
-- would cause ambiguities (integer vs Uint).
----------------------------
-- UI_From_Int Hash Table --
----------------------------
-- UI_From_Int uses a hash table to avoid duplicating entries and wasting
-- storage. This is particularly important for complex cases of back
-- annotation.
subtype Hnum is Nat range 0 .. 1022;
function Hash_Num (F : Int) return Hnum;
-- Hashing function
package UI_Ints is new Simple_HTable (
Header_Num => Hnum,
Element => Uint,
No_Element => No_Uint,
Key => Int,
Hash => Hash_Num,
Equal => "=");
-----------------------
-- Local Subprograms --
-----------------------
function Direct (U : Uint) return Boolean;
pragma Inline (Direct);
-- Returns True if U is represented directly
function Direct_Val (U : Uint) return Int;
-- U is a Uint for is represented directly. The returned result is the
-- value represented.
function GCD (Jin, Kin : Int) return Int;
-- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
procedure Image_Out
(Input : Uint;
To_Buffer : Boolean;
Format : UI_Format);
-- Common processing for UI_Image and UI_Write, To_Buffer is set True for
-- UI_Image, and false for UI_Write, and Format is copied from the Format
-- parameter to UI_Image or UI_Write.
procedure Init_Operand (UI : Uint; Vec : out UI_Vector);
pragma Inline (Init_Operand);
-- This procedure puts the value of UI into the vector in canonical
-- multiple precision format. The parameter should be of the correct size
-- as determined by a previous call to N_Digits (UI). The first digit of
-- Vec contains the sign, all other digits are always non-negative. Note
-- that the input may be directly represented, and in this case Vec will
-- contain the corresponding one or two digit value. The low bound of Vec
-- is always 1.
function Least_Sig_Digit (Arg : Uint) return Int;
pragma Inline (Least_Sig_Digit);
-- Returns the Least Significant Digit of Arg quickly. When the given Uint
-- is less than 2**15, the value returned is the input value, in this case
-- the result may be negative. It is expected that any use will mask off
-- unnecessary bits. This is used for finding Arg mod B where B is a power
-- of two. Hence the actual base is irrelevant as long as it is a power of
-- two.
procedure Most_Sig_2_Digits
(Left : Uint;
Right : Uint;
Left_Hat : out Int;
Right_Hat : out Int);
-- Returns leading two significant digits from the given pair of Uint's.
-- Mathematically: returns Left / (Base**K) and Right / (Base**K) where
-- K is as small as possible S.T. Right_Hat < Base * Base. It is required
-- that Left >= Right for the algorithm to work.
function N_Digits (Input : Uint) return Int;
pragma Inline (N_Digits);
-- Returns number of "digits" in a Uint
procedure UI_Div_Rem
(Left, Right : Uint;
Quotient : out Uint;
Remainder : out Uint;
Discard_Quotient : Boolean := False;
Discard_Remainder : Boolean := False);
-- Compute Euclidean division of Left by Right. If Discard_Quotient is
-- False then the quotient is returned in Quotient (otherwise Quotient is
-- set to No_Uint). If Discard_Remainder is False, then the remainder is
-- returned in Remainder (otherwise Remainder is set to No_Uint).
--
-- If Discard_Quotient is True, Quotient is set to No_Uint
-- If Discard_Remainder is True, Remainder is set to No_Uint
------------
-- Direct --
------------
function Direct (U : Uint) return Boolean is
begin
return Int (U) <= Int (Uint_Direct_Last);
end Direct;
----------------
-- Direct_Val --
----------------
function Direct_Val (U : Uint) return Int is
begin
pragma Assert (Direct (U));
return Int (U) - Int (Uint_Direct_Bias);
end Direct_Val;
---------
-- GCD --
---------
function GCD (Jin, Kin : Int) return Int is
J, K, Tmp : Int;
begin
pragma Assert (Jin >= Kin);
pragma Assert (Kin >= Int_0);
J := Jin;
K := Kin;
while K /= Uint_0 loop
Tmp := J mod K;
J := K;
K := Tmp;
end loop;
return J;
end GCD;
--------------
-- Hash_Num --
--------------
function Hash_Num (F : Int) return Hnum is
begin
return Types."mod" (F, Hnum'Range_Length);
end Hash_Num;
---------------
-- Image_Out --
---------------
procedure Image_Out
(Input : Uint;
To_Buffer : Boolean;
Format : UI_Format)
is
Marks : constant Uintp.Save_Mark := Uintp.Mark;
Base : Uint;
Ainput : Uint;
Digs_Output : Natural := 0;
-- Counts digits output. In hex mode, but not in decimal mode, we
-- put an underline after every four hex digits that are output.
Exponent : Natural := 0;
-- If the number is too long to fit in the buffer, we switch to an
-- approximate output format with an exponent. This variable records
-- the exponent value.
function Better_In_Hex return Boolean;
-- Determines if it is better to generate digits in base 16 (result
-- is true) or base 10 (result is false). The choice is purely a
-- matter of convenience and aesthetics, so it does not matter which
-- value is returned from a correctness point of view.
procedure Image_Char (C : Character);
-- Internal procedure to output one character
procedure Image_Exponent (N : Natural);
-- Output non-zero exponent. Note that we only use the exponent form in
-- the buffer case, so we know that To_Buffer is true.
procedure Image_Uint (U : Uint);
-- Internal procedure to output characters of non-negative Uint
-------------------
-- Better_In_Hex --
-------------------
function Better_In_Hex return Boolean is
T16 : constant Uint := Uint_2**Int'(16);
A : Uint;
begin
A := UI_Abs (Input);
-- Small values up to 2**16 can always be in decimal
if A < T16 then
return False;
end if;
-- Otherwise, see if we are a power of 2 or one less than a power
-- of 2. For the moment these are the only cases printed in hex.
if A mod Uint_2 = Uint_1 then
A := A + Uint_1;
end if;
loop
if A mod T16 /= Uint_0 then
return False;
else
A := A / T16;
end if;
exit when A < T16;
end loop;
while A > Uint_2 loop
if A mod Uint_2 /= Uint_0 then
return False;
else
A := A / Uint_2;
end if;
end loop;
return True;
end Better_In_Hex;
----------------
-- Image_Char --
----------------
procedure Image_Char (C : Character) is
begin
if To_Buffer then
if UI_Image_Length + 6 > UI_Image_Max then
Exponent := Exponent + 1;
else
UI_Image_Length := UI_Image_Length + 1;
UI_Image_Buffer (UI_Image_Length) := C;
end if;
else
Write_Char (C);
end if;
end Image_Char;
--------------------
-- Image_Exponent --
--------------------
procedure Image_Exponent (N : Natural) is
begin
if N >= 10 then
Image_Exponent (N / 10);
end if;
UI_Image_Length := UI_Image_Length + 1;
UI_Image_Buffer (UI_Image_Length) :=
Character'Val (Character'Pos ('0') + N mod 10);
end Image_Exponent;
----------------
-- Image_Uint --
----------------
procedure Image_Uint (U : Uint) is
H : constant array (Int range 0 .. 15) of Character :=
"0123456789ABCDEF";
Q, R : Uint;
begin
UI_Div_Rem (U, Base, Q, R);
if Q > Uint_0 then
Image_Uint (Q);
end if;
if Digs_Output = 4 and then Base = Uint_16 then
Image_Char ('_');
Digs_Output := 0;
end if;
Image_Char (H (UI_To_Int (R)));
Digs_Output := Digs_Output + 1;
end Image_Uint;
-- Start of processing for Image_Out
begin
if Input = No_Uint then
Image_Char ('?');
return;
end if;
UI_Image_Length := 0;
if Input < Uint_0 then
Image_Char ('-');
Ainput := -Input;
else
Ainput := Input;
end if;
if Format = Hex
or else (Format = Auto and then Better_In_Hex)
then
Base := Uint_16;
Image_Char ('1');
Image_Char ('6');
Image_Char ('#');
Image_Uint (Ainput);
Image_Char ('#');
else
Base := Uint_10;
Image_Uint (Ainput);
end if;
if Exponent /= 0 then
UI_Image_Length := UI_Image_Length + 1;
UI_Image_Buffer (UI_Image_Length) := 'E';
Image_Exponent (Exponent);
end if;
Uintp.Release (Marks);
end Image_Out;
-------------------
-- Init_Operand --
-------------------
procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
Loc : Int;
pragma Assert (Vec'First = Int'(1));
begin
if Direct (UI) then
Vec (1) := Direct_Val (UI);
if Vec (1) >= Base then
Vec (2) := Vec (1) rem Base;
Vec (1) := Vec (1) / Base;
end if;
else
Loc := Uints.Table (UI).Loc;
for J in 1 .. Uints.Table (UI).Length loop
Vec (J) := Udigits.Table (Loc + J - 1);
end loop;
end if;
end Init_Operand;
----------------
-- Initialize --
----------------
procedure Initialize is
begin
Uints.Init;
Udigits.Init;
Uint_Int_First := UI_From_Int (Int'First);
Uint_Int_Last := UI_From_Int (Int'Last);
UI_Power_2 (0) := Uint_1;
UI_Power_2_Set := 0;
UI_Power_10 (0) := Uint_1;
UI_Power_10_Set := 0;
Uints_Min := Uints.Last;
Udigits_Min := Udigits.Last;
UI_Ints.Reset;
end Initialize;
---------------------
-- Least_Sig_Digit --
---------------------
function Least_Sig_Digit (Arg : Uint) return Int is
V : Int;
begin
if Direct (Arg) then
V := Direct_Val (Arg);
if V >= Base then
V := V mod Base;
end if;
-- Note that this result may be negative
return V;
else
return
Udigits.Table
(Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
end if;
end Least_Sig_Digit;
----------
-- Mark --
----------
function Mark return Save_Mark is
begin
return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
end Mark;
-----------------------
-- Most_Sig_2_Digits --
-----------------------
procedure Most_Sig_2_Digits
(Left : Uint;
Right : Uint;
Left_Hat : out Int;
Right_Hat : out Int)
is
begin
pragma Assert (Left >= Right);
if Direct (Left) then
pragma Assert (Direct (Right));
Left_Hat := Direct_Val (Left);
Right_Hat := Direct_Val (Right);
return;
else
declare
L1 : constant Int :=
Udigits.Table (Uints.Table (Left).Loc);
L2 : constant Int :=
Udigits.Table (Uints.Table (Left).Loc + 1);
begin
-- It is not so clear what to return when Arg is negative???
Left_Hat := abs (L1) * Base + L2;
end;
end if;
declare
Length_L : constant Int := Uints.Table (Left).Length;
Length_R : Int;
R1 : Int;
R2 : Int;
T : Int;
begin
if Direct (Right) then
T := Direct_Val (Right);
R1 := abs (T / Base);
R2 := T rem Base;
Length_R := 2;
else
R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
Length_R := Uints.Table (Right).Length;
end if;
if Length_L = Length_R then
Right_Hat := R1 * Base + R2;
elsif Length_L = Length_R + Int_1 then
Right_Hat := R1;
else
Right_Hat := 0;
end if;
end;
end Most_Sig_2_Digits;
---------------
-- N_Digits --
---------------
-- Note: N_Digits returns 1 for No_Uint
function N_Digits (Input : Uint) return Int is
begin
if Direct (Input) then
if Direct_Val (Input) >= Base then
return 2;
else
return 1;
end if;
else
return Uints.Table (Input).Length;
end if;
end N_Digits;
--------------
-- Num_Bits --
--------------
function Num_Bits (Input : Uint) return Nat is
Bits : Nat;
Num : Nat;
begin
-- Largest negative number has to be handled specially, since it is in
-- Int_Range, but we cannot take the absolute value.
if Input = Uint_Int_First then
return Int'Size;
-- For any other number in Int_Range, get absolute value of number
elsif UI_Is_In_Int_Range (Input) then
Num := abs (UI_To_Int (Input));
Bits := 0;
-- If not in Int_Range then initialize bit count for all low order
-- words, and set number to high order digit.
else
Bits := Base_Bits * (Uints.Table (Input).Length - 1);
Num := abs (Udigits.Table (Uints.Table (Input).Loc));
end if;
-- Increase bit count for remaining value in Num
while Types.">" (Num, 0) loop
Num := Num / 2;
Bits := Bits + 1;
end loop;
return Bits;
end Num_Bits;
---------
-- pid --
---------
procedure pid (Input : Uint) is
begin
UI_Write (Input, Decimal);
Write_Eol;
end pid;
---------
-- pih --
---------
procedure pih (Input : Uint) is
begin
UI_Write (Input, Hex);
Write_Eol;
end pih;
-------------
-- Release --
-------------
procedure Release (M : Save_Mark) is
begin
Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
end Release;
----------------------
-- Release_And_Save --
----------------------
procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
begin
if Direct (UI) then
Release (M);
else
declare
UE_Len : constant Pos := Uints.Table (UI).Length;
UE_Loc : constant Int := Uints.Table (UI).Loc;
UD : constant Udigits.Table_Type (1 .. UE_Len) :=
Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
begin
Release (M);
Uints.Append ((Length => UE_Len, Loc => Udigits.Last + 1));
UI := Uints.Last;
for J in 1 .. UE_Len loop
Udigits.Append (UD (J));
end loop;
end;
end if;
end Release_And_Save;
procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
begin
if Direct (UI1) then
Release_And_Save (M, UI2);
elsif Direct (UI2) then
Release_And_Save (M, UI1);
else
declare
UE1_Len : constant Pos := Uints.Table (UI1).Length;
UE1_Loc : constant Int := Uints.Table (UI1).Loc;
UD1 : constant Udigits.Table_Type (1 .. UE1_Len) :=
Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
UE2_Len : constant Pos := Uints.Table (UI2).Length;
UE2_Loc : constant Int := Uints.Table (UI2).Loc;
UD2 : constant Udigits.Table_Type (1 .. UE2_Len) :=
Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
begin
Release (M);
Uints.Append ((Length => UE1_Len, Loc => Udigits.Last + 1));
UI1 := Uints.Last;
for J in 1 .. UE1_Len loop
Udigits.Append (UD1 (J));
end loop;
Uints.Append ((Length => UE2_Len, Loc => Udigits.Last + 1));
UI2 := Uints.Last;
for J in 1 .. UE2_Len loop
Udigits.Append (UD2 (J));
end loop;
end;
end if;
end Release_And_Save;
---------------
-- Tree_Read --
---------------
procedure Tree_Read is
begin
Uints.Tree_Read;
Udigits.Tree_Read;
Tree_Read_Int (Int (Uint_Int_First));
Tree_Read_Int (Int (Uint_Int_Last));
Tree_Read_Int (UI_Power_2_Set);
Tree_Read_Int (UI_Power_10_Set);
Tree_Read_Int (Int (Uints_Min));
Tree_Read_Int (Udigits_Min);
for J in 0 .. UI_Power_2_Set loop
Tree_Read_Int (Int (UI_Power_2 (J)));
end loop;
for J in 0 .. UI_Power_10_Set loop
Tree_Read_Int (Int (UI_Power_10 (J)));
end loop;
end Tree_Read;
----------------
-- Tree_Write --
----------------
procedure Tree_Write is
begin
Uints.Tree_Write;
Udigits.Tree_Write;
Tree_Write_Int (Int (Uint_Int_First));
Tree_Write_Int (Int (Uint_Int_Last));
Tree_Write_Int (UI_Power_2_Set);
Tree_Write_Int (UI_Power_10_Set);
Tree_Write_Int (Int (Uints_Min));
Tree_Write_Int (Udigits_Min);
for J in 0 .. UI_Power_2_Set loop
Tree_Write_Int (Int (UI_Power_2 (J)));
end loop;
for J in 0 .. UI_Power_10_Set loop
Tree_Write_Int (Int (UI_Power_10 (J)));
end loop;
end Tree_Write;
-------------
-- UI_Abs --
-------------
function UI_Abs (Right : Uint) return Uint is
begin
if Right < Uint_0 then
return -Right;
else
return Right;
end if;
end UI_Abs;
-------------
-- UI_Add --
-------------
function UI_Add (Left : Int; Right : Uint) return Uint is
begin
return UI_Add (UI_From_Int (Left), Right);
end UI_Add;
function UI_Add (Left : Uint; Right : Int) return Uint is
begin
return UI_Add (Left, UI_From_Int (Right));
end UI_Add;
function UI_Add (Left : Uint; Right : Uint) return Uint is
begin
-- Simple cases of direct operands and addition of zero
if Direct (Left) then
if Direct (Right) then
return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
elsif Int (Left) = Int (Uint_0) then
return Right;
end if;
elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
return Left;
end if;
-- Otherwise full circuit is needed
declare
L_Length : constant Int := N_Digits (Left);
R_Length : constant Int := N_Digits (Right);
L_Vec : UI_Vector (1 .. L_Length);
R_Vec : UI_Vector (1 .. R_Length);
Sum_Length : Int;
Tmp_Int : Int;
Carry : Int;
Borrow : Int;
X_Bigger : Boolean := False;
Y_Bigger : Boolean := False;
Result_Neg : Boolean := False;
begin
Init_Operand (Left, L_Vec);
Init_Operand (Right, R_Vec);
-- At least one of the two operands is in multi-digit form.
-- Calculate the number of digits sufficient to hold result.
if L_Length > R_Length then
Sum_Length := L_Length + 1;
X_Bigger := True;
else
Sum_Length := R_Length + 1;
if R_Length > L_Length then
Y_Bigger := True;
end if;
end if;
-- Make copies of the absolute values of L_Vec and R_Vec into X and Y
-- both with lengths equal to the maximum possibly needed. This makes
-- looping over the digits much simpler.
declare
X : UI_Vector (1 .. Sum_Length);
Y : UI_Vector (1 .. Sum_Length);
Tmp_UI : UI_Vector (1 .. Sum_Length);
begin
for J in 1 .. Sum_Length - L_Length loop
X (J) := 0;
end loop;
X (Sum_Length - L_Length + 1) := abs L_Vec (1);
for J in 2 .. L_Length loop
X (J + (Sum_Length - L_Length)) := L_Vec (J);
end loop;
for J in 1 .. Sum_Length - R_Length loop
Y (J) := 0;
end loop;
Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
for J in 2 .. R_Length loop
Y (J + (Sum_Length - R_Length)) := R_Vec (J);
end loop;
if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
-- Same sign so just add
Carry := 0;
for J in reverse 1 .. Sum_Length loop
Tmp_Int := X (J) + Y (J) + Carry;
if Tmp_Int >= Base then
Tmp_Int := Tmp_Int - Base;
Carry := 1;
else
Carry := 0;
end if;
X (J) := Tmp_Int;
end loop;
return Vector_To_Uint (X, L_Vec (1) < Int_0);
else
-- Find which one has bigger magnitude
if not (X_Bigger or Y_Bigger) then
for J in L_Vec'Range loop
if abs L_Vec (J) > abs R_Vec (J) then
X_Bigger := True;
exit;
elsif abs R_Vec (J) > abs L_Vec (J) then
Y_Bigger := True;
exit;
end if;
end loop;
end if;
-- If they have identical magnitude, just return 0, else swap
-- if necessary so that X had the bigger magnitude. Determine
-- if result is negative at this time.
Result_Neg := False;
if not (X_Bigger or Y_Bigger) then
return Uint_0;
elsif Y_Bigger then
if R_Vec (1) < Int_0 then
Result_Neg := True;
end if;
Tmp_UI := X;
X := Y;
Y := Tmp_UI;
else
if L_Vec (1) < Int_0 then
Result_Neg := True;
end if;
end if;
-- Subtract Y from the bigger X
Borrow := 0;
for J in reverse 1 .. Sum_Length loop
Tmp_Int := X (J) - Y (J) + Borrow;
if Tmp_Int < Int_0 then
Tmp_Int := Tmp_Int + Base;
Borrow := -1;
else
Borrow := 0;
end if;
X (J) := Tmp_Int;
end loop;
return Vector_To_Uint (X, Result_Neg);
end if;
end;
end;
end UI_Add;
--------------------------
-- UI_Decimal_Digits_Hi --
--------------------------
function UI_Decimal_Digits_Hi (U : Uint) return Nat is
begin
-- The maximum value of a "digit" is 32767, which is 5 decimal digits,
-- so an N_Digit number could take up to 5 times this number of digits.
-- This is certainly too high for large numbers but it is not worth
-- worrying about.
return 5 * N_Digits (U);
end UI_Decimal_Digits_Hi;
--------------------------
-- UI_Decimal_Digits_Lo --
--------------------------
function UI_Decimal_Digits_Lo (U : Uint) return Nat is
begin
-- The maximum value of a "digit" is 32767, which is more than four
-- decimal digits, but not a full five digits. The easily computed
-- minimum number of decimal digits is thus 1 + 4 * the number of
-- digits. This is certainly too low for large numbers but it is not
-- worth worrying about.
return 1 + 4 * (N_Digits (U) - 1);
end UI_Decimal_Digits_Lo;
------------
-- UI_Div --
------------
function UI_Div (Left : Int; Right : Uint) return Uint is
begin
return UI_Div (UI_From_Int (Left), Right);
end UI_Div;
function UI_Div (Left : Uint; Right : Int) return Uint is
begin
return UI_Div (Left, UI_From_Int (Right));
end UI_Div;
function UI_Div (Left, Right : Uint) return Uint is
Quotient : Uint;
Remainder : Uint;
pragma Warnings (Off, Remainder);
begin
UI_Div_Rem
(Left, Right,
Quotient, Remainder,
Discard_Remainder => True);
return Quotient;
end UI_Div;
----------------
-- UI_Div_Rem --
----------------
procedure UI_Div_Rem
(Left, Right : Uint;
Quotient : out Uint;
Remainder : out Uint;
Discard_Quotient : Boolean := False;
Discard_Remainder : Boolean := False)
is
begin
pragma Assert (Right /= Uint_0);
Quotient := No_Uint;
Remainder := No_Uint;
-- Cases where both operands are represented directly
if Direct (Left) and then Direct (Right) then
declare
DV_Left : constant Int := Direct_Val (Left);
DV_Right : constant Int := Direct_Val (Right);
begin
if not Discard_Quotient then
Quotient := UI_From_Int (DV_Left / DV_Right);
end if;
if not Discard_Remainder then
Remainder := UI_From_Int (DV_Left rem DV_Right);
end if;
return;
end;
end if;
declare
L_Length : constant Int := N_Digits (Left);
R_Length : constant Int := N_Digits (Right);
Q_Length : constant Int := L_Length - R_Length + 1;
L_Vec : UI_Vector (1 .. L_Length);
R_Vec : UI_Vector (1 .. R_Length);
D : Int;
Remainder_I : Int;
Tmp_Divisor : Int;
Carry : Int;
Tmp_Int : Int;
Tmp_Dig : Int;
procedure UI_Div_Vector
(L_Vec : UI_Vector;
R_Int : Int;
Quotient : out UI_Vector;
Remainder : out Int);
pragma Inline (UI_Div_Vector);
-- Specialised variant for case where the divisor is a single digit
procedure UI_Div_Vector
(L_Vec : UI_Vector;
R_Int : Int;
Quotient : out UI_Vector;
Remainder : out Int)
is
Tmp_Int : Int;
begin
Remainder := 0;
for J in L_Vec'Range loop
Tmp_Int := Remainder * Base + abs L_Vec (J);
Quotient (Quotient'First + J - L_Vec'First) := Tmp_Int / R_Int;
Remainder := Tmp_Int rem R_Int;
end loop;
if L_Vec (L_Vec'First) < Int_0 then
Remainder := -Remainder;
end if;
end UI_Div_Vector;
-- Start of processing for UI_Div_Rem
begin
-- Result is zero if left operand is shorter than right
if L_Length < R_Length then
if not Discard_Quotient then
Quotient := Uint_0;
end if;
if not Discard_Remainder then
Remainder := Left;
end if;
return;
end if;
Init_Operand (Left, L_Vec);
Init_Operand (Right, R_Vec);
-- Case of right operand is single digit. Here we can simply divide
-- each digit of the left operand by the divisor, from most to least
-- significant, carrying the remainder to the next digit (just like
-- ordinary long division by hand).
if R_Length = Int_1 then
Tmp_Divisor := abs R_Vec (1);
declare
Quotient_V : UI_Vector (1 .. L_Length);
begin
UI_Div_Vector (L_Vec, Tmp_Divisor, Quotient_V, Remainder_I);
if not Discard_Quotient then
Quotient :=
Vector_To_Uint
(Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
end if;
if not Discard_Remainder then
Remainder := UI_From_Int (Remainder_I);
end if;
return;
end;
end if;
-- The possible simple cases have been exhausted. Now turn to the
-- algorithm D from the section of Knuth mentioned at the top of
-- this package.
Algorithm_D : declare
Dividend : UI_Vector (1 .. L_Length + 1);
Divisor : UI_Vector (1 .. R_Length);
Quotient_V : UI_Vector (1 .. Q_Length);
Divisor_Dig1 : Int;
Divisor_Dig2 : Int;
Q_Guess : Int;
R_Guess : Int;
begin
-- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
-- scale d, and then multiply Left and Right (u and v in the book)
-- by d to get the dividend and divisor to work with.
D := Base / (abs R_Vec (1) + 1);
Dividend (1) := 0;
Dividend (2) := abs L_Vec (1);
for J in 3 .. L_Length + Int_1 loop
Dividend (J) := L_Vec (J - 1);
end loop;
Divisor (1) := abs R_Vec (1);
for J in Int_2 .. R_Length loop
Divisor (J) := R_Vec (J);
end loop;
if D > Int_1 then
-- Multiply Dividend by d
Carry := 0;
for J in reverse Dividend'Range loop
Tmp_Int := Dividend (J) * D + Carry;
Dividend (J) := Tmp_Int rem Base;
Carry := Tmp_Int / Base;
end loop;
-- Multiply Divisor by d
Carry := 0;
for J in reverse Divisor'Range loop
Tmp_Int := Divisor (J) * D + Carry;
Divisor (J) := Tmp_Int rem Base;
Carry := Tmp_Int / Base;
end loop;
end if;
-- Main loop of long division algorithm
Divisor_Dig1 := Divisor (1);
Divisor_Dig2 := Divisor (2);
for J in Quotient_V'Range loop
-- [ CALCULATE Q (hat) ] (step D3 in the algorithm)
-- Note: this version of step D3 is from the original published
-- algorithm, which is known to have a bug causing overflows.
-- See: http://www-cs-faculty.stanford.edu/~uno/err2-2e.ps.gz
-- and http://www-cs-faculty.stanford.edu/~uno/all2-pre.ps.gz.
-- The code below is the fixed version of this step.
Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
-- Initial guess
Q_Guess := Tmp_Int / Divisor_Dig1;
R_Guess := Tmp_Int rem Divisor_Dig1;
-- Refine the guess
while Q_Guess >= Base
or else Divisor_Dig2 * Q_Guess >
R_Guess * Base + Dividend (J + 2)
loop
Q_Guess := Q_Guess - 1;
R_Guess := R_Guess + Divisor_Dig1;
exit when R_Guess >= Base;
end loop;
-- [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
-- subtracted from the remaining dividend.
Carry := 0;
for K in reverse Divisor'Range loop
Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
Tmp_Dig := Tmp_Int rem Base;
Carry := Tmp_Int / Base;
if Tmp_Dig < Int_0 then
Tmp_Dig := Tmp_Dig + Base;
Carry := Carry - 1;
end if;
Dividend (J + K) := Tmp_Dig;
end loop;
Dividend (J) := Dividend (J) + Carry;
-- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
-- Here there is a slight difference from the book: the last
-- carry is always added in above and below (cancelling each
-- other). In fact the dividend going negative is used as
-- the test.
-- If the Dividend went negative, then Q_Guess was off by
-- one, so it is decremented, and the divisor is added back
-- into the relevant portion of the dividend.
if Dividend (J) < Int_0 then
Q_Guess := Q_Guess - 1;
Carry := 0;
for K in reverse Divisor'Range loop
Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
if Tmp_Int >= Base then
Tmp_Int := Tmp_Int - Base;
Carry := 1;
else
Carry := 0;
end if;
Dividend (J + K) := Tmp_Int;
end loop;
Dividend (J) := Dividend (J) + Carry;
end if;
-- Finally we can get the next quotient digit
Quotient_V (J) := Q_Guess;
end loop;
-- [ UNNORMALIZE ] (step D8)
if not Discard_Quotient then
Quotient := Vector_To_Uint
(Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
end if;
if not Discard_Remainder then
declare
Remainder_V : UI_Vector (1 .. R_Length);
Discard_Int : Int;
pragma Warnings (Off, Discard_Int);
begin
UI_Div_Vector
(Dividend (Dividend'Last - R_Length + 1 .. Dividend'Last),
D,
Remainder_V, Discard_Int);
Remainder := Vector_To_Uint (Remainder_V, L_Vec (1) < Int_0);
end;
end if;
end Algorithm_D;
end;
end UI_Div_Rem;
------------
-- UI_Eq --
------------
function UI_Eq (Left : Int; Right : Uint) return Boolean is
begin
return not UI_Ne (UI_From_Int (Left), Right);
end UI_Eq;
function UI_Eq (Left : Uint; Right : Int) return Boolean is
begin
return not UI_Ne (Left, UI_From_Int (Right));
end UI_Eq;
function UI_Eq (Left : Uint; Right : Uint) return Boolean is
begin
return not UI_Ne (Left, Right);
end UI_Eq;
--------------
-- UI_Expon --
--------------
function UI_Expon (Left : Int; Right : Uint) return Uint is
begin
return UI_Expon (UI_From_Int (Left), Right);
end UI_Expon;
function UI_Expon (Left : Uint; Right : Int) return Uint is
begin
return UI_Expon (Left, UI_From_Int (Right));
end UI_Expon;
function UI_Expon (Left : Int; Right : Int) return Uint is
begin
return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
end UI_Expon;
function UI_Expon (Left : Uint; Right : Uint) return Uint is
begin
pragma Assert (Right >= Uint_0);
-- Any value raised to power of 0 is 1
if Right = Uint_0 then
return Uint_1;
-- 0 to any positive power is 0
elsif Left = Uint_0 then
return Uint_0;
-- 1 to any power is 1
elsif Left = Uint_1 then
return Uint_1;
-- Any value raised to power of 1 is that value
elsif Right = Uint_1 then
return Left;
-- Cases which can be done by table lookup
elsif Right <= Uint_64 then
-- 2**N for N in 2 .. 64
if Left = Uint_2 then
declare
Right_Int : constant Int := Direct_Val (Right);
begin
if Right_Int > UI_Power_2_Set then
for J in UI_Power_2_Set + Int_1 .. Right_Int loop
UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
Uints_Min := Uints.Last;
Udigits_Min := Udigits.Last;
end loop;
UI_Power_2_Set := Right_Int;
end if;
return UI_Power_2 (Right_Int);
end;
-- 10**N for N in 2 .. 64
elsif Left = Uint_10 then
declare
Right_Int : constant Int := Direct_Val (Right);
begin
if Right_Int > UI_Power_10_Set then
for J in UI_Power_10_Set + Int_1 .. Right_Int loop
UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
Uints_Min := Uints.Last;
Udigits_Min := Udigits.Last;
end loop;
UI_Power_10_Set := Right_Int;
end if;
return UI_Power_10 (Right_Int);
end;
end if;
end if;
-- If we fall through, then we have the general case (see Knuth 4.6.3)
declare
N : Uint := Right;
Squares : Uint := Left;
Result : Uint := Uint_1;
M : constant Uintp.Save_Mark := Uintp.Mark;
begin
loop
if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
Result := Result * Squares;
end if;
N := N / Uint_2;
exit when N = Uint_0;
Squares := Squares * Squares;
end loop;
Uintp.Release_And_Save (M, Result);
return Result;
end;
end UI_Expon;
----------------
-- UI_From_CC --
----------------
function UI_From_CC (Input : Char_Code) return Uint is
begin
return UI_From_Int (Int (Input));
end UI_From_CC;
-----------------
-- UI_From_Int --
-----------------
function UI_From_Int (Input : Int) return Uint is
U : Uint;
begin
if Min_Direct <= Input and then Input <= Max_Direct then
return Uint (Int (Uint_Direct_Bias) + Input);
end if;
-- If already in the hash table, return entry
U := UI_Ints.Get (Input);
if U /= No_Uint then
return U;
end if;
-- For values of larger magnitude, compute digits into a vector and call
-- Vector_To_Uint.
declare
Max_For_Int : constant := 3;
-- Base is defined so that 3 Uint digits is sufficient to hold the
-- largest possible Int value.
V : UI_Vector (1 .. Max_For_Int);
Temp_Integer : Int := Input;
begin
for J in reverse V'Range loop
V (J) := abs (Temp_Integer rem Base);
Temp_Integer := Temp_Integer / Base;
end loop;
U := Vector_To_Uint (V, Input < Int_0);
UI_Ints.Set (Input, U);
Uints_Min := Uints.Last;
Udigits_Min := Udigits.Last;
return U;
end;
end UI_From_Int;
------------
-- UI_GCD --
------------
-- Lehmer's algorithm for GCD
-- The idea is to avoid using multiple precision arithmetic wherever
-- possible, substituting Int arithmetic instead. See Knuth volume II,
-- Algorithm L (page 329).
-- We use the same notation as Knuth (U_Hat standing for the obvious)
function UI_GCD (Uin, Vin : Uint) return Uint is
U, V : Uint;
-- Copies of Uin and Vin
U_Hat, V_Hat : Int;
-- The most Significant digits of U,V
A, B, C, D, T, Q, Den1, Den2 : Int;
Tmp_UI : Uint;
Marks : constant Uintp.Save_Mark := Uintp.Mark;
Iterations : Integer := 0;
begin
pragma Assert (Uin >= Vin);
pragma Assert (Vin >= Uint_0);
U := Uin;
V := Vin;
loop
Iterations := Iterations + 1;
if Direct (V) then
if V = Uint_0 then
return U;
else
return
UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
end if;
end if;
Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
A := 1;
B := 0;
C := 0;
D := 1;
loop
-- We might overflow and get division by zero here. This just
-- means we cannot take the single precision step
Den1 := V_Hat + C;
Den2 := V_Hat + D;
exit when Den1 = Int_0 or else Den2 = Int_0;
-- Compute Q, the trial quotient
Q := (U_Hat + A) / Den1;
exit when Q /= ((U_Hat + B) / Den2);
-- A single precision step Euclid step will give same answer as a
-- multiprecision one.
T := A - (Q * C);
A := C;
C := T;
T := B - (Q * D);
B := D;
D := T;
T := U_Hat - (Q * V_Hat);
U_Hat := V_Hat;
V_Hat := T;
end loop;
-- Take a multiprecision Euclid step
if B = Int_0 then
-- No single precision steps take a regular Euclid step
Tmp_UI := U rem V;
U := V;
V := Tmp_UI;
else
-- Use prior single precision steps to compute this Euclid step
Tmp_UI := (UI_From_Int (A) * U) + (UI_From_Int (B) * V);
V := (UI_From_Int (C) * U) + (UI_From_Int (D) * V);
U := Tmp_UI;
end if;
-- If the operands are very different in magnitude, the loop will
-- generate large amounts of short-lived data, which it is worth
-- removing periodically.
if Iterations > 100 then
Release_And_Save (Marks, U, V);
Iterations := 0;
end if;
end loop;
end UI_GCD;
------------
-- UI_Ge --
------------
function UI_Ge (Left : Int; Right : Uint) return Boolean is
begin
return not UI_Lt (UI_From_Int (Left), Right);
end UI_Ge;
function UI_Ge (Left : Uint; Right : Int) return Boolean is
begin
return not UI_Lt (Left, UI_From_Int (Right));
end UI_Ge;
function UI_Ge (Left : Uint; Right : Uint) return Boolean is
begin
return not UI_Lt (Left, Right);
end UI_Ge;
------------
-- UI_Gt --
------------
function UI_Gt (Left : Int; Right : Uint) return Boolean is
begin
return UI_Lt (Right, UI_From_Int (Left));
end UI_Gt;
function UI_Gt (Left : Uint; Right : Int) return Boolean is
begin
return UI_Lt (UI_From_Int (Right), Left);
end UI_Gt;
function UI_Gt (Left : Uint; Right : Uint) return Boolean is
begin
return UI_Lt (Left => Right, Right => Left);
end UI_Gt;
---------------
-- UI_Image --
---------------
procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
begin
Image_Out (Input, True, Format);
end UI_Image;
function UI_Image
(Input : Uint;
Format : UI_Format := Auto) return String
is
begin
Image_Out (Input, True, Format);
return UI_Image_Buffer (1 .. UI_Image_Length);
end UI_Image;
-------------------------
-- UI_Is_In_Int_Range --
-------------------------
function UI_Is_In_Int_Range (Input : Uint) return Boolean is
begin
-- Make sure we don't get called before Initialize
pragma Assert (Uint_Int_First /= Uint_0);
if Direct (Input) then
return True;
else
return Input >= Uint_Int_First
and then Input <= Uint_Int_Last;
end if;
end UI_Is_In_Int_Range;
------------
-- UI_Le --
------------
function UI_Le (Left : Int; Right : Uint) return Boolean is
begin
return not UI_Lt (Right, UI_From_Int (Left));
end UI_Le;
function UI_Le (Left : Uint; Right : Int) return Boolean is
begin
return not UI_Lt (UI_From_Int (Right), Left);
end UI_Le;
function UI_Le (Left : Uint; Right : Uint) return Boolean is
begin
return not UI_Lt (Left => Right, Right => Left);
end UI_Le;
------------
-- UI_Lt --
------------
function UI_Lt (Left : Int; Right : Uint) return Boolean is
begin
return UI_Lt (UI_From_Int (Left), Right);
end UI_Lt;
function UI_Lt (Left : Uint; Right : Int) return Boolean is
begin
return UI_Lt (Left, UI_From_Int (Right));
end UI_Lt;
function UI_Lt (Left : Uint; Right : Uint) return Boolean is
begin
-- Quick processing for identical arguments
if Int (Left) = Int (Right) then
return False;
-- Quick processing for both arguments directly represented
elsif Direct (Left) and then Direct (Right) then
return Int (Left) < Int (Right);
-- At least one argument is more than one digit long
else
declare
L_Length : constant Int := N_Digits (Left);
R_Length : constant Int := N_Digits (Right);
L_Vec : UI_Vector (1 .. L_Length);
R_Vec : UI_Vector (1 .. R_Length);
begin
Init_Operand (Left, L_Vec);
Init_Operand (Right, R_Vec);
if L_Vec (1) < Int_0 then
-- First argument negative, second argument non-negative
if R_Vec (1) >= Int_0 then
return True;
-- Both arguments negative
else
if L_Length /= R_Length then
return L_Length > R_Length;
elsif L_Vec (1) /= R_Vec (1) then
return L_Vec (1) < R_Vec (1);
else
for J in 2 .. L_Vec'Last loop
if L_Vec (J) /= R_Vec (J) then
return L_Vec (J) > R_Vec (J);
end if;
end loop;
return False;
end if;
end if;
else
-- First argument non-negative, second argument negative
if R_Vec (1) < Int_0 then
return False;
-- Both arguments non-negative
else
if L_Length /= R_Length then
return L_Length < R_Length;
else
for J in L_Vec'Range loop
if L_Vec (J) /= R_Vec (J) then
return L_Vec (J) < R_Vec (J);
end if;
end loop;
return False;
end if;
end if;
end if;
end;
end if;
end UI_Lt;
------------
-- UI_Max --
------------
function UI_Max (Left : Int; Right : Uint) return Uint is
begin
return UI_Max (UI_From_Int (Left), Right);
end UI_Max;
function UI_Max (Left : Uint; Right : Int) return Uint is
begin
return UI_Max (Left, UI_From_Int (Right));
end UI_Max;
function UI_Max (Left : Uint; Right : Uint) return Uint is
begin
if Left >= Right then
return Left;
else
return Right;
end if;
end UI_Max;
------------
-- UI_Min --
------------
function UI_Min (Left : Int; Right : Uint) return Uint is
begin
return UI_Min (UI_From_Int (Left), Right);
end UI_Min;
function UI_Min (Left : Uint; Right : Int) return Uint is
begin
return UI_Min (Left, UI_From_Int (Right));
end UI_Min;
function UI_Min (Left : Uint; Right : Uint) return Uint is
begin
if Left <= Right then
return Left;
else
return Right;
end if;
end UI_Min;
-------------
-- UI_Mod --
-------------
function UI_Mod (Left : Int; Right : Uint) return Uint is
begin
return UI_Mod (UI_From_Int (Left), Right);
end UI_Mod;
function UI_Mod (Left : Uint; Right : Int) return Uint is
begin
return UI_Mod (Left, UI_From_Int (Right));
end UI_Mod;
function UI_Mod (Left : Uint; Right : Uint) return Uint is
Urem : constant Uint := Left rem Right;
begin
if (Left < Uint_0) = (Right < Uint_0)
or else Urem = Uint_0
then
return Urem;
else
return Right + Urem;
end if;
end UI_Mod;
-------------------------------
-- UI_Modular_Exponentiation --
-------------------------------
function UI_Modular_Exponentiation
(B : Uint;
E : Uint;
Modulo : Uint) return Uint
is
M : constant Save_Mark := Mark;
Result : Uint := Uint_1;
Base : Uint := B;
Exponent : Uint := E;
begin
while Exponent /= Uint_0 loop
if Least_Sig_Digit (Exponent) rem Int'(2) = Int'(1) then
Result := (Result * Base) rem Modulo;
end if;
Exponent := Exponent / Uint_2;
Base := (Base * Base) rem Modulo;
end loop;
Release_And_Save (M, Result);
return Result;
end UI_Modular_Exponentiation;
------------------------
-- UI_Modular_Inverse --
------------------------
function UI_Modular_Inverse (N : Uint; Modulo : Uint) return Uint is
M : constant Save_Mark := Mark;
U : Uint;
V : Uint;
Q : Uint;
R : Uint;
X : Uint;
Y : Uint;
T : Uint;
S : Int := 1;
begin
U := Modulo;
V := N;
X := Uint_1;
Y := Uint_0;
loop
UI_Div_Rem (U, V, Quotient => Q, Remainder => R);
U := V;
V := R;
T := X;
X := Y + Q * X;
Y := T;
S := -S;
exit when R = Uint_1;
end loop;
if S = Int'(-1) then
X := Modulo - X;
end if;
Release_And_Save (M, X);
return X;
end UI_Modular_Inverse;
------------
-- UI_Mul --
------------
function UI_Mul (Left : Int; Right : Uint) return Uint is
begin
return UI_Mul (UI_From_Int (Left), Right);
end UI_Mul;
function UI_Mul (Left : Uint; Right : Int) return Uint is
begin
return UI_Mul (Left, UI_From_Int (Right));
end UI_Mul;
function UI_Mul (Left : Uint; Right : Uint) return Uint is
begin
-- Case where product fits in the range of a 32-bit integer
if Int (Left) <= Int (Uint_Max_Simple_Mul)
and then
Int (Right) <= Int (Uint_Max_Simple_Mul)
then
return UI_From_Int (Direct_Val (Left) * Direct_Val (Right));
end if;
-- Otherwise we have the general case (Algorithm M in Knuth)
declare
L_Length : constant Int := N_Digits (Left);
R_Length : constant Int := N_Digits (Right);
L_Vec : UI_Vector (1 .. L_Length);
R_Vec : UI_Vector (1 .. R_Length);
Neg : Boolean;
begin
Init_Operand (Left, L_Vec);
Init_Operand (Right, R_Vec);
Neg := (L_Vec (1) < Int_0) xor (R_Vec (1) < Int_0);
L_Vec (1) := abs (L_Vec (1));
R_Vec (1) := abs (R_Vec (1));
Algorithm_M : declare
Product : UI_Vector (1 .. L_Length + R_Length);
Tmp_Sum : Int;
Carry : Int;
begin
for J in Product'Range loop
Product (J) := 0;
end loop;
for J in reverse R_Vec'Range loop
Carry := 0;
for K in reverse L_Vec'Range loop
Tmp_Sum :=
L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
Product (J + K) := Tmp_Sum rem Base;
Carry := Tmp_Sum / Base;
end loop;
Product (J) := Carry;
end loop;
return Vector_To_Uint (Product, Neg);
end Algorithm_M;
end;
end UI_Mul;
------------
-- UI_Ne --
------------
function UI_Ne (Left : Int; Right : Uint) return Boolean is
begin
return UI_Ne (UI_From_Int (Left), Right);
end UI_Ne;
function UI_Ne (Left : Uint; Right : Int) return Boolean is
begin
return UI_Ne (Left, UI_From_Int (Right));
end UI_Ne;
function UI_Ne (Left : Uint; Right : Uint) return Boolean is
begin
-- Quick processing for identical arguments. Note that this takes
-- care of the case of two No_Uint arguments.
if Int (Left) = Int (Right) then
return False;
end if;
-- See if left operand directly represented
if Direct (Left) then
-- If right operand directly represented then compare
if Direct (Right) then
return Int (Left) /= Int (Right);
-- Left operand directly represented, right not, must be unequal
else
return True;
end if;
-- Right operand directly represented, left not, must be unequal
elsif Direct (Right) then
return True;
end if;
-- Otherwise both multi-word, do comparison
declare
Size : constant Int := N_Digits (Left);
Left_Loc : Int;
Right_Loc : Int;
begin
if Size /= N_Digits (Right) then
return True;
end if;
Left_Loc := Uints.Table (Left).Loc;
Right_Loc := Uints.Table (Right).Loc;
for J in Int_0 .. Size - Int_1 loop
if Udigits.Table (Left_Loc + J) /=
Udigits.Table (Right_Loc + J)
then
return True;
end if;
end loop;
return False;
end;
end UI_Ne;
----------------
-- UI_Negate --
----------------
function UI_Negate (Right : Uint) return Uint is
begin
-- Case where input is directly represented. Note that since the range
-- of Direct values is non-symmetrical, the result may not be directly
-- represented, this is taken care of in UI_From_Int.
if Direct (Right) then
return UI_From_Int (-Direct_Val (Right));
-- Full processing for multi-digit case. Note that we cannot just copy
-- the value to the end of the table negating the first digit, since the
-- range of Direct values is non-symmetrical, so we can have a negative
-- value that is not Direct whose negation can be represented directly.
else
declare
R_Length : constant Int := N_Digits (Right);
R_Vec : UI_Vector (1 .. R_Length);
Neg : Boolean;
begin
Init_Operand (Right, R_Vec);
Neg := R_Vec (1) > Int_0;
R_Vec (1) := abs R_Vec (1);
return Vector_To_Uint (R_Vec, Neg);
end;
end if;
end UI_Negate;
-------------
-- UI_Rem --
-------------
function UI_Rem (Left : Int; Right : Uint) return Uint is
begin
return UI_Rem (UI_From_Int (Left), Right);
end UI_Rem;
function UI_Rem (Left : Uint; Right : Int) return Uint is
begin
return UI_Rem (Left, UI_From_Int (Right));
end UI_Rem;
function UI_Rem (Left, Right : Uint) return Uint is
Remainder : Uint;
Quotient : Uint;
pragma Warnings (Off, Quotient);
begin
pragma Assert (Right /= Uint_0);
if Direct (Right) and then Direct (Left) then
return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
else
UI_Div_Rem
(Left, Right, Quotient, Remainder, Discard_Quotient => True);
return Remainder;
end if;
end UI_Rem;
------------
-- UI_Sub --
------------
function UI_Sub (Left : Int; Right : Uint) return Uint is
begin
return UI_Add (Left, -Right);
end UI_Sub;
function UI_Sub (Left : Uint; Right : Int) return Uint is
begin
return UI_Add (Left, -Right);
end UI_Sub;
function UI_Sub (Left : Uint; Right : Uint) return Uint is
begin
if Direct (Left) and then Direct (Right) then
return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
else
return UI_Add (Left, -Right);
end if;
end UI_Sub;
--------------
-- UI_To_CC --
--------------
function UI_To_CC (Input : Uint) return Char_Code is
begin
if Direct (Input) then
return Char_Code (Direct_Val (Input));
-- Case of input is more than one digit
else
declare
In_Length : constant Int := N_Digits (Input);
In_Vec : UI_Vector (1 .. In_Length);
Ret_CC : Char_Code;
begin
Init_Operand (Input, In_Vec);
-- We assume value is positive
Ret_CC := 0;
for Idx in In_Vec'Range loop
Ret_CC := Ret_CC * Char_Code (Base) +
Char_Code (abs In_Vec (Idx));
end loop;
return Ret_CC;
end;
end if;
end UI_To_CC;
----------------
-- UI_To_Int --
----------------
function UI_To_Int (Input : Uint) return Int is
pragma Assert (Input /= No_Uint);
begin
if Direct (Input) then
return Direct_Val (Input);
-- Case of input is more than one digit
else
declare
In_Length : constant Int := N_Digits (Input);
In_Vec : UI_Vector (1 .. In_Length);
Ret_Int : Int;
begin
-- Uints of more than one digit could be outside the range for
-- Ints. Caller should have checked for this if not certain.
-- Constraint_Error to attempt to convert from value outside
-- Int'Range.
if not UI_Is_In_Int_Range (Input) then
raise Constraint_Error;
end if;
-- Otherwise, proceed ahead, we are OK
Init_Operand (Input, In_Vec);
Ret_Int := 0;
-- Calculate -|Input| and then negates if value is positive. This
-- handles our current definition of Int (based on 2s complement).
-- Is it secure enough???
for Idx in In_Vec'Range loop
Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
end loop;
if In_Vec (1) < Int_0 then
return Ret_Int;
else
return -Ret_Int;
end if;
end;
end if;
end UI_To_Int;
--------------
-- UI_Write --
--------------
procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
begin
Image_Out (Input, False, Format);
end UI_Write;
---------------------
-- Vector_To_Uint --
---------------------
function Vector_To_Uint
(In_Vec : UI_Vector;
Negative : Boolean)
return Uint
is
Size : Int;
Val : Int;
begin
-- The vector can contain leading zeros. These are not stored in the
-- table, so loop through the vector looking for first non-zero digit
for J in In_Vec'Range loop
if In_Vec (J) /= Int_0 then
-- The length of the value is the length of the rest of the vector
Size := In_Vec'Last - J + 1;
-- One digit value can always be represented directly
if Size = Int_1 then
if Negative then
return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
else
return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
end if;
-- Positive two digit values may be in direct representation range
elsif Size = Int_2 and then not Negative then
Val := In_Vec (J) * Base + In_Vec (J + 1);
if Val <= Max_Direct then
return Uint (Int (Uint_Direct_Bias) + Val);
end if;
end if;
-- The value is outside the direct representation range and must
-- therefore be stored in the table. Expand the table to contain
-- the count and digits. The index of the new table entry will be
-- returned as the result.
Uints.Append ((Length => Size, Loc => Udigits.Last + 1));
if Negative then
Val := -In_Vec (J);
else
Val := +In_Vec (J);
end if;
Udigits.Append (Val);
for K in 2 .. Size loop
Udigits.Append (In_Vec (J + K - 1));
end loop;
return Uints.Last;
end if;
end loop;
-- Dropped through loop only if vector contained all zeros
return Uint_0;
end Vector_To_Uint;
----------------------
-- UI_From_Integral --
----------------------
function UI_From_Integral (Input : In_T) return Uint is
U : Uint;
begin
-- If in range of our normal conversion function, use it so we can
-- use direct access and our cache.
if In_T'Size <= Int'Size
or else Input in In_T (Int'First) .. In_T (Int'Last)
then
return UI_From_Int (Int (Input));
else
-- pragma Warnings (Off);
-- For values of larger magnitude, compute digits into a vector
-- and call Vector_To_Uint.
declare
Max_For_In_T : constant Int := 3 * In_T'Size / Int'Size;
Our_Base : constant In_T := In_T (Base);
Temp_Integer : In_T := Input;
-- Base is defined so that 3 Uint digits is sufficient to hold the
-- largest possible Int value.
V : UI_Vector (1 .. Max_For_In_T);
begin
for J in reverse V'Range loop
V (J) := Int (abs (Temp_Integer rem Our_Base));
Temp_Integer := Temp_Integer / Our_Base;
end loop;
U := Vector_To_Uint (V, Input < 0);
Uints_Min := Uints.Last;
Udigits_Min := Udigits.Last;
return U;
end;
-- pragma Warnings (On);
end if;
end UI_From_Integral;
end Uintp;
|