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 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458
|
/* Copyright (C) 2007-2015 Free Software Foundation, Inc.
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/>. */
/*****************************************************************************
*
* BID128 fma x * y + z
*
****************************************************************************/
#include "bid_internal.h"
static void
rounding_correction (unsigned int rnd_mode,
unsigned int is_inexact_lt_midpoint,
unsigned int is_inexact_gt_midpoint,
unsigned int is_midpoint_lt_even,
unsigned int is_midpoint_gt_even,
int unbexp,
UINT128 * ptrres, _IDEC_flags * ptrfpsf) {
// unbiased true exponent unbexp may be larger than emax
UINT128 res = *ptrres; // expected to have the correct sign and coefficient
// (the exponent field is ignored, as unbexp is used instead)
UINT64 sign, exp;
UINT64 C_hi, C_lo;
// general correction from RN to RA, RM, RP, RZ
// Note: if the result is negative, then is_inexact_lt_midpoint,
// is_inexact_gt_midpoint, is_midpoint_lt_even, and is_midpoint_gt_even
// have to be considered as if determined for the absolute value of the
// result (so they seem to be reversed)
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
*ptrfpsf |= INEXACT_EXCEPTION;
}
// apply correction to result calculated with unbounded exponent
sign = res.w[1] & MASK_SIGN;
exp = (UINT64) (unbexp + 6176) << 49; // valid only if expmin<=unbexp<=expmax
C_hi = res.w[1] & MASK_COEFF;
C_lo = res.w[0];
if ((!sign && ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint) ||
((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_UP) &&
is_midpoint_gt_even))) ||
(sign && ((rnd_mode == ROUNDING_DOWN && is_inexact_lt_midpoint) ||
((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_DOWN) &&
is_midpoint_gt_even)))) {
// C = C + 1
C_lo = C_lo + 1;
if (C_lo == 0)
C_hi = C_hi + 1;
if (C_hi == 0x0001ed09bead87c0ull && C_lo == 0x378d8e6400000000ull) {
// C = 10^34 => rounding overflow
C_hi = 0x0000314dc6448d93ull;
C_lo = 0x38c15b0a00000000ull; // 10^33
// exp = exp + EXP_P1;
unbexp = unbexp + 1;
exp = (UINT64) (unbexp + 6176) << 49;
}
} else if ((is_midpoint_lt_even || is_inexact_gt_midpoint) &&
((sign && (rnd_mode == ROUNDING_UP || rnd_mode == ROUNDING_TO_ZERO)) ||
(!sign && (rnd_mode == ROUNDING_DOWN || rnd_mode == ROUNDING_TO_ZERO)))) {
// C = C - 1
C_lo = C_lo - 1;
if (C_lo == 0xffffffffffffffffull)
C_hi--;
// check if we crossed into the lower decade
if (C_hi == 0x0000314dc6448d93ull && C_lo == 0x38c15b09ffffffffull) {
// C = 10^33 - 1
if (exp > 0) {
C_hi = 0x0001ed09bead87c0ull; // 10^34 - 1
C_lo = 0x378d8e63ffffffffull;
// exp = exp - EXP_P1;
unbexp = unbexp - 1;
exp = (UINT64) (unbexp + 6176) << 49;
} else { // if exp = 0 the result is tiny & inexact
*ptrfpsf |= UNDERFLOW_EXCEPTION;
}
}
} else {
; // the result is already correct
}
if (unbexp > expmax) { // 6111
*ptrfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
exp = 0;
if (!sign) { // result is positive
if (rnd_mode == ROUNDING_UP || rnd_mode == ROUNDING_TIES_AWAY) { // +inf
C_hi = 0x7800000000000000ull;
C_lo = 0x0000000000000000ull;
} else { // res = +MAXFP = (10^34-1) * 10^emax
C_hi = 0x5fffed09bead87c0ull;
C_lo = 0x378d8e63ffffffffull;
}
} else { // result is negative
if (rnd_mode == ROUNDING_DOWN || rnd_mode == ROUNDING_TIES_AWAY) { // -inf
C_hi = 0xf800000000000000ull;
C_lo = 0x0000000000000000ull;
} else { // res = -MAXFP = -(10^34-1) * 10^emax
C_hi = 0xdfffed09bead87c0ull;
C_lo = 0x378d8e63ffffffffull;
}
}
}
// assemble the result
res.w[1] = sign | exp | C_hi;
res.w[0] = C_lo;
*ptrres = res;
}
static void
add256 (UINT256 x, UINT256 y, UINT256 * pz) {
// *z = x + yl assume the sum fits in 256 bits
UINT256 z;
z.w[0] = x.w[0] + y.w[0];
if (z.w[0] < x.w[0]) {
x.w[1]++;
if (x.w[1] == 0x0000000000000000ull) {
x.w[2]++;
if (x.w[2] == 0x0000000000000000ull) {
x.w[3]++;
}
}
}
z.w[1] = x.w[1] + y.w[1];
if (z.w[1] < x.w[1]) {
x.w[2]++;
if (x.w[2] == 0x0000000000000000ull) {
x.w[3]++;
}
}
z.w[2] = x.w[2] + y.w[2];
if (z.w[2] < x.w[2]) {
x.w[3]++;
}
z.w[3] = x.w[3] + y.w[3]; // it was assumed that no carry is possible
*pz = z;
}
static void
sub256 (UINT256 x, UINT256 y, UINT256 * pz) {
// *z = x - y; assume x >= y
UINT256 z;
z.w[0] = x.w[0] - y.w[0];
if (z.w[0] > x.w[0]) {
x.w[1]--;
if (x.w[1] == 0xffffffffffffffffull) {
x.w[2]--;
if (x.w[2] == 0xffffffffffffffffull) {
x.w[3]--;
}
}
}
z.w[1] = x.w[1] - y.w[1];
if (z.w[1] > x.w[1]) {
x.w[2]--;
if (x.w[2] == 0xffffffffffffffffull) {
x.w[3]--;
}
}
z.w[2] = x.w[2] - y.w[2];
if (z.w[2] > x.w[2]) {
x.w[3]--;
}
z.w[3] = x.w[3] - y.w[3]; // no borrow possible, because x >= y
*pz = z;
}
static int
nr_digits256 (UINT256 R256) {
int ind;
// determine the number of decimal digits in R256
if (R256.w[3] == 0x0 && R256.w[2] == 0x0 && R256.w[1] == 0x0) {
// between 1 and 19 digits
for (ind = 1; ind <= 19; ind++) {
if (R256.w[0] < ten2k64[ind]) {
break;
}
}
// ind digits
} else if (R256.w[3] == 0x0 && R256.w[2] == 0x0 &&
(R256.w[1] < ten2k128[0].w[1] ||
(R256.w[1] == ten2k128[0].w[1]
&& R256.w[0] < ten2k128[0].w[0]))) {
// 20 digits
ind = 20;
} else if (R256.w[3] == 0x0 && R256.w[2] == 0x0) {
// between 21 and 38 digits
for (ind = 1; ind <= 18; ind++) {
if (R256.w[1] < ten2k128[ind].w[1] ||
(R256.w[1] == ten2k128[ind].w[1] &&
R256.w[0] < ten2k128[ind].w[0])) {
break;
}
}
// ind + 20 digits
ind = ind + 20;
} else if (R256.w[3] == 0x0 &&
(R256.w[2] < ten2k256[0].w[2] ||
(R256.w[2] == ten2k256[0].w[2] &&
R256.w[1] < ten2k256[0].w[1]) ||
(R256.w[2] == ten2k256[0].w[2] &&
R256.w[1] == ten2k256[0].w[1] &&
R256.w[0] < ten2k256[0].w[0]))) {
// 39 digits
ind = 39;
} else {
// between 40 and 68 digits
for (ind = 1; ind <= 29; ind++) {
if (R256.w[3] < ten2k256[ind].w[3] ||
(R256.w[3] == ten2k256[ind].w[3] &&
R256.w[2] < ten2k256[ind].w[2]) ||
(R256.w[3] == ten2k256[ind].w[3] &&
R256.w[2] == ten2k256[ind].w[2] &&
R256.w[1] < ten2k256[ind].w[1]) ||
(R256.w[3] == ten2k256[ind].w[3] &&
R256.w[2] == ten2k256[ind].w[2] &&
R256.w[1] == ten2k256[ind].w[1] &&
R256.w[0] < ten2k256[ind].w[0])) {
break;
}
}
// ind + 39 digits
ind = ind + 39;
}
return (ind);
}
// add/subtract C4 and C3 * 10^scale; this may follow a previous rounding, so
// use the rounding information from ptr_is_* to avoid a double rounding error
static void
add_and_round (int q3,
int q4,
int e4,
int delta,
int p34,
UINT64 z_sign,
UINT64 p_sign,
UINT128 C3,
UINT256 C4,
int rnd_mode,
int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint,
_IDEC_flags * ptrfpsf, UINT128 * ptrres) {
int scale;
int x0;
int ind;
UINT64 R64;
UINT128 P128, R128;
UINT192 P192, R192;
UINT256 R256;
int is_midpoint_lt_even = 0;
int is_midpoint_gt_even = 0;
int is_inexact_lt_midpoint = 0;
int is_inexact_gt_midpoint = 0;
int is_midpoint_lt_even0 = 0;
int is_midpoint_gt_even0 = 0;
int is_inexact_lt_midpoint0 = 0;
int is_inexact_gt_midpoint0 = 0;
int incr_exp = 0;
int is_tiny = 0;
int lt_half_ulp = 0;
int eq_half_ulp = 0;
// int gt_half_ulp = 0;
UINT128 res = *ptrres;
// scale C3 up by 10^(q4-delta-q3), 0 <= q4-delta-q3 <= 2*P34-2 = 66
scale = q4 - delta - q3; // 0 <= scale <= 66 (or 0 <= scale <= 68 if this
// comes from Cases (2), (3), (4), (5), (6), with 0 <= |delta| <= 1
// calculate C3 * 10^scale in R256 (it has at most 67 decimal digits for
// Cases (15),(16),(17) and at most 69 for Cases (2),(3),(4),(5),(6))
if (scale == 0) {
R256.w[3] = 0x0ull;
R256.w[2] = 0x0ull;
R256.w[1] = C3.w[1];
R256.w[0] = C3.w[0];
} else if (scale <= 19) { // 10^scale fits in 64 bits
P128.w[1] = 0;
P128.w[0] = ten2k64[scale];
__mul_128x128_to_256 (R256, P128, C3);
} else if (scale <= 38) { // 10^scale fits in 128 bits
__mul_128x128_to_256 (R256, ten2k128[scale - 20], C3);
} else if (scale <= 57) { // 39 <= scale <= 57
// 10^scale fits in 192 bits but C3 * 10^scale fits in 223 or 230 bits
// (10^67 has 223 bits; 10^69 has 230 bits);
// must split the computation:
// 10^scale * C3 = 10*38 * 10^(scale-38) * C3 where 10^38 takes 127
// bits and so 10^(scale-38) * C3 fits in 128 bits with certainty
// Note that 1 <= scale - 38 <= 19 => 10^(scale-38) fits in 64 bits
__mul_64x128_to_128 (R128, ten2k64[scale - 38], C3);
// now multiply R128 by 10^38
__mul_128x128_to_256 (R256, R128, ten2k128[18]);
} else { // 58 <= scale <= 66
// 10^scale takes between 193 and 220 bits,
// and C3 * 10^scale fits in 223 bits (10^67/10^69 has 223/230 bits)
// must split the computation:
// 10^scale * C3 = 10*38 * 10^(scale-38) * C3 where 10^38 takes 127
// bits and so 10^(scale-38) * C3 fits in 128 bits with certainty
// Note that 20 <= scale - 38 <= 30 => 10^(scale-38) fits in 128 bits
// Calculate first 10^(scale-38) * C3, which fits in 128 bits; because
// 10^(scale-38) takes more than 64 bits, C3 will take less than 64
__mul_64x128_to_128 (R128, C3.w[0], ten2k128[scale - 58]);
// now calculate 10*38 * 10^(scale-38) * C3
__mul_128x128_to_256 (R256, R128, ten2k128[18]);
}
// C3 * 10^scale is now in R256
// for Cases (15), (16), (17) C4 > C3 * 10^scale because C4 has at least
// one extra digit; for Cases (2), (3), (4), (5), or (6) any order is
// possible
// add/subtract C4 and C3 * 10^scale; the exponent is e4
if (p_sign == z_sign) { // R256 = C4 + R256
// calculate R256 = C4 + C3 * 10^scale = C4 + R256 which is exact,
// but may require rounding
add256 (C4, R256, &R256);
} else { // if (p_sign != z_sign) { // R256 = C4 - R256
// calculate R256 = C4 - C3 * 10^scale = C4 - R256 or
// R256 = C3 * 10^scale - C4 = R256 - C4 which is exact,
// but may require rounding
// compare first R256 = C3 * 10^scale and C4
if (R256.w[3] > C4.w[3] || (R256.w[3] == C4.w[3] && R256.w[2] > C4.w[2]) ||
(R256.w[3] == C4.w[3] && R256.w[2] == C4.w[2] && R256.w[1] > C4.w[1]) ||
(R256.w[3] == C4.w[3] && R256.w[2] == C4.w[2] && R256.w[1] == C4.w[1] &&
R256.w[0] >= C4.w[0])) { // C3 * 10^scale >= C4
// calculate R256 = C3 * 10^scale - C4 = R256 - C4, which is exact,
// but may require rounding
sub256 (R256, C4, &R256);
// flip p_sign too, because the result has the sign of z
p_sign = z_sign;
} else { // if C4 > C3 * 10^scale
// calculate R256 = C4 - C3 * 10^scale = C4 - R256, which is exact,
// but may require rounding
sub256 (C4, R256, &R256);
}
// if the result is pure zero, the sign depends on the rounding mode
// (x*y and z had opposite signs)
if (R256.w[3] == 0x0ull && R256.w[2] == 0x0ull &&
R256.w[1] == 0x0ull && R256.w[0] == 0x0ull) {
if (rnd_mode != ROUNDING_DOWN)
p_sign = 0x0000000000000000ull;
else
p_sign = 0x8000000000000000ull;
// the exponent is max (e4, expmin)
if (e4 < -6176)
e4 = expmin;
// assemble result
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49);
res.w[0] = 0x0;
*ptrres = res;
return;
}
}
// determine the number of decimal digits in R256
ind = nr_digits256 (R256);
// the exact result is (-1)^p_sign * R256 * 10^e4 where q (R256) = ind;
// round to the destination precision, with unbounded exponent
if (ind <= p34) {
// result rounded to the destination precision with unbounded exponent
// is exact
if (ind + e4 < p34 + expmin) {
is_tiny = 1; // applies to all rounding modes
}
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | R256.w[1];
res.w[0] = R256.w[0];
// Note: res is correct only if expmin <= e4 <= expmax
} else { // if (ind > p34)
// if more than P digits, round to nearest to P digits
// round R256 to p34 digits
x0 = ind - p34; // 1 <= x0 <= 34 as 35 <= ind <= 68
if (ind <= 38) {
P128.w[1] = R256.w[1];
P128.w[0] = R256.w[0];
round128_19_38 (ind, x0, P128, &R128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
} else if (ind <= 57) {
P192.w[2] = R256.w[2];
P192.w[1] = R256.w[1];
P192.w[0] = R256.w[0];
round192_39_57 (ind, x0, P192, &R192, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
R128.w[1] = R192.w[1];
R128.w[0] = R192.w[0];
} else { // if (ind <= 68)
round256_58_76 (ind, x0, R256, &R256, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
R128.w[1] = R256.w[1];
R128.w[0] = R256.w[0];
}
// the rounded result has p34 = 34 digits
e4 = e4 + x0 + incr_exp;
if (rnd_mode == ROUNDING_TO_NEAREST) {
if (e4 < expmin) {
is_tiny = 1; // for other rounding modes apply correction
}
} else {
// for RM, RP, RZ, RA apply correction in order to determine tininess
// but do not save the result; apply the correction to
// (-1)^p_sign * significand * 10^0
P128.w[1] = p_sign | 0x3040000000000000ull | R128.w[1];
P128.w[0] = R128.w[0];
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint, is_midpoint_lt_even,
is_midpoint_gt_even, 0, &P128, ptrfpsf);
scale = ((P128.w[1] & MASK_EXP) >> 49) - 6176; // -1, 0, or +1
// the number of digits in the significand is p34 = 34
if (e4 + scale < expmin) {
is_tiny = 1;
}
}
ind = p34; // the number of decimal digits in the signifcand of res
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | R128.w[1]; // RN
res.w[0] = R128.w[0];
// Note: res is correct only if expmin <= e4 <= expmax
// set the inexact flag after rounding with bounded exponent, if any
}
// at this point we have the result rounded with unbounded exponent in
// res and we know its tininess:
// res = (-1)^p_sign * significand * 10^e4,
// where q (significand) = ind <= p34
// Note: res is correct only if expmin <= e4 <= expmax
// check for overflow if RN
if (rnd_mode == ROUNDING_TO_NEAREST && (ind + e4) > (p34 + expmax)) {
res.w[1] = p_sign | 0x7800000000000000ull;
res.w[0] = 0x0000000000000000ull;
*ptrres = res;
*ptrfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
return; // BID_RETURN (res)
} // else not overflow or not RN, so continue
// if (e4 >= expmin) we have the result rounded with bounded exponent
if (e4 < expmin) {
x0 = expmin - e4; // x0 >= 1; the number of digits to chop off of res
// where the result rounded [at most] once is
// (-1)^p_sign * significand_res * 10^e4
// avoid double rounding error
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
if (x0 > ind) {
// nothing is left of res when moving the decimal point left x0 digits
is_inexact_lt_midpoint = 1;
res.w[1] = p_sign | 0x0000000000000000ull;
res.w[0] = 0x0000000000000000ull;
e4 = expmin;
} else if (x0 == ind) { // 1 <= x0 = ind <= p34 = 34
// this is <, =, or > 1/2 ulp
// compare the ind-digit value in the significand of res with
// 1/2 ulp = 5*10^(ind-1), i.e. determine whether it is
// less than, equal to, or greater than 1/2 ulp (significand of res)
R128.w[1] = res.w[1] & MASK_COEFF;
R128.w[0] = res.w[0];
if (ind <= 19) {
if (R128.w[0] < midpoint64[ind - 1]) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (R128.w[0] == midpoint64[ind - 1]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
// gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
} else { // if (ind <= 38) {
if (R128.w[1] < midpoint128[ind - 20].w[1] ||
(R128.w[1] == midpoint128[ind - 20].w[1] &&
R128.w[0] < midpoint128[ind - 20].w[0])) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (R128.w[1] == midpoint128[ind - 20].w[1] &&
R128.w[0] == midpoint128[ind - 20].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
// gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
}
if (lt_half_ulp || eq_half_ulp) {
// res = +0.0 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000000ull;
} else { // if (gt_half_ulp)
// res = +1 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000001ull;
}
res.w[1] = p_sign | res.w[1];
e4 = expmin;
} else { // if (1 <= x0 <= ind - 1 <= 33)
// round the ind-digit result to ind - x0 digits
if (ind <= 18) { // 2 <= ind <= 18
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
res.w[1] = 0x0;
res.w[0] = R64;
} else if (ind <= 38) {
P128.w[1] = res.w[1] & MASK_COEFF;
P128.w[0] = res.w[0];
round128_19_38 (ind, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
}
e4 = e4 + x0; // expmin
// we want the exponent to be expmin, so if incr_exp = 1 then
// multiply the rounded result by 10 - it will still fit in 113 bits
if (incr_exp) {
// 64 x 128 -> 128
P128.w[1] = res.w[1] & MASK_COEFF;
P128.w[0] = res.w[0];
__mul_64x128_to_128 (res, ten2k64[1], P128);
}
res.w[1] =
p_sign | ((UINT64) (e4 + 6176) << 49) | (res.w[1] & MASK_COEFF);
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// is not possible in Cases (2)-(6) or (15)-(17) which may get here
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
}
}
// res contains the correct result
// apply correction if not rounding to nearest
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint, is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, ptrfpsf);
}
if (is_midpoint_lt_even || is_midpoint_gt_even ||
is_inexact_lt_midpoint || is_inexact_gt_midpoint) {
// set the inexact flag
*ptrfpsf |= INEXACT_EXCEPTION;
if (is_tiny)
*ptrfpsf |= UNDERFLOW_EXCEPTION;
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
*ptrres = res;
return;
}
#if DECIMAL_CALL_BY_REFERENCE
static void
bid128_ext_fma (int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint, UINT128 * pres,
UINT128 * px, UINT128 * py,
UINT128 *
pz _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT128 x = *px, y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
static UINT128
bid128_ext_fma (int *ptr_is_midpoint_lt_even,
int *ptr_is_midpoint_gt_even,
int *ptr_is_inexact_lt_midpoint,
int *ptr_is_inexact_gt_midpoint, UINT128 x, UINT128 y,
UINT128 z _RND_MODE_PARAM _EXC_FLAGS_PARAM
_EXC_MASKS_PARAM _EXC_INFO_PARAM) {
#endif
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT64 x_sign, y_sign, z_sign, p_sign, tmp_sign;
UINT64 x_exp = 0, y_exp = 0, z_exp = 0, p_exp;
int true_p_exp;
UINT128 C1, C2, C3;
UINT256 C4;
int q1 = 0, q2 = 0, q3 = 0, q4;
int e1, e2, e3, e4;
int scale, ind, delta, x0;
int p34 = P34; // used to modify the limit on the number of digits
BID_UI64DOUBLE tmp;
int x_nr_bits, y_nr_bits, z_nr_bits;
unsigned int save_fpsf;
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0;
int is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
int is_midpoint_lt_even0 = 0, is_midpoint_gt_even0 = 0;
int is_inexact_lt_midpoint0 = 0, is_inexact_gt_midpoint0 = 0;
int incr_exp = 0;
int lsb;
int lt_half_ulp = 0;
int eq_half_ulp = 0;
int gt_half_ulp = 0;
int is_tiny = 0;
UINT64 R64, tmp64;
UINT128 P128, R128;
UINT192 P192, R192;
UINT256 R256;
// the following are based on the table of special cases for fma; the NaN
// behavior is similar to that of the IA-64 Architecture fma
// identify cases where at least one operand is NaN
BID_SWAP128 (x);
BID_SWAP128 (y);
BID_SWAP128 (z);
if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
// if x = {0, f, inf, NaN}, y = NaN, z = {0, f, inf, NaN} then res = Q (y)
// check first for non-canonical NaN payload
if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
(((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
(y.w[0] > 0x38c15b09ffffffffull))) {
y.w[1] = y.w[1] & 0xffffc00000000000ull;
y.w[0] = 0x0ull;
}
if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
// return quiet (y)
res.w[1] = y.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16]
res.w[0] = y.w[0];
} else { // y is QNaN
// return y
res.w[1] = y.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16]
res.w[0] = y.w[0];
// if z = SNaN or x = SNaN signal invalid exception
if ((z.w[1] & MASK_SNAN) == MASK_SNAN ||
(x.w[1] & MASK_SNAN) == MASK_SNAN) {
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
}
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((z.w[1] & MASK_NAN) == MASK_NAN) { // z is NAN
// if x = {0, f, inf, NaN}, y = {0, f, inf}, z = NaN then res = Q (z)
// check first for non-canonical NaN payload
if (((z.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
(((z.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
(z.w[0] > 0x38c15b09ffffffffull))) {
z.w[1] = z.w[1] & 0xffffc00000000000ull;
z.w[0] = 0x0ull;
}
if ((z.w[1] & MASK_SNAN) == MASK_SNAN) { // z is SNAN
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
// return quiet (z)
res.w[1] = z.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16]
res.w[0] = z.w[0];
} else { // z is QNaN
// return z
res.w[1] = z.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16]
res.w[0] = z.w[0];
// if x = SNaN signal invalid exception
if ((x.w[1] & MASK_SNAN) == MASK_SNAN) {
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
}
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
// if x = NaN, y = {0, f, inf}, z = {0, f, inf} then res = Q (x)
// check first for non-canonical NaN payload
if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
(((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
(x.w[0] > 0x38c15b09ffffffffull))) {
x.w[1] = x.w[1] & 0xffffc00000000000ull;
x.w[0] = 0x0ull;
}
if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
// return quiet (x)
res.w[1] = x.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16]
res.w[0] = x.w[0];
} else { // x is QNaN
// return x
res.w[1] = x.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16]
res.w[0] = x.w[0];
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// x, y, z are 0, f, or inf but not NaN => unpack the arguments and check
// for non-canonical values
x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
C1.w[1] = x.w[1] & MASK_COEFF;
C1.w[0] = x.w[0];
if ((x.w[1] & MASK_ANY_INF) != MASK_INF) { // x != inf
// if x is not infinity check for non-canonical values - treated as zero
if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11
// non-canonical
x_exp = (x.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits
C1.w[1] = 0; // significand high
C1.w[0] = 0; // significand low
} else { // G0_G1 != 11
x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bits
if (C1.w[1] > 0x0001ed09bead87c0ull ||
(C1.w[1] == 0x0001ed09bead87c0ull &&
C1.w[0] > 0x378d8e63ffffffffull)) {
// x is non-canonical if coefficient is larger than 10^34 -1
C1.w[1] = 0;
C1.w[0] = 0;
} else { // canonical
;
}
}
}
y_sign = y.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
C2.w[1] = y.w[1] & MASK_COEFF;
C2.w[0] = y.w[0];
if ((y.w[1] & MASK_ANY_INF) != MASK_INF) { // y != inf
// if y is not infinity check for non-canonical values - treated as zero
if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11
// non-canonical
y_exp = (y.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits
C2.w[1] = 0; // significand high
C2.w[0] = 0; // significand low
} else { // G0_G1 != 11
y_exp = y.w[1] & MASK_EXP; // biased and shifted left 49 bits
if (C2.w[1] > 0x0001ed09bead87c0ull ||
(C2.w[1] == 0x0001ed09bead87c0ull &&
C2.w[0] > 0x378d8e63ffffffffull)) {
// y is non-canonical if coefficient is larger than 10^34 -1
C2.w[1] = 0;
C2.w[0] = 0;
} else { // canonical
;
}
}
}
z_sign = z.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
C3.w[1] = z.w[1] & MASK_COEFF;
C3.w[0] = z.w[0];
if ((z.w[1] & MASK_ANY_INF) != MASK_INF) { // z != inf
// if z is not infinity check for non-canonical values - treated as zero
if ((z.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11
// non-canonical
z_exp = (z.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits
C3.w[1] = 0; // significand high
C3.w[0] = 0; // significand low
} else { // G0_G1 != 11
z_exp = z.w[1] & MASK_EXP; // biased and shifted left 49 bits
if (C3.w[1] > 0x0001ed09bead87c0ull ||
(C3.w[1] == 0x0001ed09bead87c0ull &&
C3.w[0] > 0x378d8e63ffffffffull)) {
// z is non-canonical if coefficient is larger than 10^34 -1
C3.w[1] = 0;
C3.w[0] = 0;
} else { // canonical
;
}
}
}
p_sign = x_sign ^ y_sign; // sign of the product
// identify cases where at least one operand is infinity
if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf
if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf
if (p_sign == z_sign) {
res.w[1] = z_sign | MASK_INF;
res.w[0] = 0x0;
} else {
// return QNaN Indefinite
res.w[1] = 0x7c00000000000000ull;
res.w[0] = 0x0000000000000000ull;
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
}
} else { // z = 0 or z = f
res.w[1] = p_sign | MASK_INF;
res.w[0] = 0x0;
}
} else if (C2.w[1] != 0 || C2.w[0] != 0) { // y = f
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf
if (p_sign == z_sign) {
res.w[1] = z_sign | MASK_INF;
res.w[0] = 0x0;
} else {
// return QNaN Indefinite
res.w[1] = 0x7c00000000000000ull;
res.w[0] = 0x0000000000000000ull;
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
}
} else { // z = 0 or z = f
res.w[1] = p_sign | MASK_INF;
res.w[0] = 0x0;
}
} else { // y = 0
// return QNaN Indefinite
res.w[1] = 0x7c00000000000000ull;
res.w[0] = 0x0000000000000000ull;
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf
// x = f, necessarily
if ((p_sign != z_sign)
|| (C1.w[1] == 0x0ull && C1.w[0] == 0x0ull)) {
// return QNaN Indefinite
res.w[1] = 0x7c00000000000000ull;
res.w[0] = 0x0000000000000000ull;
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
} else {
res.w[1] = z_sign | MASK_INF;
res.w[0] = 0x0;
}
} else if (C1.w[1] == 0x0 && C1.w[0] == 0x0) { // x = 0
// z = 0, f, inf
// return QNaN Indefinite
res.w[1] = 0x7c00000000000000ull;
res.w[0] = 0x0000000000000000ull;
// set invalid flag
*pfpsf |= INVALID_EXCEPTION;
} else {
// x = f and z = 0, f, necessarily
res.w[1] = p_sign | MASK_INF;
res.w[0] = 0x0;
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf
// x = 0, f and y = 0, f, necessarily
res.w[1] = z_sign | MASK_INF;
res.w[0] = 0x0;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
true_p_exp = (x_exp >> 49) - 6176 + (y_exp >> 49) - 6176;
if (true_p_exp < -6176)
p_exp = 0; // cannot be less than EXP_MIN
else
p_exp = (UINT64) (true_p_exp + 6176) << 49;
if (((C1.w[1] == 0x0 && C1.w[0] == 0x0) || (C2.w[1] == 0x0 && C2.w[0] == 0x0)) && C3.w[1] == 0x0 && C3.w[0] == 0x0) { // (x = 0 or y = 0) and z = 0
// the result is 0
if (p_exp < z_exp)
res.w[1] = p_exp; // preferred exponent
else
res.w[1] = z_exp; // preferred exponent
if (p_sign == z_sign) {
res.w[1] |= z_sign;
res.w[0] = 0x0;
} else { // x * y and z have opposite signs
if (rnd_mode == ROUNDING_DOWN) {
// res = -0.0
res.w[1] |= MASK_SIGN;
res.w[0] = 0x0;
} else {
// res = +0.0
// res.w[1] |= 0x0;
res.w[0] = 0x0;
}
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// from this point on, we may need to know the number of decimal digits
// in the significands of x, y, z when x, y, z != 0
if (C1.w[1] != 0 || C1.w[0] != 0) { // x = f (non-zero finite)
// q1 = nr. of decimal digits in x
// determine first the nr. of bits in x
if (C1.w[1] == 0) {
if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
// split the 64-bit value in two 32-bit halves to avoid rounding errors
if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
tmp.d = (double) (C1.w[0] >> 32); // exact conversion
x_nr_bits =
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
} else { // x < 2^32
tmp.d = (double) (C1.w[0]); // exact conversion
x_nr_bits =
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // if x < 2^53
tmp.d = (double) C1.w[0]; // exact conversion
x_nr_bits =
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
tmp.d = (double) C1.w[1]; // exact conversion
x_nr_bits =
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
q1 = nr_digits[x_nr_bits - 1].digits;
if (q1 == 0) {
q1 = nr_digits[x_nr_bits - 1].digits1;
if (C1.w[1] > nr_digits[x_nr_bits - 1].threshold_hi ||
(C1.w[1] == nr_digits[x_nr_bits - 1].threshold_hi &&
C1.w[0] >= nr_digits[x_nr_bits - 1].threshold_lo))
q1++;
}
}
if (C2.w[1] != 0 || C2.w[0] != 0) { // y = f (non-zero finite)
if (C2.w[1] == 0) {
if (C2.w[0] >= 0x0020000000000000ull) { // y >= 2^53
// split the 64-bit value in two 32-bit halves to avoid rounding errors
if (C2.w[0] >= 0x0000000100000000ull) { // y >= 2^32
tmp.d = (double) (C2.w[0] >> 32); // exact conversion
y_nr_bits =
32 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
} else { // y < 2^32
tmp.d = (double) C2.w[0]; // exact conversion
y_nr_bits =
((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // if y < 2^53
tmp.d = (double) C2.w[0]; // exact conversion
y_nr_bits =
((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // C2.w[1] != 0 => nr. bits = 64 + nr_bits (C2.w[1])
tmp.d = (double) C2.w[1]; // exact conversion
y_nr_bits =
64 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
q2 = nr_digits[y_nr_bits].digits;
if (q2 == 0) {
q2 = nr_digits[y_nr_bits].digits1;
if (C2.w[1] > nr_digits[y_nr_bits].threshold_hi ||
(C2.w[1] == nr_digits[y_nr_bits].threshold_hi &&
C2.w[0] >= nr_digits[y_nr_bits].threshold_lo))
q2++;
}
}
if (C3.w[1] != 0 || C3.w[0] != 0) { // z = f (non-zero finite)
if (C3.w[1] == 0) {
if (C3.w[0] >= 0x0020000000000000ull) { // z >= 2^53
// split the 64-bit value in two 32-bit halves to avoid rounding errors
if (C3.w[0] >= 0x0000000100000000ull) { // z >= 2^32
tmp.d = (double) (C3.w[0] >> 32); // exact conversion
z_nr_bits =
32 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
} else { // z < 2^32
tmp.d = (double) C3.w[0]; // exact conversion
z_nr_bits =
((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // if z < 2^53
tmp.d = (double) C3.w[0]; // exact conversion
z_nr_bits =
((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // C3.w[1] != 0 => nr. bits = 64 + nr_bits (C3.w[1])
tmp.d = (double) C3.w[1]; // exact conversion
z_nr_bits =
64 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
q3 = nr_digits[z_nr_bits].digits;
if (q3 == 0) {
q3 = nr_digits[z_nr_bits].digits1;
if (C3.w[1] > nr_digits[z_nr_bits].threshold_hi ||
(C3.w[1] == nr_digits[z_nr_bits].threshold_hi &&
C3.w[0] >= nr_digits[z_nr_bits].threshold_lo))
q3++;
}
}
if ((C1.w[1] == 0x0 && C1.w[0] == 0x0) ||
(C2.w[1] == 0x0 && C2.w[0] == 0x0)) {
// x = 0 or y = 0
// z = f, necessarily; for 0 + z return z, with the preferred exponent
// the result is z, but need to get the preferred exponent
if (z_exp <= p_exp) { // the preferred exponent is z_exp
res.w[1] = z_sign | (z_exp & MASK_EXP) | C3.w[1];
res.w[0] = C3.w[0];
} else { // if (p_exp < z_exp) the preferred exponent is p_exp
// return (C3 * 10^scale) * 10^(z_exp - scale)
// where scale = min (p34-q3, (z_exp-p_exp) >> 49)
scale = p34 - q3;
ind = (z_exp - p_exp) >> 49;
if (ind < scale)
scale = ind;
if (scale == 0) {
res.w[1] = z.w[1]; // & MASK_COEFF, which is redundant
res.w[0] = z.w[0];
} else if (q3 <= 19) { // z fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]);
}
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
}
// subtract scale from the exponent
z_exp = z_exp - ((UINT64) scale << 49);
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else {
; // continue with x = f, y = f, z = 0 or x = f, y = f, z = f
}
e1 = (x_exp >> 49) - 6176; // unbiased exponent of x
e2 = (y_exp >> 49) - 6176; // unbiased exponent of y
e3 = (z_exp >> 49) - 6176; // unbiased exponent of z
e4 = e1 + e2; // unbiased exponent of the exact x * y
// calculate C1 * C2 and its number of decimal digits, q4
// the exact product has either q1 + q2 - 1 or q1 + q2 decimal digits
// where 2 <= q1 + q2 <= 68
// calculate C4 = C1 * C2 and determine q
C4.w[3] = C4.w[2] = C4.w[1] = C4.w[0] = 0;
if (q1 + q2 <= 19) { // if 2 <= q1 + q2 <= 19, C4 = C1 * C2 fits in 64 bits
C4.w[0] = C1.w[0] * C2.w[0];
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2
if (C4.w[0] < ten2k64[q1 + q2 - 1])
q4 = q1 + q2 - 1; // q4 in [1, 18]
else
q4 = q1 + q2; // q4 in [2, 19]
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
} else if (q1 + q2 == 20) { // C4 = C1 * C2 fits in 64 or 128 bits
// q1 <= 19 and q2 <= 19 so both C1 and C2 fit in 64 bits
__mul_64x64_to_128MACH (C4, C1.w[0], C2.w[0]);
// if C4 < 10^(q1+q2-1) = 10^19 then q4 = q1+q2-1 = 19 else q4 = q1+q2 = 20
if (C4.w[1] == 0 && C4.w[0] < ten2k64[19]) { // 19 = q1+q2-1
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
q4 = 19; // 19 = q1 + q2 - 1
} else {
// if (C4.w[1] == 0)
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
q4 = 20; // 20 = q1 + q2
}
} else if (q1 + q2 <= 38) { // 21 <= q1 + q2 <= 38
// C4 = C1 * C2 fits in 64 or 128 bits
// (64 bits possibly, but only when q1 + q2 = 21 and C4 has 20 digits)
// at least one of C1, C2 has at most 19 decimal digits & fits in 64 bits
if (q1 <= 19) {
__mul_128x64_to_128 (C4, C1.w[0], C2);
} else { // q2 <= 19
__mul_128x64_to_128 (C4, C2.w[0], C1);
}
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2
if (C4.w[1] < ten2k128[q1 + q2 - 21].w[1] ||
(C4.w[1] == ten2k128[q1 + q2 - 21].w[1] &&
C4.w[0] < ten2k128[q1 + q2 - 21].w[0])) {
// if (C4.w[1] == 0) // q4 = 20, necessarily
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
q4 = q1 + q2 - 1; // q4 in [20, 37]
} else {
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
q4 = q1 + q2; // q4 in [21, 38]
}
} else if (q1 + q2 == 39) { // C4 = C1 * C2 fits in 128 or 192 bits
// both C1 and C2 fit in 128 bits (actually in 113 bits)
// may replace this by 128x128_to192
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] is 0
// if C4 < 10^(q1+q2-1) = 10^38 then q4 = q1+q2-1 = 38 else q4 = q1+q2 = 39
if (C4.w[2] == 0 && (C4.w[1] < ten2k128[18].w[1] ||
(C4.w[1] == ten2k128[18].w[1]
&& C4.w[0] < ten2k128[18].w[0]))) {
// 18 = 38 - 20 = q1+q2-1 - 20
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
q4 = 38; // 38 = q1 + q2 - 1
} else {
// if (C4.w[2] == 0)
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
q4 = 39; // 39 = q1 + q2
}
} else if (q1 + q2 <= 57) { // 40 <= q1 + q2 <= 57
// C4 = C1 * C2 fits in 128 or 192 bits
// (128 bits possibly, but only when q1 + q2 = 40 and C4 has 39 digits)
// both C1 and C2 fit in 128 bits (actually in 113 bits); at most one
// may fit in 64 bits
if (C1.w[1] == 0) { // C1 fits in 64 bits
// __mul_64x128_full (REShi64, RESlo128, A64, B128)
__mul_64x128_full (C4.w[2], C4, C1.w[0], C2);
} else if (C2.w[1] == 0) { // C2 fits in 64 bits
// __mul_64x128_full (REShi64, RESlo128, A64, B128)
__mul_64x128_full (C4.w[2], C4, C2.w[0], C1);
} else { // both C1 and C2 require 128 bits
// may use __mul_128x128_to_192 (C4.w[2], C4.w[0], C2.w[0], C1);
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] = 0
}
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2
if (C4.w[2] < ten2k256[q1 + q2 - 40].w[2] ||
(C4.w[2] == ten2k256[q1 + q2 - 40].w[2] &&
(C4.w[1] < ten2k256[q1 + q2 - 40].w[1] ||
(C4.w[1] == ten2k256[q1 + q2 - 40].w[1] &&
C4.w[0] < ten2k256[q1 + q2 - 40].w[0])))) {
// if (C4.w[2] == 0) // q4 = 39, necessarily
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
q4 = q1 + q2 - 1; // q4 in [39, 56]
} else {
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
q4 = q1 + q2; // q4 in [40, 57]
}
} else if (q1 + q2 == 58) { // C4 = C1 * C2 fits in 192 or 256 bits
// both C1 and C2 fit in 128 bits (actually in 113 bits); at most one
// may fit in 64 bits
if (C1.w[1] == 0) { // C1 * C2 will fit in 192 bits
__mul_64x128_full (C4.w[2], C4, C1.w[0], C2); // may use 64x128_to_192
} else if (C2.w[1] == 0) { // C1 * C2 will fit in 192 bits
__mul_64x128_full (C4.w[2], C4, C2.w[0], C1); // may use 64x128_to_192
} else { // C1 * C2 will fit in 192 bits or in 256 bits
__mul_128x128_to_256 (C4, C1, C2);
}
// if C4 < 10^(q1+q2-1) = 10^57 then q4 = q1+q2-1 = 57 else q4 = q1+q2 = 58
if (C4.w[3] == 0 && (C4.w[2] < ten2k256[18].w[2] ||
(C4.w[2] == ten2k256[18].w[2]
&& (C4.w[1] < ten2k256[18].w[1]
|| (C4.w[1] == ten2k256[18].w[1]
&& C4.w[0] < ten2k256[18].w[0]))))) {
// 18 = 57 - 39 = q1+q2-1 - 39
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
q4 = 57; // 57 = q1 + q2 - 1
} else {
// if (C4.w[3] == 0)
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
q4 = 58; // 58 = q1 + q2
}
} else { // if 59 <= q1 + q2 <= 68
// C4 = C1 * C2 fits in 192 or 256 bits
// (192 bits possibly, but only when q1 + q2 = 59 and C4 has 58 digits)
// both C1 and C2 fit in 128 bits (actually in 113 bits); none fits in
// 64 bits
// may use __mul_128x128_to_192 (C4.w[2], C4.w[0], C2.w[0], C1);
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] = 0
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2
if (C4.w[3] < ten2k256[q1 + q2 - 40].w[3] ||
(C4.w[3] == ten2k256[q1 + q2 - 40].w[3] &&
(C4.w[2] < ten2k256[q1 + q2 - 40].w[2] ||
(C4.w[2] == ten2k256[q1 + q2 - 40].w[2] &&
(C4.w[1] < ten2k256[q1 + q2 - 40].w[1] ||
(C4.w[1] == ten2k256[q1 + q2 - 40].w[1] &&
C4.w[0] < ten2k256[q1 + q2 - 40].w[0])))))) {
// if (C4.w[3] == 0) // q4 = 58, necessarily
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192;
// else
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
q4 = q1 + q2 - 1; // q4 in [58, 67]
} else {
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256;
q4 = q1 + q2; // q4 in [59, 68]
}
}
if (C3.w[1] == 0x0 && C3.w[0] == 0x0) { // x = f, y = f, z = 0
save_fpsf = *pfpsf; // sticky bits - caller value must be preserved
*pfpsf = 0;
if (q4 > p34) {
// truncate C4 to p34 digits into res
// x = q4-p34, 1 <= x <= 34 because 35 <= q4 <= 68
x0 = q4 - p34;
if (q4 <= 38) {
P128.w[1] = C4.w[1];
P128.w[0] = C4.w[0];
round128_19_38 (q4, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
} else if (q4 <= 57) { // 35 <= q4 <= 57
P192.w[2] = C4.w[2];
P192.w[1] = C4.w[1];
P192.w[0] = C4.w[0];
round192_39_57 (q4, x0, P192, &R192, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
res.w[0] = R192.w[0];
res.w[1] = R192.w[1];
} else { // if (q4 <= 68)
round256_58_76 (q4, x0, C4, &R256, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
res.w[0] = R256.w[0];
res.w[1] = R256.w[1];
}
e4 = e4 + x0;
if (incr_exp) {
e4 = e4 + 1;
}
q4 = p34;
// res is now the coefficient of the result rounded to the destination
// precision, with unbounded exponent; the exponent is e4; q4=digits(res)
} else { // if (q4 <= p34)
// C4 * 10^e4 is the result rounded to the destination precision, with
// unbounded exponent (which is exact)
if ((q4 + e4 <= p34 + expmax) && (e4 > expmax)) {
// e4 is too large, but can be brought within range by scaling up C4
scale = e4 - expmax; // 1 <= scale < P-q4 <= P-1 => 1 <= scale <= P-2
// res = (C4 * 10^scale) * 10^expmax
if (q4 <= 19) { // C4 fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C4.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C4.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C4.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C4.w[0], ten2k128[scale - 20]);
}
} else { // C4 fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * CC43
__mul_128x64_to_128 (res, ten2k64[scale], C4);
}
e4 = e4 - scale; // expmax
q4 = q4 + scale;
} else {
res.w[1] = C4.w[1];
res.w[0] = C4.w[0];
}
// res is the coefficient of the result rounded to the destination
// precision, with unbounded exponent (it has q4 digits); the exponent
// is e4 (exact result)
}
// check for overflow
if (q4 + e4 > p34 + expmax) {
if (rnd_mode == ROUNDING_TO_NEAREST) {
res.w[1] = p_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
} else {
res.w[1] = p_sign | res.w[1];
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, pfpsf);
}
*pfpsf |= save_fpsf;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// check for underflow
if (q4 + e4 < expmin + P34) {
is_tiny = 1; // the result is tiny
if (e4 < expmin) {
// if e4 < expmin, we must truncate more of res
x0 = expmin - e4; // x0 >= 1
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
// the number of decimal digits in res is q4
if (x0 < q4) { // 1 <= x0 <= q4-1 => round res to q4 - x0 digits
if (q4 <= 18) { // 2 <= q4 <= 18, 1 <= x0 <= 17
round64_2_18 (q4, x0, res.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
if (incr_exp) {
// R64 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 17
R64 = ten2k64[q4 - x0];
}
// res.w[1] = 0; (from above)
res.w[0] = R64;
} else { // if (q4 <= 34)
// 19 <= q4 <= 38
P128.w[1] = res.w[1];
P128.w[0] = res.w[0];
round128_19_38 (q4, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
if (incr_exp) {
// increase coefficient by a factor of 10; this will be <= 10^33
// R128 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 37
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19
// res.w[1] = 0;
res.w[0] = ten2k64[q4 - x0];
} else { // 20 <= q4 - x0 <= 37
res.w[0] = ten2k128[q4 - x0 - 20].w[0];
res.w[1] = ten2k128[q4 - x0 - 20].w[1];
}
}
}
e4 = e4 + x0; // expmin
} else if (x0 == q4) {
// the second rounding is for 0.d(0)d(1)...d(q4-1) * 10^emin
// determine relationship with 1/2 ulp
if (q4 <= 19) {
if (res.w[0] < midpoint64[q4 - 1]) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (res.w[0] == midpoint64[q4 - 1]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
// gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
} else { // if (q4 <= 34)
if (res.w[1] < midpoint128[q4 - 20].w[1] ||
(res.w[1] == midpoint128[q4 - 20].w[1] &&
res.w[0] < midpoint128[q4 - 20].w[0])) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (res.w[1] == midpoint128[q4 - 20].w[1] &&
res.w[0] == midpoint128[q4 - 20].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
// gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
}
if (lt_half_ulp || eq_half_ulp) {
// res = +0.0 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000000ull;
} else { // if (gt_half_ulp)
// res = +1 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000001ull;
}
e4 = expmin;
} else { // if (x0 > q4)
// the second rounding is for 0.0...d(0)d(1)...d(q4-1) * 10^emin
res.w[1] = 0;
res.w[0] = 0;
e4 = expmin;
is_inexact_lt_midpoint = 1;
}
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// not possible for f * f + 0
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
} else { // if e4 >= emin then q4 < P and the result is tiny and exact
if (e3 < e4) {
// if (e3 < e4) the preferred exponent is e3
// return (C4 * 10^scale) * 10^(e4 - scale)
// where scale = min (p34-q4, (e4 - e3))
scale = p34 - q4;
ind = e4 - e3;
if (ind < scale)
scale = ind;
if (scale == 0) {
; // res and e4 are unchanged
} else if (q4 <= 19) { // C4 fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 res.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, res.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 res.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, res.w[0], ten2k128[scale - 20]);
}
} else { // res fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], res);
}
// subtract scale from the exponent
e4 = e4 - scale;
}
}
// check for inexact result
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
// set the inexact flag and the underflow flag
*pfpsf |= INEXACT_EXCEPTION;
*pfpsf |= UNDERFLOW_EXCEPTION;
}
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1];
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, pfpsf);
}
*pfpsf |= save_fpsf;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// no overflow, and no underflow for rounding to nearest
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1];
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, pfpsf);
// if e4 = expmin && significand < 10^33 => result is tiny (for RD, RZ)
if (e4 == expmin) {
if ((res.w[1] & MASK_COEFF) < 0x0000314dc6448d93ull ||
((res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull &&
res.w[0] < 0x38c15b0a00000000ull)) {
is_tiny = 1;
}
}
}
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
if (is_tiny)
*pfpsf |= UNDERFLOW_EXCEPTION;
}
if ((*pfpsf & INEXACT_EXCEPTION) == 0) { // x * y is exact
// need to ensure that the result has the preferred exponent
p_exp = res.w[1] & MASK_EXP;
if (z_exp < p_exp) { // the preferred exponent is z_exp
// signficand of res in C3
C3.w[1] = res.w[1] & MASK_COEFF;
C3.w[0] = res.w[0];
// the number of decimal digits of x * y is q4 <= 34
// Note: the coefficient fits in 128 bits
// return (C3 * 10^scale) * 10^(p_exp - scale)
// where scale = min (p34-q4, (p_exp-z_exp) >> 49)
scale = p34 - q4;
ind = (p_exp - z_exp) >> 49;
if (ind < scale)
scale = ind;
// subtract scale from the exponent
p_exp = p_exp - ((UINT64) scale << 49);
if (scale == 0) {
; // leave res unchanged
} else if (q4 <= 19) { // x * y fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]);
}
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1];
} else { // x * y fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1];
}
} // else leave the result as it is, because p_exp <= z_exp
}
*pfpsf |= save_fpsf;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} // else we have f * f + f
// continue with x = f, y = f, z = f
delta = q3 + e3 - q4 - e4;
delta_ge_zero:
if (delta >= 0) {
if (p34 <= delta - 1 || // Case (1')
(p34 == delta && e3 + 6176 < p34 - q3)) { // Case (1''A)
// check for overflow, which can occur only in Case (1')
if ((q3 + e3) > (p34 + expmax) && p34 <= delta - 1) {
// e3 > expmax implies p34 <= delta-1 and e3 > expmax is a necessary
// condition for (q3 + e3) > (p34 + expmax)
if (rnd_mode == ROUNDING_TO_NEAREST) {
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
} else {
if (p_sign == z_sign) {
is_inexact_lt_midpoint = 1;
} else {
is_inexact_gt_midpoint = 1;
}
// q3 <= p34; if (q3 < p34) scale C3 up by 10^(p34-q3)
scale = p34 - q3;
if (scale == 0) {
res.w[1] = z_sign | C3.w[1];
res.w[0] = C3.w[0];
} else {
if (q3 <= 19) { // C3 fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0],
ten2k128[scale - 20]);
}
} else { // C3 fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
}
// the coefficient in res has q3 + scale = p34 digits
}
e3 = e3 - scale;
res.w[1] = z_sign | res.w[1];
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e3, &res, pfpsf);
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// res = z
if (q3 < p34) { // the preferred exponent is z_exp - (p34 - q3)
// return (C3 * 10^scale) * 10^(z_exp - scale)
// where scale = min (p34-q3, z_exp-EMIN)
scale = p34 - q3;
ind = e3 + 6176;
if (ind < scale)
scale = ind;
if (scale == 0) {
res.w[1] = C3.w[1];
res.w[0] = C3.w[0];
} else if (q3 <= 19) { // z fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]);
}
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
}
// the coefficient in res has q3 + scale digits
// subtract scale from the exponent
z_exp = z_exp - ((UINT64) scale << 49);
e3 = e3 - scale;
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
if (scale + q3 < p34)
*pfpsf |= UNDERFLOW_EXCEPTION;
} else {
scale = 0;
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | C3.w[1];
res.w[0] = C3.w[0];
}
// use the following to avoid double rounding errors when operating on
// mixed formats in rounding to nearest, and for correcting the result
// if not rounding to nearest
if ((p_sign != z_sign) && (delta == (q3 + scale + 1))) {
// there is a gap of exactly one digit between the scaled C3 and C4
// C3 * 10^ scale = 10^(q3+scale-1) <=> C3 = 10^(q3-1) is special case
if ((q3 <= 19 && C3.w[0] != ten2k64[q3 - 1]) ||
(q3 == 20 && (C3.w[1] != 0 || C3.w[0] != ten2k64[19])) ||
(q3 >= 21 && (C3.w[1] != ten2k128[q3 - 21].w[1] ||
C3.w[0] != ten2k128[q3 - 21].w[0]))) {
// C3 * 10^ scale != 10^(q3-1)
// if ((res.w[1] & MASK_COEFF) != 0x0000314dc6448d93ull ||
// res.w[0] != 0x38c15b0a00000000ull) { // C3 * 10^scale != 10^33
is_inexact_gt_midpoint = 1; // if (z_sign), set as if for abs. value
} else { // if C3 * 10^scale = 10^(q3+scale-1)
// ok from above e3 = (z_exp >> 49) - 6176;
// the result is always inexact
if (q4 == 1) {
R64 = C4.w[0];
} else {
// if q4 > 1 then truncate C4 from q4 digits to 1 digit;
// x = q4-1, 1 <= x <= 67 and check if this operation is exact
if (q4 <= 18) { // 2 <= q4 <= 18
round64_2_18 (q4, q4 - 1, C4.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
} else if (q4 <= 38) {
P128.w[1] = C4.w[1];
P128.w[0] = C4.w[0];
round128_19_38 (q4, q4 - 1, P128, &R128, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R128.w[0]; // one decimal digit
} else if (q4 <= 57) {
P192.w[2] = C4.w[2];
P192.w[1] = C4.w[1];
P192.w[0] = C4.w[0];
round192_39_57 (q4, q4 - 1, P192, &R192, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R192.w[0]; // one decimal digit
} else { // if (q4 <= 68)
round256_58_76 (q4, q4 - 1, C4, &R256, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R256.w[0]; // one decimal digit
}
if (incr_exp) {
R64 = 10;
}
}
if (q4 == 1 && C4.w[0] == 5) {
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 1;
is_midpoint_gt_even = 0;
} else if ((e3 == expmin) ||
R64 < 5 || (R64 == 5 && is_inexact_gt_midpoint)) {
// result does not change
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
// result decremented is 10^(q3+scale) - 1
if ((q3 + scale) <= 19) {
res.w[1] = 0;
res.w[0] = ten2k64[q3 + scale];
} else { // if ((q3 + scale + 1) <= 35)
res.w[1] = ten2k128[q3 + scale - 20].w[1];
res.w[0] = ten2k128[q3 + scale - 20].w[0];
}
res.w[0] = res.w[0] - 1; // borrow never occurs
z_exp = z_exp - EXP_P1;
e3 = e3 - 1;
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1];
}
if (e3 == expmin) {
if (R64 < 5 || (R64 == 5 && !is_inexact_lt_midpoint)) {
; // result not tiny (in round-to-nearest mode)
} else {
*pfpsf |= UNDERFLOW_EXCEPTION;
}
}
} // end 10^(q3+scale-1)
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
} else {
if (p_sign == z_sign) {
// if (z_sign), set as if for absolute value
is_inexact_lt_midpoint = 1;
} else { // if (p_sign != z_sign)
// if (z_sign), set as if for absolute value
is_inexact_gt_midpoint = 1;
}
*pfpsf |= INEXACT_EXCEPTION;
}
// the result is always inexact => set the inexact flag
// Determine tininess:
// if (exp > expmin)
// the result is not tiny
// else // if exp = emin
// if (q3 + scale < p34)
// the result is tiny
// else // if (q3 + scale = p34)
// if (C3 * 10^scale > 10^33)
// the result is not tiny
// else // if C3 * 10^scale = 10^33
// if (xy * z > 0)
// the result is not tiny
// else // if (xy * z < 0)
// if (z > 0)
// if rnd_mode != RP
// the result is tiny
// else // if RP
// the result is not tiny
// else // if (z < 0)
// if rnd_mode != RM
// the result is tiny
// else // if RM
// the result is not tiny
// endif
// endif
// endif
// endif
// endif
// endif
if ((e3 == expmin && (q3 + scale) < p34) ||
(e3 == expmin && (q3 + scale) == p34 &&
(res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull && // 10^33_high
res.w[0] == 0x38c15b0a00000000ull && // 10^33_low
z_sign != p_sign && ((!z_sign && rnd_mode != ROUNDING_UP) ||
(z_sign && rnd_mode != ROUNDING_DOWN)))) {
*pfpsf |= UNDERFLOW_EXCEPTION;
}
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e3, &res, pfpsf);
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if (p34 == delta) { // Case (1''B)
// because Case (1''A) was treated above, e3 + 6176 >= p34 - q3
// and C3 can be scaled up to p34 digits if needed
// scale C3 to p34 digits if needed
scale = p34 - q3; // 0 <= scale <= p34 - 1
if (scale == 0) {
res.w[1] = C3.w[1];
res.w[0] = C3.w[0];
} else if (q3 <= 19) { // z fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]);
}
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
}
// subtract scale from the exponent
z_exp = z_exp - ((UINT64) scale << 49);
e3 = e3 - scale;
// now z_sign, z_exp, and res correspond to a z scaled to p34 = 34 digits
// determine whether x * y is less than, equal to, or greater than
// 1/2 ulp (z)
if (q4 <= 19) {
if (C4.w[0] < midpoint64[q4 - 1]) { // < 1/2 ulp
lt_half_ulp = 1;
} else if (C4.w[0] == midpoint64[q4 - 1]) { // = 1/2 ulp
eq_half_ulp = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
}
} else if (q4 <= 38) {
if (C4.w[2] == 0 && (C4.w[1] < midpoint128[q4 - 20].w[1] ||
(C4.w[1] == midpoint128[q4 - 20].w[1] &&
C4.w[0] < midpoint128[q4 - 20].w[0]))) { // < 1/2 ulp
lt_half_ulp = 1;
} else if (C4.w[2] == 0 && C4.w[1] == midpoint128[q4 - 20].w[1] &&
C4.w[0] == midpoint128[q4 - 20].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
}
} else if (q4 <= 58) {
if (C4.w[3] == 0 && (C4.w[2] < midpoint192[q4 - 39].w[2] ||
(C4.w[2] == midpoint192[q4 - 39].w[2] &&
C4.w[1] < midpoint192[q4 - 39].w[1]) ||
(C4.w[2] == midpoint192[q4 - 39].w[2] &&
C4.w[1] == midpoint192[q4 - 39].w[1] &&
C4.w[0] < midpoint192[q4 - 39].w[0]))) { // < 1/2 ulp
lt_half_ulp = 1;
} else if (C4.w[3] == 0 && C4.w[2] == midpoint192[q4 - 39].w[2] &&
C4.w[1] == midpoint192[q4 - 39].w[1] &&
C4.w[0] == midpoint192[q4 - 39].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
}
} else {
if (C4.w[3] < midpoint256[q4 - 59].w[3] ||
(C4.w[3] == midpoint256[q4 - 59].w[3] &&
C4.w[2] < midpoint256[q4 - 59].w[2]) ||
(C4.w[3] == midpoint256[q4 - 59].w[3] &&
C4.w[2] == midpoint256[q4 - 59].w[2] &&
C4.w[1] < midpoint256[q4 - 59].w[1]) ||
(C4.w[3] == midpoint256[q4 - 59].w[3] &&
C4.w[2] == midpoint256[q4 - 59].w[2] &&
C4.w[1] == midpoint256[q4 - 59].w[1] &&
C4.w[0] < midpoint256[q4 - 59].w[0])) { // < 1/2 ulp
lt_half_ulp = 1;
} else if (C4.w[3] == midpoint256[q4 - 59].w[3] &&
C4.w[2] == midpoint256[q4 - 59].w[2] &&
C4.w[1] == midpoint256[q4 - 59].w[1] &&
C4.w[0] == midpoint256[q4 - 59].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
}
}
if (p_sign == z_sign) {
if (lt_half_ulp) {
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
// use the following to avoid double rounding errors when operating on
// mixed formats in rounding to nearest
is_inexact_lt_midpoint = 1; // if (z_sign), as if for absolute value
} else if ((eq_half_ulp && (res.w[0] & 0x01)) || gt_half_ulp) {
// add 1 ulp to the significand
res.w[0]++;
if (res.w[0] == 0x0ull)
res.w[1]++;
// check for rounding overflow, when coeff == 10^34
if ((res.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull &&
res.w[0] == 0x378d8e6400000000ull) { // coefficient = 10^34
e3 = e3 + 1;
// coeff = 10^33
z_exp = ((UINT64) (e3 + 6176) << 49) & MASK_EXP;
res.w[1] = 0x0000314dc6448d93ull;
res.w[0] = 0x38c15b0a00000000ull;
}
// end add 1 ulp
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
if (eq_half_ulp) {
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value
} else {
is_inexact_gt_midpoint = 1; // if (z_sign), as if for absolute value
}
} else { // if (eq_half_ulp && !(res.w[0] & 0x01))
// leave unchanged
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
is_midpoint_gt_even = 1; // if (z_sign), as if for absolute value
}
// the result is always inexact, and never tiny
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
// check for overflow
if (e3 > expmax && rnd_mode == ROUNDING_TO_NEAREST) {
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e3, &res, pfpsf);
z_exp = res.w[1] & MASK_EXP;
}
} else { // if (p_sign != z_sign)
// consider two cases, because C3 * 10^scale = 10^33 is a special case
if (res.w[1] != 0x0000314dc6448d93ull ||
res.w[0] != 0x38c15b0a00000000ull) { // C3 * 10^scale != 10^33
if (lt_half_ulp) {
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
// use the following to avoid double rounding errors when operating
// on mixed formats in rounding to nearest
is_inexact_gt_midpoint = 1; // if (z_sign), as if for absolute value
} else if ((eq_half_ulp && (res.w[0] & 0x01)) || gt_half_ulp) {
// subtract 1 ulp from the significand
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
if (eq_half_ulp) {
is_midpoint_gt_even = 1; // if (z_sign), as if for absolute value
} else {
is_inexact_lt_midpoint = 1; //if(z_sign), as if for absolute value
}
} else { // if (eq_half_ulp && !(res.w[0] & 0x01))
// leave unchanged
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value
}
// the result is always inexact, and never tiny
// check for overflow for RN
if (e3 > expmax) {
if (rnd_mode == ROUNDING_TO_NEAREST) {
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
} else {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even,
is_midpoint_gt_even, e3, &res,
pfpsf);
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even,
is_midpoint_gt_even, e3, &res, pfpsf);
}
z_exp = res.w[1] & MASK_EXP;
} else { // if C3 * 10^scale = 10^33
e3 = (z_exp >> 49) - 6176;
if (e3 > expmin) {
// the result is exact if exp > expmin and C4 = d*10^(q4-1),
// where d = 1, 2, 3, ..., 9; it could be tiny too, but exact
if (q4 == 1) {
// if q4 = 1 the result is exact
// result coefficient = 10^34 - C4
res.w[1] = 0x0001ed09bead87c0ull;
res.w[0] = 0x378d8e6400000000ull - C4.w[0];
z_exp = z_exp - EXP_P1;
e3 = e3 - 1;
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
} else {
// if q4 > 1 then truncate C4 from q4 digits to 1 digit;
// x = q4-1, 1 <= x <= 67 and check if this operation is exact
if (q4 <= 18) { // 2 <= q4 <= 18
round64_2_18 (q4, q4 - 1, C4.w[0], &R64, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
} else if (q4 <= 38) {
P128.w[1] = C4.w[1];
P128.w[0] = C4.w[0];
round128_19_38 (q4, q4 - 1, P128, &R128, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R128.w[0]; // one decimal digit
} else if (q4 <= 57) {
P192.w[2] = C4.w[2];
P192.w[1] = C4.w[1];
P192.w[0] = C4.w[0];
round192_39_57 (q4, q4 - 1, P192, &R192, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R192.w[0]; // one decimal digit
} else { // if (q4 <= 68)
round256_58_76 (q4, q4 - 1, C4, &R256, &incr_exp,
&is_midpoint_lt_even,
&is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R64 = R256.w[0]; // one decimal digit
}
if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// the result is exact: 10^34 - R64
// incr_exp = 0 with certainty
z_exp = z_exp - EXP_P1;
e3 = e3 - 1;
res.w[1] =
z_sign | (z_exp & MASK_EXP) | 0x0001ed09bead87c0ull;
res.w[0] = 0x378d8e6400000000ull - R64;
} else {
// We want R64 to be the top digit of C4, but we actually
// obtained (C4 * 10^(-q4+1))RN; a correction may be needed,
// because the top digit is (C4 * 10^(-q4+1))RZ
// however, if incr_exp = 1 then R64 = 10 with certainty
if (incr_exp) {
R64 = 10;
}
// the result is inexact as C4 has more than 1 significant digit
// and C3 * 10^scale = 10^33
// example of case that is treated here:
// 100...0 * 10^e3 - 0.41 * 10^e3 =
// 0999...9.59 * 10^e3 -> rounds to 99...96*10^(e3-1)
// note that (e3 > expmin}
// in order to round, subtract R64 from 10^34 and then compare
// C4 - R64 * 10^(q4-1) with 1/2 ulp
// calculate 10^34 - R64
res.w[1] = 0x0001ed09bead87c0ull;
res.w[0] = 0x378d8e6400000000ull - R64;
z_exp = z_exp - EXP_P1; // will be OR-ed with sign & significand
// calculate C4 - R64 * 10^(q4-1); this is a rare case and
// R64 is small, 1 <= R64 <= 9
e3 = e3 - 1;
if (is_inexact_lt_midpoint) {
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
} else if (is_inexact_gt_midpoint) {
is_inexact_gt_midpoint = 0;
is_inexact_lt_midpoint = 1;
} else if (is_midpoint_lt_even) {
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 1;
} else if (is_midpoint_gt_even) {
is_midpoint_gt_even = 0;
is_midpoint_lt_even = 1;
} else {
;
}
// the result is always inexact, and never tiny
// check for overflow for RN
if (e3 > expmax) {
if (rnd_mode == ROUNDING_TO_NEAREST) {
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
} else {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even,
is_midpoint_gt_even, e3, &res,
pfpsf);
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
res.w[1] =
z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1];
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even,
is_midpoint_gt_even, e3, &res,
pfpsf);
}
z_exp = res.w[1] & MASK_EXP;
} // end result is inexact
} // end q4 > 1
} else { // if (e3 = emin)
// if e3 = expmin the result is also tiny (the condition for
// tininess is C4 > 050...0 [q4 digits] which is met because
// the msd of C4 is not zero)
// the result is tiny and inexact in all rounding modes;
// it is either 100...0 or 0999...9 (use lt_half_ulp, eq_half_ulp,
// gt_half_ulp to calculate)
// if (lt_half_ulp || eq_half_ulp) res = 10^33 stays unchanged
// p_sign != z_sign so swap gt_half_ulp and lt_half_ulp
if (gt_half_ulp) { // res = 10^33 - 1
res.w[1] = 0x0000314dc6448d93ull;
res.w[0] = 0x38c15b09ffffffffull;
} else {
res.w[1] = 0x0000314dc6448d93ull;
res.w[0] = 0x38c15b0a00000000ull;
}
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
*pfpsf |= UNDERFLOW_EXCEPTION; // inexact is set later
if (eq_half_ulp) {
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value
} else if (lt_half_ulp) {
is_inexact_gt_midpoint = 1; //if(z_sign), as if for absolute value
} else { // if (gt_half_ulp)
is_inexact_lt_midpoint = 1; //if(z_sign), as if for absolute value
}
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even,
is_midpoint_gt_even, e3, &res,
pfpsf);
z_exp = res.w[1] & MASK_EXP;
}
} // end e3 = emin
// set the inexact flag (if the result was not exact)
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even)
*pfpsf |= INEXACT_EXCEPTION;
} // end 10^33
} // end if (p_sign != z_sign)
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1];
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if (((q3 <= delta && delta < p34 && p34 < delta + q4) || // Case (2)
(q3 <= delta && delta + q4 <= p34) || // Case (3)
(delta < q3 && p34 < delta + q4) || // Case (4)
(delta < q3 && q3 <= delta + q4 && delta + q4 <= p34) || // Case (5)
(delta + q4 < q3)) && // Case (6)
!(delta <= 1 && p_sign != z_sign)) { // Case (2), (3), (4), (5) or (6)
// the result has the sign of z
if ((q3 <= delta && delta < p34 && p34 < delta + q4) || // Case (2)
(delta < q3 && p34 < delta + q4)) { // Case (4)
// round first the sum x * y + z with unbounded exponent
// scale C3 up by scale = p34 - q3, 1 <= scale <= p34-1,
// 1 <= scale <= 33
// calculate res = C3 * 10^scale
scale = p34 - q3;
x0 = delta + q4 - p34;
} else if (delta + q4 < q3) { // Case (6)
// make Case (6) look like Case (3) or Case (5) with scale = 0
// by scaling up C4 by 10^(q3 - delta - q4)
scale = q3 - delta - q4; // 1 <= scale <= 33
if (q4 <= 19) { // 1 <= scale <= 19; C4 fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C4.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (P128, C4.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C4.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (P128, C4.w[0], ten2k128[scale - 20]);
}
} else { // C4 fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C4
__mul_128x64_to_128 (P128, ten2k64[scale], C4);
}
C4.w[0] = P128.w[0];
C4.w[1] = P128.w[1];
// e4 does not need adjustment, as it is not used from this point on
scale = 0;
x0 = 0;
// now Case (6) looks like Case (3) or Case (5) with scale = 0
} else { // if Case (3) or Case (5)
// Note: Case (3) is similar to Case (2), but scale differs and the
// result is exact, unless it is tiny (so x0 = 0 when calculating the
// result with unbounded exponent)
// calculate first the sum x * y + z with unbounded exponent (exact)
// scale C3 up by scale = delta + q4 - q3, 1 <= scale <= p34-1,
// 1 <= scale <= 33
// calculate res = C3 * 10^scale
scale = delta + q4 - q3;
x0 = 0;
// Note: the comments which follow refer [mainly] to Case (2)]
}
case2_repeat:
if (scale == 0) { // this could happen e.g. if we return to case2_repeat
// or in Case (4)
res.w[1] = C3.w[1];
res.w[0] = C3.w[0];
} else if (q3 <= 19) { // 1 <= scale <= 19; z fits in 64 bits
if (scale <= 19) { // 10^scale fits in 64 bits
// 64 x 64 C3.w[0] * ten2k64[scale]
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]);
} else { // 10^scale fits in 128 bits
// 64 x 128 C3.w[0] * ten2k128[scale - 20]
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]);
}
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits
// 64 x 128 ten2k64[scale] * C3
__mul_128x64_to_128 (res, ten2k64[scale], C3);
}
// e3 is already calculated
e3 = e3 - scale;
// now res = C3 * 10^scale and e3 = e3 - scale
// Note: C3 * 10^scale could be 10^34 if we returned to case2_repeat
// because the result was too small
// round C4 to nearest to q4 - x0 digits, where x0 = delta + q4 - p34,
// 1 <= x0 <= min (q4 - 1, 2 * p34 - 1) <=> 1 <= x0 <= min (q4 - 1, 67)
// Also: 1 <= q4 - x0 <= p34 -1 => 1 <= q4 - x0 <= 33 (so the result of
// the rounding fits in 128 bits!)
// x0 = delta + q4 - p34 (calculated before reaching case2_repeat)
// because q3 + q4 - x0 <= P => x0 >= q3 + q4 - p34
if (x0 == 0) { // this could happen only if we return to case2_repeat, or
// for Case (3) or Case (6)
R128.w[1] = C4.w[1];
R128.w[0] = C4.w[0];
} else if (q4 <= 18) {
// 2 <= q4 <= 18, max(1, q3+q4-p34) <= x0 <= q4 - 1, 1 <= x0 <= 17
round64_2_18 (q4, x0, C4.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
if (incr_exp) {
// R64 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 17
R64 = ten2k64[q4 - x0];
}
R128.w[1] = 0;
R128.w[0] = R64;
} else if (q4 <= 38) {
// 19 <= q4 <= 38, max(1, q3+q4-p34) <= x0 <= q4 - 1, 1 <= x0 <= 37
P128.w[1] = C4.w[1];
P128.w[0] = C4.w[0];
round128_19_38 (q4, x0, P128, &R128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
if (incr_exp) {
// R128 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 37
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19
R128.w[0] = ten2k64[q4 - x0];
// R128.w[1] stays 0
} else { // 20 <= q4 - x0 <= 37
R128.w[0] = ten2k128[q4 - x0 - 20].w[0];
R128.w[1] = ten2k128[q4 - x0 - 20].w[1];
}
}
} else if (q4 <= 57) {
// 38 <= q4 <= 57, max(1, q3+q4-p34) <= x0 <= q4 - 1, 5 <= x0 <= 56
P192.w[2] = C4.w[2];
P192.w[1] = C4.w[1];
P192.w[0] = C4.w[0];
round192_39_57 (q4, x0, P192, &R192, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
// R192.w[2] is always 0
if (incr_exp) {
// R192 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 5, 1 <= q4 - x0 <= 52
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19
R192.w[0] = ten2k64[q4 - x0];
// R192.w[1] stays 0
// R192.w[2] stays 0
} else { // 20 <= q4 - x0 <= 33
R192.w[0] = ten2k128[q4 - x0 - 20].w[0];
R192.w[1] = ten2k128[q4 - x0 - 20].w[1];
// R192.w[2] stays 0
}
}
R128.w[1] = R192.w[1];
R128.w[0] = R192.w[0];
} else {
// 58 <= q4 <= 68, max(1, q3+q4-p34) <= x0 <= q4 - 1, 25 <= x0 <= 67
round256_58_76 (q4, x0, C4, &R256, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
// R256.w[3] and R256.w[2] are always 0
if (incr_exp) {
// R256 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 25, 1 <= q4 - x0 <= 43
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19
R256.w[0] = ten2k64[q4 - x0];
// R256.w[1] stays 0
// R256.w[2] stays 0
// R256.w[3] stays 0
} else { // 20 <= q4 - x0 <= 33
R256.w[0] = ten2k128[q4 - x0 - 20].w[0];
R256.w[1] = ten2k128[q4 - x0 - 20].w[1];
// R256.w[2] stays 0
// R256.w[3] stays 0
}
}
R128.w[1] = R256.w[1];
R128.w[0] = R256.w[0];
}
// now add C3 * 10^scale in res and the signed top (q4-x0) digits of C4,
// rounded to nearest, which were copied into R128
if (z_sign == p_sign) {
lsb = res.w[0] & 0x01; // lsb of C3 * 10^scale
// the sum can result in [up to] p34 or p34 + 1 digits
res.w[0] = res.w[0] + R128.w[0];
res.w[1] = res.w[1] + R128.w[1];
if (res.w[0] < R128.w[0])
res.w[1]++; // carry
// if res > 10^34 - 1 need to increase x0 and decrease scale by 1
if (res.w[1] > 0x0001ed09bead87c0ull ||
(res.w[1] == 0x0001ed09bead87c0ull &&
res.w[0] > 0x378d8e63ffffffffull)) {
// avoid double rounding error
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
P128.w[1] = res.w[1];
P128.w[0] = res.w[0];
round128_19_38 (35, 1, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
// incr_exp is 0 with certainty in this case
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// not possible in Cases (2)-(6) or (15)-(17) which may get here
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint
&& !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0
|| is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0
|| is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
// adjust exponent
e3 = e3 + 1;
if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
if (is_midpoint_lt_even0 || is_midpoint_gt_even0 ||
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0) {
is_inexact_lt_midpoint = 1;
}
}
} else {
// this is the result rounded with unbounded exponent, unless a
// correction is needed
res.w[1] = res.w[1] & MASK_COEFF;
if (lsb == 1) {
if (is_midpoint_gt_even) {
// res = res + 1
is_midpoint_gt_even = 0;
is_midpoint_lt_even = 1;
res.w[0]++;
if (res.w[0] == 0x0)
res.w[1]++;
// check for rounding overflow
if (res.w[1] == 0x0001ed09bead87c0ull &&
res.w[0] == 0x378d8e6400000000ull) {
// res = 10^34 => rounding overflow
res.w[1] = 0x0000314dc6448d93ull;
res.w[0] = 0x38c15b0a00000000ull; // 10^33
e3++;
}
} else if (is_midpoint_lt_even) {
// res = res - 1
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 1;
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// if the result is pure zero, the sign depends on the rounding
// mode (x*y and z had opposite signs)
if (res.w[1] == 0x0ull && res.w[0] == 0x0ull) {
if (rnd_mode != ROUNDING_DOWN)
z_sign = 0x0000000000000000ull;
else
z_sign = 0x8000000000000000ull;
// the exponent is max (e3, expmin)
res.w[1] = 0x0;
res.w[0] = 0x0;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
} else {
;
}
}
}
} else { // if (z_sign != p_sign)
lsb = res.w[0] & 0x01; // lsb of C3 * 10^scale; R128 contains rounded C4
// used to swap rounding indicators if p_sign != z_sign
// the sum can result in [up to] p34 or p34 - 1 digits
tmp64 = res.w[0];
res.w[0] = res.w[0] - R128.w[0];
res.w[1] = res.w[1] - R128.w[1];
if (res.w[0] > tmp64)
res.w[1]--; // borrow
// if res < 10^33 and exp > expmin need to decrease x0 and
// increase scale by 1
if (e3 > expmin && ((res.w[1] < 0x0000314dc6448d93ull ||
(res.w[1] == 0x0000314dc6448d93ull &&
res.w[0] < 0x38c15b0a00000000ull)) ||
(is_inexact_lt_midpoint
&& res.w[1] == 0x0000314dc6448d93ull
&& res.w[0] == 0x38c15b0a00000000ull))
&& x0 >= 1) {
x0 = x0 - 1;
// first restore e3, otherwise it will be too small
e3 = e3 + scale;
scale = scale + 1;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
incr_exp = 0;
goto case2_repeat;
}
// else this is the result rounded with unbounded exponent;
// because the result has opposite sign to that of C4 which was
// rounded, need to change the rounding indicators
if (is_inexact_lt_midpoint) {
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
} else if (is_inexact_gt_midpoint) {
is_inexact_gt_midpoint = 0;
is_inexact_lt_midpoint = 1;
} else if (lsb == 0) {
if (is_midpoint_lt_even) {
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 1;
} else if (is_midpoint_gt_even) {
is_midpoint_gt_even = 0;
is_midpoint_lt_even = 1;
} else {
;
}
} else if (lsb == 1) {
if (is_midpoint_lt_even) {
// res = res + 1
res.w[0]++;
if (res.w[0] == 0x0)
res.w[1]++;
// check for rounding overflow
if (res.w[1] == 0x0001ed09bead87c0ull &&
res.w[0] == 0x378d8e6400000000ull) {
// res = 10^34 => rounding overflow
res.w[1] = 0x0000314dc6448d93ull;
res.w[0] = 0x38c15b0a00000000ull; // 10^33
e3++;
}
} else if (is_midpoint_gt_even) {
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// if the result is pure zero, the sign depends on the rounding
// mode (x*y and z had opposite signs)
if (res.w[1] == 0x0ull && res.w[0] == 0x0ull) {
if (rnd_mode != ROUNDING_DOWN)
z_sign = 0x0000000000000000ull;
else
z_sign = 0x8000000000000000ull;
// the exponent is max (e3, expmin)
res.w[1] = 0x0;
res.w[0] = 0x0;
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
} else {
;
}
} else {
;
}
}
// check for underflow
if (e3 == expmin) { // and if significand < 10^33 => result is tiny
if ((res.w[1] & MASK_COEFF) < 0x0000314dc6448d93ull ||
((res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull &&
res.w[0] < 0x38c15b0a00000000ull)) {
is_tiny = 1;
}
} else if (e3 < expmin) {
// the result is tiny, so we must truncate more of res
is_tiny = 1;
x0 = expmin - e3;
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
// determine the number of decimal digits in res
if (res.w[1] == 0x0) {
// between 1 and 19 digits
for (ind = 1; ind <= 19; ind++) {
if (res.w[0] < ten2k64[ind]) {
break;
}
}
// ind digits
} else if (res.w[1] < ten2k128[0].w[1] ||
(res.w[1] == ten2k128[0].w[1]
&& res.w[0] < ten2k128[0].w[0])) {
// 20 digits
ind = 20;
} else { // between 21 and 38 digits
for (ind = 1; ind <= 18; ind++) {
if (res.w[1] < ten2k128[ind].w[1] ||
(res.w[1] == ten2k128[ind].w[1] &&
res.w[0] < ten2k128[ind].w[0])) {
break;
}
}
// ind + 20 digits
ind = ind + 20;
}
// at this point ind >= x0; because delta >= 2 on this path, the case
// ind = x0 can occur only in Case (2) or case (3), when C3 has one
// digit (q3 = 1) equal to 1 (C3 = 1), e3 is expmin (e3 = expmin),
// the signs of x * y and z are opposite, and through cancellation
// the most significant decimal digit in res has the weight
// 10^(emin-1); however, it is clear that in this case the most
// significant digit is 9, so the result before rounding is
// 0.9... * 10^emin
// Otherwise, ind > x0 because there are non-zero decimal digits in the
// result with weight of at least 10^emin, and correction for underflow
// can be carried out using the round*_*_2_* () routines
if (x0 == ind) { // the result before rounding is 0.9... * 10^emin
res.w[1] = 0x0;
res.w[0] = 0x1;
is_inexact_gt_midpoint = 1;
} else if (ind <= 18) { // check that 2 <= ind
// 2 <= ind <= 18, 1 <= x0 <= 17
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
if (incr_exp) {
// R64 = 10^(ind-x0), 1 <= ind - x0 <= ind - 1, 1 <= ind - x0 <= 17
R64 = ten2k64[ind - x0];
}
res.w[1] = 0;
res.w[0] = R64;
} else if (ind <= 38) {
// 19 <= ind <= 38
P128.w[1] = res.w[1];
P128.w[0] = res.w[0];
round128_19_38 (ind, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
if (incr_exp) {
// R128 = 10^(ind-x0), 1 <= ind - x0 <= ind - 1, 1 <= ind - x0 <= 37
if (ind - x0 <= 19) { // 1 <= ind - x0 <= 19
res.w[0] = ten2k64[ind - x0];
// res.w[1] stays 0
} else { // 20 <= ind - x0 <= 37
res.w[0] = ten2k128[ind - x0 - 20].w[0];
res.w[1] = ten2k128[ind - x0 - 20].w[1];
}
}
}
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// not possible in Cases (2)-(6) which may get here
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
// adjust exponent
e3 = e3 + x0;
if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
if (is_midpoint_lt_even0 || is_midpoint_gt_even0 ||
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0) {
is_inexact_lt_midpoint = 1;
}
}
} else {
; // not underflow
}
// check for inexact result
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
if (is_tiny)
*pfpsf |= UNDERFLOW_EXCEPTION;
}
// now check for significand = 10^34 (may have resulted from going
// back to case2_repeat)
if (res.w[1] == 0x0001ed09bead87c0ull &&
res.w[0] == 0x378d8e6400000000ull) { // if res = 10^34
res.w[1] = 0x0000314dc6448d93ull; // res = 10^33
res.w[0] = 0x38c15b0a00000000ull;
e3 = e3 + 1;
}
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1];
// check for overflow
if (rnd_mode == ROUNDING_TO_NEAREST && e3 > expmax) {
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
}
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e3, &res, pfpsf);
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else {
// we get here only if delta <= 1 in Cases (2), (3), (4), (5), or (6) and
// the signs of x*y and z are opposite; in these cases massive
// cancellation can occur, so it is better to scale either C3 or C4 and
// to perform the subtraction before rounding; rounding is performed
// next, depending on the number of decimal digits in the result and on
// the exponent value
// Note: overlow is not possible in this case
// this is similar to Cases (15), (16), and (17)
if (delta + q4 < q3) { // from Case (6)
// Case (6) with 0<= delta <= 1 is similar to Cases (15), (16), and
// (17) if we swap (C3, C4), (q3, q4), (e3, e4), (z_sign, p_sign)
// and call add_and_round; delta stays positive
// C4.w[3] = 0 and C4.w[2] = 0, so swap just the low part of C4 with C3
P128.w[1] = C3.w[1];
P128.w[0] = C3.w[0];
C3.w[1] = C4.w[1];
C3.w[0] = C4.w[0];
C4.w[1] = P128.w[1];
C4.w[0] = P128.w[0];
ind = q3;
q3 = q4;
q4 = ind;
ind = e3;
e3 = e4;
e4 = ind;
tmp_sign = z_sign;
z_sign = p_sign;
p_sign = tmp_sign;
} else { // from Cases (2), (3), (4), (5)
// In Cases (2), (3), (4), (5) with 0 <= delta <= 1 C3 has to be
// scaled up by q4 + delta - q3; this is the same as in Cases (15),
// (16), and (17) if we just change the sign of delta
delta = -delta;
}
add_and_round (q3, q4, e4, delta, p34, z_sign, p_sign, C3, C4,
rnd_mode, &is_midpoint_lt_even,
&is_midpoint_gt_even, &is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, pfpsf, &res);
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
} else { // if delta < 0
delta = -delta;
if (p34 < q4 && q4 <= delta) { // Case (7)
// truncate C4 to p34 digits into res
// x = q4-p34, 1 <= x <= 34 because 35 <= q4 <= 68
x0 = q4 - p34;
if (q4 <= 38) {
P128.w[1] = C4.w[1];
P128.w[0] = C4.w[0];
round128_19_38 (q4, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
} else if (q4 <= 57) { // 35 <= q4 <= 57
P192.w[2] = C4.w[2];
P192.w[1] = C4.w[1];
P192.w[0] = C4.w[0];
round192_39_57 (q4, x0, P192, &R192, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
res.w[0] = R192.w[0];
res.w[1] = R192.w[1];
} else { // if (q4 <= 68)
round256_58_76 (q4, x0, C4, &R256, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
res.w[0] = R256.w[0];
res.w[1] = R256.w[1];
}
e4 = e4 + x0;
if (incr_exp) {
e4 = e4 + 1;
}
if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if C4 rounded to p34 digits is exact then the result is inexact,
// in a way that depends on the signs of x * y and z
if (p_sign == z_sign) {
is_inexact_lt_midpoint = 1;
} else { // if (p_sign != z_sign)
if (res.w[1] != 0x0000314dc6448d93ull ||
res.w[0] != 0x38c15b0a00000000ull) { // res != 10^33
is_inexact_gt_midpoint = 1;
} else { // res = 10^33 and exact is a special case
// if C3 < 1/2 ulp then res = 10^33 and is_inexact_gt_midpoint = 1
// if C3 = 1/2 ulp then res = 10^33 and is_midpoint_lt_even = 1
// if C3 > 1/2 ulp then res = 10^34-1 and is_inexact_lt_midpoint = 1
// Note: ulp is really ulp/10 (after borrow which propagates to msd)
if (delta > p34 + 1) { // C3 < 1/2
// res = 10^33, unchanged
is_inexact_gt_midpoint = 1;
} else { // if (delta == p34 + 1)
if (q3 <= 19) {
if (C3.w[0] < midpoint64[q3 - 1]) { // C3 < 1/2 ulp
// res = 10^33, unchanged
is_inexact_gt_midpoint = 1;
} else if (C3.w[0] == midpoint64[q3 - 1]) { // C3 = 1/2 ulp
// res = 10^33, unchanged
is_midpoint_lt_even = 1;
} else { // if (C3.w[0] > midpoint64[q3-1]), C3 > 1/2 ulp
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1
res.w[0] = 0x378d8e63ffffffffull;
e4 = e4 - 1;
is_inexact_lt_midpoint = 1;
}
} else { // if (20 <= q3 <=34)
if (C3.w[1] < midpoint128[q3 - 20].w[1] ||
(C3.w[1] == midpoint128[q3 - 20].w[1] &&
C3.w[0] < midpoint128[q3 - 20].w[0])) { // C3 < 1/2 ulp
// res = 10^33, unchanged
is_inexact_gt_midpoint = 1;
} else if (C3.w[1] == midpoint128[q3 - 20].w[1] &&
C3.w[0] == midpoint128[q3 - 20].w[0]) { // C3 = 1/2 ulp
// res = 10^33, unchanged
is_midpoint_lt_even = 1;
} else { // if (C3 > midpoint128[q3-20]), C3 > 1/2 ulp
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1
res.w[0] = 0x378d8e63ffffffffull;
e4 = e4 - 1;
is_inexact_lt_midpoint = 1;
}
}
}
}
}
} else if (is_midpoint_lt_even) {
if (z_sign != p_sign) {
// needs correction: res = res - 1
res.w[0] = res.w[0] - 1;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// if it is (10^33-1)*10^e4 then the corect result is
// (10^34-1)*10(e4-1)
if (res.w[1] == 0x0000314dc6448d93ull &&
res.w[0] == 0x38c15b09ffffffffull) {
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1
res.w[0] = 0x378d8e63ffffffffull;
e4 = e4 - 1;
}
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else { // if (z_sign == p_sign)
is_midpoint_lt_even = 0;
is_inexact_gt_midpoint = 1;
}
} else if (is_midpoint_gt_even) {
if (z_sign == p_sign) {
// needs correction: res = res + 1 (cannot cross in the next binade)
res.w[0] = res.w[0] + 1;
if (res.w[0] == 0x0000000000000000ull)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else { // if (z_sign != p_sign)
is_midpoint_gt_even = 0;
is_inexact_lt_midpoint = 1;
}
} else {
; // the rounded result is already correct
}
// check for overflow
if (rnd_mode == ROUNDING_TO_NEAREST && e4 > expmax) {
res.w[1] = p_sign | 0x7800000000000000ull;
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (OVERFLOW_EXCEPTION | INEXACT_EXCEPTION);
} else { // no overflow or not RN
p_exp = ((UINT64) (e4 + 6176) << 49);
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1];
}
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, pfpsf);
}
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((q4 <= p34 && p34 <= delta) || // Case (8)
(q4 <= delta && delta < p34 && p34 < delta + q3) || // Case (9)
(q4 <= delta && delta + q3 <= p34) || // Case (10)
(delta < q4 && q4 <= p34 && p34 < delta + q3) || // Case (13)
(delta < q4 && q4 <= delta + q3 && delta + q3 <= p34) || // Case (14)
(delta + q3 < q4 && q4 <= p34)) { // Case (18)
// Case (8) is similar to Case (1), with C3 and C4 swapped
// Case (9) is similar to Case (2), with C3 and C4 swapped
// Case (10) is similar to Case (3), with C3 and C4 swapped
// Case (13) is similar to Case (4), with C3 and C4 swapped
// Case (14) is similar to Case (5), with C3 and C4 swapped
// Case (18) is similar to Case (6), with C3 and C4 swapped
// swap (C3, C4), (q3, q4), (e3, 34), (z_sign, p_sign), (z_exp, p_exp)
// and go back to delta_ge_zero
// C4.w[3] = 0 and C4.w[2] = 0, so swap just the low part of C4 with C3
P128.w[1] = C3.w[1];
P128.w[0] = C3.w[0];
C3.w[1] = C4.w[1];
C3.w[0] = C4.w[0];
C4.w[1] = P128.w[1];
C4.w[0] = P128.w[0];
ind = q3;
q3 = q4;
q4 = ind;
ind = e3;
e3 = e4;
e4 = ind;
tmp_sign = z_sign;
z_sign = p_sign;
p_sign = tmp_sign;
tmp.ui64 = z_exp;
z_exp = p_exp;
p_exp = tmp.ui64;
goto delta_ge_zero;
} else if ((p34 <= delta && delta < q4 && q4 < delta + q3) || // Case (11)
(delta < p34 && p34 < q4 && q4 < delta + q3)) { // Case (12)
// round C3 to nearest to q3 - x0 digits, where x0 = e4 - e3,
// 1 <= x0 <= q3 - 1 <= p34 - 1
x0 = e4 - e3; // or x0 = delta + q3 - q4
if (q3 <= 18) { // 2 <= q3 <= 18
round64_2_18 (q3, x0, C3.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
// C3.w[1] = 0;
C3.w[0] = R64;
} else if (q3 <= 38) {
round128_19_38 (q3, x0, C3, &R128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
C3.w[1] = R128.w[1];
C3.w[0] = R128.w[0];
}
// the rounded result has q3 - x0 digits
// we want the exponent to be e4, so if incr_exp = 1 then
// multiply the rounded result by 10 - it will still fit in 113 bits
if (incr_exp) {
// 64 x 128 -> 128
P128.w[1] = C3.w[1];
P128.w[0] = C3.w[0];
__mul_64x128_to_128 (C3, ten2k64[1], P128);
}
e3 = e3 + x0; // this is e4
// now add/subtract the 256-bit C4 and the new (and shorter) 128-bit C3;
// the result will have the sign of x * y; the exponent is e4
R256.w[3] = 0;
R256.w[2] = 0;
R256.w[1] = C3.w[1];
R256.w[0] = C3.w[0];
if (p_sign == z_sign) { // R256 = C4 + R256
add256 (C4, R256, &R256);
} else { // if (p_sign != z_sign) { // R256 = C4 - R256
sub256 (C4, R256, &R256); // the result cannot be pure zero
// because the result has opposite sign to that of R256 which was
// rounded, need to change the rounding indicators
lsb = C4.w[0] & 0x01;
if (is_inexact_lt_midpoint) {
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
} else if (is_inexact_gt_midpoint) {
is_inexact_gt_midpoint = 0;
is_inexact_lt_midpoint = 1;
} else if (lsb == 0) {
if (is_midpoint_lt_even) {
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 1;
} else if (is_midpoint_gt_even) {
is_midpoint_gt_even = 0;
is_midpoint_lt_even = 1;
} else {
;
}
} else if (lsb == 1) {
if (is_midpoint_lt_even) {
// res = res + 1
R256.w[0]++;
if (R256.w[0] == 0x0) {
R256.w[1]++;
if (R256.w[1] == 0x0) {
R256.w[2]++;
if (R256.w[2] == 0x0) {
R256.w[3]++;
}
}
}
// no check for rounding overflow - R256 was a difference
} else if (is_midpoint_gt_even) {
// res = res - 1
R256.w[0]--;
if (R256.w[0] == 0xffffffffffffffffull) {
R256.w[1]--;
if (R256.w[1] == 0xffffffffffffffffull) {
R256.w[2]--;
if (R256.w[2] == 0xffffffffffffffffull) {
R256.w[3]--;
}
}
}
} else {
;
}
} else {
;
}
}
// determine the number of decimal digits in R256
ind = nr_digits256 (R256); // ind >= p34
// if R256 is sum, then ind > p34; if R256 is a difference, then
// ind >= p34; this means that we can calculate the result rounded to
// the destination precision, with unbounded exponent, starting from R256
// and using the indicators from the rounding of C3 to avoid a double
// rounding error
if (ind < p34) {
;
} else if (ind == p34) {
// the result rounded to the destination precision with
// unbounded exponent
// is (-1)^p_sign * R256 * 10^e4
res.w[1] = R256.w[1];
res.w[0] = R256.w[0];
} else { // if (ind > p34)
// if more than P digits, round to nearest to P digits
// round R256 to p34 digits
x0 = ind - p34; // 1 <= x0 <= 34 as 35 <= ind <= 68
// save C3 rounding indicators to help avoid double rounding error
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
// initialize rounding indicators
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
// round to p34 digits; the result fits in 113 bits
if (ind <= 38) {
P128.w[1] = R256.w[1];
P128.w[0] = R256.w[0];
round128_19_38 (ind, x0, P128, &R128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
} else if (ind <= 57) {
P192.w[2] = R256.w[2];
P192.w[1] = R256.w[1];
P192.w[0] = R256.w[0];
round192_39_57 (ind, x0, P192, &R192, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R128.w[1] = R192.w[1];
R128.w[0] = R192.w[0];
} else { // if (ind <= 68)
round256_58_76 (ind, x0, R256, &R256, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
R128.w[1] = R256.w[1];
R128.w[0] = R256.w[0];
}
// the rounded result has p34 = 34 digits
e4 = e4 + x0 + incr_exp;
res.w[1] = R128.w[1];
res.w[0] = R128.w[0];
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// not possible in Cases (2)-(6) or (15)-(17) which may get here
// if this is 10^33 - 1 make it 10^34 - 1 and decrement exponent
if (res.w[1] == 0x0000314dc6448d93ull &&
res.w[0] == 0x38c15b09ffffffffull) { // 10^33 - 1
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1
res.w[0] = 0x378d8e63ffffffffull;
e4--;
}
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
}
// determine tininess
if (rnd_mode == ROUNDING_TO_NEAREST) {
if (e4 < expmin) {
is_tiny = 1; // for other rounding modes apply correction
}
} else {
// for RM, RP, RZ, RA apply correction in order to determine tininess
// but do not save the result; apply the correction to
// (-1)^p_sign * res * 10^0
P128.w[1] = p_sign | 0x3040000000000000ull | res.w[1];
P128.w[0] = res.w[0];
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
0, &P128, pfpsf);
scale = ((P128.w[1] & MASK_EXP) >> 49) - 6176; // -1, 0, or +1
// the number of digits in the significand is p34 = 34
if (e4 + scale < expmin) {
is_tiny = 1;
}
}
// the result rounded to the destination precision with unbounded exponent
// is (-1)^p_sign * res * 10^e4
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1]; // RN
// res.w[0] unchanged;
// Note: res is correct only if expmin <= e4 <= expmax
ind = p34; // the number of decimal digits in the signifcand of res
// at this point we have the result rounded with unbounded exponent in
// res and we know its tininess:
// res = (-1)^p_sign * significand * 10^e4,
// where q (significand) = ind = p34
// Note: res is correct only if expmin <= e4 <= expmax
// check for overflow if RN
if (rnd_mode == ROUNDING_TO_NEAREST
&& (ind + e4) > (p34 + expmax)) {
res.w[1] = p_sign | 0x7800000000000000ull;
res.w[0] = 0x0000000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} // else not overflow or not RN, so continue
// from this point on this is similar to the last part of the computation
// for Cases (15), (16), (17)
// if (e4 >= expmin) we have the result rounded with bounded exponent
if (e4 < expmin) {
x0 = expmin - e4; // x0 >= 1; the number of digits to chop off of res
// where the result rounded [at most] once is
// (-1)^p_sign * significand_res * 10^e4
// avoid double rounding error
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
if (x0 > ind) {
// nothing is left of res when moving the decimal point left x0 digits
is_inexact_lt_midpoint = 1;
res.w[1] = p_sign | 0x0000000000000000ull;
res.w[0] = 0x0000000000000000ull;
e4 = expmin;
} else if (x0 == ind) { // 1 <= x0 = ind <= p34 = 34
// this is <, =, or > 1/2 ulp
// compare the ind-digit value in the significand of res with
// 1/2 ulp = 5*10^(ind-1), i.e. determine whether it is
// less than, equal to, or greater than 1/2 ulp (significand of res)
R128.w[1] = res.w[1] & MASK_COEFF;
R128.w[0] = res.w[0];
if (ind <= 19) {
if (R128.w[0] < midpoint64[ind - 1]) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (R128.w[0] == midpoint64[ind - 1]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
} else { // if (ind <= 38)
if (R128.w[1] < midpoint128[ind - 20].w[1] ||
(R128.w[1] == midpoint128[ind - 20].w[1] &&
R128.w[0] < midpoint128[ind - 20].w[0])) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (R128.w[1] == midpoint128[ind - 20].w[1] &&
R128.w[0] == midpoint128[ind - 20].w[0]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
}
if (lt_half_ulp || eq_half_ulp) {
// res = +0.0 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000000ull;
} else { // if (gt_half_ulp)
// res = +1 * 10^expmin
res.w[1] = 0x0000000000000000ull;
res.w[0] = 0x0000000000000001ull;
}
res.w[1] = p_sign | res.w[1];
e4 = expmin;
} else { // if (1 <= x0 <= ind - 1 <= 33)
// round the ind-digit result to ind - x0 digits
if (ind <= 18) { // 2 <= ind <= 18
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
res.w[1] = 0x0;
res.w[0] = R64;
} else if (ind <= 38) {
P128.w[1] = res.w[1] & MASK_COEFF;
P128.w[0] = res.w[0];
round128_19_38 (ind, x0, P128, &res, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint);
}
e4 = e4 + x0; // expmin
// we want the exponent to be expmin, so if incr_exp = 1 then
// multiply the rounded result by 10 - it will still fit in 113 bits
if (incr_exp) {
// 64 x 128 -> 128
P128.w[1] = res.w[1] & MASK_COEFF;
P128.w[0] = res.w[0];
__mul_64x128_to_128 (res, ten2k64[1], P128);
}
res.w[1] =
p_sign | ((UINT64) (e4 + 6176) << 49) | (res.
w[1] & MASK_COEFF);
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res.w[0]--;
if (res.w[0] == 0xffffffffffffffffull)
res.w[1]--;
// Note: a double rounding error upward is not possible; for this
// the result after the first rounding would have to be 99...95
// (35 digits in all), possibly followed by a number of zeros; this
// not possible in this underflow case
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res.w[0]++;
if (res.w[0] == 0)
res.w[1]++;
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint
&& !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0
|| is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0
|| is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
}
}
// res contains the correct result
// apply correction if not rounding to nearest
if (rnd_mode != ROUNDING_TO_NEAREST) {
rounding_correction (rnd_mode,
is_inexact_lt_midpoint,
is_inexact_gt_midpoint,
is_midpoint_lt_even, is_midpoint_gt_even,
e4, &res, pfpsf);
}
if (is_midpoint_lt_even || is_midpoint_gt_even ||
is_inexact_lt_midpoint || is_inexact_gt_midpoint) {
// set the inexact flag
*pfpsf |= INEXACT_EXCEPTION;
if (is_tiny)
*pfpsf |= UNDERFLOW_EXCEPTION;
}
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else if ((p34 <= delta && delta + q3 <= q4) || // Case (15)
(delta < p34 && p34 < delta + q3 && delta + q3 <= q4) || //Case (16)
(delta + q3 <= p34 && p34 < q4)) { // Case (17)
// calculate first the result rounded to the destination precision, with
// unbounded exponent
add_and_round (q3, q4, e4, delta, p34, z_sign, p_sign, C3, C4,
rnd_mode, &is_midpoint_lt_even,
&is_midpoint_gt_even, &is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, pfpsf, &res);
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
} else {
;
}
} // end if delta < 0
*ptr_is_midpoint_lt_even = is_midpoint_lt_even;
*ptr_is_midpoint_gt_even = is_midpoint_gt_even;
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint;
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint;
BID_SWAP128 (res);
BID_RETURN (res)
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128_fma (UINT128 * pres, UINT128 * px, UINT128 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT128 x = *px, y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128_fma (UINT128 x, UINT128 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even, is_midpoint_gt_even,
is_inexact_lt_midpoint, is_inexact_gt_midpoint;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
#if DECIMAL_CALL_BY_REFERENCE
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, &x, &y, &z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x, y,
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128ddd_fma (UINT128 * pres, UINT64 * px, UINT64 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px, y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128ddd_fma (UINT64 x, UINT64 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 x1, y1, z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, &x1, &y1, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x1, y1,
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128ddq_fma (UINT128 * pres, UINT64 * px, UINT64 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px, y = *py;
UINT128 z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128ddq_fma (UINT64 x, UINT64 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 x1, y1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, &x1, &y1, &z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x1, y1,
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128dqd_fma (UINT128 * pres, UINT64 * px, UINT128 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128dqd_fma (UINT64 x, UINT128 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 x1, z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, &x1, py, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x1, y,
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128dqq_fma (UINT128 * pres, UINT64 * px, UINT128 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128dqq_fma (UINT64 x, UINT128 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 x1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, &x1, py, pz
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x1, y,
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128qdd_fma (UINT128 * pres, UINT128 * px, UINT64 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128qdd_fma (UINT128 x, UINT64 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 y1, z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, px, &y1, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x, y1,
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128qdq_fma (UINT128 * pres, UINT128 * px, UINT64 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 y = *py;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128qdq_fma (UINT128 x, UINT64 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 y1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, px, &y1, pz
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x, y1,
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid128qqd_fma (UINT128 * pres, UINT128 * px, UINT128 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT128
bid128qqd_fma (UINT128 x, UINT128 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint,
&res, px, py, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint,
&is_inexact_gt_midpoint, x, y,
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res);
}
// Note: bid128qqq_fma is represented by bid128_fma
// Note: bid64ddd_fma is represented by bid64_fma
#if DECIMAL_CALL_BY_REFERENCE
void
bid64ddq_fma (UINT64 * pres, UINT64 * px, UINT64 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px, y = *py;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64ddq_fma (UINT64 x, UINT64 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 x1, y1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, &x1, &y1, pz
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x1, y1, z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64dqd_fma (UINT64 * pres, UINT64 * px, UINT128 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64dqd_fma (UINT64 x, UINT128 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 x1, z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, &x1, py, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x1, y, z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64dqq_fma (UINT64 * pres, UINT64 * px, UINT128 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 x = *px;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64dqq_fma (UINT64 x, UINT128 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 x1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, &x1, py, pz
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x1, y, z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64qdd_fma (UINT64 * pres, UINT128 * px, UINT64 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64qdd_fma (UINT128 x, UINT64 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 y1, z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, px, &y1, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x, y1, z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64qdq_fma (UINT64 * pres, UINT128 * px, UINT64 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 y = *py;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64qdq_fma (UINT128 x, UINT64 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 y1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, px, &y1, pz
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x, y1, z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64qqd_fma (UINT64 * pres, UINT128 * px, UINT128 * py, UINT64 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT64 z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64qqd_fma (UINT128 x, UINT128 y, UINT64 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
UINT64 res1 = 0xbaddbaddbaddbaddull;
UINT128 z1;
#if DECIMAL_CALL_BY_REFERENCE
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
bid64qqq_fma (&res1, px, py, &z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG);
res1 = bid64qqq_fma (x, y, z1
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
BID_RETURN (res1);
}
#if DECIMAL_CALL_BY_REFERENCE
void
bid64qqq_fma (UINT64 * pres, UINT128 * px, UINT128 * py, UINT128 * pz
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
UINT128 x = *px, y = *py, z = *pz;
#if !DECIMAL_GLOBAL_ROUNDING
unsigned int rnd_mode = *prnd_mode;
#endif
#else
UINT64
bid64qqq_fma (UINT128 x, UINT128 y, UINT128 z
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
_EXC_INFO_PARAM) {
#endif
int is_midpoint_lt_even0 = 0, is_midpoint_gt_even0 = 0,
is_inexact_lt_midpoint0 = 0, is_inexact_gt_midpoint0 = 0;
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0,
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0;
int incr_exp;
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT128 res128 = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} };
UINT64 res1 = 0xbaddbaddbaddbaddull;
unsigned int save_fpsf; // needed because of the call to bid128_ext_fma
UINT64 sign;
UINT64 exp;
int unbexp;
UINT128 C;
BID_UI64DOUBLE tmp;
int nr_bits;
int q, x0;
int scale;
int lt_half_ulp = 0, eq_half_ulp = 0;
// Note: for rounding modes other than RN or RA, the result can be obtained
// by rounding first to BID128 and then to BID64
save_fpsf = *pfpsf; // sticky bits - caller value must be preserved
*pfpsf = 0;
#if DECIMAL_CALL_BY_REFERENCE
bid128_ext_fma (&is_midpoint_lt_even0, &is_midpoint_gt_even0,
&is_inexact_lt_midpoint0, &is_inexact_gt_midpoint0,
&res, &x, &y, &z
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#else
res = bid128_ext_fma (&is_midpoint_lt_even0, &is_midpoint_gt_even0,
&is_inexact_lt_midpoint0,
&is_inexact_gt_midpoint0, x, y,
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG
_EXC_INFO_ARG);
#endif
if ((rnd_mode == ROUNDING_DOWN) || (rnd_mode == ROUNDING_UP) ||
(rnd_mode == ROUNDING_TO_ZERO) || // no double rounding error is possible
((res.w[HIGH_128W] & MASK_NAN) == MASK_NAN) || //res=QNaN (cannot be SNaN)
((res.w[HIGH_128W] & MASK_ANY_INF) == MASK_INF)) { // result is infinity
#if DECIMAL_CALL_BY_REFERENCE
bid128_to_bid64 (&res1, &res _RND_MODE_ARG _EXC_FLAGS_ARG);
#else
res1 = bid128_to_bid64 (res _RND_MODE_ARG _EXC_FLAGS_ARG);
#endif
// determine the unbiased exponent of the result
unbexp = ((res1 >> 53) & 0x3ff) - 398;
// if subnormal, res1 must have exp = -398
// if tiny and inexact set underflow and inexact status flags
if (!((res1 & MASK_NAN) == MASK_NAN) && // res1 not NaN
(unbexp == -398)
&& ((res1 & MASK_BINARY_SIG1) < 1000000000000000ull)
&& (is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0
|| is_midpoint_lt_even0 || is_midpoint_gt_even0)) {
// set the inexact flag and the underflow flag
*pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION);
} else if (is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0 ||
is_midpoint_lt_even0 || is_midpoint_gt_even0) {
// set the inexact flag and the underflow flag
*pfpsf |= INEXACT_EXCEPTION;
}
*pfpsf |= save_fpsf;
BID_RETURN (res1);
} // else continue, and use rounding to nearest to round to 16 digits
// at this point the result is rounded to nearest (even or away) to 34 digits
// (or less if exact), and it is zero or finite non-zero canonical [sub]normal
sign = res.w[HIGH_128W] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
exp = res.w[HIGH_128W] & MASK_EXP; // biased and shifted left 49 bits
unbexp = (exp >> 49) - 6176;
C.w[1] = res.w[HIGH_128W] & MASK_COEFF;
C.w[0] = res.w[LOW_128W];
if ((C.w[1] == 0x0 && C.w[0] == 0x0) || // result is zero
(unbexp <= (-398 - 35)) || (unbexp >= (369 + 16))) {
// clear under/overflow
#if DECIMAL_CALL_BY_REFERENCE
bid128_to_bid64 (&res1, &res _RND_MODE_ARG _EXC_FLAGS_ARG);
#else
res1 = bid128_to_bid64 (res _RND_MODE_ARG _EXC_FLAGS_ARG);
#endif
*pfpsf |= save_fpsf;
BID_RETURN (res1);
} // else continue
// -398 - 34 <= unbexp <= 369 + 15
if (rnd_mode == ROUNDING_TIES_AWAY) {
// apply correction, if needed, to make the result rounded to nearest-even
if (is_midpoint_gt_even) {
// res = res - 1
res1--; // res1 is now even
} // else the result is already correctly rounded to nearest-even
}
// at this point the result is finite, non-zero canonical normal or subnormal,
// and in most cases overflow or underflow will not occur
// determine the number of digits q in the result
// q = nr. of decimal digits in x
// determine first the nr. of bits in x
if (C.w[1] == 0) {
if (C.w[0] >= 0x0020000000000000ull) { // x >= 2^53
// split the 64-bit value in two 32-bit halves to avoid rounding errors
if (C.w[0] >= 0x0000000100000000ull) { // x >= 2^32
tmp.d = (double) (C.w[0] >> 32); // exact conversion
nr_bits =
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
} else { // x < 2^32
tmp.d = (double) (C.w[0]); // exact conversion
nr_bits =
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // if x < 2^53
tmp.d = (double) C.w[0]; // exact conversion
nr_bits =
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
} else { // C.w[1] != 0 => nr. bits = 64 + nr_bits (C.w[1])
tmp.d = (double) C.w[1]; // exact conversion
nr_bits =
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff);
}
q = nr_digits[nr_bits - 1].digits;
if (q == 0) {
q = nr_digits[nr_bits - 1].digits1;
if (C.w[1] > nr_digits[nr_bits - 1].threshold_hi ||
(C.w[1] == nr_digits[nr_bits - 1].threshold_hi &&
C.w[0] >= nr_digits[nr_bits - 1].threshold_lo))
q++;
}
// if q > 16, round to nearest even to 16 digits (but for underflow it may
// have to be truncated even more)
if (q > 16) {
x0 = q - 16;
if (q <= 18) {
round64_2_18 (q, x0, C.w[0], &res1, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
} else { // 19 <= q <= 34
round128_19_38 (q, x0, C, &res128, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
res1 = res128.w[0]; // the result fits in 64 bits
}
unbexp = unbexp + x0;
if (incr_exp)
unbexp++;
q = 16; // need to set in case denormalization is necessary
} else {
// the result does not require a second rounding (and it must have
// been exact in the first rounding, since q <= 16)
res1 = C.w[0];
}
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res1--; // res1 becomes odd
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
if (res1 == 0x00038d7ea4c67fffull) { // 10^15 - 1
res1 = 0x002386f26fc0ffffull; // 10^16 - 1
unbexp--;
}
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res1++; // res1 becomes odd (so it cannot be 10^16)
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this second rounding was exact the result may still be
// inexact because of the first rounding
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
// this is the result rounded correctly to nearest even, with unbounded exp.
// check for overflow
if (q + unbexp > P16 + expmax16) {
res1 = sign | 0x7800000000000000ull;
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION);
*pfpsf |= save_fpsf;
BID_RETURN (res1)
} else if (unbexp > expmax16) { // q + unbexp <= P16 + expmax16
// not overflow; the result must be exact, and we can multiply res1 by
// 10^(unbexp - expmax16) and the product will fit in 16 decimal digits
scale = unbexp - expmax16;
res1 = res1 * ten2k64[scale]; // res1 * 10^scale
unbexp = expmax16; // unbexp - scale
} else {
; // continue
}
// check for underflow
if (q + unbexp < P16 + expmin16) {
if (unbexp < expmin16) {
// we must truncate more of res
x0 = expmin16 - unbexp; // x0 >= 1
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint;
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint;
is_midpoint_lt_even0 = is_midpoint_lt_even;
is_midpoint_gt_even0 = is_midpoint_gt_even;
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
// the number of decimal digits in res1 is q
if (x0 < q) { // 1 <= x0 <= q-1 => round res to q - x0 digits
// 2 <= q <= 16, 1 <= x0 <= 15
round64_2_18 (q, x0, res1, &res1, &incr_exp,
&is_midpoint_lt_even, &is_midpoint_gt_even,
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint);
if (incr_exp) {
// res1 = 10^(q-x0), 1 <= q - x0 <= q - 1, 1 <= q - x0 <= 15
res1 = ten2k64[q - x0];
}
unbexp = unbexp + x0; // expmin16
} else if (x0 == q) {
// the second rounding is for 0.d(0)d(1)...d(q-1) * 10^emin
// determine relationship with 1/2 ulp
// q <= 16
if (res1 < midpoint64[q - 1]) { // < 1/2 ulp
lt_half_ulp = 1;
is_inexact_lt_midpoint = 1;
} else if (res1 == midpoint64[q - 1]) { // = 1/2 ulp
eq_half_ulp = 1;
is_midpoint_gt_even = 1;
} else { // > 1/2 ulp
// gt_half_ulp = 1;
is_inexact_gt_midpoint = 1;
}
if (lt_half_ulp || eq_half_ulp) {
// res = +0.0 * 10^expmin16
res1 = 0x0000000000000000ull;
} else { // if (gt_half_ulp)
// res = +1 * 10^expmin16
res1 = 0x0000000000000001ull;
}
unbexp = expmin16;
} else { // if (x0 > q)
// the second rounding is for 0.0...d(0)d(1)...d(q-1) * 10^emin
res1 = 0x0000000000000000ull;
unbexp = expmin16;
is_inexact_lt_midpoint = 1;
}
// avoid a double rounding error
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) &&
is_midpoint_lt_even) { // double rounding error upward
// res = res - 1
res1--; // res1 becomes odd
is_midpoint_lt_even = 0;
is_inexact_lt_midpoint = 1;
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) &&
is_midpoint_gt_even) { // double rounding error downward
// res = res + 1
res1++; // res1 becomes odd
is_midpoint_gt_even = 0;
is_inexact_gt_midpoint = 1;
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even &&
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) {
// if this rounding was exact the result may still be
// inexact because of the previous roundings
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) {
is_inexact_gt_midpoint = 1;
}
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) {
is_inexact_lt_midpoint = 1;
}
} else if (is_midpoint_gt_even &&
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) {
// pulled up to a midpoint
is_inexact_lt_midpoint = 1;
is_inexact_gt_midpoint = 0;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else if (is_midpoint_lt_even &&
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) {
// pulled down to a midpoint
is_inexact_lt_midpoint = 0;
is_inexact_gt_midpoint = 1;
is_midpoint_lt_even = 0;
is_midpoint_gt_even = 0;
} else {
;
}
}
// else if unbexp >= emin then q < P (because q + unbexp < P16 + expmin16)
// and the result is tiny and exact
// check for inexact result
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even ||
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0 ||
is_midpoint_lt_even0 || is_midpoint_gt_even0) {
// set the inexact flag and the underflow flag
*pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION);
}
} else if (is_inexact_lt_midpoint || is_inexact_gt_midpoint ||
is_midpoint_lt_even || is_midpoint_gt_even) {
*pfpsf |= INEXACT_EXCEPTION;
}
// this is the result rounded correctly to nearest, with bounded exponent
if (rnd_mode == ROUNDING_TIES_AWAY && is_midpoint_gt_even) { // correction
// res = res + 1
res1++; // res1 is now odd
} // else the result is already correct
// assemble the result
if (res1 < 0x0020000000000000ull) { // res < 2^53
res1 = sign | ((UINT64) (unbexp + 398) << 53) | res1;
} else { // res1 >= 2^53
res1 = sign | MASK_STEERING_BITS |
((UINT64) (unbexp + 398) << 51) | (res1 & MASK_BINARY_SIG2);
}
*pfpsf |= save_fpsf;
BID_RETURN (res1);
}
|