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
|
/******************************************************************************
Copyright (c) 2007-2024, Intel Corp.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#define BASE_E 0
#define BASE_2 1
#define BASE_10 2
#if MAKE_COMMON && !defined(BUILD_FILE_NAME)
# if (BASE_OF_LOG == BASE_2) || (LOG2)
# define BUILD_FILE_NAME F_LOG2_BUILD_FILE_NAME
# elif (BASE_OF_LOG == BASE_10) || (LOG10 || FAST_LOG10)
# define BUILD_FILE_NAME F_LOG10_BUILD_FILE_NAME
# else
# define BUILD_FILE_NAME F_LOG_BUILD_FILE_NAME
# endif
#endif
#include "dpml_private.h"
#if MAKE_INCLUDE && LOG10
# error "To generate table for base 10, define BASE_OF_LOG = BASE_10"
#endif
#if MAKE_INCLUDE && LOG2
# error "To generate table for base 2, define BASE_OF_LOG = BASE_2"
#endif
#if MAKE_INCLUDE
# if BASE_OF_LOG == BASE_2
# define BASE_NAME LOG2_BASE_NAME
# define MPLOG log2
# elif BASE_OF_LOG == BASE_10
# define BASE_NAME LOG10_BASE_NAME
# define MPLOG log10
# else
# define BASE_NAME LN_BASE_NAME
# define MPLOG log
# endif
# if MAKE_COMMON
# if BASE_OF_LOG == BASE_10
# define LOG_TABLE_NAME F_LOG10_TABLE_NAME
# elif BASE_OF_LOG == BASE_2
# define LOG_TABLE_NAME F_LOG2_TABLE_NAME
# else
# define LOG_TABLE_NAME F_LOG_TABLE_NAME
# endif
# else
# define LOG_TABLE_NAME TABLE_NAME
# endif
#endif
/*
* Compile time information, depending on which function is compiled:
* default function names and base names;
* error codes for the exception dispatcher (note that the FAST functions
* do not invoke the exception dispatching mechanism);
* and other function-related symbolic constants, e.g. natural log or not,
* fast function or not.
*/
#undef NATURAL
#undef DO_LOG1P
#undef FAST
#if !MAKE_INCLUDE
# if FAST_LOG
# define _F_ENTRY_NAME F_FAST_LN_NAME
# define BASE_NAME FAST_LN_BASE_NAME
# define NATURAL 1
# define FAST 1
# elif FAST_LOG10
# define _F_ENTRY_NAME F_FAST_LOG10_NAME
# define BASE_NAME FAST_LOG10_BASE_NAME
# define FAST 1
# elif LOG2
# define _F_ENTRY_NAME F_LOG2_NAME
# define BASE_NAME LOG2_BASE_NAME
# define LOG_ZERO LOG2_OF_ZERO
# define LOG_NEGATIVE LOG2_OF_NEGATIVE
# elif LOG10
# define _F_ENTRY_NAME F_LOG10_NAME
# define BASE_NAME LOG10_BASE_NAME
# define LOG_ZERO LOG10_OF_ZERO
# define LOG_NEGATIVE LOG10_OF_NEGATIVE
# elif LOG1P
# define _F_ENTRY_NAME F_LOG1P_NAME
# define BASE_NAME LN_BASE_NAME
# define LOG_ZERO LOG1P_M1
# define LOG_NEGATIVE LOG1P_LESS_M1
# define NATURAL 1
# define DO_LOG1P 1
# else
# define _F_ENTRY_NAME F_LN_NAME
# define BASE_NAME LN_BASE_NAME
# define LOG_ZERO LOG_OF_ZERO
# define LOG_NEGATIVE LOG_OF_NEGATIVE
# define NATURAL 1
# endif
#endif
#if MAKE_COMMON && QUAD_PRECISION
# error "Shared tables not yet implemented for quad precision"
#endif
#if FAST && QUAD_PRECISION
# error "Fast functions are not implemented in quad precision"
#endif
#if FAST && !MAKE_COMMON
# error "Fast functions expect shared tables"
#endif
/*
* SUMMARY OF THE ALGORITHM
*
* The algorithm uses a large table for argument reduction, and then
* polynomial approximation. Certain aspects of the algorithm were
* suggested in an article by Peter Tang, in Transactions on Mathematical
* Software, December 1990.
*
* In general, log of x can be computed by first reducing x to a small range
* near 1, and computing the log of the reduced argument. Write x = 2^m * f,
* where f is in the interval [1, 2], then log(x) = m * log(2) + log(f).
* Chop up the interval [1,2] into tiny subintervals [F(i), F(i+1)].
* Determine which division point F(j) is closest to f; then
* x = 2^m * f = 2^m * F(j) * ( f/F(j) ),
* where the last factor is very close to 1. Then
* log(x) = m * log(2) + log(F(j)) + log(f/F(j)) .
* Since w = f/F(j) is very close to 1, its log can be approximated with a
* relatively small degree polynomial in w - 1 or (1-w)/(1+w) (details in
* a following section).
*
* When x is quite close to 1, however, the roundoff error in computing the
* variable w becomes visible, and can lead to quite large relative error.
* First, when x is very close to 1 (and, say x > 1), then m = 0 and F(j) = 1.
* The terms m * log(2) and log(F(j)) are both zero, so we need an accurate
* approximation for log(f/F(j)). If x is a little farther from 1, so that
* j is a small positive integer, then m = 0 and F(j) is about 1 + j/2^LOG_K
* (depending on how F(j) is defined), and log(F(j)) is about j/2^LOG_K.
* The leading term of the polynomial in w can be as large as 1/2^(LOG_K + 1),
* which is not much smaller than log(F(j)) and possibly has opposite sign.
*
* To finesse this problem, the current implementation of log uses a special
* path for x "near 1". This approach is efficient because it permits a
* less stringent approximation for log(f/F(j)), provided that the cost
* of a branch is not too great. An exception: the single precision fast
* logs use one path, because the error bound of the polynomial
* approximation for log(f/F(j)) is within the (relaxed) error requirements
* for fast functions.
*
* ONE PATH
* The accurate log functions have a ONE_PATH compile option (non-shared),
* which is appropriate in a situation that severely penalizes branching.
* In the ONE_PATH algorithm, the variable f/F(j) - 1 is computed very
* carefully in hi and lo parts, and the linear term of the polynomial
* is carefully split up into hi and lo parts and added to the respective
* hi and lo terms of the expression. Unfortunately, the cost of this
* additional care has to be paid even when the care is not necessary,
* e.g. when x is large.
*
* TABLE CONSTANTS
* Both the ONE_PATH and the two path approaches store F(j), log(F(j)),
* and 1/F(j) in a table, and index into the table using the leading
* LOG_K fraction bits of f.
*
* In order to maintain the accuracy, computations use backup precision
* wherever possible.
*
* If no backup precision is available, log(F(j)) and (for the ONE_PATH
* algorithm) 1/F(j) are stored in the table in hi and lo parts.
* log(2) and, for base 2 and 10, log(e), are also given in hi and lo parts.
* The hi part of log(2) is generated to have at least F_EXP_WIDTH trailing
* zero bits so that m * log(2) will be an exact product. Similarly,
* the hi part of log(F(j)) has enough trailing zeros so that
* when it is added to m * log2_hi, no significant bits of log(F(j))_hi
* will be lost. The lo parts of log(2) and log(F) are given in full
* precision. Any roundoff error generated in computations that involve
* the lo parts will be shifted off.
*
* All of these constants, the coefficients for the approximation polynomials,
* and the table of F(j) data are stored in the same array.
*
* LOG1P
* The computation of log1p(x) is similar to that for ln(x). If x is not
* too close to zero, we compute ln(1+x) following Tang's recommendations
* to avoid roundoff error in the computation of (y - Fj)/Fj.
* If x is within the interval [T1 - 1, T2 - 1], then x + 1
* will be in the small interval [T1, T2] used in this implementation.
* If x is really close to zero, return x. The error cases are analogous
* to those for natural log.
*
* POLYNOMIAL APPROXIMATIONS
* There are two standard approaches for polynomial approximation for the
* natural log, ln(w), where w is close to 1.
* 1. Write f = y + F, then f/F = 1 + y/F = 1 + z. Using the Taylor's
* expansion,
* log(1 + z) = z - z^2/2 + z^3/3 + ... + (-1)^(n+1) * z^n / n + ...
* This suggests the minmax polynomial will be of the form
* Q(z) = z + C1*z^2 + C2*z^3 + ....
* This implementation computes the variable z by taking
* z = (f-F) * (1/F), where the reciprocal 1/F is stored in the F table.
*
* 2. Write z = ((f/F) - 1)/( (f/F) + 1) = (f - F)/(f + F).
* The approximation for log(1 + f/F) can be derived from the Taylor's
* expansion:
* log(1 + f/F) = 2z + 2* z^3/3 + 2* z^5/5 + ...
* The factor 2 can be absorbed into z, so that the final form of the
* polynomial is
* log(1 + f/F) = z + z^3/12 + z^5/80 + ...
* This suggests that the minmax polynomial will be
* z + z * (C1* (z^2) + C2* (z^2)^2 + ... = z + z*P(z^2).
*
* To achieve comparable accuracy, more terms are required in the first
* approach's polynomial (the "reciprocal" approach) than in the second
* (the "quotient" approach), but the second approach requires a division
* in computing the approximation variable.
*
* In this implementation, both the "normal path" (x not near 1) and the
* "near 1" path can be computed either with the first or second approach.
* By default, both paths use the approximation that avoids the use of
* floating point division. These defaults can be overridden with compile
* options:
* -U USE_RECIP for the "normal path"
* -U NO_DIVISIONS for the "near 1 path"
* These choices are independent of one another. The choices could also
* be tied to architecture considerations, e.g. the relative instruction
* speeds of divisions, and adds and multiplies. In any case, coefficients
* for both sets of polynomials are generated in the non-shared include files
* at build time.
*
*/
#if !defined(USE_RECIP)
#define USE_RECIP 1
#endif
#if !defined(NO_DIVISIONS)
#define NO_DIVISIONS 1
#endif
/*
* If the log base 10 or base 2 is required, since
* log_base(x) = ln(x) / ln(base) = ln(x) * log_base(e),
* each of the coefficients for the polynomial will be multiplied by
* by the appropriate base log of e. These factors will be absorbed into
* the coefficients, except for the linear term z, which will be multiplied
* by log_base(e) in hi and lo parts.
*
* All constants, including coefficients of the appropriate polynomials,
* are computed at build-time and are given in the shared or non-shared
* include files.
*/
#if ONE_PATH && MAKE_COMMON
# error "ONE_PATH not implemented using shared tables"
#endif
#if ONE_PATH && SINGLE_PRECISION && !PRECISION_BACKUP_AVAILABLE
# error "ONE_PATH requires using double precision as a backup"
#endif
/*
* ERROR CASES
* Special cases (negative input, zero, NaNs, infinity, reserved operand)
* and denormalized numbers are screened out early in the routine,
* by examining the sign and exponent fields. The accurate routines
* use the DPML exception dispatching mechanism to raise the appropriate
* error and return the appropriate value.
*
* VAX format:
* x = 0 raises an error ("log of zero")
* x < 0 raises an error ("log of negative")
*
* IEEE format:
* x = NaN returns the NaN, without raising an error
* x = +INFINITY returns +INFINITY, without raising an error.
* x = +denormal is scaled, and the log is computed.
* x = +0 returns -INFINITY, through the exception dispatcher.
* x negative (including x = -INFINITY and x = -0) returns a NaN
* through the exception dispatcher.
*
* The fast routines raise a floating point overflow for error cases.
* Input positive denorms are treated as zero in the fast routines (the
* accurate routines compute the correct result). If x was a NaN or
* positive infinity, the fast routines return x.
*
*
* ACCURACY
* The accuracy characteristics for this implementation (max error, in lsb):
*
* ln: .55
* log2: .58
* log10: .56
*
* Fast ln and log10: single precision < 1.0
* double precision < 4.0
*/
/*
* If the table is built non-shared and ONE_PATH, the symbolic constant
* DO_ONE_PATH will get defined in the include file, to access the
* ONE_PATH algorithmic steps in the code. Similarly, if the table is
* built shared, the symbolic constant DO_SHARED_TABLE will be defined
* in the generated include file. These constants can be used to check
* that compile-time options are consistent with built-time options.
*/
#undef DO_SHARED_TABLE
#undef DO_ONE_PATH
#if !MAKE_INCLUDE
# if MAKE_COMMON
# define TABLE_IS_EXTERNAL 1
# else
# undef TABLE_IS_EXTERNAL
# endif
# include STR(BUILD_FILE_NAME)
#endif
#if LOG1P && DO_ONE_PATH
# error Cannot use ONE_PATH algorithm for log1p
#endif
#if DO_ONE_PATH && !USE_RECIP
# error "ONE_PATH requires using USE_RECIP option"
#endif
#if DO_SHARED_TABLE && !USE_RECIP
# error "Shared table must have USE_RECIP true - no alternate polys"
#endif
#if DO_SHARED_TABLE && !NO_DIVISIONS
# error "Shared table must have NO_DIVISIONS true - no alternate polys"
#endif
/*
* MPHOC code to generate the include file.
*
* When processed by MPHOC, this code will generate arrays and definitions
* for various constants, coefficients, and the table of Fj data.
*
* Since the coefficients and tables are different, depending on whether
* ONE_PATH or two paths are required, if the one path algorithm is desired,
* ONE_PATH must be specified at build-time, and non-shared tables must be
* selected. The include file generated by ONE_PATH will set another
* symbolic constant, DO_ONE_PATH.
*
* The arrays contain:
* - various constants, e.g. 1.0, log(2), log(e);
* - coefficients for all the approximating polynomials
* - the table of Fj data, which contains Fj, 1/Fj and log(Fj) in either
* hi and lo parts or in backup precision, in the appropriate base.
*
* The endpoints for the "near 1" interval, T1 and T2, in a useful format,
* are also generated.
*
* Logarithms are given in base e by default. The compile options
* BASE_OF_LOG = BASE_2 or BASE_10 generate the include files for the
* log2 and the log10 families of functions.
*
*/
#if !defined(LOG_K)
# if (!(MAKE_COMMON)) && PRECISION_BACKUP_AVAILABLE
# define LOG_K 6
# else
# define LOG_K 7
# endif
#endif
# define TMP_FILE ADD_EXTENSION(BUILD_FILE_NAME,tmp)
#if MAKE_COMMON
# define START_TABLE(n,o) START_GLOBAL_TABLE(n,o)
#else
# define START_TABLE(n,o) START_STATIC_TABLE(n,o)
#endif
#if MAKE_INCLUDE
@divert divertText
/*
* Various constants, e.g. log(2), will be built with EXP_WIDTH trailing
* zeros, so that multiplying by the exponent will not lose any bits.
*/
#define PARTIAL_PRECISION (B_PRECISION - B_EXP_WIDTH)
/*
* Print statements.
*/
#define PRINT_F_ITEM(a) PRINT_1_TYPE_ENTRY(F_CHAR, a, offset)
#define PRINT_R_ITEM(a) PRINT_1_TYPE_ENTRY(R_CHAR, a, offset)
#define PRINT_B_ITEM(a) PRINT_1_TYPE_ENTRY(B_CHAR, a, offset)
#if MAKE_COMMON
# define PRINT_POLY_ITEM(a) PRINT_B_ITEM(a)
#else
# define PRINT_POLY_ITEM(a) PRINT_F_ITEM(a)
#endif
#define PRINT_TABLE_ITEM( a, b, c, d, i) \
printf("\t/* %3i */", BYTES(offset));\
printf("\t %#.4"STR(B_CHAR)",", a);\
printf("\t %#.4"STR(B_CHAR)",", b);\
printf("\t %#.4"STR(B_CHAR)",", c);\
printf("\t %#.4"STR(B_CHAR)",", d);\
printf("\t/* row %i */ \n", i); \
offset += 4*BITS_PER_B_TYPE;
#define PRINT_ONE_PATH_TABLE_ITEM( a, b, c, d, z, i) \
printf("\t/* %3i */", BYTES(offset));\
printf("\t %#.4"STR(R_CHAR)",", a);\
printf("\t %#.4"STR(R_CHAR)",", b);\
printf("\t %#.4"STR(B_CHAR)",", c);\
printf("\t %#.4"STR(B_CHAR)",", d);\
printf("\t %#.4"STR(B_CHAR)",", z);\
printf("\t/* row %i */ \n", i); \
offset += 4*BITS_PER_B_TYPE;
# define SET_MP_PREC(x) precision = x
# define WORKING_PRECISION ceil( (B_PRECISION + 1) / MP_RADIX_BITS) + 2
# define WORKING_REMES_PREC ( ceil(2*B_PRECISION/MP_RADIX_BITS) + 5) + 10
/*
* MPHOC function for remes approximation of log(z), using the Taylor's
* series.
*/
function log_x_plus_1_over_x()
{
if ($1 == 0)
return 1;
else {
if (($1 < 1/MP_RADIX) && ($1 > - 1/MP_RADIX))
return (logx1($1))/($1);
else
return log(1 + $1)/($1);
}
}
function
do_regular_coeffs()
{
a = - $1;
b = $1;
remes_bits_of_accuracy = $2;
remes(REMES_FIND_POLYNOMIAL + REMES_RELATIVE_WEIGHT + REMES_LINEAR_ARG,
a, b, log_x_plus_1_over_x, remes_bits_of_accuracy,
&remes_degree_numer, &remes_coeff_numer);
for (i = 1; i <= remes_degree_numer ; i++) {
y = remes_coeff_numer[i];
PRINT_POLY_ITEM( y * log_of_e);
}
return (remes_degree_numer);
}
/*
* MPHOC function for remes approximation of log(z), using the variable
* z = (1 + x)/(1 - x)
*/
function atanh_ov_x()
{
if ($1 == 0)
return 2;
else
return 2*atanh($1)/($1);
}
function
do_quot_coeffs()
{
a = 0;
b = $1;
remes_bits_of_accuracy = $2;
remes(REMES_FIND_POLYNOMIAL + REMES_RELATIVE_WEIGHT + REMES_SQUARE_ARG,
a, b, atanh_ov_x, remes_bits_of_accuracy,
&remes_degree_numer, &remes_coeff_numer);
div_by_two = 2;
for (i = 1; i <= remes_degree_numer ; i++) {
div_by_two *= 4;
y = remes_coeff_numer[i];
PRINT_F_ITEM( y * log_of_e / div_by_two);
}
return (remes_degree_numer);
}
/*
* Constant log(2), either in backup precision or in hi and lo parts.
*
* For base 2 and 10, constant log(e), either in backup precision or in
* hi and lo parts. The hi and lo parts are given in two ways:
* LOGE_HI and LOGE_LO are in full working precision
* LOGE_HI2 and LOGE_LO2 are in half precision ("shortened").
* Note that LOGE_HI2 + LOGE_LO2 = LOGE_HI.
*/
procedure do_constants() {
v = MPLOG(2);
#if MAKE_COMMON || !PRECISION_BACKUP_AVAILABLE
TABLE_COMMENT("log of 2 in hi and lo parts");
#if ONE_PATH
y = bround(v, R_PRECISION);
#else
y = rint(part_prec * v) / part_prec;
#endif
z = v - y;
PRINT_TABLE_VALUE_DEFINE(LOG2_HI, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM( y);
PRINT_TABLE_VALUE_DEFINE(LOG2_LO, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM( z);
#else
PAD_IF_NEEDED(offset, BITS_PER_B_TYPE);
TABLE_COMMENT("log of 2 in full precision");
PRINT_TABLE_VALUE_DEFINE(LOG2_HI, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(v);
#endif
#if (BASE_OF_LOG != BASE_E)
TABLE_COMMENT("log of e, in hi and lo parts");
a = bround(log_of_e, B_PRECISION);
b = bround(log_of_e, F_PRECISION/2);
c = log_of_e - a;
d = log_of_e - b;
# if PRECISION_BACKUP_AVAILABLE && !MAKE_COMMON
PRINT_TABLE_VALUE_DEFINE(LOGE_HI, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(a);
PRINT_TABLE_VALUE_DEFINE(LOGE_LO, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(c);
# else
PRINT_TABLE_VALUE_DEFINE(LOGE_HI, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(a);
PRINT_TABLE_VALUE_DEFINE(LOGE_HI2, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(b);
PRINT_TABLE_VALUE_DEFINE(LOGE_LO, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(c);
PRINT_TABLE_VALUE_DEFINE(LOGE_LO2, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(d);
# endif
#endif
}
/*
* There are 3 ways to build the table of F(j) and related data:
* 1. if ONE_PATH and DOUBLE_PRECISION or larger, we need to store 1/F(j)
* in hi and lo parts, to preserve accuracy. So each row
* of the table contains 5 items:
*
* F(j) = 1 + j/2^LOG_K in "half" precision
* hi part of log(F(j)) in "half" precision
* full precision form of 1/F(j)
* lo part of 1/F(j) where (1/F)_full - (1/F)_lo is "short"
* (has around "half" precision - 3 bits)
* lo part of log(F(j))
*
* 2. each row contains F(j), 1/F(j), and logF(j) in hi and lo parts.
* But F(j) can be
* F(j) = 1 + j/2^LOG_K + 1/2^(LOG_K+1)
*
* or
*
* 3. F(j) = 1 + j/2^LOG_K.
*
* The first format allows indexing to be a little simpler; this
* is used in the non-shared table. The second format allows the
* table to be used for single precision fast log, which does not
* have a special path near 1. The second format is used in the
* shared table.
*/
procedure do_table() {
#if (!SINGLE_PRECISION && ONE_PATH)
TABLE_COMMENT("Table of F and log(F) hi in half-precision, ");
TABLE_COMMENT(" and 1/F, 1/F lo, and log(F) lo in working precision");
for (i = 0; i <= two_to_k; i++) {
a = 1 + (i/two_to_k);
b = 1/a;
d = bround(b, R_PRECISION - 3);
w = b - d;
x = MPLOG(a);
y = bround(x, R_PRECISION);
z = x - y;
PRINT_ONE_PATH_TABLE_ITEM( a, y, b, w, z, i);
}
#else
TABLE_COMMENT("Table of F, 1/F, and hi and lo log of F");
# if (MAKE_COMMON || (ONE_PATH && SINGLE_PRECISION))
for (i = 0; i <= two_to_k; i++) {
a = 1 + (i/two_to_k);
# else
for (i = 0; i < two_to_k; i++) {
a = 1 + (i/two_to_k) + (1/two_to_k_plus_1);
# endif
d = 1/a;
x = MPLOG(a);
y = rint(part_prec * x) / part_prec;
z = x - y;
PRINT_TABLE_ITEM( a, d, y, z, i);
}
#endif
}
/*
* This procedure generates T1 and T2 (the boundaries of the interval
* "near 1") given as "integers" in the form sign-expon-frac.
*
* The values of T1 and T2 depend on LOG_K. If x > 1 is very close to 1,
* say, m = 0, but j > 0, then
* log(x) = log(F(j))_hi + z + poly(z) + log(F(j))_lo
* where z = (x - F(j))* 1/F(j). Although x - F(j) is exact, multiplying
* by 1/F causes a rounding error (3/2 lsb in the worst case). log(F(j)) is
* approximately j/2^LOG_K, and is given in extra precision. If z is
* computed only to working precision, then by forcing an alignment shift
* of at least 2 bits, the error in z is safely shifted off. When using
* backup precision, a smaller alignment shift will work.
* The alignment shift used here is log2(SAFE_LIM) = 3 if no backup
* type exists, and an alignment shift of approximately 1 if there is
* backup, leading to a max relative error of approx .56 lsb (double).
* The smallest value of F(j) used is 1 + SAFE_LIM/2^LOG_K = T2, and
* 1 - SAFE_LIM/2^LOG_K for T1.
*
* In base 2 or 10, z is multiplied by log(e), which introduces an additional
* 1 bit rounding error in the variable. If we enlarge the interval [T1,T2]
* slightly, this error is also shifted off.
*
* The size of the error is related to the alignment shift: an additional
* shift of 1 bit reduces the (error - .5) by approximately 1/2.
* This corresponds to doubling the size of the interval [T1, T2].
*
* In the code, the hi word of input x is extracted into integer variable
* hi_x, with sign, exponent, and most significant fraction bits in the
* msb part of the integer. VAX floating point will be shuffled to match
* this format, if there are insufficient fraction bits in the hi word.
* Then, we subtract and use unsigned integer compares
* to determine whether x was in the interval [T1, T2].
*/
#if IEEE_FLOATING
# define R_SAFE_LIM 2
#else
# define R_SAFE_LIM 4
#endif
#if BASE_OF_LOG == BASE_E
# define B_SAFE_LIM 8.5
#else
# define B_SAFE_LIM 9.75
#endif
#if PRECISION_BACKUP_AVAILABLE
# define EXTRA_N 2
# define EXTRA_N_Q 2
# define EXTRA_A_Q -2
#else
# define EXTRA_N 4
# if (QUAD_PRECISION && !NATURAL)
# define EXTRA_N_Q 5
# else
# define EXTRA_N_Q -1
# endif
# define EXTRA_A_Q 2
#endif
#if (VAX_FLOATING && (LOG_K > R_EXP_POS))
# define R_FRAC_BITS_NEEDED (R_EXP_POS + 16)
#else
# define R_FRAC_BITS_NEEDED (R_EXP_POS)
#endif
#define B_FRAC_BITS_NEEDED (31 - B_EXP_WIDTH)
#define R_EXP_POS_64 R_EXP_POS
#define R_EXP_POS_32 R_EXP_POS
#define B_EXP_POS_64 (63 - B_EXP_WIDTH)
#define B_EXP_POS_32 (31 - B_EXP_WIDTH)
#define R_BIAS_M1 (R_EXP_BIAS - 1 - F_NORM)
#define B_BIAS_M1 (B_EXP_BIAS - 1 - F_NORM)
#if (F_PRECISION < 32)
# define F_EXP_POS_64 R_EXP_POS_64
# define F_EXP_POS_32 R_EXP_POS_32
# define F_FRAC_BITS_NEEDED R_FRAC_BITS_NEEDED
# define F_SAFE_LIM R_SAFE_LIM
# define F_BIAS R_BIAS_M1
#else
# define F_EXP_POS_64 B_EXP_POS_64
# define F_EXP_POS_32 B_EXP_POS_32
# define F_FRAC_BITS_NEEDED B_FRAC_BITS_NEEDED
# define F_SAFE_LIM B_SAFE_LIM
# define F_BIAS B_BIAS_M1
#endif
procedure
do_T_consts()
{
#if MAKE_COMMON
x = 1 - (R_SAFE_LIM - 0.5)/2^(LOG_K );
i = bexp(x);
x = bround(x, R_FRAC_BITS_NEEDED + 1);
frac1 = bextr(x, 2, R_FRAC_BITS_NEEDED + 1);
y = 1 + R_SAFE_LIM/2^(LOG_K ) ;
j = bexp(y);
y = bround(y, R_FRAC_BITS_NEEDED + 1);
frac2 = bextr(y, 2, R_FRAC_BITS_NEEDED + 1);
a = ( (R_BIAS_M1 + i) * 2^R_EXP_POS_64) +
frac1 * 2^(R_EXP_POS_64 - R_FRAC_BITS_NEEDED);
b = ( (R_BIAS_M1 + j) * 2^R_EXP_POS_64) +
frac2 * 2^(R_EXP_POS_64 - R_FRAC_BITS_NEEDED);
printf("\n#define R_T1_64 (WORD) %#..16i ", a);
printf("\n#define R_T2_64 (WORD) %#..16i ", b);
c = ( (R_BIAS_M1 + i) * 2^R_EXP_POS_32) +
frac1 * 2^(R_EXP_POS_32 - R_FRAC_BITS_NEEDED);
d = ( (R_BIAS_M1 + j) * 2^R_EXP_POS_32) +
frac2 * 2^(R_EXP_POS_32 - R_FRAC_BITS_NEEDED);
printf("\n#define R_T1_32 (WORD) %#..16i ", c);
printf("\n#define R_T2_32 (WORD) %#..16i ", d);
x = 1 - (B_SAFE_LIM - 1)/2^(LOG_K );
i = bexp(x);
x = bround(x, B_FRAC_BITS_NEEDED + 1);
frac1 = bextr(x, 2, B_FRAC_BITS_NEEDED + 1);
y = 1 + B_SAFE_LIM/2^(LOG_K ) ;
j = bexp(y);
y = bround(y, B_FRAC_BITS_NEEDED + 1);
frac2 = bextr(y, 2, B_FRAC_BITS_NEEDED + 1);
a = ( (B_BIAS_M1 + i) * 2^B_EXP_POS_64) +
frac1 * 2^(B_EXP_POS_64 - B_FRAC_BITS_NEEDED);
b = ( (B_BIAS_M1 + j) * 2^B_EXP_POS_64) +
frac2 * 2^(B_EXP_POS_64 - B_FRAC_BITS_NEEDED);
printf("\n#define B_T1_64 (WORD) %#..16i ", a);
printf("\n#define B_T2_64 (WORD) %#..16i ", b);
c = ( (B_BIAS_M1 + i) * 2^B_EXP_POS_32) +
frac1 * 2^(B_EXP_POS_32 - B_FRAC_BITS_NEEDED);
d = ( (B_BIAS_M1 + j) * 2^B_EXP_POS_32) +
frac2 * 2^(B_EXP_POS_32 - B_FRAC_BITS_NEEDED);
printf("\n#define B_T1_32 (WORD) %#..16i ", c);
printf("\n#define B_T2_32 (WORD) %#..16i ", d);
#else
x = 1 - F_SAFE_LIM/2^(LOG_K );
i = bexp(x);
x = bround(x, F_FRAC_BITS_NEEDED + 1);
frac1 = bextr(x, 2, F_FRAC_BITS_NEEDED + 1);
y = 1 + F_SAFE_LIM/2^(LOG_K ) ;
j = bexp(y);
y = bround(y, F_FRAC_BITS_NEEDED + 1);
frac2 = bextr(y, 2, F_FRAC_BITS_NEEDED + 1);
a = ( (F_BIAS + i) * 2^F_EXP_POS_64) +
frac1 * 2^(F_EXP_POS_64 - F_FRAC_BITS_NEEDED);
b = ( (F_BIAS + j) * 2^F_EXP_POS_64) +
frac2 * 2^(F_EXP_POS_64 - F_FRAC_BITS_NEEDED);
printf("\n#define T1_64 (WORD) %#..16i ", a);
printf("\n#define T2_64 (WORD) %#..16i ", b);
c = ( (F_BIAS + i) * 2^F_EXP_POS_32) +
frac1 * 2^(F_EXP_POS_32 - F_FRAC_BITS_NEEDED);
d = ( (F_BIAS + j) * 2^F_EXP_POS_32) +
frac2 * 2^(F_EXP_POS_32 - F_FRAC_BITS_NEEDED);
printf("\n#define T1_32 (WORD) %#..16i ", c);
printf("\n#define T2_32 (WORD) %#..16i ", d);
#endif
printf("\n#define T2_MINUS_T1 (T2 - T1) \n");
}
/*
* The MPHOC code itself.
*
* The first constant is 1.0, used in scaling x to the interval [1,2).
* In the shared table, 1.0 is given in both double and single precision.
* The shared table also contains a big number which, when squared,
* generates an overflow.
*/
working_prec = WORKING_PRECISION;
SET_MP_PREC(working_prec);
log_of_e = MPLOG(exp(1.0));
part_prec = 2^PARTIAL_PRECISION;
two_to_k = 2^LOG_K;
two_to_k_plus_1 = 2^(LOG_K+1);
#if MAKE_COMMON
printf("\n#include \"dpml_private.h\"\n\n");
#endif
printf("\n#define LOG_TABLE_NAME "STR(LOG_TABLE_NAME));
printf("\n#define TABLE_CONST %i\n\n", LOG_K);
#if ONE_PATH
printf("\n#define DO_ONE_PATH 1\n");
#endif
#if MAKE_COMMON
printf("\n#define DO_SHARED_TABLE 1\n");
printf("\n#if !TABLE_IS_EXTERNAL\n");
#endif
START_TABLE(LOG_TABLE_NAME, offset);
#if MAKE_COMMON
TABLE_COMMENT("1.0 in double precision");
PRINT_TABLE_VALUE_DEFINE(B_ONE, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(1);
TABLE_COMMENT("1.0 in single precision");
PRINT_TABLE_VALUE_DEFINE(R_ONE, LOG_TABLE_NAME, offset, R_TYPE);
PRINT_R_ITEM(1);
PAD_IF_NEEDED(offset, BITS_PER_B_TYPE);
TABLE_COMMENT("max float, to generate overflow in fast log");
PRINT_TABLE_VALUE_DEFINE(OVF_LIM, LOG_TABLE_NAME, offset, B_TYPE);
PRINT_B_ITEM(MPHOC_D_POS_HUGE);
working_prec = WORKING_REMES_PREC;
SET_MP_PREC(working_prec);
/*
* The shared table includes poly coefficients, in single and double
* precision, for both accurate and fast log. Seven polynomials are
* generated:
* 'accurate' single precision, near 1 and away from 1
* 'fast' single precision, away from 1 (we use one path)
* 'accurate' double precision, near 1 and away from 1
* 'fast' double precision, near 1 and away from 1
*
* Single precision coefficients are stored in double precision.
* The approximation interval for F_FLOAT is twice as large as for S_FLOAT,
* because of the interaction of table size, VAX float format, and indexing.
*
* Double precision fast coefficients are split into two polynomials,
* to allow greater flexibility in adding terms, for performance.
*/
TABLE_COMMENT("accurate poly coeffs, single precision, near 1");
PRINT_TABLE_ADDRESS_DEFINE(R_POLY_ADD_ACC_NEAR, LOG_TABLE_NAME,
offset, B_TYPE);
max_arg = R_SAFE_LIM/(2^(LOG_K ));
deg_sa_near = do_regular_coeffs(max_arg, R_PRECISION + 2);
TABLE_COMMENT("accurate/fast poly coeffs, single precision, away from 1");
PRINT_TABLE_ADDRESS_DEFINE(R_POLY_ADD_AWAY, LOG_TABLE_NAME,
offset, B_TYPE);
# if IEEE_FLOATING
max_arg = 1/(2^(LOG_K + 1));
# else
max_arg = 1/(2^(LOG_K));
# endif
deg_sa_away = do_regular_coeffs(max_arg, R_PRECISION - 3 );
TABLE_COMMENT("accurate poly coeffs, double precision, near 1");
PRINT_TABLE_ADDRESS_DEFINE(B_POLY_ADD_ACC_NEAR, LOG_TABLE_NAME,
offset, B_TYPE);
max_arg = B_SAFE_LIM/(2^(LOG_K ));
deg_da_near = do_regular_coeffs(max_arg, B_PRECISION + 4);
TABLE_COMMENT("constant 1, the linear coeff of the fast near 1 poly");
PRINT_B_ITEM(log_of_e);
TABLE_COMMENT("fast poly coeffs, double precision, near 1");
PRINT_TABLE_ADDRESS_DEFINE(B_POLY_ADD_FAST_NEAR, LOG_TABLE_NAME,
offset, B_TYPE);
max_arg = B_SAFE_LIM/(2^(LOG_K ));
deg_df_near = do_regular_coeffs(max_arg, B_PRECISION + 2);
TABLE_COMMENT("accurate poly coeffs, double precision, away from 1");
PRINT_TABLE_ADDRESS_DEFINE(B_POLY_ADD_ACC_AWAY, LOG_TABLE_NAME,
offset, B_TYPE);
max_arg = 1/(2^(LOG_K + 1));
deg_da_away = do_regular_coeffs(max_arg, B_PRECISION - 2 );
TABLE_COMMENT("fast poly coeffs, double precision, away from 1");
PRINT_TABLE_ADDRESS_DEFINE(B_POLY_ADD_FAST_AWAY, LOG_TABLE_NAME,
offset, B_TYPE);
max_arg = 1/(2^(LOG_K + 1));
deg_df_away = do_regular_coeffs(max_arg, B_PRECISION - 8);
#else
/*
* The non-shared poly coefficients are all given in working precision.
* They include either the ONE_PATH coefficients (one poly), or
* near-1 and away-from-1 coefficients, each given in the default expansion
* or the form that requires a divide (4 polys).
*/
TABLE_COMMENT("1.0 in working precision");
PRINT_TABLE_VALUE_DEFINE(F_ONE, LOG_TABLE_NAME, offset, F_TYPE);
PRINT_F_ITEM(1);
working_prec = WORKING_REMES_PREC;
SET_MP_PREC(working_prec);
# if ONE_PATH
TABLE_COMMENT("poly coeffs for ONE_PATH");
PRINT_TABLE_ADDRESS_DEFINE(POLY_ADDRESS_ONEP, LOG_TABLE_NAME, offset, F_TYPE);
max_arg = 1/(2^(LOG_K + 1));
deg_away = do_regular_coeffs(max_arg, F_PRECISION + 3);
# else
TABLE_COMMENT("poly coeffs, near 1");
PRINT_TABLE_ADDRESS_DEFINE(POLY_ADDRESS_NEAR, LOG_TABLE_NAME,
offset, F_TYPE);
max_arg = F_SAFE_LIM/(2^(LOG_K ));
deg_near = do_regular_coeffs(max_arg, F_PRECISION + EXTRA_N);
TABLE_COMMENT("poly coeffs, quotient, near 1");
PRINT_TABLE_ADDRESS_DEFINE(POLY_ADD_N_Q, LOG_TABLE_NAME,
offset, F_TYPE);
max_arg = F_SAFE_LIM/2^(LOG_K);
deg_near_q = do_quot_coeffs(max_arg, F_PRECISION + EXTRA_N_Q);
for (j = 0; j < deg_near_q ; j++) {
printf("\n#define B%i POLY_ADD_N_Q[%i]", (2*j + 3), j);
}
TABLE_COMMENT("poly coeffs, away from 1");
PRINT_TABLE_ADDRESS_DEFINE(POLY_ADDRESS_AWAY, LOG_TABLE_NAME,
offset, F_TYPE);
max_arg = 1/(2^(LOG_K + 1));
deg_away = do_regular_coeffs(max_arg, F_PRECISION - 2 );
TABLE_COMMENT("poly coeffs, quotient, away from 1");
PRINT_TABLE_ADDRESS_DEFINE(POLY_ADD_A_Q, LOG_TABLE_NAME,
offset, F_TYPE);
max_arg = 1/2^LOG_K;
deg_away_q = do_quot_coeffs(max_arg, F_PRECISION + EXTRA_A_Q );
for (j = 0; j < deg_away_q ; j++) {
printf("\n#define C%i POLY_ADD_A_Q[%i]", (2*j + 3), j);
}
# endif
printf("\n");
PAD_IF_NEEDED(offset, BITS_PER_B_TYPE);
#endif
working_prec = 2* WORKING_PRECISION;
SET_MP_PREC(working_prec);
/*
* The constants include MPLOG(2), either in backup precision or in hi and
* lo parts, and MPLOG(e), in backup precision or in hi and lo parts.
*/
do_constants();
/*
* The table of F(j), reciprocal of F(j), and logF(j).
*/
printf("\n#define LOG_F_TABLE %i \n", BYTES(offset) );
do_table();
END_TABLE;
#if MAKE_COMMON
printf("\n#else\n");
printf("\n extern const TABLE_UNION "STR(LOG_TABLE_NAME)"[]; \n");
printf("\n#endif\n");
#endif
/*
* The constants T1 and T2, which define the endpoints of the "near 1"
* interval.
*/
#if !ONE_PATH
do_T_consts();
#endif
/*
#define GENP(addr, name) shell(STR(GENPOLY_EXECUTABLE)" \
" offset=-%i degree=%i " \
"define=\"" STR(macro) "(x,y) y = \" " \
"cn=\"" STR(table) "[%%d]\" "
*/
#if MAKE_COMMON
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 c2=0 cn=\"R_POLY_ADD_ACC_NEAR[%%d]\" define=\"R_POLY_NEAR(x,y) y = \" ", deg_sa_near + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 cn=\"R_POLY_ADD_AWAY[%%d]\" define=\"R_POLY_AWAY(x,y) y = \" ", deg_sa_away + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 c2=0 cn=\"B_POLY_ADD_ACC_NEAR[%%d]\" define=\"B_POLY_NEAR(x,y) y = \" ", deg_da_near + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 cn=\"B_POLY_ADD_FAST_NEAR[%%d]\" define=\"B_FAST_POLY_NEAR(x,y) y = \" ", deg_df_near + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 cn=\"B_POLY_ADD_ACC_AWAY[%%d]\" define=\"B_POLY_AWAY(x,y) y = \" ", deg_da_away + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 cn=\"B_POLY_ADD_FAST_AWAY[%%d]\" define=\"B_FAST_POLY_AWAY(x,y) y = \" ",3);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 c2=0 c3=0 cn=\"B_POLY_ADD_FAST_AWAY[%%d]\" define=\"B_FAST_POLY_AWAY2(x,y) y = \" ", deg_df_away + 1);
#else
# if ONE_PATH
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 cn=\"POLY_ADDRESS_ONEP[%%d]\" define=\"POLY_AWAY(x,y) y = \" ", deg_away + 1);
# else
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 c2=0 cn=\"POLY_ADDRESS_NEAR[%%d]\" define=\"POLY_NEAR(x,y) y = \" ", deg_near + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one odd degree=%i c1=0 cn=B%%d define=\"POLY_NEAR_Q(x,y) y = \" ", 2*deg_near_q + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one offset=-2 degree=%i c0=0 c1=0 cn=\"POLY_ADDRESS_AWAY[%%d]\" define=\"POLY_AWAY(x,y) y = \" ", deg_away + 1);
shell(""STR(GENPOLY_EXECUTABLE)" one odd degree=%i c1=0 cn=C%%d define=\"POLY_AWAY_Q(x,y) y = \" ", 2*deg_away_q + 1);
# endif
#endif
printf("\n\n");
@end_divert
@eval my $outText = MphocEval( GetStream( "divertText" ) ); \
my $defineText = Egrep( "#define", $outText, \$tableText ); \
my $headerText = GetHeaderText( STR(BUILD_FILE_NAME), \
"Definitions and constants for " . \
STR(F_ENTRY_NAME), __FILE__); \
print "$headerText\n\n$tableText\n\n$defineText";
#endif
/*
* MACROS
*/
/*
* Defines for the poly evaluations.
*/
#if DO_SHARED_TABLE
# if FAST
# if SINGLE_PRECISION
# define LO_POLY_AWAY R_POLY_AWAY
# define HI_POLY_AWAY
# define POLY_NEAR
# else
# define LO_POLY_AWAY B_FAST_POLY_AWAY
# define HI_POLY_AWAY B_FAST_POLY_AWAY2
# define POLY_NEAR B_FAST_POLY_NEAR
# endif
# else
# if SINGLE_PRECISION
# define POLY_AWAY R_POLY_AWAY
# define POLY_NEAR R_POLY_NEAR
# else
# define POLY_AWAY B_POLY_AWAY
# define POLY_NEAR B_POLY_NEAR
# endif
# endif
#endif
#if FAST
# define EVAL_NEAR_POLY POLY_NEAR
# define EVAL_LO_FAR_POLY LO_POLY_AWAY
# define EVAL_HI_FAR_POLY HI_POLY_AWAY
#endif
#if USE_RECIP
# define EVAL_FAR_POLY POLY_AWAY
#else
# define EVAL_FAR_POLY POLY_AWAY_Q
#endif
#if NO_DIVISIONS
# if DO_ONE_PATH
# define EVAL_NEAR_POLY
# else
# define EVAL_NEAR_POLY POLY_NEAR
# endif
#else
# define EVAL_NEAR_POLY POLY_NEAR_Q
#endif
/*
* Macros for naming the endpoints of the "near 1" interval, depending
* on precision and BITS_PER_WORD.
*/
#if DO_SHARED_TABLE
# if SINGLE_PRECISION
# define T1_64 R_T1_64
# define T2_64 R_T2_64
# define T1_32 R_T1_32
# define T2_32 R_T2_32
# elif DOUBLE_PRECISION
# define T1_64 B_T1_64
# define T2_64 B_T2_64
# define T1_32 B_T1_32
# define T2_32 B_T2_32
# endif
#endif
#if (BITS_PER_WORD == 64)
# define T1 T1_64
# define T2 T2_64
#else
# define T1 T1_32
# define T2 T2_32
#endif
#if DO_SHARED_TABLE
# if SINGLE_PRECISION
# define ONE R_ONE
# else
# define ONE B_ONE
# endif
#else
# define ONE F_ONE
#endif
/*
* Macros for fetching data from the F table.
*/
#if DO_ONE_PATH && !PRECISION_BACKUP_AVAILABLE
# define GET_F(j) *((R_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j )))
# define LOGF_HI(j) *((R_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + BYTES_PER_R_TYPE))
# define RECIP_F_FULL(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + BYTES_PER_B_TYPE))
# define RECIP_F_LO(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + 2*BYTES_PER_B_TYPE))
# define LOGF_LO(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + 3*BYTES_PER_B_TYPE))
#else
# define GET_F(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j )))
# define RECIP_F(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + BYTES_PER_B_TYPE))
# define LOGF_HI(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + 2*BYTES_PER_B_TYPE))
# define LOGF_LO(j) *((B_TYPE *) ((char *)LOG_TABLE_NAME + LOG_F_TABLE + (j ) + 3*BYTES_PER_B_TYPE))
#endif
/*
* For log1p, fetch the constant 1.0, and add it to x (provided that rounding
* modes are not "round up" and x is not fmax). For IEEE, screen out NaNs
* and infinities, and also input near 0 (including denorms).
*/
#if DO_LOG1P
# if VAX_FLOATING
# define LOGP_CHANGE_VAR_AND_GET_ONE(x, tmpx, float_one, j, m, label) \
float_one = ONE; \
tmpx = x + float_one;
# else
# define LOWER_LIMIT (F_EXP_BIAS - F_PRECISION - 2)
# if DYNAMIC_ROUNDING_MODES
# define LOGP_CHANGE_VAR_AND_GET_ONE(x, tmpx, float_one, j, m, label) \
GET_EXP_WORD(x, j); \
float_one = ONE; \
m = (j & F_EXP_MASK); \
if (m <= ((U_WORD) LOWER_LIMIT << F_EXP_POS)) return (x); \
if (m > ((U_WORD)F_MAX_BIASED_EXP << F_EXP_POS)) goto label; \
if (m == ((U_WORD)F_MAX_BIASED_EXP << F_EXP_POS)) tmpx = x; \
else tmpx = x + float_one;
# else
# define LOGP_CHANGE_VAR_AND_GET_ONE(x, tmpx, float_one, j, m, label) \
GET_EXP_WORD(x, j); \
float_one = ONE; \
m = (j & F_EXP_MASK); \
if (m > ((U_WORD)F_MAX_BIASED_EXP << F_EXP_POS)) goto label; \
if (m <= ((U_WORD) LOWER_LIMIT << F_EXP_POS)) return (x); \
tmpx = x + float_one;
# endif
# endif
#else
# define LOGP_CHANGE_VAR_AND_GET_ONE(x, tmpx, float_one, j, m, label)
#endif
/*
* Macro GET_HI_WORD gets the sign, exponent, and hi fraction bits, of either
* the original x, or (for log1p) of temp_x.
*
* VAX F format, accurate log, is treated specially, because we don't want
* to incur a penalty for using the "long" indexing, which costs 2 extra
* instructions. Otherwise, for VAX format, if INDEX_BITS_NEEDED >
* number of contiguous hi fraction bits, we do a PDP shuffle to get more
* fraction bits. The fast F format log needs the first non-contiguous
* fraction bit as "rounding" information, because it does not have a
* special path near 1 and so has to be careful.
*
* The resulting integer word is used in screening for "near 1" and in
* computing the index. We also need to know the location of x's sign bit.
*/
#if DO_SHARED_TABLE && VAX_FLOATING && (TABLE_CONST == F_EXP_POS)
# define SPECIAL_VAX 1
#else
# define SPECIAL_VAX 0
#endif
#if DO_ONE_PATH || (DO_SHARED_TABLE && !SPECIAL_VAX)
# define LONG_INDEX 1
# define INDEX_BITS_NEEDED (TABLE_CONST + 1)
#else
# define LONG_INDEX 0
# define INDEX_BITS_NEEDED TABLE_CONST
#endif
#if VAX_FLOATING
# if (INDEX_BITS_NEEDED > F_EXP_POS)
# define CURRENT_EXP_POS (BITS_PER_WORD - (F_EXP_WIDTH + 1))
# define GET_HI_WORD(input, j) GET_EXP_WORD(input, j);j = PDP_SHUFFLE(j);
# define FINAL_SIGN_BIT_POSITION(j) j = PDP_SHUFFLE(F_SIGN_BIT_MASK)
# elif (SPECIAL_VAX && !FAST)
# define HI_HALF MAKE_MASK(16,0)
# define CURRENT_EXP_POS F_EXP_POS
# define GET_HI_WORD(input, j) GET_EXP_WORD(input, j); j &= HI_HALF;
# define FINAL_SIGN_BIT_POSITION(j) j = (F_SIGN_BIT_MASK)
# else
# define CURRENT_EXP_POS F_EXP_POS
# define GET_HI_WORD(input, j) GET_EXP_WORD(input, j);
# define FINAL_SIGN_BIT_POSITION(j) j = (F_SIGN_BIT_MASK)
# endif
#elif IEEE_FLOATING
# define CURRENT_EXP_POS F_EXP_POS
# define GET_HI_WORD(input, j) GET_EXP_WORD(input, j)
#else
# error Unsupported floating point format
#endif
/*
* Loads the constant 1.0 as early as possible, but after starting to fetch
* the hi word of x (except in log1p, which loads 1.0 even earlier).
* In fast log, also encourage the early loading of other constants.
*/
#if DO_LOG1P
# define PRE_LOAD_ONE(z)
/*
clear = CLEAR_MASK; \
index_mask = JMASK; \
rounding_bit = ROUND_BIT;
*/
#else
# if FAST
# define PRE_LOAD_ONE(z) z = ONE;\
clear = CLEAR_MASK; \
index_mask = JMASK; \
rounding_bit = ROUND_BIT;
# else
# define PRE_LOAD_ONE(z) z = ONE;
# endif
#endif
/*
* Computes the index into the F_table, using the hi LOG_K fraction bits
* of x (really, from the integer hi word of x, PDP shuffled for double
* precision VAX). The original hi part of x has been manipulated so that
* at least INDEX_BITS_NEEDED bits lie to the right of the exponent field,
* down to (and including) the lsb of the integer. The index is these leading
* INDEX_BITS_NEEDED fraction bits, multiplied by 2^4 or 2^5 for single or
* double precision respectively (each row of the table has 4 floating point
* numbers, for a total of 16 or 32 bytes).
*
* Indexing into the table is slightly different for ONE_PATH or two-path
* algorithm. In the ONE_PATH case, F(j) = 1 + j/2^LOG_K. So to find
* the nearest F(j) to a given scaled x, we need to round the fraction to
* to LOG_K bits, by clearing out the exponent field, adding 1 in the LOG_K +1
* position, and examining the first LOG_K fraction bits and the low order
* exponent bit. In the two-path case, F(j) = 1 + j/2^LOG_K + 1/2^(LOG_K+1).
* So to find the nearest F(j), we need only look at the first LOG_K bits.
*/
#define LOG2_ITEMS_PER_TABLE_ENTRY 2
#if (BYTES_PER_B_TYPE <= 4) && !PRECISION_BACKUP_AVAILABLE
#define SHIFT_THE_INDEX (2 + LOG2_ITEMS_PER_TABLE_ENTRY)
#elif (BYTES_PER_B_TYPE <= 8)
#define SHIFT_THE_INDEX (3 + LOG2_ITEMS_PER_TABLE_ENTRY)
#elif (BYTES_PER_B_TYPE <= 16)
#define SHIFT_THE_INDEX (4 + LOG2_ITEMS_PER_TABLE_ENTRY)
#else
#error Unknown floating point type
#endif
#define EXP_SIGN_MASK MAKE_MASK((F_EXP_WIDTH + 1), 0)
#define SIGN_ONLY_MASK MAKE_MASK(1, (F_EXP_WIDTH))
#if SPECIAL_VAX
# define JMASK MAKE_MASK(INDEX_BITS_NEEDED,(CURRENT_EXP_POS - TABLE_CONST))
# define CLEAR_MASK EXP_SIGN_MASK
# define SHIFT_AMOUNT (SHIFT_THE_INDEX - (CURRENT_EXP_POS - TABLE_CONST))
# define ROUND_BIT ((U_WORD) 1 << (31))
# if FAST
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
rounding_bit &= hi_x; \
j = (hi_x & index_mask); \
j <<= SHIFT_AMOUNT; \
other = ((rounding_bit) ? ((U_WORD)1 << SHIFT_AMOUNT) : 0); \
j += other; \
m = (hi_x >> CURRENT_EXP_POS); \
m &= clear;
# else
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
index_mask = JMASK; \
j = (hi_x & index_mask); \
j <<= SHIFT_AMOUNT; \
m = (hi_x >> CURRENT_EXP_POS);
# endif
#elif LONG_INDEX
# if (CURRENT_EXP_POS - (TABLE_CONST + SHIFT_THE_INDEX) >= 0)
# define JMASK MAKE_MASK(INDEX_BITS_NEEDED, SHIFT_THE_INDEX)
# define CLEAR_MASK MAKE_MASK(INDEX_BITS_NEEDED, SHIFT_THE_INDEX - 1)
# define ROUND_BIT ((U_WORD)1 << (SHIFT_THE_INDEX - 1))
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
clear = CLEAR_MASK; \
index_mask = JMASK; \
rounding_bit = ROUND_BIT; \
j = (hi_x >> (CURRENT_EXP_POS - (TABLE_CONST + SHIFT_THE_INDEX))); \
m = hi_x >> CURRENT_EXP_POS; \
j &= clear; \
j += rounding_bit; \
j &= index_mask;
# else
# define JMASK MAKE_MASK(INDEX_BITS_NEEDED,(CURRENT_EXP_POS - TABLE_CONST))
# define CLEAR_MASK \
MAKE_MASK(INDEX_BITS_NEEDED,(CURRENT_EXP_POS - INDEX_BITS_NEEDED))
# define ROUND_BIT ((U_WORD) 1 << (CURRENT_EXP_POS - INDEX_BITS_NEEDED))
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
clear = CLEAR_MASK; \
index_mask = JMASK; \
rounding_bit = ROUND_BIT; \
j = (hi_x & clear); \
j += rounding_bit; \
j &= index_mask; \
j <<= (SHIFT_THE_INDEX - (CURRENT_EXP_POS - TABLE_CONST)); \
m = (hi_x >> CURRENT_EXP_POS); \
m &= EXP_SIGN_MASK;
# endif
#else
# if (CURRENT_EXP_POS - (TABLE_CONST + SHIFT_THE_INDEX) >= 0)
# define JMASK MAKE_MASK(INDEX_BITS_NEEDED, SHIFT_THE_INDEX)
# define CLEAR_MASK 0
# define ROUND_BIT 0
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
index_mask = JMASK; \
j = (hi_x >> (CURRENT_EXP_POS - (TABLE_CONST + SHIFT_THE_INDEX))); \
j = (j & index_mask); \
m = hi_x >> CURRENT_EXP_POS;
# else
# define JMASK MAKE_MASK(INDEX_BITS_NEEDED,(CURRENT_EXP_POS - TABLE_CONST))
# define CLEAR_MASK EXP_SIGN_MASK
# define ROUND_BIT 0
# define SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j) \
clear = CLEAR_MASK; \
index_mask = JMASK; \
j = (hi_x & index_mask); \
j <<= (SHIFT_THE_INDEX - (CURRENT_EXP_POS - TABLE_CONST)); \
m = (hi_x >> CURRENT_EXP_POS); \
m &= clear;
# endif
#endif
/*
* Screen out bad x.
*
* In principle, for VAX format, we could screen with
* "if (x <= 0) goto label"
* but in practice, integer compares are faster and have less impact on
* code scheduling than floating compares.
*
* Bad x: m = sign/exponent has been shifted to the right.
* For VAX double precision, if m = 0, x was zero; if m < 0, x was negative.
* For VAX single precision, need to make sure any fraction bits to the
* left of the exponent were zeroed out.
* For IEEE, screen out negatives and MAX_EXPONENT as well.
*/
#if VAX_FLOATING
# if SPECIAL_VAX && !FAST
# define SCREEN_OUT_BAD_X(hi_x, label) \
if ((U_WORD)((hi_x >> CURRENT_EXP_POS) - 1) >= \
F_MAX_BIASED_EXP) goto label
# elif (INDEX_BITS_NEEDED <= F_EXP_POS)
# define SCREEN_OUT_BAD_X(hi_x, label) \
if ((U_WORD)(((hi_x >> CURRENT_EXP_POS) & EXP_SIGN_MASK) - 1) >= \
F_MAX_BIASED_EXP) goto label
# else
# define SCREEN_OUT_BAD_X(hi_x, label) if ((WORD) hi_x <= 0) goto label
# endif
#elif IEEE_FLOATING
# define SCREEN_OUT_BAD_X(hi_x, label) \
if ((U_WORD)((hi_x >> CURRENT_EXP_POS) - 1) >= F_MAX_BIASED_EXP)\
goto label
#else
# error Unsupported floating point format
#endif
/*
* Compute the unbiased, IEEE-style exponent, in integer position.
*/
#define FINAL_VERSION_OF_EXPONENT(m) m -= (F_EXP_BIAS - F_NORM)
/*
* "Shortens" a variable to half precision or so, so that products of
* "short" variables will be exact. There are two flavors of "shorten":
* add and subtract a BIG number to clear out a specific number
* number of bits. the original number must be known to be in
* a given range.
* cast to smaller precision, so that the "shortened" number has lots
* of trailing zeros, or is flushed to zero.
* When backup precision is available (we assume this is the case for
* single precision), no need to shorten variables.
*/
#define BIG F_POW_2(F_PRECISION - (F_PRECISION - TABLE_CONST)/2 - 1)
#if F_COPY_SIGN_IS_FAST
# define SHORTEN(z, w) { \
F_TYPE tmp = BIG; \
w = z; \
F_COPY_SIGN(tmp, z, tmp); \
ADD_SUB_BIG(w, tmp); \
}
#else
# define SHORTEN(z, w) { \
F_TYPE tmp = BIG; \
w = z; \
tmp = ( (z > 0) ? tmp : -(tmp) ); \
ADD_SUB_BIG(w, tmp); \
}
#endif
#define SHORTEN2(z, w) SHORTEN_VIA_CASTS(z, w)
/*
* There are two approximation polynomials for log:
* using reciprocal: f/F(j) - 1 = (f - F(j))*(1/F(j))
* and
* using quotient: 2*(f - F(j))/(f + F(j))
* where f is the scaled-down x.
*
* The macro PREPARE_VARIABLE_FOR_POLY chooses one approach or the other,
* depending on the current value of USE_RECIP.
* In the first case,
* variable = (f - Fj) * (1/Fj) where the reciprocal is fetched from
* the table.
* In the second case, compute
* variable = 2*(f - Fj)/(f + Fj).
*
*
* In the ONE_PATH case, we do a variant of the USE_RECIP which computes
* the variable in hi and lo parts. First, the reciprocal is stored in
* full precision and as a lo part, in the table. A first approximation
* to y = (f - Fj)*(1/Fj) is computed, using the full reciprocal (1/F)_full.
* y is good enough to use in the polynomial, but we need something better
* for the linear term. Compute 1/F_hi = 1/F_full - 1/F_lo.
*
* Shorten t = (f - Fj) into a hi part, and subtract to get a lo part:
* t_hi = shorten(t); t_lo = t - t_hi;
* Then a better approximation to the product (f - Fj)*(1/Fj) consists of
* the pieces
* (t_hi * 1/F_hi) and ( t_lo * 1/F_hi + (t_hi + t_lo)*1/F_lo )
*
*
* The variable for the approximation for log1p is also more complicated
* than the standard variable for log, because f = the scaled (1 + x)
* minus F(j) must be computed accurately.
*
* m is the IEEE-style exponent for 1 + x, and f = 2^(-m) * (1 + x).
* Fj is the nearest division point to f, as above. Then t is f - F(j),
* computed as:
*
* m = -2, -3, ...: t = f - Fj
* m = -1 : t = (2 - Fj) + 2x
* m = 0 : t = (1 - Fj) + x
* m = 1, 2, ... PREC-1: t ((2^-m) - Fj) + (2^-m)*x
* m = PREC, PREC+1: t = ((2^-m)*x - Fj) + (2^-m)
* m = PREC+2, PREC+3, ... t = f - Fj.
*
* Once t has been computed, the approximation variable is either
* t*(1/Fj) or 2t/(t + 2Fj).
*/
#define SCALE_DOWN(j, tmp) \
B_MAKE_FLOAT(((U_WORD)(B_EXP_BIAS - F_NORM - j) << B_EXP_POS), tmp)
#if DO_LOG1P
# define SET_UP_FOR_LOGP_POLY(j, Fj, tmp, one, x, m, tmpx, y) \
if (m == -1) \
y = ((B_TYPE) 2.0 - Fj) + (B_TYPE) (x + x); \
else if (m == 0)\
y = ((B_TYPE) one - Fj) + (B_TYPE) x; \
else if ((m > 0) && (m < F_PRECISION)) \
{ SCALE_DOWN(m, tmp); \
y = (tmp - Fj) + tmp * (B_TYPE) x; } \
else if ((m == F_PRECISION) || (m == (F_PRECISION + 1))) \
{ SCALE_DOWN(m, tmp); \
y = (tmp * (B_TYPE) x - Fj) + tmp; } \
else y = (B_TYPE) tmpx - Fj;
# if USE_RECIP
# define PREPARE_VARIABLE_FOR_FAR_POLY(j, Fj, tmp, one, x, m, tmpx, z, y) \
SET_UP_FOR_LOGP_POLY(j, Fj, tmp, one, x, m, tmpx, y); \
tmp = RECIP_F(j); y = y * tmp;
# else
# define PREPARE_VARIABLE_FOR_FAR_POLY(j, Fj, tmp, one, x, m, tmpx, z, y) \
SET_UP_FOR_LOGP_POLY(j, Fj, tmp, one, x, m, tmpx, y); \
tmp = (y + Fj) + Fj; y = y/tmp ; y += y;
# endif
#else
/* not LOG1P */
# if DO_ONE_PATH && !PRECISION_BACKUP_AVAILABLE
# define PREPARE_VARIABLE_FOR_FAR_POLY(j, Fj, tmp, one, x, m, tmpx, z, y) \
tmp = (B_TYPE) RECIP_F_FULL(j); tmpx -= Fj;\
x = RECIP_F_LO(j); y = tmpx * tmp ; tmp -= x; \
SHORTEN2(tmpx, z); x *= tmpx; tmpx -= z; \
z *= tmp; x += tmp * tmpx;
# else
# if USE_RECIP
# define PREPARE_VARIABLE_FOR_FAR_POLY(j, Fj, tmp, one, x, m, tmpx, z, y)\
tmp = RECIP_F(j); y = (B_TYPE) tmpx - Fj; y *= tmp;
# else
# define PREPARE_VARIABLE_FOR_FAR_POLY(j, Fj, tmp, one, x, m, tmpx, z, y)\
tmp = (B_TYPE) tmpx - Fj; \
y = (B_TYPE) tmpx + Fj; y = tmp/y; y += y;
# endif
# endif
#endif
/*
* Add the linear term or a reasonable facsimile to m*log2 + logF, to
* the hi or lo parts as appropriate.
*
* In the ONE_PATH algorithm, the linear term is split into a "hi" part
* that is added to (m*log2 + log(F))_hi - which can range in size from
* just a little larger than the linear term, to very large - and a "lo"
* part which is added to (m*log2 + log(F))_lo + poly.
*/
#if NATURAL
# if DO_ONE_PATH && !PRECISION_BACKUP_AVAILABLE
# define ADD_LINEAR_TERM_TO_LOG_F(w, v, x, t, tmpx, y, z) \
w += x; \
t = v + z; tmpx = t - v; v = t; w += z - tmpx;
# else
# define ADD_LINEAR_TERM_TO_LOG_F(w, v, x, t, tmpx, y, z) w += y;
# endif
#else
# if DO_ONE_PATH && !PRECISION_BACKUP_AVAILABLE
# define ADD_LINEAR_TERM_TO_LOG_F(w, v, x, t, tmpx, y, z) \
SHORTEN2(z, tmpx); z -= tmpx; \
w += z * LOGE_HI; w += tmpx * LOGE_LO2; w += x * LOGE_HI; \
z = tmpx * LOGE_HI2; t = v + z; z -= t - v; v = t; w += z;
# else
# define ADD_LINEAR_TERM_TO_LOG_F(w, v, x, t, tmpx, y, z) \
w += y * LOGE_HI;
# endif
#endif
/*
* If x was close to 1, the polynomial approximation uses either x - 1
* (for LOG1P, x itself), or the quotient z = 2*(x - 1)/(x + 1)
* (for LOG1P, x/2 + x) as the variable for the polynomial approximation.
*
* Since the quotient will not be exact, in general, the macro
* computes a rough estimate of z. The error in this computed z is
* calculated in another macro GET_ACCURATE_LO_PART_OF_QUOTIENT.
* The current macro computes the quotient z, and also f = x - 1, and
* x = 1/(x + 1). Both f and x are used later in GET_ACCURATE_LO_PART macro.
*/
#if DO_LOG1P
# define TWO (B_TYPE) 2.0
# if NO_DIVISIONS
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp) w = (B_TYPE) x
# else
# if PRECISION_BACKUP_AVAILABLE
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp)\
tmp = (B_TYPE) x; w = tmp + TWO; w = tmp/w; w += w;
# else
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp)\
tmp = x; x = x + TWO; x = one/x; w = tmp * x; w += w;
# endif
# endif
#else
# if NO_DIVISIONS
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp) w = (B_TYPE)(x - one)
# else
# if PRECISION_BACKUP_AVAILABLE
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp) \
tmp = (B_TYPE)(x - one); w = (B_TYPE)(x + one); w = tmp/w; w += w;
# else
# define PREPARE_VARIABLE_NEAR_1(x, one, w, tmp) \
tmp = x - one; x = x + one; x = one/x; w = tmp * x; w += w;
# endif
# endif
#endif
/*
* Shortens the "near 1" variable, in preparation for adding the linear term
* carefully to the rest of the polynomial.
*
* When backup precision is available, no need to shorten. When using the
* quotient approximation near 1, the lo part is computed much more carefully,
* in the macro GET_ACCURATE_LO_PART.
*/
#if PRECISION_BACKUP_AVAILABLE
# define SHORTEN_IF_NECESSARY(z, hi, lo)
#else
# if NO_DIVISIONS
# define SHORTEN_IF_NECESSARY(z, hi, lo) SHORTEN(z, hi); lo = z - hi;
# else
# define SHORTEN_IF_NECESSARY(z, hi, lo) SHORTEN2(z, hi);
# endif
#endif
/*
* This macro extends the precision of the quotient u = 2(x - 1)/(x + 1),
* where x is the original input to the log function and x is near 1.
*
* x lies in a small interval around 1, [T1, T2] .
* f was already computed as f = x - 1. Because x was near 1, f is exact.
* u_hi was already computed by taking a preliminary approximation to
* the quotient u, and then "shortening" u to roughly half precision.
* g was already computed as g = 1/(x + 1). Clearly g is not exact.
*
* We need to find u_lo so that u = u_hi + u_lo.
* Compute f_hi = "shortened" f, and f_lo = f - f_hi.
* Both f_hi and f_lo are exact, and u_hi is exact (at least
* what there is of it).
*
* Multiply the basic equation u = 2(x - 1)/(x + 1) through by (x + 1):
*
* (x + 1) * u = (x + 1) * (u_hi + u_lo) = 2 * f .
*
* Replace f by (f_hi + f_lo), and solve for u_lo:
*
* (x + 1) * u_lo = 2 * f - (x + 1) * u_hi = 2 * f - (f + 2) * u_hi =
* 2 * f - ( f_hi * u_hi - f_lo * u_hi - 2 * u_hi).
*
* All the terms on the right hand side are either exact or are products
* of two "short" quantities (and are therefore exact). Grouping the
* terms in order of size, and dividing through by (x + 1):
*
* u_lo = [ ( 2 * (f - u_hi) - f_hi * u_hi) - f_lo * u_hi ] * g.
*
* This macro returns u_lo in the parameter f.
*/
#if (NO_DIVISIONS || PRECISION_BACKUP_AVAILABLE)
# define GET_ACCURATE_LO_PART_OF_QUOTIENT(f, u_hi, g)
#else
# define GET_ACCURATE_LO_PART_OF_QUOTIENT(f, u_hi, g) \
{ B_TYPE f_hi, f_lo; \
SHORTEN2(f, f_hi); \
f_lo = f - f_hi; \
f -= u_hi; \
f += f; \
f_hi *= u_hi; \
f -= f_hi; \
f_lo *= u_hi; \
f -= f_lo; \
f *= g; }
#endif
/*
* Combine poly term, linear term, and the second order term (if NO_DIVISIONS)
* and compute the final result.
*
* When backup precision is available, a direct sum of the terms
* z - z*z/2 + poly is sufficient.
*
* When using the variable z = x - 1 (or x, in LOG1P), we have shortened
* z into z_hi + z_lo. The sum of the first two terms is computed as
* [ z - (z_hi * z_hi)/2 ] + poly - ( (z + z_lo)*z_lo)/2
* where the first term is exact (note that z has (LOG_K + 1) trailing zeros).
*
* When using the quotient variable, we add in the "fixed up" part of the
* linear term in the variable "extra". There is no square term in the
* quotient variable approximation.
*
* In base 10 and base 2, the linear term must be multiplied carefully by
* log(e) and the result added carefully to the polynomial.
*/
#define HALF ( (F_TYPE) 0.5)
#if PRECISION_BACKUP_AVAILABLE
# if NATURAL
# if NO_DIVISIONS
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
lin -= (lin * lin)*HALF; \
t += lin;
# else
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t += lin;
# endif
# else
# if NO_DIVISIONS
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
lin -= (lin * lin)*HALF; \
t += (lin * LOGE_HI);
# else
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t += (lin * LOGE_LO); \
t += (lin * LOGE_HI);
# endif
# endif
#else
# if NATURAL
# if !NO_DIVISIONS
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t += extra; \
t += short;
# else
# if DO_LOG1P
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t -= ((lin + short)*lo)*HALF; \
short -= (short * short)*HALF; \
t += lo; \
t += short;
# else
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t -= ((lin + short)*lo)*HALF; \
lin -= (short * short)*HALF; \
t += lin;
# endif
# endif
# else
# if NO_DIVISIONS
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
tmp = ((lin + short)*lo)*HALF; \
t -= tmp * LOGE_HI;\
lin -= (short * short)*HALF; \
SHORTEN2(lin, short); \
lo = lin - short; \
t += lo * LOGE_HI; \
t += short * LOGE_LO2; \
t += short * LOGE_HI2;
# else
# define SUBTRACT_SQ_TERM_AND_COMBINE(t, lin, short, lo, tmp, extra) \
t += extra * LOGE_LO2; \
tmp = short * LOGE_LO2; \
tmp += extra * LOGE_HI2; \
t += tmp; \
t += short * LOGE_HI2;
# endif
# endif
#endif
/*
* Looks at the exponent field of x in hi_x, to see if it is zero.
*/
#define WHERE_IS_EXPON_NOW (CURRENT_EXP_POS - F_EXP_POS)
#define ZERO_EXPON(j) ( !( (j) & (F_EXP_MASK << WHERE_IS_EXPON_NOW)) )
#if !defined F_ENTRY_NAME
# define F_ENTRY_NAME _F_ENTRY_NAME
#endif
#if !FAST
/*
* The code for logarithm.
*/
F_TYPE
F_ENTRY_NAME(F_TYPE x)
{
EXCEPTION_RECORD_DECLARATION
WORD m, hi_x, j;
WORD clear, index_mask, rounding_bit;
U_WORD screen;
F_TYPE float_one, temp_x;
/*
* In log1p, start by getting the constant 1.0. Check if x is really tiny,
* e.g. denorm, by looking at the exponent - if so, return x. Then, add 1
* to x, provided that this won't overflow (only a danger if the rounding mode
* is "round to pos infinity" and x was FMAX).
*/
LOGP_CHANGE_VAR_AND_GET_ONE(x, temp_x, float_one, hi_x, m, bad_x);
/*
* Fetch the sign, exponent, and highest fraction bits as an integer
* in hi_x. VAX format numbers are massaged so that the fraction bits that
* were not adjacent to the sign and exponent are either ignored (single
* precision) or are swapped into the "lower" half of the integer word
* (double precision).
*/
#if LOG1P
GET_HI_WORD(temp_x, hi_x) ;
#else
GET_HI_WORD(x, hi_x) ;
#endif
/*
* Load the constant 1.0 as early as possible, forcing early computation
* of the address of the array (in log1p, done already).
*/
PRE_LOAD_ONE(float_one);
/*
* Now, screen x to see if it is in the interval [T1, T2], "near 1".
* For efficiency, the comparison is done with an unsigned integer compare:
* (x - T1) < (T2 - T1).
*/
#if !DO_ONE_PATH
screen = (U_WORD) (hi_x - T1);
if ( screen < T2_MINUS_T1 ) goto near_1;
#endif
{
B_TYPE w, t, y, v, z;
/*
* Normalize (a copy of) the fraction field of x to have a value between
* 1 and 2, by putting the exponent of 1.0 into the exponent field,
* either with COPY_SIGN_EXP or directly.
*/
#if DO_LOG1P
F_COPY_SIGN_AND_EXP(temp_x, float_one, temp_x);
#else
F_COPY_SIGN_AND_EXP(x, float_one, temp_x);
#endif
/*
* x is not in the interval [T1, T2], but it still might be negative,
* zero, infinity or NaN. On the way to screening these out, shift the
* exponent and fraction field to the right to isolate the leading
* INDEX_BITS_NEEDED fraction bits, in order to get the index of the jth row.
*/
SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j);
/*
* Continue shifting down, to isolate the exponent. Screen out the special
* cases with another unsigned integer compare, to see if sign = 1, or
* exponent = 0, or exponent = MAX (IEEE only). Note that if x's exponent
* was zero, subtracting 1 makes it look like a large (unsigned) integer.
*/
SCREEN_OUT_BAD_X(hi_x, bad_x);
/*
* So x is OK. Get the unbiased, IEEE-style exponent m.
*/
FINAL_VERSION_OF_EXPONENT(m);
#if !defined( LOG1p )
denorms_rejoin:
#endif
/*
* Fetch the division point F(j), which is the closest table entry to x.
* abs(x - F(j) ) < 1/ 2^(LOG_K+1).
*/
t = (B_TYPE) GET_F(j);
/*
* The variable for the approximation polynomial is either
* (scaled_x - F(j)) * 1/F(j) where F(j) is fetched from the table
* (when USE_RECIP is true)
* or
* 2(x - F(j))/(x + F(j)).
* Use the latter only when divides are relatively fast.
*/
PREPARE_VARIABLE_FOR_FAR_POLY(j, t, w, float_one, x, m, temp_x, z, y);
/*
* Compute m*log(2) + log(F), in hi and lo parts:
* m*log(2)_lo + log(F)_lo
* m*log(2)_hi + log(F)_hi
*
* The log(F) is fetched from the F_table. The base of these logs is
* 2, 10 or e, as appropriate.
*/
v = (B_TYPE) m;
#if !PRECISION_BACKUP_AVAILABLE
w = LOG2_LO;
w *= v;
w += LOGF_LO(j); /* m*log(2)_lo + log(F)_lo */
v *= LOG2_HI;
v += (B_TYPE) LOGF_HI(j); /* m*log(2)_hi + log(F)_hi */
#else
w = LOG2_HI;
w *= v;
w += LOGF_HI(j);
#endif
/*
* The first order term of the polynomial, (loge)*y, is added to log(F).
* In the ONE_PATH algorithm, this must be done very carefully, in order
* not to lose accuracy.
*/
ADD_LINEAR_TERM_TO_LOG_F(w, v, x, t, temp_x, y, z);
/*
* t = poly(y)
*/
EVAL_FAR_POLY(y, t);
/*
* Combine the poly with log(F).
*/
w += t;
#if !PRECISION_BACKUP_AVAILABLE
w += v;
#endif
/*
* So, if x = 2^m * fraction = 2^m * (F + rest) = 2^m * F * f,
*
* log(x) = m * log(2) + log(F) + log(f) =
*
* (m * log(2)_hi + log(F)_hi) +
* (f * log(e)_hi + ( f*log(e)_lo + poly(f) +
* ( m * log(2)_lo + log(F)_lo ) ) )
*
* where these terms are given in descending size.
*/
return ((F_TYPE) w );
} /* end x not in interval */
/*
* The approximation for x near 1, in the interval (T1, T2), involves
* computing the variable for approximation with PREPARE_VARIABLE_NEAR_1,
* to get either z = x - 1 or z = 2(x-1)/(x+1), and then splitting z
* into hi and lo parts z_hi and z_lo using the SHORTEN macro.
*
* In the first, NO_DIVISIONS approach, it's important to split z carefully
* so that z_hi has N bits. Because x was near 1, x - 1 is no smaller
* than 2^(-F_PRECISION + 1) (except in log1p).
* x - 1 has some trailing zeros; in fact, the smaller z is, the more
* trailing zeros.
*
* We can perserve accuracy in the approximation
* ln(1 + z) = z - z^2/2 + z^3/3 - ....
* by splitting the second term z^2/2 into
* z_hi^2/2 (exact) + z_lo*(z + z_hi)/2.
*
* The smaller z is, the greater the alignment shift between z_hi^2/2 and z,
* but then the more trailing zeros z has. So z - z_hi^2/2 is exact,
* provided that the number of bits is small enough ( < F_PRECISION/2 - 1).
*
* In log1p, we use z_hi - (z_hi^2/2). We also know that z is no smaller
* than 2^(-F_PRECISION + 1).
*
*
* When NO_DIVISIONS is FALSE, the quotient 2*(x-1)/(x+1) is computed
* a second time, using "shortened" variables to preserve accuracy by
* guaranteeing that products are exact.
*
* Let f = x - 1 and f1 = shortened f. f2 = f - f1 is exact.
* u = 2(x-1)/(x + 1) is not exact.
* Let u1 = shortened u.
* The macro GET_ACCURATE_LO_PART_OF_QUOTIENT computes u2 = u - u1,
* by reconstructing u itself in extra accuracy.
*
*
* In both approaches, single precision uses double precision as a backup type
* in critical steps.
*/
#if !DO_ONE_PATH
near_1:
#if DO_LOG1P
if (temp_x == float_one) return (x);
#endif
{
B_TYPE t, z, w, v, y;
/*
* z = x - 1 or 2(x-1)/(x+1). In the second case, x is returned
* as x/(1+x), and y is returned as x - 1. These variables will be
* used later in getting a more accurate version of the quotient.
*/
PREPARE_VARIABLE_NEAR_1(x, float_one, z, y);
/*
* If no backup precision is available, split z into hi and lo parts.
*/
SHORTEN_IF_NECESSARY(z, w, v);
/*
* t = poly(z). Does not include linear or square terms.
*/
EVAL_NEAR_POLY(z, t);
/*
* An accurate estimate of the lo part of the approximation variable is
* returned in y (NO_DIVISIONS = FALSE case).
*/
GET_ACCURATE_LO_PART_OF_QUOTIENT(y, w, x);
/*
* Combine the first and second order terms (using hi and lo if needed)
* with the remainder of the poly.
*/
SUBTRACT_SQ_TERM_AND_COMBINE(t, z, w, v, temp_x, y);
return ( (F_TYPE) t);
} /* end of x in (T1, T2) */
#endif
/* end "good" points */
/* "Bad" points:
* 1. if sign is negative but exponent is zero
* could be -0 (IEEE) : return NaN, via exception dispatcher
* or -denorm (IEEE) : return NaN and raise error
* or reserved operand (VAX) : return x and raise error
* 2. if sign is negative and exponent is not zero
* could be NaN (IEEE) : return NaN itself
* or -infinity (IEEE) : return NaN and raise error
* or negative number : return NaN (IEEE) and raise error
* 3. if sign is positive but exponent is zero
* could be true zero : return -infinity (VAX, raise error)
* or pos denorm (IEEE) : a valid case! scale x by 2^PRECISION
* and subtract PRECISION from the exponent.
* 4. if sign is positive and exponent = EMAX (for IEEE only)
* could be NaN : return NaN
* or pos infinity : return x
*/
bad_x:
#if IEEE_FLOATING
/* Check for negative arguement */
if ((hi_x & F_SIGN_BIT_MASK) != 0) {
# if COMPATIBILITY_MODE
if ZERO_EXPON(hi_x) { /* sign = negative, exponent is zero
could be -0 or -denorm */
GET_EXCEPTION_RESULT_1(LOG_NEGATIVE, x, x);
return x;
}
else { /* sign = negative, exponent not zero */
F_SET_FLAG_IF_NAN(x, m);
if (!m) /* -inf or negative: return NaN, error */
GET_EXCEPTION_RESULT_1(LOG_NEGATIVE, x, x)
return x;
}
# else
F_SET_FLAG_IF_NAN( x, m ) ;
if ( m )
return x ;
else {
WORD func_error_word ;
func_error_word = ERROR_WORD( STATUS_INVALID,
NEG_HUGE_INDEX,
NAN_INDEX,
F_TYPE_ENUM,
DPML_EDOM,
SIGNAL_LOGZERNEG ) ;
RETURN_EXCEPTION_RESULT_1( func_error_word, x, F_F, _FpCodeLog ) ;
}
# endif
}
else if ZERO_EXPON(hi_x) { /* sign = positive, expon = zero */
#if DO_LOG1P
# if COMPATIBILITY_MODE
/* +0: return -inf, via RAISE */
GET_EXCEPTION_RESULT_1(LOG_ZERO, x, x);
return x;
# else
{ WORD func_error_word ;
func_error_word = ERROR_WORD( STATUS_OVERFLOW,
NEG_HUGE_INDEX,
NEG_INFINITY_INDEX,
F_TYPE_ENUM,
DPML_ERANGE,
SIGNAL_LOGZERNEG ) ;
RETURN_EXCEPTION_RESULT_1( func_error_word, x, F_F, _FpCodeLog ) ;
}
# endif
}
#else /* if DO_LOG1P */
/*
* Push in a known exponent to check if x was zero without doing a floating
* compare, and (if not) to scale x to be between 2.0 and 2^PRECISION.
*/
DENORM_TO_NORM(x, temp_x);
if (temp_x == 0.0) { /* +0: should return -inf, via RAISE */
# if COMPATIBILITY_MODE
GET_EXCEPTION_RESULT_1(LOG_ZERO, x, x);
return x;
# else
WORD func_error_word ;
func_error_word = ERROR_WORD( STATUS_OVERFLOW,
NEG_HUGE_INDEX,
NEG_INFINITY_INDEX,
F_TYPE_ENUM,
DPML_ERANGE,
SIGNAL_LOGZERNEG ) ;
RETURN_EXCEPTION_RESULT_1( func_error_word, x, F_F, _FpCodeLog ) ;
# endif
} else { /* x is positive denorm - scale and compute log */
GET_HI_WORD(temp_x, hi_x) ;
/* compute the index again */
F_COPY_SIGN_AND_EXP(temp_x, float_one, temp_x);
SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j);
FINAL_VERSION_OF_EXPONENT(m);
m -= __LOG2_DENORM_SCALE;
goto denorms_rejoin;
}
}
#endif
else { /* sign = positive, expon = MAX */
/* pos inf or NaN: just return x, no err */
return (x);
}
#elif VAX_FLOATING
FINAL_SIGN_BIT_POSITION(m);
if ((hi_x & m) != 0) { /* sign = negative. */
/* if exponent is zero, reserved operand */
/* if exponent not zero, negative number */
GET_EXCEPTION_RESULT_1(LOG_NEGATIVE, x, x)
return x;
}
else if ZERO_EXPON(hi_x) { /* sign = positive, expon = zero */
/* zero: raise error */
GET_EXCEPTION_RESULT_1(LOG_ZERO, x, x);
return x;
}
#else
#error Unsupported floating point format
#endif
} /* end of logarithm */
#else
/*
* Fast logarithm. The algorithmic steps are the same as in accurate log,
* but in slightly different order.
*/
F_TYPE
F_ENTRY_NAME(F_TYPE x)
{ WORD m, hi_x, j;
WORD clear, index_mask, rounding_bit, other;
U_WORD screen;
F_TYPE float_one, tx;
B_TYPE w, w1, w4, t, y, v, z;
printf("x = %8.8x\n", (int *) &x);
GET_HI_WORD(x, hi_x);
PRE_LOAD_ONE(float_one);
F_COPY_SIGN_AND_EXP(x, float_one, tx);
w1 = LOG2_HI;
#if DOUBLE_PRECISION
w = LOG2_LO;
#endif
SHIFT_GET_INDEX_AND_EXPONENT(hi_x, m, j);
FINAL_VERSION_OF_EXPONENT(m);
v = (B_TYPE) m;
#if DOUBLE_PRECISION
screen = (U_WORD) (hi_x - T1);
#endif
t = (B_TYPE) GET_F(j);
z = RECIP_F(j);
y = (B_TYPE) tx - t; /* f - Fj */
#if DOUBLE_PRECISION
if ( screen < T2_MINUS_T1 ) goto near_1;
#endif
y *= z; /* y= (f - Fj)*recip */
SCREEN_OUT_BAD_X(hi_x, bad_x);
w1 *= v; /* m*log2_hi */
#if DOUBLE_PRECISION
w *= v; /* m*log2_lo */
#endif
EVAL_LO_FAR_POLY(y, z);
#if DOUBLE_PRECISION
EVAL_HI_FAR_POLY(y, w4);
#endif
#if SINGLE_PRECISION
# if NATURAL
w = y + (B_TYPE) LOGF_HI(j);
v = w + w1;
# else
w1 += (B_TYPE) LOGF_HI(j);
v = w1 + y*LOGE_HI;
# endif
#else
# if NATURAL
v = w1 + (B_TYPE) LOGF_HI(j); /* m*log(2)_hi + log(F)_hi */
w += LOGF_LO(j); /* m*log(2)_lo + log(F)_lo */
v += y; /* hi sum + y */
# else
v = w1 + (B_TYPE) LOGF_HI(j); /* m*log(2)_hi + log(F)_hi */
w += LOGF_LO(j); /* m*log(2)_lo + log(F)_lo */
v += y * LOGE_HI; /* hi sum + y */
# endif
#endif
v += z; /* hi sum + hi poly */
#if DOUBLE_PRECISION
w += w4; /* lo sum + lo poly */
v += w;
#endif
return( (F_TYPE) v);
/*
* In fast log, only double precision has the near 1 code path.
*/
#if DOUBLE_PRECISION
near_1:
z = (B_TYPE)(x - float_one);
EVAL_NEAR_POLY(z, v);
return(v);
#endif
/*
* Error cases:
* hi_x holds the sign, exponent, some fraction bits of x, and,
* if VAX format and PDP-shuffle was not performed, possibly some
* fraction bits to the left of the exponent.
*
* If +Inf or NaN return x. Otherwise signal error
*
* We raise an error by generating an overflow in floating multiply.
*/
bad_x:
# if IEEE_FLOATING
# define EXP_MASK MAKE_MASK(F_EXP_WIDTH, CURRENT_EXP_POS)
m = hi_x & EXP_MASK;
if (m == EXP_MASK)
{ /* x is NaN or Inf - Check sign for -Inf */
F_SET_FLAG_IF_NAN(x, j);
if (j || ((hi_x ^ m) == 0))
/* x was NaN or +Inf */
return x;
}
# endif
return ((F_TYPE) (OVF_LIM * OVF_LIM));
return (x);
}
#endif
|