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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "big.h"
#include "error.h"
#include "bif.h"
#define ZERO_DIGITS(v, sz) do { \
dsize_t _t_sz = sz; \
ErtsDigit* _t_v = v; \
while(_t_sz--) *_t_v++ = 0; \
} while(0)
#define MOVE_DIGITS(dst, src, sz) do { \
dsize_t _t_sz = sz; \
ErtsDigit* _t_dst; \
ErtsDigit* _t_src; \
if (dst < src) { \
_t_dst = dst; \
_t_src = src; \
while(_t_sz--) *_t_dst++ = *_t_src++; \
} \
else if (dst > src) { \
_t_dst = (dst)+((sz)-1); \
_t_src = (src)+((sz)-1); \
while(_t_sz--) *_t_dst-- = *_t_src--; \
} \
} while(0)
/* add a and b with carry in + out */
#define DSUMc(a,b,c,s) do { \
ErtsDigit ___cr = (c); \
ErtsDigit ___xr = (a)+(___cr); \
ErtsDigit ___yr = (b); \
___cr = (___xr < ___cr); \
___xr = ___yr + ___xr; \
___cr += (___xr < ___yr); \
s = ___xr; \
c = ___cr; \
} while(0)
/* add a and b with carry out */
#define DSUM(a,b,c,s) do { \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b); \
___xr = ___yr + ___xr; \
s = ___xr; \
c = (___xr < ___yr); \
} while(0)
#define DSUBb(a,b,r,d) do { \
ErtsDigit ___cr = (r); \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b)+___cr; \
___cr = (___yr < ___cr); \
___yr = ___xr - ___yr; \
___cr += (___yr > ___xr); \
d = ___yr; \
r = ___cr; \
} while(0)
#define DSUB(a,b,r,d) do { \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b); \
___yr = ___xr - ___yr; \
r = (___yr > ___xr); \
d = ___yr; \
} while(0)
/* type a constant as a ErtsDigit - to get shifts correct */
#define DCONST(n) ((ErtsDigit)(n))
/*
* BIG_HAVE_DOUBLE_DIGIT is defined if we have defined
* the type ErtsDoubleDigit which MUST have
* sizeof(ErtsDoubleDigit) >= sizeof(ErtsDigit)
*/
#ifdef BIG_HAVE_DOUBLE_DIGIT
/* ErtsDoubleDigit => ErtsDigit */
#define DLOW(x) ((ErtsDigit)(x))
#define DHIGH(x) ((ErtsDigit)(((ErtsDoubleDigit)(x)) >> D_EXP))
/* ErtsDigit => ErtsDoubleDigit */
#define DLOW2HIGH(x) (((ErtsDoubleDigit)(x)) << D_EXP)
#define DDIGIT(a1,a0) (DLOW2HIGH(a1) + (a0))
#define DMULc(a,b,c,p) do { \
ErtsDoubleDigit _t = ((ErtsDoubleDigit)(a))*(b) + (c); \
p = DLOW(_t); \
c = DHIGH(_t); \
} while(0)
#define DMUL(a,b,c1,c0) do { \
ErtsDoubleDigit _t = ((ErtsDoubleDigit)(a))*(b); \
c0 = DLOW(_t); \
c1 = DHIGH(_t); \
} while(0)
#define DDIV(a1,a0,b,q) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
q = _t / (b); \
} while(0)
#define DDIV2(a1,a0,b1,b0,q) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
q = _t / DDIGIT((b1),(b0)); \
} while(0)
#define DREM(a1,a0,b,r) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
r = _t % (b); \
} while(0)
#else
/* If we do not have double digit then we have some more work to do */
#define H_EXP (D_EXP >> 1)
#define LO_MASK ((ErtsDigit)((DCONST(1) << H_EXP)-1))
#define HI_MASK ((ErtsDigit)(LO_MASK << H_EXP))
#define DGT(a,b) ((a)>(b))
#define DEQ(a,b) ((a)==(b))
#define D2GT(a1,a0,b1,b0) (DGT(a1,b1) || (((a1)==(b1)) && DGT(a0,b0)))
#define D2EQ(a1,a0,b1,b0) (DEQ(a1,b1) && DEQ(a0,b0))
#define D2LT(a1,a0,b1,b0) D2GT(b1,b0,a1,a0)
#define D2GTE(a1,a0,b1,b0) (!D2LT(a1,a0,b1,b0))
#define D2LTE(a1,a0,b1,b0) (!D2GT(a1,a0,b1,b0))
// Add (A+B), A=(a1B+a0) B=(b1B+b0)
#define D2ADD(a1,a0,b1,b0,c1,c0) do { \
ErtsDigit __ci = 0; \
DSUM(a0,b0,__ci,c0); \
DSUMc(a1,b1,__ci,c1); \
} while(0)
// Subtract (A-B), A=(a1B+a0), B=(b1B+b0) (A>=B)
#define D2SUB(a1,a0,b1,b0,c1,c0) do { \
ErtsDigit __bi; \
DSUB(a0,b0,__bi,c0); \
DSUBb(a1,b1,__bi,c1); \
} while(0)
/* Left shift (multiply by 2) (A <<= 1 where A=a1*B+a0) */
#define D2LSHIFT1(a1,a0) do { \
a1 = ((a0) >> (D_EXP-1)) | ((a1)<<1); \
a0 = (a0) << 1; \
} while(0)
/* Right shift (divide by 2) (A >>= 1 where A=a1*B+a0) */
#define D2RSHIFT1(a1,a0) do { \
a0 = (((a1) & 1) << (D_EXP-1)) | ((a0)>>1); \
a1 = ((a1) >> 1); \
} while(0)
/* Calculate a*b + d1 and store double prec result in d1, d0 */
#define DMULc(a,b,d1,d0) do { \
ErtsHalfDigit __a0 = (a); \
ErtsHalfDigit __a1 = ((a) >> H_EXP); \
ErtsHalfDigit __b0 = (b); \
ErtsHalfDigit __b1 = ((b) >> H_EXP); \
ErtsDigit __a0b0 = (ErtsDigit)__a0*__b0; \
ErtsDigit __a0b1 = (ErtsDigit)__a0*__b1; \
ErtsDigit __a1b0 = (ErtsDigit)__a1*__b0; \
ErtsDigit __a1b1 = (ErtsDigit)__a1*__b1; \
ErtsDigit __p0,__p1,__p2,__c0; \
DSUM(__a0b0,d1,__c0,__p0); \
DSUM((__c0<<H_EXP),(__p0>>H_EXP),__p2,__p1); \
DSUM(__p1,__a0b1,__c0,__p1); \
__p2 += __c0; \
DSUM(__p1,__a1b0,__c0,__p1); \
__p2 += __c0; \
DSUM(__p1,__a1b1<<H_EXP,__c0,__p1); \
__p2 += __c0; \
DSUM(__a1b1, (__p2<<H_EXP),__c0,__p2); \
d1 = (__p2 & HI_MASK) | (__p1 >> H_EXP); \
d0 = (__p1 << H_EXP) | (__p0 & LO_MASK); \
} while(0)
#define DMUL(a,b,d1,d0) do { \
ErtsDigit _ds = 0; \
DMULc(a,b,_ds,d0); \
d1 = _ds; \
} while(0)
/* Calculate a*(Bb1 + b0) + d2 = a*b1B + a*b0 + d2 */
#define D2MULc(a,b1,b0,d2,d1,d0) do { \
DMULc(a, b0, d2, d0); \
DMULc(a, b1, d2, d1); \
} while(0)
/* Calculate s in a = 2^s*a1 */
/* NOTE since D2PF is used by other macros variables is prefixed bt __ */
#if D_EXP == 64
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x00000000FFFFFFFF) { __s += 32; __x <<= 32; } \
if (__x <= 0x0000FFFFFFFFFFFF) { __s += 16; __x <<= 16; } \
if (__x <= 0x00FFFFFFFFFFFFFF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFFFFFFFFFFFFFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFFFFFFFFFFFFFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFFFFFFFFFFFFFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 32
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x0000FFFF) { __s += 16; __x <<= 16; } \
if (__x <= 0x00FFFFFF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFFFFFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFFFFFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFFFFFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 16
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x00FF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 8
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x0F) { __s += 4; __x <<= 4; } \
if (__x <= 0x3F) { __s += 2; __x <<= 2; } \
if (__x <= 0x7F) { __s += 1; } \
s = _s; \
} while(0)
#endif
/* Calculate q = (a1B + a0) / b, assume a1 < b */
#define DDIVREM(a1,a0,b,q,r) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b = (b); \
ErtsHalfDigit _un1, _un0; \
ErtsHalfDigit _vn1, _vn0; \
ErtsDigit _q1, _q0; \
ErtsDigit _un32, _un21, _un10; \
ErtsDigit _rh; \
Sint _s; \
D2PF(_b, _s); \
_b = _b << _s; \
_vn1 = _b >> H_EXP; \
_vn0 = _b & LO_MASK; \
_un32 = (_a1 << _s) | ((_a0>>(D_EXP-_s)) & (-_s >> (D_EXP-1))); \
_un10 = _a0 << _s; \
_un1 = _un10 >> H_EXP; \
_un0 = _un10 & LO_MASK; \
_q1 = _un32/_vn1; \
_rh = _un32 - _q1*_vn1; \
while ((_q1 >= (DCONST(1)<<H_EXP))||(_q1*_vn0 > (_rh<<H_EXP)+_un1)) { \
_q1--; \
_rh += _vn1; \
if (_rh >= (DCONST(1)<<H_EXP)) break; \
} \
_un21 = (_un32<<H_EXP) + _un1 - _q1*_b; \
_q0 = _un21/_vn1; \
_rh = _un21 - _q0*_vn1; \
while ((_q0 >= (DCONST(1)<<H_EXP))||(_q0*_vn0 > ((_rh<<H_EXP)+_un0))) { \
_q0--; \
_rh += _vn1; \
if (_rh >= (DCONST(1)<<H_EXP)) break; \
} \
r = ((_un21<<H_EXP) + _un0 - _q0*_b) >> _s; \
q = (_q1<<H_EXP) + _q0; \
} while(0)
/* divide any a=(a1*B + a0) with b */
#define DDIVREM2(a1,a0,b,q1,q0,r) do { \
ErtsDigit __a1 = (a1); \
ErtsDigit __b = (b); \
q1 = __a1 / __b; \
DDIVREM(__a1 % __b, (a0), __b, q0, r); \
} while(0)
/* Calculate q = (a1B + a0) % b */
#define DREM(a1,a0,b,r) do { \
ErtsDigit __a1 = (a1); \
ErtsDigit __b = (b); \
ErtsDigit __q0; \
DDIVREM((__a1 % __b), (a0), __b, __q0, r); \
} while(0)
#define DDIV(a1,a0,b,q) do { \
ErtsDigit _tmp; \
DDIVREM(a1,a0,b,q,_tmp); \
} while(0)
/* Calculate q, r A = Bq+R when, assume A1 >= B */
#if (SIZEOF_VOID_P == 8)
#define QUOT_LIM 0x7FFFFFFFFFFFFFFF
#else
#define QUOT_LIM 0x7FFFFFFF
#endif
#define D2DIVREM(a1,a0,b1,b0,q0,r1,r0) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b1 = (b1); \
ErtsDigit _b0 = (b0); \
ErtsDigit _q = 0; \
int _as = 1; \
while(D2GTE(_a1,_a0,_b1,_b0)) { \
ErtsDigit _q1; \
ErtsDigit _t2=0, _t1, _t0; \
if ((_b1 == 1) && (_a1 > 1)) \
_q1 = _a1 / 2; \
else if ((_a1 > QUOT_LIM) && (_b1 < _a1)) \
_q1 = _a1/(_b1+1); \
else \
_q1 = _a1/_b1; \
if (_as<0) \
_q -= _q1; \
else \
_q += _q1; \
D2MULc(_q1, _b1, _b0, _t2, _t1, _t0); \
ASSERT(_t2 == 0); \
if (D2GT(_t1,_t0,_a1,_a0)) { \
D2SUB(_t1,_t0,_a1,_a0,_a1,_a0); \
_as = -_as; \
} \
else { \
D2SUB(_a1,_a0,_t1,_t0,_a1,_a0); \
} \
} \
if (_as < 0) { \
_q--; \
D2SUB(_b1,_b0,_a1,_a0,_a1,_a0); \
} \
q0 = _q; \
r1 = _a1; \
r0 = _a0; \
} while(0)
/* Calculate q, r A = Bq+R when assume B>0 */
#define D2DIVREM_0(a1,a0,b1,b0,q1,q0,r1,r0) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b1 = (b1); \
ErtsDigit _b0 = (b0); \
if (D2EQ(_a1,_a0,0,0)) { \
q1 = q0 = 0; \
r1 = r0 = 0; \
} \
else { \
ErtsDigit _res1 = 0; \
ErtsDigit _res0 = 0; \
ErtsDigit _d1 = 0; \
ErtsDigit _d0 = 1; \
ErtsDigit _e1 = (1 << (D_EXP-1)); \
ErtsDigit _e0 = 0; \
while(_e1 && !(_a1 & _e1)) \
_e1 >>= 1; \
if (_e1 == 0) { \
_e0 = (1 << (D_EXP-1)); \
while(_e0 && !(_a0 & _e0)) \
_e0 >>= 1; \
} \
if (D2GT(_b1,_b0,0,0)) { \
while(D2GT(_e1,_e0,_b1,_b0)) { \
D2LSHIFT1(_b1,_b0); \
D2LSHIFT1(_d1,_d0); \
} \
} \
do { \
if (!D2GT(_b1,_b0,_a1,_a0)) { \
D2SUB(_a1,_a0, _b1, _b0, _a1, _a0); \
D2ADD(_d1,_d0, _res1,_res0, _res1, _res0); \
} \
D2RSHIFT1(_b1,_b0); \
D2RSHIFT1(_d1,_d0); \
} while (!D2EQ(_d1,_d0,0,0)); \
r1 = _a1; \
r0 = _a0; \
q1 = _res1; \
q0 = _res0; \
} \
} while(0)
#define DDIV2(a1,a0,b1,b0,q) do { \
ErtsDigit _tmp_r1; \
ErtsDigit _tmp_r0; \
D2DIVREM(a1,a0,b1,b0,q,_tmp_r1,_tmp_r0); \
} while(0)
#endif
/*
** compare two number vectors
*/
static int I_comp(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl)
{
if (xl < yl)
return -1;
else if (xl > yl)
return 1;
else {
if (x == y)
return 0;
x += (xl-1);
y += (yl-1);
while((xl > 0) && (*x == *y)) {
x--;
y--;
xl--;
}
if (xl == 0)
return 0;
return (*x < *y) ? -1 : 1;
}
}
/*
** Add digits in x and y and store them in r
** assumption: (xl >= yl)
*/
static dsize_t I_add(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
dsize_t sz = xl;
register ErtsDigit yr, xr;
register ErtsDigit c = 0;
ASSERT(xl >= yl);
xl -= yl;
do {
xr = *x++ + c;
yr = *y++;
c = (xr < c);
xr = yr + xr;
c += (xr < yr);
*r++ = xr;
} while(--yl);
while(xl--) {
xr = *x++ + c;
c = (xr < c);
*r++ = xr;
}
if (c) {
*r = 1;
return sz+1;
}
return sz;
}
/*
** Add a digits in v1 and store result in vr
*/
static dsize_t D_add(ErtsDigit* x, dsize_t xl, ErtsDigit c, ErtsDigit* r)
{
dsize_t sz = xl;
register ErtsDigit xr;
while(xl--) {
xr = *x++ + c;
c = (xr < c);
*r++ = xr;
}
if (c) {
*r = 1;
return sz+1;
}
return sz;
}
/*
** Subtract digits v2 from v1 and store result in v3
** Assert I_comp(x, xl, y, yl) >= 0
**
*/
static dsize_t I_sub(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr, xr;
register ErtsDigit c = 0;
ASSERT(I_comp(x, xl, y, yl) >= 0);
xl -= yl;
do {
yr = *y++ + c;
xr = *x++;
c = (yr < c);
yr = xr - yr;
c += (yr > xr);
*r++ = yr;
} while(--yl);
while(xl--) {
xr = *x++;
yr = xr - c;
c = (yr > xr);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Subtract digit d from v1 and store result in vr
*/
static dsize_t D_sub(ErtsDigit* x, dsize_t xl, ErtsDigit c, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr, xr;
ASSERT(I_comp(x, xl, x, 1) >= 0);
while(xl--) {
xr = *x++;
yr = xr - c;
c = (yr > xr);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** subtract Z000...0 - y and store result in r, return new size
*/
static dsize_t Z_sub(ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr;
register ErtsDigit c = 0;
while(yl--) {
yr = *y++ + c;
c = (yr < c);
yr = 0 - yr;
c += (yr > 0);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Multiply digits in x with digits in y and store in r
** Assumption: digits in r must be 0 (upto the size of x)
*/
static dsize_t I_mul(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
ErtsDigit* rt = r;
while(xl--) {
ErtsDigit cp = 0;
ErtsDigit c = 0;
dsize_t n = yl;
ErtsDigit* yt = y;
ErtsDigit d;
ErtsDigit p;
d = *x;
x++;
rt = r;
switch(d) {
case 0:
rt = rt + n;
break;
case 1:
while(n--) {
DSUMc(*yt, *rt, c, p);
*rt++ = p;
yt++;
}
break;
case 2:
while(n--) {
p = *yt;
DSUMc(p, p, cp, p);
DSUMc(p, *rt, c, p);
*rt++ = p;
yt++;
}
break;
default:
while(n--) {
DMULc(d,*yt, cp, p);
DSUMc(p,*rt, c, p);
*rt++ = p;
yt++;
}
break;
}
*rt = c + cp;
r++;
}
if (*rt == 0)
return (rt - r0);
else
return (rt - r0) + 1;
}
/*
** Square digits in x store in r (x & r may point into a common area)
** Assumption: x is destroyed if common area and digits in r are zero
** to the size of xl+1
*/
static dsize_t I_sqr(ErtsDigit* x, dsize_t xl, ErtsDigit* r)
{
ErtsDigit d_next = *x;
ErtsDigit d;
ErtsDigit* r0 = r;
ErtsDigit* s = r;
if ((r + xl) == x) /* "Inline" operation */
*x = 0;
x++;
while(xl--) {
ErtsDigit* y = x;
ErtsDigit y_0 = 0, y_1 = 0, y_2 = 0, y_3 = 0;
ErtsDigit b0, b1;
ErtsDigit z0, z1, z2;
ErtsDigit t;
dsize_t y_l = xl;
s = r;
d = d_next;
d_next = *x;
x++;
DMUL(d, d, b1, b0);
DSUMc(*s, b0, y_3, t);
*s++ = t;
z1 = b1;
while(y_l--) {
DMUL(d, *y, b1, b0);
y++;
DSUMc(b0, b0, y_0, z0);
DSUMc(z0, z1, y_2, z2);
DSUMc(*s, z2, y_3, t);
*s++ = t;
DSUMc(b1, b1, y_1, z1);
}
z0 = y_0;
DSUMc(z0, z1, y_2, z2);
DSUMc(*s, z2, y_3, t);
*s = t;
if (xl != 0) {
s++;
t = (y_1+y_2+y_3);
*s = t;
r += 2;
}
else {
ASSERT((y_1+y_2+y_3) == 0);
}
}
if (*s == 0)
return (s - r0);
else
return (s - r0) + 1;
}
/*
** Multiply digits d with digits in x and store in r
*/
static dsize_t D_mul(ErtsDigit* x, dsize_t xl, ErtsDigit d, ErtsDigit* r)
{
ErtsDigit c = 0;
dsize_t rl = xl;
ErtsDigit p;
switch(d) {
case 0:
ZERO_DIGITS(r, 1);
return 1;
case 1:
if (x != r)
MOVE_DIGITS(r, x, xl);
return xl;
case 2:
while(xl--) {
p = *x;
DSUMc(p, p, c, p);
*r++ = p;
x++;
}
break;
default:
while(xl--) {
DMULc(d, *x, c, p);
*r++ = p;
x++;
}
break;
}
if (c == 0)
return rl;
*r = c;
return rl+1;
}
/*
** Multiply and subtract
** calculate r(i) = x(i) - d*y(i)
** assumption: xl = yl || xl == yl+1
**
** Return size of r
** 0 means borrow
*/
static dsize_t D_mulsub(ErtsDigit* x, dsize_t xl, ErtsDigit d,
ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit c = 0;
ErtsDigit b = 0;
ErtsDigit c0;
ErtsDigit* r0 = r;
ErtsDigit s;
ASSERT(xl == yl || xl == yl+1);
xl -= yl;
while(yl--) {
DMULc(d, *y, c, c0);
DSUBb(*x, c0, b, s);
*r++ = s;
x++;
y++;
}
if (xl == 0) {
if (c != 0 || b != 0)
return 0;
}
else { /* xl == 1 */
DSUBb(*x, c, b, s);
*r++ = s;
}
if (b != 0) return 0;
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Divide digits in x with a digit,
** quotient is returned in q and remainder digit in r
** x and q may be equal
*/
static dsize_t D_div(ErtsDigit* x, dsize_t xl, ErtsDigit d, ErtsDigit* q, ErtsDigit* r)
{
ErtsDigit* xp = x + (xl-1);
ErtsDigit* qp = q + (xl-1);
dsize_t qsz = xl;
ErtsDigit a1;
a1 = *xp;
xp--;
if (d > a1) {
if (xl == 1) {
*r = a1;
*qp = 0;
return 1;
}
qsz--;
qp--;
}
do {
ErtsDigit q0, a0, b1, b0, b;
if (d > a1) {
a0 = *xp;
xp--;
}
else {
a0 = a1; a1 = 0;
}
DDIV(a1, a0, d, q0);
DMUL(d, q0, b1, b0);
DSUB(a0,b0, b, a1);
*qp = q0;
qp--;
} while (xp >= x);
*r = a1;
return qsz;
}
/*
** Divide digits in x with digits in y and return qutient in q
** and remainder in r
** assume that integer(x) > integer(y)
** Return remainder in x (length int rl)
** Return quotient size
*/
static dsize_t I_div(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl,
ErtsDigit* q, ErtsDigit* r, dsize_t* rlp)
{
ErtsDigit* rp;
ErtsDigit* qp;
ErtsDigit b1 = y[yl-1];
ErtsDigit b2 = y[yl-2];
ErtsDigit a1;
ErtsDigit a2;
int r_signed = 0;
dsize_t ql;
dsize_t rl;
if (x != r)
MOVE_DIGITS(r, x, xl);
rp = r + (xl-yl);
rl = xl;
ZERO_DIGITS(q, xl-yl+1);
qp = q + (xl-yl);
ql = 0;
/* Adjust length */
a1 = rp[yl-1];
a2 = rp[yl-2];
if (b1 < a1 || (b1 == a1 && b2 <= a2))
ql = 1;
do {
ErtsDigit q0;
dsize_t nsz = yl;
dsize_t nnsz;
a1 = rp[yl-1];
a2 = rp[yl-2];
if (b1 < a1)
DDIV2(a1,a2,b1,b2,q0);
else if (b1 > a1) {
DDIV(a1,a2,b1,q0);
nsz++;
rp--;
qp--;
ql++;
}
else { /* (b1 == a1) */
if (b2 <= a2)
q0 = 1;
else {
q0 = D_MASK;
nsz++;
rp--;
qp--;
ql++;
}
}
if (r_signed)
ql = D_sub(qp, ql, q0, qp);
else
ql = D_add(qp, ql, q0, qp);
if ((nnsz = D_mulsub(rp, nsz, q0, y, yl, rp)) == 0) {
nnsz = Z_sub(r, rl, r);
if (nsz > (rl-nnsz))
nnsz = nsz - (rl-nnsz);
else
nnsz = 1;
r_signed = !r_signed;
}
if ((nnsz == 1) && (*rp == 0))
nnsz = 0;
rp = rp - (yl-nnsz);
rl -= (nsz-nnsz);
qp = qp - (yl-nnsz);
ql += (yl-nnsz);
} while (I_comp(r, rl, y, yl) >= 0);
ql -= (q - qp);
qp = q;
if (rl == 0)
rl = 1;
while(rl > 1 && r[rl-1] == 0) /* Remove "trailing zeroes" */
--rl;
if (r_signed && (rl > 1 || *r != 0)) {
rl = I_sub(y, yl, r, rl, r);
ql = D_sub(qp, ql, 1, qp);
}
*rlp = rl;
return ql;
}
/*
** Remainder of digits in x and a digit d
*/
static ErtsDigit D_rem(ErtsDigit* x, dsize_t xl, ErtsDigit d)
{
ErtsDigit rem = 0;
x += (xl-1);
do {
if (rem != 0)
DREM(rem, *x, d, rem);
else
DREM(0, *x, d, rem);
x--;
xl--;
} while(xl > 0);
return rem;
}
/*
** Remainder of x and y
**
** Assumtions: xl >= yl, yl > 1
** r must contain at least xl number of digits
*/
static dsize_t I_rem(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* rp;
ErtsDigit b1 = y[yl-1];
ErtsDigit b2 = y[yl-2];
ErtsDigit a1;
ErtsDigit a2;
int r_signed = 0;
dsize_t rl;
if (x != r)
MOVE_DIGITS(r, x, xl);
rp = r + (xl-yl);
rl = xl;
do {
ErtsDigit q0;
dsize_t nsz = yl;
dsize_t nnsz;
a1 = rp[yl-1];
a2 = rp[yl-2];
if (b1 < a1)
DDIV2(a1,a2,b1,b2,q0);
else if (b1 > a1) {
DDIV(a1,a2,b1,q0);
nsz++;
rp--;
}
else { /* (b1 == a1) */
if (b2 <= a2)
q0 = 1;
else {
q0 = D_MASK;
nsz++;
rp--;
}
}
if ((nnsz = D_mulsub(rp, nsz, q0, y, yl, rp)) == 0) {
nnsz = Z_sub(r, rl, r);
if (nsz > (rl-nnsz))
nnsz = nsz - (rl-nnsz);
else
nnsz = 1;
r_signed = !r_signed;
}
if (nnsz == 1 && *rp == 0)
nnsz = 0;
rp = rp - (yl-nnsz);
rl -= (nsz-nnsz);
} while (I_comp(r, rl, y, yl) >= 0);
if (rl == 0)
rl = 1;
while(rl > 1 && r[rl-1] == 0) /* Remove "trailing zeroes" */
--rl;
if (r_signed && (rl > 1 || *r != 0))
rl = I_sub(y, yl, r, rl, r);
return rl;
}
/*
** Remove trailing digits from bitwise operations
*/
static dsize_t I_btrail(ErtsDigit* r0, ErtsDigit* r, short sign)
{
/* convert negative numbers to one complement */
if (sign) {
dsize_t rl;
ErtsDigit d;
/* 1 remove all 0xffff words */
do {
r--;
} while(((d = *r) == D_MASK) && (r != r0));
/* 2 complement high digit */
if (d == D_MASK)
*r = 0;
else {
ErtsDigit prev_mask = 0;
ErtsDigit mask = (DCONST(1) << (D_EXP-1));
while((d & mask) == mask) {
prev_mask = mask;
mask = (prev_mask >> 1) | (DCONST(1)<<(D_EXP-1));
}
*r = ~d & ~prev_mask;
}
rl = (r - r0) + 1;
while(r != r0) {
r--;
*r = ~*r;
}
return D_add(r0, rl, 1, r0);
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Bitwise and
*/
static dsize_t I_band(ErtsDigit* x, dsize_t xl, short xsgn,
ErtsDigit* y, dsize_t yl, short ysgn, ErtsDigit* r)
{
ErtsDigit* r0 = r;
short sign = xsgn && ysgn;
ASSERT(xl >= yl);
xl -= yl;
if (!xsgn) {
if (!ysgn) {
while(yl--)
*r++ = *x++ & *y++;
}
else {
ErtsDigit b;
ErtsDigit c;
DSUB(*y,1,b,c);
*r++ = *x++ & ~c;
y++;
yl--;
while(yl--) {
DSUBb(*y,0,b,c);
*r++ = *x++ & ~c;
y++;
}
while (xl--) {
*r++ = *x++;
}
}
}
else {
if (!ysgn) {
ErtsDigit b;
ErtsDigit c;
DSUB(*x,1,b,c);
*r = ~c & *y;
x++; y++; r++;
yl--;
while(yl--) {
DSUBb(*x,0,b,c);
*r++ = ~c & *y++;
x++;
}
}
else {
ErtsDigit b1, b2;
ErtsDigit c1, c2;
DSUB(*x,1,b1,c1);
DSUB(*y,1,b2,c2);
*r++ = ~c1 & ~c2;
x++; y++;
yl--;
while(yl--) {
DSUBb(*x,0,b1,c1);
DSUBb(*y,0,b2,c2);
*r++ = ~c1 & ~c2;
x++; y++;
}
while(xl--)
*r++ = ~*x++;
}
}
return I_btrail(r0, r, sign);
}
/*
* Bitwise 'or'.
*/
static dsize_t
I_bor(ErtsDigit* x, dsize_t xl, short xsgn, ErtsDigit* y,
dsize_t yl, short ysgn, ErtsDigit* r)
{
ErtsDigit* r0 = r;
short sign = xsgn || ysgn;
ASSERT(xl >= yl);
xl -= yl;
if (!xsgn) {
if (!ysgn) {
while(yl--)
*r++ = *x++ | *y++;
while(xl--)
*r++ = *x++;
}
else {
ErtsDigit b;
ErtsDigit c;
DSUB(*y,1,b,c);
*r++ = *x++ | ~c;
y++;
yl--;
while(yl--) {
DSUBb(*y,0,b,c);
*r++ = *x++ | ~c;
y++;
}
}
}
else {
if (!ysgn) {
ErtsDigit b;
ErtsDigit c;
DSUB(*x,1,b,c);
*r++ = ~c | *y++;
x++;
yl--;
while(yl--) {
DSUBb(*x,0,b,c);
*r++ = ~c | *y++;
x++;
}
while(xl--) {
DSUBb(*x,0,b,c);
*r++ = ~c;
x++;
}
}
else {
ErtsDigit b1, b2;
ErtsDigit c1, c2;
DSUB(*x,1,b1,c1);
DSUB(*y,1,b2,c2);
*r++ = ~c1 | ~c2;
x++; y++;
yl--;
while(yl--) {
DSUBb(*x,0,b1,c1);
DSUBb(*y,0,b2,c2);
*r++ = ~c1 | ~c2;
x++; y++;
}
}
}
return I_btrail(r0, r, sign);
}
/*
** Bitwise xor
*/
static dsize_t I_bxor(ErtsDigit* x, dsize_t xl, short xsgn,
ErtsDigit* y, dsize_t yl, short ysgn, ErtsDigit* r)
{
ErtsDigit* r0 = r;
short sign = xsgn != ysgn;
ASSERT(xl >= yl);
xl -= yl;
if (!xsgn) {
if (!ysgn) {
while(yl--)
*r++ = *x++ ^ *y++;
while(xl--)
*r++ = *x++;
}
else {
ErtsDigit b;
ErtsDigit c;
DSUB(*y,1,b,c);
*r++ = *x++ ^ ~c;
y++;
yl--;
while(yl--) {
DSUBb(*y,0,b,c);
*r++ = *x++ ^ ~c;
y++;
}
while(xl--)
*r++ = ~*x++;
}
}
else {
if (!ysgn) {
ErtsDigit b;
ErtsDigit c;
DSUB(*x,1,b,c);
*r++ = ~c ^ *y++;
x++;
yl--;
while(yl--) {
DSUBb(*x,0,b,c);
*r++ = ~c ^ *y++;
x++;
}
while(xl--)
*r++ = ~*x++;
}
else {
ErtsDigit b1, b2;
ErtsDigit c1, c2;
DSUB(*x,1,b1,c1);
DSUB(*y,1,b2,c2);
*r++ = ~c1 ^ ~c2;
x++; y++;
yl--;
while(yl--) {
DSUBb(*x,0,b1,c1);
DSUBb(*y,0,b2,c2);
*r++ = ~c1 ^ ~c2;
x++; y++;
}
while(xl--) {
*r++ = *x++;
}
}
}
return I_btrail(r0, r, sign);
}
/*
** Bitwise not simulated as
** bnot -X == (X - 1)
** bnot +X == -(X + 1)
*/
static dsize_t I_bnot(ErtsDigit* x, dsize_t xl, short xsgn, ErtsDigit* r)
{
if (xsgn)
return D_add(x, xl, 1, r);
else
return D_sub(x, xl, 1, r);
}
/*
** Arithmetic left shift or right
*/
static dsize_t I_lshift(ErtsDigit* x, dsize_t xl, Sint y,
short sign, ErtsDigit* r)
{
if (y == 0) {
MOVE_DIGITS(r, x, xl);
return xl;
}
else if (xl == 1 && *x == 0) {
*r = 0;
return 1;
}
else {
long ay = (y < 0) ? -y : y;
int bw = ay / D_EXP;
int sw = ay % D_EXP;
dsize_t rl;
ErtsDigit a1=0;
ErtsDigit a0=0;
if (y > 0) { /* shift left */
rl = xl + bw + 1;
while(bw--)
*r++ = 0;
if (sw) { // NOTE! x >> 32 is not = 0!
while(xl--) {
a0 = (*x << sw) | a1;
a1 = (*x >> (D_EXP - sw));
*r++ = a0;
x++;
}
}
else {
while(xl--) {
*r++ = *x++;
}
}
if (a1 == 0)
return rl-1;
*r = a1;
return rl;
}
else { /* shift right */
ErtsDigit* r0 = r;
int add_one = 0;
if (xl <= bw) {
if (sign)
*r = 1;
else
*r = 0;
return 1;
}
if (sign) {
int zl = bw;
ErtsDigit* z = x;
while(zl--) {
if (*z != 0) {
add_one = 1;
break;
}
z++;
}
}
rl = xl - bw;
x += (xl-1);
r += (rl-1);
xl -= bw;
if (sw) { // NOTE! x >> 32 is not = 0!
while(xl--) {
a1 = (*x >> sw) | a0;
a0 = (*x << (D_EXP-sw));
*r-- = a1;
x--;
}
}
else {
while(xl--) {
*r-- = *x--;
}
}
if (sign && (a0 != 0))
add_one = 1;
if (r[rl] == 0) {
if (rl == 1) {
if (sign)
r[1] = 1;
return 1;
}
rl--;
}
if (add_one)
return D_add(r0, rl, 1, r0);
return rl;
}
}
}
/*
** Return log(x)/log(2)
*/
static int I_lg(ErtsDigit* x, dsize_t xl)
{
dsize_t sz = xl - 1;
ErtsDigit d = x[sz];
sz *= D_EXP;
while(d != 0) {
d >>= 1;
sz++;
}
return sz - 1;
}
/*
** Create bigint on heap if necessary. Like the previously existing
** make_small_or_big(), except for a HAlloc() instead of an
** ArithAlloc().
** NOTE: Only use erts_make_integer(), when order of heap fragments is
** guaranteed to be correct.
*/
Eterm
erts_make_integer(Uint x, Process *p)
{
Eterm* hp;
if (IS_USMALL(0,x))
return make_small(x);
else {
hp = HAlloc(p, BIG_UINT_HEAP_SIZE);
return uint_to_big(x,hp);
}
}
/*
** convert Uint to bigint
** (must only be used if x is to big to be stored as a small)
*/
Eterm uint_to_big(Uint x, Eterm *y)
{
*y = make_pos_bignum_header(1);
BIG_DIGIT(y, 0) = x;
return make_big(y);
}
/*
** convert UWord to bigint
** (must only be used if x is to big to be stored as a small)
** Allocation is tricky, the heap need has to be calculated
** with the macro BIG_UWORD_HEAP_SIZE(x)
*/
Eterm uword_to_big(UWord x, Eterm *y)
{
#if HALFWORD_HEAP
Uint upper = x >> 32;
Uint lower = x & 0xFFFFFFFFUL;
if (upper == 0) {
*y = make_pos_bignum_header(1);
} else {
*y = make_pos_bignum_header(2);
BIG_DIGIT(y, 1) = upper;
}
BIG_DIGIT(y, 0) = lower;
#else
*y = make_pos_bignum_header(1);
BIG_DIGIT(y, 0) = x;
#endif
return make_big(y);
}
/*
** convert signed int to bigint
*/
Eterm small_to_big(Sint x, Eterm *y)
{
if (x >= 0) {
*y = make_pos_bignum_header(1);
} else {
x = -x;
*y = make_neg_bignum_header(1);
}
BIG_DIGIT(y, 0) = x;
return make_big(y);
}
Eterm erts_uint64_to_big(Uint64 x, Eterm **hpp)
{
Eterm *hp = *hpp;
#ifdef ARCH_32
if (x >= (((Uint64) 1) << 32)) {
*hp = make_pos_bignum_header(2);
BIG_DIGIT(hp, 0) = (Uint) (x & ((Uint) 0xffffffff));
BIG_DIGIT(hp, 1) = (Uint) ((x >> 32) & ((Uint) 0xffffffff));
*hpp += 2;
}
else
#endif
{
*hp = make_pos_bignum_header(1);
BIG_DIGIT(hp, 0) = (Uint) x;
*hpp += 1;
}
return make_big(hp);
}
Eterm erts_sint64_to_big(Sint64 x, Eterm **hpp)
{
Eterm *hp = *hpp;
int neg;
if (x >= 0)
neg = 0;
else {
neg = 1;
x = -x;
}
#ifdef ARCH_32
if (x >= (((Uint64) 1) << 32)) {
if (neg)
*hp = make_neg_bignum_header(2);
else
*hp = make_pos_bignum_header(2);
BIG_DIGIT(hp, 0) = (Uint) (x & ((Uint) 0xffffffff));
BIG_DIGIT(hp, 1) = (Uint) ((x >> 32) & ((Uint) 0xffffffff));
*hpp += 2;
}
else
#endif
{
if (neg)
*hp = make_neg_bignum_header(1);
else
*hp = make_pos_bignum_header(1);
BIG_DIGIT(hp, 0) = (Uint) x;
*hpp += 1;
}
return make_big(hp);
}
/*
** Convert a bignum to a double float
*/
int
big_to_double(Eterm x, double* resp)
{
double d = 0.0;
Eterm* xp = big_val(x);
dsize_t xl = BIG_SIZE(xp);
ErtsDigit* s = BIG_V(xp) + xl;
short xsgn = BIG_SIGN(xp);
double dbase = ((double)(D_MASK)+1);
#ifndef NO_FPE_SIGNALS
volatile unsigned long *fpexnp = erts_get_current_fp_exception();
#endif
__ERTS_SAVE_FP_EXCEPTION(fpexnp);
__ERTS_FP_CHECK_INIT(fpexnp);
while (xl--) {
d = d * dbase + *--s;
__ERTS_FP_ERROR(fpexnp, d, __ERTS_RESTORE_FP_EXCEPTION(fpexnp); return -1);
}
*resp = xsgn ? -d : d;
__ERTS_FP_ERROR(fpexnp,*resp,;);
__ERTS_RESTORE_FP_EXCEPTION(fpexnp);
return 0;
}
/*
** Estimate the number of decimal digits (include sign)
*/
int big_decimal_estimate(Eterm x)
{
Eterm* xp = big_val(x);
int lg = I_lg(BIG_V(xp), BIG_SIZE(xp));
int lg10 = ((lg+1)*28/93)+1;
if (BIG_SIGN(xp)) lg10++; /* add sign */
return lg10+1; /* add null */
}
/*
** Convert a bignum into a string of decimal numbers
*/
static void write_big(Eterm x, void (*write_func)(void *, char), void *arg)
{
Eterm* xp = big_val(x);
ErtsDigit* dx = BIG_V(xp);
dsize_t xl = BIG_SIZE(xp);
short sign = BIG_SIGN(xp);
ErtsDigit rem;
if (xl == 1 && *dx < D_DECIMAL_BASE) {
rem = *dx;
if (rem == 0)
(*write_func)(arg, '0');
else {
while(rem) {
(*write_func)(arg, (rem % 10) + '0');
rem /= 10;
}
}
}
else {
ErtsDigit* tmp = (ErtsDigit*) erts_alloc(ERTS_ALC_T_TMP,
sizeof(ErtsDigit)*xl);
dsize_t tmpl = xl;
MOVE_DIGITS(tmp, dx, xl);
while(1) {
tmpl = D_div(tmp, tmpl, D_DECIMAL_BASE, tmp, &rem);
if (tmpl == 1 && *tmp == 0) {
while(rem) {
(*write_func)(arg, (rem % 10)+'0');
rem /= 10;
}
break;
}
else {
int i = D_DECIMAL_EXP;
while(i--) {
(*write_func)(arg, (rem % 10)+'0');
rem /= 10;
}
}
}
erts_free(ERTS_ALC_T_TMP, (void *) tmp);
}
if (sign)
(*write_func)(arg, '-');
}
struct big_list__ {
Eterm *hp;
Eterm res;
};
static void
write_list(void *arg, char c)
{
struct big_list__ *blp = (struct big_list__ *) arg;
blp->res = CONS(blp->hp, make_small(c), blp->res);
blp->hp += 2;
}
Eterm erts_big_to_list(Eterm x, Eterm **hpp)
{
struct big_list__ bl;
bl.hp = *hpp;
bl.res = NIL;
write_big(x, write_list, (void *) &bl);
*hpp = bl.hp;
return bl.res;
}
static void
write_string(void *arg, char c)
{
*(--(*((char **) arg))) = c;
}
char *erts_big_to_string(Eterm x, char *buf, Uint buf_sz)
{
char *big_str = buf + buf_sz - 1;
*big_str = '\0';
write_big(x, write_string, (void *) &big_str);
ASSERT(buf <= big_str && big_str <= buf + buf_sz - 1);
return big_str;
}
/*
** Normalize a bignum given thing pointer length in digits and a sign
** patch zero if odd length
*/
static Eterm big_norm(Eterm *x, dsize_t xl, short sign)
{
Uint arity;
if (xl == 1) {
Uint y = BIG_DIGIT(x, 0);
if (D_EXP < SMALL_BITS || IS_USMALL(sign, y)) {
if (sign)
return make_small(-((Sint)y));
else
return make_small(y);
}
}
/* __alpha__: This was fixed */
if ((arity = BIG_NEED_SIZE(xl)-1) > BIG_ARITY_MAX)
return NIL; /* signal error (too big) */
if (sign) {
*x = make_neg_bignum_header(arity);
}
else {
*x = make_pos_bignum_header(arity);
}
return make_big(x);
}
/*
** Compare bignums
*/
int big_comp(Eterm x, Eterm y)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
if (BIG_SIGN(xp) == BIG_SIGN(yp)) {
int c = I_comp(BIG_V(xp), BIG_SIZE(xp), BIG_V(yp), BIG_SIZE(yp));
if (BIG_SIGN(xp))
return -c;
else
return c;
}
else
return BIG_SIGN(xp) ? -1 : 1;
}
/*
** Unsigned compare
*/
int big_ucomp(Eterm x, Eterm y)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
return I_comp(BIG_V(xp), BIG_SIZE(xp), BIG_V(yp), BIG_SIZE(yp));
}
/*
** Return number of bytes in the bignum
*/
dsize_t big_bytes(Eterm x)
{
Eterm* xp = big_val(x);
dsize_t sz = BIG_SIZE(xp);
ErtsDigit d = BIG_DIGIT(xp, sz-1);
sz = (sz-1) * sizeof(ErtsDigit);
while (d != 0) {
++sz;
d >>= 8;
}
return sz;
}
/*
** Load a bignum from bytes
** xsz is the number of bytes in xp
*/
Eterm bytes_to_big(byte *xp, dsize_t xsz, int xsgn, Eterm *r)
{
ErtsDigit* rwp = BIG_V(r);
dsize_t rsz = 0;
ErtsDigit d;
int i;
while(xsz >= sizeof(ErtsDigit)) {
d = 0;
for(i = sizeof(ErtsDigit); --i >= 0;)
d = (d << 8) | xp[i];
*rwp = d;
rwp++;
xsz -= sizeof(ErtsDigit);
xp += sizeof(ErtsDigit);
rsz++;
}
if (xsz > 0) {
d = 0;
for(i = xsz; --i >= 0;)
d = (d << 8) | xp[i];
*rwp = d;
rwp++;
rsz++;
}
return big_norm(r, rsz, (short) xsgn);
}
/*
** Store digits in the array of bytes pointed to by p
*/
byte* big_to_bytes(Eterm x, byte *p)
{
ErtsDigit* xr = big_v(x);
dsize_t xl = big_size(x);
ErtsDigit d;
int i;
while(xl > 1) {
d = *xr;
xr++;
for(i = 0; i < sizeof(ErtsDigit); ++i) {
p[i] = d & 0xff;
d >>= 8;
}
p += sizeof(ErtsDigit);
xl--;
}
d = *xr;
do {
*p++ = d & 0xff;
d >>= 8;
} while (d != 0);
return p;
}
/*
* Converts a positive term (small or bignum) to an Uint.
*
* Fails returning 0 if the term is neither a small nor a bignum,
* if it's negative, or the big number does not fit in an Uint;
* in addition the error reason, BADARG or SYSTEM_LIMIT, will be
* stored in *up.
*
* Otherwise returns a non-zero value and the converted number
* in *up.
*/
int
term_to_Uint(Eterm term, Uint *up)
{
if (is_small(term)) {
Sint i = signed_val(term);
if (i < 0) {
*up = BADARG;
return 0;
}
*up = (Uint) i;
return 1;
} else if (is_big(term)) {
ErtsDigit* xr = big_v(term);
dsize_t xl = big_size(term);
Uint uval = 0;
int n = 0;
if (big_sign(term)) {
*up = BADARG;
return 0;
} else if (xl*D_EXP > sizeof(Uint)*8) {
*up = SYSTEM_LIMIT;
return 0;
}
while (xl-- > 0) {
uval |= ((Uint)(*xr++)) << n;
n += D_EXP;
}
*up = uval;
return 1;
} else {
*up = BADARG;
return 0;
}
}
int term_to_Sint(Eterm term, Sint *sp)
{
if (is_small(term)) {
*sp = signed_val(term);
return 1;
} else if (is_big(term)) {
ErtsDigit* xr = big_v(term);
dsize_t xl = big_size(term);
int sign = big_sign(term);
Uint uval = 0;
int n = 0;
if (xl*D_EXP > sizeof(Uint)*8) {
return 0;
}
while (xl-- > 0) {
uval |= ((Uint)(*xr++)) << n;
n += D_EXP;
}
if (sign) {
uval = -uval;
if ((Sint)uval > 0)
return 0;
} else {
if ((Sint)uval < 0)
return 0;
}
*sp = uval;
return 1;
} else {
return 0;
}
}
/*
** Add and subtract
*/
static Eterm B_plus_minus(ErtsDigit *x, dsize_t xl, short xsgn,
ErtsDigit *y, dsize_t yl, short ysgn, Eterm *r)
{
if (xsgn == ysgn) {
if (xl > yl)
return big_norm(r, I_add(x,xl,y,yl,BIG_V(r)), xsgn);
else
return big_norm(r, I_add(y,yl,x,xl,BIG_V(r)), xsgn);
}
else {
int comp = I_comp(x, xl, y, yl);
if (comp == 0)
return make_small(0);
else if (comp > 0)
return big_norm(r, I_sub(x,xl,y,yl,BIG_V(r)), xsgn);
else
return big_norm(r, I_sub(y,yl,x,xl,BIG_V(r)), ysgn);
}
}
/*
** Add bignums
*/
Eterm big_plus(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
return B_plus_minus(BIG_V(xp),BIG_SIZE(xp),(short) BIG_SIGN(xp),
BIG_V(yp),BIG_SIZE(yp),(short) BIG_SIGN(yp), r);
}
/*
** Subtract bignums
*/
Eterm big_minus(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
return B_plus_minus(BIG_V(xp),BIG_SIZE(xp),(short) BIG_SIGN(xp),
BIG_V(yp),BIG_SIZE(yp),(short) !BIG_SIGN(yp), r);
}
/*
** Subtract a digit from big number
*/
Eterm big_minus_small(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
if (BIG_SIGN(xp))
return big_norm(r, D_add(BIG_V(xp),BIG_SIZE(xp), (ErtsDigit) y, BIG_V(r)),
(short) BIG_SIGN(xp));
else
return big_norm(r, D_sub(BIG_V(xp),BIG_SIZE(xp), (ErtsDigit) y, BIG_V(r)),
(short) BIG_SIGN(xp));
}
/*
** Multiply smallnums
*/
Eterm small_times(Sint x, Sint y, Eterm *r)
{
short sign = (x<0) != (y<0);
ErtsDigit xu = (x > 0) ? x : -x;
ErtsDigit yu = (y > 0) ? y : -y;
ErtsDigit d1=0;
ErtsDigit d0;
Uint arity;
DMULc(xu, yu, d1, d0);
if (!d1 && ((D_EXP < SMALL_BITS) || IS_USMALL(sign, d0))) {
if (sign)
return make_small(-((Sint)d0));
else
return make_small(d0);
}
BIG_DIGIT(r,0) = d0;
arity = d1 ? 2 : 1;
if (sign)
*r = make_neg_bignum_header(arity);
else
*r = make_pos_bignum_header(arity);
if (d1)
BIG_DIGIT(r,1) = d1;
return make_big(r);
}
/*
** Multiply bignums
*/
Eterm big_times(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short sign = BIG_SIGN(xp) != BIG_SIGN(yp);
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
dsize_t rsz;
if (ysz == 1)
rsz = D_mul(BIG_V(xp), xsz, BIG_DIGIT(yp, 0), BIG_V(r));
else if (xsz == 1)
rsz = D_mul(BIG_V(yp), ysz, BIG_DIGIT(xp, 0), BIG_V(r));
else if (xp == yp) {
ZERO_DIGITS(BIG_V(r), xsz+1);
rsz = I_sqr(BIG_V(xp), xsz, BIG_V(r));
}
else if (xsz >= ysz) {
ZERO_DIGITS(BIG_V(r), xsz);
rsz = I_mul(BIG_V(xp), xsz, BIG_V(yp), ysz, BIG_V(r));
}
else {
ZERO_DIGITS(BIG_V(r), ysz);
rsz = I_mul(BIG_V(yp), ysz, BIG_V(xp), xsz, BIG_V(r));
}
return big_norm(r, rsz, sign);
}
/*
** Divide bignums
*/
Eterm big_div(Eterm x, Eterm y, Eterm *q)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short sign = BIG_SIGN(xp) != BIG_SIGN(yp);
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
dsize_t qsz;
if (ysz == 1) {
ErtsDigit rem;
qsz = D_div(BIG_V(xp), xsz, BIG_DIGIT(yp,0), BIG_V(q), &rem);
}
else {
Eterm* remp;
dsize_t rem_sz;
qsz = xsz - ysz + 1;
remp = q + BIG_NEED_SIZE(qsz);
qsz = I_div(BIG_V(xp), xsz, BIG_V(yp), ysz, BIG_V(q), BIG_V(remp),
&rem_sz);
}
return big_norm(q, qsz, sign);
}
/*
** Remainder
*/
Eterm big_rem(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short sign = BIG_SIGN(xp);
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
if (ysz == 1) {
ErtsDigit rem;
rem = D_rem(BIG_V(xp), xsz, BIG_DIGIT(yp,0));
if (IS_USMALL(sign, rem)) {
if (sign)
return make_small(-(Sint)rem);
else
return make_small(rem);
}
else {
if (sign)
*r = make_neg_bignum_header(1);
else
*r = make_pos_bignum_header(1);
BIG_DIGIT(r, 0) = rem;
return make_big(r);
}
}
else {
dsize_t rsz = I_rem(BIG_V(xp), xsz, BIG_V(yp), ysz, BIG_V(r));
return big_norm(r, rsz, sign);
}
}
Eterm big_neg(Eterm x, Eterm *r)
{
Eterm* xp = big_val(x);
dsize_t xsz = BIG_SIZE(xp);
short xsgn = BIG_SIGN(xp);
MOVE_DIGITS(BIG_V(r), BIG_V(xp), xsz);
return big_norm(r, xsz, (short) !xsgn);
}
Eterm big_band(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short xsgn = BIG_SIGN(xp);
short ysgn = BIG_SIGN(yp);
short sign = xsgn && ysgn;
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
if (xsz >= ysz)
return big_norm(r,I_band(BIG_V(xp),xsz,xsgn,
BIG_V(yp),ysz,ysgn,
BIG_V(r)),sign);
else
return big_norm(r,I_band(BIG_V(yp),ysz,ysgn,
BIG_V(xp),xsz,xsgn,
BIG_V(r)),sign);
}
Eterm big_bor(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short xsgn = BIG_SIGN(xp);
short ysgn = BIG_SIGN(yp);
short sign = (xsgn || ysgn);
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
if (xsz >= ysz)
return big_norm(r,I_bor(BIG_V(xp),xsz,xsgn,
BIG_V(yp),ysz,ysgn,
BIG_V(r)),sign);
else
return big_norm(r,I_bor(BIG_V(yp),ysz,ysgn,
BIG_V(xp),xsz,xsgn,
BIG_V(r)),sign);
}
Eterm big_bxor(Eterm x, Eterm y, Eterm *r)
{
Eterm* xp = big_val(x);
Eterm* yp = big_val(y);
short xsgn = BIG_SIGN(xp);
short ysgn = BIG_SIGN(yp);
short sign = (xsgn != ysgn);
dsize_t xsz = BIG_SIZE(xp);
dsize_t ysz = BIG_SIZE(yp);
if (xsz >= ysz)
return big_norm(r,I_bxor(BIG_V(xp),xsz,xsgn,
BIG_V(yp),ysz,ysgn,
BIG_V(r)),sign);
else
return big_norm(r,I_bxor(BIG_V(yp),ysz,ysgn,
BIG_V(xp),xsz,xsgn,
BIG_V(r)),sign);
}
Eterm big_bnot(Eterm x, Eterm *r)
{
Eterm* xp = big_val(x);
short sign = !BIG_SIGN(xp);
dsize_t xsz = BIG_SIZE(xp);
return big_norm(r, I_bnot(BIG_V(xp), xsz, sign, BIG_V(r)), sign);
}
Eterm big_lshift(Eterm x, Sint y, Eterm *r)
{
Eterm* xp = big_val(x);
short sign = BIG_SIGN(xp);
dsize_t xsz = BIG_SIZE(xp);
return big_norm(r, I_lshift(BIG_V(xp), xsz, y, sign, BIG_V(r)), sign);
}
/* add unsigned small int y to x */
Eterm big_plus_small(Eterm x, Uint y, Eterm *r)
{
Eterm* xp = big_val(x);
if (BIG_SIGN(xp))
return big_norm(r, D_sub(BIG_V(xp),BIG_SIZE(xp), (ErtsDigit) y,
BIG_V(r)), (short) BIG_SIGN(xp));
else
return big_norm(r, D_add(BIG_V(xp),BIG_SIZE(xp), (ErtsDigit) y,
BIG_V(r)), (short) BIG_SIGN(xp));
}
Eterm big_times_small(Eterm x, Uint y, Eterm *r)
{
Eterm* xp = big_val(x);
return big_norm(r, D_mul(BIG_V(xp),BIG_SIZE(xp), (ErtsDigit) y,
BIG_V(r)), (short) BIG_SIGN(xp));
}
/*
** Expects the big to fit.
*/
Uint32 big_to_uint32(Eterm b)
{
Uint u;
if (!term_to_Uint(b, &u)) {
ASSERT(0);
return 0;
}
return u;
}
/*
* Check if a fixnum or bignum equals 2^32.
*/
int term_equals_2pow32(Eterm x)
{
if (sizeof(Uint) > 4) {
Uint u;
if (!term_to_Uint(x, &u))
return 0;
return (u & 0xFFFFFFFF) == 0 && ((u >> 16) >> 16) == 1;
} else {
Eterm *bp;
if (!is_big(x))
return 0;
bp = big_val(x);
#if D_EXP == 16 // 16 bit platfrom not really supported!!!
return (BIG_SIZE(bp) == 3) && !BIG_DIGIT(bp,0) && !BIG_DIGIT(bp,1) &&
BIG_DIGIT(bp,2) == 1;
#elif D_EXP == 32
return (BIG_SIZE(bp) == 2) && !BIG_DIGIT(bp,0) &&
BIG_DIGIT(bp,1) == 1;
#elif D_EXP == 64
return (BIG_SIZE(bp) == 1) &&
((BIG_DIGIT(bp,0) & 0xffffffff) == 0) &&
((BIG_DIGIT(bp,0) >> 32) == 1);
#endif
return 0;
}
}
|