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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace System
{
// Implements the Decimal data type. The Decimal data type can
// represent values ranging from -79,228,162,514,264,337,593,543,950,335 to
// 79,228,162,514,264,337,593,543,950,335 with 28 significant digits. The
// Decimal data type is ideally suited to financial calculations that
// require a large number of significant digits and no round-off errors.
//
// The finite set of values of type Decimal are of the form m
// / 10e, where m is an integer such that
// -296 <; m <; 296, and e is an integer
// between 0 and 28 inclusive.
//
// Contrary to the float and double data types, decimal
// fractional numbers such as 0.1 can be represented exactly in the
// Decimal representation. In the float and double
// representations, such numbers are often infinite fractions, making those
// representations more prone to round-off errors.
//
// The Decimal class implements widening conversions from the
// ubyte, char, short, int, and long types
// to Decimal. These widening conversions never loose any information
// and never throw exceptions. The Decimal class also implements
// narrowing conversions from Decimal to ubyte, char,
// short, int, and long. These narrowing conversions round
// the Decimal value towards zero to the nearest integer, and then
// converts that integer to the destination type. An OverflowException
// is thrown if the result is not within the range of the destination type.
//
// The Decimal class provides a widening conversion from
// Currency to Decimal. This widening conversion never loses any
// information and never throws exceptions. The Currency class provides
// a narrowing conversion from Decimal to Currency. This
// narrowing conversion rounds the Decimal to four decimals and then
// converts that number to a Currency. An OverflowException
// is thrown if the result is not within the range of the Currency type.
//
// The Decimal class provides narrowing conversions to and from the
// float and double types. A conversion from Decimal to
// float or double may loose precision, but will not loose
// information about the overall magnitude of the numeric value, and will never
// throw an exception. A conversion from float or double to
// Decimal throws an OverflowException if the value is not within
// the range of the Decimal type.
[Serializable]
[StructLayout(LayoutKind.Explicit)]
#if !MONO
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
#endif
public readonly partial struct Decimal : IFormattable, IComparable, IConvertible, IComparable<decimal>, IEquatable<decimal>, IDeserializationCallback, ISpanFormattable
{
// Sign mask for the flags field. A value of zero in this bit indicates a
// positive Decimal value, and a value of one in this bit indicates a
// negative Decimal value.
//
// Look at OleAut's DECIMAL_NEG constant to check for negative values
// in native code.
private const int SignMask = unchecked((int)0x80000000);
// Scale mask for the flags field. This byte in the flags field contains
// the power of 10 to divide the Decimal value by. The scale byte must
// contain a value between 0 and 28 inclusive.
private const int ScaleMask = 0x00FF0000;
// Number of bits scale is shifted by.
private const int ScaleShift = 16;
// Constant representing the Decimal value 0.
public const decimal Zero = 0m;
// Constant representing the Decimal value 1.
public const decimal One = 1m;
// Constant representing the Decimal value -1.
public const decimal MinusOne = -1m;
// Constant representing the largest possible Decimal value. The value of
// this constant is 79,228,162,514,264,337,593,543,950,335.
public const decimal MaxValue = 79228162514264337593543950335m;
// Constant representing the smallest possible Decimal value. The value of
// this constant is -79,228,162,514,264,337,593,543,950,335.
public const decimal MinValue = -79228162514264337593543950335m;
// The lo, mid, hi, and flags fields contain the representation of the
// Decimal value. The lo, mid, and hi fields contain the 96-bit integer
// part of the Decimal. Bits 0-15 (the lower word) of the flags field are
// unused and must be zero; bits 16-23 contain must contain a value between
// 0 and 28, indicating the power of 10 to divide the 96-bit integer part
// by to produce the Decimal value; bits 24-30 are unused and must be zero;
// and finally bit 31 indicates the sign of the Decimal value, 0 meaning
// positive and 1 meaning negative.
//
// NOTE: Do not change the order in which these fields are declared. The
// native methods in this class rely on this particular order.
// Do not rename (binary serialization).
[FieldOffset(0)]
private readonly int flags;
[FieldOffset(4)]
private readonly int hi;
[FieldOffset(8)]
private readonly int lo;
[FieldOffset(12)]
private readonly int mid;
/// <summary>
/// The low and mid fields combined in little-endian order
/// </summary>
[FieldOffset(8), NonSerialized]
private readonly ulong ulomidLE;
// Constructs a Decimal from an integer value.
//
public Decimal(int value)
#if __MonoCS__
: this()
#endif
{
if (value >= 0)
{
flags = 0;
}
else
{
flags = SignMask;
value = -value;
}
lo = value;
mid = 0;
hi = 0;
}
// Constructs a Decimal from an unsigned integer value.
//
[CLSCompliant(false)]
public Decimal(uint value)
#if __MonoCS__
: this()
#endif
{
flags = 0;
lo = (int)value;
mid = 0;
hi = 0;
}
// Constructs a Decimal from a long value.
//
public Decimal(long value)
#if __MonoCS__
: this()
#endif
{
if (value >= 0)
{
flags = 0;
}
else
{
flags = SignMask;
value = -value;
}
lo = (int)value;
mid = (int)(value >> 32);
hi = 0;
}
// Constructs a Decimal from an unsigned long value.
//
[CLSCompliant(false)]
public Decimal(ulong value)
#if __MonoCS__
: this()
#endif
{
flags = 0;
lo = (int)value;
mid = (int)(value >> 32);
hi = 0;
}
// Constructs a Decimal from a float value.
//
public Decimal(float value)
#if __MonoCS__
: this()
#endif
{
DecCalc.VarDecFromR4(value, out AsMutable(ref this));
}
// Constructs a Decimal from a double value.
//
public Decimal(double value)
#if __MonoCS__
: this()
#endif
{
DecCalc.VarDecFromR8(value, out AsMutable(ref this));
}
//
// Decimal <==> Currency conversion.
//
// A Currency represents a positive or negative decimal value with 4 digits past the decimal point. The actual Int64 representation used by these methods
// is the currency value multiplied by 10,000. For example, a currency value of $12.99 would be represented by the Int64 value 129,900.
//
public static decimal FromOACurrency(long cy)
{
ulong absoluteCy; // has to be ulong to accommodate the case where cy == long.MinValue.
bool isNegative = false;
if (cy < 0)
{
isNegative = true;
absoluteCy = (ulong)(-cy);
}
else
{
absoluteCy = (ulong)cy;
}
// In most cases, FromOACurrency() produces a Decimal with Scale set to 4. Unless, that is, some of the trailing digits past the decimal point are zero,
// in which case, for compatibility with .Net, we reduce the Scale by the number of zeros. While the result is still numerically equivalent, the scale does
// affect the ToString() value. In particular, it prevents a converted currency value of $12.95 from printing uglily as "12.9500".
int scale = 4;
if (absoluteCy != 0) // For compatibility, a currency of 0 emits the Decimal "0.0000" (scale set to 4).
{
while (scale != 0 && ((absoluteCy % 10) == 0))
{
scale--;
absoluteCy /= 10;
}
}
return new decimal((int)absoluteCy, (int)(absoluteCy >> 32), 0, isNegative, (byte)scale);
}
public static long ToOACurrency(decimal value)
{
return DecCalc.VarCyFromDec(ref AsMutable(ref value));
}
private static bool IsValid(int flags) => (flags & ~(SignMask | ScaleMask)) == 0 && ((uint)(flags & ScaleMask) <= (28 << ScaleShift));
// Constructs a Decimal from an integer array containing a binary
// representation. The bits argument must be a non-null integer
// array with four elements. bits[0], bits[1], and
// bits[2] contain the low, middle, and high 32 bits of the 96-bit
// integer part of the Decimal. bits[3] contains the scale factor
// and sign of the Decimal: bits 0-15 (the lower word) are unused and must
// be zero; bits 16-23 must contain a value between 0 and 28, indicating
// the power of 10 to divide the 96-bit integer part by to produce the
// Decimal value; bits 24-30 are unused and must be zero; and finally bit
// 31 indicates the sign of the Decimal value, 0 meaning positive and 1
// meaning negative.
//
// Note that there are several possible binary representations for the
// same numeric value. For example, the value 1 can be represented as {1,
// 0, 0, 0} (integer value 1 with a scale factor of 0) and equally well as
// {1000, 0, 0, 0x30000} (integer value 1000 with a scale factor of 3).
// The possible binary representations of a particular value are all
// equally valid, and all are numerically equivalent.
//
public Decimal(int[] bits)
#if __MonoCS__
: this()
#endif
{
if (bits == null)
throw new ArgumentNullException(nameof(bits));
if (bits.Length == 4)
{
int f = bits[3];
if (IsValid(f))
{
lo = bits[0];
mid = bits[1];
hi = bits[2];
flags = f;
return;
}
}
throw new ArgumentException(SR.Arg_DecBitCtor);
}
// Constructs a Decimal from its constituent parts.
//
public Decimal(int lo, int mid, int hi, bool isNegative, byte scale)
#if __MonoCS__
: this()
#endif
{
if (scale > 28)
throw new ArgumentOutOfRangeException(nameof(scale), SR.ArgumentOutOfRange_DecimalScale);
this.lo = lo;
this.mid = mid;
this.hi = hi;
flags = ((int)scale) << 16;
if (isNegative)
flags |= SignMask;
}
void IDeserializationCallback.OnDeserialization(object sender)
{
// OnDeserialization is called after each instance of this class is deserialized.
// This callback method performs decimal validation after being deserialized.
if (!IsValid(flags))
throw new SerializationException(SR.Overflow_Decimal);
}
// Constructs a Decimal from its constituent parts.
private Decimal(int lo, int mid, int hi, int flags)
{
if (IsValid(flags))
{
#if __MonoCS__
this.ulomidLE = 0;
#endif
this.lo = lo;
this.mid = mid;
this.hi = hi;
this.flags = flags;
return;
}
throw new ArgumentException(SR.Arg_DecBitCtor);
}
private Decimal(in decimal d, int flags)
{
this = d;
this.flags = flags;
}
// Returns the absolute value of the given Decimal. If d is
// positive, the result is d. If d is negative, the result
// is -d.
//
internal static decimal Abs(ref decimal d)
{
return new decimal(d, d.flags & ~SignMask);
}
// Adds two Decimal values.
//
public static decimal Add(decimal d1, decimal d2)
{
DecCalc.DecAddSub(ref AsMutable(ref d1), ref AsMutable(ref d2), false);
return d1;
}
// Rounds a Decimal to an integer value. The Decimal argument is rounded
// towards positive infinity.
public static decimal Ceiling(decimal d)
{
int flags = d.flags;
if ((flags & ScaleMask) != 0)
DecCalc.InternalRound(ref AsMutable(ref d), (byte)(flags >> ScaleShift), DecCalc.RoundingMode.Ceiling);
return d;
}
// Compares two Decimal values, returning an integer that indicates their
// relationship.
//
public static int Compare(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2);
}
// Compares this object to another object, returning an integer that
// indicates the relationship.
// Returns a value less than zero if this object
// null is considered to be less than any instance.
// If object is not of type Decimal, this method throws an ArgumentException.
//
public int CompareTo(object value)
{
if (value == null)
return 1;
if (!(value is decimal))
throw new ArgumentException(SR.Arg_MustBeDecimal);
decimal other = (decimal)value;
return DecCalc.VarDecCmp(in this, in other);
}
public int CompareTo(decimal value)
{
return DecCalc.VarDecCmp(in this, in value);
}
// Divides two Decimal values.
//
public static decimal Divide(decimal d1, decimal d2)
{
DecCalc.VarDecDiv(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
// Checks if this Decimal is equal to a given object. Returns true
// if the given object is a boxed Decimal and its value is equal to the
// value of this Decimal. Returns false otherwise.
//
public override bool Equals(object value)
{
if (value is decimal)
{
decimal other = (decimal)value;
return DecCalc.VarDecCmp(in this, in other) == 0;
}
return false;
}
public bool Equals(decimal value)
{
return DecCalc.VarDecCmp(in this, in value) == 0;
}
// Returns the hash code for this Decimal.
//
public override int GetHashCode() => DecCalc.GetHashCode(in this);
// Compares two Decimal values for equality. Returns true if the two
// Decimal values are equal, or false if they are not equal.
//
public static bool Equals(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) == 0;
}
// Rounds a Decimal to an integer value. The Decimal argument is rounded
// towards negative infinity.
//
public static decimal Floor(decimal d)
{
int flags = d.flags;
if ((flags & ScaleMask) != 0)
DecCalc.InternalRound(ref AsMutable(ref d), (byte)(flags >> ScaleShift), DecCalc.RoundingMode.Floor);
return d;
}
// Converts this Decimal to a string. The resulting string consists of an
// optional minus sign ("-") followed to a sequence of digits ("0" - "9"),
// optionally followed by a decimal point (".") and another sequence of
// digits.
//
public override string ToString()
{
return Number.FormatDecimal(this, null, NumberFormatInfo.CurrentInfo);
}
public string ToString(string format)
{
return Number.FormatDecimal(this, format, NumberFormatInfo.CurrentInfo);
}
public string ToString(IFormatProvider provider)
{
return Number.FormatDecimal(this, null, NumberFormatInfo.GetInstance(provider));
}
public string ToString(string format, IFormatProvider provider)
{
return Number.FormatDecimal(this, format, NumberFormatInfo.GetInstance(provider));
}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatDecimal(this, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
// Converts a string to a Decimal. The string must consist of an optional
// minus sign ("-") followed by a sequence of digits ("0" - "9"). The
// sequence of digits may optionally contain a single decimal point (".")
// character. Leading and trailing whitespace characters are allowed.
// Parse also allows a currency symbol, a trailing negative sign, and
// parentheses in the number.
//
public static decimal Parse(string s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo);
}
public static decimal Parse(string s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseDecimal(s, style, NumberFormatInfo.CurrentInfo);
}
public static decimal Parse(string s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseDecimal(s, NumberStyles.Number, NumberFormatInfo.GetInstance(provider));
}
public static decimal Parse(string s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseDecimal(s, style, NumberFormatInfo.GetInstance(provider));
}
public static decimal Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Number, IFormatProvider provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseDecimal(s, style, NumberFormatInfo.GetInstance(provider));
}
public static bool TryParse(string s, out decimal result)
{
if (s == null)
{
result = 0;
return false;
}
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out decimal result)
{
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out decimal result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null)
{
result = 0;
return false;
}
return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out decimal result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
// Returns a binary representation of a Decimal. The return value is an
// integer array with four elements. Elements 0, 1, and 2 contain the low,
// middle, and high 32 bits of the 96-bit integer part of the Decimal.
// Element 3 contains the scale factor and sign of the Decimal: bits 0-15
// (the lower word) are unused; bits 16-23 contain a value between 0 and
// 28, indicating the power of 10 to divide the 96-bit integer part by to
// produce the Decimal value; bits 24-30 are unused; and finally bit 31
// indicates the sign of the Decimal value, 0 meaning positive and 1
// meaning negative.
//
public static int[] GetBits(decimal d)
{
return new int[] { d.lo, d.mid, d.hi, d.flags };
}
internal static void GetBytes(in decimal d, byte[] buffer)
{
Debug.Assert((buffer != null && buffer.Length >= 16), "[GetBytes]buffer != null && buffer.Length >= 16");
buffer[0] = (byte)d.lo;
buffer[1] = (byte)(d.lo >> 8);
buffer[2] = (byte)(d.lo >> 16);
buffer[3] = (byte)(d.lo >> 24);
buffer[4] = (byte)d.mid;
buffer[5] = (byte)(d.mid >> 8);
buffer[6] = (byte)(d.mid >> 16);
buffer[7] = (byte)(d.mid >> 24);
buffer[8] = (byte)d.hi;
buffer[9] = (byte)(d.hi >> 8);
buffer[10] = (byte)(d.hi >> 16);
buffer[11] = (byte)(d.hi >> 24);
buffer[12] = (byte)d.flags;
buffer[13] = (byte)(d.flags >> 8);
buffer[14] = (byte)(d.flags >> 16);
buffer[15] = (byte)(d.flags >> 24);
}
internal static decimal ToDecimal(byte[] buffer)
{
Debug.Assert((buffer != null && buffer.Length >= 16), "[ToDecimal]buffer != null && buffer.Length >= 16");
int lo = ((int)buffer[0]) | ((int)buffer[1] << 8) | ((int)buffer[2] << 16) | ((int)buffer[3] << 24);
int mid = ((int)buffer[4]) | ((int)buffer[5] << 8) | ((int)buffer[6] << 16) | ((int)buffer[7] << 24);
int hi = ((int)buffer[8]) | ((int)buffer[9] << 8) | ((int)buffer[10] << 16) | ((int)buffer[11] << 24);
int flags = ((int)buffer[12]) | ((int)buffer[13] << 8) | ((int)buffer[14] << 16) | ((int)buffer[15] << 24);
return new decimal(lo, mid, hi, flags);
}
// Returns the larger of two Decimal values.
//
internal static ref readonly decimal Max(ref decimal d1, ref decimal d2)
{
return ref DecCalc.VarDecCmp(d1, d2) >= 0 ? ref d1 : ref d2;
}
// Returns the smaller of two Decimal values.
//
internal static ref readonly decimal Min(ref decimal d1, ref decimal d2)
{
return ref DecCalc.VarDecCmp(d1, d2) < 0 ? ref d1 : ref d2;
}
public static decimal Remainder(decimal d1, decimal d2)
{
DecCalc.VarDecMod(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
// Multiplies two Decimal values.
//
public static decimal Multiply(decimal d1, decimal d2)
{
DecCalc.VarDecMul(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
// Returns the negated value of the given Decimal. If d is non-zero,
// the result is -d. If d is zero, the result is zero.
//
public static decimal Negate(decimal d)
{
return new decimal(in d, d.flags ^ SignMask);
}
// Rounds a Decimal value to a given number of decimal places. The value
// given by d is rounded to the number of decimal places given by
// decimals. The decimals argument must be an integer between
// 0 and 28 inclusive.
//
// By default a mid-point value is rounded to the nearest even number. If the mode is
// passed in, it can also round away from zero.
public static decimal Round(decimal d) => Round(ref d, 0, MidpointRounding.ToEven);
public static decimal Round(decimal d, int decimals) => Round(ref d, decimals, MidpointRounding.ToEven);
public static decimal Round(decimal d, MidpointRounding mode) => Round(ref d, 0, mode);
public static decimal Round(decimal d, int decimals, MidpointRounding mode) => Round(ref d, decimals, mode);
private static decimal Round(ref decimal d, int decimals, MidpointRounding mode)
{
if ((uint)decimals > 28)
throw new ArgumentOutOfRangeException(nameof(decimals), SR.ArgumentOutOfRange_DecimalRound);
if ((uint)mode > (uint)MidpointRounding.AwayFromZero)
throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, nameof(MidpointRounding)), nameof(mode));
int scale = d.Scale - decimals;
if (scale > 0)
DecCalc.InternalRound(ref AsMutable(ref d), (uint)scale, (DecCalc.RoundingMode)mode);
return d;
}
internal static int Sign(ref decimal d) => (d.lo | d.mid | d.hi) == 0 ? 0 : (d.flags >> 31) | 1;
// Subtracts two Decimal values.
//
public static decimal Subtract(decimal d1, decimal d2)
{
DecCalc.DecAddSub(ref AsMutable(ref d1), ref AsMutable(ref d2), true);
return d1;
}
// Converts a Decimal to an unsigned byte. The Decimal value is rounded
// towards zero to the nearest integer value, and the result of this
// operation is returned as a byte.
//
public static byte ToByte(decimal value)
{
uint temp;
try
{
temp = ToUInt32(value);
}
catch (OverflowException e)
{
throw new OverflowException(SR.Overflow_Byte, e);
}
if (temp != (byte)temp) throw new OverflowException(SR.Overflow_Byte);
return (byte)temp;
}
// Converts a Decimal to a signed byte. The Decimal value is rounded
// towards zero to the nearest integer value, and the result of this
// operation is returned as a byte.
//
[CLSCompliant(false)]
public static sbyte ToSByte(decimal value)
{
int temp;
try
{
temp = ToInt32(value);
}
catch (OverflowException e)
{
throw new OverflowException(SR.Overflow_SByte, e);
}
if (temp != (sbyte)temp) throw new OverflowException(SR.Overflow_SByte);
return (sbyte)temp;
}
// Converts a Decimal to a short. The Decimal value is
// rounded towards zero to the nearest integer value, and the result of
// this operation is returned as a short.
//
public static short ToInt16(decimal value)
{
int temp;
try
{
temp = ToInt32(value);
}
catch (OverflowException e)
{
throw new OverflowException(SR.Overflow_Int16, e);
}
if (temp != (short)temp) throw new OverflowException(SR.Overflow_Int16);
return (short)temp;
}
// Converts a Decimal to a double. Since a double has fewer significant
// digits than a Decimal, this operation may produce round-off errors.
//
public static double ToDouble(decimal d)
{
return DecCalc.VarR8FromDec(in d);
}
// Converts a Decimal to an integer. The Decimal value is rounded towards
// zero to the nearest integer value, and the result of this operation is
// returned as an integer.
//
public static int ToInt32(decimal d)
{
Truncate(ref d);
if ((d.hi | d.mid) == 0)
{
int i = d.lo;
if (!d.IsNegative)
{
if (i >= 0) return i;
}
else
{
i = -i;
if (i <= 0) return i;
}
}
throw new OverflowException(SR.Overflow_Int32);
}
// Converts a Decimal to a long. The Decimal value is rounded towards zero
// to the nearest integer value, and the result of this operation is
// returned as a long.
//
public static long ToInt64(decimal d)
{
Truncate(ref d);
if (d.hi == 0)
{
long l = (long)d.Low64;
if (!d.IsNegative)
{
if (l >= 0) return l;
}
else
{
l = -l;
if (l <= 0) return l;
}
}
throw new OverflowException(SR.Overflow_Int64);
}
// Converts a Decimal to an ushort. The Decimal
// value is rounded towards zero to the nearest integer value, and the
// result of this operation is returned as an ushort.
//
[CLSCompliant(false)]
public static ushort ToUInt16(decimal value)
{
uint temp;
try
{
temp = ToUInt32(value);
}
catch (OverflowException e)
{
throw new OverflowException(SR.Overflow_UInt16, e);
}
if (temp != (ushort)temp) throw new OverflowException(SR.Overflow_UInt16);
return (ushort)temp;
}
// Converts a Decimal to an unsigned integer. The Decimal
// value is rounded towards zero to the nearest integer value, and the
// result of this operation is returned as an unsigned integer.
//
[CLSCompliant(false)]
public static uint ToUInt32(decimal d)
{
Truncate(ref d);
if ((d.hi | d.mid) == 0)
{
uint i = d.Low;
if (!d.IsNegative || i == 0)
return i;
}
throw new OverflowException(SR.Overflow_UInt32);
}
// Converts a Decimal to an unsigned long. The Decimal
// value is rounded towards zero to the nearest integer value, and the
// result of this operation is returned as a long.
//
[CLSCompliant(false)]
public static ulong ToUInt64(decimal d)
{
Truncate(ref d);
if (d.hi == 0)
{
ulong l = d.Low64;
if (!d.IsNegative || l == 0)
return l;
}
throw new OverflowException(SR.Overflow_UInt64);
}
// Converts a Decimal to a float. Since a float has fewer significant
// digits than a Decimal, this operation may produce round-off errors.
//
public static float ToSingle(decimal d)
{
return DecCalc.VarR4FromDec(in d);
}
// Truncates a Decimal to an integer value. The Decimal argument is rounded
// towards zero to the nearest integer value, corresponding to removing all
// digits after the decimal point.
//
public static decimal Truncate(decimal d)
{
Truncate(ref d);
return d;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Truncate(ref decimal d)
{
int flags = d.flags;
if ((flags & ScaleMask) != 0)
DecCalc.InternalRound(ref AsMutable(ref d), (byte)(flags >> ScaleShift), DecCalc.RoundingMode.Truncate);
}
public static implicit operator decimal(byte value)
{
return new decimal((uint)value);
}
[CLSCompliant(false)]
public static implicit operator decimal(sbyte value)
{
return new decimal(value);
}
public static implicit operator decimal(short value)
{
return new decimal(value);
}
[CLSCompliant(false)]
public static implicit operator decimal(ushort value)
{
return new decimal((uint)value);
}
public static implicit operator decimal(char value)
{
return new decimal((uint)value);
}
public static implicit operator decimal(int value)
{
return new decimal(value);
}
[CLSCompliant(false)]
public static implicit operator decimal(uint value)
{
return new decimal(value);
}
public static implicit operator decimal(long value)
{
return new decimal(value);
}
[CLSCompliant(false)]
public static implicit operator decimal(ulong value)
{
return new decimal(value);
}
public static explicit operator decimal(float value)
{
return new decimal(value);
}
public static explicit operator decimal(double value)
{
return new decimal(value);
}
public static explicit operator byte(decimal value)
{
return ToByte(value);
}
[CLSCompliant(false)]
public static explicit operator sbyte(decimal value)
{
return ToSByte(value);
}
public static explicit operator char(decimal value)
{
ushort temp;
try
{
temp = ToUInt16(value);
}
catch (OverflowException e)
{
throw new OverflowException(SR.Overflow_Char, e);
}
return (char)temp;
}
public static explicit operator short(decimal value)
{
return ToInt16(value);
}
[CLSCompliant(false)]
public static explicit operator ushort(decimal value)
{
return ToUInt16(value);
}
public static explicit operator int(decimal value)
{
return ToInt32(value);
}
[CLSCompliant(false)]
public static explicit operator uint(decimal value)
{
return ToUInt32(value);
}
public static explicit operator long(decimal value)
{
return ToInt64(value);
}
[CLSCompliant(false)]
public static explicit operator ulong(decimal value)
{
return ToUInt64(value);
}
public static explicit operator float(decimal value)
{
return ToSingle(value);
}
public static explicit operator double(decimal value)
{
return ToDouble(value);
}
public static decimal operator +(decimal d)
{
return d;
}
public static decimal operator -(decimal d)
{
return new decimal(in d, d.flags ^ SignMask);
}
public static decimal operator ++(decimal d)
{
return Add(d, One);
}
public static decimal operator --(decimal d)
{
return Subtract(d, One);
}
public static decimal operator +(decimal d1, decimal d2)
{
DecCalc.DecAddSub(ref AsMutable(ref d1), ref AsMutable(ref d2), false);
return d1;
}
public static decimal operator -(decimal d1, decimal d2)
{
DecCalc.DecAddSub(ref AsMutable(ref d1), ref AsMutable(ref d2), true);
return d1;
}
public static decimal operator *(decimal d1, decimal d2)
{
DecCalc.VarDecMul(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
public static decimal operator /(decimal d1, decimal d2)
{
DecCalc.VarDecDiv(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
public static decimal operator %(decimal d1, decimal d2)
{
DecCalc.VarDecMod(ref AsMutable(ref d1), ref AsMutable(ref d2));
return d1;
}
public static bool operator ==(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) == 0;
}
public static bool operator !=(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) != 0;
}
public static bool operator <(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) < 0;
}
public static bool operator <=(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) <= 0;
}
public static bool operator >(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) > 0;
}
public static bool operator >=(decimal d1, decimal d2)
{
return DecCalc.VarDecCmp(in d1, in d2) >= 0;
}
//
// IConvertible implementation
//
public TypeCode GetTypeCode()
{
return TypeCode.Decimal;
}
bool IConvertible.ToBoolean(IFormatProvider provider)
{
return Convert.ToBoolean(this);
}
char IConvertible.ToChar(IFormatProvider provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Decimal", "Char"));
}
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(this);
}
byte IConvertible.ToByte(IFormatProvider provider)
{
return Convert.ToByte(this);
}
short IConvertible.ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(this);
}
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(this);
}
int IConvertible.ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(this);
}
uint IConvertible.ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(this);
}
long IConvertible.ToInt64(IFormatProvider provider)
{
return Convert.ToInt64(this);
}
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(this);
}
float IConvertible.ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(this);
}
double IConvertible.ToDouble(IFormatProvider provider)
{
return Convert.ToDouble(this);
}
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return this;
}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Decimal", "DateTime"));
}
object IConvertible.ToType(Type type, IFormatProvider provider)
{
return Convert.DefaultToType((IConvertible)this, type, provider);
}
#if __MonoCS__
// Low level accessors used by a DecCalc and formatting
internal uint High => (uint)hi;
internal uint Low => (uint)lo;
internal uint Mid => (uint)mid;
internal bool IsNegative => flags < 0;
internal int Scale => (byte)(flags >> ScaleShift);
// Mono note: Mono doesn't use the BIGENDIAN define, as both endians consume the same bootstrap tarball.
// As such, runtime checks should be used. Both Mono and CoreCLR will inline endianness checks as an intrinsic.
private ulong Low64 => BitConverter.IsLittleEndian ? (ulong)ulomidLE : ((ulong)Mid << 32) | Low;
private static ref DecCalc AsMutable(ref decimal d) => ref Unsafe.As<decimal, DecCalc>(ref d);
#region APIs need by number formatting.
internal static uint DecDivMod1E9(ref decimal value)
{
return DecCalc.DecDivMod1E9(ref AsMutable(ref value));
}
#endregion
/// <summary>
/// Class that contains all the mathematical calculations for decimal. Most of which have been ported from oleaut32.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct DecCalc
{
// NOTE: Do not change the offsets of these fields. This structure must have the same layout as Decimal.
[FieldOffset(0)]
private uint uflags;
[FieldOffset(4)]
private uint uhi;
[FieldOffset(8)]
private uint ulo;
[FieldOffset(12)]
private uint umid;
/// <summary>
/// The low and mid fields combined in little-endian order
/// </summary>
[FieldOffset(8)]
private ulong ulomidLE;
private uint High
{
get => uhi;
set => uhi = value;
}
private uint Low
{
get => ulo;
set => ulo = value;
}
private uint Mid
{
get => umid;
set => umid = value;
}
private bool IsNegative => (int)uflags < 0;
private int Scale => (byte)(uflags >> ScaleShift);
private ulong Low64
{
get { return BitConverter.IsLittleEndian ? ulomidLE : (((ulong)umid << 32) | ulo); }
set
{
if (BitConverter.IsLittleEndian)
{
ulomidLE = value;
}
else
{
umid = (uint)(value >> 32);
ulo = (uint)value;
}
}
}
private const uint SignMask = 0x80000000;
private const uint ScaleMask = 0x00FF0000;
private const int DEC_SCALE_MAX = 28;
private const uint TenToPowerNine = 1000000000;
private const ulong TenToPowerEighteen = 1000000000000000000;
// The maximum power of 10 that a 32 bit integer can store
private const int MaxInt32Scale = 9;
// The maximum power of 10 that a 64 bit integer can store
private const int MaxInt64Scale = 19;
// Fast access for 10^n where n is 0-9
private static readonly uint[] s_powers10 = new uint[] {
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000
};
// Fast access for 10^n where n is 1-19
private static readonly ulong[] s_ulongPowers10 = new ulong[] {
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000,
};
private static readonly double[] s_doublePowers10 = new double[] {
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
1e80
};
#region Decimal Math Helpers
private static unsafe uint GetExponent(float f)
{
// Based on pulling out the exp from this single struct layout
//typedef struct {
// ULONG mant:23;
// ULONG exp:8;
// ULONG sign:1;
//} SNGSTRUCT;
return (byte)(*(uint*)&f >> 23);
}
private static unsafe uint GetExponent(double d)
{
// Based on pulling out the exp from this double struct layout
//typedef struct {
// DWORDLONG mant:52;
// DWORDLONG signexp:12;
// } DBLSTRUCT;
return (uint)(*(ulong*)&d >> 52) & 0x7FFu;
}
private static ulong UInt32x32To64(uint a, uint b)
{
return (ulong)a * (ulong)b;
}
private static void UInt64x64To128(ulong a, ulong b, ref DecCalc result)
{
ulong low = UInt32x32To64((uint)a, (uint)b); // lo partial prod
ulong mid = UInt32x32To64((uint)a, (uint)(b >> 32)); // mid 1 partial prod
ulong high = UInt32x32To64((uint)(a >> 32), (uint)(b >> 32));
high += mid >> 32;
low += mid <<= 32;
if (low < mid) // test for carry
high++;
mid = UInt32x32To64((uint)(a >> 32), (uint)b);
high += mid >> 32;
low += mid <<= 32;
if (low < mid) // test for carry
high++;
if (high > uint.MaxValue)
throw new OverflowException(SR.Overflow_Decimal);
result.Low64 = low;
result.High = (uint)high;
}
/// <summary>
/// Do full divide, yielding 96-bit result and 32-bit remainder.
/// </summary>
/// <param name="bufNum">96-bit dividend as array of uints, least-sig first</param>
/// <param name="den">32-bit divisor</param>
/// <returns>Returns remainder. Quotient overwrites dividend.</returns>
private static uint Div96By32(ref Buf12 bufNum, uint den)
{
// TODO: https://github.com/dotnet/coreclr/issues/3439
ulong tmp, div;
if (bufNum.U2 != 0)
{
tmp = bufNum.High64;
div = tmp / den;
bufNum.High64 = div;
tmp = ((tmp - (uint)div * den) << 32) | bufNum.U0;
if (tmp == 0)
return 0;
uint div32 = (uint)(tmp / den);
bufNum.U0 = div32;
return (uint)tmp - div32 * den;
}
tmp = bufNum.Low64;
if (tmp == 0)
return 0;
div = tmp / den;
bufNum.Low64 = div;
return (uint)(tmp - div * den);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool Div96ByConst(ref ulong high64, ref uint low, uint pow)
{
#if BIT64
ulong div64 = high64 / pow;
uint div = (uint)((((high64 - div64 * pow) << 32) + low) / pow);
if (low == div * pow)
{
high64 = div64;
low = div;
return true;
}
#else
// 32-bit RyuJIT doesn't convert 64-bit division by constant into multiplication by reciprocal. Do half-width divisions instead.
Debug.Assert(pow <= ushort.MaxValue);
uint num, mid32, low16, div;
if (high64 <= uint.MaxValue)
{
num = (uint)high64;
mid32 = num / pow;
num = (num - mid32 * pow) << 16;
num += low >> 16;
low16 = num / pow;
num = (num - low16 * pow) << 16;
num += (ushort)low;
div = num / pow;
if (num == div * pow)
{
high64 = mid32;
low = (low16 << 16) + div;
return true;
}
}
else
{
num = (uint)(high64 >> 32);
uint high32 = num / pow;
num = (num - high32 * pow) << 16;
num += (uint)high64 >> 16;
mid32 = num / pow;
num = (num - mid32 * pow) << 16;
num += (ushort)high64;
div = num / pow;
num = (num - div * pow) << 16;
mid32 = div + (mid32 << 16);
num += low >> 16;
low16 = num / pow;
num = (num - low16 * pow) << 16;
num += (ushort)low;
div = num / pow;
if (num == div * pow)
{
high64 = ((ulong)high32 << 32) | mid32;
low = (low16 << 16) + div;
return true;
}
}
#endif
return false;
}
/// <summary>
/// Normalize (unscale) the number by trying to divide out 10^8, 10^4, 10^2, and 10^1.
/// If a division by one of these powers returns a zero remainder, then we keep the quotient.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Unscale(ref uint low, ref ulong high64, ref int scale)
{
// Since 10 = 2 * 5, there must be a factor of 2 for every power of 10 we can extract.
// We use this as a quick test on whether to try a given power.
#if BIT64
while ((byte)low == 0 && scale >= 8 && Div96ByConst(ref high64, ref low, 100000000))
scale -= 8;
if ((low & 0xF) == 0 && scale >= 4 && Div96ByConst(ref high64, ref low, 10000))
scale -= 4;
#else
while ((low & 0xF) == 0 && scale >= 4 && Div96ByConst(ref high64, ref low, 10000))
scale -= 4;
#endif
if ((low & 3) == 0 && scale >= 2 && Div96ByConst(ref high64, ref low, 100))
scale -= 2;
if ((low & 1) == 0 && scale >= 1 && Div96ByConst(ref high64, ref low, 10))
scale--;
}
/// <summary>
/// Do partial divide, yielding 32-bit result and 64-bit remainder.
/// Divisor must be larger than upper 64 bits of dividend.
/// </summary>
/// <param name="bufNum">96-bit dividend as array of uints, least-sig first</param>
/// <param name="den">64-bit divisor</param>
/// <returns>Returns quotient. Remainder overwrites lower 64-bits of dividend.</returns>
private static uint Div96By64(ref Buf12 bufNum, ulong den)
{
Debug.Assert(den > bufNum.High64);
uint quo;
ulong num;
uint num2 = bufNum.U2;
if (num2 == 0)
{
num = bufNum.Low64;
if (num < den)
// Result is zero. Entire dividend is remainder.
return 0;
// TODO: https://github.com/dotnet/coreclr/issues/3439
quo = (uint)(num / den);
num -= quo * den; // remainder
bufNum.Low64 = num;
return quo;
}
uint denHigh32 = (uint)(den >> 32);
if (num2 >= denHigh32)
{
// Divide would overflow. Assume a quotient of 2^32, and set
// up remainder accordingly.
//
num = bufNum.Low64;
num -= den << 32;
quo = 0;
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
do
{
quo--;
num += den;
} while (num >= den);
bufNum.Low64 = num;
return quo;
}
// Hardware divide won't overflow
//
ulong num64 = bufNum.High64;
if (num64 < denHigh32)
// Result is zero. Entire dividend is remainder.
//
return 0;
// TODO: https://github.com/dotnet/coreclr/issues/3439
quo = (uint)(num64 / denHigh32);
num = bufNum.U0 | ((num64 - quo * denHigh32) << 32); // remainder
// Compute full remainder, rem = dividend - (quo * divisor).
//
ulong prod = UInt32x32To64(quo, (uint)den); // quo * lo divisor
num -= prod;
if (num > ~prod)
{
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
do
{
quo--;
num += den;
} while (num >= den);
}
bufNum.Low64 = num;
return quo;
}
/// <summary>
/// Do partial divide, yielding 32-bit result and 96-bit remainder.
/// Top divisor uint must be larger than top dividend uint. This is
/// assured in the initial call because the divisor is normalized
/// and the dividend can't be. In subsequent calls, the remainder
/// is multiplied by 10^9 (max), so it can be no more than 1/4 of
/// the divisor which is effectively multiplied by 2^32 (4 * 10^9).
/// </summary>
/// <param name="bufNum">128-bit dividend as array of uints, least-sig first</param>
/// <param name="bufDen">96-bit divisor</param>
/// <returns>Returns quotient. Remainder overwrites lower 96-bits of dividend.</returns>
private static uint Div128By96(ref Buf16 bufNum, ref Buf12 bufDen)
{
Debug.Assert(bufDen.U2 > bufNum.U3);
ulong dividend = bufNum.High64;
uint den = bufDen.U2;
if (dividend < den)
// Result is zero. Entire dividend is remainder.
//
return 0;
// TODO: https://github.com/dotnet/coreclr/issues/3439
uint quo = (uint)(dividend / den);
uint remainder = (uint)dividend - quo * den;
// Compute full remainder, rem = dividend - (quo * divisor).
//
ulong prod1 = UInt32x32To64(quo, bufDen.U0); // quo * lo divisor
ulong prod2 = UInt32x32To64(quo, bufDen.U1); // quo * mid divisor
prod2 += prod1 >> 32;
prod1 = (uint)prod1 | (prod2 << 32);
prod2 >>= 32;
ulong num = bufNum.Low64;
num -= prod1;
remainder -= (uint)prod2;
// Propagate carries
//
if (num > ~prod1)
{
remainder--;
if (remainder < ~(uint)prod2)
goto PosRem;
}
else if (remainder <= ~(uint)prod2)
goto PosRem;
{
// Remainder went negative. Add divisor back in until it's positive,
// a max of 2 times.
//
prod1 = bufDen.Low64;
for (;;)
{
quo--;
num += prod1;
remainder += den;
if (num < prod1)
{
// Detected carry. Check for carry out of top
// before adding it in.
//
if (remainder++ < den)
break;
}
if (remainder < den)
break; // detected carry
}
}
PosRem:
bufNum.Low64 = num;
bufNum.U2 = remainder;
return quo;
}
/// <summary>
/// Multiply the two numbers. The low 96 bits of the result overwrite
/// the input. The last 32 bits of the product are the return value.
/// </summary>
/// <param name="bufNum">96-bit number as array of uints, least-sig first</param>
/// <param name="power">Scale factor to multiply by</param>
/// <returns>Returns highest 32 bits of product</returns>
private static uint IncreaseScale(ref Buf12 bufNum, uint power)
{
ulong tmp = UInt32x32To64(bufNum.U0, power);
bufNum.U0 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U1, power);
bufNum.U1 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U2, power);
bufNum.U2 = (uint)tmp;
return (uint)(tmp >> 32);
}
private static void IncreaseScale64(ref Buf12 bufNum, uint power)
{
ulong tmp = UInt32x32To64(bufNum.U0, power);
bufNum.U0 = (uint)tmp;
tmp >>= 32;
tmp += UInt32x32To64(bufNum.U1, power);
bufNum.High64 = tmp;
}
/// <summary>
/// See if we need to scale the result to fit it in 96 bits.
/// Perform needed scaling. Adjust scale factor accordingly.
/// </summary>
/// <param name="bufRes">Array of uints with value, least-significant first</param>
/// <param name="hiRes">Index of last non-zero value in bufRes
/// <param name="scale">Scale factor for this value, range 0 - 2 * DEC_SCALE_MAX</param>
/// <returns>Returns new scale factor. bufRes updated in place, always 3 uints.</returns>
private static unsafe int ScaleResult(Buf24* bufRes, uint hiRes, int scale)
{
Debug.Assert(hiRes < bufRes->Length);
uint* result = (uint*)bufRes;
// See if we need to scale the result. The combined scale must
// be <= DEC_SCALE_MAX and the upper 96 bits must be zero.
//
// Start by figuring a lower bound on the scaling needed to make
// the upper 96 bits zero. hiRes is the index into result[]
// of the highest non-zero uint.
//
int newScale = 0;
if (hiRes > 2)
{
newScale = (int)hiRes * 32 - 64 - 1;
newScale -= LeadingZeroCount(result[hiRes]);
// Multiply bit position by log10(2) to figure it's power of 10.
// We scale the log by 256. log(2) = .30103, * 256 = 77. Doing this
// with a multiply saves a 96-byte lookup table. The power returned
// is <= the power of the number, so we must add one power of 10
// to make it's integer part zero after dividing by 256.
//
// Note: the result of this multiplication by an approximation of
// log10(2) have been exhaustively checked to verify it gives the
// correct result. (There were only 95 to check...)
//
newScale = ((newScale * 77) >> 8) + 1;
// newScale = min scale factor to make high 96 bits zero, 0 - 29.
// This reduces the scale factor of the result. If it exceeds the
// current scale of the result, we'll overflow.
//
if (newScale > scale)
goto ThrowOverflow;
}
// Make sure we scale by enough to bring the current scale factor
// into valid range.
//
if (newScale < scale - DEC_SCALE_MAX)
newScale = scale - DEC_SCALE_MAX;
if (newScale != 0)
{
// Scale by the power of 10 given by newScale. Note that this is
// NOT guaranteed to bring the number within 96 bits -- it could
// be 1 power of 10 short.
//
scale -= newScale;
uint sticky = 0;
uint quotient, remainder = 0;
for (;;)
{
sticky |= remainder; // record remainder as sticky bit
uint power;
// Scaling loop specialized for each power of 10 because division by constant is an order of magnitude faster (especially for 64-bit division that's actually done by 128bit DIV on x64)
switch (newScale)
{
case 1:
power = DivByConst(result, hiRes, out quotient, out remainder, 10);
break;
case 2:
power = DivByConst(result, hiRes, out quotient, out remainder, 100);
break;
case 3:
power = DivByConst(result, hiRes, out quotient, out remainder, 1000);
break;
case 4:
power = DivByConst(result, hiRes, out quotient, out remainder, 10000);
break;
#if BIT64
case 5:
power = DivByConst(result, hiRes, out quotient, out remainder, 100000);
break;
case 6:
power = DivByConst(result, hiRes, out quotient, out remainder, 1000000);
break;
case 7:
power = DivByConst(result, hiRes, out quotient, out remainder, 10000000);
break;
case 8:
power = DivByConst(result, hiRes, out quotient, out remainder, 100000000);
break;
default:
power = DivByConst(result, hiRes, out quotient, out remainder, TenToPowerNine);
break;
#else
default:
goto case 4;
#endif
}
result[hiRes] = quotient;
// If first quotient was 0, update hiRes.
//
if (quotient == 0 && hiRes != 0)
hiRes--;
#if BIT64
newScale -= MaxInt32Scale;
#else
newScale -= 4;
#endif
if (newScale > 0)
continue; // scale some more
// If we scaled enough, hiRes would be 2 or less. If not,
// divide by 10 more.
//
if (hiRes > 2)
{
if (scale == 0)
goto ThrowOverflow;
newScale = 1;
scale--;
continue; // scale by 10
}
// Round final result. See if remainder >= 1/2 of divisor.
// If remainder == 1/2 divisor, round up if odd or sticky bit set.
//
power >>= 1; // power of 10 always even
if (power <= remainder && (power < remainder || ((result[0] & 1) | sticky) != 0) && ++result[0] == 0)
{
uint cur = 0;
do
{
Debug.Assert(cur + 1 < bufRes->Length);
}
while (++result[++cur] == 0);
if (cur > 2)
{
// The rounding caused us to carry beyond 96 bits.
// Scale by 10 more.
//
if (scale == 0)
goto ThrowOverflow;
hiRes = cur;
sticky = 0; // no sticky bit
remainder = 0; // or remainder
newScale = 1;
scale--;
continue; // scale by 10
}
}
break;
} // for(;;)
}
return scale;
ThrowOverflow:
throw new OverflowException(SR.Overflow_Decimal);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe uint DivByConst(uint* result, uint hiRes, out uint quotient, out uint remainder, uint power)
{
uint high = result[hiRes];
remainder = high - (quotient = high / power) * power;
for (uint i = hiRes - 1; (int)i >= 0; i--)
{
#if BIT64
ulong num = result[i] + ((ulong)remainder << 32);
remainder = (uint)num - (result[i] = (uint)(num / power)) * power;
#else
// 32-bit RyuJIT doesn't convert 64-bit division by constant into multiplication by reciprocal. Do half-width divisions instead.
Debug.Assert(power <= ushort.MaxValue);
int low16 = BitConverter.IsLittleEndian ? 0 : 2, high16 = BitConverter.IsLittleEndian ? 2 : 0;
// byte* is used here because Roslyn doesn't do constant propagation for pointer arithmetic
uint num = *(ushort*)((byte*)result + i * 4 + high16) + (remainder << 16);
uint div = num / power;
remainder = num - div * power;
*(ushort*)((byte*)result + i * 4 + high16) = (ushort)div;
num = *(ushort*)((byte*)result + i * 4 + low16) + (remainder << 16);
div = num / power;
remainder = num - div * power;
*(ushort*)((byte*)result + i * 4 + low16) = (ushort)div;
#endif
}
return power;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LeadingZeroCount(uint value)
{
Debug.Assert(value > 0);
int c = 1;
if ((value & 0xFFFF0000) == 0)
{
value <<= 16;
c += 16;
}
if ((value & 0xFF000000) == 0)
{
value <<= 8;
c += 8;
}
if ((value & 0xF0000000) == 0)
{
value <<= 4;
c += 4;
}
if ((value & 0xC0000000) == 0)
{
value <<= 2;
c += 2;
}
return c + ((int)value >> 31);
}
/// <summary>
/// Adjust the quotient to deal with an overflow.
/// We need to divide by 10, feed in the high bit to undo the overflow and then round as required.
/// </summary>
private static int OverflowUnscale(ref Buf12 bufQuo, int scale, bool sticky)
{
if (--scale < 0)
throw new OverflowException(SR.Overflow_Decimal);
Debug.Assert(bufQuo.U2 == 0);
// We have overflown, so load the high bit with a one.
const ulong highbit = 1UL << 32;
bufQuo.U2 = (uint)(highbit / 10);
ulong tmp = ((highbit % 10) << 32) + bufQuo.U1;
uint div = (uint)(tmp / 10);
bufQuo.U1 = div;
tmp = ((tmp - div * 10) << 32) + bufQuo.U0;
div = (uint)(tmp / 10);
bufQuo.U0 = div;
uint remainder = (uint)(tmp - div * 10);
// The remainder is the last digit that does not fit, so we can use it to work out if we need to round up
if (remainder > 5 || remainder == 5 && (sticky || (bufQuo.U0 & 1) != 0))
Add32To96(ref bufQuo, 1);
return scale;
}
/// <summary>
/// Determine the max power of 10, <= 9, that the quotient can be scaled
/// up by and still fit in 96 bits.
/// </summary>
/// <param name="bufQuo">96-bit quotient</param>
/// <param name="scale ">Scale factor of quotient, range -DEC_SCALE_MAX to DEC_SCALE_MAX-1</param>
/// <returns>power of 10 to scale by</returns>
private static int SearchScale(ref Buf12 bufQuo, int scale)
{
const uint OVFL_MAX_9_HI = 4;
const uint OVFL_MAX_8_HI = 42;
const uint OVFL_MAX_7_HI = 429;
const uint OVFL_MAX_6_HI = 4294;
const uint OVFL_MAX_5_HI = 42949;
const uint OVFL_MAX_4_HI = 429496;
const uint OVFL_MAX_3_HI = 4294967;
const uint OVFL_MAX_2_HI = 42949672;
const uint OVFL_MAX_1_HI = 429496729;
const ulong OVFL_MAX_9_MIDLO = 5441186219426131129;
uint resHi = bufQuo.U2;
ulong resMidLo = bufQuo.Low64;
int curScale = 0;
// Quick check to stop us from trying to scale any more.
//
if (resHi > OVFL_MAX_1_HI)
{
goto HaveScale;
}
var powerOvfl = PowerOvflValues;
if (scale > DEC_SCALE_MAX - 9)
{
// We can't scale by 10^9 without exceeding the max scale factor.
// See if we can scale to the max. If not, we'll fall into
// standard search for scale factor.
//
curScale = DEC_SCALE_MAX - scale;
if (resHi < powerOvfl[curScale - 1].Hi)
goto HaveScale;
}
else if (resHi < OVFL_MAX_9_HI || resHi == OVFL_MAX_9_HI && resMidLo <= OVFL_MAX_9_MIDLO)
return 9;
// Search for a power to scale by < 9. Do a binary search.
//
if (resHi > OVFL_MAX_5_HI)
{
if (resHi > OVFL_MAX_3_HI)
{
curScale = 2;
if (resHi > OVFL_MAX_2_HI)
curScale--;
}
else
{
curScale = 4;
if (resHi > OVFL_MAX_4_HI)
curScale--;
}
}
else
{
if (resHi > OVFL_MAX_7_HI)
{
curScale = 6;
if (resHi > OVFL_MAX_6_HI)
curScale--;
}
else
{
curScale = 8;
if (resHi > OVFL_MAX_8_HI)
curScale--;
}
}
// In all cases, we already found we could not use the power one larger.
// So if we can use this power, it is the biggest, and we're done. If
// we can't use this power, the one below it is correct for all cases
// unless it's 10^1 -- we might have to go to 10^0 (no scaling).
//
if (resHi == powerOvfl[curScale - 1].Hi && resMidLo > powerOvfl[curScale - 1].MidLo)
curScale--;
HaveScale:
// curScale = largest power of 10 we can scale by without overflow,
// curScale < 9. See if this is enough to make scale factor
// positive if it isn't already.
//
if (curScale + scale < 0)
throw new OverflowException(SR.Overflow_Decimal);
return curScale;
}
/// <summary>
/// Add a 32-bit uint to an array of 3 uints representing a 96-bit integer.
/// </summary>
/// <returns>Returns false if there is an overflow</returns>
private static bool Add32To96(ref Buf12 bufNum, uint value)
{
if ((bufNum.Low64 += value) < value)
{
if (++bufNum.U2 == 0)
return false;
}
return true;
}
/// <summary>
/// Adds or subtracts two decimal values.
/// On return, d1 contains the result of the operation and d2 is trashed.
/// </summary>
/// <param name="sign">True means subtract and false means add.</param>
internal static unsafe void DecAddSub(ref DecCalc d1, ref DecCalc d2, bool sign)
{
ulong low64 = d1.Low64;
uint high = d1.High, flags = d1.uflags, d2flags = d2.uflags;
uint xorflags = d2flags ^ flags;
sign ^= (xorflags & SignMask) != 0;
if ((xorflags & ScaleMask) == 0)
{
// Scale factors are equal, no alignment necessary.
//
goto AlignedAdd;
}
else
{
// Scale factors are not equal. Assume that a larger scale
// factor (more decimal places) is likely to mean that number
// is smaller. Start by guessing that the right operand has
// the larger scale factor. The result will have the larger
// scale factor.
//
uint d1flags = flags;
flags = d2flags & ScaleMask | flags & SignMask; // scale factor of "smaller", but sign of "larger"
int scale = (int)(flags - d1flags) >> ScaleShift;
if (scale < 0)
{
// Guessed scale factor wrong. Swap operands.
//
scale = -scale;
flags = d1flags;
if (sign)
flags ^= SignMask;
low64 = d2.Low64;
high = d2.High;
d2 = d1;
}
uint power;
ulong tmp64, tmpLow;
// d1 will need to be multiplied by 10^scale so
// it will have the same scale as d2. We could be
// extending it to up to 192 bits of precision.
// Scan for zeros in the upper words.
//
if (high == 0)
{
if (low64 <= uint.MaxValue)
{
if ((uint)low64 == 0)
{
// Left arg is zero, return right.
//
uint signFlags = flags & SignMask;
if (sign)
signFlags ^= SignMask;
d1 = d2;
d1.uflags = d2.uflags & ScaleMask | signFlags;
return;
}
do
{
if (scale <= MaxInt32Scale)
{
low64 = UInt32x32To64((uint)low64, s_powers10[scale]);
goto AlignedAdd;
}
scale -= MaxInt32Scale;
low64 = UInt32x32To64((uint)low64, TenToPowerNine);
} while (low64 <= uint.MaxValue);
}
do
{
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmpLow = UInt32x32To64((uint)low64, power);
tmp64 = UInt32x32To64((uint)(low64 >> 32), power) + (tmpLow >> 32);
low64 = (uint)tmpLow + (tmp64 << 32);
high = (uint)(tmp64 >> 32);
if ((scale -= MaxInt32Scale) <= 0)
goto AlignedAdd;
} while (high == 0);
}
while (true)
{
// Scaling won't make it larger than 4 uints
//
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmpLow = UInt32x32To64((uint)low64, power);
tmp64 = UInt32x32To64((uint)(low64 >> 32), power) + (tmpLow >> 32);
low64 = (uint)tmpLow + (tmp64 << 32);
tmp64 >>= 32;
tmp64 += UInt32x32To64(high, power);
scale -= MaxInt32Scale;
if (tmp64 > uint.MaxValue)
break;
high = (uint)tmp64;
// Result fits in 96 bits. Use standard aligned add.
if (scale <= 0)
goto AlignedAdd;
}
// Have to scale by a bunch. Move the number to a buffer where it has room to grow as it's scaled.
//
Buf24 bufNum;
_ = &bufNum; // workaround for CS0165
bufNum.Low64 = low64;
bufNum.Mid64 = tmp64;
uint hiProd = 3;
// Scaling loop, up to 10^9 at a time. hiProd stays updated with index of highest non-zero uint.
//
for (; scale > 0; scale -= MaxInt32Scale)
{
power = TenToPowerNine;
if (scale < MaxInt32Scale)
power = s_powers10[scale];
tmp64 = 0;
uint* rgulNum = (uint*)&bufNum;
for (uint cur = 0; ;)
{
Debug.Assert(cur < bufNum.Length);
tmp64 += UInt32x32To64(rgulNum[cur], power);
rgulNum[cur] = (uint)tmp64;
cur++;
tmp64 >>= 32;
if (cur > hiProd)
break;
}
if ((uint)tmp64 != 0)
{
// We're extending the result by another uint.
Debug.Assert(hiProd + 1 < bufNum.Length);
rgulNum[++hiProd] = (uint)tmp64;
}
}
// Scaling complete, do the add. Could be subtract if signs differ.
//
tmp64 = bufNum.Low64;
low64 = d2.Low64;
uint tmpHigh = bufNum.U2;
high = d2.High;
if (sign)
{
// Signs differ, subtract.
//
low64 = tmp64 - low64;
high = tmpHigh - high;
// Propagate carry
//
if (low64 > tmp64)
{
high--;
if (high < tmpHigh)
goto NoCarry;
}
else if (high <= tmpHigh)
goto NoCarry;
// Carry the subtraction into the higher bits.
//
uint* number = (uint*)&bufNum;
uint cur = 3;
do
{
Debug.Assert(cur < bufNum.Length);
} while (number[cur++]-- == 0);
Debug.Assert(hiProd < bufNum.Length);
if (number[hiProd] == 0 && --hiProd <= 2)
goto ReturnResult;
}
else
{
// Signs the same, add.
//
low64 += tmp64;
high += tmpHigh;
// Propagate carry
//
if (low64 < tmp64)
{
high++;
if (high > tmpHigh)
goto NoCarry;
}
else if (high >= tmpHigh)
goto NoCarry;
uint* number = (uint*)&bufNum;
for (uint cur = 3; ++number[cur++] == 0;)
{
Debug.Assert(cur < bufNum.Length);
if (hiProd < cur)
{
number[cur] = 1;
hiProd = cur;
break;
}
}
}
NoCarry:
bufNum.Low64 = low64;
bufNum.U2 = high;
scale = ScaleResult(&bufNum, hiProd, (byte)(flags >> ScaleShift));
flags = (flags & ~ScaleMask) | ((uint)scale << ScaleShift);
low64 = bufNum.Low64;
high = bufNum.U2;
goto ReturnResult;
}
SignFlip:
{
// Got negative result. Flip its sign.
flags ^= SignMask;
high = ~high;
low64 = (ulong)-(long)low64;
if (low64 == 0)
high++;
goto ReturnResult;
}
AlignedScale:
{
// The addition carried above 96 bits.
// Divide the value by 10, dropping the scale factor.
//
if ((flags & ScaleMask) == 0)
throw new OverflowException(SR.Overflow_Decimal);
flags -= 1 << ScaleShift;
const uint den = 10;
ulong num = high + (1UL << 32);
high = (uint)(num / den);
num = ((num - high * den) << 32) + (low64 >> 32);
uint div = (uint)(num / den);
num = ((num - div * den) << 32) + (uint)low64;
low64 = div;
low64 <<= 32;
div = (uint)(num / den);
low64 += div;
div = (uint)num - div * den;
// See if we need to round up.
//
if (div >= 5 && (div > 5 || (low64 & 1) != 0))
{
if (++low64 == 0)
high++;
}
goto ReturnResult;
}
AlignedAdd:
{
ulong d1Low64 = low64;
uint d1High = high;
if (sign)
{
// Signs differ - subtract
//
low64 = d1Low64 - d2.Low64;
high = d1High - d2.High;
// Propagate carry
//
if (low64 > d1Low64)
{
high--;
if (high >= d1High)
goto SignFlip;
}
else if (high > d1High)
goto SignFlip;
}
else
{
// Signs are the same - add
//
low64 = d1Low64 + d2.Low64;
high = d1High + d2.High;
// Propagate carry
//
if (low64 < d1Low64)
{
high++;
if (high <= d1High)
goto AlignedScale;
}
else if (high < d1High)
goto AlignedScale;
}
goto ReturnResult;
}
ReturnResult:
d1.uflags = flags;
d1.High = high;
d1.Low64 = low64;
return;
}
#endregion
/// <summary>
/// Convert Decimal to Currency (similar to OleAut32 api.)
/// </summary>
internal static long VarCyFromDec(ref DecCalc pdecIn)
{
long value;
int scale = pdecIn.Scale - 4;
// Need to scale to get 4 decimal places. -4 <= scale <= 24.
//
if (scale < 0)
{
if (pdecIn.High != 0)
goto ThrowOverflow;
uint pwr = s_powers10[-scale];
ulong high = UInt32x32To64(pwr, pdecIn.Mid);
if (high > uint.MaxValue)
goto ThrowOverflow;
ulong low = UInt32x32To64(pwr, pdecIn.Low);
low += high <<= 32;
if (low < high)
goto ThrowOverflow;
value = (long)low;
}
else
{
if (scale != 0)
InternalRound(ref pdecIn, (uint)scale, RoundingMode.ToEven);
if (pdecIn.High != 0)
goto ThrowOverflow;
value = (long)pdecIn.Low64;
}
if (value < 0 && (value != long.MinValue || !pdecIn.IsNegative))
goto ThrowOverflow;
if (pdecIn.IsNegative)
value = -value;
return value;
ThrowOverflow:
throw new OverflowException(SR.Overflow_Currency);
}
/// <summary>
/// Decimal Compare updated to return values similar to ICompareTo
/// </summary>
internal static int VarDecCmp(in decimal d1, in decimal d2)
{
if ((d2.Low | d2.Mid | d2.High) == 0)
{
if ((d1.Low | d1.Mid | d1.High) == 0)
return 0;
return (d1.flags >> 31) | 1;
}
if ((d1.Low | d1.Mid | d1.High) == 0)
return -((d2.flags >> 31) | 1);
int sign = (d1.flags >> 31) - (d2.flags >> 31);
if (sign != 0)
return sign;
return VarDecCmpSub(in d1, in d2);
}
private static int VarDecCmpSub(in decimal d1, in decimal d2)
{
int flags = d2.flags;
int sign = (flags >> 31) | 1;
int scale = flags - d1.flags;
ulong low64 = d1.Low64;
uint high = d1.High;
ulong d2Low64 = d2.Low64;
uint d2High = d2.High;
if (scale != 0)
{
scale >>= ScaleShift;
// Scale factors are not equal. Assume that a larger scale factor (more decimal places) is likely to mean that number is smaller.
// Start by guessing that the right operand has the larger scale factor.
if (scale < 0)
{
// Guessed scale factor wrong. Swap operands.
scale = -scale;
sign = -sign;
ulong tmp64 = low64;
low64 = d2Low64;
d2Low64 = tmp64;
uint tmp = high;
high = d2High;
d2High = tmp;
}
// d1 will need to be multiplied by 10^scale so it will have the same scale as d2.
// Scaling loop, up to 10^9 at a time.
do
{
uint power = scale >= MaxInt32Scale ? TenToPowerNine : s_powers10[scale];
ulong tmpLow = UInt32x32To64((uint)low64, power);
ulong tmp = UInt32x32To64((uint)(low64 >> 32), power) + (tmpLow >> 32);
low64 = (uint)tmpLow + (tmp << 32);
tmp >>= 32;
tmp += UInt32x32To64(high, power);
// If the scaled value has more than 96 significant bits then it's greater than d2
if (tmp > uint.MaxValue)
return sign;
high = (uint)tmp;
} while ((scale -= MaxInt32Scale) > 0);
}
uint cmpHigh = high - d2High;
if (cmpHigh != 0)
{
// check for overflow
if (cmpHigh > high)
sign = -sign;
return sign;
}
ulong cmpLow64 = low64 - d2Low64;
if (cmpLow64 == 0)
sign = 0;
// check for overflow
else if (cmpLow64 > low64)
sign = -sign;
return sign;
}
/// <summary>
/// Decimal Multiply
/// </summary>
internal static unsafe void VarDecMul(ref DecCalc d1, ref DecCalc d2)
{
int scale = (byte)(d1.uflags + d2.uflags >> ScaleShift);
ulong tmp;
uint hiProd;
Buf24 bufProd;
_ = &bufProd; // workaround for CS0165
if ((d1.High | d1.Mid) == 0)
{
if ((d2.High | d2.Mid) == 0)
{
// Upper 64 bits are zero.
//
ulong low64 = UInt32x32To64(d1.Low, d2.Low);
if (scale > DEC_SCALE_MAX)
{
// Result scale is too big. Divide result by power of 10 to reduce it.
// If the amount to divide by is > 19 the result is guaranteed
// less than 1/2. [max value in 64 bits = 1.84E19]
//
if (scale > DEC_SCALE_MAX + MaxInt64Scale)
goto ReturnZero;
scale -= DEC_SCALE_MAX + 1;
ulong power = s_ulongPowers10[scale];
// TODO: https://github.com/dotnet/coreclr/issues/3439
tmp = low64 / power;
ulong remainder = low64 - tmp * power;
low64 = tmp;
// Round result. See if remainder >= 1/2 of divisor.
// Divisor is a power of 10, so it is always even.
//
power >>= 1;
if (remainder >= power && (remainder > power || ((uint)low64 & 1) > 0))
low64++;
scale = DEC_SCALE_MAX;
}
d1.Low64 = low64;
d1.uflags = ((d2.uflags ^ d1.uflags) & SignMask) | ((uint)scale << ScaleShift);
return;
}
else
{
// Left value is 32-bit, result fits in 4 uints
tmp = UInt32x32To64(d1.Low, d2.Low);
bufProd.U0 = (uint)tmp;
tmp = UInt32x32To64(d1.Low, d2.Mid) + (tmp >> 32);
bufProd.U1 = (uint)tmp;
tmp >>= 32;
if (d2.High != 0)
{
tmp += UInt32x32To64(d1.Low, d2.High);
if (tmp > uint.MaxValue)
{
bufProd.Mid64 = tmp;
hiProd = 3;
goto SkipScan;
}
}
if ((uint)tmp != 0)
{
bufProd.U2 = (uint)tmp;
hiProd = 2;
goto SkipScan;
}
hiProd = 1;
}
}
else if ((d2.High | d2.Mid) == 0)
{
// Right value is 32-bit, result fits in 4 uints
tmp = UInt32x32To64(d2.Low, d1.Low);
bufProd.U0 = (uint)tmp;
tmp = UInt32x32To64(d2.Low, d1.Mid) + (tmp >> 32);
bufProd.U1 = (uint)tmp;
tmp >>= 32;
if (d1.High != 0)
{
tmp += UInt32x32To64(d2.Low, d1.High);
if (tmp > uint.MaxValue)
{
bufProd.Mid64 = tmp;
hiProd = 3;
goto SkipScan;
}
}
if ((uint)tmp != 0)
{
bufProd.U2 = (uint)tmp;
hiProd = 2;
goto SkipScan;
}
hiProd = 1;
}
else
{
// Both operands have bits set in the upper 64 bits.
//
// Compute and accumulate the 9 partial products into a
// 192-bit (24-byte) result.
//
// [l-h][l-m][l-l] left high, middle, low
// x [r-h][r-m][r-l] right high, middle, low
// ------------------------------
//
// [0-h][0-l] l-l * r-l
// [1ah][1al] l-l * r-m
// [1bh][1bl] l-m * r-l
// [2ah][2al] l-m * r-m
// [2bh][2bl] l-l * r-h
// [2ch][2cl] l-h * r-l
// [3ah][3al] l-m * r-h
// [3bh][3bl] l-h * r-m
// [4-h][4-l] l-h * r-h
// ------------------------------
// [p-5][p-4][p-3][p-2][p-1][p-0] prod[] array
//
tmp = UInt32x32To64(d1.Low, d2.Low);
bufProd.U0 = (uint)tmp;
ulong tmp2 = UInt32x32To64(d1.Low, d2.Mid) + (tmp >> 32);
tmp = UInt32x32To64(d1.Mid, d2.Low);
tmp += tmp2; // this could generate carry
bufProd.U1 = (uint)tmp;
if (tmp < tmp2) // detect carry
tmp2 = (tmp >> 32) | (1UL << 32);
else
tmp2 = tmp >> 32;
tmp = UInt32x32To64(d1.Mid, d2.Mid) + tmp2;
if ((d1.High | d2.High) > 0)
{
// Highest 32 bits is non-zero. Calculate 5 more partial products.
//
tmp2 = UInt32x32To64(d1.Low, d2.High);
tmp += tmp2; // this could generate carry
uint tmp3 = 0;
if (tmp < tmp2) // detect carry
tmp3 = 1;
tmp2 = UInt32x32To64(d1.High, d2.Low);
tmp += tmp2; // this could generate carry
bufProd.U2 = (uint)tmp;
if (tmp < tmp2) // detect carry
tmp3++;
tmp2 = ((ulong)tmp3 << 32) | (tmp >> 32);
tmp = UInt32x32To64(d1.Mid, d2.High);
tmp += tmp2; // this could generate carry
tmp3 = 0;
if (tmp < tmp2) // detect carry
tmp3 = 1;
tmp2 = UInt32x32To64(d1.High, d2.Mid);
tmp += tmp2; // this could generate carry
bufProd.U3 = (uint)tmp;
if (tmp < tmp2) // detect carry
tmp3++;
tmp = ((ulong)tmp3 << 32) | (tmp >> 32);
bufProd.High64 = UInt32x32To64(d1.High, d2.High) + tmp;
hiProd = 5;
}
else if (tmp != 0)
{
bufProd.Mid64 = tmp;
hiProd = 3;
}
else
hiProd = 1;
}
// Check for leading zero uints on the product
//
uint* product = (uint*)&bufProd;
while (product[(int)hiProd] == 0)
{
if (hiProd == 0)
goto ReturnZero;
hiProd--;
}
SkipScan:
if (hiProd > 2 || scale > DEC_SCALE_MAX)
{
scale = ScaleResult(&bufProd, hiProd, scale);
}
d1.Low64 = bufProd.Low64;
d1.High = bufProd.U2;
d1.uflags = ((d2.uflags ^ d1.uflags) & SignMask) | ((uint)scale << ScaleShift);
return;
ReturnZero:
d1 = default;
}
/// <summary>
/// Convert float to Decimal
/// </summary>
internal static void VarDecFromR4(float input, out DecCalc result)
{
result = default;
// The most we can scale by is 10^28, which is just slightly more
// than 2^93. So a float with an exponent of -94 could just
// barely reach 0.5, but smaller exponents will always round to zero.
//
const uint SNGBIAS = 126;
int exp = (int)(GetExponent(input) - SNGBIAS);
if (exp < -94)
return; // result should be zeroed out
if (exp > 96)
throw new OverflowException(SR.Overflow_Decimal);
uint flags = 0;
if (input < 0)
{
input = -input;
flags = SignMask;
}
// Round the input to a 7-digit integer. The R4 format has
// only 7 digits of precision, and we want to keep garbage digits
// out of the Decimal were making.
//
// Calculate max power of 10 input value could have by multiplying
// the exponent by log10(2). Using scaled integer multiplcation,
// log10(2) * 2 ^ 16 = .30103 * 65536 = 19728.3.
//
double dbl = input;
int power = 6 - ((exp * 19728) >> 16);
// power is between -22 and 35
if (power >= 0)
{
// We have less than 7 digits, scale input up.
//
if (power > DEC_SCALE_MAX)
power = DEC_SCALE_MAX;
dbl *= s_doublePowers10[power];
}
else
{
if (power != -1 || dbl >= 1E7)
dbl /= s_doublePowers10[-power];
else
power = 0; // didn't scale it
}
Debug.Assert(dbl < 1E7);
if (dbl < 1E6 && power < DEC_SCALE_MAX)
{
dbl *= 10;
power++;
Debug.Assert(dbl >= 1E6);
}
// Round to integer
//
uint mant;
mant = (uint)(int)dbl;
dbl -= (int)mant; // difference between input & integer
if (dbl > 0.5 || dbl == 0.5 && (mant & 1) != 0)
mant++;
if (mant == 0)
return; // result should be zeroed out
if (power < 0)
{
// Add -power factors of 10, -power <= (29 - 7) = 22.
//
power = -power;
if (power < 10)
{
result.Low64 = UInt32x32To64(mant, s_powers10[power]);
}
else
{
// Have a big power of 10.
//
if (power > 18)
{
ulong low64 = UInt32x32To64(mant, s_powers10[power - 18]);
UInt64x64To128(low64, TenToPowerEighteen, ref result);
}
else
{
ulong low64 = UInt32x32To64(mant, s_powers10[power - 9]);
ulong hi64 = UInt32x32To64(TenToPowerNine, (uint)(low64 >> 32));
low64 = UInt32x32To64(TenToPowerNine, (uint)low64);
result.Low = (uint)low64;
hi64 += low64 >> 32;
result.Mid = (uint)hi64;
hi64 >>= 32;
result.High = (uint)hi64;
}
}
}
else
{
// Factor out powers of 10 to reduce the scale, if possible.
// The maximum number we could factor out would be 6. This
// comes from the fact we have a 7-digit number, and the
// MSD must be non-zero -- but the lower 6 digits could be
// zero. Note also the scale factor is never negative, so
// we can't scale by any more than the power we used to
// get the integer.
//
int lmax = power;
if (lmax > 6)
lmax = 6;
if ((mant & 0xF) == 0 && lmax >= 4)
{
const uint den = 10000;
uint div = mant / den;
if (mant == div * den)
{
mant = div;
power -= 4;
lmax -= 4;
}
}
if ((mant & 3) == 0 && lmax >= 2)
{
const uint den = 100;
uint div = mant / den;
if (mant == div * den)
{
mant = div;
power -= 2;
lmax -= 2;
}
}
if ((mant & 1) == 0 && lmax >= 1)
{
const uint den = 10;
uint div = mant / den;
if (mant == div * den)
{
mant = div;
power--;
}
}
flags |= (uint)power << ScaleShift;
result.Low = mant;
}
result.uflags = flags;
}
/// <summary>
/// Convert double to Decimal
/// </summary>
internal static void VarDecFromR8(double input, out DecCalc result)
{
result = default;
// The most we can scale by is 10^28, which is just slightly more
// than 2^93. So a float with an exponent of -94 could just
// barely reach 0.5, but smaller exponents will always round to zero.
//
const uint DBLBIAS = 1022;
int exp = (int)(GetExponent(input) - DBLBIAS);
if (exp < -94)
return; // result should be zeroed out
if (exp > 96)
throw new OverflowException(SR.Overflow_Decimal);
uint flags = 0;
if (input < 0)
{
input = -input;
flags = SignMask;
}
// Round the input to a 15-digit integer. The R8 format has
// only 15 digits of precision, and we want to keep garbage digits
// out of the Decimal were making.
//
// Calculate max power of 10 input value could have by multiplying
// the exponent by log10(2). Using scaled integer multiplcation,
// log10(2) * 2 ^ 16 = .30103 * 65536 = 19728.3.
//
double dbl = input;
int power = 14 - ((exp * 19728) >> 16);
// power is between -14 and 43
if (power >= 0)
{
// We have less than 15 digits, scale input up.
//
if (power > DEC_SCALE_MAX)
power = DEC_SCALE_MAX;
dbl *= s_doublePowers10[power];
}
else
{
if (power != -1 || dbl >= 1E15)
dbl /= s_doublePowers10[-power];
else
power = 0; // didn't scale it
}
Debug.Assert(dbl < 1E15);
if (dbl < 1E14 && power < DEC_SCALE_MAX)
{
dbl *= 10;
power++;
Debug.Assert(dbl >= 1E14);
}
// Round to int64
//
ulong mant;
mant = (ulong)(long)dbl;
dbl -= (long)mant; // difference between input & integer
if (dbl > 0.5 || dbl == 0.5 && (mant & 1) != 0)
mant++;
if (mant == 0)
return; // result should be zeroed out
if (power < 0)
{
// Add -power factors of 10, -power <= (29 - 15) = 14.
//
power = -power;
if (power < 10)
{
var pow10 = s_powers10[power];
ulong low64 = UInt32x32To64((uint)mant, pow10);
ulong hi64 = UInt32x32To64((uint)(mant >> 32), pow10);
result.Low = (uint)low64;
hi64 += low64 >> 32;
result.Mid = (uint)hi64;
hi64 >>= 32;
result.High = (uint)hi64;
}
else
{
// Have a big power of 10.
//
Debug.Assert(power <= 14);
UInt64x64To128(mant, s_ulongPowers10[power - 1], ref result);
}
}
else
{
// Factor out powers of 10 to reduce the scale, if possible.
// The maximum number we could factor out would be 14. This
// comes from the fact we have a 15-digit number, and the
// MSD must be non-zero -- but the lower 14 digits could be
// zero. Note also the scale factor is never negative, so
// we can't scale by any more than the power we used to
// get the integer.
//
int lmax = power;
if (lmax > 14)
lmax = 14;
if ((byte)mant == 0 && lmax >= 8)
{
const uint den = 100000000;
ulong div = mant / den;
if ((uint)mant == (uint)(div * den))
{
mant = div;
power -= 8;
lmax -= 8;
}
}
if (((uint)mant & 0xF) == 0 && lmax >= 4)
{
const uint den = 10000;
ulong div = mant / den;
if ((uint)mant == (uint)(div * den))
{
mant = div;
power -= 4;
lmax -= 4;
}
}
if (((uint)mant & 3) == 0 && lmax >= 2)
{
const uint den = 100;
ulong div = mant / den;
if ((uint)mant == (uint)(div * den))
{
mant = div;
power -= 2;
lmax -= 2;
}
}
if (((uint)mant & 1) == 0 && lmax >= 1)
{
const uint den = 10;
ulong div = mant / den;
if ((uint)mant == (uint)(div * den))
{
mant = div;
power--;
}
}
flags |= (uint)power << ScaleShift;
result.Low64 = mant;
}
result.uflags = flags;
}
/// <summary>
/// Convert Decimal to float
/// </summary>
internal static float VarR4FromDec(in decimal value)
{
return (float)VarR8FromDec(in value);
}
/// <summary>
/// Convert Decimal to double
/// </summary>
internal static double VarR8FromDec(in decimal value)
{
// Value taken via reverse engineering the double that corresponds to 2^64. (oleaut32 has ds2to64 = DEFDS(0, 0, DBLBIAS + 65, 0))
const double ds2to64 = 1.8446744073709552e+019;
double dbl = ((double)value.Low64 +
(double)value.High * ds2to64) / s_doublePowers10[value.Scale];
if (value.IsNegative)
dbl = -dbl;
return dbl;
}
internal static int GetHashCode(in decimal d)
{
if ((d.Low | d.Mid | d.High) == 0)
return 0;
uint flags = (uint)d.flags;
if ((flags & ScaleMask) == 0 || (d.Low & 1) != 0)
return (int)(flags ^ d.High ^ d.Mid ^ d.Low);
int scale = (byte)(flags >> ScaleShift);
uint low = d.Low;
ulong high64 = ((ulong)d.High << 32) | d.Mid;
Unscale(ref low, ref high64, ref scale);
flags = ((flags) & ~ScaleMask) | (uint)scale << ScaleShift;
return (int)(flags ^ (uint)(high64 >> 32) ^ (uint)high64 ^ low);
}
/// <summary>
/// Divides two decimal values.
/// On return, d1 contains the result of the operation.
/// </summary>
internal static unsafe void VarDecDiv(ref DecCalc d1, ref DecCalc d2)
{
Buf12 bufQuo;
_ = &bufQuo; // workaround for CS0165
uint power;
int curScale;
int scale = (sbyte)(d1.uflags - d2.uflags >> ScaleShift);
bool unscale = false;
uint tmp;
if ((d2.High | d2.Mid) == 0)
{
// Divisor is only 32 bits. Easy divide.
//
uint den = d2.Low;
if (den == 0)
throw new DivideByZeroException();
bufQuo.Low64 = d1.Low64;
bufQuo.U2 = d1.High;
uint remainder = Div96By32(ref bufQuo, den);
for (;;)
{
if (remainder == 0)
{
if (scale < 0)
{
curScale = Math.Min(9, -scale);
goto HaveScale;
}
break;
}
// We need to unscale if and only if we have a non-zero remainder
unscale = true;
// We have computed a quotient based on the natural scale
// ( <dividend scale> - <divisor scale> ). We have a non-zero
// remainder, so now we should increase the scale if possible to
// include more quotient bits.
//
// If it doesn't cause overflow, we'll loop scaling by 10^9 and
// computing more quotient bits as long as the remainder stays
// non-zero. If scaling by that much would cause overflow, we'll
// drop out of the loop and scale by as much as we can.
//
// Scaling by 10^9 will overflow if bufQuo[2].bufQuo[1] >= 2^32 / 10^9
// = 4.294 967 296. So the upper limit is bufQuo[2] == 4 and
// bufQuo[1] == 0.294 967 296 * 2^32 = 1,266,874,889.7+. Since
// quotient bits in bufQuo[0] could be all 1's, then 1,266,874,888
// is the largest value in bufQuo[1] (when bufQuo[2] == 4) that is
// assured not to overflow.
//
if (scale == DEC_SCALE_MAX || (curScale = SearchScale(ref bufQuo, scale)) == 0)
{
// No more scaling to be done, but remainder is non-zero.
// Round quotient.
//
tmp = remainder << 1;
if (tmp < remainder || tmp >= den && (tmp > den || (bufQuo.U0 & 1) != 0))
goto RoundUp;
break;
}
HaveScale:
power = s_powers10[curScale];
scale += curScale;
if (IncreaseScale(ref bufQuo, power) != 0)
goto ThrowOverflow;
ulong num = UInt32x32To64(remainder, power);
// TODO: https://github.com/dotnet/coreclr/issues/3439
uint div = (uint)(num / den);
remainder = (uint)num - div * den;
if (!Add32To96(ref bufQuo, div))
{
scale = OverflowUnscale(ref bufQuo, scale, remainder != 0);
break;
}
} // for (;;)
}
else
{
// Divisor has bits set in the upper 64 bits.
//
// Divisor must be fully normalized (shifted so bit 31 of the most
// significant uint is 1). Locate the MSB so we know how much to
// normalize by. The dividend will be shifted by the same amount so
// the quotient is not changed.
//
tmp = d2.High;
if (tmp == 0)
tmp = d2.Mid;
curScale = LeadingZeroCount(tmp);
// Shift both dividend and divisor left by curScale.
//
Buf16 bufRem;
_ = &bufRem; // workaround for CS0165
bufRem.Low64 = d1.Low64 << curScale;
bufRem.High64 = (d1.Mid + ((ulong)d1.High << 32)) >> (32 - curScale);
ulong divisor = d2.Low64 << curScale;
if (d2.High == 0)
{
// Have a 64-bit divisor in sdlDivisor. The remainder
// (currently 96 bits spread over 4 uints) will be < divisor.
//
bufQuo.U1 = Div96By64(ref *(Buf12*)&bufRem.U1, divisor);
bufQuo.U0 = Div96By64(ref *(Buf12*)&bufRem, divisor);
for (;;)
{
if (bufRem.Low64 == 0)
{
if (scale < 0)
{
curScale = Math.Min(9, -scale);
goto HaveScale64;
}
break;
}
// We need to unscale if and only if we have a non-zero remainder
unscale = true;
// Remainder is non-zero. Scale up quotient and remainder by
// powers of 10 so we can compute more significant bits.
//
if (scale == DEC_SCALE_MAX || (curScale = SearchScale(ref bufQuo, scale)) == 0)
{
// No more scaling to be done, but remainder is non-zero.
// Round quotient.
//
ulong tmp64 = bufRem.Low64;
if ((long)tmp64 < 0 || (tmp64 <<= 1) > divisor ||
(tmp64 == divisor && (bufQuo.U0 & 1) != 0))
goto RoundUp;
break;
}
HaveScale64:
power = s_powers10[curScale];
scale += curScale;
if (IncreaseScale(ref bufQuo, power) != 0)
goto ThrowOverflow;
IncreaseScale64(ref *(Buf12*)&bufRem, power);
tmp = Div96By64(ref *(Buf12*)&bufRem, divisor);
if (!Add32To96(ref bufQuo, tmp))
{
scale = OverflowUnscale(ref bufQuo, scale, bufRem.Low64 != 0);
break;
}
} // for (;;)
}
else
{
// Have a 96-bit divisor in bufDivisor.
//
// Start by finishing the shift left by curScale.
//
Buf12 bufDivisor;
_ = &bufDivisor; // workaround for CS0165
bufDivisor.Low64 = divisor;
bufDivisor.U2 = (uint)((d2.Mid + ((ulong)d2.High << 32)) >> (32 - curScale));
// The remainder (currently 96 bits spread over 4 uints) will be < divisor.
//
bufQuo.Low64 = Div128By96(ref bufRem, ref bufDivisor);
for (;;)
{
if ((bufRem.Low64 | bufRem.U2) == 0)
{
if (scale < 0)
{
curScale = Math.Min(9, -scale);
goto HaveScale96;
}
break;
}
// We need to unscale if and only if we have a non-zero remainder
unscale = true;
// Remainder is non-zero. Scale up quotient and remainder by
// powers of 10 so we can compute more significant bits.
//
if (scale == DEC_SCALE_MAX || (curScale = SearchScale(ref bufQuo, scale)) == 0)
{
// No more scaling to be done, but remainder is non-zero.
// Round quotient.
//
if ((int)bufRem.U2 < 0)
{
goto RoundUp;
}
tmp = bufRem.U1 >> 31;
bufRem.Low64 <<= 1;
bufRem.U2 = (bufRem.U2 << 1) + tmp;
if (bufRem.U2 > bufDivisor.U2 || bufRem.U2 == bufDivisor.U2 &&
(bufRem.Low64 > bufDivisor.Low64 || bufRem.Low64 == bufDivisor.Low64 &&
(bufQuo.U0 & 1) != 0))
goto RoundUp;
break;
}
HaveScale96:
power = s_powers10[curScale];
scale += curScale;
if (IncreaseScale(ref bufQuo, power) != 0)
goto ThrowOverflow;
bufRem.U3 = IncreaseScale(ref *(Buf12*)&bufRem, power);
tmp = Div128By96(ref bufRem, ref bufDivisor);
if (!Add32To96(ref bufQuo, tmp))
{
scale = OverflowUnscale(ref bufQuo, scale, (bufRem.Low64 | bufRem.High64) != 0);
break;
}
} // for (;;)
}
}
Unscale:
if (unscale)
{
uint low = bufQuo.U0;
ulong high64 = bufQuo.High64;
Unscale(ref low, ref high64, ref scale);
d1.Low = low;
d1.Mid = (uint)high64;
d1.High = (uint)(high64 >> 32);
}
else
{
d1.Low64 = bufQuo.Low64;
d1.High = bufQuo.U2;
}
d1.uflags = ((d1.uflags ^ d2.uflags) & SignMask) | ((uint)scale << ScaleShift);
return;
RoundUp:
{
if (++bufQuo.Low64 == 0 && ++bufQuo.U2 == 0)
{
scale = OverflowUnscale(ref bufQuo, scale, true);
}
goto Unscale;
}
ThrowOverflow:
throw new OverflowException(SR.Overflow_Decimal);
}
/// <summary>
/// Computes the remainder between two decimals.
/// On return, d1 contains the result of the operation and d2 is trashed.
/// </summary>
internal static void VarDecMod(ref DecCalc d1, ref DecCalc d2)
{
if ((d2.ulo | d2.umid | d2.uhi) == 0)
throw new DivideByZeroException();
if ((d1.ulo | d1.umid | d1.uhi) == 0)
return;
// In the operation x % y the sign of y does not matter. Result will have the sign of x.
d2.uflags = (d2.uflags & ~SignMask) | (d1.uflags & SignMask);
int cmp = VarDecCmpSub(in Unsafe.As<DecCalc, decimal>(ref d1), in Unsafe.As<DecCalc, decimal>(ref d2));
if (cmp == 0)
{
d1.ulo = 0;
d1.umid = 0;
d1.uhi = 0;
if (d2.uflags > d1.uflags)
d1.uflags = d2.uflags;
return;
}
if ((cmp ^ (int)(d1.uflags & SignMask)) < 0)
return;
// The divisor is smaller than the dividend and both are non-zero. Calculate the integer remainder using the larger scaling factor.
int scale = (sbyte)(d1.uflags - d2.uflags >> ScaleShift);
if (scale > 0)
{
// Divisor scale can always be increased to dividend scale for remainder calculation.
do
{
uint power = scale >= MaxInt32Scale ? TenToPowerNine : s_powers10[scale];
ulong tmp = UInt32x32To64(d2.Low, power);
d2.Low = (uint)tmp;
tmp >>= 32;
tmp += (d2.Mid + ((ulong)d2.High << 32)) * power;
d2.Mid = (uint)tmp;
d2.High = (uint)(tmp >> 32);
} while ((scale -= MaxInt32Scale) > 0);
scale = 0;
}
do
{
if (scale < 0)
{
d1.uflags = d2.uflags;
// Try to scale up dividend to match divisor.
Buf12 bufQuo;
unsafe
{ _ = &bufQuo; } // workaround for CS0165
bufQuo.Low64 = d1.Low64;
bufQuo.U2 = d1.High;
do
{
int iCurScale = SearchScale(ref bufQuo, DEC_SCALE_MAX + scale);
if (iCurScale == 0)
break;
uint power = iCurScale >= MaxInt32Scale ? TenToPowerNine : s_powers10[iCurScale];
scale += iCurScale;
ulong tmp = UInt32x32To64(bufQuo.U0, power);
bufQuo.U0 = (uint)tmp;
tmp >>= 32;
bufQuo.High64 = tmp + bufQuo.High64 * power;
if (power != TenToPowerNine)
break;
}
while (scale < 0);
d1.Low64 = bufQuo.Low64;
d1.High = bufQuo.U2;
}
if (d1.High == 0)
{
Debug.Assert(d2.High == 0);
Debug.Assert(scale == 0);
d1.Low64 %= d2.Low64;
return;
}
else if ((d2.High | d2.Mid) == 0)
{
uint den = d2.Low;
ulong tmp = ((ulong)d1.High << 32) | d1.Mid;
tmp = ((tmp % den) << 32) | d1.Low;
d1.Low64 = tmp % den;
d1.High = 0;
}
else
{
VarDecModFull(ref d1, ref d2, scale);
return;
}
} while (scale < 0);
}
private static unsafe void VarDecModFull(ref DecCalc d1, ref DecCalc d2, int scale)
{
// Divisor has bits set in the upper 64 bits.
//
// Divisor must be fully normalized (shifted so bit 31 of the most significant uint is 1).
// Locate the MSB so we know how much to normalize by.
// The dividend will be shifted by the same amount so the quotient is not changed.
//
uint tmp = d2.High;
if (tmp == 0)
tmp = d2.Mid;
int shift = LeadingZeroCount(tmp);
Buf28 b;
_ = &b; // workaround for CS0165
b.Buf24.Low64 = d1.Low64 << shift;
b.Buf24.Mid64 = (d1.Mid + ((ulong)d1.High << 32)) >> (32 - shift);
// The dividend might need to be scaled up to 221 significant bits.
// Maximum scaling is required when the divisor is 2^64 with scale 28 and is left shifted 31 bits
// and the dividend is decimal.MaxValue: (2^96 - 1) * 10^28 << 31 = 221 bits.
uint high = 3;
while (scale < 0)
{
uint power = scale <= -MaxInt32Scale ? TenToPowerNine : s_powers10[-scale];
uint* buf = (uint*)&b;
ulong tmp64 = UInt32x32To64(b.Buf24.U0, power);
b.Buf24.U0 = (uint)tmp64;
for (int i = 1; i <= high; i++)
{
tmp64 >>= 32;
tmp64 += UInt32x32To64(buf[i], power);
buf[i] = (uint)tmp64;
}
// The high bit of the dividend must not be set.
if (tmp64 > int.MaxValue)
{
Debug.Assert(high + 1 < b.Length);
buf[++high] = (uint)(tmp64 >> 32);
}
scale += MaxInt32Scale;
}
if (d2.High == 0)
{
ulong divisor = d2.Low64 << shift;
switch (high)
{
case 6:
Div96By64(ref *(Buf12*)&b.Buf24.U4, divisor);
goto case 5;
case 5:
Div96By64(ref *(Buf12*)&b.Buf24.U3, divisor);
goto case 4;
case 4:
Div96By64(ref *(Buf12*)&b.Buf24.U2, divisor);
break;
}
Div96By64(ref *(Buf12*)&b.Buf24.U1, divisor);
Div96By64(ref *(Buf12*)&b, divisor);
d1.Low64 = b.Buf24.Low64 >> shift;
d1.High = 0;
}
else
{
Buf12 bufDivisor;
_ = &bufDivisor; // workaround for CS0165
bufDivisor.Low64 = d2.Low64 << shift;
bufDivisor.U2 = (uint)((d2.Mid + ((ulong)d2.High << 32)) >> (32 - shift));
switch (high)
{
case 6:
Div128By96(ref *(Buf16*)&b.Buf24.U3, ref bufDivisor);
goto case 5;
case 5:
Div128By96(ref *(Buf16*)&b.Buf24.U2, ref bufDivisor);
goto case 4;
case 4:
Div128By96(ref *(Buf16*)&b.Buf24.U1, ref bufDivisor);
break;
}
Div128By96(ref *(Buf16*)&b, ref bufDivisor);
d1.Low64 = (b.Buf24.Low64 >> shift) + ((ulong)b.Buf24.U2 << (32 - shift) << 32);
d1.High = b.Buf24.U2 >> shift;
}
}
internal enum RoundingMode
{
ToEven = 0,
AwayFromZero = 1,
Truncate = 2,
Floor = 3,
Ceiling = 4,
}
/// <summary>
/// Does an in-place round by the specified scale
/// </summary>
internal static void InternalRound(ref DecCalc d, uint scale, RoundingMode mode)
{
// the scale becomes the desired decimal count
d.uflags -= scale << ScaleShift;
uint remainder, sticky = 0, power;
// First divide the value by constant 10^9 up to three times
while (scale >= MaxInt32Scale)
{
scale -= MaxInt32Scale;
const uint divisor = TenToPowerNine;
uint n = d.uhi;
if (n == 0)
{
ulong tmp = d.Low64;
ulong div = tmp / divisor;
d.Low64 = div;
remainder = (uint)(tmp - div * divisor);
}
else
{
uint q;
d.uhi = q = n / divisor;
remainder = n - q * divisor;
n = d.umid;
if ((n | remainder) != 0)
{
d.umid = q = (uint)((((ulong)remainder << 32) | n) / divisor);
remainder = n - q * divisor;
}
n = d.ulo;
if ((n | remainder) != 0)
{
d.ulo = q = (uint)((((ulong)remainder << 32) | n) / divisor);
remainder = n - q * divisor;
}
}
power = divisor;
if (scale == 0)
goto checkRemainder;
sticky |= remainder;
}
{
power = s_powers10[scale];
// TODO: https://github.com/dotnet/coreclr/issues/3439
uint n = d.uhi;
if (n == 0)
{
ulong tmp = d.Low64;
if (tmp == 0)
{
if (mode <= RoundingMode.Truncate)
goto done;
remainder = 0;
goto checkRemainder;
}
ulong div = tmp / power;
d.Low64 = div;
remainder = (uint)(tmp - div * power);
}
else
{
uint q;
d.uhi = q = n / power;
remainder = n - q * power;
n = d.umid;
if ((n | remainder) != 0)
{
d.umid = q = (uint)((((ulong)remainder << 32) | n) / power);
remainder = n - q * power;
}
n = d.ulo;
if ((n | remainder) != 0)
{
d.ulo = q = (uint)((((ulong)remainder << 32) | n) / power);
remainder = n - q * power;
}
}
}
checkRemainder:
if (mode == RoundingMode.Truncate)
goto done;
else if (mode == RoundingMode.ToEven)
{
// To do IEEE rounding, we add LSB of result to sticky bits so either causes round up if remainder * 2 == last divisor.
remainder <<= 1;
if ((sticky | d.ulo & 1) != 0)
remainder++;
if (power >= remainder)
goto done;
}
else if (mode == RoundingMode.AwayFromZero)
{
// Round away from zero at the mid point.
remainder <<= 1;
if (power > remainder)
goto done;
}
else if (mode == RoundingMode.Floor)
{
// Round toward -infinity if we have chopped off a non-zero amount from a negative value.
if ((remainder | sticky) == 0 || !d.IsNegative)
goto done;
}
else
{
Debug.Assert(mode == RoundingMode.Ceiling);
// Round toward infinity if we have chopped off a non-zero amount from a positive value.
if ((remainder | sticky) == 0 || d.IsNegative)
goto done;
}
if (++d.Low64 == 0)
d.uhi++;
done:
return;
}
internal static uint DecDivMod1E9(ref DecCalc value)
{
ulong high64 = ((ulong)value.uhi << 32) + value.umid;
ulong div64 = high64 / TenToPowerNine;
value.uhi = (uint)(div64 >> 32);
value.umid = (uint)div64;
ulong num = ((high64 - (uint)div64 * TenToPowerNine) << 32) + value.ulo;
uint div = (uint)(num / TenToPowerNine);
value.ulo = div;
return (uint)num - div * TenToPowerNine;
}
struct PowerOvfl
{
public readonly uint Hi;
public readonly ulong MidLo;
public PowerOvfl(uint hi, uint mid, uint lo)
{
Hi = hi;
MidLo = ((ulong)mid << 32) + lo;
}
}
static readonly PowerOvfl[] PowerOvflValues = new[]
{
// This is a table of the largest values that can be in the upper two
// uints of a 96-bit number that will not overflow when multiplied
// by a given power. For the upper word, this is a table of
// 2^32 / 10^n for 1 <= n <= 8. For the lower word, this is the
// remaining fraction part * 2^32. 2^32 = 4294967296.
//
new PowerOvfl(429496729, 2576980377, 2576980377), // 10^1 remainder 0.6
new PowerOvfl(42949672, 4123168604, 687194767), // 10^2 remainder 0.16
new PowerOvfl(4294967, 1271310319, 2645699854), // 10^3 remainder 0.616
new PowerOvfl(429496, 3133608139, 694066715), // 10^4 remainder 0.1616
new PowerOvfl(42949, 2890341191, 2216890319), // 10^5 remainder 0.51616
new PowerOvfl(4294, 4154504685, 2369172679), // 10^6 remainder 0.551616
new PowerOvfl(429, 2133437386, 4102387834), // 10^7 remainder 0.9551616
new PowerOvfl(42, 4078814305, 410238783), // 10^8 remainder 0.09991616
};
[StructLayout(LayoutKind.Explicit)]
private struct Buf12
{
[FieldOffset(0 * 4)]
public uint U0;
[FieldOffset(1 * 4)]
public uint U1;
[FieldOffset(2 * 4)]
public uint U2;
[FieldOffset(0)]
private ulong ulo64LE;
[FieldOffset(4)]
private ulong uhigh64LE;
public ulong Low64
{
get => BitConverter.IsLittleEndian ? ulo64LE : (((ulong)U1 << 32) | U0);
set
{
if (BitConverter.IsLittleEndian)
{
ulo64LE = value;
}
else
{
U1 = (uint)(value >> 32);
U0 = (uint)value;
}
}
}
/// <summary>
/// U1-U2 combined (overlaps with Low64)
/// </summary>
public ulong High64
{
get => BitConverter.IsLittleEndian ? uhigh64LE : (((ulong)U2 << 32) | U1);
set
{
if (BitConverter.IsLittleEndian)
{
uhigh64LE = value;
}
else
{
U2 = (uint)(value >> 32);
U1 = (uint)value;
}
}
}
}
[StructLayout(LayoutKind.Explicit)]
private struct Buf16
{
[FieldOffset(0 * 4)]
public uint U0;
[FieldOffset(1 * 4)]
public uint U1;
[FieldOffset(2 * 4)]
public uint U2;
[FieldOffset(3 * 4)]
public uint U3;
[FieldOffset(0 * 8)]
private ulong ulo64LE;
[FieldOffset(1 * 8)]
private ulong uhigh64LE;
public ulong Low64
{
get => BitConverter.IsLittleEndian ? ulo64LE : (((ulong)U1 << 32) | U0);
set
{
if (BitConverter.IsLittleEndian)
{
ulo64LE = value;
}
else
{
U1 = (uint)(value >> 32);
U0 = (uint)value;
}
}
}
public ulong High64
{
get => BitConverter.IsLittleEndian ? uhigh64LE : (((ulong)U3 << 32) | U2);
set
{
if (BitConverter.IsLittleEndian)
{
uhigh64LE = value;
}
else
{
U3 = (uint)(value >> 32);
U2 = (uint)value;
}
}
}
}
[StructLayout(LayoutKind.Explicit)]
private struct Buf24
{
[FieldOffset(0 * 4)]
public uint U0;
[FieldOffset(1 * 4)]
public uint U1;
[FieldOffset(2 * 4)]
public uint U2;
[FieldOffset(3 * 4)]
public uint U3;
[FieldOffset(4 * 4)]
public uint U4;
[FieldOffset(5 * 4)]
public uint U5;
[FieldOffset(0 * 8)]
private ulong ulo64LE;
[FieldOffset(1 * 8)]
private ulong umid64LE;
[FieldOffset(2 * 8)]
private ulong uhigh64LE;
public ulong Low64
{
get => BitConverter.IsLittleEndian ? ulo64LE : (((ulong)U1 << 32) | U0);
set
{
if (BitConverter.IsLittleEndian)
{
ulo64LE = value;
}
else
{
U1 = (uint)(value >> 32);
U0 = (uint)value;
}
}
}
public ulong Mid64
{
get => BitConverter.IsLittleEndian ? umid64LE : (((ulong)U3 << 32) | U2);
set
{
if (BitConverter.IsLittleEndian)
{
umid64LE = value;
}
else
{
U3 = (uint)(value >> 32);
U2 = (uint)value;
}
}
}
public ulong High64
{
get => BitConverter.IsLittleEndian ? uhigh64LE : (((ulong)U5 << 32) | U4);
set
{
if (BitConverter.IsLittleEndian)
{
uhigh64LE = value;
}
else
{
U5 = (uint)(value >> 32);
U4 = (uint)value;
}
}
}
public int Length => 6;
}
private struct Buf28
{
public Buf24 Buf24;
public uint U6;
public int Length => 7;
}
}
#endif
}
}
|