aboutsummaryrefslogtreecommitdiff
path: root/libdecnumber/decBasic.c
blob: 27f98263b4e3a53864360c29b23f278030d27ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
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
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
/* Common base code for the decNumber C Library.
   Copyright (C) 2007-2022 Free Software Foundation, Inc.
   Contributed by IBM Corporation.  Author Mike Cowlishaw.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

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/>.  */

/* ------------------------------------------------------------------ */
/* decBasic.c -- common base code for Basic decimal types	      */
/* ------------------------------------------------------------------ */
/* This module comprises code that is shared between decDouble and    */
/* decQuad (but not decSingle).  The main arithmetic operations are   */
/* here (Add, Subtract, Multiply, FMA, and Division operators).       */
/*								      */
/* Unlike decNumber, parameterization takes place at compile time     */
/* rather than at runtime.  The parameters are set in the decDouble.c */
/* (etc.) files, which then include this one to produce the compiled  */
/* code.  The functions here, therefore, are code shared between      */
/* multiple formats.						      */
/*								      */
/* This must be included after decCommon.c.			      */
/* ------------------------------------------------------------------ */
/* Names here refer to decFloat rather than to decDouble, etc., and */
/* the functions are in strict alphabetical order. */

/* The compile-time flags SINGLE, DOUBLE, and QUAD are set up in */
/* decCommon.c */
#if !defined(QUAD)
  #error decBasic.c must be included after decCommon.c
#endif
#if SINGLE
  #error Routines in decBasic.c are for decDouble and decQuad only
#endif

/* Private constants */
#define DIVIDE	    0x80000000	   /* Divide operations [as flags] */
#define REMAINDER   0x40000000	   /* .. */
#define DIVIDEINT   0x20000000	   /* .. */
#define REMNEAR     0x10000000	   /* .. */

/* Private functions (local, used only by routines in this module) */
static decFloat *decDivide(decFloat *, const decFloat *,
			      const decFloat *, decContext *, uInt);
static decFloat *decCanonical(decFloat *, const decFloat *);
static void	 decFiniteMultiply(bcdnum *, uByte *, const decFloat *,
			      const decFloat *);
static decFloat *decInfinity(decFloat *, const decFloat *);
static decFloat *decInvalid(decFloat *, decContext *);
static decFloat *decNaNs(decFloat *, const decFloat *, const decFloat *,
			      decContext *);
static Int	 decNumCompare(const decFloat *, const decFloat *, Flag);
static decFloat *decToIntegral(decFloat *, const decFloat *, decContext *,
			      enum rounding, Flag);
static uInt	 decToInt32(const decFloat *, decContext *, enum rounding,
			      Flag, Flag);

/* ------------------------------------------------------------------ */
/* decCanonical -- copy a decFloat, making canonical		      */
/*								      */
/*   result gets the canonicalized df				      */
/*   df     is the decFloat to copy and make canonical		      */
/*   returns result						      */
/*								      */
/* This is exposed via decFloatCanonical for Double and Quad only.    */
/* This works on specials, too; no error or exception is possible.    */
/* ------------------------------------------------------------------ */
static decFloat * decCanonical(decFloat *result, const decFloat *df) {
  uInt encode, precode, dpd;	   /* work */
  uInt inword, uoff, canon;	   /* .. */
  Int  n;			   /* counter (down) */
  if (df!=result) *result=*df;	   /* effect copy if needed */
  if (DFISSPECIAL(result)) {
    if (DFISINF(result)) return decInfinity(result, df); /* clean Infinity */
    /* is a NaN */
    DFWORD(result, 0)&=~ECONNANMASK;	/* clear ECON except selector */
    if (DFISCCZERO(df)) return result;	/* coefficient continuation is 0 */
    /* drop through to check payload */
    }
  /* return quickly if the coefficient continuation is canonical */
  { /* declare block */
  #if DOUBLE
    uInt sourhi=DFWORD(df, 0);
    uInt sourlo=DFWORD(df, 1);
    if (CANONDPDOFF(sourhi, 8)
     && CANONDPDTWO(sourhi, sourlo, 30)
     && CANONDPDOFF(sourlo, 20)
     && CANONDPDOFF(sourlo, 10)
     && CANONDPDOFF(sourlo, 0)) return result;
  #elif QUAD
    uInt sourhi=DFWORD(df, 0);
    uInt sourmh=DFWORD(df, 1);
    uInt sourml=DFWORD(df, 2);
    uInt sourlo=DFWORD(df, 3);
    if (CANONDPDOFF(sourhi, 4)
     && CANONDPDTWO(sourhi, sourmh, 26)
     && CANONDPDOFF(sourmh, 16)
     && CANONDPDOFF(sourmh, 6)
     && CANONDPDTWO(sourmh, sourml, 28)
     && CANONDPDOFF(sourml, 18)
     && CANONDPDOFF(sourml, 8)
     && CANONDPDTWO(sourml, sourlo, 30)
     && CANONDPDOFF(sourlo, 20)
     && CANONDPDOFF(sourlo, 10)
     && CANONDPDOFF(sourlo, 0)) return result;
  #endif
  } /* block */

  /* Loop to repair a non-canonical coefficent, as needed */
  inword=DECWORDS-1;		   /* current input word */
  uoff=0;			   /* bit offset of declet */
  encode=DFWORD(result, inword);
  for (n=DECLETS-1; n>=0; n--) {   /* count down declets of 10 bits */
    dpd=encode>>uoff;
    uoff+=10;
    if (uoff>32) {		   /* crossed uInt boundary */
      inword--;
      encode=DFWORD(result, inword);
      uoff-=32;
      dpd|=encode<<(10-uoff);	   /* get pending bits */
      }
    dpd&=0x3ff; 		   /* clear uninteresting bits */
    if (dpd<0x16e) continue;	   /* must be canonical */
    canon=BIN2DPD[DPD2BIN[dpd]];   /* determine canonical declet */
    if (canon==dpd) continue;	   /* have canonical declet */
    /* need to replace declet */
    if (uoff>=10) {		   /* all within current word */
      encode&=~(0x3ff<<(uoff-10)); /* clear the 10 bits ready for replace */
      encode|=canon<<(uoff-10);    /* insert the canonical form */
      DFWORD(result, inword)=encode;	/* .. and save */
      continue;
      }
    /* straddled words */
    precode=DFWORD(result, inword+1);	/* get previous */
    precode&=0xffffffff>>(10-uoff);	/* clear top bits */
    DFWORD(result, inword+1)=precode|(canon<<(32-(10-uoff)));
    encode&=0xffffffff<<uoff;		/* clear bottom bits */
    encode|=canon>>(10-uoff);		/* insert canonical */
    DFWORD(result, inword)=encode;	/* .. and save */
    } /* n */
  return result;
  } /* decCanonical */

/* ------------------------------------------------------------------ */
/* decDivide -- divide operations				      */
/*								      */
/*   result gets the result of dividing dfl by dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   op     is the operation selector				      */
/*   returns result						      */
/*								      */
/* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR.	      */
/* ------------------------------------------------------------------ */
#define DIVCOUNT  0		   /* 1 to instrument subtractions counter */
#define DIVBASE   ((uInt)BILLION)  /* the base used for divide */
#define DIVOPLEN  DECPMAX9	   /* operand length ('digits' base 10**9) */
#define DIVACCLEN (DIVOPLEN*3)	   /* accumulator length (ditto) */
static decFloat * decDivide(decFloat *result, const decFloat *dfl,
			    const decFloat *dfr, decContext *set, uInt op) {
  decFloat quotient;		   /* for remainders */
  bcdnum num;			   /* for final conversion */
  uInt	 acc[DIVACCLEN];	   /* coefficent in base-billion .. */
  uInt	 div[DIVOPLEN]; 	   /* divisor in base-billion .. */
  uInt	 quo[DIVOPLEN+1];	   /* quotient in base-billion .. */
  uByte  bcdacc[(DIVOPLEN+1)*9+2]; /* for quotient in BCD, +1, +1 */
  uInt	 *msua, *msud, *msuq;	   /* -> msu of acc, div, and quo */
  Int	 divunits, accunits;	   /* lengths */
  Int	 quodigits;		   /* digits in quotient */
  uInt	 *lsua, *lsuq;		   /* -> current acc and quo lsus */
  Int	 length, multiplier;	   /* work */
  uInt	 carry, sign;		   /* .. */
  uInt	 *ua, *ud, *uq; 	   /* .. */
  uByte  *ub;			   /* .. */
  uInt	 uiwork;		   /* for macros */
  uInt	 divtop;		   /* top unit of div adjusted for estimating */
  #if DIVCOUNT
  static uInt maxcount=0;	   /* worst-seen subtractions count */
  uInt	 divcount=0;		   /* subtractions count [this divide] */
  #endif

  /* calculate sign */
  num.sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;

  if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */
    /* NaNs are handled as usual */
    if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    /* one or two infinities */
    if (DFISINF(dfl)) {
      if (DFISINF(dfr)) return decInvalid(result, set); /* Two infinities bad */
      if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* as is rem */
      /* Infinity/x is infinite and quiet, even if x=0 */
      DFWORD(result, 0)=num.sign;
      return decInfinity(result, result);
      }
    /* must be x/Infinity -- remainders are lhs */
    if (op&(REMAINDER|REMNEAR)) return decCanonical(result, dfl);
    /* divides: return zero with correct sign and exponent depending */
    /* on op (Etiny for divide, 0 for divideInt) */
    decFloatZero(result);
    if (op==DIVIDEINT) DFWORD(result, 0)|=num.sign; /* add sign */
     else DFWORD(result, 0)=num.sign;	     /* zeros the exponent, too */
    return result;
    }
  /* next, handle zero operands (x/0 and 0/x) */
  if (DFISZERO(dfr)) {			     /* x/0 */
    if (DFISZERO(dfl)) {		     /* 0/0 is undefined */
      decFloatZero(result);
      DFWORD(result, 0)=DECFLOAT_qNaN;
      set->status|=DEC_Division_undefined;
      return result;
      }
    if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* bad rem */
    set->status|=DEC_Division_by_zero;
    DFWORD(result, 0)=num.sign;
    return decInfinity(result, result);      /* x/0 -> signed Infinity */
    }
  num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr);  /* ideal exponent */
  if (DFISZERO(dfl)) {			     /* 0/x (x!=0) */
    /* if divide, result is 0 with ideal exponent; divideInt has */
    /* exponent=0, remainders give zero with lower exponent */
    if (op&DIVIDEINT) {
      decFloatZero(result);
      DFWORD(result, 0)|=num.sign;	     /* add sign */
      return result;
      }
    if (!(op&DIVIDE)) { 		     /* a remainder */
      /* exponent is the minimum of the operands */
      num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr));
      /* if the result is zero the sign shall be sign of dfl */
      num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
      }
    bcdacc[0]=0;
    num.msd=bcdacc;			     /* -> 0 */
    num.lsd=bcdacc;			     /* .. */
    return decFinalize(result, &num, set);   /* [divide may clamp exponent] */
    } /* 0/x */
  /* [here, both operands are known to be finite and non-zero] */

  /* extract the operand coefficents into 'units' which are */
  /* base-billion; the lhs is high-aligned in acc and the msu of both */
  /* acc and div is at the right-hand end of array (offset length-1); */
  /* the quotient can need one more unit than the operands as digits */
  /* in it are not necessarily aligned neatly; further, the quotient */
  /* may not start accumulating until after the end of the initial */
  /* operand in acc if that is small (e.g., 1) so the accumulator */
  /* must have at least that number of units extra (at the ls end) */
  GETCOEFFBILL(dfl, acc+DIVACCLEN-DIVOPLEN);
  GETCOEFFBILL(dfr, div);
  /* zero the low uInts of acc */
  acc[0]=0;
  acc[1]=0;
  acc[2]=0;
  acc[3]=0;
  #if DOUBLE
    #if DIVOPLEN!=2
      #error Unexpected Double DIVOPLEN
    #endif
  #elif QUAD
  acc[4]=0;
  acc[5]=0;
  acc[6]=0;
  acc[7]=0;
    #if DIVOPLEN!=4
      #error Unexpected Quad DIVOPLEN
    #endif
  #endif

  /* set msu and lsu pointers */
  msua=acc+DIVACCLEN-1;       /* [leading zeros removed below] */
  msuq=quo+DIVOPLEN;
  /*[loop for div will terminate because operands are non-zero] */
  for (msud=div+DIVOPLEN-1; *msud==0;) msud--;
  /* the initial least-significant unit of acc is set so acc appears */
  /* to have the same length as div. */
  /* This moves one position towards the least possible for each */
  /* iteration */
  divunits=(Int)(msud-div+1); /* precalculate */
  lsua=msua-divunits+1;       /* initial working lsu of acc */
  lsuq=msuq;		      /* and of quo */

  /* set up the estimator for the multiplier; this is the msu of div, */
  /* plus two bits from the unit below (if any) rounded up by one if */
  /* there are any non-zero bits or units below that [the extra two */
  /* bits makes for a much better estimate when the top unit is small] */
  divtop=*msud<<2;
  if (divunits>1) {
    uInt *um=msud-1;
    uInt d=*um;
    if (d>=750000000) {divtop+=3; d-=750000000;}
     else if (d>=500000000) {divtop+=2; d-=500000000;}
     else if (d>=250000000) {divtop++; d-=250000000;}
    if (d) divtop++;
     else for (um--; um>=div; um--) if (*um) {
      divtop++;
      break;
      }
    } /* >1 unit */

  #if DECTRACE
  {Int i;
  printf("----- div=");
  for (i=divunits-1; i>=0; i--) printf("%09ld ", (LI)div[i]);
  printf("\n");}
  #endif

  /* now collect up to DECPMAX+1 digits in the quotient (this may */
  /* need OPLEN+1 uInts if unaligned) */
  quodigits=0;		      /* no digits yet */
  for (;; lsua--) {	      /* outer loop -- each input position */
    #if DECCHECK
    if (lsua<acc) {
      printf("Acc underrun...\n");
      break;
      }
    #endif
    #if DECTRACE
    printf("Outer: quodigits=%ld acc=", (LI)quodigits);
    for (ua=msua; ua>=lsua; ua--) printf("%09ld ", (LI)*ua);
    printf("\n");
    #endif
    *lsuq=0;		      /* default unit result is 0 */
    for (;;) {		      /* inner loop -- calculate quotient unit */
      /* strip leading zero units from acc (either there initially or */
      /* from subtraction below); this may strip all if exactly 0 */
      for (; *msua==0 && msua>=lsua;) msua--;
      accunits=(Int)(msua-lsua+1);		  /* [maybe 0] */
      /* subtraction is only necessary and possible if there are as */
      /* least as many units remaining in acc for this iteration as */
      /* there are in div */
      if (accunits<divunits) {
	if (accunits==0) msua++;		  /* restore */
	break;
	}

      /* If acc is longer than div then subtraction is definitely */
      /* possible (as msu of both is non-zero), but if they are the */
      /* same length a comparison is needed. */
      /* If a subtraction is needed then a good estimate of the */
      /* multiplier for the subtraction is also needed in order to */
      /* minimise the iterations of this inner loop because the */
      /* subtractions needed dominate division performance. */
      if (accunits==divunits) {
	/* compare the high divunits of acc and div: */
	/* acc<div:  this quotient unit is unchanged; subtraction */
	/*	     will be possible on the next iteration */
	/* acc==div: quotient gains 1, set acc=0 */
	/* acc>div:  subtraction necessary at this position */
	for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break;
	/* [now at first mismatch or lsu] */
	if (*ud>*ua) break;			  /* next time... */
	if (*ud==*ua) { 			  /* all compared equal */
	  *lsuq+=1;				  /* increment result */
	  msua=lsua;				  /* collapse acc units */
	  *msua=0;				  /* .. to a zero */
	  break;
	  }

	/* subtraction necessary; estimate multiplier [see above] */
	/* if both *msud and *msua are small it is cost-effective to */
	/* bring in part of the following units (if any) to get a */
	/* better estimate (assume some other non-zero in div) */
	#define DIVLO 1000000U
	#define DIVHI (DIVBASE/DIVLO)
	#if DECUSE64
	  if (divunits>1) {
	    /* there cannot be a *(msud-2) for DECDOUBLE so next is */
	    /* an exact calculation unless DECQUAD (which needs to */
	    /* assume bits out there if divunits>2) */
	    uLong mul=(uLong)*msua * DIVBASE + *(msua-1);
	    uLong div=(uLong)*msud * DIVBASE + *(msud-1);
	    #if QUAD
	    if (divunits>2) div++;
	    #endif
	    mul/=div;
	    multiplier=(Int)mul;
	    }
	   else multiplier=*msua/(*msud);
	#else
	  if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
	    multiplier=(*msua*DIVHI + *(msua-1)/DIVLO)
		      /(*msud*DIVHI + *(msud-1)/DIVLO +1);
	    }
	   else multiplier=(*msua<<2)/divtop;
	#endif
	}
       else {					  /* accunits>divunits */
	/* msud is one unit 'lower' than msua, so estimate differently */
	#if DECUSE64
	  uLong mul;
	  /* as before, bring in extra digits if possible */
	  if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
	    mul=((uLong)*msua * DIVHI * DIVBASE) + *(msua-1) * DIVHI
	       + *(msua-2)/DIVLO;
	    mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1);
	    }
	   else if (divunits==1) {
	    mul=(uLong)*msua * DIVBASE + *(msua-1);
	    mul/=*msud;       /* no more to the right */
	    }
	   else {
	    mul=(uLong)(*msua) * (uInt)(DIVBASE<<2)
		+ (*(msua-1)<<2);
	    mul/=divtop;      /* [divtop already allows for sticky bits] */
	    }
	  multiplier=(Int)mul;
	#else
	  multiplier=*msua * ((DIVBASE<<2)/divtop);
	#endif
	}
      if (multiplier==0) multiplier=1;		  /* marginal case */
      *lsuq+=multiplier;

      #if DIVCOUNT
      /* printf("Multiplier: %ld\n", (LI)multiplier); */
      divcount++;
      #endif

      /* Carry out the subtraction  acc-(div*multiplier); for each */
      /* unit in div, do the multiply, split to units (see */
      /* decFloatMultiply for the algorithm), and subtract from acc */
      #define DIVMAGIC	2305843009U		  /* 2**61/10**9 */
      #define DIVSHIFTA 29
      #define DIVSHIFTB 32
      carry=0;
      for (ud=div, ua=lsua; ud<=msud; ud++, ua++) {
	uInt lo, hop;
	#if DECUSE64
	  uLong sub=(uLong)multiplier*(*ud)+carry;
	  if (sub<DIVBASE) {
	    carry=0;
	    lo=(uInt)sub;
	    }
	   else {
	    hop=(uInt)(sub>>DIVSHIFTA);
	    carry=(uInt)(((uLong)hop*DIVMAGIC)>>DIVSHIFTB);
	    /* the estimate is now in hi; now calculate sub-hi*10**9 */
	    /* to get the remainder (which will be <DIVBASE)) */
	    lo=(uInt)sub;
	    lo-=carry*DIVBASE;			  /* low word of result */
	    if (lo>=DIVBASE) {
	      lo-=DIVBASE;			  /* correct by +1 */
	      carry++;
	      }
	    }
	#else /* 32-bit */
	  uInt hi;
	  /* calculate multiplier*(*ud) into hi and lo */
	  LONGMUL32HI(hi, *ud, multiplier);	  /* get the high word */
	  lo=multiplier*(*ud);			  /* .. and the low */
	  lo+=carry;				  /* add the old hi */
	  carry=hi+(lo<carry);			  /* .. with any carry */
	  if (carry || lo>=DIVBASE) {		  /* split is needed */
	    hop=(carry<<3)+(lo>>DIVSHIFTA);	  /* hi:lo/2**29 */
	    LONGMUL32HI(carry, hop, DIVMAGIC);	  /* only need the high word */
	    /* [DIVSHIFTB is 32, so carry can be used directly] */
	    /* the estimate is now in carry; now calculate hi:lo-est*10**9; */
	    /* happily the top word of the result is irrelevant because it */
	    /* will always be zero so this needs only one multiplication */
	    lo-=(carry*DIVBASE);
	    /* the correction here will be at most +1; do it */
	    if (lo>=DIVBASE) {
	      lo-=DIVBASE;
	      carry++;
	      }
	    }
	#endif
	if (lo>*ua) {		   /* borrow needed */
	  *ua+=DIVBASE;
	  carry++;
	  }
	*ua-=lo;
	} /* ud loop */
      if (carry) *ua-=carry;	   /* accdigits>divdigits [cannot borrow] */
      } /* inner loop */

    /* the outer loop terminates when there is either an exact result */
    /* or enough digits; first update the quotient digit count and */
    /* pointer (if any significant digits) */
    #if DECTRACE
    if (*lsuq || quodigits) printf("*lsuq=%09ld\n", (LI)*lsuq);
    #endif
    if (quodigits) {
      quodigits+=9;		   /* had leading unit earlier */
      lsuq--;
      if (quodigits>DECPMAX+1) break;	/* have enough */
      }
     else if (*lsuq) {		   /* first quotient digits */
      const uInt *pow;
      for (pow=DECPOWERS; *lsuq>=*pow; pow++) quodigits++;
      lsuq--;
      /* [cannot have >DECPMAX+1 on first unit] */
      }

    if (*msua!=0) continue;	   /* not an exact result */
    /* acc is zero iff used all of original units and zero down to lsua */
    /* (must also continue to original lsu for correct quotient length) */
    if (lsua>acc+DIVACCLEN-DIVOPLEN) continue;
    for (; msua>lsua && *msua==0;) msua--;
    if (*msua==0 && msua==lsua) break;
    } /* outer loop */

  /* all of the original operand in acc has been covered at this point */
  /* quotient now has at least DECPMAX+2 digits */
  /* *msua is now non-0 if inexact and sticky bits */
  /* lsuq is one below the last uint of the quotient */
  lsuq++;			   /* set -> true lsu of quo */
  if (*msua) *lsuq|=1;		   /* apply sticky bit */

  /* quo now holds the (unrounded) quotient in base-billion; one */
  /* base-billion 'digit' per uInt. */
  #if DECTRACE
  printf("DivQuo:");
  for (uq=msuq; uq>=lsuq; uq--) printf(" %09ld", (LI)*uq);
  printf("\n");
  #endif

  /* Now convert to BCD for rounding and cleanup, starting from the */
  /* most significant end [offset by one into bcdacc to leave room */
  /* for a possible carry digit if rounding for REMNEAR is needed] */
  for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) {
    uInt top, mid, rem; 		/* work */
    if (*uq==0) {			/* no split needed */
      UBFROMUI(ub, 0);			/* clear 9 BCD8s */
      UBFROMUI(ub+4, 0);		/* .. */
      *(ub+8)=0;			/* .. */
      continue;
      }
    /* *uq is non-zero -- split the base-billion digit into */
    /* hi, mid, and low three-digits */
    #define divsplit9 1000000		/* divisor */
    #define divsplit6 1000		/* divisor */
    /* The splitting is done by simple divides and remainders, */
    /* assuming the compiler will optimize these [GCC does] */
    top=*uq/divsplit9;
    rem=*uq%divsplit9;
    mid=rem/divsplit6;
    rem=rem%divsplit6;
    /* lay out the nine BCD digits (plus one unwanted byte) */
    UBFROMUI(ub,   UBTOUI(&BIN2BCD8[top*4]));
    UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
    UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
    } /* BCD conversion loop */
  ub--; 				/* -> lsu */

  /* complete the bcdnum; quodigits is correct, so the position of */
  /* the first non-zero is known */
  num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
  num.lsd=ub;

  /* make exponent adjustments, etc */
  if (lsua<acc+DIVACCLEN-DIVOPLEN) {	/* used extra digits */
    num.exponent-=(Int)((acc+DIVACCLEN-DIVOPLEN-lsua)*9);
    /* if the result was exact then there may be up to 8 extra */
    /* trailing zeros in the overflowed quotient final unit */
    if (*msua==0) {
      for (; *ub==0;) ub--;		/* drop zeros */
      num.exponent+=(Int)(num.lsd-ub);	/* and adjust exponent */
      num.lsd=ub;
      }
    } /* adjustment needed */

  #if DIVCOUNT
  if (divcount>maxcount) {		/* new high-water nark */
    maxcount=divcount;
    printf("DivNewMaxCount: %ld\n", (LI)maxcount);
    }
  #endif

  if (op&DIVIDE) return decFinalize(result, &num, set); /* all done */

  /* Is DIVIDEINT or a remainder; there is more to do -- first form */
  /* the integer (this is done 'after the fact', unlike as in */
  /* decNumber, so as not to tax DIVIDE) */

  /* The first non-zero digit will be in the first 9 digits, known */
  /* from quodigits and num.msd, so there is always space for DECPMAX */
  /* digits */

  length=(Int)(num.lsd-num.msd+1);
  /*printf("Length exp: %ld %ld\n", (LI)length, (LI)num.exponent); */

  if (length+num.exponent>DECPMAX) { /* cannot fit */
    decFloatZero(result);
    DFWORD(result, 0)=DECFLOAT_qNaN;
    set->status|=DEC_Division_impossible;
    return result;
    }

  if (num.exponent>=0) {	   /* already an int, or need pad zeros */
    for (ub=num.lsd+1; ub<=num.lsd+num.exponent; ub++) *ub=0;
    num.lsd+=num.exponent;
    }
   else {			   /* too long: round or truncate needed */
    Int drop=-num.exponent;
    if (!(op&REMNEAR)) {	   /* simple truncate */
      num.lsd-=drop;
      if (num.lsd<num.msd) {	   /* truncated all */
	num.lsd=num.msd;	   /* make 0 */
	*num.lsd=0;		   /* .. [sign still relevant] */
	}
      }
     else {			   /* round to nearest even [sigh] */
      /* round-to-nearest, in-place; msd is at or to right of bcdacc+1 */
      /* (this is a special case of Quantize -- q.v. for commentary) */
      uByte *roundat;		   /* -> re-round digit */
      uByte reround;		   /* reround value */
      *(num.msd-1)=0;		   /* in case of left carry, or make 0 */
      if (drop<length) roundat=num.lsd-drop+1;
       else if (drop==length) roundat=num.msd;
       else roundat=num.msd-1;	   /* [-> 0] */
      reround=*roundat;
      for (ub=roundat+1; ub<=num.lsd; ub++) {
	if (*ub!=0) {
	  reround=DECSTICKYTAB[reround];
	  break;
	  }
	} /* check stickies */
      if (roundat>num.msd) num.lsd=roundat-1;
       else {
	num.msd--;			     /* use the 0 .. */
	num.lsd=num.msd;		     /* .. at the new MSD place */
	}
      if (reround!=0) { 		     /* discarding non-zero */
	uInt bump=0;
	/* rounding is DEC_ROUND_HALF_EVEN always */
	if (reround>5) bump=1;		     /* >0.5 goes up */
	 else if (reround==5)		     /* exactly 0.5000 .. */
	  bump=*(num.lsd) & 0x01;	     /* .. up iff [new] lsd is odd */
	if (bump!=0) {			     /* need increment */
	  /* increment the coefficient; this might end up with 1000... */
	  ub=num.lsd;
	  for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0);
	  for (; *ub==9; ub--) *ub=0;	     /* at most 3 more */
	  *ub+=1;
	  if (ub<num.msd) num.msd--;	     /* carried */
	  } /* bump needed */
	} /* reround!=0 */
      } /* remnear */
    } /* round or truncate needed */
  num.exponent=0;			     /* all paths */
  /*decShowNum(&num, "int"); */

  if (op&DIVIDEINT) return decFinalize(result, &num, set); /* all done */

  /* Have a remainder to calculate */
  decFinalize(&quotient, &num, set);	     /* lay out the integer so far */
  DFWORD(&quotient, 0)^=DECFLOAT_Sign;	     /* negate it */
  sign=DFWORD(dfl, 0);			     /* save sign of dfl */
  decFloatFMA(result, &quotient, dfr, dfl, set);
  if (!DFISZERO(result)) return result;
  /* if the result is zero the sign shall be sign of dfl */
  DFWORD(&quotient, 0)=sign;		     /* construct decFloat of sign */
  return decFloatCopySign(result, result, &quotient);
  } /* decDivide */

/* ------------------------------------------------------------------ */
/* decFiniteMultiply -- multiply two finite decFloats		      */
/*								      */
/*   num    gets the result of multiplying dfl and dfr		      */
/*   bcdacc .. with the coefficient in this array		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*								      */
/* This effects the multiplication of two decFloats, both known to be */
/* finite, leaving the result in a bcdnum ready for decFinalize (for  */
/* use in Multiply) or in a following addition (FMA).		      */
/*								      */
/* bcdacc must have space for at least DECPMAX9*18+1 bytes.	      */
/* No error is possible and no status is set.			      */
/* ------------------------------------------------------------------ */
/* This routine has two separate implementations of the core */
/* multiplication; both using base-billion.  One uses only 32-bit */
/* variables (Ints and uInts) or smaller; the other uses uLongs (for */
/* multiplication and addition only).  Both implementations cover */
/* both arithmetic sizes (DOUBLE and QUAD) in order to allow timing */
/* comparisons.  In any one compilation only one implementation for */
/* each size can be used, and if DECUSE64 is 0 then use of the 32-bit */
/* version is forced. */
/* */
/* Historical note: an earlier version of this code also supported the */
/* 256-bit format and has been preserved.  That is somewhat trickier */
/* during lazy carry splitting because the initial quotient estimate */
/* (est) can exceed 32 bits. */

#define MULTBASE  ((uInt)BILLION)  /* the base used for multiply */
#define MULOPLEN  DECPMAX9	   /* operand length ('digits' base 10**9) */
#define MULACCLEN (MULOPLEN*2)		    /* accumulator length (ditto) */
#define LEADZEROS (MULACCLEN*9 - DECPMAX*2) /* leading zeros always */

/* Assertions: exponent not too large and MULACCLEN is a multiple of 4 */
#if DECEMAXD>9
  #error Exponent may overflow when doubled for Multiply
#endif
#if MULACCLEN!=(MULACCLEN/4)*4
  /* This assumption is used below only for initialization */
  #error MULACCLEN is not a multiple of 4
#endif

static void decFiniteMultiply(bcdnum *num, uByte *bcdacc,
			      const decFloat *dfl, const decFloat *dfr) {
  uInt	 bufl[MULOPLEN];	   /* left  coefficient (base-billion) */
  uInt	 bufr[MULOPLEN];	   /* right coefficient (base-billion) */
  uInt	 *ui, *uj;		   /* work */
  uByte  *ub;			   /* .. */
  uInt	 uiwork;		   /* for macros */

  #if DECUSE64
  uLong  accl[MULACCLEN];	   /* lazy accumulator (base-billion+) */
  uLong  *pl;			   /* work -> lazy accumulator */
  uInt	 acc[MULACCLEN];	   /* coefficent in base-billion .. */
  #else
  uInt	 acc[MULACCLEN*2];	   /* accumulator in base-billion .. */
  #endif
  uInt	 *pa;			   /* work -> accumulator */
  /*printf("Base10**9: OpLen=%d MulAcclen=%d\n", OPLEN, MULACCLEN); */

  /* Calculate sign and exponent */
  num->sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
  num->exponent=GETEXPUN(dfl)+GETEXPUN(dfr); /* [see assertion above] */

  /* Extract the coefficients and prepare the accumulator */
  /* the coefficients of the operands are decoded into base-billion */
  /* numbers in uInt arrays (bufl and bufr, LSD at offset 0) of the */
  /* appropriate size. */
  GETCOEFFBILL(dfl, bufl);
  GETCOEFFBILL(dfr, bufr);
  #if DECTRACE && 0
    printf("CoeffbL:");
    for (ui=bufl+MULOPLEN-1; ui>=bufl; ui--) printf(" %08lx", (LI)*ui);
    printf("\n");
    printf("CoeffbR:");
    for (uj=bufr+MULOPLEN-1; uj>=bufr; uj--) printf(" %08lx", (LI)*uj);
    printf("\n");
  #endif

  /* start the 64-bit/32-bit differing paths... */
#if DECUSE64

  /* zero the accumulator */
  #if MULACCLEN==4
    accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0;
  #else 				     /* use a loop */
    /* MULACCLEN is a multiple of four, asserted above */
    for (pl=accl; pl<accl+MULACCLEN; pl+=4) {
      *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;/* [reduce overhead] */
      } /* pl */
  #endif

  /* Effect the multiplication */
  /* The multiplcation proceeds using MFC's lazy-carry resolution */
  /* algorithm from decNumber.	First, the multiplication is */
  /* effected, allowing accumulation of the partial products (which */
  /* are in base-billion at each column position) into 64 bits */
  /* without resolving back to base=billion after each addition. */
  /* These 64-bit numbers (which may contain up to 19 decimal digits) */
  /* are then split using the Clark & Cowlishaw algorithm (see below). */
  /* [Testing for 0 in the inner loop is not really a 'win'] */
  for (ui=bufr; ui<bufr+MULOPLEN; ui++) { /* over each item in rhs */
    if (*ui==0) continue;		  /* product cannot affect result */
    pl=accl+(ui-bufr);			  /* where to add the lhs */
    for (uj=bufl; uj<bufl+MULOPLEN; uj++, pl++) { /* over each item in lhs */
      /* if (*uj==0) continue;		  // product cannot affect result */
      *pl+=((uLong)*ui)*(*uj);
      } /* uj */
    } /* ui */

  /* The 64-bit carries must now be resolved; this means that a */
  /* quotient/remainder has to be calculated for base-billion (1E+9). */
  /* For this, Clark & Cowlishaw's quotient estimation approach (also */
  /* used in decNumber) is needed, because 64-bit divide is generally */
  /* extremely slow on 32-bit machines, and may be slower than this */
  /* approach even on 64-bit machines.	This algorithm splits X */
  /* using: */
  /* */
  /*   magic=2**(A+B)/1E+9;   // 'magic number' */
  /*   hop=X/2**A;	      // high order part of X (by shift) */
  /*   est=magic*hop/2**B     // quotient estimate (may be low by 1) */
  /* */
  /* A and B are quite constrained; hop and magic must fit in 32 bits, */
  /* and 2**(A+B) must be as large as possible (which is 2**61 if */
  /* magic is to fit).	Further, maxX increases with the length of */
  /* the operands (and hence the number of partial products */
  /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */
  /* */
  /* It can be shown that when OPLEN is 2 then the maximum error in */
  /* the estimated quotient is <1, but for larger maximum x the */
  /* maximum error is above 1 so a correction that is >1 may be */
  /* needed.  Values of A and B are chosen to satisfy the constraints */
  /* just mentioned while minimizing the maximum error (and hence the */
  /* maximum correction), as shown in the following table: */
  /* */
  /*   Type    OPLEN   A   B	 maxX	 maxError  maxCorrection */
  /*   --------------------------------------------------------- */
  /*   DOUBLE	 2    29  32  <2*10**18    0.63       1 */
  /*   QUAD	 4    30  31  <4*10**18    1.17       2 */
  /* */
  /* In the OPLEN==2 case there is most choice, but the value for B */
  /* of 32 has a big advantage as then the calculation of the */
  /* estimate requires no shifting; the compiler can extract the high */
  /* word directly after multiplying magic*hop. */
  #define MULMAGIC 2305843009U		/* 2**61/10**9	[both cases] */
  #if DOUBLE
    #define MULSHIFTA 29
    #define MULSHIFTB 32
  #elif QUAD
    #define MULSHIFTA 30
    #define MULSHIFTB 31
  #else
    #error Unexpected type
  #endif

  #if DECTRACE
  printf("MulAccl:");
  for (pl=accl+MULACCLEN-1; pl>=accl; pl--)
    printf(" %08lx:%08lx", (LI)(*pl>>32), (LI)(*pl&0xffffffff));
  printf("\n");
  #endif

  for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { /* each column position */
    uInt lo, hop;			/* work */
    uInt est;				/* cannot exceed 4E+9 */
    if (*pl>=MULTBASE) {
      /* *pl holds a binary number which needs to be split */
      hop=(uInt)(*pl>>MULSHIFTA);
      est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB);
      /* the estimate is now in est; now calculate hi:lo-est*10**9; */
      /* happily the top word of the result is irrelevant because it */
      /* will always be zero so this needs only one multiplication */
      lo=(uInt)(*pl-((uLong)est*MULTBASE));  /* low word of result */
      /* If QUAD, the correction here could be +2 */
      if (lo>=MULTBASE) {
	lo-=MULTBASE;			/* correct by +1 */
	est++;
	#if QUAD
	/* may need to correct by +2 */
	if (lo>=MULTBASE) {
	  lo-=MULTBASE;
	  est++;
	  }
	#endif
	}
      /* finally place lo as the new coefficient 'digit' and add est to */
      /* the next place up [this is safe because this path is never */
      /* taken on the final iteration as *pl will fit] */
      *pa=lo;
      *(pl+1)+=est;
      } /* *pl needed split */
     else {				/* *pl<MULTBASE */
      *pa=(uInt)*pl;			/* just copy across */
      }
    } /* pl loop */

#else  /* 32-bit */
  for (pa=acc;; pa+=4) {		     /* zero the accumulator */
    *pa=0; *(pa+1)=0; *(pa+2)=0; *(pa+3)=0;  /* [reduce overhead] */
    if (pa==acc+MULACCLEN*2-4) break;	     /* multiple of 4 asserted */
    } /* pa */

  /* Effect the multiplication */
  /* uLongs are not available (and in particular, there is no uLong */
  /* divide) but it is still possible to use MFC's lazy-carry */
  /* resolution algorithm from decNumber.  First, the multiplication */
  /* is effected, allowing accumulation of the partial products */
  /* (which are in base-billion at each column position) into 64 bits */
  /* [with the high-order 32 bits in each position being held at */
  /* offset +ACCLEN from the low-order 32 bits in the accumulator]. */
  /* These 64-bit numbers (which may contain up to 19 decimal digits) */
  /* are then split using the Clark & Cowlishaw algorithm (see */
  /* below). */
  for (ui=bufr;; ui++) {		/* over each item in rhs */
    uInt hi, lo;			/* words of exact multiply result */
    pa=acc+(ui-bufr);			/* where to add the lhs */
    for (uj=bufl;; uj++, pa++) {	/* over each item in lhs */
      LONGMUL32HI(hi, *ui, *uj);	/* calculate product of digits */
      lo=(*ui)*(*uj);			/* .. */
      *pa+=lo;				/* accumulate low bits and .. */
      *(pa+MULACCLEN)+=hi+(*pa<lo);	/* .. high bits with any carry */
      if (uj==bufl+MULOPLEN-1) break;
      }
    if (ui==bufr+MULOPLEN-1) break;
    }

  /* The 64-bit carries must now be resolved; this means that a */
  /* quotient/remainder has to be calculated for base-billion (1E+9). */
  /* For this, Clark & Cowlishaw's quotient estimation approach (also */
  /* used in decNumber) is needed, because 64-bit divide is generally */
  /* extremely slow on 32-bit machines.  This algorithm splits X */
  /* using: */
  /* */
  /*   magic=2**(A+B)/1E+9;   // 'magic number' */
  /*   hop=X/2**A;	      // high order part of X (by shift) */
  /*   est=magic*hop/2**B     // quotient estimate (may be low by 1) */
  /* */
  /* A and B are quite constrained; hop and magic must fit in 32 bits, */
  /* and 2**(A+B) must be as large as possible (which is 2**61 if */
  /* magic is to fit).	Further, maxX increases with the length of */
  /* the operands (and hence the number of partial products */
  /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */
  /* */
  /* It can be shown that when OPLEN is 2 then the maximum error in */
  /* the estimated quotient is <1, but for larger maximum x the */
  /* maximum error is above 1 so a correction that is >1 may be */
  /* needed.  Values of A and B are chosen to satisfy the constraints */
  /* just mentioned while minimizing the maximum error (and hence the */
  /* maximum correction), as shown in the following table: */
  /* */
  /*   Type    OPLEN   A   B	 maxX	 maxError  maxCorrection */
  /*   --------------------------------------------------------- */
  /*   DOUBLE	 2    29  32  <2*10**18    0.63       1 */
  /*   QUAD	 4    30  31  <4*10**18    1.17       2 */
  /* */
  /* In the OPLEN==2 case there is most choice, but the value for B */
  /* of 32 has a big advantage as then the calculation of the */
  /* estimate requires no shifting; the high word is simply */
  /* calculated from multiplying magic*hop. */
  #define MULMAGIC 2305843009U		/* 2**61/10**9	[both cases] */
  #if DOUBLE
    #define MULSHIFTA 29
    #define MULSHIFTB 32
  #elif QUAD
    #define MULSHIFTA 30
    #define MULSHIFTB 31
  #else
    #error Unexpected type
  #endif

  #if DECTRACE
  printf("MulHiLo:");
  for (pa=acc+MULACCLEN-1; pa>=acc; pa--)
    printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa);
  printf("\n");
  #endif

  for (pa=acc;; pa++) { 		/* each low uInt */
    uInt hi, lo;			/* words of exact multiply result */
    uInt hop, estlo;			/* work */
    #if QUAD
    uInt esthi; 			/* .. */
    #endif

    lo=*pa;
    hi=*(pa+MULACCLEN); 		/* top 32 bits */
    /* hi and lo now hold a binary number which needs to be split */

    #if DOUBLE
      hop=(hi<<3)+(lo>>MULSHIFTA);	/* hi:lo/2**29 */
      LONGMUL32HI(estlo, hop, MULMAGIC);/* only need the high word */
      /* [MULSHIFTB is 32, so estlo can be used directly] */
      /* the estimate is now in estlo; now calculate hi:lo-est*10**9; */
      /* happily the top word of the result is irrelevant because it */
      /* will always be zero so this needs only one multiplication */
      lo-=(estlo*MULTBASE);
      /* esthi=0;			// high word is ignored below */
      /* the correction here will be at most +1; do it */
      if (lo>=MULTBASE) {
	lo-=MULTBASE;
	estlo++;
	}
    #elif QUAD
      hop=(hi<<2)+(lo>>MULSHIFTA);	/* hi:lo/2**30 */
      LONGMUL32HI(esthi, hop, MULMAGIC);/* shift will be 31 .. */
      estlo=hop*MULMAGIC;		/* .. so low word needed */
      estlo=(esthi<<1)+(estlo>>MULSHIFTB); /* [just the top bit] */
      /* esthi=0;			// high word is ignored below */
      lo-=(estlo*MULTBASE);		/* as above */
      /* the correction here could be +1 or +2 */
      if (lo>=MULTBASE) {
	lo-=MULTBASE;
	estlo++;
	}
      if (lo>=MULTBASE) {
	lo-=MULTBASE;
	estlo++;
	}
    #else
      #error Unexpected type
    #endif

    /* finally place lo as the new accumulator digit and add est to */
    /* the next place up; this latter add could cause a carry of 1 */
    /* to the high word of the next place */
    *pa=lo;
    *(pa+1)+=estlo;
    /* esthi is always 0 for DOUBLE and QUAD so this is skipped */
    /* *(pa+1+MULACCLEN)+=esthi; */
    if (*(pa+1)<estlo) *(pa+1+MULACCLEN)+=1; /* carry */
    if (pa==acc+MULACCLEN-2) break;	     /* [MULACCLEN-1 will never need split] */
    } /* pa loop */
#endif

  /* At this point, whether using the 64-bit or the 32-bit paths, the */
  /* accumulator now holds the (unrounded) result in base-billion; */
  /* one base-billion 'digit' per uInt. */
  #if DECTRACE
  printf("MultAcc:");
  for (pa=acc+MULACCLEN-1; pa>=acc; pa--) printf(" %09ld", (LI)*pa);
  printf("\n");
  #endif

  /* Now convert to BCD for rounding and cleanup, starting from the */
  /* most significant end */
  pa=acc+MULACCLEN-1;
  if (*pa!=0) num->msd=bcdacc+LEADZEROS;/* drop known lead zeros */
   else {				/* >=1 word of leading zeros */
    num->msd=bcdacc;			/* known leading zeros are gone */
    pa--;				/* skip first word .. */
    for (; *pa==0; pa--) if (pa==acc) break; /* .. and any more leading 0s */
    }
  for (ub=bcdacc;; pa--, ub+=9) {
    if (*pa!=0) {			/* split(s) needed */
      uInt top, mid, rem;		/* work */
      /* *pa is non-zero -- split the base-billion acc digit into */
      /* hi, mid, and low three-digits */
      #define mulsplit9 1000000 	/* divisor */
      #define mulsplit6 1000		/* divisor */
      /* The splitting is done by simple divides and remainders, */
      /* assuming the compiler will optimize these where useful */
      /* [GCC does] */
      top=*pa/mulsplit9;
      rem=*pa%mulsplit9;
      mid=rem/mulsplit6;
      rem=rem%mulsplit6;
      /* lay out the nine BCD digits (plus one unwanted byte) */
      UBFROMUI(ub,   UBTOUI(&BIN2BCD8[top*4]));
      UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
      UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
      }
     else {				/* *pa==0 */
      UBFROMUI(ub, 0);			/* clear 9 BCD8s */
      UBFROMUI(ub+4, 0);		/* .. */
      *(ub+8)=0;			/* .. */
      }
    if (pa==acc) break;
    } /* BCD conversion loop */

  num->lsd=ub+8;			/* complete the bcdnum .. */

  #if DECTRACE
  decShowNum(num, "postmult");
  decFloatShow(dfl, "dfl");
  decFloatShow(dfr, "dfr");
  #endif
  return;
  } /* decFiniteMultiply */

/* ------------------------------------------------------------------ */
/* decFloatAbs -- absolute value, heeding NaNs, etc.		      */
/*								      */
/*   result gets the canonicalized df with sign 0		      */
/*   df     is the decFloat to abs				      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This has the same effect as decFloatPlus unless df is negative,    */
/* in which case it has the same effect as decFloatMinus.  The	      */
/* effect is also the same as decFloatCopyAbs except that NaNs are    */
/* handled normally (the sign of a NaN is not affected, and an sNaN   */
/* will signal) and the result will be canonical.		      */
/* ------------------------------------------------------------------ */
decFloat * decFloatAbs(decFloat *result, const decFloat *df,
		       decContext *set) {
  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  decCanonical(result, df);		/* copy and check */
  DFBYTE(result, 0)&=~0x80;		/* zero sign bit */
  return result;
  } /* decFloatAbs */

/* ------------------------------------------------------------------ */
/* decFloatAdd -- add two decFloats				      */
/*								      */
/*   result gets the result of adding dfl and dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
#if QUAD
/* Table for testing MSDs for fastpath elimination; returns the MSD of */
/* a decDouble or decQuad (top 6 bits tested) ignoring the sign. */
/* Infinities return -32 and NaNs return -128 so that summing the two */
/* MSDs also allows rapid tests for the Specials (see code below). */
const Int DECTESTMSD[64]={
  0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,   6,    7,
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128,
  0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,   6,    7,
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128};
#else
/* The table for testing MSDs is shared between the modules */
extern const Int DECTESTMSD[64];
#endif

decFloat * decFloatAdd(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  bcdnum num;			   /* for final conversion */
  Int	 bexpl, bexpr;		   /* left and right biased exponents */
  uByte  *ub, *us, *ut; 	   /* work */
  uInt	 uiwork;		   /* for macros */
  #if QUAD
  uShort uswork;		   /* .. */
  #endif

  uInt sourhil, sourhir;	   /* top words from source decFloats */
				   /* [valid only through end of */
				   /* fastpath code -- before swap] */
  uInt diffsign;		   /* non-zero if signs differ */
  uInt carry;			   /* carry: 0 or 1 before add loop */
  Int  overlap; 		   /* coefficient overlap (if full) */
  Int  summ;			   /* sum of the MSDs */
  /* the following buffers hold coefficients with various alignments */
  /* (see commentary and diagrams below) */
  uByte acc[4+2+DECPMAX*3+8];
  uByte buf[4+2+DECPMAX*2];
  uByte *umsd, *ulsd;		   /* local MSD and LSD pointers */

  #if DECLITEND
    #define CARRYPAT 0x01000000    /* carry=1 pattern */
  #else
    #define CARRYPAT 0x00000001    /* carry=1 pattern */
  #endif

  /* Start decoding the arguments */
  /* The initial exponents are placed into the opposite Ints to */
  /* that which might be expected; there are two sets of data to */
  /* keep track of (each decFloat and the corresponding exponent), */
  /* and this scheme means that at the swap point (after comparing */
  /* exponents) only one pair of words needs to be swapped */
  /* whichever path is taken (thereby minimising worst-case path). */
  /* The calculated exponents will be nonsense when the arguments are */
  /* Special, but are not used in that path */
  sourhil=DFWORD(dfl, 0);	   /* LHS top word */
  summ=DECTESTMSD[sourhil>>26];    /* get first MSD for testing */
  bexpr=DECCOMBEXP[sourhil>>26];   /* get exponent high bits (in place) */
  bexpr+=GETECON(dfl);		   /* .. + continuation */

  sourhir=DFWORD(dfr, 0);	   /* RHS top word */
  summ+=DECTESTMSD[sourhir>>26];   /* sum MSDs for testing */
  bexpl=DECCOMBEXP[sourhir>>26];
  bexpl+=GETECON(dfr);

  /* here bexpr has biased exponent from lhs, and vice versa */

  diffsign=(sourhil^sourhir)&DECFLOAT_Sign;

  /* now determine whether to take a fast path or the full-function */
  /* slow path.  The slow path must be taken when: */
  /*   -- both numbers are finite, and: */
  /*	     the exponents are different, or */
  /*	     the signs are different, or */
  /*	     the sum of the MSDs is >8 (hence might overflow) */
  /* specialness and the sum of the MSDs can be tested at once using */
  /* the summ value just calculated, so the test for specials is no */
  /* longer on the worst-case path (as of 3.60) */

  if (summ<=8) {		   /* MSD+MSD is good, or there is a special */
    if (summ<0) {		   /* there is a special */
      /* Inf+Inf would give -64; Inf+finite is -32 or higher */
      if (summ<-64) return decNaNs(result, dfl, dfr, set);  /* one or two NaNs */
      /* two infinities with different signs is invalid */
      if (summ==-64 && diffsign) return decInvalid(result, set);
      if (DFISINF(dfl)) return decInfinity(result, dfl);    /* LHS is infinite */
      return decInfinity(result, dfr);			    /* RHS must be Inf */
      }
    /* Here when both arguments are finite; fast path is possible */
    /* (currently only for aligned and same-sign) */
    if (bexpr==bexpl && !diffsign) {
      uInt tac[DECLETS+1];		/* base-1000 coefficient */
      uInt encode;			/* work */

      /* Get one coefficient as base-1000 and add the other */
      GETCOEFFTHOU(dfl, tac);		/* least-significant goes to [0] */
      ADDCOEFFTHOU(dfr, tac);
      /* here the sum of the MSDs (plus any carry) will be <10 due to */
      /* the fastpath test earlier */

      /* construct the result; low word is the same for both formats */
      encode =BIN2DPD[tac[0]];
      encode|=BIN2DPD[tac[1]]<<10;
      encode|=BIN2DPD[tac[2]]<<20;
      encode|=BIN2DPD[tac[3]]<<30;
      DFWORD(result, (DECBYTES/4)-1)=encode;

      /* collect next two declets (all that remains, for Double) */
      encode =BIN2DPD[tac[3]]>>2;
      encode|=BIN2DPD[tac[4]]<<8;

      #if QUAD
      /* complete and lay out middling words */
      encode|=BIN2DPD[tac[5]]<<18;
      encode|=BIN2DPD[tac[6]]<<28;
      DFWORD(result, 2)=encode;

      encode =BIN2DPD[tac[6]]>>4;
      encode|=BIN2DPD[tac[7]]<<6;
      encode|=BIN2DPD[tac[8]]<<16;
      encode|=BIN2DPD[tac[9]]<<26;
      DFWORD(result, 1)=encode;

      /* and final two declets */
      encode =BIN2DPD[tac[9]]>>6;
      encode|=BIN2DPD[tac[10]]<<4;
      #endif

      /* add exponent continuation and sign (from either argument) */
      encode|=sourhil & (ECONMASK | DECFLOAT_Sign);

      /* create lookup index = MSD + top two bits of biased exponent <<4 */
      tac[DECLETS]|=(bexpl>>DECECONL)<<4;
      encode|=DECCOMBFROM[tac[DECLETS]]; /* add constructed combination field */
      DFWORD(result, 0)=encode; 	 /* complete */

      /* decFloatShow(result, ">"); */
      return result;
      } /* fast path OK */
    /* drop through to slow path */
    } /* low sum or Special(s) */

  /* Slow path required -- arguments are finite and might overflow,   */
  /* or require alignment, or might have different signs	      */

  /* now swap either exponents or argument pointers */
  if (bexpl<=bexpr) {
    /* original left is bigger */
    Int bexpswap=bexpl;
    bexpl=bexpr;
    bexpr=bexpswap;
    /* printf("left bigger\n"); */
    }
   else {
    const decFloat *dfswap=dfl;
    dfl=dfr;
    dfr=dfswap;
    /* printf("right bigger\n"); */
    }
  /* [here dfl and bexpl refer to the datum with the larger exponent, */
  /* of if the exponents are equal then the original LHS argument] */

  /* if lhs is zero then result will be the rhs (now known to have */
  /* the smaller exponent), which also may need to be tested for zero */
  /* for the weird IEEE 754 sign rules */
  if (DFISZERO(dfl)) {
    decCanonical(result, dfr);		     /* clean copy */
    /* "When the sum of two operands with opposite signs is */
    /* exactly zero, the sign of that sum shall be '+' in all */
    /* rounding modes except round toward -Infinity, in which */
    /* mode that sign shall be '-'." */
    if (diffsign && DFISZERO(result)) {
      DFWORD(result, 0)&=~DECFLOAT_Sign;     /* assume sign 0 */
      if (set->round==DEC_ROUND_FLOOR) DFWORD(result, 0)|=DECFLOAT_Sign;
      }
    return result;
    } /* numfl is zero */
  /* [here, LHS is non-zero; code below assumes that] */

  /* Coefficients layout during the calculations to follow: */
  /* */
  /*	   Overlap case: */
  /*	   +------------------------------------------------+ */
  /* acc:  |0000|      coeffa	   | tail B |		    | */
  /*	   +------------------------------------------------+ */
  /* buf:  |0000| pad0s |      coeffb	    |		    | */
  /*	   +------------------------------------------------+ */
  /* */
  /*	   Touching coefficients or gap: */
  /*	   +------------------------------------------------+ */
  /* acc:  |0000|      coeffa	   | gap |	coeffb	    | */
  /*	   +------------------------------------------------+ */
  /*	   [buf not used or needed; gap clamped to Pmax] */

  /* lay out lhs coefficient into accumulator; this starts at acc+4 */
  /* for decDouble or acc+6 for decQuad so the LSD is word- */
  /* aligned; the top word gap is there only in case a carry digit */
  /* is prefixed after the add -- it does not need to be zeroed */
  #if DOUBLE
    #define COFF 4			/* offset into acc */
  #elif QUAD
    UBFROMUS(acc+4, 0); 		/* prefix 00 */
    #define COFF 6			/* offset into acc */
  #endif

  GETCOEFF(dfl, acc+COFF);		/* decode from decFloat */
  ulsd=acc+COFF+DECPMAX-1;
  umsd=acc+4;				/* [having this here avoids */

  #if DECTRACE
  {bcdnum tum;
  tum.msd=umsd;
  tum.lsd=ulsd;
  tum.exponent=bexpl-DECBIAS;
  tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
  decShowNum(&tum, "dflx");}
  #endif

  /* if signs differ, take ten's complement of lhs (here the */
  /* coefficient is subtracted from all-nines; the 1 is added during */
  /* the later add cycle -- zeros to the right do not matter because */
  /* the complement of zero is zero); these are fixed-length inverts */
  /* where the lsd is known to be at a 4-byte boundary (so no borrow */
  /* possible) */
  carry=0;				/* assume no carry */
  if (diffsign) {
    carry=CARRYPAT;			/* for +1 during add */
    UBFROMUI(acc+ 4, 0x09090909-UBTOUI(acc+ 4));
    UBFROMUI(acc+ 8, 0x09090909-UBTOUI(acc+ 8));
    UBFROMUI(acc+12, 0x09090909-UBTOUI(acc+12));
    UBFROMUI(acc+16, 0x09090909-UBTOUI(acc+16));
    #if QUAD
    UBFROMUI(acc+20, 0x09090909-UBTOUI(acc+20));
    UBFROMUI(acc+24, 0x09090909-UBTOUI(acc+24));
    UBFROMUI(acc+28, 0x09090909-UBTOUI(acc+28));
    UBFROMUI(acc+32, 0x09090909-UBTOUI(acc+32));
    UBFROMUI(acc+36, 0x09090909-UBTOUI(acc+36));
    #endif
    } /* diffsign */

  /* now process the rhs coefficient; if it cannot overlap lhs then */
  /* it can be put straight into acc (with an appropriate gap, if */
  /* needed) because no actual addition will be needed (except */
  /* possibly to complete ten's complement) */
  overlap=DECPMAX-(bexpl-bexpr);
  #if DECTRACE
  printf("exps: %ld %ld\n", (LI)(bexpl-DECBIAS), (LI)(bexpr-DECBIAS));
  printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry);
  #endif

  if (overlap<=0) {			/* no overlap possible */
    uInt gap;				/* local work */
    /* since a full addition is not needed, a ten's complement */
    /* calculation started above may need to be completed */
    if (carry) {
      for (ub=ulsd; *ub==9; ub--) *ub=0;
      *ub+=1;
      carry=0;				/* taken care of */
      }
    /* up to DECPMAX-1 digits of the final result can extend down */
    /* below the LSD of the lhs, so if the gap is >DECPMAX then the */
    /* rhs will be simply sticky bits.	In this case the gap is */
    /* clamped to DECPMAX and the exponent adjusted to suit [this is */
    /* safe because the lhs is non-zero]. */
    gap=-overlap;
    if (gap>DECPMAX) {
      bexpr+=gap-1;
      gap=DECPMAX;
      }
    ub=ulsd+gap+1;			/* where MSD will go */
    /* Fill the gap with 0s; note that there is no addition to do */
    ut=acc+COFF+DECPMAX;		/* start of gap */
    for (; ut<ub; ut+=4) UBFROMUI(ut, 0); /* mind the gap */
    if (overlap<-DECPMAX) {		/* gap was > DECPMAX */
      *ub=(uByte)(!DFISZERO(dfr));	/* make sticky digit */
      }
     else {				/* need full coefficient */
      GETCOEFF(dfr, ub);		/* decode from decFloat */
      ub+=DECPMAX-1;			/* new LSD... */
      }
    ulsd=ub;				/* save new LSD */
    } /* no overlap possible */

   else {				/* overlap>0 */
    /* coefficients overlap (perhaps completely, although also */
    /* perhaps only where zeros) */
    if (overlap==DECPMAX) {		/* aligned */
      ub=buf+COFF;			/* where msd will go */
      #if QUAD
      UBFROMUS(buf+4, 0);		/* clear quad's 00 */
      #endif
      GETCOEFF(dfr, ub);		/* decode from decFloat */
      }
     else {				/* unaligned */
      ub=buf+COFF+DECPMAX-overlap;	/* where MSD will go */
      /* Fill the prefix gap with 0s; 8 will cover most common */
      /* unalignments, so start with direct assignments (a loop is */
      /* then used for any remaining -- the loop (and the one in a */
      /* moment) is not then on the critical path because the number */
      /* of additions is reduced by (at least) two in this case) */
      UBFROMUI(buf+4, 0);		/* [clears decQuad 00 too] */
      UBFROMUI(buf+8, 0);
      if (ub>buf+12) {
	ut=buf+12;			/* start any remaining */
	for (; ut<ub; ut+=4) UBFROMUI(ut, 0); /* fill them */
	}
      GETCOEFF(dfr, ub);		/* decode from decFloat */

      /* now move tail of rhs across to main acc; again use direct */
      /* copies for 8 digits-worth */
      UBFROMUI(acc+COFF+DECPMAX,   UBTOUI(buf+COFF+DECPMAX));
      UBFROMUI(acc+COFF+DECPMAX+4, UBTOUI(buf+COFF+DECPMAX+4));
      if (buf+COFF+DECPMAX+8<ub+DECPMAX) {
	us=buf+COFF+DECPMAX+8;		/* source */
	ut=acc+COFF+DECPMAX+8;		/* target */
	for (; us<ub+DECPMAX; us+=4, ut+=4) UBFROMUI(ut, UBTOUI(us));
	}
      } /* unaligned */

    ulsd=acc+(ub-buf+DECPMAX-1);	/* update LSD pointer */

    /* Now do the add of the non-tail; this is all nicely aligned, */
    /* and is over a multiple of four digits (because for Quad two */
    /* zero digits were added on the left); words in both acc and */
    /* buf (buf especially) will often be zero */
    /* [byte-by-byte add, here, is about 15% slower total effect than */
    /* the by-fours] */

    /* Now effect the add; this is harder on a little-endian */
    /* machine as the inter-digit carry cannot use the usual BCD */
    /* addition trick because the bytes are loaded in the wrong order */
    /* [this loop could be unrolled, but probably scarcely worth it] */

    ut=acc+COFF+DECPMAX-4;		/* target LSW (acc) */
    us=buf+COFF+DECPMAX-4;		/* source LSW (buf, to add to acc) */

    #if !DECLITEND
    for (; ut>=acc+4; ut-=4, us-=4) {	/* big-endian add loop */
      /* bcd8 add */
      carry+=UBTOUI(us);		/* rhs + carry */
      if (carry==0) continue;		/* no-op */
      carry+=UBTOUI(ut);		/* lhs */
      /* Big-endian BCD adjust (uses internal carry) */
      carry+=0x76f6f6f6;		/* note top nibble not all bits */
      /* apply BCD adjust and save */
      UBFROMUI(ut, (carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4));
      carry>>=31;			/* true carry was at far left */
      } /* add loop */
    #else
    for (; ut>=acc+4; ut-=4, us-=4) {	/* little-endian add loop */
      /* bcd8 add */
      carry+=UBTOUI(us);		/* rhs + carry */
      if (carry==0) continue;		/* no-op [common if unaligned] */
      carry+=UBTOUI(ut);		/* lhs */
      /* Little-endian BCD adjust; inter-digit carry must be manual */
      /* because the lsb from the array will be in the most-significant */
      /* byte of carry */
      carry+=0x76767676;		/* note no inter-byte carries */
      carry+=(carry & 0x80000000)>>15;
      carry+=(carry & 0x00800000)>>15;
      carry+=(carry & 0x00008000)>>15;
      carry-=(carry & 0x60606060)>>4;	/* BCD adjust back */
      UBFROMUI(ut, carry & 0x0f0f0f0f); /* clear debris and save */
      /* here, final carry-out bit is at 0x00000080; move it ready */
      /* for next word-add (i.e., to 0x01000000) */
      carry=(carry & 0x00000080)<<17;
      } /* add loop */
    #endif

    #if DECTRACE
    {bcdnum tum;
    printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign);
    tum.msd=umsd;  /* acc+4; */
    tum.lsd=ulsd;
    tum.exponent=0;
    tum.sign=0;
    decShowNum(&tum, "dfadd");}
    #endif
    } /* overlap possible */

  /* ordering here is a little strange in order to have slowest path */
  /* first in GCC asm listing */
  if (diffsign) {		   /* subtraction */
    if (!carry) {		   /* no carry out means RHS<LHS */
      /* borrowed -- take ten's complement */
      /* sign is lhs sign */
      num.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;

      /* invert the coefficient first by fours, then add one; space */
      /* at the end of the buffer ensures the by-fours is always */
      /* safe, but lsd+1 must be cleared to prevent a borrow */
      /* if big-endian */
      #if !DECLITEND
      *(ulsd+1)=0;
      #endif
      /* there are always at least four coefficient words */
      UBFROMUI(umsd,	0x09090909-UBTOUI(umsd));
      UBFROMUI(umsd+4,	0x09090909-UBTOUI(umsd+4));
      UBFROMUI(umsd+8,	0x09090909-UBTOUI(umsd+8));
      UBFROMUI(umsd+12, 0x09090909-UBTOUI(umsd+12));
      #if DOUBLE
	#define BNEXT 16
      #elif QUAD
	UBFROMUI(umsd+16, 0x09090909-UBTOUI(umsd+16));
	UBFROMUI(umsd+20, 0x09090909-UBTOUI(umsd+20));
	UBFROMUI(umsd+24, 0x09090909-UBTOUI(umsd+24));
	UBFROMUI(umsd+28, 0x09090909-UBTOUI(umsd+28));
	UBFROMUI(umsd+32, 0x09090909-UBTOUI(umsd+32));
	#define BNEXT 36
      #endif
      if (ulsd>=umsd+BNEXT) {		/* unaligned */
	/* eight will handle most unaligments for Double; 16 for Quad */
	UBFROMUI(umsd+BNEXT,   0x09090909-UBTOUI(umsd+BNEXT));
	UBFROMUI(umsd+BNEXT+4, 0x09090909-UBTOUI(umsd+BNEXT+4));
	#if DOUBLE
	#define BNEXTY (BNEXT+8)
	#elif QUAD
	UBFROMUI(umsd+BNEXT+8,	0x09090909-UBTOUI(umsd+BNEXT+8));
	UBFROMUI(umsd+BNEXT+12, 0x09090909-UBTOUI(umsd+BNEXT+12));
	#define BNEXTY (BNEXT+16)
	#endif
	if (ulsd>=umsd+BNEXTY) {	/* very unaligned */
	  ut=umsd+BNEXTY;		/* -> continue */
	  for (;;ut+=4) {
	    UBFROMUI(ut, 0x09090909-UBTOUI(ut)); /* invert four digits */
	    if (ut>=ulsd-3) break;	/* all done */
	    }
	  }
	}
      /* complete the ten's complement by adding 1 */
      for (ub=ulsd; *ub==9; ub--) *ub=0;
      *ub+=1;
      } /* borrowed */

     else {			   /* carry out means RHS>=LHS */
      num.sign=DFWORD(dfr, 0) & DECFLOAT_Sign;
      /* all done except for the special IEEE 754 exact-zero-result */
      /* rule (see above); while testing for zero, strip leading */
      /* zeros (which will save decFinalize doing it) (this is in */
      /* diffsign path, so carry impossible and true umsd is */
      /* acc+COFF) */

      /* Check the initial coefficient area using the fast macro; */
      /* this will often be all that needs to be done (as on the */
      /* worst-case path when the subtraction was aligned and */
      /* full-length) */
      if (ISCOEFFZERO(acc+COFF)) {
	umsd=acc+COFF+DECPMAX-1;   /* so far, so zero */
	if (ulsd>umsd) {	   /* more to check */
	  umsd++;		   /* to align after checked area */
	  for (; UBTOUI(umsd)==0 && umsd+3<ulsd;) umsd+=4;
	  for (; *umsd==0 && umsd<ulsd;) umsd++;
	  }
	if (*umsd==0) { 	   /* must be true zero (and diffsign) */
	  num.sign=0;		   /* assume + */
	  if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign;
	  }
	}
      /* [else was not zero, might still have leading zeros] */
      } /* subtraction gave positive result */
    } /* diffsign */

   else { /* same-sign addition */
    num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
    #if DOUBLE
    if (carry) {		   /* only possible with decDouble */
      *(acc+3)=1;		   /* [Quad has leading 00] */
      umsd=acc+3;
      }
    #endif
    } /* same sign */

  num.msd=umsd; 		   /* set MSD .. */
  num.lsd=ulsd; 		   /* .. and LSD */
  num.exponent=bexpr-DECBIAS;	   /* set exponent to smaller, unbiassed */

  #if DECTRACE
  decFloatShow(dfl, "dfl");
  decFloatShow(dfr, "dfr");
  decShowNum(&num, "postadd");
  #endif
  return decFinalize(result, &num, set); /* round, check, and lay out */
  } /* decFloatAdd */

/* ------------------------------------------------------------------ */
/* decFloatAnd -- logical digitwise AND of two decFloats	      */
/*								      */
/*   result gets the result of ANDing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result, which will be canonical with sign=0	      */
/*								      */
/* The operands must be positive, finite with exponent q=0, and       */
/* comprise just zeros and ones; if not, Invalid operation results.   */
/* ------------------------------------------------------------------ */
decFloat * decFloatAnd(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
   || !DFISCC01(dfl)   || !DFISCC01(dfr)) return decInvalid(result, set);
  /* the operands are positive finite integers (q=0) with just 0s and 1s */
  #if DOUBLE
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04009124);
   DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x49124491;
  #elif QUAD
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04000912);
   DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x44912449;
   DFWORD(result, 2)=(DFWORD(dfl, 2) & DFWORD(dfr, 2))&0x12449124;
   DFWORD(result, 3)=(DFWORD(dfl, 3) & DFWORD(dfr, 3))&0x49124491;
  #endif
  return result;
  } /* decFloatAnd */

/* ------------------------------------------------------------------ */
/* decFloatCanonical -- copy a decFloat, making canonical	      */
/*								      */
/*   result gets the canonicalized df				      */
/*   df     is the decFloat to copy and make canonical		      */
/*   returns result						      */
/*								      */
/* This works on specials, too; no error or exception is possible.    */
/* ------------------------------------------------------------------ */
decFloat * decFloatCanonical(decFloat *result, const decFloat *df) {
  return decCanonical(result, df);
  } /* decFloatCanonical */

/* ------------------------------------------------------------------ */
/* decFloatClass -- return the class of a decFloat		      */
/*								      */
/*   df is the decFloat to test 				      */
/*   returns the decClass that df falls into			      */
/* ------------------------------------------------------------------ */
enum decClass decFloatClass(const decFloat *df) {
  Int exp;			   /* exponent */
  if (DFISSPECIAL(df)) {
    if (DFISQNAN(df)) return DEC_CLASS_QNAN;
    if (DFISSNAN(df)) return DEC_CLASS_SNAN;
    /* must be an infinity */
    if (DFISSIGNED(df)) return DEC_CLASS_NEG_INF;
    return DEC_CLASS_POS_INF;
    }
  if (DFISZERO(df)) {		   /* quite common */
    if (DFISSIGNED(df)) return DEC_CLASS_NEG_ZERO;
    return DEC_CLASS_POS_ZERO;
    }
  /* is finite and non-zero; similar code to decFloatIsNormal, here */
  /* [this could be speeded up slightly by in-lining decFloatDigits] */
  exp=GETEXPUN(df)		   /* get unbiased exponent .. */
     +decFloatDigits(df)-1;	   /* .. and make adjusted exponent */
  if (exp>=DECEMIN) {		   /* is normal */
    if (DFISSIGNED(df)) return DEC_CLASS_NEG_NORMAL;
    return DEC_CLASS_POS_NORMAL;
    }
  /* is subnormal */
  if (DFISSIGNED(df)) return DEC_CLASS_NEG_SUBNORMAL;
  return DEC_CLASS_POS_SUBNORMAL;
  } /* decFloatClass */

/* ------------------------------------------------------------------ */
/* decFloatClassString -- return the class of a decFloat as a string  */
/*								      */
/*   df is the decFloat to test 				      */
/*   returns a constant string describing the class df falls into     */
/* ------------------------------------------------------------------ */
const char *decFloatClassString(const decFloat *df) {
  enum decClass eclass=decFloatClass(df);
  if (eclass==DEC_CLASS_POS_NORMAL)    return DEC_ClassString_PN;
  if (eclass==DEC_CLASS_NEG_NORMAL)    return DEC_ClassString_NN;
  if (eclass==DEC_CLASS_POS_ZERO)      return DEC_ClassString_PZ;
  if (eclass==DEC_CLASS_NEG_ZERO)      return DEC_ClassString_NZ;
  if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS;
  if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS;
  if (eclass==DEC_CLASS_POS_INF)       return DEC_ClassString_PI;
  if (eclass==DEC_CLASS_NEG_INF)       return DEC_ClassString_NI;
  if (eclass==DEC_CLASS_QNAN)	       return DEC_ClassString_QN;
  if (eclass==DEC_CLASS_SNAN)	       return DEC_ClassString_SN;
  return DEC_ClassString_UN;	       /* Unknown */
  } /* decFloatClassString */

/* ------------------------------------------------------------------ */
/* decFloatCompare -- compare two decFloats; quiet NaNs allowed       */
/*								      */
/*   result gets the result of comparing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result, which may be -1, 0, 1, or NaN (Unordered)	      */
/* ------------------------------------------------------------------ */
decFloat * decFloatCompare(decFloat *result,
			   const decFloat *dfl, const decFloat *dfr,
			   decContext *set) {
  Int comp;				     /* work */
  /* NaNs are handled as usual */
  if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  /* numeric comparison needed */
  comp=decNumCompare(dfl, dfr, 0);
  decFloatZero(result);
  if (comp==0) return result;
  DFBYTE(result, DECBYTES-1)=0x01;	/* LSD=1 */
  if (comp<0) DFBYTE(result, 0)|=0x80;	/* set sign bit */
  return result;
  } /* decFloatCompare */

/* ------------------------------------------------------------------ */
/* decFloatCompareSignal -- compare two decFloats; all NaNs signal    */
/*								      */
/*   result gets the result of comparing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result, which may be -1, 0, 1, or NaN (Unordered)	      */
/* ------------------------------------------------------------------ */
decFloat * decFloatCompareSignal(decFloat *result,
				 const decFloat *dfl, const decFloat *dfr,
				 decContext *set) {
  Int comp;				     /* work */
  /* NaNs are handled as usual, except that all NaNs signal */
  if (DFISNAN(dfl) || DFISNAN(dfr)) {
    set->status|=DEC_Invalid_operation;
    return decNaNs(result, dfl, dfr, set);
    }
  /* numeric comparison needed */
  comp=decNumCompare(dfl, dfr, 0);
  decFloatZero(result);
  if (comp==0) return result;
  DFBYTE(result, DECBYTES-1)=0x01;	/* LSD=1 */
  if (comp<0) DFBYTE(result, 0)|=0x80;	/* set sign bit */
  return result;
  } /* decFloatCompareSignal */

/* ------------------------------------------------------------------ */
/* decFloatCompareTotal -- compare two decFloats with total ordering  */
/*								      */
/*   result gets the result of comparing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   returns result, which may be -1, 0, or 1			      */
/* ------------------------------------------------------------------ */
decFloat * decFloatCompareTotal(decFloat *result,
				const decFloat *dfl, const decFloat *dfr) {
  Int  comp;				     /* work */
  uInt uiwork;				     /* for macros */
  #if QUAD
  uShort uswork;			     /* .. */
  #endif
  if (DFISNAN(dfl) || DFISNAN(dfr)) {
    Int nanl, nanr;			     /* work */
    /* morph NaNs to +/- 1 or 2, leave numbers as 0 */
    nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2;      /* quiet > signalling */
    if (DFISSIGNED(dfl)) nanl=-nanl;
    nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2;
    if (DFISSIGNED(dfr)) nanr=-nanr;
    if (nanl>nanr) comp=+1;
     else if (nanl<nanr) comp=-1;
     else { /* NaNs are the same type and sign .. must compare payload */
      /* buffers need +2 for QUAD */
      uByte bufl[DECPMAX+4];		     /* for LHS coefficient + foot */
      uByte bufr[DECPMAX+4];		     /* for RHS coefficient + foot */
      uByte *ub, *uc;			     /* work */
      Int sigl; 			     /* signum of LHS */
      sigl=(DFISSIGNED(dfl) ? -1 : +1);

      /* decode the coefficients */
      /* (shift both right two if Quad to make a multiple of four) */
      #if QUAD
	UBFROMUS(bufl, 0);
	UBFROMUS(bufr, 0);
      #endif
      GETCOEFF(dfl, bufl+QUAD*2);	     /* decode from decFloat */
      GETCOEFF(dfr, bufr+QUAD*2);	     /* .. */
      /* all multiples of four, here */
      comp=0;				     /* assume equal */
      for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
	uInt ui=UBTOUI(ub);
	if (ui==UBTOUI(uc)) continue; /* so far so same */
	/* about to find a winner; go by bytes in case little-endian */
	for (;; ub++, uc++) {
	  if (*ub==*uc) continue;
	  if (*ub>*uc) comp=sigl;	     /* difference found */
	   else comp=-sigl;		     /* .. */
	   break;
	  }
	}
      } /* same NaN type and sign */
    }
   else {
    /* numeric comparison needed */
    comp=decNumCompare(dfl, dfr, 1);	/* total ordering */
    }
  decFloatZero(result);
  if (comp==0) return result;
  DFBYTE(result, DECBYTES-1)=0x01;	/* LSD=1 */
  if (comp<0) DFBYTE(result, 0)|=0x80;	/* set sign bit */
  return result;
  } /* decFloatCompareTotal */

/* ------------------------------------------------------------------ */
/* decFloatCompareTotalMag -- compare magnitudes with total ordering  */
/*								      */
/*   result gets the result of comparing abs(dfl) and abs(dfr)	      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   returns result, which may be -1, 0, or 1			      */
/* ------------------------------------------------------------------ */
decFloat * decFloatCompareTotalMag(decFloat *result,
				const decFloat *dfl, const decFloat *dfr) {
  decFloat a, b;			/* for copy if needed */
  /* copy and redirect signed operand(s) */
  if (DFISSIGNED(dfl)) {
    decFloatCopyAbs(&a, dfl);
    dfl=&a;
    }
  if (DFISSIGNED(dfr)) {
    decFloatCopyAbs(&b, dfr);
    dfr=&b;
    }
  return decFloatCompareTotal(result, dfl, dfr);
  } /* decFloatCompareTotalMag */

/* ------------------------------------------------------------------ */
/* decFloatCopy -- copy a decFloat as-is			      */
/*								      */
/*   result gets the copy of dfl				      */
/*   dfl    is the decFloat to copy				      */
/*   returns result						      */
/*								      */
/* This is a bitwise operation; no errors or exceptions are possible. */
/* ------------------------------------------------------------------ */
decFloat * decFloatCopy(decFloat *result, const decFloat *dfl) {
  if (dfl!=result) *result=*dfl;	     /* copy needed */
  return result;
  } /* decFloatCopy */

/* ------------------------------------------------------------------ */
/* decFloatCopyAbs -- copy a decFloat as-is and set sign bit to 0     */
/*								      */
/*   result gets the copy of dfl with sign bit 0		      */
/*   dfl    is the decFloat to copy				      */
/*   returns result						      */
/*								      */
/* This is a bitwise operation; no errors or exceptions are possible. */
/* ------------------------------------------------------------------ */
decFloat * decFloatCopyAbs(decFloat *result, const decFloat *dfl) {
  if (dfl!=result) *result=*dfl;	/* copy needed */
  DFBYTE(result, 0)&=~0x80;		/* zero sign bit */
  return result;
  } /* decFloatCopyAbs */

/* ------------------------------------------------------------------ */
/* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */
/*								      */
/*   result gets the copy of dfl with sign bit inverted 	      */
/*   dfl    is the decFloat to copy				      */
/*   returns result						      */
/*								      */
/* This is a bitwise operation; no errors or exceptions are possible. */
/* ------------------------------------------------------------------ */
decFloat * decFloatCopyNegate(decFloat *result, const decFloat *dfl) {
  if (dfl!=result) *result=*dfl;	/* copy needed */
  DFBYTE(result, 0)^=0x80;		/* invert sign bit */
  return result;
  } /* decFloatCopyNegate */

/* ------------------------------------------------------------------ */
/* decFloatCopySign -- copy a decFloat with the sign of another       */
/*								      */
/*   result gets the result of copying dfl with the sign of dfr       */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   returns result						      */
/*								      */
/* This is a bitwise operation; no errors or exceptions are possible. */
/* ------------------------------------------------------------------ */
decFloat * decFloatCopySign(decFloat *result,
			    const decFloat *dfl, const decFloat *dfr) {
  uByte sign=(uByte)(DFBYTE(dfr, 0)&0x80);   /* save sign bit */
  if (dfl!=result) *result=*dfl;	     /* copy needed */
  DFBYTE(result, 0)&=~0x80;		     /* clear sign .. */
  DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* .. and set saved */
  return result;
  } /* decFloatCopySign */

/* ------------------------------------------------------------------ */
/* decFloatDigits -- return the number of digits in a decFloat	      */
/*								      */
/*   df is the decFloat to investigate				      */
/*   returns the number of significant digits in the decFloat; a      */
/*     zero coefficient returns 1 as does an infinity (a NaN returns  */
/*     the number of digits in the payload)			      */
/* ------------------------------------------------------------------ */
/* private macro to extract a declet according to provided formula */
/* (form), and if it is non-zero then return the calculated digits */
/* depending on the declet number (n), where n=0 for the most */
/* significant declet; uses uInt dpd for work */
#define dpdlenchk(n, form) {dpd=(form)&0x3ff;	  \
  if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);}
/* next one is used when it is known that the declet must be */
/* non-zero, or is the final zero declet */
#define dpdlendun(n, form) {dpd=(form)&0x3ff;	  \
  if (dpd==0) return 1; 			  \
  return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);}

uInt decFloatDigits(const decFloat *df) {
  uInt dpd;			   /* work */
  uInt sourhi=DFWORD(df, 0);	   /* top word from source decFloat */
  #if QUAD
  uInt sourmh, sourml;
  #endif
  uInt sourlo;

  if (DFISINF(df)) return 1;
  /* A NaN effectively has an MSD of 0; otherwise if non-zero MSD */
  /* then the coefficient is full-length */
  if (!DFISNAN(df) && DECCOMBMSD[sourhi>>26]) return DECPMAX;

  #if DOUBLE
    if (sourhi&0x0003ffff) {	 /* ends in first */
      dpdlenchk(0, sourhi>>8);
      sourlo=DFWORD(df, 1);
      dpdlendun(1, (sourhi<<2) | (sourlo>>30));
      } /* [cannot drop through] */
    sourlo=DFWORD(df, 1);  /* sourhi not involved now */
    if (sourlo&0xfff00000) {	 /* in one of first two */
      dpdlenchk(1, sourlo>>30);  /* very rare */
      dpdlendun(2, sourlo>>20);
      } /* [cannot drop through] */
    dpdlenchk(3, sourlo>>10);
    dpdlendun(4, sourlo);
    /* [cannot drop through] */

  #elif QUAD
    if (sourhi&0x00003fff) {	 /* ends in first */
      dpdlenchk(0, sourhi>>4);
      sourmh=DFWORD(df, 1);
      dpdlendun(1, ((sourhi)<<6) | (sourmh>>26));
      } /* [cannot drop through] */
    sourmh=DFWORD(df, 1);
    if (sourmh) {
      dpdlenchk(1, sourmh>>26);
      dpdlenchk(2, sourmh>>16);
      dpdlenchk(3, sourmh>>6);
      sourml=DFWORD(df, 2);
      dpdlendun(4, ((sourmh)<<4) | (sourml>>28));
      } /* [cannot drop through] */
    sourml=DFWORD(df, 2);
    if (sourml) {
      dpdlenchk(4, sourml>>28);
      dpdlenchk(5, sourml>>18);
      dpdlenchk(6, sourml>>8);
      sourlo=DFWORD(df, 3);
      dpdlendun(7, ((sourml)<<2) | (sourlo>>30));
      } /* [cannot drop through] */
    sourlo=DFWORD(df, 3);
    if (sourlo&0xfff00000) {	 /* in one of first two */
      dpdlenchk(7, sourlo>>30);  /* very rare */
      dpdlendun(8, sourlo>>20);
      } /* [cannot drop through] */
    dpdlenchk(9, sourlo>>10);
    dpdlendun(10, sourlo);
    /* [cannot drop through] */
  #endif
  } /* decFloatDigits */

/* ------------------------------------------------------------------ */
/* decFloatDivide -- divide a decFloat by another		      */
/*								      */
/*   result gets the result of dividing dfl by dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
/* This is just a wrapper. */
decFloat * decFloatDivide(decFloat *result,
			  const decFloat *dfl, const decFloat *dfr,
			  decContext *set) {
  return decDivide(result, dfl, dfr, set, DIVIDE);
  } /* decFloatDivide */

/* ------------------------------------------------------------------ */
/* decFloatDivideInteger -- integer divide a decFloat by another      */
/*								      */
/*   result gets the result of dividing dfl by dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
decFloat * decFloatDivideInteger(decFloat *result,
			     const decFloat *dfl, const decFloat *dfr,
			     decContext *set) {
  return decDivide(result, dfl, dfr, set, DIVIDEINT);
  } /* decFloatDivideInteger */

/* ------------------------------------------------------------------ */
/* decFloatFMA -- multiply and add three decFloats, fused	      */
/*								      */
/*   result gets the result of (dfl*dfr)+dff with a single rounding   */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   dff    is the final decFloat (fhs) 			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
decFloat * decFloatFMA(decFloat *result, const decFloat *dfl,
		       const decFloat *dfr, const decFloat *dff,
		       decContext *set) {

  /* The accumulator has the bytes needed for FiniteMultiply, plus */
  /* one byte to the left in case of carry, plus DECPMAX+2 to the */
  /* right for the final addition (up to full fhs + round & sticky) */
  #define FMALEN (ROUNDUP4(1+ (DECPMAX9*18+1) +DECPMAX+2))
  uByte  acc[FMALEN];		   /* for multiplied coefficient in BCD */
				   /* .. and for final result */
  bcdnum mul;			   /* for multiplication result */
  bcdnum fin;			   /* for final operand, expanded */
  uByte  coe[ROUNDUP4(DECPMAX)];   /* dff coefficient in BCD */
  bcdnum *hi, *lo;		   /* bcdnum with higher/lower exponent */
  uInt	 diffsign;		   /* non-zero if signs differ */
  uInt	 hipad; 		   /* pad digit for hi if needed */
  Int	 padding;		   /* excess exponent */
  uInt	 carry; 		   /* +1 for ten's complement and during add */
  uByte  *ub, *uh, *ul; 	   /* work */
  uInt	 uiwork;		   /* for macros */

  /* handle all the special values [any special operand leads to a */
  /* special result] */
  if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) {
    decFloat proxy;		   /* multiplication result proxy */
    /* NaNs are handled as usual, giving priority to sNaNs */
    if (DFISSNAN(dfl) || DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    if (DFISSNAN(dff)) return decNaNs(result, dff, NULL, set);
    if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    if (DFISNAN(dff)) return decNaNs(result, dff, NULL, set);
    /* One or more of the three is infinite */
    /* infinity times zero is bad */
    decFloatZero(&proxy);
    if (DFISINF(dfl)) {
      if (DFISZERO(dfr)) return decInvalid(result, set);
      decInfinity(&proxy, &proxy);
      }
     else if (DFISINF(dfr)) {
      if (DFISZERO(dfl)) return decInvalid(result, set);
      decInfinity(&proxy, &proxy);
      }
    /* compute sign of multiplication and place in proxy */
    DFWORD(&proxy, 0)|=(DFWORD(dfl, 0)^DFWORD(dfr, 0))&DECFLOAT_Sign;
    if (!DFISINF(dff)) return decFloatCopy(result, &proxy);
    /* dff is Infinite */
    if (!DFISINF(&proxy)) return decInfinity(result, dff);
    /* both sides of addition are infinite; different sign is bad */
    if ((DFWORD(dff, 0)&DECFLOAT_Sign)!=(DFWORD(&proxy, 0)&DECFLOAT_Sign))
      return decInvalid(result, set);
    return decFloatCopy(result, &proxy);
    }

  /* Here when all operands are finite */

  /* First multiply dfl*dfr */
  decFiniteMultiply(&mul, acc+1, dfl, dfr);
  /* The multiply is complete, exact and unbounded, and described in */
  /* mul with the coefficient held in acc[1...] */

  /* now add in dff; the algorithm is essentially the same as */
  /* decFloatAdd, but the code is different because the code there */
  /* is highly optimized for adding two numbers of the same size */
  fin.exponent=GETEXPUN(dff);		/* get dff exponent and sign */
  fin.sign=DFWORD(dff, 0)&DECFLOAT_Sign;
  diffsign=mul.sign^fin.sign;		/* note if signs differ */
  fin.msd=coe;
  fin.lsd=coe+DECPMAX-1;
  GETCOEFF(dff, coe);			/* extract the coefficient */

  /* now set hi and lo so that hi points to whichever of mul and fin */
  /* has the higher exponent and lo points to the other [don't care, */
  /* if the same].  One coefficient will be in acc, the other in coe. */
  if (mul.exponent>=fin.exponent) {
    hi=&mul;
    lo=&fin;
    }
   else {
    hi=&fin;
    lo=&mul;
    }

  /* remove leading zeros on both operands; this will save time later */
  /* and make testing for zero trivial (tests are safe because acc */
  /* and coe are rounded up to uInts) */
  for (; UBTOUI(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4;
  for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++;
  for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
  for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;

  /* if hi is zero then result will be lo (which has the smaller */
  /* exponent), which also may need to be tested for zero for the */
  /* weird IEEE 754 sign rules */
  if (*hi->msd==0) {			     /* hi is zero */
    /* "When the sum of two operands with opposite signs is */
    /* exactly zero, the sign of that sum shall be '+' in all */
    /* rounding modes except round toward -Infinity, in which */
    /* mode that sign shall be '-'." */
    if (diffsign) {
      if (*lo->msd==0) {		     /* lo is zero */
	lo->sign=0;
	if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
	} /* diffsign && lo=0 */
      } /* diffsign */
    return decFinalize(result, lo, set);     /* may need clamping */
    } /* numfl is zero */
  /* [here, both are minimal length and hi is non-zero] */
  /* (if lo is zero then padding with zeros may be needed, below) */

  /* if signs differ, take the ten's complement of hi (zeros to the */
  /* right do not matter because the complement of zero is zero); the */
  /* +1 is done later, as part of the addition, inserted at the */
  /* correct digit */
  hipad=0;
  carry=0;
  if (diffsign) {
    hipad=9;
    carry=1;
    /* exactly the correct number of digits must be inverted */
    for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UBFROMUI(uh, 0x09090909-UBTOUI(uh));
    for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh);
    }

  /* ready to add; note that hi has no leading zeros so gap */
  /* calculation does not have to be as pessimistic as in decFloatAdd */
  /* (this is much more like the arbitrary-precision algorithm in */
  /* Rexx and decNumber) */

  /* padding is the number of zeros that would need to be added to hi */
  /* for its lsd to be aligned with the lsd of lo */
  padding=hi->exponent-lo->exponent;
  /* printf("FMA pad %ld\n", (LI)padding); */

  /* the result of the addition will be built into the accumulator, */
  /* starting from the far right; this could be either hi or lo, and */
  /* will be aligned */
  ub=acc+FMALEN-1;		   /* where lsd of result will go */
  ul=lo->lsd;			   /* lsd of rhs */

  if (padding!=0) {		   /* unaligned */
    /* if the msd of lo is more than DECPMAX+2 digits to the right of */
    /* the original msd of hi then it can be reduced to a single */
    /* digit at the right place, as it stays clear of hi digits */
    /* [it must be DECPMAX+2 because during a subtraction the msd */
    /* could become 0 after a borrow from 1.000 to 0.9999...] */

    Int hilen=(Int)(hi->lsd-hi->msd+1); /* length of hi */
    Int lolen=(Int)(lo->lsd-lo->msd+1); /* and of lo */

    if (hilen+padding-lolen > DECPMAX+2) {   /* can reduce lo to single */
      /* make sure it is virtually at least DECPMAX from hi->msd, at */
      /* least to right of hi->lsd (in case of destructive subtract), */
      /* and separated by at least two digits from either of those */
      /* (the tricky DOUBLE case is when hi is a 1 that will become a */
      /* 0.9999... by subtraction: */
      /*   hi:	 1				     E+16 */
      /*   lo:	  .................1000000000000000  E-16 */
      /* which for the addition pads to: */
      /*   hi:	 1000000000000000000		     E-16 */
      /*   lo:	  .................1000000000000000  E-16 */
      Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3;

      /* printf("FMA reduce: %ld\n", (LI)reduce); */
      lo->lsd=lo->msd;			     /* to single digit [maybe 0] */
      lo->exponent=newexp;		     /* new lowest exponent */
      padding=hi->exponent-lo->exponent;     /* recalculate */
      ul=lo->lsd;			     /* .. and repoint */
      }

    /* padding is still > 0, but will fit in acc (less leading carry slot) */
    #if DECCHECK
      if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding);
      if (hilen+padding+1>FMALEN)
	printf("FMA excess hilen+padding: %ld+%ld \n", (LI)hilen, (LI)padding);
      /* printf("FMA padding: %ld\n", (LI)padding); */
    #endif

    /* padding digits can now be set in the result; one or more of */
    /* these will come from lo; others will be zeros in the gap */
    for (; ul-3>=lo->msd && padding>3; padding-=4, ul-=4, ub-=4) {
      UBFROMUI(ub-3, UBTOUI(ul-3));	     /* [cannot overlap] */
      }
    for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul;
    for (;padding>0; padding--, ub--) *ub=0; /* mind the gap */
    }

  /* addition now complete to the right of the rightmost digit of hi */
  uh=hi->lsd;

  /* dow do the add from hi->lsd to the left */
  /* [bytewise, because either operand can run out at any time] */
  /* carry was set up depending on ten's complement above */
  /* first assume both operands have some digits */
  for (;; ub--) {
    if (uh<hi->msd || ul<lo->msd) break;
    *ub=(uByte)(carry+(*uh--)+(*ul--));
    carry=0;
    if (*ub<10) continue;
    *ub-=10;
    carry=1;
    } /* both loop */

  if (ul<lo->msd) {	      /* to left of lo */
    for (;; ub--) {
      if (uh<hi->msd) break;
      *ub=(uByte)(carry+(*uh--));  /* [+0] */
      carry=0;
      if (*ub<10) continue;
      *ub-=10;
      carry=1;
      } /* hi loop */
    }
   else {		      /* to left of hi */
    for (;; ub--) {
      if (ul<lo->msd) break;
      *ub=(uByte)(carry+hipad+(*ul--));
      carry=0;
      if (*ub<10) continue;
      *ub-=10;
      carry=1;
      } /* lo loop */
    }

  /* addition complete -- now handle carry, borrow, etc. */
  /* use lo to set up the num (its exponent is already correct, and */
  /* sign usually is) */
  lo->msd=ub+1;
  lo->lsd=acc+FMALEN-1;
  /* decShowNum(lo, "lo"); */
  if (!diffsign) {		   /* same-sign addition */
    if (carry) {		   /* carry out */
      *ub=1;			   /* place the 1 .. */
      lo->msd--;		   /* .. and update */
      }
    } /* same sign */
   else {			   /* signs differed (subtraction) */
    if (!carry) {		   /* no carry out means hi<lo */
      /* borrowed -- take ten's complement of the right digits */
      lo->sign=hi->sign;	   /* sign is lhs sign */
      for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UBFROMUI(ul, 0x09090909-UBTOUI(ul));
      for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); /* [leaves ul at lsd+1] */
      /* complete the ten's complement by adding 1 [cannot overrun] */
      for (ul--; *ul==9; ul--) *ul=0;
      *ul+=1;
      } /* borrowed */
     else {			   /* carry out means hi>=lo */
      /* sign to use is lo->sign */
      /* all done except for the special IEEE 754 exact-zero-result */
      /* rule (see above); while testing for zero, strip leading */
      /* zeros (which will save decFinalize doing it) */
      for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
      for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;
      if (*lo->msd==0) {	   /* must be true zero (and diffsign) */
	lo->sign=0;		   /* assume + */
	if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
	}
      /* [else was not zero, might still have leading zeros] */
      } /* subtraction gave positive result */
    } /* diffsign */

  #if DECCHECK
  /* assert no left underrun */
  if (lo->msd<acc) {
    printf("FMA underrun by %ld \n", (LI)(acc-lo->msd));
    }
  #endif

  return decFinalize(result, lo, set);	/* round, check, and lay out */
  } /* decFloatFMA */

/* ------------------------------------------------------------------ */
/* decFloatFromInt -- initialise a decFloat from an Int 	      */
/*								      */
/*   result gets the converted Int				      */
/*   n	    is the Int to convert				      */
/*   returns result						      */
/*								      */
/* The result is Exact; no errors or exceptions are possible.	      */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromInt32(decFloat *result, Int n) {
  uInt u=(uInt)n;			/* copy as bits */
  uInt encode;				/* work */
  DFWORD(result, 0)=ZEROWORD;		/* always */
  #if QUAD
    DFWORD(result, 1)=0;
    DFWORD(result, 2)=0;
  #endif
  if (n<0) {				/* handle -n with care */
    /* [This can be done without the test, but is then slightly slower] */
    u=(~u)+1;
    DFWORD(result, 0)|=DECFLOAT_Sign;
    }
  /* Since the maximum value of u now is 2**31, only the low word of */
  /* result is affected */
  encode=BIN2DPD[u%1000];
  u/=1000;
  encode|=BIN2DPD[u%1000]<<10;
  u/=1000;
  encode|=BIN2DPD[u%1000]<<20;
  u/=1000;				/* now 0, 1, or 2 */
  encode|=u<<30;
  DFWORD(result, DECWORDS-1)=encode;
  return result;
  } /* decFloatFromInt32 */

/* ------------------------------------------------------------------ */
/* decFloatFromUInt -- initialise a decFloat from a uInt	      */
/*								      */
/*   result gets the converted uInt				      */
/*   n	    is the uInt to convert				      */
/*   returns result						      */
/*								      */
/* The result is Exact; no errors or exceptions are possible.	      */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromUInt32(decFloat *result, uInt u) {
  uInt encode;				/* work */
  DFWORD(result, 0)=ZEROWORD;		/* always */
  #if QUAD
    DFWORD(result, 1)=0;
    DFWORD(result, 2)=0;
  #endif
  encode=BIN2DPD[u%1000];
  u/=1000;
  encode|=BIN2DPD[u%1000]<<10;
  u/=1000;
  encode|=BIN2DPD[u%1000]<<20;
  u/=1000;				/* now 0 -> 4 */
  encode|=u<<30;
  DFWORD(result, DECWORDS-1)=encode;
  DFWORD(result, DECWORDS-2)|=u>>2;	/* rarely non-zero */
  return result;
  } /* decFloatFromUInt32 */

/* ------------------------------------------------------------------ */
/* decFloatInvert -- logical digitwise INVERT of a decFloat	      */
/*								      */
/*   result gets the result of INVERTing df			      */
/*   df     is the decFloat to invert				      */
/*   set    is the context					      */
/*   returns result, which will be canonical with sign=0	      */
/*								      */
/* The operand must be positive, finite with exponent q=0, and	      */
/* comprise just zeros and ones; if not, Invalid operation results.   */
/* ------------------------------------------------------------------ */
decFloat * decFloatInvert(decFloat *result, const decFloat *df,
			  decContext *set) {
  uInt sourhi=DFWORD(df, 0);		/* top word of dfs */

  if (!DFISUINT01(df) || !DFISCC01(df)) return decInvalid(result, set);
  /* the operand is a finite integer (q=0) */
  #if DOUBLE
   DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04009124);
   DFWORD(result, 1)=(~DFWORD(df, 1))	&0x49124491;
  #elif QUAD
   DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04000912);
   DFWORD(result, 1)=(~DFWORD(df, 1))	&0x44912449;
   DFWORD(result, 2)=(~DFWORD(df, 2))	&0x12449124;
   DFWORD(result, 3)=(~DFWORD(df, 3))	&0x49124491;
  #endif
  return result;
  } /* decFloatInvert */

/* ------------------------------------------------------------------ */
/* decFloatIs -- decFloat tests (IsSigned, etc.)		      */
/*								      */
/*   df is the decFloat to test 				      */
/*   returns 0 or 1 in a uInt					      */
/*								      */
/* Many of these could be macros, but having them as real functions   */
/* is a little cleaner (and they can be referred to here by the       */
/* generic names)						      */
/* ------------------------------------------------------------------ */
uInt decFloatIsCanonical(const decFloat *df) {
  if (DFISSPECIAL(df)) {
    if (DFISINF(df)) {
      if (DFWORD(df, 0)&ECONMASK) return 0;  /* exponent continuation */
      if (!DFISCCZERO(df)) return 0;	     /* coefficient continuation */
      return 1;
      }
    /* is a NaN */
    if (DFWORD(df, 0)&ECONNANMASK) return 0; /* exponent continuation */
    if (DFISCCZERO(df)) return 1;	     /* coefficient continuation */
    /* drop through to check payload */
    }
  { /* declare block */
  #if DOUBLE
    uInt sourhi=DFWORD(df, 0);
    uInt sourlo=DFWORD(df, 1);
    if (CANONDPDOFF(sourhi, 8)
     && CANONDPDTWO(sourhi, sourlo, 30)
     && CANONDPDOFF(sourlo, 20)
     && CANONDPDOFF(sourlo, 10)
     && CANONDPDOFF(sourlo, 0)) return 1;
  #elif QUAD
    uInt sourhi=DFWORD(df, 0);
    uInt sourmh=DFWORD(df, 1);
    uInt sourml=DFWORD(df, 2);
    uInt sourlo=DFWORD(df, 3);
    if (CANONDPDOFF(sourhi, 4)
     && CANONDPDTWO(sourhi, sourmh, 26)
     && CANONDPDOFF(sourmh, 16)
     && CANONDPDOFF(sourmh, 6)
     && CANONDPDTWO(sourmh, sourml, 28)
     && CANONDPDOFF(sourml, 18)
     && CANONDPDOFF(sourml, 8)
     && CANONDPDTWO(sourml, sourlo, 30)
     && CANONDPDOFF(sourlo, 20)
     && CANONDPDOFF(sourlo, 10)
     && CANONDPDOFF(sourlo, 0)) return 1;
  #endif
  } /* block */
  return 0;    /* a declet is non-canonical */
  }

uInt decFloatIsFinite(const decFloat *df) {
  return !DFISSPECIAL(df);
  }
uInt decFloatIsInfinite(const decFloat *df) {
  return DFISINF(df);
  }
uInt decFloatIsInteger(const decFloat *df) {
  return DFISINT(df);
  }
uInt decFloatIsNaN(const decFloat *df) {
  return DFISNAN(df);
  }
uInt decFloatIsNormal(const decFloat *df) {
  Int exp;			   /* exponent */
  if (DFISSPECIAL(df)) return 0;
  if (DFISZERO(df)) return 0;
  /* is finite and non-zero */
  exp=GETEXPUN(df)		   /* get unbiased exponent .. */
     +decFloatDigits(df)-1;	   /* .. and make adjusted exponent */
  return (exp>=DECEMIN);	   /* < DECEMIN is subnormal */
  }
uInt decFloatIsSignaling(const decFloat *df) {
  return DFISSNAN(df);
  }
uInt decFloatIsSignalling(const decFloat *df) {
  return DFISSNAN(df);
  }
uInt decFloatIsSigned(const decFloat *df) {
  return DFISSIGNED(df);
  }
uInt decFloatIsSubnormal(const decFloat *df) {
  if (DFISSPECIAL(df)) return 0;
  /* is finite */
  if (decFloatIsNormal(df)) return 0;
  /* it is <Nmin, but could be zero */
  if (DFISZERO(df)) return 0;
  return 1;				     /* is subnormal */
  }
uInt decFloatIsZero(const decFloat *df) {
  return DFISZERO(df);
  } /* decFloatIs... */

/* ------------------------------------------------------------------ */
/* decFloatLogB -- return adjusted exponent, by 754 rules	      */
/*								      */
/*   result gets the adjusted exponent as an integer, or a NaN etc.   */
/*   df     is the decFloat to be examined			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* Notable cases:						      */
/*   A<0 -> Use |A|						      */
/*   A=0 -> -Infinity (Division by zero)			      */
/*   A=Infinite -> +Infinity (Exact)				      */
/*   A=1 exactly -> 0 (Exact)					      */
/*   NaNs are propagated as usual				      */
/* ------------------------------------------------------------------ */
decFloat * decFloatLogB(decFloat *result, const decFloat *df,
			decContext *set) {
  Int ae;				     /* adjusted exponent */
  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  if (DFISINF(df)) {
    DFWORD(result, 0)=0;		     /* need +ve */
    return decInfinity(result, result);      /* canonical +Infinity */
    }
  if (DFISZERO(df)) {
    set->status|=DEC_Division_by_zero;	     /* as per 754 */
    DFWORD(result, 0)=DECFLOAT_Sign;	     /* make negative */
    return decInfinity(result, result);      /* canonical -Infinity */
    }
  ae=GETEXPUN(df)			/* get unbiased exponent .. */
    +decFloatDigits(df)-1;		/* .. and make adjusted exponent */
  /* ae has limited range (3 digits for DOUBLE and 4 for QUAD), so */
  /* it is worth using a special case of decFloatFromInt32 */
  DFWORD(result, 0)=ZEROWORD;		/* always */
  if (ae<0) {
    DFWORD(result, 0)|=DECFLOAT_Sign;	/* -0 so far */
    ae=-ae;
    }
  #if DOUBLE
    DFWORD(result, 1)=BIN2DPD[ae];	/* a single declet */
  #elif QUAD
    DFWORD(result, 1)=0;
    DFWORD(result, 2)=0;
    DFWORD(result, 3)=(ae/1000)<<10;	/* is <10, so need no DPD encode */
    DFWORD(result, 3)|=BIN2DPD[ae%1000];
  #endif
  return result;
  } /* decFloatLogB */

/* ------------------------------------------------------------------ */
/* decFloatMax -- return maxnum of two operands 		      */
/*								      */
/*   result gets the chosen decFloat				      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* If just one operand is a quiet NaN it is ignored.		      */
/* ------------------------------------------------------------------ */
decFloat * decFloatMax(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  Int comp;
  if (DFISNAN(dfl)) {
    /* sNaN or both NaNs leads to normal NaN processing */
    if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
    return decCanonical(result, dfr);	     /* RHS is numeric */
    }
  if (DFISNAN(dfr)) {
    /* sNaN leads to normal NaN processing (both NaN handled above) */
    if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    return decCanonical(result, dfl);	     /* LHS is numeric */
    }
  /* Both operands are numeric; numeric comparison needed -- use */
  /* total order for a well-defined choice (and +0 > -0) */
  comp=decNumCompare(dfl, dfr, 1);
  if (comp>=0) return decCanonical(result, dfl);
  return decCanonical(result, dfr);
  } /* decFloatMax */

/* ------------------------------------------------------------------ */
/* decFloatMaxMag -- return maxnummag of two operands		      */
/*								      */
/*   result gets the chosen decFloat				      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* Returns according to the magnitude comparisons if both numeric and */
/* unequal, otherwise returns maxnum				      */
/* ------------------------------------------------------------------ */
decFloat * decFloatMaxMag(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  Int comp;
  decFloat absl, absr;
  if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMax(result, dfl, dfr, set);

  decFloatCopyAbs(&absl, dfl);
  decFloatCopyAbs(&absr, dfr);
  comp=decNumCompare(&absl, &absr, 0);
  if (comp>0) return decCanonical(result, dfl);
  if (comp<0) return decCanonical(result, dfr);
  return decFloatMax(result, dfl, dfr, set);
  } /* decFloatMaxMag */

/* ------------------------------------------------------------------ */
/* decFloatMin -- return minnum of two operands 		      */
/*								      */
/*   result gets the chosen decFloat				      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* If just one operand is a quiet NaN it is ignored.		      */
/* ------------------------------------------------------------------ */
decFloat * decFloatMin(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  Int comp;
  if (DFISNAN(dfl)) {
    /* sNaN or both NaNs leads to normal NaN processing */
    if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
    return decCanonical(result, dfr);	     /* RHS is numeric */
    }
  if (DFISNAN(dfr)) {
    /* sNaN leads to normal NaN processing (both NaN handled above) */
    if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    return decCanonical(result, dfl);	     /* LHS is numeric */
    }
  /* Both operands are numeric; numeric comparison needed -- use */
  /* total order for a well-defined choice (and +0 > -0) */
  comp=decNumCompare(dfl, dfr, 1);
  if (comp<=0) return decCanonical(result, dfl);
  return decCanonical(result, dfr);
  } /* decFloatMin */

/* ------------------------------------------------------------------ */
/* decFloatMinMag -- return minnummag of two operands		      */
/*								      */
/*   result gets the chosen decFloat				      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* Returns according to the magnitude comparisons if both numeric and */
/* unequal, otherwise returns minnum				      */
/* ------------------------------------------------------------------ */
decFloat * decFloatMinMag(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  Int comp;
  decFloat absl, absr;
  if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMin(result, dfl, dfr, set);

  decFloatCopyAbs(&absl, dfl);
  decFloatCopyAbs(&absr, dfr);
  comp=decNumCompare(&absl, &absr, 0);
  if (comp<0) return decCanonical(result, dfl);
  if (comp>0) return decCanonical(result, dfr);
  return decFloatMin(result, dfl, dfr, set);
  } /* decFloatMinMag */

/* ------------------------------------------------------------------ */
/* decFloatMinus -- negate value, heeding NaNs, etc.		      */
/*								      */
/*   result gets the canonicalized 0-df 			      */
/*   df     is the decFloat to minus				      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This has the same effect as 0-df where the exponent of the zero is */
/* the same as that of df (if df is finite).			      */
/* The effect is also the same as decFloatCopyNegate except that NaNs */
/* are handled normally (the sign of a NaN is not affected, and an    */
/* sNaN will signal), the result is canonical, and zero gets sign 0.  */
/* ------------------------------------------------------------------ */
decFloat * decFloatMinus(decFloat *result, const decFloat *df,
			 decContext *set) {
  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  decCanonical(result, df);			  /* copy and check */
  if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80;	  /* turn off sign bit */
   else DFBYTE(result, 0)^=0x80;		  /* flip sign bit */
  return result;
  } /* decFloatMinus */

/* ------------------------------------------------------------------ */
/* decFloatMultiply -- multiply two decFloats			      */
/*								      */
/*   result gets the result of multiplying dfl and dfr: 	      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
decFloat * decFloatMultiply(decFloat *result,
			    const decFloat *dfl, const decFloat *dfr,
			    decContext *set) {
  bcdnum num;			   /* for final conversion */
  uByte  bcdacc[DECPMAX9*18+1];    /* for coefficent in BCD */

  if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */
    /* NaNs are handled as usual */
    if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    /* infinity times zero is bad */
    if (DFISINF(dfl) && DFISZERO(dfr)) return decInvalid(result, set);
    if (DFISINF(dfr) && DFISZERO(dfl)) return decInvalid(result, set);
    /* both infinite; return canonical infinity with computed sign */
    DFWORD(result, 0)=DFWORD(dfl, 0)^DFWORD(dfr, 0); /* compute sign */
    return decInfinity(result, result);
    }

  /* Here when both operands are finite */
  decFiniteMultiply(&num, bcdacc, dfl, dfr);
  return decFinalize(result, &num, set); /* round, check, and lay out */
  } /* decFloatMultiply */

/* ------------------------------------------------------------------ */
/* decFloatNextMinus -- next towards -Infinity			      */
/*								      */
/*   result gets the next lesser decFloat			      */
/*   dfl    is the decFloat to start with			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This is 754 nextdown; Invalid is the only status possible (from    */
/* an sNaN).							      */
/* ------------------------------------------------------------------ */
decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl,
			     decContext *set) {
  decFloat delta;			/* tiny increment */
  uInt savestat;			/* saves status */
  enum rounding saveround;		/* .. and mode */

  /* +Infinity is the special case */
  if (DFISINF(dfl) && !DFISSIGNED(dfl)) {
    DFSETNMAX(result);
    return result;			/* [no status to set] */
    }
  /* other cases are effected by sutracting a tiny delta -- this */
  /* should be done in a wider format as the delta is unrepresentable */
  /* here (but can be done with normal add if the sign of zero is */
  /* treated carefully, because no Inexactitude is interesting); */
  /* rounding to -Infinity then pushes the result to next below */
  decFloatZero(&delta); 		/* set up tiny delta */
  DFWORD(&delta, DECWORDS-1)=1; 	/* coefficient=1 */
  DFWORD(&delta, 0)=DECFLOAT_Sign;	/* Sign=1 + biased exponent=0 */
  /* set up for the directional round */
  saveround=set->round; 		/* save mode */
  set->round=DEC_ROUND_FLOOR;		/* .. round towards -Infinity */
  savestat=set->status; 		/* save status */
  decFloatAdd(result, dfl, &delta, set);
  /* Add rules mess up the sign when going from +Ntiny to 0 */
  if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */
  set->status&=DEC_Invalid_operation;	/* preserve only sNaN status */
  set->status|=savestat;		/* restore pending flags */
  set->round=saveround; 		/* .. and mode */
  return result;
  } /* decFloatNextMinus */

/* ------------------------------------------------------------------ */
/* decFloatNextPlus -- next towards +Infinity			      */
/*								      */
/*   result gets the next larger decFloat			      */
/*   dfl    is the decFloat to start with			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This is 754 nextup; Invalid is the only status possible (from      */
/* an sNaN).							      */
/* ------------------------------------------------------------------ */
decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl,
			    decContext *set) {
  uInt savestat;			/* saves status */
  enum rounding saveround;		/* .. and mode */
  decFloat delta;			/* tiny increment */

  /* -Infinity is the special case */
  if (DFISINF(dfl) && DFISSIGNED(dfl)) {
    DFSETNMAX(result);
    DFWORD(result, 0)|=DECFLOAT_Sign;	/* make negative */
    return result;			/* [no status to set] */
    }
  /* other cases are effected by sutracting a tiny delta -- this */
  /* should be done in a wider format as the delta is unrepresentable */
  /* here (but can be done with normal add if the sign of zero is */
  /* treated carefully, because no Inexactitude is interesting); */
  /* rounding to +Infinity then pushes the result to next above */
  decFloatZero(&delta); 		/* set up tiny delta */
  DFWORD(&delta, DECWORDS-1)=1; 	/* coefficient=1 */
  DFWORD(&delta, 0)=0;			/* Sign=0 + biased exponent=0 */
  /* set up for the directional round */
  saveround=set->round; 		/* save mode */
  set->round=DEC_ROUND_CEILING; 	/* .. round towards +Infinity */
  savestat=set->status; 		/* save status */
  decFloatAdd(result, dfl, &delta, set);
  /* Add rules mess up the sign when going from -Ntiny to -0 */
  if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */
  set->status&=DEC_Invalid_operation;	/* preserve only sNaN status */
  set->status|=savestat;		/* restore pending flags */
  set->round=saveround; 		/* .. and mode */
  return result;
  } /* decFloatNextPlus */

/* ------------------------------------------------------------------ */
/* decFloatNextToward -- next towards a decFloat		      */
/*								      */
/*   result gets the next decFloat				      */
/*   dfl    is the decFloat to start with			      */
/*   dfr    is the decFloat to move toward			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This is 754-1985 nextafter, as modified during revision (dropped   */
/* from 754-2008); status may be set unless the result is a normal    */
/* number.							      */
/* ------------------------------------------------------------------ */
decFloat * decFloatNextToward(decFloat *result,
			      const decFloat *dfl, const decFloat *dfr,
			      decContext *set) {
  decFloat delta;			/* tiny increment or decrement */
  decFloat pointone;			/* 1e-1 */
  uInt	savestat;			/* saves status */
  enum	rounding saveround;		/* .. and mode */
  uInt	deltatop;			/* top word for delta */
  Int	comp;				/* work */

  if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  /* Both are numeric, so Invalid no longer a possibility */
  comp=decNumCompare(dfl, dfr, 0);
  if (comp==0) return decFloatCopySign(result, dfl, dfr); /* equal */
  /* unequal; do NextPlus or NextMinus but with different status rules */

  if (comp<0) { /* lhs<rhs, do NextPlus, see above for commentary */
    if (DFISINF(dfl) && DFISSIGNED(dfl)) {   /* -Infinity special case */
      DFSETNMAX(result);
      DFWORD(result, 0)|=DECFLOAT_Sign;
      return result;
      }
    saveround=set->round;		     /* save mode */
    set->round=DEC_ROUND_CEILING;	     /* .. round towards +Infinity */
    deltatop=0; 			     /* positive delta */
    }
   else { /* lhs>rhs, do NextMinus, see above for commentary */
    if (DFISINF(dfl) && !DFISSIGNED(dfl)) {  /* +Infinity special case */
      DFSETNMAX(result);
      return result;
      }
    saveround=set->round;		     /* save mode */
    set->round=DEC_ROUND_FLOOR; 	     /* .. round towards -Infinity */
    deltatop=DECFLOAT_Sign;		     /* negative delta */
    }
  savestat=set->status; 		     /* save status */
  /* Here, Inexact is needed where appropriate (and hence Underflow, */
  /* etc.).  Therefore the tiny delta which is otherwise */
  /* unrepresentable (see NextPlus and NextMinus) is constructed */
  /* using the multiplication of FMA. */
  decFloatZero(&delta); 		/* set up tiny delta */
  DFWORD(&delta, DECWORDS-1)=1; 	/* coefficient=1 */
  DFWORD(&delta, 0)=deltatop;		/* Sign + biased exponent=0 */
  decFloatFromString(&pointone, "1E-1", set); /* set up multiplier */
  decFloatFMA(result, &delta, &pointone, dfl, set);
  /* [Delta is truly tiny, so no need to correct sign of zero] */
  /* use new status unless the result is normal */
  if (decFloatIsNormal(result)) set->status=savestat; /* else goes forward */
  set->round=saveround; 		/* restore mode */
  return result;
  } /* decFloatNextToward */

/* ------------------------------------------------------------------ */
/* decFloatOr -- logical digitwise OR of two decFloats		      */
/*								      */
/*   result gets the result of ORing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result, which will be canonical with sign=0	      */
/*								      */
/* The operands must be positive, finite with exponent q=0, and       */
/* comprise just zeros and ones; if not, Invalid operation results.   */
/* ------------------------------------------------------------------ */
decFloat * decFloatOr(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
   || !DFISCC01(dfl)   || !DFISCC01(dfr)) return decInvalid(result, set);
  /* the operands are positive finite integers (q=0) with just 0s and 1s */
  #if DOUBLE
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04009124);
   DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x49124491;
  #elif QUAD
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04000912);
   DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x44912449;
   DFWORD(result, 2)=(DFWORD(dfl, 2) | DFWORD(dfr, 2))&0x12449124;
   DFWORD(result, 3)=(DFWORD(dfl, 3) | DFWORD(dfr, 3))&0x49124491;
  #endif
  return result;
  } /* decFloatOr */

/* ------------------------------------------------------------------ */
/* decFloatPlus -- add value to 0, heeding NaNs, etc.		      */
/*								      */
/*   result gets the canonicalized 0+df 			      */
/*   df     is the decFloat to plus				      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This has the same effect as 0+df where the exponent of the zero is */
/* the same as that of df (if df is finite).			      */
/* The effect is also the same as decFloatCopy except that NaNs       */
/* are handled normally (the sign of a NaN is not affected, and an    */
/* sNaN will signal), the result is canonical, and zero gets sign 0.  */
/* ------------------------------------------------------------------ */
decFloat * decFloatPlus(decFloat *result, const decFloat *df,
			decContext *set) {
  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  decCanonical(result, df);			  /* copy and check */
  if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80;	  /* turn off sign bit */
  return result;
  } /* decFloatPlus */

/* ------------------------------------------------------------------ */
/* decFloatQuantize -- quantize a decFloat			      */
/*								      */
/*   result gets the result of quantizing dfl to match dfr	      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs), which sets the exponent     */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* Unless there is an error or the result is infinite, the exponent   */
/* of result is guaranteed to be the same as that of dfr.	      */
/* ------------------------------------------------------------------ */
decFloat * decFloatQuantize(decFloat *result,
			    const decFloat *dfl, const decFloat *dfr,
			    decContext *set) {
  Int	explb, exprb;	      /* left and right biased exponents */
  uByte *ulsd;		      /* local LSD pointer */
  uByte *ub, *uc;	      /* work */
  Int	drop;		      /* .. */
  uInt	dpd;		      /* .. */
  uInt	encode; 	      /* encoding accumulator */
  uInt	sourhil, sourhir;     /* top words from source decFloats */
  uInt	uiwork; 	      /* for macros */
  #if QUAD
  uShort uswork;	      /* .. */
  #endif
  /* the following buffer holds the coefficient for manipulation */
  uByte buf[4+DECPMAX*3+2*QUAD];   /* + space for zeros to left or right */
  #if DECTRACE
  bcdnum num;			   /* for trace displays */
  #endif

  /* Start decoding the arguments */
  sourhil=DFWORD(dfl, 0);	   /* LHS top word */
  explb=DECCOMBEXP[sourhil>>26];   /* get exponent high bits (in place) */
  sourhir=DFWORD(dfr, 0);	   /* RHS top word */
  exprb=DECCOMBEXP[sourhir>>26];

  if (EXPISSPECIAL(explb | exprb)) { /* either is special? */
    /* NaNs are handled as usual */
    if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
    /* one infinity but not both is bad */
    if (DFISINF(dfl)!=DFISINF(dfr)) return decInvalid(result, set);
    /* both infinite; return canonical infinity with sign of LHS */
    return decInfinity(result, dfl);
    }

  /* Here when both arguments are finite */
  /* complete extraction of the exponents [no need to unbias] */
  explb+=GETECON(dfl);		   /* + continuation */
  exprb+=GETECON(dfr);		   /* .. */

  /* calculate the number of digits to drop from the coefficient */
  drop=exprb-explb;		   /* 0 if nothing to do */
  if (drop==0) return decCanonical(result, dfl); /* return canonical */

  /* the coefficient is needed; lay it out into buf, offset so zeros */
  /* can be added before or after as needed -- an extra heading is */
  /* added so can safely pad Quad DECPMAX-1 zeros to the left by */
  /* fours */
  #define BUFOFF (buf+4+DECPMAX)
  GETCOEFF(dfl, BUFOFF);	   /* decode from decFloat */
  /* [now the msd is at BUFOFF and the lsd is at BUFOFF+DECPMAX-1] */

  #if DECTRACE
  num.msd=BUFOFF;
  num.lsd=BUFOFF+DECPMAX-1;
  num.exponent=explb-DECBIAS;
  num.sign=sourhil & DECFLOAT_Sign;
  decShowNum(&num, "dfl");
  #endif

  if (drop>0) { 			/* [most common case] */
    /* (this code is very similar to that in decFloatFinalize, but */
    /* has many differences so is duplicated here -- so any changes */
    /* may need to be made there, too) */
    uByte *roundat;			     /* -> re-round digit */
    uByte reround;			     /* reround value */
    /* printf("Rounding; drop=%ld\n", (LI)drop); */

    /* there is at least one zero needed to the left, in all but one */
    /* exceptional (all-nines) case, so place four zeros now; this is */
    /* needed almost always and makes rounding all-nines by fours safe */
    UBFROMUI(BUFOFF-4, 0);

    /* Three cases here: */
    /*	 1. new LSD is in coefficient (almost always) */
    /*	 2. new LSD is digit to left of coefficient (so MSD is */
    /*	    round-for-reround digit) */
    /*	 3. new LSD is to left of case 2 (whole coefficient is sticky) */
    /* Note that leading zeros can safely be treated as useful digits */

    /* [duplicate check-stickies code to save a test] */
    /* [by-digit check for stickies as runs of zeros are rare] */
    if (drop<DECPMAX) { 		     /* NB lengths not addresses */
      roundat=BUFOFF+DECPMAX-drop;
      reround=*roundat;
      for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
	if (*ub!=0) {			     /* non-zero to be discarded */
	  reround=DECSTICKYTAB[reround];     /* apply sticky bit */
	  break;			     /* [remainder don't-care] */
	  }
	} /* check stickies */
      ulsd=roundat-1;			     /* set LSD */
      }
     else {				     /* edge case */
      if (drop==DECPMAX) {
	roundat=BUFOFF;
	reround=*roundat;
	}
       else {
	roundat=BUFOFF-1;
	reround=0;
	}
      for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
	if (*ub!=0) {			     /* non-zero to be discarded */
	  reround=DECSTICKYTAB[reround];     /* apply sticky bit */
	  break;			     /* [remainder don't-care] */
	  }
	} /* check stickies */
      *BUFOFF=0;			     /* make a coefficient of 0 */
      ulsd=BUFOFF;			     /* .. at the MSD place */
      }

    if (reround!=0) {			     /* discarding non-zero */
      uInt bump=0;
      set->status|=DEC_Inexact;

      /* next decide whether to increment the coefficient */
      if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */
	if (reround>5) bump=1;		     /* >0.5 goes up */
	 else if (reround==5)		     /* exactly 0.5000 .. */
	  bump=*ulsd & 0x01;		     /* .. up iff [new] lsd is odd */
	} /* r-h-e */
       else switch (set->round) {
	case DEC_ROUND_DOWN: {
	  /* no change */
	  break;} /* r-d */
	case DEC_ROUND_HALF_DOWN: {
	  if (reround>5) bump=1;
	  break;} /* r-h-d */
	case DEC_ROUND_HALF_UP: {
	  if (reround>=5) bump=1;
	  break;} /* r-h-u */
	case DEC_ROUND_UP: {
	  if (reround>0) bump=1;
	  break;} /* r-u */
	case DEC_ROUND_CEILING: {
	  /* same as _UP for positive numbers, and as _DOWN for negatives */
	  if (!(sourhil&DECFLOAT_Sign) && reround>0) bump=1;
	  break;} /* r-c */
	case DEC_ROUND_FLOOR: {
	  /* same as _UP for negative numbers, and as _DOWN for positive */
	  /* [negative reround cannot occur on 0] */
	  if (sourhil&DECFLOAT_Sign && reround>0) bump=1;
	  break;} /* r-f */
	case DEC_ROUND_05UP: {
	  if (reround>0) { /* anything out there is 'sticky' */
	    /* bump iff lsd=0 or 5; this cannot carry so it could be */
	    /* effected immediately with no bump -- but the code */
	    /* is clearer if this is done the same way as the others */
	    if (*ulsd==0 || *ulsd==5) bump=1;
	    }
	  break;} /* r-r */
	default: {	/* e.g., DEC_ROUND_MAX */
	  set->status|=DEC_Invalid_context;
	  #if DECCHECK
	  printf("Unknown rounding mode: %ld\n", (LI)set->round);
	  #endif
	  break;}
	} /* switch (not r-h-e) */
      /* printf("ReRound: %ld  bump: %ld\n", (LI)reround, (LI)bump); */

      if (bump!=0) {			     /* need increment */
	/* increment the coefficient; this could give 1000... (after */
	/* the all nines case) */
	ub=ulsd;
	for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0);
	/* now at most 3 digits left to non-9 (usually just the one) */
	for (; *ub==9; ub--) *ub=0;
	*ub+=1;
	/* [the all-nines case will have carried one digit to the */
	/* left of the original MSD -- just where it is needed] */
	} /* bump needed */
      } /* inexact rounding */

    /* now clear zeros to the left so exactly DECPMAX digits will be */
    /* available in the coefficent -- the first word to the left was */
    /* cleared earlier for safe carry; now add any more needed */
    if (drop>4) {
      UBFROMUI(BUFOFF-8, 0);		     /* must be at least 5 */
      for (uc=BUFOFF-12; uc>ulsd-DECPMAX-3; uc-=4) UBFROMUI(uc, 0);
      }
    } /* need round (drop>0) */

   else { /* drop<0; padding with -drop digits is needed */
    /* This is the case where an error can occur if the padded */
    /* coefficient will not fit; checking for this can be done in the */
    /* same loop as padding for zeros if the no-hope and zero cases */
    /* are checked first */
    if (-drop>DECPMAX-1) {		     /* cannot fit unless 0 */
      if (!ISCOEFFZERO(BUFOFF)) return decInvalid(result, set);
      /* a zero can have any exponent; just drop through and use it */
      ulsd=BUFOFF+DECPMAX-1;
      }
     else { /* padding will fit (but may still be too long) */
      /* final-word mask depends on endianess */
      #if DECLITEND
      static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff};
      #else
      static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00};
      #endif
      /* note that here zeros to the right are added by fours, so in */
      /* the Quad case this could write 36 zeros if the coefficient has */
      /* fewer than three significant digits (hence the +2*QUAD for buf) */
      for (uc=BUFOFF+DECPMAX;; uc+=4) {
	UBFROMUI(uc, 0);
	if (UBTOUI(uc-DECPMAX)!=0) {		  /* could be bad */
	  /* if all four digits should be zero, definitely bad */
	  if (uc<=BUFOFF+DECPMAX+(-drop)-4)
	    return decInvalid(result, set);
	  /* must be a 1- to 3-digit sequence; check more carefully */
	  if ((UBTOUI(uc-DECPMAX)&dmask[(-drop)%4])!=0)
	    return decInvalid(result, set);
	  break;    /* no need for loop end test */
	  }
	if (uc>=BUFOFF+DECPMAX+(-drop)-4) break;  /* done */
	}
      ulsd=BUFOFF+DECPMAX+(-drop)-1;
      } /* pad and check leading zeros */
    } /* drop<0 */

  #if DECTRACE
  num.msd=ulsd-DECPMAX+1;
  num.lsd=ulsd;
  num.exponent=explb-DECBIAS;
  num.sign=sourhil & DECFLOAT_Sign;
  decShowNum(&num, "res");
  #endif

  /*------------------------------------------------------------------*/
  /* At this point the result is DECPMAX digits, ending at ulsd, so   */
  /* fits the encoding exactly; there is no possibility of error      */
  /*------------------------------------------------------------------*/
  encode=((exprb>>DECECONL)<<4) + *(ulsd-DECPMAX+1); /* make index */
  encode=DECCOMBFROM[encode];		     /* indexed by (0-2)*16+msd */
  /* the exponent continuation can be extracted from the original RHS */
  encode|=sourhir & ECONMASK;
  encode|=sourhil&DECFLOAT_Sign;	     /* add the sign from LHS */

  /* finally encode the coefficient */
  /* private macro to encode a declet; this version can be used */
  /* because all coefficient digits exist */
  #define getDPD3q(dpd, n) ub=ulsd-(3*(n))-2;			\
    dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];

  #if DOUBLE
    getDPD3q(dpd, 4); encode|=dpd<<8;
    getDPD3q(dpd, 3); encode|=dpd>>2;
    DFWORD(result, 0)=encode;
    encode=dpd<<30;
    getDPD3q(dpd, 2); encode|=dpd<<20;
    getDPD3q(dpd, 1); encode|=dpd<<10;
    getDPD3q(dpd, 0); encode|=dpd;
    DFWORD(result, 1)=encode;

  #elif QUAD
    getDPD3q(dpd,10); encode|=dpd<<4;
    getDPD3q(dpd, 9); encode|=dpd>>6;
    DFWORD(result, 0)=encode;
    encode=dpd<<26;
    getDPD3q(dpd, 8); encode|=dpd<<16;
    getDPD3q(dpd, 7); encode|=dpd<<6;
    getDPD3q(dpd, 6); encode|=dpd>>4;
    DFWORD(result, 1)=encode;
    encode=dpd<<28;
    getDPD3q(dpd, 5); encode|=dpd<<18;
    getDPD3q(dpd, 4); encode|=dpd<<8;
    getDPD3q(dpd, 3); encode|=dpd>>2;
    DFWORD(result, 2)=encode;
    encode=dpd<<30;
    getDPD3q(dpd, 2); encode|=dpd<<20;
    getDPD3q(dpd, 1); encode|=dpd<<10;
    getDPD3q(dpd, 0); encode|=dpd;
    DFWORD(result, 3)=encode;
  #endif
  return result;
  } /* decFloatQuantize */

/* ------------------------------------------------------------------ */
/* decFloatReduce -- reduce finite coefficient to minimum length      */
/*								      */
/*   result gets the reduced decFloat				      */
/*   df     is the source decFloat				      */
/*   set    is the context					      */
/*   returns result, which will be canonical			      */
/*								      */
/* This removes all possible trailing zeros from the coefficient;     */
/* some may remain when the number is very close to Nmax.	      */
/* Special values are unchanged and no status is set unless df=sNaN.  */
/* Reduced zero has an exponent q=0.				      */
/* ------------------------------------------------------------------ */
decFloat * decFloatReduce(decFloat *result, const decFloat *df,
			  decContext *set) {
  bcdnum num;				/* work */
  uByte buf[DECPMAX], *ub;		/* coefficient and pointer */
  if (df!=result) *result=*df;		/* copy, if needed */
  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);   /* sNaN */
  /* zeros and infinites propagate too */
  if (DFISINF(df)) return decInfinity(result, df);     /* canonical */
  if (DFISZERO(df)) {
    uInt sign=DFWORD(df, 0)&DECFLOAT_Sign;
    decFloatZero(result);
    DFWORD(result, 0)|=sign;
    return result;			/* exponent dropped, sign OK */
    }
  /* non-zero finite */
  GETCOEFF(df, buf);
  ub=buf+DECPMAX-1;			/* -> lsd */
  if (*ub) return result;		/* no trailing zeros */
  for (ub--; *ub==0;) ub--;		/* terminates because non-zero */
  /* *ub is the first non-zero from the right */
  num.sign=DFWORD(df, 0)&DECFLOAT_Sign; /* set up number... */
  num.exponent=GETEXPUN(df)+(Int)(buf+DECPMAX-1-ub); /* adjusted exponent */
  num.msd=buf;
  num.lsd=ub;
  return decFinalize(result, &num, set);
  } /* decFloatReduce */

/* ------------------------------------------------------------------ */
/* decFloatRemainder -- integer divide and return remainder	      */
/*								      */
/*   result gets the remainder of dividing dfl by dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
decFloat * decFloatRemainder(decFloat *result,
			     const decFloat *dfl, const decFloat *dfr,
			     decContext *set) {
  return decDivide(result, dfl, dfr, set, REMAINDER);
  } /* decFloatRemainder */

/* ------------------------------------------------------------------ */
/* decFloatRemainderNear -- integer divide to nearest and remainder   */
/*								      */
/*   result gets the remainder of dividing dfl by dfr:		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This is the IEEE remainder, where the nearest integer is used.     */
/* ------------------------------------------------------------------ */
decFloat * decFloatRemainderNear(decFloat *result,
			     const decFloat *dfl, const decFloat *dfr,
			     decContext *set) {
  return decDivide(result, dfl, dfr, set, REMNEAR);
  } /* decFloatRemainderNear */

/* ------------------------------------------------------------------ */
/* decFloatRotate -- rotate the coefficient of a decFloat left/right  */
/*								      */
/*   result gets the result of rotating dfl			      */
/*   dfl    is the source decFloat to rotate			      */
/*   dfr    is the count of digits to rotate, an integer (with q=0)   */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* The digits of the coefficient of dfl are rotated to the left (if   */
/* dfr is positive) or to the right (if dfr is negative) without      */
/* adjusting the exponent or the sign of dfl.			      */
/*								      */
/* dfr must be in the range -DECPMAX through +DECPMAX.		      */
/* NaNs are propagated as usual.  An infinite dfl is unaffected (but  */
/* dfr must be valid).	No status is set unless dfr is invalid or an  */
/* operand is an sNaN.	The result is canonical.		      */
/* ------------------------------------------------------------------ */
#define PHALF (ROUNDUP(DECPMAX/2, 4))	/* half length, rounded up */
decFloat * decFloatRotate(decFloat *result,
			 const decFloat *dfl, const decFloat *dfr,
			 decContext *set) {
  Int rotate;				/* dfr as an Int */
  uByte buf[DECPMAX+PHALF];		/* coefficient + half */
  uInt digits, savestat;		/* work */
  bcdnum num;				/* .. */
  uByte *ub;				/* .. */

  if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  if (!DFISINT(dfr)) return decInvalid(result, set);
  digits=decFloatDigits(dfr);			 /* calculate digits */
  if (digits>2) return decInvalid(result, set);  /* definitely out of range */
  rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */
  if (rotate>DECPMAX) return decInvalid(result, set); /* too big */
  /* [from here on no error or status change is possible] */
  if (DFISINF(dfl)) return decInfinity(result, dfl);  /* canonical */
  /* handle no-rotate cases */
  if (rotate==0 || rotate==DECPMAX) return decCanonical(result, dfl);
  /* a real rotate is needed: 0 < rotate < DECPMAX */
  /* reduce the rotation to no more than half to reduce copying later */
  /* (for QUAD in fact half + 2 digits) */
  if (DFISSIGNED(dfr)) rotate=-rotate;
  if (abs(rotate)>PHALF) {
    if (rotate<0) rotate=DECPMAX+rotate;
     else rotate=rotate-DECPMAX;
    }
  /* now lay out the coefficient, leaving room to the right or the */
  /* left depending on the direction of rotation */
  ub=buf;
  if (rotate<0) ub+=PHALF;    /* rotate right, so space to left */
  GETCOEFF(dfl, ub);
  /* copy half the digits to left or right, and set num.msd */
  if (rotate<0) {
    memcpy(buf, buf+DECPMAX, PHALF);
    num.msd=buf+PHALF+rotate;
    }
   else {
    memcpy(buf+DECPMAX, buf, PHALF);
    num.msd=buf+rotate;
    }
  /* fill in rest of num */
  num.lsd=num.msd+DECPMAX-1;
  num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  num.exponent=GETEXPUN(dfl);
  savestat=set->status; 		/* record */
  decFinalize(result, &num, set);
  set->status=savestat; 		/* restore */
  return result;
  } /* decFloatRotate */

/* ------------------------------------------------------------------ */
/* decFloatSameQuantum -- test decFloats for same quantum	      */
/*								      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   returns 1 if the operands have the same quantum, 0 otherwise     */
/*								      */
/* No error is possible and no status results.			      */
/* ------------------------------------------------------------------ */
uInt decFloatSameQuantum(const decFloat *dfl, const decFloat *dfr) {
  if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) {
    if (DFISNAN(dfl) && DFISNAN(dfr)) return 1;
    if (DFISINF(dfl) && DFISINF(dfr)) return 1;
    return 0;  /* any other special mixture gives false */
    }
  if (GETEXP(dfl)==GETEXP(dfr)) return 1; /* biased exponents match */
  return 0;
  } /* decFloatSameQuantum */

/* ------------------------------------------------------------------ */
/* decFloatScaleB -- multiply by a power of 10, as per 754	      */
/*								      */
/*   result gets the result of the operation			      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs), am integer (with q=0)       */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* This computes result=dfl x 10**dfr where dfr is an integer in the  */
/* range +/-2*(emax+pmax), typically resulting from LogB.	      */
/* Underflow and Overflow (with Inexact) may occur.  NaNs propagate   */
/* as usual.							      */
/* ------------------------------------------------------------------ */
#define SCALEBMAX 2*(DECEMAX+DECPMAX)	/* D=800, Q=12356 */
decFloat * decFloatScaleB(decFloat *result,
			  const decFloat *dfl, const decFloat *dfr,
			  decContext *set) {
  uInt digits;				/* work */
  Int  expr;				/* dfr as an Int */

  if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  if (!DFISINT(dfr)) return decInvalid(result, set);
  digits=decFloatDigits(dfr);		     /* calculate digits */

  #if DOUBLE
  if (digits>3) return decInvalid(result, set);   /* definitely out of range */
  expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff];		  /* must be in bottom declet */
  #elif QUAD
  if (digits>5) return decInvalid(result, set);   /* definitely out of range */
  expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff]		  /* in bottom 2 declets .. */
      +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000;  /* .. */
  #endif
  if (expr>SCALEBMAX) return decInvalid(result, set);  /* oops */
  /* [from now on no error possible] */
  if (DFISINF(dfl)) return decInfinity(result, dfl);   /* canonical */
  if (DFISSIGNED(dfr)) expr=-expr;
  /* dfl is finite and expr is valid */
  *result=*dfl; 			     /* copy to target */
  return decFloatSetExponent(result, set, GETEXPUN(result)+expr);
  } /* decFloatScaleB */

/* ------------------------------------------------------------------ */
/* decFloatShift -- shift the coefficient of a decFloat left or right */
/*								      */
/*   result gets the result of shifting dfl			      */
/*   dfl    is the source decFloat to shift			      */
/*   dfr    is the count of digits to shift, an integer (with q=0)    */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* The digits of the coefficient of dfl are shifted to the left (if   */
/* dfr is positive) or to the right (if dfr is negative) without      */
/* adjusting the exponent or the sign of dfl.			      */
/*								      */
/* dfr must be in the range -DECPMAX through +DECPMAX.		      */
/* NaNs are propagated as usual.  An infinite dfl is unaffected (but  */
/* dfr must be valid).	No status is set unless dfr is invalid or an  */
/* operand is an sNaN.	The result is canonical.		      */
/* ------------------------------------------------------------------ */
decFloat * decFloatShift(decFloat *result,
			 const decFloat *dfl, const decFloat *dfr,
			 decContext *set) {
  Int	 shift; 			/* dfr as an Int */
  uByte  buf[DECPMAX*2];		/* coefficient + padding */
  uInt	 digits, savestat;		/* work */
  bcdnum num;				/* .. */
  uInt	 uiwork;			/* for macros */

  if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  if (!DFISINT(dfr)) return decInvalid(result, set);
  digits=decFloatDigits(dfr);			  /* calculate digits */
  if (digits>2) return decInvalid(result, set);   /* definitely out of range */
  shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff];   /* is in bottom declet */
  if (shift>DECPMAX) return decInvalid(result, set);   /* too big */
  /* [from here on no error or status change is possible] */

  if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */
  /* handle no-shift and all-shift (clear to zero) cases */
  if (shift==0) return decCanonical(result, dfl);
  if (shift==DECPMAX) { 		     /* zero with sign */
    uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); /* save sign bit */
    decFloatZero(result);		     /* make +0 */
    DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* and set sign */
    /* [cannot safely use CopySign] */
    return result;
    }
  /* a real shift is needed: 0 < shift < DECPMAX */
  num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  num.exponent=GETEXPUN(dfl);
  num.msd=buf;
  GETCOEFF(dfl, buf);
  if (DFISSIGNED(dfr)) { /* shift right */
    /* edge cases are taken care of, so this is easy */
    num.lsd=buf+DECPMAX-shift-1;
    }
   else { /* shift left -- zero padding needed to right */
    UBFROMUI(buf+DECPMAX, 0);		/* 8 will handle most cases */
    UBFROMUI(buf+DECPMAX+4, 0); 	/* .. */
    if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); /* all other cases */
    num.msd+=shift;
    num.lsd=num.msd+DECPMAX-1;
    }
  savestat=set->status; 		/* record */
  decFinalize(result, &num, set);
  set->status=savestat; 		/* restore */
  return result;
  } /* decFloatShift */

/* ------------------------------------------------------------------ */
/* decFloatSubtract -- subtract a decFloat from another 	      */
/*								      */
/*   result gets the result of subtracting dfr from dfl:	      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* ------------------------------------------------------------------ */
decFloat * decFloatSubtract(decFloat *result,
			    const decFloat *dfl, const decFloat *dfr,
			    decContext *set) {
  decFloat temp;
  /* NaNs must propagate without sign change */
  if (DFISNAN(dfr)) return decFloatAdd(result, dfl, dfr, set);
  temp=*dfr;				       /* make a copy */
  DFBYTE(&temp, 0)^=0x80;		       /* flip sign */
  return decFloatAdd(result, dfl, &temp, set); /* and add to the lhs */
  } /* decFloatSubtract */

/* ------------------------------------------------------------------ */
/* decFloatToInt -- round to 32-bit binary integer (4 flavours)       */
/*								      */
/*   df    is the decFloat to round				      */
/*   set   is the context					      */
/*   round is the rounding mode to use				      */
/*   returns a uInt or an Int, rounded according to the name	      */
/*								      */
/* Invalid will always be signaled if df is a NaN, is Infinite, or is */
/* outside the range of the target; Inexact will not be signaled for  */
/* simple rounding unless 'Exact' appears in the name.		      */
/* ------------------------------------------------------------------ */
uInt decFloatToUInt32(const decFloat *df, decContext *set,
		      enum rounding round) {
  return decToInt32(df, set, round, 0, 1);}

uInt decFloatToUInt32Exact(const decFloat *df, decContext *set,
			   enum rounding round) {
  return decToInt32(df, set, round, 1, 1);}

Int decFloatToInt32(const decFloat *df, decContext *set,
		    enum rounding round) {
  return (Int)decToInt32(df, set, round, 0, 0);}

Int decFloatToInt32Exact(const decFloat *df, decContext *set,
			 enum rounding round) {
  return (Int)decToInt32(df, set, round, 1, 0);}

/* ------------------------------------------------------------------ */
/* decFloatToIntegral -- round to integral value (two flavours)       */
/*								      */
/*   result gets the result					      */
/*   df     is the decFloat to round				      */
/*   set    is the context					      */
/*   round  is the rounding mode to use 			      */
/*   returns result						      */
/*								      */
/* No exceptions, even Inexact, are raised except for sNaN input, or  */
/* if 'Exact' appears in the name.				      */
/* ------------------------------------------------------------------ */
decFloat * decFloatToIntegralValue(decFloat *result, const decFloat *df,
				   decContext *set, enum rounding round) {
  return decToIntegral(result, df, set, round, 0);}

decFloat * decFloatToIntegralExact(decFloat *result, const decFloat *df,
				   decContext *set) {
  return decToIntegral(result, df, set, set->round, 1);}

/* ------------------------------------------------------------------ */
/* decFloatXor -- logical digitwise XOR of two decFloats	      */
/*								      */
/*   result gets the result of XORing dfl and dfr		      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs)			      */
/*   set    is the context					      */
/*   returns result, which will be canonical with sign=0	      */
/*								      */
/* The operands must be positive, finite with exponent q=0, and       */
/* comprise just zeros and ones; if not, Invalid operation results.   */
/* ------------------------------------------------------------------ */
decFloat * decFloatXor(decFloat *result,
		       const decFloat *dfl, const decFloat *dfr,
		       decContext *set) {
  if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
   || !DFISCC01(dfl)   || !DFISCC01(dfr)) return decInvalid(result, set);
  /* the operands are positive finite integers (q=0) with just 0s and 1s */
  #if DOUBLE
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04009124);
   DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x49124491;
  #elif QUAD
   DFWORD(result, 0)=ZEROWORD
		   |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04000912);
   DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x44912449;
   DFWORD(result, 2)=(DFWORD(dfl, 2) ^ DFWORD(dfr, 2))&0x12449124;
   DFWORD(result, 3)=(DFWORD(dfl, 3) ^ DFWORD(dfr, 3))&0x49124491;
  #endif
  return result;
  } /* decFloatXor */

/* ------------------------------------------------------------------ */
/* decInvalid -- set Invalid_operation result			      */
/*								      */
/*   result gets a canonical NaN				      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* status has Invalid_operation added				      */
/* ------------------------------------------------------------------ */
static decFloat *decInvalid(decFloat *result, decContext *set) {
  decFloatZero(result);
  DFWORD(result, 0)=DECFLOAT_qNaN;
  set->status|=DEC_Invalid_operation;
  return result;
  } /* decInvalid */

/* ------------------------------------------------------------------ */
/* decInfinity -- set canonical Infinity with sign from a decFloat    */
/*								      */
/*   result gets a canonical Infinity				      */
/*   df     is source decFloat (only the sign is used)		      */
/*   returns result						      */
/*								      */
/* df may be the same as result 				      */
/* ------------------------------------------------------------------ */
static decFloat *decInfinity(decFloat *result, const decFloat *df) {
  uInt sign=DFWORD(df, 0);	   /* save source signword */
  decFloatZero(result); 	   /* clear everything */
  DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign);
  return result;
  } /* decInfinity */

/* ------------------------------------------------------------------ */
/* decNaNs -- handle NaN argument(s)				      */
/*								      */
/*   result gets the result of handling dfl and dfr, one or both of   */
/*	    which is a NaN					      */
/*   dfl    is the first decFloat (lhs) 			      */
/*   dfr    is the second decFloat (rhs) -- may be NULL for a single- */
/*	    operand operation					      */
/*   set    is the context					      */
/*   returns result						      */
/*								      */
/* Called when one or both operands is a NaN, and propagates the      */
/* appropriate result to res.  When an sNaN is found, it is changed   */
/* to a qNaN and Invalid operation is set.			      */
/* ------------------------------------------------------------------ */
static decFloat *decNaNs(decFloat *result,
			 const decFloat *dfl, const decFloat *dfr,
			 decContext *set) {
  /* handle sNaNs first */
  if (dfr!=NULL && DFISSNAN(dfr) && !DFISSNAN(dfl)) dfl=dfr; /* use RHS */
  if (DFISSNAN(dfl)) {
    decCanonical(result, dfl);		/* propagate canonical sNaN */
    DFWORD(result, 0)&=~(DECFLOAT_qNaN ^ DECFLOAT_sNaN); /* quiet */
    set->status|=DEC_Invalid_operation;
    return result;
    }
  /* one or both is a quiet NaN */
  if (!DFISNAN(dfl)) dfl=dfr;		/* RHS must be NaN, use it */
  return decCanonical(result, dfl);	/* propagate canonical qNaN */
  } /* decNaNs */

/* ------------------------------------------------------------------ */
/* decNumCompare -- numeric comparison of two decFloats 	      */
/*								      */
/*   dfl    is the left-hand decFloat, which is not a NaN	      */
/*   dfr    is the right-hand decFloat, which is not a NaN	      */
/*   tot    is 1 for total order compare, 0 for simple numeric	      */
/*   returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr 	      */
/*								      */
/* No error is possible; status and mode are unchanged. 	      */
/* ------------------------------------------------------------------ */
static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) {
  Int	sigl, sigr;			/* LHS and RHS non-0 signums */
  Int	shift;				/* shift needed to align operands */
  uByte *ub, *uc;			/* work */
  uInt	uiwork; 			/* for macros */
  /* buffers +2 if Quad (36 digits), need double plus 4 for safe padding */
  uByte bufl[DECPMAX*2+QUAD*2+4];	/* for LHS coefficient + padding */
  uByte bufr[DECPMAX*2+QUAD*2+4];	/* for RHS coefficient + padding */

  sigl=1;
  if (DFISSIGNED(dfl)) {
    if (!DFISSIGNED(dfr)) {		/* -LHS +RHS */
      if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
      return -1;			/* RHS wins */
      }
    sigl=-1;
    }
  if (DFISSIGNED(dfr)) {
    if (!DFISSIGNED(dfl)) {		/* +LHS -RHS */
      if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
      return +1;			/* LHS wins */
      }
    }

  /* signs are the same; operand(s) could be zero */
  sigr=-sigl;				/* sign to return if abs(RHS) wins */

  if (DFISINF(dfl)) {
    if (DFISINF(dfr)) return 0; 	/* both infinite & same sign */
    return sigl;			/* inf > n */
    }
  if (DFISINF(dfr)) return sigr;	/* n < inf [dfl is finite] */

  /* here, both are same sign and finite; calculate their offset */
  shift=GETEXP(dfl)-GETEXP(dfr);	/* [0 means aligned] */
  /* [bias can be ignored -- the absolute exponent is not relevant] */

  if (DFISZERO(dfl)) {
    if (!DFISZERO(dfr)) return sigr;	/* LHS=0, RHS!=0 */
    /* both are zero, return 0 if both same exponent or numeric compare */
    if (shift==0 || !tot) return 0;
    if (shift>0) return sigl;
    return sigr;			/* [shift<0] */
    }
   else {				/* LHS!=0 */
    if (DFISZERO(dfr)) return sigl;	/* LHS!=0, RHS=0 */
    }
  /* both are known to be non-zero at this point */

  /* if the exponents are so different that the coefficients do not */
  /* overlap (by even one digit) then a full comparison is not needed */
  if (abs(shift)>=DECPMAX) {		/* no overlap */
    /* coefficients are known to be non-zero */
    if (shift>0) return sigl;
    return sigr;			/* [shift<0] */
    }

  /* decode the coefficients */
  /* (shift both right two if Quad to make a multiple of four) */
  #if QUAD
    UBFROMUI(bufl, 0);
    UBFROMUI(bufr, 0);
  #endif
  GETCOEFF(dfl, bufl+QUAD*2);		/* decode from decFloat */
  GETCOEFF(dfr, bufr+QUAD*2);		/* .. */
  if (shift==0) {			/* aligned; common and easy */
    /* all multiples of four, here */
    for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
      uInt ui=UBTOUI(ub);
      if (ui==UBTOUI(uc)) continue;	/* so far so same */
      /* about to find a winner; go by bytes in case little-endian */
      for (;; ub++, uc++) {
	if (*ub>*uc) return sigl;	/* difference found */
	if (*ub<*uc) return sigr;	/* .. */
	}
      }
    } /* aligned */
   else if (shift>0) {			/* lhs to left */
    ub=bufl;				/* RHS pointer */
    /* pad bufl so right-aligned; most shifts will fit in 8 */
    UBFROMUI(bufl+DECPMAX+QUAD*2, 0);	/* add eight zeros */
    UBFROMUI(bufl+DECPMAX+QUAD*2+4, 0); /* .. */
    if (shift>8) {
      /* more than eight; fill the rest, and also worth doing the */
      /* lead-in by fours */
      uByte *up;			/* work */
      uByte *upend=bufl+DECPMAX+QUAD*2+shift;
      for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0);
      /* [pads up to 36 in all for Quad] */
      for (;; ub+=4) {
	if (UBTOUI(ub)!=0) return sigl;
	if (ub+4>bufl+shift-4) break;
	}
      }
    /* check remaining leading digits */
    for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl;
    /* now start the overlapped part; bufl has been padded, so the */
    /* comparison can go for the full length of bufr, which is a */
    /* multiple of 4 bytes */
    for (uc=bufr; ; uc+=4, ub+=4) {
      uInt ui=UBTOUI(ub);
      if (ui!=UBTOUI(uc)) {		/* mismatch found */
	for (;; uc++, ub++) {		/* check from left [little-endian?] */
	  if (*ub>*uc) return sigl;	/* difference found */
	  if (*ub<*uc) return sigr;	/* .. */
	  }
	} /* mismatch */
      if (uc==bufr+QUAD*2+DECPMAX-4) break; /* all checked */
      }
    } /* shift>0 */

   else { /* shift<0) .. RHS is to left of LHS; mirror shift>0 */
    uc=bufr;				/* RHS pointer */
    /* pad bufr so right-aligned; most shifts will fit in 8 */
    UBFROMUI(bufr+DECPMAX+QUAD*2, 0);	/* add eight zeros */
    UBFROMUI(bufr+DECPMAX+QUAD*2+4, 0); /* .. */
    if (shift<-8) {
      /* more than eight; fill the rest, and also worth doing the */
      /* lead-in by fours */
      uByte *up;			/* work */
      uByte *upend=bufr+DECPMAX+QUAD*2-shift;
      for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0);
      /* [pads up to 36 in all for Quad] */
      for (;; uc+=4) {
	if (UBTOUI(uc)!=0) return sigr;
	if (uc+4>bufr-shift-4) break;
	}
      }
    /* check remaining leading digits */
    for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr;
    /* now start the overlapped part; bufr has been padded, so the */
    /* comparison can go for the full length of bufl, which is a */
    /* multiple of 4 bytes */
    for (ub=bufl; ; ub+=4, uc+=4) {
      uInt ui=UBTOUI(ub);
      if (ui!=UBTOUI(uc)) {		/* mismatch found */
	for (;; ub++, uc++) {		/* check from left [little-endian?] */
	  if (*ub>*uc) return sigl;	/* difference found */
	  if (*ub<*uc) return sigr;	/* .. */
	  }
	} /* mismatch */
      if (ub==bufl+QUAD*2+DECPMAX-4) break; /* all checked */
      }
    } /* shift<0 */

  /* Here when compare equal */
  if (!tot) return 0;			/* numerically equal */
  /* total ordering .. exponent matters */
  if (shift>0) return sigl;		/* total order by exponent */
  if (shift<0) return sigr;		/* .. */
  return 0;
  } /* decNumCompare */

/* ------------------------------------------------------------------ */
/* decToInt32 -- local routine to effect ToInteger conversions	      */
/*								      */
/*   df     is the decFloat to convert				      */
/*   set    is the context					      */
/*   rmode  is the rounding mode to use 			      */
/*   exact  is 1 if Inexact should be signalled 		      */
/*   unsign is 1 if the result a uInt, 0 if an Int (cast to uInt)     */
/*   returns 32-bit result as a uInt				      */
/*								      */
/* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */
/* these cases 0 is returned.					      */
/* ------------------------------------------------------------------ */
static uInt decToInt32(const decFloat *df, decContext *set,
		       enum rounding rmode, Flag exact, Flag unsign) {
  Int  exp;			   /* exponent */
  uInt sourhi, sourpen, sourlo;    /* top word from source decFloat .. */
  uInt hi, lo;			   /* .. penultimate, least, etc. */
  decFloat zero, result;	   /* work */
  Int  i;			   /* .. */

  /* Start decoding the argument */
  sourhi=DFWORD(df, 0); 		/* top word */
  exp=DECCOMBEXP[sourhi>>26];		/* get exponent high bits (in place) */
  if (EXPISSPECIAL(exp)) {		/* is special? */
    set->status|=DEC_Invalid_operation; /* signal */
    return 0;
    }

  /* Here when the argument is finite */
  if (GETEXPUN(df)==0) result=*df;	/* already a true integer */
   else {				/* need to round to integer */
    enum rounding saveround;		/* saver */
    uInt savestatus;			/* .. */
    saveround=set->round;		/* save rounding mode .. */
    savestatus=set->status;		/* .. and status */
    set->round=rmode;			/* set mode */
    decFloatZero(&zero);		/* make 0E+0 */
    set->status=0;			/* clear */
    decFloatQuantize(&result, df, &zero, set); /* [this may fail] */
    set->round=saveround;		/* restore rounding mode .. */
    if (exact) set->status|=savestatus; /* include Inexact */
     else set->status=savestatus;	/* .. or just original status */
    }

  /* only the last four declets of the coefficient can contain */
  /* non-zero; check for others (and also NaN or Infinity from the */
  /* Quantize) first (see DFISZERO for explanation): */
  /* decFloatShow(&result, "sofar"); */
  #if DOUBLE
  if ((DFWORD(&result, 0)&0x1c03ff00)!=0
   || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
  #elif QUAD
  if ((DFWORD(&result, 2)&0xffffff00)!=0
   ||  DFWORD(&result, 1)!=0
   || (DFWORD(&result, 0)&0x1c003fff)!=0
   || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
  #endif
    set->status|=DEC_Invalid_operation; /* Invalid or out of range */
    return 0;
    }
  /* get last twelve digits of the coefficent into hi & ho, base */
  /* 10**9 (see GETCOEFFBILL): */
  sourlo=DFWORD(&result, DECWORDS-1);
  lo=DPD2BIN0[sourlo&0x3ff]
    +DPD2BINK[(sourlo>>10)&0x3ff]
    +DPD2BINM[(sourlo>>20)&0x3ff];
  sourpen=DFWORD(&result, DECWORDS-2);
  hi=DPD2BIN0[((sourpen<<2) | (sourlo>>30))&0x3ff];

  /* according to request, check range carefully */
  if (unsign) {
    if (hi>4 || (hi==4 && lo>294967295) || (hi+lo!=0 && DFISSIGNED(&result))) {
      set->status|=DEC_Invalid_operation; /* out of range */
      return 0;
      }
    return hi*BILLION+lo;
    }
  /* signed */
  if (hi>2 || (hi==2 && lo>147483647)) {
    /* handle the usual edge case */
    if (lo==147483648 && hi==2 && DFISSIGNED(&result)) return 0x80000000;
    set->status|=DEC_Invalid_operation; /* truly out of range */
    return 0;
    }
  i=hi*BILLION+lo;
  if (DFISSIGNED(&result)) i=-i;
  return (uInt)i;
  } /* decToInt32 */

/* ------------------------------------------------------------------ */
/* decToIntegral -- local routine to effect ToIntegral value	      */
/*								      */
/*   result gets the result					      */
/*   df     is the decFloat to round				      */
/*   set    is the context					      */
/*   rmode  is the rounding mode to use 			      */
/*   exact  is 1 if Inexact should be signalled 		      */
/*   returns result						      */
/* ------------------------------------------------------------------ */
static decFloat * decToIntegral(decFloat *result, const decFloat *df,
				decContext *set, enum rounding rmode,
				Flag exact) {
  Int  exp;			   /* exponent */
  uInt sourhi;			   /* top word from source decFloat */
  enum rounding saveround;	   /* saver */
  uInt savestatus;		   /* .. */
  decFloat zero;		   /* work */

  /* Start decoding the argument */
  sourhi=DFWORD(df, 0); 	   /* top word */
  exp=DECCOMBEXP[sourhi>>26];	   /* get exponent high bits (in place) */

  if (EXPISSPECIAL(exp)) {	   /* is special? */
    /* NaNs are handled as usual */
    if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
    /* must be infinite; return canonical infinity with sign of df */
    return decInfinity(result, df);
    }

  /* Here when the argument is finite */
  /* complete extraction of the exponent */
  exp+=GETECON(df)-DECBIAS;		/* .. + continuation and unbias */

  if (exp>=0) return decCanonical(result, df); /* already integral */

  saveround=set->round; 		/* save rounding mode .. */
  savestatus=set->status;		/* .. and status */
  set->round=rmode;			/* set mode */
  decFloatZero(&zero);			/* make 0E+0 */
  decFloatQuantize(result, df, &zero, set); /* 'integrate'; cannot fail */
  set->round=saveround; 		/* restore rounding mode .. */
  if (!exact) set->status=savestatus;	/* .. and status, unless exact */
  return result;
  } /* decToIntegral */