1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806
|
// -*- mode: c++ -*-
/*
Copyright (c) 2010-2025, Intel Corporation
SPDX-License-Identifier: BSD-3-Clause
*/
/** @file stdlib.ispc
@brief Portion of the ispc standard library implementation that's in
ispc code
*/
#define ISPC_INTERNAL_STDLIB_COMPILATION
#include "core.isph"
#include "builtins.isph"
#include "stdlib.isph"
#include "svml.isph"
#include "target.isph"
#undef ISPC_INTERNAL_STDLIB_COMPILATION
///////////////////////////////////////////////////////////////////////////
// Low level primitives
__declspec(safe, cost0) static inline float16 float16bits(unsigned int16 a) { return __halfbits_varying_int16(a); }
__declspec(safe, cost0) static inline uniform float16 float16bits(uniform unsigned int16 a) {
return __halfbits_uniform_int16(a);
}
__declspec(safe, cost0) static inline float16 float16bits(int16 a) { return __halfbits_varying_int16(a); }
__declspec(safe, cost0) static inline uniform float16 float16bits(uniform int16 a) {
return __halfbits_uniform_int16(a);
}
__declspec(safe, cost0) static inline float floatbits(unsigned int a) { return __floatbits_varying_int32(a); }
__declspec(safe, cost0) static inline uniform float floatbits(uniform unsigned int a) {
return __floatbits_uniform_int32(a);
}
__declspec(safe, cost0) static inline float floatbits(int a) { return __floatbits_varying_int32(a); }
__declspec(safe, cost0) static inline uniform float floatbits(uniform int a) { return __floatbits_uniform_int32(a); }
__declspec(safe, cost0) static inline double doublebits(unsigned int64 a) { return __doublebits_varying_int64(a); }
__declspec(safe, cost0) static inline uniform double doublebits(uniform unsigned int64 a) {
return __doublebits_uniform_int64(a);
}
__declspec(safe, cost0) static inline unsigned int16 intbits(float16 a) { return __intbits_varying_half(a); }
__declspec(safe, cost0) static inline uniform unsigned int16 intbits(uniform float16 a) {
return __intbits_uniform_half(a);
}
__declspec(safe, cost0) static inline unsigned int intbits(float a) { return __intbits_varying_float(a); }
__declspec(safe, cost0) static inline uniform unsigned int intbits(uniform float a) {
return __intbits_uniform_float(a);
}
__declspec(safe, cost0) static inline unsigned int64 intbits(double d) { return __intbits_varying_double(d); }
__declspec(safe, cost0) static inline uniform unsigned int64 intbits(uniform double d) {
return __intbits_uniform_double(d);
}
__declspec(safe) static inline float broadcast(float v, uniform int i) { return __broadcast_float(v, i); }
__declspec(safe) static inline int8 broadcast(int8 v, uniform int i) { return __broadcast_i8(v, i); }
__declspec(safe) static inline unsigned int8 broadcast(unsigned int8 v, uniform int i) { return __broadcast_i8(v, i); }
__declspec(safe) static inline int16 broadcast(int16 v, uniform int i) { return __broadcast_i16(v, i); }
__declspec(safe) static inline unsigned int16 broadcast(unsigned int16 v, uniform int i) {
return __broadcast_i16(v, i);
}
__declspec(safe) static inline float16 broadcast(float16 v, uniform int i) { return __broadcast_half(v, i); }
__declspec(safe) static inline int32 broadcast(int32 v, uniform int i) { return __broadcast_i32(v, i); }
__declspec(safe) static inline unsigned int32 broadcast(unsigned int32 v, uniform int i) {
return __broadcast_i32(v, i);
}
__declspec(safe) static inline double broadcast(double v, uniform int i) { return __broadcast_double(v, i); }
__declspec(safe) static inline int64 broadcast(int64 v, uniform int i) { return __broadcast_i64(v, i); }
__declspec(safe) static inline unsigned int64 broadcast(unsigned int64 v, uniform int i) {
return __broadcast_i64(v, i);
}
template <typename T> unmasked varying T __rotate_via_shuffle(varying T v, uniform int32 i) {
// Pre-computed shuffle masks for each rotation
#if TARGET_WIDTH == 4
static const int masks[4] = {
{0, 1, 2, 3}, // rotate by 0
{1, 2, 3, 0}, // rotate by 1
{2, 3, 0, 1}, // rotate by 2
{3, 0, 1, 2} // rotate by 3
};
#elif TARGET_WIDTH == 8
static const int masks[8] = {
{0, 1, 2, 3, 4, 5, 6, 7}, // rotate by 0
{1, 2, 3, 4, 5, 6, 7, 0}, // rotate by 1
{2, 3, 4, 5, 6, 7, 0, 1}, // rotate by 2
{3, 4, 5, 6, 7, 0, 1, 2}, // rotate by 3
{4, 5, 6, 7, 0, 1, 2, 3}, // rotate by 4
{5, 6, 7, 0, 1, 2, 3, 4}, // rotate by 5
{6, 7, 0, 1, 2, 3, 4, 5}, // rotate by 6
{7, 0, 1, 2, 3, 4, 5, 6} // rotate by 7
};
#elif TARGET_WIDTH == 16
static const int masks[16] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, // rotate by 0
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0}, // rotate by 1
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1}, // rotate by 2
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2}, // rotate by 3
{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3}, // rotate by 4
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4}, // rotate by 5
{6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5}, // rotate by 6
{7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6}, // rotate by 7
{8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}, // rotate by 8
{9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8}, // rotate by 9
{10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // rotate by 10
{11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, // rotate by 11
{12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // rotate by 12
{13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, // rotate by 13
{14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, // rotate by 14
{15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} // rotate by 15
};
#elif TARGET_WIDTH == 32
static const int masks[32] = {
{0, 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}, // rotate by 0
{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, 0}, // rotate by 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, 0, 1}, // rotate by 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, 0, 1, 2}, // rotate by 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, 0, 1, 2, 3}, // rotate by 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, 0, 1, 2, 3, 4}, // rotate by 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, 0, 1, 2, 3, 4, 5}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7}, // rotate by 8
{9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8}, // rotate by 9
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // rotate by 10
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, // rotate by 11
{12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // rotate by 12
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, // rotate by 13
{14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, // rotate by 14
{15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, // rotate by 15
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, // rotate by 16
{17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, // rotate by 17
{18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, // rotate by 18
{19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, // rotate by 19
{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // rotate by 20
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, // rotate by 21
{22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, // rotate by 22
{23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, // rotate by 23
{24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, // rotate by 24
{25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, // rotate by 25
{26, 27, 28, 29, 30, 31, 0, 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}, // rotate by 26
{27, 28, 29, 30, 31, 0, 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}, // rotate by 27
{28, 29, 30, 31, 0, 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}, // rotate by 28
{29, 30, 31, 0, 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}, // rotate by 29
{30, 31, 0, 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}, // rotate by 30
{31, 0, 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} // rotate by 31
};
#elif TARGET_WIDTH == 64
static const int masks[64] = {
{0, 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}, // rotate by 0
{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, 0}, // rotate by 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, 0, 1}, // rotate by 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, 0, 1, 2}, // rotate by 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, 0, 1, 2, 3}, // rotate by 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, 0, 1, 2, 3, 4}, // rotate by 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, 0, 1, 2, 3, 4, 5}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, // rotate by 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, // rotate by 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,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // rotate by 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, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, // rotate by 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, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, // rotate by 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, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, // rotate by 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, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, // rotate by 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, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 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, 0, 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}, // rotate by 40
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 0, 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}, // rotate by 41
{42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 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}, // rotate by 42
{43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0,
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}, // rotate by 43
{44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 44
{45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 45
{46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 46
{47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 47
{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 48
{49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 49
{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 50
{51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 51
{52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 52
{53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 53
{54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 54
{55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 55
{56, 57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 56
{57, 58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 57
{58, 59, 60, 61, 62, 63, 0, 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}, // rotate by 58
{59, 60, 61, 62, 63, 0, 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}, // rotate by 59
{60, 61, 62, 63, 0, 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}, // rotate by 60
{61, 62, 63, 0, 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}, // rotate by 61
{62, 63, 0, 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}, // rotate by 62
{63, 0, 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} // rotate by 63
};
#endif
i = i & (TARGET_WIDTH - 1);
return shuffle(v, masks[i]);
}
#if defined(ISPC_TARGET_AVX512SKX) || defined(ISPC_TARGET_AVX512ICL) || defined(ISPC_TARGET_AVX512SPR) || \
defined(ISPC_TARGET_AVX10_2)
#define USE_ROTATE_VIA_SHUFFLE_8
#define USE_ROTATE_VIA_SHUFFLE_16
#if TARGET_WIDTH <= 32
#define USE_ROTATE_VIA_SHUFFLE_32
#endif
#if TARGET_WIDTH <= 16
#define USE_ROTATE_VIA_SHUFFLE_64
#endif
#endif // avx512 or avx10
__declspec(safe) static inline int8 rotate(int8 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_8
return __rotate_via_shuffle(v, i);
#else
return __rotate_i8(v, i);
#endif
}
__declspec(safe) static inline unsigned int8 rotate(unsigned int8 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_8
return __rotate_via_shuffle(v, i);
#else
return __rotate_i8(v, i);
#endif
}
__declspec(safe) static inline float16 rotate(float16 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_16
return __rotate_via_shuffle(v, i);
#else
return __rotate_half(v, i);
#endif
}
__declspec(safe) static inline int16 rotate(int16 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_16
return __rotate_via_shuffle(v, i);
#else
return __rotate_i16(v, i);
#endif
}
__declspec(safe) static inline unsigned int16 rotate(unsigned int16 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_16
return __rotate_via_shuffle(v, i);
#else
return __rotate_i16(v, i);
#endif
}
__declspec(safe) static inline float rotate(float v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_32
return __rotate_via_shuffle(v, i);
#else
return __rotate_float(v, i);
#endif
}
__declspec(safe) static inline int32 rotate(int32 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_32
return __rotate_via_shuffle(v, i);
#else
return __rotate_i32(v, i);
#endif
}
__declspec(safe) static inline unsigned int32 rotate(unsigned int32 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_32
return __rotate_via_shuffle(v, i);
#else
return __rotate_i32(v, i);
#endif
}
__declspec(safe) static inline double rotate(double v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_64
return __rotate_via_shuffle(v, i);
#else
return __rotate_double(v, i);
#endif
}
__declspec(safe) static inline int64 rotate(int64 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_64
return __rotate_via_shuffle(v, i);
#else
return __rotate_i64(v, i);
#endif
}
__declspec(safe) static inline unsigned int64 rotate(unsigned int64 v, uniform int i) {
#ifdef USE_ROTATE_VIA_SHUFFLE_64
return __rotate_via_shuffle(v, i);
#else
return __rotate_i64(v, i);
#endif
}
template <typename T> unmasked varying T __shift_via_shuffle(varying T v, uniform int32 i) {
// Unlike circular shift (rotate), this is a linear shift that nullifies
// out-of-bounds elements (not wrapping around).
// Calling here shuffle with sourceIndex containing the out-of-bounds
// indexes is fine until shuffle is implemented with on-registers shuffles.
// When it is implemented via gather/scatter, it really should not be used,
// so we don't defined USE_SHIFT_VIA_SHUFFLE_64 for avx512-x32/x64 and
// USE_SHIFT_VIA_SHUFFLE_32 for avx512-x64.
varying int32 sourceIndex = programIndex + i;
varying T shuffled = shuffle(v, sourceIndex);
varying bool inBounds = (sourceIndex >= 0) & (sourceIndex < TARGET_WIDTH);
return select(inBounds, shuffled, (varying T)0);
}
#if defined(ISPC_TARGET_AVX512SKX) || defined(ISPC_TARGET_AVX512ICL) || defined(ISPC_TARGET_AVX512SPR) || \
defined(ISPC_TARGET_AVX10_2)
#define USE_SHIFT_VIA_SHUFFLE_8
#define USE_SHIFT_VIA_SHUFFLE_16
#if TARGET_WIDTH < 64
#define USE_SHIFT_VIA_SHUFFLE_32
#endif
#if TARGET_WIDTH < 32
#define USE_SHIFT_VIA_SHUFFLE_64
#endif
#endif // avx512 or avx10
__declspec(safe) static inline float shift(float v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_32
return __shift_via_shuffle(v, i);
#else
varying float result;
unmasked { result = __shift_float(v, i); }
return result;
#endif
}
__declspec(safe) static inline int8 shift(int8 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_8
return __shift_via_shuffle(v, i);
#else
varying int8 result;
unmasked { result = __shift_i8(v, i); }
return result;
#endif
}
__declspec(safe) static inline unsigned int8 shift(unsigned int8 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_8
return __shift_via_shuffle(v, i);
#else
varying unsigned int8 result;
unmasked { result = __shift_i8(v, i); }
return result;
#endif
}
__declspec(safe) static inline int16 shift(int16 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_16
return __shift_via_shuffle(v, i);
#else
varying int16 result;
unmasked { result = __shift_i16(v, i); }
return result;
#endif
}
__declspec(safe) static inline unsigned int16 shift(unsigned int16 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_16
return __shift_via_shuffle(v, i);
#else
varying unsigned int16 result;
unmasked { result = __shift_i16(v, i); }
return result;
#endif
}
__declspec(safe) static inline float16 shift(float16 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_16
return __shift_via_shuffle(v, i);
#else
varying float16 result;
unmasked { result = __shift_half(v, i); }
return result;
#endif
}
__declspec(safe) static inline int32 shift(int32 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_32
return __shift_via_shuffle(v, i);
#else
varying int32 result;
unmasked { result = __shift_i32(v, i); }
return result;
#endif
}
__declspec(safe) static inline unsigned int32 shift(unsigned int32 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_32
return __shift_via_shuffle(v, i);
#else
varying unsigned int32 result;
unmasked { result = __shift_i32(v, i); }
return result;
#endif
}
__declspec(safe) static inline double shift(double v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_64
return __shift_via_shuffle(v, i);
#else
varying double result;
unmasked { result = __shift_double(v, i); }
return result;
#endif
}
__declspec(safe) static inline int64 shift(int64 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_64
return __shift_via_shuffle(v, i);
#else
varying int64 result;
unmasked { result = __shift_i64(v, i); }
return result;
#endif
}
__declspec(safe) static inline unsigned int64 shift(unsigned int64 v, uniform int i) {
#ifdef USE_SHIFT_VIA_SHUFFLE_64
return __shift_via_shuffle(v, i);
#else
varying unsigned int64 result;
unmasked { result = __shift_i64(v, i); }
return result;
#endif
}
__declspec(safe) static inline float shuffle(float v, int i) { return __shuffle_float(v, i); }
__declspec(safe) static inline int8 shuffle(int8 v, int i) { return __shuffle_i8(v, i); }
__declspec(safe) static inline unsigned int8 shuffle(unsigned int8 v, int i) { return __shuffle_i8(v, i); }
__declspec(safe) static inline int16 shuffle(int16 v, int i) { return __shuffle_i16(v, i); }
__declspec(safe) static inline unsigned int16 shuffle(unsigned int16 v, int i) { return __shuffle_i16(v, i); }
__declspec(safe) static inline float16 shuffle(float16 v, int i) { return __shuffle_half(v, i); }
__declspec(safe) static inline int32 shuffle(int32 v, int i) { return __shuffle_i32(v, i); }
__declspec(safe) static inline unsigned int32 shuffle(unsigned int32 v, int i) { return __shuffle_i32(v, i); }
__declspec(safe) static inline double shuffle(double v, int i) { return __shuffle_double(v, i); }
__declspec(safe) static inline int64 shuffle(int64 v, int i) { return __shuffle_i64(v, i); }
__declspec(safe) static inline unsigned int64 shuffle(unsigned int64 v, int i) { return __shuffle_i64(v, i); }
__declspec(safe) static inline float shuffle(float v0, float v1, int i) { return __shuffle2_float(v0, v1, i); }
__declspec(safe) static inline int8 shuffle(int8 v0, int8 v1, int i) { return __shuffle2_i8(v0, v1, i); }
__declspec(safe) static inline unsigned int8 shuffle(unsigned int8 v0, unsigned int8 v1, int i) {
return __shuffle2_i8(v0, v1, i);
}
__declspec(safe) static inline int16 shuffle(int16 v0, int16 v1, int i) { return __shuffle2_i16(v0, v1, i); }
__declspec(safe) static inline unsigned int16 shuffle(unsigned int16 v0, unsigned int16 v1, int i) {
return __shuffle2_i16(v0, v1, i);
}
__declspec(safe) static inline float16 shuffle(float16 v0, float16 v1, int i) { return __shuffle2_half(v0, v1, i); }
__declspec(safe) static inline int32 shuffle(int32 v0, int32 v1, int i) { return __shuffle2_i32(v0, v1, i); }
__declspec(safe) static inline unsigned int32 shuffle(unsigned int32 v0, unsigned int32 v1, int i) {
return __shuffle2_i32(v0, v1, i);
}
__declspec(safe) static inline double shuffle(double v0, double v1, int i) { return __shuffle2_double(v0, v1, i); }
__declspec(safe) static inline int64 shuffle(int64 v0, int64 v1, int i) { return __shuffle2_i64(v0, v1, i); }
__declspec(safe) static inline unsigned int64 shuffle(unsigned int64 v0, unsigned int64 v1, int i) {
return __shuffle2_i64(v0, v1, i);
}
// x[i]
__declspec(safe, cost1) static inline uniform float extract(float x, uniform int i) {
return floatbits(__extract_int32((int)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform bool extract(bool x, uniform int i) { return __extract_bool(x, i); }
__declspec(safe, cost1) static inline uniform int8 extract(int8 x, uniform int i) { return __extract_int8(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int8 extract(unsigned int8 x, uniform int i) {
return __extract_int8(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform int16 extract(int16 x, uniform int i) { return __extract_int16(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int16 extract(unsigned int16 x, uniform int i) {
return __extract_int16(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform float16 extract(float16 x, uniform int i) {
return float16bits(__extract_int16((int16)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform int32 extract(int32 x, uniform int i) { return __extract_int32(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int32 extract(unsigned int32 x, uniform int i) {
return __extract_int32(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform double extract(double x, uniform int i) {
return doublebits(__extract_int64((int64)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform int64 extract(int64 x, uniform int i) { return __extract_int64(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int64 extract(unsigned int64 x, uniform int i) {
return __extract_int64(x, (uniform unsigned int)i);
}
// x[i] = v
__declspec(safe, cost1) static inline float insert(float x, uniform int i, uniform float v) {
return floatbits(__insert_int32((int)intbits(x), i, (uniform int)intbits(v)));
}
__declspec(safe, cost1) static inline bool insert(bool x, uniform int i, uniform bool v) {
return __insert_bool(x, i, v);
}
__declspec(safe, cost1) static inline int8 insert(int8 x, uniform int i, uniform int8 v) {
return __insert_int8(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int8 insert(unsigned int8 x, uniform int i, uniform unsigned int8 v) {
return __insert_int8(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline float16 insert(float16 x, uniform int i, uniform float16 v) {
return float16bits(__insert_int16((int16)intbits(x), i, (uniform int16)intbits(v)));
}
__declspec(safe, cost1) static inline int16 insert(int16 x, uniform int i, uniform int16 v) {
return __insert_int16(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int16 insert(unsigned int16 x, uniform int i, uniform unsigned int16 v) {
return __insert_int16(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline int32 insert(int32 x, uniform int i, uniform int32 v) {
return __insert_int32(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int32 insert(unsigned int32 x, uniform int i, uniform unsigned int32 v) {
return __insert_int32(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline double insert(double x, uniform int i, uniform double v) {
return doublebits(__insert_int64((int64)intbits(x), i, (uniform int64)intbits(v)));
}
__declspec(safe, cost1) static inline int64 insert(int64 x, uniform int i, uniform int64 v) {
return __insert_int64(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int64 insert(unsigned int64 x, uniform int i, uniform unsigned int64 v) {
return __insert_int64(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline uniform int32 sign_extend(uniform bool v) { return __sext_uniform_bool(v); }
__declspec(safe, cost1) static inline int32 sign_extend(bool v) { return __sext_varying_bool(v); }
__declspec(safe) static inline uniform bool any(bool v) {
// We only care about whether "any" is true for the active program instances,
// so we have to make v with the current program mask.
#if (ISPC_MASK_BITS == 1)
return __any(v & __mask);
#else
return __any((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
__declspec(safe) static inline uniform bool all(bool v) {
// As with any(), we need to explicitly mask v with the current program mask
// so we're only looking at the current lanes
#if (ISPC_MASK_BITS == 1)
return __all(v | !__mask);
#else
// !__mask returns a 'bool' type. But for the logic to work, we need to
// convert this to a 'UIntMaskType' type. For 'TRUE' bool, we can be
// certain that the LSB will be set to '1'. Therefore using
// '__sext_varying_bool(!__mask)' to convert '!__mask' to 'UIntMaskType'
// is the safest option to ensure all bits are set to '1' for 'TRUE' and
// '0' for 'False'.
return __all((UIntMaskType)__sext_varying_bool(v) | (UIntMaskType)__sext_varying_bool(!__mask));
#endif
}
__declspec(safe) static inline uniform bool none(bool v) {
// As with any(), we need to explicitly mask v with the current program mask
// so we're only looking at the current lanes
#if (ISPC_MASK_BITS == 1)
return __none(v & __mask);
#else
return __none((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
__declspec(safe) static inline uniform int32 popcnt(uniform int32 v) { return __popcnt_int32(v); }
__declspec(safe) static inline uniform int popcnt(uniform int64 v) { return (int32)__popcnt_int64(v); }
__declspec(safe) static inline int popcnt(int v) {
int r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, popcnt(extract(v, i)));
return __mask ? r : 0;
}
__declspec(safe) static inline int popcnt(int64 v) {
int r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, popcnt(extract(v, i)));
return __mask ? r : 0;
}
__declspec(safe) static inline uniform int popcnt(bool v) {
// As with any() and all(), only count across the active lanes
#if (ISPC_MASK_BITS == 1)
return __popcnt_int64(__movmsk(v & __mask));
#else
return __popcnt_int64(__movmsk((UIntMaskType)__sext_varying_bool(v) & __mask));
#endif
}
__declspec(safe) static inline uniform unsigned int64 lanemask() { return __movmsk(__mask); }
__declspec(safe) static inline uniform unsigned int64 packmask(bool v) {
#if (ISPC_MASK_BITS == 1)
return __movmsk(v & __mask);
#else
return __movmsk((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
///////////////////////////////////////////////////////////////////////////
// memcpy/memmove/memset
static inline void memcpy(void *uniform dst, void *uniform src, uniform int32 count) {
if (__is_xe_target) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
__memcpy32((uniform int8 * uniform) dst, (uniform int8 * uniform) src, count);
}
}
static inline void memcpy64(void *uniform dst, void *uniform src, uniform int64 count) {
if (__is_xe_target) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
__memcpy64((uniform int8 * uniform) dst, (uniform int8 * uniform) src, count);
}
}
static inline void memcpy(void *varying dst, void *varying src, int32 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
if (__is_xe_target) {
for (uniform int j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
__memcpy32((uniform int8 * uniform) d, (uniform int8 * uniform) s, extract(count, i));
}
}
}
static inline void memcpy64(void *varying dst, void *varying src, int64 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
if (__is_xe_target) {
for (uniform int64 j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
__memcpy64((uniform int8 * uniform) d, (uniform int8 * uniform) s, extract(count, i));
}
}
}
static inline void memmove(void *uniform dst, void *uniform src, uniform int32 count) {
if (__is_xe_target) {
if ((uintptr_t)dst - (uintptr_t)src >= (size_t)count) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
for (uniform int j = count - 1; j >= 0; j--) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
}
} else {
__memmove32((uniform int8 * uniform) dst, (uniform int8 * uniform) src, count);
}
}
static inline void memmove64(void *uniform dst, void *uniform src, uniform int64 count) {
if (__is_xe_target) {
if ((uintptr_t)dst - (uintptr_t)src >= (size_t)count) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
for (uniform int64 j = count - 1; j >= 0; j--) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
}
} else {
__memmove64((uniform int8 * uniform) dst, (uniform int8 * uniform) src, count);
}
}
static inline void memmove(void *varying dst, void *varying src, int32 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
uniform int c = extract(count, i);
if (__is_xe_target) {
if ((uintptr_t)d - (uintptr_t)s >= (size_t)c) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
for (uniform int j = c - 1; j >= 0; j--) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
}
} else {
__memmove32((uniform int8 * uniform) d, (uniform int8 * uniform) s, c);
}
}
}
static inline void memmove64(void *varying dst, void *varying src, int64 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
uniform int64 c = extract(count, i);
if (__is_xe_target) {
if ((uintptr_t)d - (uintptr_t)s >= (size_t)c) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
for (uniform int64 j = c - 1; j >= 0; j--) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
}
} else {
__memmove64((uniform int8 * uniform) d, (uniform int8 * uniform) s, c);
}
}
}
static inline void memset(void *uniform ptr, uniform int8 val, uniform int32 count) {
if (__is_xe_target) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) ptr)[j] = val;
}
} else {
__memset32((uniform int8 * uniform) ptr, val, count);
}
}
static inline void memset64(void *uniform ptr, uniform int8 val, uniform int64 count) {
if (__is_xe_target) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) ptr)[j] = val;
}
} else {
__memset64((uniform int8 * uniform) ptr, val, count);
}
}
static inline void memset(void *varying ptr, int8 val, int32 count) {
void *uniform pa[programCount];
pa[programIndex] = ptr;
foreach_active(i) {
if (__is_xe_target) {
void *uniform d = pa[i];
for (uniform int j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = extract(val, i);
}
} else {
__memset32((uniform int8 * uniform) pa[i], extract(val, i), extract(count, i));
}
}
}
static inline void memset64(void *varying ptr, int8 val, int64 count) {
void *uniform pa[programCount];
pa[programIndex] = ptr;
foreach_active(i) {
if (__is_xe_target) {
void *uniform d = pa[i];
for (uniform int64 j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = extract(val, i);
}
} else {
__memset64((uniform int8 * uniform) pa[i], extract(val, i), extract(count, i));
}
}
}
///////////////////////////////////////////////////////////////////////////
// count leading/trailing zeros
__declspec(safe, cost1) static inline uniform unsigned int32 count_leading_zeros(uniform unsigned int32 v) {
return __count_leading_zeros_uniform_i32(v);
}
__declspec(safe, cost1) static inline uniform unsigned int64 count_leading_zeros(uniform unsigned int64 v) {
return __count_leading_zeros_uniform_i64(v);
}
__declspec(safe, cost1) static inline uniform unsigned int32 count_trailing_zeros(uniform unsigned int32 v) {
return __count_trailing_zeros_uniform_i32(v);
}
__declspec(safe, cost1) static inline uniform unsigned int64 count_trailing_zeros(uniform unsigned int64 v) {
return __count_trailing_zeros_uniform_i64(v);
}
__declspec(safe, cost1) static inline uniform int32 count_leading_zeros(uniform int32 v) {
return __count_leading_zeros_uniform_i32(v);
}
__declspec(safe, cost1) static inline uniform int64 count_leading_zeros(uniform int64 v) {
return __count_leading_zeros_uniform_i64(v);
}
__declspec(safe, cost1) static inline uniform int32 count_trailing_zeros(uniform int32 v) {
return __count_trailing_zeros_uniform_i32(v);
}
__declspec(safe, cost1) static inline uniform int64 count_trailing_zeros(uniform int64 v) {
return __count_trailing_zeros_uniform_i64(v);
}
__declspec(safe) static inline unsigned int32 count_leading_zeros(unsigned int32 v) {
if (__have_conflict_detection) {
return __count_leading_zeros_varying_i32(v);
} else {
unsigned int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, (uniform unsigned int32)__count_leading_zeros_uniform_i32(extract(v, i)));
return r;
}
}
__declspec(safe) static inline unsigned int64 count_leading_zeros(unsigned int64 v) {
if (__have_conflict_detection) {
return __count_leading_zeros_varying_i64(v);
} else {
unsigned int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, (uniform unsigned int64)__count_leading_zeros_uniform_i64(extract(v, i)));
return r;
}
}
__declspec(safe) static inline unsigned int32 count_trailing_zeros(unsigned int32 v) {
// Using the vector LLVM's cttz intrinsic is more efficient than
// counting trailing zeros in a loop. However, it is not true for ctlz -
// a loop of uniform calculations performs better than vector LLVM's ctlz intrinsic.
// https://github.com/llvm/llvm-project/issues/124993
return __count_trailing_zeros_varying_i32(v);
}
__declspec(safe) static inline unsigned int64 count_trailing_zeros(unsigned int64 v) {
// Using the vector LLVM's cttz intrinsic is more efficient than
// counting trailing zeros in a loop. However, it is not true for ctlz -
// a loop of uniform calculations performs better than vector LLVM's ctlz intrinsic.
// https://github.com/llvm/llvm-project/issues/124993
return __count_trailing_zeros_varying_i64(v);
}
__declspec(safe) static inline int32 count_leading_zeros(int32 v) {
if (__have_conflict_detection) {
return __count_leading_zeros_varying_i32(v);
} else {
int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_uniform_i32(extract(v, i)));
return r;
}
}
__declspec(safe) static inline int64 count_leading_zeros(int64 v) {
if (__have_conflict_detection) {
return __count_leading_zeros_varying_i64(v);
} else {
int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_uniform_i64(extract(v, i)));
return r;
}
}
__declspec(safe) static inline int32 count_trailing_zeros(int32 v) {
// Using the vector LLVM's cttz intrinsic is more efficient than
// counting trailing zeros in a loop. However, it is not true for ctlz -
// a loop of uniform calculations performs better than vector LLVM's ctlz intrinsic.
// https://github.com/llvm/llvm-project/issues/124993
return __count_trailing_zeros_varying_i32(v);
}
__declspec(safe) static inline int64 count_trailing_zeros(int64 v) {
// Using the vector LLVM's cttz intrinsic is more efficient than
// counting trailing zeros in a loop. However, it is not true for ctlz -
// a loop of uniform calculations performs better than vector LLVM's ctlz intrinsic.
// https://github.com/llvm/llvm-project/issues/124993
return __count_trailing_zeros_varying_i64(v);
}
///////////////////////////////////////////////////////////////////////////
// AOS/SOA conversion
static inline void aos_to_soa2(uniform float a[], varying float *uniform v0, varying float *uniform v1) {
__aos_to_soa2_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1);
}
static inline void soa_to_aos2(float v0, float v1, uniform float a[]) { __soa_to_aos2_float(v0, v1, (opaque_ptr_t)a); }
static inline void aos_to_soa3(uniform float a[], varying float *uniform v0, varying float *uniform v1,
varying float *uniform v2) {
__aos_to_soa3_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2);
}
static inline void soa_to_aos3(float v0, float v1, float v2, uniform float a[]) {
__soa_to_aos3_float(v0, v1, v2, (opaque_ptr_t)a);
}
static inline void aos_to_soa4(uniform float a[], varying float *uniform v0, varying float *uniform v1,
varying float *uniform v2, varying float *uniform v3) {
__aos_to_soa4_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2, (opaque_ptr_t)v3);
}
static inline void soa_to_aos4(float v0, float v1, float v2, float v3, uniform float a[]) {
__soa_to_aos4_float(v0, v1, v2, v3, (opaque_ptr_t)a);
}
static inline void aos_to_soa2(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1) {
aos_to_soa2((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1);
}
static inline void soa_to_aos2(int32 v0, int32 v1, uniform int32 a[]) {
soa_to_aos2(floatbits(v0), floatbits(v1), (uniform float *uniform)a);
}
static inline void aos_to_soa3(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1,
varying int32 *uniform v2) {
aos_to_soa3((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1,
(varying float *uniform)v2);
}
static inline void soa_to_aos3(int32 v0, int32 v1, int32 v2, uniform int32 a[]) {
soa_to_aos3(floatbits(v0), floatbits(v1), floatbits(v2), (uniform float *uniform)a);
}
static inline void aos_to_soa4(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1,
varying int32 *uniform v2, varying int32 *uniform v3) {
aos_to_soa4((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1,
(varying float *uniform)v2, (varying float *uniform)v3);
}
static inline void soa_to_aos4(int32 v0, int32 v1, int32 v2, int32 v3, uniform int32 a[]) {
soa_to_aos4(floatbits(v0), floatbits(v1), floatbits(v2), floatbits(v3), (uniform float *uniform)a);
}
static inline void aos_to_soa2(uniform double a[], varying double *uniform v0, varying double *uniform v1) {
__aos_to_soa2_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1);
}
static inline void soa_to_aos2(double v0, double v1, uniform double a[]) {
__soa_to_aos2_double(v0, v1, (opaque_ptr_t)a);
}
static inline void aos_to_soa3(uniform double a[], varying double *uniform v0, varying double *uniform v1,
varying double *uniform v2) {
__aos_to_soa3_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2);
}
static inline void soa_to_aos3(double v0, double v1, double v2, uniform double a[]) {
__soa_to_aos3_double(v0, v1, v2, (opaque_ptr_t)a);
}
static inline void aos_to_soa4(uniform double a[], varying double *uniform v0, varying double *uniform v1,
varying double *uniform v2, varying double *uniform v3) {
__aos_to_soa4_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2, (opaque_ptr_t)v3);
}
static inline void soa_to_aos4(double v0, double v1, double v2, double v3, uniform double a[]) {
__soa_to_aos4_double(v0, v1, v2, v3, (opaque_ptr_t)a);
}
static inline void aos_to_soa2(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1) {
aos_to_soa2((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1);
}
static inline void soa_to_aos2(int64 v0, int64 v1, uniform int64 a[]) {
soa_to_aos2(doublebits(v0), doublebits(v1), (uniform double *uniform)a);
}
static inline void aos_to_soa3(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1,
varying int64 *uniform v2) {
aos_to_soa3((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1,
(varying double *uniform)v2);
}
static inline void soa_to_aos3(int64 v0, int64 v1, int64 v2, uniform int64 a[]) {
soa_to_aos3(doublebits(v0), doublebits(v1), doublebits(v2), (uniform double *uniform)a);
}
static inline void aos_to_soa4(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1,
varying int64 *uniform v2, varying int64 *uniform v3) {
aos_to_soa4((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1,
(varying double *uniform)v2, (varying double *uniform)v3);
}
static inline void soa_to_aos4(int64 v0, int64 v1, int64 v2, int64 v3, uniform int64 a[]) {
soa_to_aos4(doublebits(v0), doublebits(v1), doublebits(v2), doublebits(v3), (uniform double *uniform)a);
}
///////////////////////////////////////////////////////////////////////////
// Prefetching
__declspec(safe, cost1) static inline void prefetch_l1(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_1((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_1((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l1(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_1((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_1((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l2(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_2((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_2((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l2(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_2((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_2((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l3(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_3((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_3((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l3(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_3((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_3((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_nt(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_nt((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_nt((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_nt(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_nt((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_nt((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetchw_l1(const void *uniform ptr) {
__prefetch_write_uniform_1((opaque_ptr_t)ptr);
}
__declspec(safe, cost1) static inline void prefetchw_l2(const void *uniform ptr) {
__prefetch_write_uniform_2((opaque_ptr_t)ptr);
}
__declspec(safe, cost1) static inline void prefetchw_l3(const void *uniform ptr) {
__prefetch_write_uniform_3((opaque_ptr_t)ptr);
}
static inline void prefetch_l1(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_1((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_1((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l1(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_1((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_1((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l2(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_2((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_2((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l2(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_2((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_2((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l3(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_3((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_3((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l3(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_3((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_3((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_nt(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_nt((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_nt((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_nt(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_nt((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_nt((int64)ptr, (IntMaskType)__mask);
}
}
__declspec(safe, cost1) static inline void prefetchw_l1(const void *varying ptr) {
__pseudo_prefetch_write_varying_1((int64)ptr, (IntMaskType)__mask);
}
__declspec(safe, cost1) static inline void prefetchw_l2(const void *varying ptr) {
__pseudo_prefetch_write_varying_2((int64)ptr, (IntMaskType)__mask);
}
__declspec(safe, cost1) static inline void prefetchw_l3(const void *varying ptr) {
__pseudo_prefetch_write_varying_3((int64)ptr, (IntMaskType)__mask);
}
///////////////////////////////////////////////////////////////////////////
// non-short-circuiting alternatives
__declspec(safe, cost1) static inline bool and (bool a, bool b) { return a && b; }
__declspec(safe, cost1) static inline uniform bool and (uniform bool a, uniform bool b) { return a && b; }
__declspec(safe, cost1) static inline bool or (bool a, bool b) { return a || b; }
__declspec(safe, cost1) static inline uniform bool or (uniform bool a, uniform bool b) { return a || b; }
__declspec(safe, cost1) static inline int8 select(bool cond, int8 t, int8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint8 select(bool cond, uint8 t, uint8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int8 select(uniform bool cond, int8 t, int8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint8 select(uniform bool cond, uint8 t, uint8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int8 select(uniform bool cond, uniform int8 t, uniform int8 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline int16 select(bool cond, int16 t, int16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint16 select(bool cond, uint16 t, uint16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int16 select(uniform bool cond, int16 t, int16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint16 select(uniform bool cond, uint16 t, uint16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int16 select(uniform bool cond, uniform int16 t, uniform int16 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline float16 select(bool cond, float16 t, float16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline float16 select(uniform bool cond, float16 t, float16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform float16 select(uniform bool cond, uniform float16 t, uniform float16 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline int32 select(bool cond, int32 t, int32 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint32 select(bool cond, uint32 t, uint32 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int32 select(uniform bool cond, int32 t, int32 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint32 select(uniform bool cond, uint32 t, uint32 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int32 select(uniform bool cond, uniform int32 t, uniform int32 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline int64 select(bool cond, int64 t, int64 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint64 select(bool cond, uint64 t, uint64 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int64 select(uniform bool cond, int64 t, int64 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uint64 select(uniform bool cond, uint64 t, uint64 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int64 select(uniform bool cond, uniform int64 t, uniform int64 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline float select(bool cond, float t, float f) { return cond ? t : f; }
__declspec(safe, cost1) static inline float select(uniform bool cond, float t, float f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform float select(uniform bool cond, uniform float t, uniform float f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline double select(bool cond, double t, double f) { return cond ? t : f; }
__declspec(safe, cost1) static inline double select(uniform bool cond, double t, double f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform double select(uniform bool cond, uniform double t, uniform double f) {
return cond ? t : f;
}
///////////////////////////////////////////////////////////////////////////
// Horizontal ops / reductions
__declspec(safe) static inline uniform int16 reduce_add(int8 x) { return __reduce_add_int8(__mask ? x : (int8)0); }
__declspec(safe) static inline uniform int8 reduce_min(int8 v) {
// Set values for non-running lanes to the maximum int8 value so
// they don't affect the result.
return __reduce_min_int8(__mask ? v : INT8_MAX);
}
__declspec(safe) static inline uniform int8 reduce_max(int8 v) {
// Set values for non-running lanes to the minimum int8 value so
// they don't affect the result.
return __reduce_max_int8(__mask ? v : INT8_MIN);
}
__declspec(safe) static inline uniform unsigned int16 reduce_add(unsigned int8 x) {
return __reduce_add_int8(__mask ? x : (int8)0);
}
__declspec(safe) static inline uniform unsigned int8 reduce_min(unsigned int8 v) {
// Set values for non-running lanes to the maximum uint8 value so
// they don't affect the result.
return __reduce_min_uint8(__mask ? v : UINT8_MAX);
}
__declspec(safe) static inline uniform unsigned int8 reduce_max(unsigned int8 v) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_max_uint8(__mask ? v : 0);
}
__declspec(safe) static inline uniform int32 reduce_add(int16 x) { return __reduce_add_int16(__mask ? x : (int16)0); }
__declspec(safe) static inline uniform int16 reduce_min(int16 v) {
// Set values for non-running lanes to the maximum int8 value so
// they don't affect the result.
return __reduce_min_int16(__mask ? v : INT16_MAX);
}
__declspec(safe) static inline uniform int16 reduce_max(int16 v) {
// Set values for non-running lanes to the minimum int8 value so
// they don't affect the result.
return __reduce_max_int16(__mask ? v : INT16_MIN);
}
__declspec(safe) static inline uniform unsigned int32 reduce_add(unsigned int16 x) {
return __reduce_add_int16(__mask ? x : (int16)0);
}
__declspec(safe) static inline uniform unsigned int16 reduce_min(unsigned int16 v) {
// Set values for non-running lanes to the maximum uint8 value so
// they don't affect the result.
return __reduce_min_uint16(__mask ? v : UINT16_MAX);
}
__declspec(safe) static inline uniform unsigned int16 reduce_max(unsigned int16 v) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_max_uint16(__mask ? v : 0);
}
__declspec(safe) static inline uniform float16 reduce_add(float16 x) {
// zero the lanes where the mask is off
return __reduce_add_half(__mask ? x : 0.0f16);
}
__declspec(safe) static inline uniform float16 reduce_min(float16 v) {
// For the lanes where the mask is off, replace the given value with
// infinity, so that it doesn't affect the result.
const int16 iflt_max = 0x7c00; // infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_min_float() are calculated without a mask.
bool test = __mask;
uniform float16 result;
unmasked { result = __reduce_min_half(test ? v : float16bits(iflt_max)); }
return result;
}
__declspec(safe) static inline uniform float16 reduce_max(float16 v) {
// For the lanes where the mask is off, replace the given value with
// negative infinity, so that it doesn't affect the result.
const int16 iflt_neg_max = 0xfc00; // -infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_max_half() are calculated without a mask.
bool test = __mask;
uniform float16 result;
unmasked { result = __reduce_max_half(test ? v : float16bits(iflt_neg_max)); }
return result;
}
__declspec(safe) static inline uniform float reduce_add(float x) {
// zero the lanes where the mask is off
return __reduce_add_float(__mask ? x : 0.);
}
__declspec(safe) static inline uniform float reduce_min(float v) {
// For the lanes where the mask is off, replace the given value with
// infinity, so that it doesn't affect the result.
int iflt_max = 0x7f800000; // infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_min_float() are calculated without a mask.
bool test = __mask;
uniform float result;
unmasked { result = __reduce_min_float(test ? v : floatbits(iflt_max)); }
return result;
}
__declspec(safe) static inline uniform float reduce_max(float v) {
// For the lanes where the mask is off, replace the given value with
// negative infinity, so that it doesn't affect the result.
const int iflt_neg_max = 0xff800000; // -infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_max_float() are calculated without a mask.
bool test = __mask;
uniform float result;
unmasked { result = __reduce_max_float(test ? v : floatbits(iflt_neg_max)); }
return result;
}
__declspec(safe) static inline uniform int64 reduce_add(int32 x) {
// Zero out the values for lanes that aren't running
return __reduce_add_int32(__mask ? x : 0);
}
__declspec(safe) static inline uniform int reduce_min(int v) {
// Set values for non-running lanes to the maximum integer value so
// they don't affect the result.
return __reduce_min_int32(__mask ? v : INT32_MAX);
}
__declspec(safe) static inline uniform int reduce_max(int v) {
// Set values for non-running lanes to the minimum integer value so
// they don't affect the result.
return __reduce_max_int32(__mask ? v : INT32_MIN);
}
__declspec(safe) static inline uniform unsigned int64 reduce_add(unsigned int32 x) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_add_int32(__mask ? x : 0);
}
__declspec(safe) static inline uniform unsigned int reduce_min(unsigned int v) {
// Set values for non-running lanes to the maximum unsigned integer
// value so they don't affect the result.
return __reduce_min_uint32(__mask ? v : UINT32_MAX);
}
__declspec(safe) static inline uniform unsigned int reduce_max(unsigned int v) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_max_uint32(__mask ? v : 0);
}
__declspec(safe) static inline uniform double reduce_add(double x) {
// zero the lanes where the mask is off
return __reduce_add_double(__mask ? x : 0.);
}
__declspec(safe) static inline uniform double reduce_min(double v) {
int64 iflt_max = 0x7ff0000000000000; // infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_min_double() are calculated without a mask.
bool test = __mask;
uniform double result;
unmasked { result = __reduce_min_double(test ? v : doublebits(iflt_max)); }
return result;
}
__declspec(safe) static inline uniform double reduce_max(double v) {
const int64 iflt_neg_max = 0xfff0000000000000; // -infinity
// unmasked block is needed to make sure that argument for unmasked
// function __reduce_max_double() are calculated without a mask.
bool test = __mask;
uniform double result;
unmasked { result = __reduce_max_double(test ? v : doublebits(iflt_neg_max)); }
return result;
}
__declspec(safe) static inline uniform int64 reduce_add(int64 x) {
// Zero out the values for lanes that aren't running
return __reduce_add_int64(__mask ? x : 0);
}
__declspec(safe) static inline uniform int64 reduce_min(int64 v) {
// Set values for non-running lanes to the maximum integer value so
// they don't affect the result.
return __reduce_min_int64(__mask ? v : INT64_MAX);
}
__declspec(safe) static inline uniform int64 reduce_max(int64 v) {
// Set values for non-running lanes to the minimum integer value so
// they don't affect the result.
return __reduce_max_int64(__mask ? v : INT64_MIN);
}
__declspec(safe) static inline uniform unsigned int64 reduce_add(unsigned int64 x) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_add_int64(__mask ? x : 0);
}
__declspec(safe) static inline uniform unsigned int64 reduce_min(unsigned int64 v) {
// Set values for non-running lanes to the maximum unsigned integer
// value so they don't affect the result.
return __reduce_min_uint64(__mask ? v : UINT64_MAX);
}
__declspec(safe) static inline uniform unsigned int64 reduce_max(unsigned int64 v) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_max_uint64(__mask ? v : 0);
}
#define REDUCE_EQUAL(TYPE, FUNCTYPE, MASKTYPE) \
__declspec(safe) static inline uniform bool reduce_equal(TYPE v) { \
uniform int8 unusedValue; \
return __reduce_equal_##FUNCTYPE(v, &unusedValue, (MASKTYPE)__mask); \
} \
__declspec(safe) static inline uniform bool reduce_equal(TYPE v, uniform TYPE *uniform value) { \
return __reduce_equal_##FUNCTYPE(v, (opaque_ptr_t)value, (MASKTYPE)__mask); \
}
REDUCE_EQUAL(float16, half, IntMaskType)
REDUCE_EQUAL(int8, int8, IntMaskType)
REDUCE_EQUAL(unsigned int8, int8, UIntMaskType)
REDUCE_EQUAL(int16, int16, IntMaskType)
REDUCE_EQUAL(unsigned int16, int16, UIntMaskType)
REDUCE_EQUAL(int32, int32, IntMaskType)
REDUCE_EQUAL(unsigned int32, int32, UIntMaskType)
REDUCE_EQUAL(float, float, IntMaskType)
REDUCE_EQUAL(int64, int64, IntMaskType)
REDUCE_EQUAL(unsigned int64, int64, UIntMaskType)
REDUCE_EQUAL(double, double, IntMaskType)
static float16 exclusive_scan_add(float16 v) { return __exclusive_scan_add_half(v, __mask); }
static int8 exclusive_scan_add(int8 v) { return __exclusive_scan_add_i8(v, (IntMaskType)__mask); }
static unsigned int8 exclusive_scan_add(unsigned int8 v) {
return __exclusive_scan_add_i8((int8)v, (IntMaskType)__mask);
}
static int16 exclusive_scan_add(int16 v) { return __exclusive_scan_add_i16(v, (IntMaskType)__mask); }
static unsigned int16 exclusive_scan_add(unsigned int16 v) {
return __exclusive_scan_add_i16((int16)v, (IntMaskType)__mask);
}
static int32 exclusive_scan_add(int32 v) { return __exclusive_scan_add_i32(v, (IntMaskType)__mask); }
static unsigned int32 exclusive_scan_add(unsigned int32 v) {
return __exclusive_scan_add_i32((int32)v, (IntMaskType)__mask);
}
static float exclusive_scan_add(float v) { return __exclusive_scan_add_float(v, __mask); }
static int64 exclusive_scan_add(int64 v) { return __exclusive_scan_add_i64(v, (IntMaskType)__mask); }
static unsigned int64 exclusive_scan_add(unsigned int64 v) { return __exclusive_scan_add_i64(v, (UIntMaskType)__mask); }
static double exclusive_scan_add(double v) { return __exclusive_scan_add_double(v, __mask); }
static int8 exclusive_scan_and(int8 v) { return __exclusive_scan_and_i8(v, (IntMaskType)__mask); }
static unsigned int8 exclusive_scan_and(unsigned int8 v) { return __exclusive_scan_and_i8(v, (UIntMaskType)__mask); }
static int16 exclusive_scan_and(int16 v) { return __exclusive_scan_and_i16(v, (IntMaskType)__mask); }
static unsigned int16 exclusive_scan_and(unsigned int16 v) { return __exclusive_scan_and_i16(v, (UIntMaskType)__mask); }
static int32 exclusive_scan_and(int32 v) { return __exclusive_scan_and_i32(v, (IntMaskType)__mask); }
static unsigned int32 exclusive_scan_and(unsigned int32 v) { return __exclusive_scan_and_i32(v, (UIntMaskType)__mask); }
static int64 exclusive_scan_and(int64 v) { return __exclusive_scan_and_i64(v, (IntMaskType)__mask); }
static unsigned int64 exclusive_scan_and(unsigned int64 v) { return __exclusive_scan_and_i64(v, (UIntMaskType)__mask); }
static int8 exclusive_scan_or(int8 v) { return __exclusive_scan_or_i8(v, (IntMaskType)__mask); }
static unsigned int8 exclusive_scan_or(unsigned int8 v) { return __exclusive_scan_or_i8(v, (UIntMaskType)__mask); }
static int16 exclusive_scan_or(int16 v) { return __exclusive_scan_or_i16(v, (IntMaskType)__mask); }
static unsigned int16 exclusive_scan_or(unsigned int16 v) { return __exclusive_scan_or_i16(v, (UIntMaskType)__mask); }
static int32 exclusive_scan_or(int32 v) { return __exclusive_scan_or_i32(v, (IntMaskType)__mask); }
static unsigned int32 exclusive_scan_or(unsigned int32 v) { return __exclusive_scan_or_i32(v, (UIntMaskType)__mask); }
static int64 exclusive_scan_or(int64 v) { return __exclusive_scan_or_i64(v, (IntMaskType)__mask); }
static unsigned int64 exclusive_scan_or(unsigned int64 v) { return __exclusive_scan_or_i64(v, (UIntMaskType)__mask); }
///////////////////////////////////////////////////////////////////////////
// packed load, store
/* unsigned int32 implementations. */
// unsigned int32 load.
static inline uniform int packed_load_active(uniform unsigned int a[], varying unsigned int *uniform vals) {
return __packed_load_activei32((opaque_ptr_t)a, (opaque_ptr_t)vals, (UIntMaskType)__mask);
}
// unsigned int32 store.
static inline uniform int packed_store_active(uniform unsigned int a[], unsigned int vals) {
return __packed_store_activei32((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
// unsigned int32 store2.
static inline uniform int packed_store_active2(uniform unsigned int a[], unsigned int vals) {
return __packed_store_active2i32((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
/* unsigned int16 implementations. */
// unsigned int16 load.
static inline uniform int packed_load_active(uniform unsigned int16 a[], varying unsigned int16 *uniform vals) {
return __packed_load_activei16((opaque_ptr_t)a, (opaque_ptr_t)vals, (UIntMaskType)__mask);
}
// unsigned int16 store.
static inline uniform int packed_store_active(uniform unsigned int16 a[], unsigned int16 vals) {
return __packed_store_activei16((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
// unsigned int16 store2.
static inline uniform int packed_store_active2(uniform unsigned int16 a[], unsigned int16 vals) {
return __packed_store_active2i16((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
/* unsigned int8 implementations. */
// unsigned int8 load.
static inline uniform int packed_load_active(uniform unsigned int8 a[], varying unsigned int8 *uniform vals) {
return __packed_load_activei8((opaque_ptr_t)a, (opaque_ptr_t)vals, (UIntMaskType)__mask);
}
// unsigned int8 store.
static inline uniform int packed_store_active(uniform unsigned int8 a[], unsigned int8 vals) {
return __packed_store_activei8((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
// unsigned int8 store2.
static inline uniform int packed_store_active2(uniform unsigned int8 a[], unsigned int8 vals) {
return __packed_store_active2i8((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
/* unsigned int64 implementations. */
// unsigned int64 load.
static inline uniform int packed_load_active(uniform unsigned int64 a[], varying unsigned int64 *uniform vals) {
return __packed_load_activei64((opaque_ptr_t)a, (opaque_ptr_t)vals, (UIntMaskType)__mask);
}
// unsigned int64 store.
static inline uniform int packed_store_active(uniform unsigned int64 a[], unsigned int64 vals) {
return __packed_store_activei64((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
// unsigned int64 store2.
static inline uniform int packed_store_active2(uniform unsigned int64 a[], unsigned int64 vals) {
return __packed_store_active2i64((opaque_ptr_t)a, vals, (UIntMaskType)__mask);
}
/* int32 implementations. */
// int32 load.
static inline uniform int packed_load_active(uniform int a[], varying int *uniform vals) {
return __packed_load_activei32((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// int32 store.
static inline uniform int packed_store_active(uniform int a[], int vals) {
return __packed_store_activei32((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int32 store2.
static inline uniform int packed_store_active2(uniform int a[], int vals) {
return __packed_store_active2i32((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int32 store with lanes.
static inline uniform int packed_store_active(bool active, uniform int a[], int vals) {
return __packed_store_activei32((opaque_ptr_t)a, vals, (IntMaskType)(-(int)active));
}
/* int16 implementations. */
// int16 load.
static inline uniform int packed_load_active(uniform int16 a[], varying int16 *uniform vals) {
return __packed_load_activei16((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// int16 store.
static inline uniform int packed_store_active(uniform int16 a[], int16 vals) {
return __packed_store_activei16((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int16 store2.
static inline uniform int packed_store_active2(uniform int16 a[], int16 vals) {
return __packed_store_active2i16((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
/* int8 implementations. */
// int8 load.
static inline uniform int packed_load_active(uniform int8 a[], varying int8 *uniform vals) {
return __packed_load_activei8((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// int8 store.
static inline uniform int packed_store_active(uniform int8 a[], int8 vals) {
return __packed_store_activei8((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int8 store2.
static inline uniform int packed_store_active2(uniform int8 a[], int8 vals) {
return __packed_store_active2i8((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
/* int64 implementations. */
// int64 load.
static inline uniform int packed_load_active(uniform int64 a[], varying int64 *uniform vals) {
return __packed_load_activei64((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// int64 store.
static inline uniform int packed_store_active(uniform int64 a[], int64 vals) {
return __packed_store_activei64((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int64 store2.
static inline uniform int packed_store_active2(uniform int64 a[], int64 vals) {
return __packed_store_active2i64((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// int64 store with lanes.
static inline uniform int packed_store_active(bool active, uniform int64 a[], int64 vals) {
return __packed_store_activei64((opaque_ptr_t)a, vals, (IntMaskType)(-(int)active));
}
/* float16 implementations. */
// float16 load.
static inline uniform int packed_load_active(uniform float16 a[], varying float16 *uniform vals) {
return __packed_load_activef16((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// float16 store.
static inline uniform int packed_store_active(uniform float16 a[], float16 vals) {
return __packed_store_activef16((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// float16 store2.
static inline uniform int packed_store_active2(uniform float16 a[], float16 vals) {
return __packed_store_active2f16((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
/* float implementations. */
// float load.
static inline uniform int packed_load_active(uniform float a[], varying float *uniform vals) {
return __packed_load_activef32((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// float store.
static inline uniform int packed_store_active(uniform float a[], float vals) {
return __packed_store_activef32((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// float store2.
static inline uniform int packed_store_active2(uniform float a[], float vals) {
return __packed_store_active2f32((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
/* double implementations. */
// double load.
static inline uniform int packed_load_active(uniform double a[], varying double *uniform vals) {
return __packed_load_activef64((opaque_ptr_t)a, (opaque_ptr_t)vals, (IntMaskType)__mask);
}
// double store.
static inline uniform int packed_store_active(uniform double a[], double vals) {
return __packed_store_activef64((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
// double store2.
static inline uniform int packed_store_active2(uniform double a[], double vals) {
return __packed_store_active2f64((opaque_ptr_t)a, vals, (IntMaskType)__mask);
}
///////////////////////////////////////////////////////////////////////////
// streaming store
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int8 a[], unsigned int8 vals) {
__streaming_store_varying_i8((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int8 a[], int8 vals) {
__streaming_store_varying_i8((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int16 a[], unsigned int16 vals) {
__streaming_store_varying_i16((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int16 a[], int16 vals) {
__streaming_store_varying_i16((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform float16 a[], float16 vals) {
__streaming_store_varying_half((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int a[], unsigned int vals) {
__streaming_store_varying_i32((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int a[], int vals) {
__streaming_store_varying_i32((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int64 a[], unsigned int64 vals) {
__streaming_store_varying_i64((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int64 a[], int64 vals) {
__streaming_store_varying_i64((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform float a[], float vals) {
__streaming_store_varying_float((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform double a[], double vals) {
__streaming_store_varying_double((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int8 a[], uniform unsigned int8 vals) {
__streaming_store_uniform_i8((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int8 a[], uniform int8 vals) {
__streaming_store_uniform_i8((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int16 a[], uniform unsigned int16 vals) {
__streaming_store_uniform_i16((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int16 a[], uniform int16 vals) {
__streaming_store_uniform_i16((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform float16 a[], uniform float16 vals) {
__streaming_store_uniform_half((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int a[], uniform unsigned int vals) {
__streaming_store_uniform_i32((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int a[], uniform int vals) {
__streaming_store_uniform_i32((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform unsigned int64 a[], uniform unsigned int64 vals) {
__streaming_store_uniform_i64((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform int64 a[], uniform int64 vals) {
__streaming_store_uniform_i64((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform float a[], uniform float vals) {
__streaming_store_uniform_float((opaque_ptr_t)a, vals);
}
__declspec(safe, cost1) static inline void streaming_store(uniform double a[], uniform double vals) {
__streaming_store_uniform_double((opaque_ptr_t)a, vals);
}
///////////////////////////////////////////////////////////////////////////
// streaming load
__declspec(safe, cost1) static inline varying unsigned int8 streaming_load(uniform unsigned int8 a[]) {
return (unsigned int8)__streaming_load_varying_i8((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying int8 streaming_load(uniform int8 a[]) {
return __streaming_load_varying_i8((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform unsigned int8 streaming_load_uniform(uniform unsigned int8 a[]) {
return (unsigned int8)__streaming_load_uniform_i8((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform int8 streaming_load_uniform(uniform int8 a[]) {
return __streaming_load_uniform_i8((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying unsigned int16 streaming_load(uniform unsigned int16 a[]) {
return (unsigned int16)__streaming_load_varying_i16((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying int16 streaming_load(uniform int16 a[]) {
return __streaming_load_varying_i16((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform unsigned int16 streaming_load_uniform(uniform unsigned int16 a[]) {
return (unsigned int16)__streaming_load_uniform_i16((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform int16 streaming_load_uniform(uniform int16 a[]) {
return __streaming_load_uniform_i16((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying float16 streaming_load(uniform float16 a[]) {
return __streaming_load_varying_half((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform float16 streaming_load_uniform(uniform float16 a[]) {
return __streaming_load_uniform_half((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying unsigned int streaming_load(uniform unsigned int a[]) {
return (unsigned int)__streaming_load_varying_i32((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying int streaming_load(uniform int a[]) {
return __streaming_load_varying_i32((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform unsigned int streaming_load_uniform(uniform unsigned int a[]) {
return (unsigned int)__streaming_load_uniform_i32((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform int streaming_load_uniform(uniform int a[]) {
return __streaming_load_uniform_i32((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying unsigned int64 streaming_load(uniform unsigned int64 a[]) {
return (unsigned int64)__streaming_load_varying_i64((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying int64 streaming_load(uniform int64 a[]) {
return __streaming_load_varying_i64((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform unsigned int64 streaming_load_uniform(uniform unsigned int64 a[]) {
return (unsigned int64)__streaming_load_uniform_i64((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform int64 streaming_load_uniform(uniform int64 a[]) {
return __streaming_load_uniform_i64((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying float streaming_load(uniform float a[]) {
return __streaming_load_varying_float((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform float streaming_load_uniform(uniform float a[]) {
return __streaming_load_uniform_float((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline varying double streaming_load(uniform double a[]) {
return __streaming_load_varying_double((opaque_ptr_t)a);
}
__declspec(safe, cost1) static inline uniform double streaming_load_uniform(uniform double a[]) {
return __streaming_load_uniform_double((opaque_ptr_t)a);
}
///////////////////////////////////////////////////////////////////////////
// System information
static inline uniform int num_cores() { return __num_cores(); }
__declspec(safe) static inline uniform int64 clock() { return __clock(); }
///////////////////////////////////////////////////////////////////////////
// Floating-Point Math
__declspec(safe, cost1) static inline uniform bool isnan(uniform float16 v) { return v != v; }
__declspec(safe, cost1) static inline bool isnan(float16 v) { return v != v; }
__declspec(safe, cost1) static inline uniform bool isnan(uniform float v) { return v != v; }
__declspec(safe, cost1) static inline bool isnan(float v) { return v != v; }
__declspec(safe, cost1) static inline uniform bool isnan(uniform double v) { return v != v; }
__declspec(safe, cost1) static inline bool isnan(double v) { return v != v; }
__declspec(safe, cost1) static inline uniform bool isinf(uniform float16 v) { return intbits(abs(v)) == 0x7C00; }
__declspec(safe, cost1) static inline bool isinf(float16 v) { return intbits(abs(v)) == 0x7C00; }
__declspec(safe, cost1) static inline uniform bool isinf(uniform float v) { return intbits(abs(v)) == 0x7F800000; }
__declspec(safe, cost1) static inline bool isinf(float v) { return intbits(abs(v)) == 0x7F800000; }
__declspec(safe, cost1) static inline uniform bool isinf(uniform double v) {
return intbits(abs(v)) == 0x7FF0000000000000;
}
__declspec(safe, cost1) static inline bool isinf(double v) { return intbits(abs(v)) == 0x7FF0000000000000; }
__declspec(safe, cost1) static inline uniform bool isfinite(uniform float16 v) {
return (intbits(v) & 0x7C00) != 0x7C00;
}
__declspec(safe, cost1) static inline bool isfinite(float16 v) { return (intbits(v) & 0x7C00) != 0x7C00; }
__declspec(safe, cost1) static inline uniform bool isfinite(uniform float v) {
return (intbits(v) & 0x7F800000) != 0x7F800000;
}
__declspec(safe, cost1) static inline bool isfinite(float v) { return (intbits(v) & 0x7F800000) != 0x7F800000; }
__declspec(safe, cost1) static inline uniform bool isfinite(uniform double v) {
return (intbits(v) & 0x7FF0000000000000) != 0x7FF0000000000000;
}
__declspec(safe, cost1) static inline bool isfinite(double v) {
return (intbits(v) & 0x7FF0000000000000) != 0x7FF0000000000000;
}
__declspec(safe, cost1) static inline int8 abs(int8 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline uniform int8 abs(uniform int8 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline int16 abs(int16 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline uniform int16 abs(uniform int16 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline int abs(int a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline uniform int abs(uniform int a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline int64 abs(int64 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline uniform int64 abs(uniform int64 a) { return a > 0 ? a : -a; }
__declspec(safe, cost1) static inline float16 abs(float16 a) {
// Floating-point hack: zeroing the high bit clears the sign
unsigned int16 i = intbits(a);
i &= 0x7fff;
return float16bits(i);
}
__declspec(safe, cost1) static inline uniform float16 abs(uniform float16 a) {
uniform unsigned int16 i = intbits(a);
i &= 0x7fff;
return float16bits(i);
}
__declspec(safe, cost1) static inline float abs(float a) {
// Floating-point hack: zeroing the high bit clears the sign
unsigned int i = intbits(a);
i &= 0x7fffffff;
return floatbits(i);
}
__declspec(safe, cost1) static inline uniform float abs(uniform float a) {
uniform unsigned int i = intbits(a);
i &= 0x7fffffff;
return floatbits(i);
}
__declspec(safe, cost1) static inline double abs(double a) {
// zeroing the high bit clears the sign
unsigned int64 i = intbits(a);
i &= 0x7fffffffffffffff;
return doublebits(i);
}
__declspec(safe, cost1) static inline uniform double abs(uniform double a) {
uniform unsigned int64 i = intbits(a);
i &= 0x7fffffffffffffff;
return doublebits(i);
}
__declspec(safe, cost1) static inline unsigned int16 signbits(float16 x) {
unsigned int16 i = intbits(x);
return (i & 0x8000);
}
__declspec(safe, cost1) static inline uniform unsigned int16 signbits(uniform float16 x) {
uniform unsigned int16 i = intbits(x);
return (i & 0x8000);
}
__declspec(safe, cost1) static inline unsigned int signbits(float x) {
unsigned int i = intbits(x);
return (i & 0x80000000);
}
__declspec(safe, cost1) static inline uniform unsigned int signbits(uniform float x) {
uniform unsigned int i = intbits(x);
return (i & 0x80000000);
}
__declspec(safe, cost1) static inline unsigned int64 signbits(double x) {
unsigned int64 i = intbits(x);
return (i & 0x8000000000000000);
}
__declspec(safe, cost1) static inline uniform unsigned int64 signbits(uniform double x) {
uniform unsigned int64 i = intbits(x);
return (i & 0x8000000000000000);
}
__declspec(safe, cost2) static inline float16 round(float16 x) { return __round_varying_half(x); }
__declspec(safe, cost2) static inline uniform float16 round(uniform float16 x) { return __round_uniform_half(x); }
__declspec(safe, cost2) static inline float round(float x) { return __round_varying_float(x); }
__declspec(safe, cost2) static inline uniform float round(uniform float x) { return __round_uniform_float(x); }
__declspec(safe, cost2) static inline double round(double x) { return __round_varying_double(x); }
__declspec(safe, cost2) static inline uniform double round(uniform double x) { return __round_uniform_double(x); }
__declspec(safe, cost2) static inline float16 floor(float16 x) { return __floor_varying_half(x); }
__declspec(safe, cost2) static inline uniform float16 floor(uniform float16 x) { return __floor_uniform_half(x); }
__declspec(safe, cost2) static inline float floor(float x) { return __floor_varying_float(x); }
__declspec(safe, cost2) static inline uniform float floor(uniform float x) { return __floor_uniform_float(x); }
__declspec(safe, cost2) static inline double floor(double x) { return __floor_varying_double(x); }
__declspec(safe, cost2) static inline uniform double floor(uniform double x) { return __floor_uniform_double(x); }
__declspec(safe, cost2) static inline float16 ceil(float16 x) { return __ceil_varying_half(x); }
__declspec(safe, cost2) static inline uniform float16 ceil(uniform float16 x) { return __ceil_uniform_half(x); }
__declspec(safe, cost2) static inline float ceil(float x) { return __ceil_varying_float(x); }
__declspec(safe, cost2) static inline uniform float ceil(uniform float x) { return __ceil_uniform_float(x); }
__declspec(safe, cost2) static inline double ceil(double x) { return __ceil_varying_double(x); }
__declspec(safe, cost2) static inline uniform double ceil(uniform double x) { return __ceil_uniform_double(x); }
///////////////////////////
__declspec(safe, cost2) static inline float16 trunc(float16 x) { return __trunc_varying_half(x); }
__declspec(safe, cost2) static inline uniform float16 trunc(uniform float16 x) { return __trunc_uniform_half(x); }
__declspec(safe, cost2) static inline float trunc(float x) { return __trunc_varying_float(x); }
__declspec(safe, cost2) static inline uniform float trunc(uniform float x) { return __trunc_uniform_float(x); }
__declspec(safe, cost2) static inline double trunc(double x) { return __trunc_varying_double(x); }
__declspec(safe, cost2) static inline uniform double trunc(uniform double x) { return __trunc_uniform_double(x); }
__declspec(safe) static inline float rcp(float v) { return __rcp_varying_float(v); }
__declspec(safe) static inline uniform float rcp(uniform float v) { return __rcp_uniform_float(v); }
__declspec(safe) static inline float rcp_fast(float v) { return __rcp_fast_varying_float(v); }
__declspec(safe) static inline uniform float rcp_fast(uniform float v) { return __rcp_fast_uniform_float(v); }
#define RCPD(QUAL) \
__declspec(safe) static inline QUAL double __rcp_iterate_##QUAL##_double(QUAL double v, QUAL double iv) { \
iv = iv * (2.0d - v * iv); \
iv = iv * (2.0d - v * iv); \
return iv; \
} \
__declspec(safe) static inline QUAL double __rcp_safe_##QUAL##_double(QUAL double x) { \
if (x <= 1.0e+33d && x >= 1.0e-33d) \
return __rcp_iterate_##QUAL##_double(x, rcp((QUAL float)x)); \
QUAL int64 ex = intbits(x) & 0x7fe0000000000000; \
QUAL double exp = doublebits(0x7fd0000000000000 + ~ex); \
QUAL double y = rcp((QUAL float)(x * exp)); \
return __rcp_iterate_##QUAL##_double(x, y * exp); \
}
RCPD(varying)
__declspec(safe) static inline double rcp(double v) {
if (__have_native_rcpd)
return __rcp_varying_double(v);
else
return __rcp_safe_varying_double(v);
}
RCPD(uniform)
__declspec(safe) static inline uniform double rcp(uniform double v) {
if (__have_native_rcpd)
return __rcp_uniform_double(v);
else
return __rcp_safe_uniform_double(v);
}
__declspec(safe) static inline double rcp_fast(double v) {
if (__have_native_rcpd) {
return __rcp_fast_varying_double(v);
} else {
return __rcp_safe_varying_double(v);
}
}
__declspec(safe) static inline uniform double rcp_fast(uniform double v) {
if (__have_native_rcpd) {
return __rcp_fast_uniform_double(v);
} else {
return __rcp_safe_uniform_double(v);
}
}
__declspec(safe) static inline float16 rcp(float16 v) {
if (__have_native_half_full_support) {
return __rcp_varying_half(v);
} else {
return (float16)(rcp((float)v));
}
}
__declspec(safe) static inline uniform float16 rcp(uniform float16 v) {
if (__have_native_half_full_support) {
return __rcp_uniform_half(v);
} else {
return (uniform float16)(rcp((uniform float)v));
}
}
__declspec(safe, cost2) static inline float16 fmod(float16 x, float16 y) {
// Handle special cases: NaN inputs or y == 0.0f16
if (isnan(x) || isnan(y) || y == 0.0f16) {
// Return NaN with the sign of x
uniform int16 nan_bits = 0x7E00; // Quiet NaN for float16
if (signbits(x)) {
nan_bits |= 0x8000; // Set sign bit if x is negative
}
return float16bits(nan_bits);
}
// Handle cases where y is infinite
int16 y_abs_bits = intbits(y) & (int16)0x7FFF; // Absolute value of y bits
if (y_abs_bits == 0x7C00) { // y is infinite
return x;
}
// Handle cases where x is infinite
int16 x_abs_bits = intbits(x) & (int16)0x7FFF; // Absolute value of x bits
if (x_abs_bits == 0x7C00) { // x is infinite
// Return NaN with the sign of x
uniform int16 nan_bits = 0x7E00; // Quiet NaN for float16
if (signbits(x)) {
nan_bits |= 0x8000; // Set sign bit if x is negative
}
return float16bits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0f16) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
__declspec(safe, cost2) static inline uniform float16 fmod(uniform float16 x, uniform float16 y) {
// Handle special cases: NaN inputs or y == 0.0f16
if (isnan(x) || isnan(y) || y == 0.0f16) {
// Return NaN with the sign of x
uniform int16 nan_bits = 0x7E00; // Quiet NaN for float16
if (signbits(x)) {
nan_bits |= 0x8000; // Set sign bit if x is negative
}
return float16bits(nan_bits);
}
// Handle cases where y is infinite
uniform int16 y_abs_bits = intbits(y) & (uniform int16)0x7FFF; // Absolute value of y bits
if (y_abs_bits == 0x7C00) { // y is infinite
return x;
}
// Handle cases where x is infinite
uniform int16 x_abs_bits = intbits(x) & (uniform int16)0x7FFF; // Absolute value of x bits
if (x_abs_bits == 0x7C00) { // x is infinite
// Return NaN with the sign of x
uniform int16 nan_bits = 0x7E00; // Quiet NaN for float16
if (signbits(x)) {
nan_bits |= 0x8000; // Set sign bit if x is negative
}
return float16bits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0f16) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
__declspec(safe, cost2) static inline float fmod(float x, float y) {
// Handle special cases: NaN inputs or y == 0.0f
if (isnan(x) || isnan(y) || y == 0.0f) {
// Return NaN with the sign of x
uniform int nan_bits = 0x7FC00000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x80000000; // Set sign bit if x is negative
}
return floatbits(nan_bits);
}
// Handle cases where y is infinite
int y_abs_bits = intbits(y) & (int)0x7FFFFFFF; // Absolute value of y bits
if (y_abs_bits == 0x7F800000) { // y is infinite
return x;
}
// Handle cases where x is infinite
int x_abs_bits = intbits(x) & (int)0x7FFFFFFF; // Absolute value of x bits
if (x_abs_bits == 0x7F800000) { // x is infinite
// Return NaN with the sign of x
uniform int nan_bits = 0x7FC00000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x80000000; // Set sign bit if x is negative
}
return floatbits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0f) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
__declspec(safe, cost2) static inline uniform float fmod(uniform float x, uniform float y) {
// Handle special cases: NaN inputs or y == 0.0f
if (isnan(x) || isnan(y) || y == 0.0f) {
// Return NaN with the sign of x
uniform int nan_bits = 0x7FC00000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x80000000; // Set sign bit if x is negative
}
return floatbits(nan_bits);
}
// Handle cases where y is infinite
uniform int y_abs_bits = intbits(y) & (uniform int)0x7FFFFFFF; // Absolute value of y bits
if (y_abs_bits == 0x7F800000) { // y is infinite
return x;
}
// Handle cases where x is infinite
uniform int x_abs_bits = intbits(x) & (uniform int)0x7FFFFFFF; // Absolute value of x bits
if (x_abs_bits == 0x7F800000) { // x is infinite
// Return NaN with the sign of x
uniform int nan_bits = 0x7FC00000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x80000000; // Set sign bit if x is negative
}
return floatbits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0f) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
__declspec(safe, cost2) static inline double fmod(double x, double y) {
// Handle special cases: NaN inputs or y == 0.0
if (isnan(x) || isnan(y) || y == 0.0d) {
// Return NaN with the sign of x
uniform int64 nan_bits = 0x7FF8000000000000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x8000000000000000; // Set sign bit if x is negative
}
return doublebits(nan_bits);
}
// Handle cases where y is infinite
int64 y_abs_bits = intbits(y) & (int64)0x7FFFFFFFFFFFFFFF; // Absolute value of y bits
if (y_abs_bits == 0x7FF0000000000000) { // y is infinite
return x;
}
// Handle cases where x is infinite
int64 x_abs_bits = intbits(x) & (int64)0x7FFFFFFFFFFFFFFF; // Absolute value of x bits
if (x_abs_bits == 0x7FF0000000000000) { // x is infinite
// Return NaN with the sign of x
uniform int64 nan_bits = 0x7FF8000000000000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x8000000000000000; // Set sign bit if x is negative
}
return doublebits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0d) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
__declspec(safe, cost2) static inline uniform double fmod(uniform double x, uniform double y) {
// Handle special cases: NaN inputs or y == 0.0
if (isnan(x) || isnan(y) || y == 0.0d) {
// Return NaN with the sign of x
uniform int64 nan_bits = 0x7FF8000000000000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x8000000000000000; // Set sign bit if x is negative
}
return doublebits(nan_bits);
}
// Handle cases where y is infinite
uniform int64 y_abs_bits = intbits(y) & (uniform int64)0x7FFFFFFFFFFFFFFF; // Absolute value of y bits
if (y_abs_bits == 0x7FF0000000000000) { // y is infinite
return x;
}
// Handle cases where x is infinite
uniform int64 x_abs_bits = intbits(x) & (uniform int64)0x7FFFFFFFFFFFFFFF; // Absolute value of x bits
if (x_abs_bits == 0x7FF0000000000000) { // x is infinite
// Return NaN with the sign of x
uniform int64 nan_bits = 0x7FF8000000000000; // Quiet NaN
if (signbits(x)) {
nan_bits |= 0x8000000000000000; // Set sign bit if x is negative
}
return doublebits(nan_bits);
}
// Handle cases where x is zero
if (x == 0.0d) {
return x;
}
// Compute the floating-point remainder
return x - trunc(x * rcp(y)) * y;
}
///////////////////////////////////////////////////////////////////////////
// min/max
// float16
__declspec(safe, cost1) static inline float16 min(float16 a, float16 b) { return __min_varying_half(a, b); }
__declspec(safe, cost1) static inline uniform float16 min(uniform float16 a, uniform float16 b) {
return __min_uniform_half(a, b);
}
__declspec(safe, cost1) static inline float16 max(float16 a, float16 b) { return __max_varying_half(a, b); }
__declspec(safe, cost1) static inline uniform float16 max(uniform float16 a, uniform float16 b) {
return __max_uniform_half(a, b);
}
// float
__declspec(safe, cost1) static inline float min(float a, float b) { return __min_varying_float(a, b); }
__declspec(safe, cost1) static inline uniform float min(uniform float a, uniform float b) {
return __min_uniform_float(a, b);
}
__declspec(safe, cost1) static inline float max(float a, float b) { return __max_varying_float(a, b); }
__declspec(safe, cost1) static inline uniform float max(uniform float a, uniform float b) {
return __max_uniform_float(a, b);
}
// double
__declspec(safe) static inline double min(double a, double b) { return __min_varying_double(a, b); }
__declspec(safe) static inline uniform double min(uniform double a, uniform double b) {
return __min_uniform_double(a, b);
}
__declspec(safe) static inline double max(double a, double b) { return __max_varying_double(a, b); }
__declspec(safe) static inline uniform double max(uniform double a, uniform double b) {
return __max_uniform_double(a, b);
}
// int8
__declspec(safe, cost1) static inline uniform unsigned int8 min(uniform unsigned int8 a, uniform unsigned int8 b) {
return __min_uniform_uint8(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int8 max(uniform unsigned int8 a, uniform unsigned int8 b) {
return __max_uniform_uint8(a, b);
}
__declspec(safe, cost1) static inline uniform int8 min(uniform int8 a, uniform int8 b) {
return __min_uniform_int8(a, b);
}
__declspec(safe, cost1) static inline uniform int8 max(uniform int8 a, uniform int8 b) {
return __max_uniform_int8(a, b);
}
__declspec(safe, cost1) static inline unsigned int8 min(unsigned int8 a, unsigned int8 b) {
return __min_varying_uint8(a, b);
}
__declspec(safe, cost1) static inline unsigned int8 max(unsigned int8 a, unsigned int8 b) {
return __max_varying_uint8(a, b);
}
__declspec(safe, cost1) static inline int8 min(int8 a, int8 b) { return __min_varying_int8(a, b); }
__declspec(safe, cost1) static inline int8 max(int8 a, int8 b) { return __max_varying_int8(a, b); }
// int16
__declspec(safe, cost1) static inline uniform unsigned int16 min(uniform unsigned int16 a, uniform unsigned int16 b) {
return __min_uniform_uint16(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int16 max(uniform unsigned int16 a, uniform unsigned int16 b) {
return __max_uniform_uint16(a, b);
}
__declspec(safe, cost1) static inline uniform int16 min(uniform int16 a, uniform int16 b) {
return __min_uniform_int16(a, b);
}
__declspec(safe, cost1) static inline uniform int16 max(uniform int16 a, uniform int16 b) {
return __max_uniform_int16(a, b);
}
__declspec(safe, cost1) static inline unsigned int16 min(unsigned int16 a, unsigned int16 b) {
return __min_varying_uint16(a, b);
}
__declspec(safe, cost1) static inline unsigned int16 max(unsigned int16 a, unsigned int16 b) {
return __max_varying_uint16(a, b);
}
__declspec(safe, cost1) static inline int16 min(int16 a, int16 b) { return __min_varying_int16(a, b); }
__declspec(safe, cost1) static inline int16 max(int16 a, int16 b) { return __max_varying_int16(a, b); }
// int32
__declspec(safe, cost1) static inline unsigned int min(unsigned int a, unsigned int b) {
return __min_varying_uint32(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int min(uniform unsigned int a, uniform unsigned int b) {
return __min_uniform_uint32(a, b);
}
__declspec(safe, cost1) static inline unsigned int max(unsigned int a, unsigned int b) {
return __max_varying_uint32(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int max(uniform unsigned int a, uniform unsigned int b) {
return __max_uniform_uint32(a, b);
}
__declspec(safe, cost1) static inline int min(int a, int b) { return __min_varying_int32(a, b); }
__declspec(safe, cost1) static inline uniform int min(uniform int a, uniform int b) {
return __min_uniform_int32(a, b);
}
__declspec(safe, cost1) static inline int max(int a, int b) { return __max_varying_int32(a, b); }
__declspec(safe, cost1) static inline uniform int max(uniform int a, uniform int b) {
return __max_uniform_int32(a, b);
}
// int64
__declspec(safe, cost1) static inline unsigned int64 min(unsigned int64 a, unsigned int64 b) {
return __min_varying_uint64(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int64 min(uniform unsigned int64 a, uniform unsigned int64 b) {
return __min_uniform_uint64(a, b);
}
__declspec(safe, cost1) static inline unsigned int64 max(unsigned int64 a, unsigned int64 b) {
return __max_varying_uint64(a, b);
}
__declspec(safe, cost1) static inline uniform unsigned int64 max(uniform unsigned int64 a, uniform unsigned int64 b) {
return __max_uniform_uint64(a, b);
}
__declspec(safe, cost1) static inline int64 min(int64 a, int64 b) { return __min_varying_int64(a, b); }
__declspec(safe, cost1) static inline uniform int64 min(uniform int64 a, uniform int64 b) {
return __min_uniform_int64(a, b);
}
__declspec(safe, cost1) static inline int64 max(int64 a, int64 b) { return __max_varying_int64(a, b); }
__declspec(safe, cost1) static inline uniform int64 max(uniform int64 a, uniform int64 b) {
return __max_uniform_int64(a, b);
}
///////////////////////////////////////////////////////////////////////////
// clamps
// float16
__declspec(safe, cost2) static inline float16 clamp(float16 v, float16 low, float16 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline uniform float16 clamp(uniform float16 v, uniform float16 low,
uniform float16 high) {
return min(max(v, low), high);
}
// float
__declspec(safe, cost2) static inline float clamp(float v, float low, float high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform float clamp(uniform float v, uniform float low, uniform float high) {
return min(max(v, low), high);
}
// double
__declspec(safe, cost2) static inline double clamp(double v, double low, double high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform double clamp(uniform double v, uniform double low, uniform double high) {
return min(max(v, low), high);
}
// int8
__declspec(safe, cost2) static inline unsigned int8 clamp(unsigned int8 v, unsigned int8 low, unsigned int8 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline uniform unsigned int8 clamp(uniform unsigned int8 v, uniform unsigned int8 low,
uniform unsigned int8 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline int8 clamp(int8 v, int8 low, int8 high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform int8 clamp(uniform int8 v, uniform int8 low, uniform int8 high) {
return min(max(v, low), high);
}
// int16
__declspec(safe, cost2) static inline unsigned int16 clamp(unsigned int16 v, unsigned int16 low, unsigned int16 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline uniform unsigned int16 clamp(uniform unsigned int16 v, uniform unsigned int16 low,
uniform unsigned int16 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline int16 clamp(int16 v, int16 low, int16 high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform int16 clamp(uniform int16 v, uniform int16 low, uniform int16 high) {
return min(max(v, low), high);
}
// int32
__declspec(safe, cost2) static inline unsigned int clamp(unsigned int v, unsigned int low, unsigned int high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline uniform unsigned int clamp(uniform unsigned int v, uniform unsigned int low,
uniform unsigned int high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline int clamp(int v, int low, int high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform int clamp(uniform int v, uniform int low, uniform int high) {
return min(max(v, low), high);
}
// int64
__declspec(safe, cost2) static inline unsigned int64 clamp(unsigned int64 v, unsigned int64 low, unsigned int64 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline uniform unsigned int64 clamp(uniform unsigned int64 v, uniform unsigned int64 low,
uniform unsigned int64 high) {
return min(max(v, low), high);
}
__declspec(safe, cost2) static inline int64 clamp(int64 v, int64 low, int64 high) { return min(max(v, low), high); }
__declspec(safe, cost2) static inline uniform int64 clamp(uniform int64 v, uniform int64 low, uniform int64 high) {
return min(max(v, low), high);
}
///////////////////////////////////////////////////////////////////////////
// Global atomics and memory barriers
static inline void memory_barrier() { __memory_barrier(); }
#define DEFINE_ATOMIC_OP(TA, TB, OPA, OPB, MASKTYPE, TC) \
static inline TA atomic_##OPA##_global(uniform TA *uniform ptr, TA value) { \
TA ret = __atomic_##OPB##_##TB##_global((opaque_ptr_t)ptr, value, (MASKTYPE)__mask); \
return ret; \
} \
static inline uniform TA atomic_##OPA##_global(uniform TA *uniform ptr, uniform TA value) { \
uniform TA ret = __atomic_##OPB##_uniform_##TB##_global((opaque_ptr_t)ptr, value); \
return ret; \
} \
static inline TA atomic_##OPA##_global(uniform TA *varying ptr, TA value) { \
uniform TA *uniform ptrArray[programCount]; \
ptrArray[programIndex] = ptr; \
TA ret; \
foreach_active(i) { \
uniform int8 *uniform p = (opaque_ptr_t)ptrArray[i]; \
uniform TA v = extract(value, i); \
uniform TA r = __atomic_##OPB##_uniform_##TB##_global(p, v); \
ret = insert(ret, i, r); \
} \
return ret; \
}
#define DEFINE_ATOMIC_SWAP(TA, TB, MASKTYPE, TC) \
static inline TA atomic_swap_global(uniform TA *uniform ptr, TA value) { \
uniform int i = 0; \
TA ret[programCount]; \
TA memVal; \
uniform int lastSwap; \
uniform unsigned int64 mask = lanemask(); \
/* First, have the first running program instance (if any) perform \
the swap with memory with its value of "value"; record the \
value returned. */ \
for (; i < programCount; ++i) { \
if ((mask & (1ull << i)) == 0) \
continue; \
memVal = __atomic_swap_uniform_##TB##_global((opaque_ptr_t)ptr, extract(value, i)); \
lastSwap = i; \
break; \
} \
/* Now, for all of the remaining running program instances, set the \
return value of the last instance that did a swap with this \
instance's value of "value"; this gives the same effect as if the \
current instance had executed a hardware atomic swap right before \
the last one that did a swap. */ \
for (; i < programCount; ++i) { \
if ((mask & (1ull << i)) == 0) \
continue; \
ret[lastSwap] = extract(value, i); \
lastSwap = i; \
} \
/* And the last instance that wanted to swap gets the value we \
originally got back from memory... */ \
ret[lastSwap] = memVal; \
return ret[programIndex]; \
} \
static inline uniform TA atomic_swap_global(uniform TA *uniform ptr, uniform TA value) { \
uniform TA ret = __atomic_swap_uniform_##TB##_global((opaque_ptr_t)ptr, value); \
return ret; \
} \
static inline TA atomic_swap_global(uniform TA *varying ptr, TA value) { \
uniform TA *uniform ptrArray[programCount]; \
ptrArray[programIndex] = ptr; \
TA ret; \
foreach_active(i) { \
uniform int8 *uniform p = (opaque_ptr_t)ptrArray[i]; \
uniform TA v = extract(value, i); \
uniform TA r = __atomic_swap_uniform_##TB##_global(p, v); \
ret = insert(ret, i, r); \
} \
return ret; \
}
#define DEFINE_ATOMIC_MINMAX_OP(TA, TB, OPA, OPB, MASKTYPE, TC) \
static inline TA atomic_##OPA##_global(uniform TA *uniform ptr, TA value) { \
uniform TA oneval = reduce_##OPA(value); \
TA ret; \
if (lanemask() != 0) \
ret = __atomic_##OPB##_uniform_##TB##_global((opaque_ptr_t)ptr, oneval); \
return ret; \
} \
static inline uniform TA atomic_##OPA##_global(uniform TA *uniform ptr, uniform TA value) { \
uniform TA ret = __atomic_##OPB##_uniform_##TB##_global((opaque_ptr_t)ptr, value); \
return ret; \
} \
static inline TA atomic_##OPA##_global(uniform TA *varying ptr, TA value) { \
uniform TA *uniform ptrArray[programCount]; \
ptrArray[programIndex] = ptr; \
TA ret; \
foreach_active(i) { \
uniform int8 *uniform p = (opaque_ptr_t)ptrArray[i]; \
uniform TA v = extract(value, i); \
uniform TA r = __atomic_##OPB##_uniform_##TB##_global(p, v); \
ret = insert(ret, i, r); \
} \
return ret; \
}
DEFINE_ATOMIC_OP(int32, int32, add, add, IntMaskType, int64)
DEFINE_ATOMIC_OP(int32, int32, subtract, sub, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(int32, int32, min, min, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(int32, int32, max, max, IntMaskType, int64)
DEFINE_ATOMIC_OP(int32, int32, and, and, IntMaskType, int64)
DEFINE_ATOMIC_OP(int32, int32, or, or, IntMaskType, int64)
DEFINE_ATOMIC_OP(int32, int32, xor, xor, IntMaskType, int64)
DEFINE_ATOMIC_SWAP(int32, int32, IntMaskType, int64)
// For everything but atomic min and max, we can use the same
// implementations for unsigned as for signed.
DEFINE_ATOMIC_OP(unsigned int32, int32, add, add, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int32, int32, subtract, sub, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_MINMAX_OP(unsigned int32, uint32, min, umin, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_MINMAX_OP(unsigned int32, uint32, max, umax, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int32, int32, and, and, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int32, int32, or, or, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int32, int32, xor, xor, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_SWAP(unsigned int32, int32, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(float, float, add, fadd, IntMaskType, int64)
DEFINE_ATOMIC_OP(float, float, subtract, fsub, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(float, float, min, fmin, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(float, float, max, fmax, IntMaskType, int64)
DEFINE_ATOMIC_SWAP(float, float, IntMaskType, int64)
DEFINE_ATOMIC_OP(int64, int64, add, add, IntMaskType, int64)
DEFINE_ATOMIC_OP(int64, int64, subtract, sub, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(int64, int64, min, min, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(int64, int64, max, max, IntMaskType, int64)
DEFINE_ATOMIC_OP(int64, int64, and, and, IntMaskType, int64)
DEFINE_ATOMIC_OP(int64, int64, or, or, IntMaskType, int64)
DEFINE_ATOMIC_OP(int64, int64, xor, xor, IntMaskType, int64)
DEFINE_ATOMIC_SWAP(int64, int64, IntMaskType, int64)
// For everything but atomic min and max, we can use the same
// implementations for unsigned as for signed.
DEFINE_ATOMIC_OP(unsigned int64, int64, add, add, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int64, int64, subtract, sub, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_MINMAX_OP(unsigned int64, uint64, min, umin, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_MINMAX_OP(unsigned int64, uint64, max, umax, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int64, int64, and, and, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int64, int64, or, or, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(unsigned int64, int64, xor, xor, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_SWAP(unsigned int64, int64, UIntMaskType, unsigned int64)
DEFINE_ATOMIC_OP(double, double, add, fadd, IntMaskType, int64)
DEFINE_ATOMIC_OP(double, double, subtract, fsub, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(double, double, min, fmin, IntMaskType, int64)
DEFINE_ATOMIC_MINMAX_OP(double, double, max, fmax, IntMaskType, int64)
DEFINE_ATOMIC_SWAP(double, double, IntMaskType, int64)
#undef DEFINE_ATOMIC_OP
#undef DEFINE_ATOMIC_MINMAX_OP
#undef DEFINE_ATOMIC_SWAP
#define ATOMIC_DECL_CMPXCHG(TA, TB, MASKTYPE, TC) \
static inline uniform TA atomic_compare_exchange_global(uniform TA *uniform ptr, uniform TA oldval, \
uniform TA newval) { \
uniform TA ret = __atomic_compare_exchange_uniform_##TB##_global((opaque_ptr_t)ptr, oldval, newval); \
return ret; \
} \
static inline TA atomic_compare_exchange_global(uniform TA *uniform ptr, TA oldval, TA newval) { \
TA ret = __atomic_compare_exchange_##TB##_global((opaque_ptr_t)ptr, oldval, newval, (MASKTYPE)__mask); \
return ret; \
} \
static inline TA atomic_compare_exchange_global(uniform TA *varying ptr, TA oldval, TA newval) { \
uniform TA *uniform ptrArray[programCount]; \
ptrArray[programIndex] = ptr; \
TA ret; \
foreach_active(i) { \
uniform TA r = __atomic_compare_exchange_uniform_##TB##_global((opaque_ptr_t)ptrArray[i], \
extract(oldval, i), extract(newval, i)); \
ret = insert(ret, i, r); \
} \
return ret; \
}
ATOMIC_DECL_CMPXCHG(int32, int32, IntMaskType, int64)
ATOMIC_DECL_CMPXCHG(unsigned int32, int32, UIntMaskType, unsigned int64)
ATOMIC_DECL_CMPXCHG(float, float, IntMaskType, int64)
ATOMIC_DECL_CMPXCHG(int64, int64, IntMaskType, int64)
ATOMIC_DECL_CMPXCHG(unsigned int64, int64, UIntMaskType, unsigned int64)
ATOMIC_DECL_CMPXCHG(double, double, IntMaskType, int64)
#undef ATOMIC_DECL_CMPXCHG
// void * variants of swap and compare exchange
static inline void *atomic_swap_global(void **uniform ptr, void *value) {
return (void *)atomic_swap_global((uniform intptr_t * uniform) ptr, (intptr_t)value);
}
static inline void *uniform atomic_swap_global(void **uniform ptr, void *uniform value) {
return (void *uniform)atomic_swap_global((uniform intptr_t * uniform) ptr, (uniform intptr_t)value);
}
static inline void *atomic_swap_global(void **ptr, void *value) {
return (void *)atomic_swap_global((intptr_t *)ptr, (intptr_t)value);
}
static inline void *atomic_compare_exchange_global(void **uniform ptr, void *oldval, void *newval) {
return (void *)atomic_compare_exchange_global((uniform intptr_t * uniform) ptr, (intptr_t)oldval, (intptr_t)newval);
}
static inline void *uniform atomic_compare_exchange_global(void **uniform ptr, void *uniform oldval,
void *uniform newval) {
return (void *uniform)atomic_compare_exchange_global((uniform intptr_t * uniform) ptr, (uniform intptr_t)oldval,
(uniform intptr_t)newval);
}
static inline void *atomic_compare_exchange_global(void **ptr, void *oldval, void *newval) {
return (void *)atomic_compare_exchange_global((intptr_t *)ptr, (intptr_t)oldval, (intptr_t)newval);
}
///////////////////////////////////////////////////////////////////////////
// local atomics
#define LOCAL_ATOMIC(TYPE, NAME, OPFUNC) \
static inline uniform TYPE atomic_##NAME##_local(uniform TYPE *uniform ptr, uniform TYPE value) { \
uniform TYPE ret = *ptr; \
*ptr = OPFUNC(*ptr, value); \
return ret; \
} \
static inline TYPE atomic_##NAME##_local(uniform TYPE *uniform ptr, TYPE value) { \
TYPE ret; \
foreach_active(i) { \
ret = insert(ret, i, *ptr); \
*ptr = OPFUNC(*ptr, extract(value, i)); \
} \
return ret; \
} \
static inline TYPE atomic_##NAME##_local(uniform TYPE *p, TYPE value) { \
TYPE ret; \
uniform TYPE *uniform ptrs[programCount]; \
ptrs[programIndex] = p; \
foreach_active(i) { \
ret = insert(ret, i, *ptrs[i]); \
*ptrs[i] = OPFUNC(*ptrs[i], extract(value, i)); \
} \
return ret; \
}
static inline uniform int32 __add(uniform int32 a, uniform int32 b) { return a + b; }
static inline uniform int32 __sub(uniform int32 a, uniform int32 b) { return a - b; }
static inline uniform int32 __and(uniform int32 a, uniform int32 b) { return a & b; }
static inline uniform int32 __or(uniform int32 a, uniform int32 b) { return a | b; }
static inline uniform int32 __xor(uniform int32 a, uniform int32 b) { return a ^ b; }
static inline uniform int32 __swap(uniform int32 a, uniform int32 b) { return b; }
static inline uniform unsigned int32 __add(uniform unsigned int32 a, uniform unsigned int32 b) { return a + b; }
static inline uniform unsigned int32 __sub(uniform unsigned int32 a, uniform unsigned int32 b) { return a - b; }
static inline uniform unsigned int32 __and(uniform unsigned int32 a, uniform unsigned int32 b) { return a & b; }
static inline uniform unsigned int32 __or(uniform unsigned int32 a, uniform unsigned int32 b) { return a | b; }
static inline uniform unsigned int32 __xor(uniform unsigned int32 a, uniform unsigned int32 b) { return a ^ b; }
static inline uniform unsigned int32 __swap(uniform unsigned int32 a, uniform unsigned int32 b) { return b; }
static inline uniform float __add(uniform float a, uniform float b) { return a + b; }
static inline uniform float __sub(uniform float a, uniform float b) { return a - b; }
static inline uniform float __swap(uniform float a, uniform float b) { return b; }
static inline uniform int64 __add(uniform int64 a, uniform int64 b) { return a + b; }
static inline uniform int64 __sub(uniform int64 a, uniform int64 b) { return a - b; }
static inline uniform int64 __and(uniform int64 a, uniform int64 b) { return a & b; }
static inline uniform int64 __or(uniform int64 a, uniform int64 b) { return a | b; }
static inline uniform int64 __xor(uniform int64 a, uniform int64 b) { return a ^ b; }
static inline uniform int64 __swap(uniform int64 a, uniform int64 b) { return b; }
static inline uniform unsigned int64 __add(uniform unsigned int64 a, uniform unsigned int64 b) { return a + b; }
static inline uniform unsigned int64 __sub(uniform unsigned int64 a, uniform unsigned int64 b) { return a - b; }
static inline uniform unsigned int64 __and(uniform unsigned int64 a, uniform unsigned int64 b) { return a & b; }
static inline uniform unsigned int64 __or(uniform unsigned int64 a, uniform unsigned int64 b) { return a | b; }
static inline uniform unsigned int64 __xor(uniform unsigned int64 a, uniform unsigned int64 b) { return a ^ b; }
static inline uniform unsigned int64 __swap(uniform unsigned int64 a, uniform unsigned int64 b) { return b; }
static inline uniform double __add(uniform double a, uniform double b) { return a + b; }
static inline uniform double __sub(uniform double a, uniform double b) { return a - b; }
static inline uniform double __swap(uniform double a, uniform double b) { return a - b; }
LOCAL_ATOMIC(int32, add, __add)
LOCAL_ATOMIC(int32, subtract, __sub)
LOCAL_ATOMIC(int32, and, __and)
LOCAL_ATOMIC(int32, or, __or)
LOCAL_ATOMIC(int32, xor, __xor)
LOCAL_ATOMIC(int32, min, min)
LOCAL_ATOMIC(int32, max, max)
LOCAL_ATOMIC(int32, swap, __swap)
LOCAL_ATOMIC(unsigned int32, add, __add)
LOCAL_ATOMIC(unsigned int32, subtract, __sub)
LOCAL_ATOMIC(unsigned int32, and, __and)
LOCAL_ATOMIC(unsigned int32, or, __or)
LOCAL_ATOMIC(unsigned int32, xor, __xor)
LOCAL_ATOMIC(unsigned int32, min, min)
LOCAL_ATOMIC(unsigned int32, max, max)
LOCAL_ATOMIC(unsigned int32, swap, __swap)
LOCAL_ATOMIC(float, add, __add)
LOCAL_ATOMIC(float, subtract, __sub)
LOCAL_ATOMIC(float, min, min)
LOCAL_ATOMIC(float, max, max)
LOCAL_ATOMIC(float, swap, __swap)
LOCAL_ATOMIC(int64, add, __add)
LOCAL_ATOMIC(int64, subtract, __sub)
LOCAL_ATOMIC(int64, and, __and)
LOCAL_ATOMIC(int64, or, __or)
LOCAL_ATOMIC(int64, xor, __xor)
LOCAL_ATOMIC(int64, min, min)
LOCAL_ATOMIC(int64, max, max)
LOCAL_ATOMIC(int64, swap, __swap)
LOCAL_ATOMIC(unsigned int64, add, __add)
LOCAL_ATOMIC(unsigned int64, subtract, __sub)
LOCAL_ATOMIC(unsigned int64, and, __and)
LOCAL_ATOMIC(unsigned int64, or, __or)
LOCAL_ATOMIC(unsigned int64, xor, __xor)
LOCAL_ATOMIC(unsigned int64, min, min)
LOCAL_ATOMIC(unsigned int64, max, max)
LOCAL_ATOMIC(unsigned int64, swap, __swap)
LOCAL_ATOMIC(double, add, __add)
LOCAL_ATOMIC(double, subtract, __sub)
LOCAL_ATOMIC(double, min, min)
LOCAL_ATOMIC(double, max, max)
LOCAL_ATOMIC(double, swap, __swap)
// compare exchange
#define LOCAL_CMPXCHG(TYPE) \
static inline uniform TYPE atomic_compare_exchange_local(uniform TYPE *uniform ptr, uniform TYPE cmp, \
uniform TYPE update) { \
uniform TYPE old = *ptr; \
if (old == cmp) \
*ptr = update; \
return old; \
} \
static inline TYPE atomic_compare_exchange_local(uniform TYPE *uniform ptr, TYPE cmp, TYPE update) { \
TYPE ret; \
foreach_active(i) { \
uniform TYPE old = *ptr; \
if (old == extract(cmp, i)) \
*ptr = extract(update, i); \
ret = insert(ret, i, old); \
} \
return ret; \
} \
static inline TYPE atomic_compare_exchange_local(uniform TYPE *varying p, TYPE cmp, TYPE update) { \
uniform TYPE *uniform ptrs[programCount]; \
ptrs[programIndex] = p; \
TYPE ret; \
foreach_active(i) { \
uniform TYPE old = *ptrs[i]; \
if (old == extract(cmp, i)) \
*ptrs[i] = extract(update, i); \
ret = insert(ret, i, old); \
} \
return ret; \
}
LOCAL_CMPXCHG(int32)
LOCAL_CMPXCHG(unsigned int32)
LOCAL_CMPXCHG(float)
LOCAL_CMPXCHG(int64)
LOCAL_CMPXCHG(unsigned int64)
LOCAL_CMPXCHG(double)
#undef LOCAL_ATOMIC
#undef LOCAL_CMPXCHG
// void * variants of swap and compare exchange
static inline void *atomic_swap_local(void **uniform ptr, void *value) {
return (void *)atomic_swap_local((uniform intptr_t * uniform) ptr, (intptr_t)value);
}
static inline void *uniform atomic_swap_local(void **uniform ptr, void *uniform value) {
return (void *uniform)atomic_swap_local((uniform intptr_t * uniform) ptr, (uniform intptr_t)value);
}
static inline void *atomic_swap_local(void **ptr, void *value) {
return (void *)atomic_swap_local((intptr_t *)ptr, (intptr_t)value);
}
static inline void *atomic_compare_exchange_local(void **uniform ptr, void *oldval, void *newval) {
return (void *)atomic_compare_exchange_local((uniform intptr_t * uniform) ptr, (intptr_t)oldval, (intptr_t)newval);
}
static inline void *uniform atomic_compare_exchange_local(void **uniform ptr, void *uniform oldval,
void *uniform newval) {
return (void *uniform)atomic_compare_exchange_local((uniform intptr_t * uniform) ptr, (uniform intptr_t)oldval,
(uniform intptr_t)newval);
}
static inline void *atomic_compare_exchange_local(void **ptr, void *oldval, void *newval) {
return (void *)atomic_compare_exchange_local((intptr_t *)ptr, (intptr_t)oldval, (intptr_t)newval);
}
// Transcendentals (float precision)
__declspec(safe) static inline float sqrt(float v) {
if (__math_lib == __math_lib_svml) {
return __svml_sqrtf(v);
} else {
return __sqrt_varying_float(v);
}
}
__declspec(safe) static inline uniform float sqrt(uniform float v) { return __sqrt_uniform_float(v); }
__declspec(safe) static inline float rsqrt(float v) {
if (__math_lib == __math_lib_svml) {
return __svml_invsqrtf(v);
} else {
return __rsqrt_varying_float(v);
}
}
__declspec(safe) static inline uniform float rsqrt(uniform float v) { return __rsqrt_uniform_float(v); }
__declspec(safe) static inline float rsqrt_fast(float v) { return __rsqrt_fast_varying_float(v); }
__declspec(safe) static inline uniform float rsqrt_fast(uniform float v) { return __rsqrt_fast_uniform_float(v); }
__declspec(safe) static inline float ldexp(float x, int n) {
varying unsigned int ex = 0x7F800000u;
varying unsigned int ix = intbits(x);
ex &= ix; // extract old exponent;
float res = floatbits((ix & ~0x7F800000u) | ((n << 23) + ex));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0f) ? x : res;
}
}
__declspec(safe) static inline uniform float ldexp(uniform float x, uniform int n) {
uniform unsigned int ex = 0x7F800000u;
uniform unsigned int ix = intbits(x);
ex &= ix; // extract old exponent;
uniform float res = floatbits((ix & ~0x7F800000u) | ((n << 23) + ex));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0f) ? x : res;
}
}
__declspec(safe) static inline float frexp(float x, varying int *uniform pw2) {
unsigned int ex = 0x7F800000u; // exponent mask
unsigned int ix = intbits(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
*pw2 = (int)(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return floatbits(ix);
}
__declspec(safe) static inline uniform float frexp(uniform float x, uniform int *uniform pw2) {
uniform unsigned int ex = 0x7F800000u; // exponent mask
uniform unsigned int ix = intbits(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
*pw2 = (uniform int)(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return floatbits(ix);
}
// Most of the transcendental implementations in ispc code here come from
// Solomon Boulos's "syrah": https://github.com/boulos/syrah/
__declspec(safe) static inline float sin(float x_full) {
if (__have_native_trigonometry) {
return __sin_varying_float(x_full);
} else if (__math_lib == __math_lib_svml) {
return __svml_sinf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_sinf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
static const float pi_over_two_vec = 1.57079637050628662109375;
static const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
bool flip_sign = (k_mod4 > 1);
// These coefficients are from sollya with fpminimax(sin(x)/x, [|0, 2,
// 4, 6, 8, 10|], [|single...|], [0;Pi/2]);
static const float sin_c2 = -0.16666667163372039794921875;
static const float sin_c4 = 8.333347737789154052734375e-3;
static const float sin_c6 = -1.9842604524455964565277099609375e-4;
static const float sin_c8 = 2.760012648650445044040679931640625e-6;
static const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
static const float cos_c2 = -0.5;
static const float cos_c4 = 4.166664183139801025390625e-2;
static const float cos_c6 = -1.388833043165504932403564453125e-3;
static const float cos_c8 = 2.47562347794882953166961669921875e-5;
static const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float outside = sin_usecos ? 1 : x;
float c2 = sin_usecos ? cos_c2 : sin_c2;
float c4 = sin_usecos ? cos_c4 : sin_c4;
float c6 = sin_usecos ? cos_c6 : sin_c6;
float c8 = sin_usecos ? cos_c8 : sin_c8;
float c10 = sin_usecos ? cos_c10 : sin_c10;
float x2 = x * x;
float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
__declspec(safe) static inline uniform float sin(uniform float x_full) {
if (__have_native_trigonometry) {
return __sin_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_sinf(x_full);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
static const uniform float pi_over_two_vec = 1.57079637050628662109375;
static const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
uniform bool flip_sign = (k_mod4 > 1);
// These coefficients are from sollya with fpminimax(sin(x)/x, [|0, 2,
// 4, 6, 8, 10|], [|single...|], [0;Pi/2]);
static const uniform float sin_c2 = -0.16666667163372039794921875;
static const uniform float sin_c4 = 8.333347737789154052734375e-3;
static const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
static const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
static const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
static const uniform float cos_c2 = -0.5;
static const uniform float cos_c4 = 4.166664183139801025390625e-2;
static const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
static const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
static const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float outside, c2, c4, c6, c8, c10;
if (sin_usecos) {
outside = 1.;
c2 = cos_c2;
c4 = cos_c4;
c6 = cos_c6;
c8 = cos_c8;
c10 = cos_c10;
} else {
outside = x;
c2 = sin_c2;
c4 = sin_c4;
c6 = sin_c6;
c8 = sin_c8;
c10 = sin_c10;
}
uniform float x2 = x * x;
uniform float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
__declspec(safe) static inline float asin(float x0) {
bool isneg = x0 < 0;
float x = abs(x0);
bool isnan = (x > 1);
float v;
if (__have_native_trigonometry && !__is_xe_target) {
return __asin_varying_float(x0);
} else if (__math_lib == __math_lib_svml) {
return __svml_asinf(x0);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_asinf(extract(x0, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc) {
// sollya
// fpminimax(((asin(x)-pi/2)/-sqrt(1-x)), [|0,1,2,3,4,5,6,7,8,9,10|],
// [|single...|], [1e-20;.9999999999999999]);
// avg error: 8.5716801e-09, max error: 2.1373853e-07
v = 1.57079637050628662109375f +
x * (-0.21460501849651336669921875f +
x * (8.9116774499416351318359375e-2f +
x * (-5.146093666553497314453125e-2f +
x * (3.7269376218318939208984375e-2f +
x * (-3.5882405936717987060546875e-2f +
x * (4.14929799735546112060546875e-2f +
x * (-4.25077490508556365966796875e-2f +
x * (3.05023305118083953857421875e-2f +
x * (-1.2897425331175327301025390625e-2f +
x * 2.38926825113594532012939453125e-3f)))))))));
} else if (__math_lib == __math_lib_ispc_fast) {
// sollya
// fpminimax(((asin(x)-pi/2)/-sqrt(1-x)), [|0,1,2,3,4,5|],[|single...|],
// [1e-20;.9999999999999999]);
// avg error: 1.1105439e-06, max error 1.3187528e-06
v = 1.57079517841339111328125f +
x * (-0.21450997889041900634765625f +
x * (8.78556668758392333984375e-2f +
x * (-4.489909112453460693359375e-2f +
x * (1.928029954433441162109375e-2f + x * (-4.3095736764371395111083984375e-3f)))));
}
v *= -sqrt(1.f - x);
v = v + 1.57079637050628662109375;
if (v < 0)
v = 0;
// v = max(0, v);
if (isneg)
v = -v;
if (isnan)
v = floatbits(0x7fc00000);
return v;
}
__declspec(safe) static inline uniform float asin(uniform float x0) {
uniform bool isneg = x0 < 0;
uniform float x = abs(x0);
uniform bool isnan = (x > 1);
uniform float v;
if (__have_native_trigonometry && !__is_xe_target) {
return __asin_uniform_float(x0);
} else if (__math_lib == __math_lib_svml || __math_lib == __math_lib_system) {
return __stdlib_asinf(x0);
} else if (__math_lib == __math_lib_ispc) {
// sollya
// fpminimax(((asin(x)-pi/2)/-sqrt(1-x)), [|0,1,2,3,4,5,6,7,8,9,10|],
// [|single...|], [1e-20;.9999999999999999]);
// avg error: 8.5716801e-09, max error: 2.1373853e-07
v = 1.57079637050628662109375f +
x * (-0.21460501849651336669921875f +
x * (8.9116774499416351318359375e-2f +
x * (-5.146093666553497314453125e-2f +
x * (3.7269376218318939208984375e-2f +
x * (-3.5882405936717987060546875e-2f +
x * (4.14929799735546112060546875e-2f +
x * (-4.25077490508556365966796875e-2f +
x * (3.05023305118083953857421875e-2f +
x * (-1.2897425331175327301025390625e-2f +
x * 2.38926825113594532012939453125e-3f)))))))));
} else if (__math_lib == __math_lib_ispc_fast) {
// sollya
// fpminimax(((asin(x)-pi/2)/-sqrt(1-x)), [|0,1,2,3,4,5|],[|single...|],
// [1e-20;.9999999999999999]);
// avg error: 1.1105439e-06, max error 1.3187528e-06
v = 1.57079517841339111328125f +
x * (-0.21450997889041900634765625f +
x * (8.78556668758392333984375e-2f +
x * (-4.489909112453460693359375e-2f +
x * (1.928029954433441162109375e-2f + x * (-4.3095736764371395111083984375e-3f)))));
}
v *= -sqrt(1.f - x);
v = v + 1.57079637050628662109375;
if (v < 0)
v = 0;
// v = max(0, v);
if (isneg)
v = -v;
if (isnan)
v = floatbits(0x7fc00000);
return v;
}
__declspec(safe) static inline float cos(float x_full) {
if (__have_native_trigonometry) {
return __cos_varying_float(x_full);
}
if (__math_lib == __math_lib_svml) {
return __svml_cosf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_cosf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
static const float pi_over_two_vec = 1.57079637050628662109375;
static const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
bool flip_sign = (k_mod4 == 1 || k_mod4 == 2);
const float sin_c2 = -0.16666667163372039794921875;
const float sin_c4 = 8.333347737789154052734375e-3;
const float sin_c6 = -1.9842604524455964565277099609375e-4;
const float sin_c8 = 2.760012648650445044040679931640625e-6;
const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const float cos_c2 = -0.5;
const float cos_c4 = 4.166664183139801025390625e-2;
const float cos_c6 = -1.388833043165504932403564453125e-3;
const float cos_c8 = 2.47562347794882953166961669921875e-5;
const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float outside = cos_usecos ? 1. : x;
float c2 = cos_usecos ? cos_c2 : sin_c2;
float c4 = cos_usecos ? cos_c4 : sin_c4;
float c6 = cos_usecos ? cos_c6 : sin_c6;
float c8 = cos_usecos ? cos_c8 : sin_c8;
float c10 = cos_usecos ? cos_c10 : sin_c10;
float x2 = x * x;
float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
__declspec(safe) static inline uniform float cos(uniform float x_full) {
if (__have_native_trigonometry) {
return __cos_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_cosf(x_full);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
static const uniform float pi_over_two_vec = 1.57079637050628662109375;
static const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
uniform bool flip_sign = (k_mod4 == 1 || k_mod4 == 2);
const uniform float sin_c2 = -0.16666667163372039794921875;
const uniform float sin_c4 = 8.333347737789154052734375e-3;
const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const uniform float cos_c2 = -0.5;
const uniform float cos_c4 = 4.166664183139801025390625e-2;
const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float outside, c2, c4, c6, c8, c10;
if (cos_usecos) {
outside = 1.;
c2 = cos_c2;
c4 = cos_c4;
c6 = cos_c6;
c8 = cos_c8;
c10 = cos_c10;
} else {
outside = x;
c2 = sin_c2;
c4 = sin_c4;
c6 = sin_c6;
c8 = sin_c8;
c10 = sin_c10;
}
uniform float x2 = x * x;
uniform float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
__declspec(safe) static inline float acos(float v) {
if (__have_native_trigonometry && !__is_xe_target) {
return __acos_varying_float(v);
} else if (__math_lib == __math_lib_svml) {
return __svml_acosf(v);
} else {
return 1.57079637050628662109375 - asin(v);
}
}
__declspec(safe) static inline uniform float acos(uniform float v) {
if (__have_native_trigonometry && !__is_xe_target)
return __acos_uniform_float(v);
else
return 1.57079637050628662109375 - asin(v);
}
__declspec(safe) static inline void sincos(float x_full, varying float *uniform sin_result,
varying float *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_varying_float(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
return;
}
if (__math_lib == __math_lib_svml) {
__svml_sincosf(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else if (__math_lib == __math_lib_system) {
foreach_active(i) {
uniform float s, c;
__stdlib_sincosf(extract(x_full, i), (opaque_ptr_t)&s, (opaque_ptr_t)&c);
*sin_result = insert(*sin_result, i, s);
*cos_result = insert(*cos_result, i, c);
}
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const float pi_over_two_vec = 1.57079637050628662109375;
const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
bool sin_flipsign = (k_mod4 > 1);
bool cos_flipsign = (k_mod4 == 1 || k_mod4 == 2);
const float one_vec = 1.;
const float sin_c2 = -0.16666667163372039794921875;
const float sin_c4 = 8.333347737789154052734375e-3;
const float sin_c6 = -1.9842604524455964565277099609375e-4;
const float sin_c8 = 2.760012648650445044040679931640625e-6;
const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const float cos_c2 = -0.5;
const float cos_c4 = 4.166664183139801025390625e-2;
const float cos_c6 = -1.388833043165504932403564453125e-3;
const float cos_c8 = 2.47562347794882953166961669921875e-5;
const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float x2 = x * x;
float sin_formula = x2 * sin_c10 + sin_c8;
float cos_formula = x2 * cos_c10 + cos_c8;
sin_formula = x2 * sin_formula + sin_c6;
cos_formula = x2 * cos_formula + cos_c6;
sin_formula = x2 * sin_formula + sin_c4;
cos_formula = x2 * cos_formula + cos_c4;
sin_formula = x2 * sin_formula + sin_c2;
cos_formula = x2 * cos_formula + cos_c2;
sin_formula = x2 * sin_formula + one_vec;
cos_formula = x2 * cos_formula + one_vec;
sin_formula *= x;
*sin_result = sin_usecos ? cos_formula : sin_formula;
*cos_result = cos_usecos ? cos_formula : sin_formula;
*sin_result = sin_flipsign ? -*sin_result : *sin_result;
*cos_result = cos_flipsign ? -*cos_result : *cos_result;
}
}
__declspec(safe) static inline void sincos(uniform float x_full, uniform float *uniform sin_result,
uniform float *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_uniform_float(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
return;
}
if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
__stdlib_sincosf(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const uniform float pi_over_two_vec = 1.57079637050628662109375;
const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (uniform int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
uniform bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
uniform bool sin_flipsign = (k_mod4 > 1);
uniform bool cos_flipsign = (k_mod4 == 1 || k_mod4 == 2);
const uniform float one_vec = 1.;
const uniform float sin_c2 = -0.16666667163372039794921875;
const uniform float sin_c4 = 8.333347737789154052734375e-3;
const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const uniform float cos_c2 = -0.5;
const uniform float cos_c4 = 4.166664183139801025390625e-2;
const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float x2 = x * x;
uniform float sin_formula = x2 * sin_c10 + sin_c8;
uniform float cos_formula = x2 * cos_c10 + cos_c8;
sin_formula = x2 * sin_formula + sin_c6;
cos_formula = x2 * cos_formula + cos_c6;
sin_formula = x2 * sin_formula + sin_c4;
cos_formula = x2 * cos_formula + cos_c4;
sin_formula = x2 * sin_formula + sin_c2;
cos_formula = x2 * cos_formula + cos_c2;
sin_formula = x2 * sin_formula + one_vec;
cos_formula = x2 * cos_formula + one_vec;
sin_formula *= x;
*sin_result = sin_usecos ? cos_formula : sin_formula;
*cos_result = cos_usecos ? cos_formula : sin_formula;
*sin_result = sin_flipsign ? -*sin_result : *sin_result;
*cos_result = cos_flipsign ? -*cos_result : *cos_result;
}
}
__declspec(safe) static inline float tan(float x_full) {
if (__have_native_trigonometry) {
return __tan_varying_float(x_full);
} else if (__math_lib == __math_lib_svml) {
return __svml_tanf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_tanf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const float pi_over_four_vec = 0.785398185253143310546875;
const float four_over_pi_vec = 1.27323949337005615234375;
bool x_lt_0 = x_full < 0.;
float y = x_lt_0 ? -x_full : x_full;
float scaled = y * four_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
float x = y - k_real * pi_over_four_vec;
// if k & 1, x -= Pi/4
bool need_offset = (k & 1) != 0;
x = need_offset ? x - pi_over_four_vec : x;
// if k & 3 == (0 or 3) let z = tan_In...(y) otherwise z = -cot_In0To...
int k_mod4 = k & 3;
bool use_cotan = (k_mod4 == 1) || (k_mod4 == 2);
const float one_vec = 1.0;
const float tan_c2 = 0.33333075046539306640625;
const float tan_c4 = 0.13339905440807342529296875;
const float tan_c6 = 5.3348250687122344970703125e-2;
const float tan_c8 = 2.46033705770969390869140625e-2;
const float tan_c10 = 2.892402000725269317626953125e-3;
const float tan_c12 = 9.500005282461643218994140625e-3;
const float cot_c2 = -0.3333333432674407958984375;
const float cot_c4 = -2.222204394638538360595703125e-2;
const float cot_c6 = -2.11752182804048061370849609375e-3;
const float cot_c8 = -2.0846328698098659515380859375e-4;
const float cot_c10 = -2.548247357481159269809722900390625e-5;
const float cot_c12 = -3.5257363606433500535786151885986328125e-7;
float x2 = x * x;
float z;
cif(use_cotan) {
float cot_val = x2 * cot_c12 + cot_c10;
cot_val = x2 * cot_val + cot_c8;
cot_val = x2 * cot_val + cot_c6;
cot_val = x2 * cot_val + cot_c4;
cot_val = x2 * cot_val + cot_c2;
cot_val = x2 * cot_val + one_vec;
// The equation is for x * cot(x) but we need -x * cot(x) for the tan part.
cot_val /= -x;
z = cot_val;
}
else {
float tan_val = x2 * tan_c12 + tan_c10;
tan_val = x2 * tan_val + tan_c8;
tan_val = x2 * tan_val + tan_c6;
tan_val = x2 * tan_val + tan_c4;
tan_val = x2 * tan_val + tan_c2;
tan_val = x2 * tan_val + one_vec;
// Equation was for tan(x)/x
tan_val *= x;
z = tan_val;
}
return x_lt_0 ? -z : z;
}
}
__declspec(safe) static inline uniform float tan(uniform float x_full) {
if (__have_native_trigonometry) {
return __tan_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_tanf(x_full);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const uniform float pi_over_four_vec = 0.785398185253143310546875;
const uniform float four_over_pi_vec = 1.27323949337005615234375;
uniform bool x_lt_0 = x_full < 0.;
uniform float y = x_lt_0 ? -x_full : x_full;
uniform float scaled = y * four_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
uniform float x = y - k_real * pi_over_four_vec;
// if k & 1, x -= Pi/4
uniform bool need_offset = (k & 1) != 0;
x = need_offset ? x - pi_over_four_vec : x;
// if k & 3 == (0 or 3) let z = tan_In...(y) otherwise z = -cot_In0To...
uniform int k_mod4 = k & 3;
uniform bool use_cotan = (k_mod4 == 1) || (k_mod4 == 2);
const uniform float one_vec = 1.0;
const uniform float tan_c2 = 0.33333075046539306640625;
const uniform float tan_c4 = 0.13339905440807342529296875;
const uniform float tan_c6 = 5.3348250687122344970703125e-2;
const uniform float tan_c8 = 2.46033705770969390869140625e-2;
const uniform float tan_c10 = 2.892402000725269317626953125e-3;
const uniform float tan_c12 = 9.500005282461643218994140625e-3;
const uniform float cot_c2 = -0.3333333432674407958984375;
const uniform float cot_c4 = -2.222204394638538360595703125e-2;
const uniform float cot_c6 = -2.11752182804048061370849609375e-3;
const uniform float cot_c8 = -2.0846328698098659515380859375e-4;
const uniform float cot_c10 = -2.548247357481159269809722900390625e-5;
const uniform float cot_c12 = -3.5257363606433500535786151885986328125e-7;
uniform float x2 = x * x;
uniform float z;
if (use_cotan) {
uniform float cot_val = x2 * cot_c12 + cot_c10;
cot_val = x2 * cot_val + cot_c8;
cot_val = x2 * cot_val + cot_c6;
cot_val = x2 * cot_val + cot_c4;
cot_val = x2 * cot_val + cot_c2;
cot_val = x2 * cot_val + one_vec;
// The equation is for x * cot(x) but we need -x * cot(x) for the tan part.
cot_val /= -x;
z = cot_val;
} else {
uniform float tan_val = x2 * tan_c12 + tan_c10;
tan_val = x2 * tan_val + tan_c8;
tan_val = x2 * tan_val + tan_c6;
tan_val = x2 * tan_val + tan_c4;
tan_val = x2 * tan_val + tan_c2;
tan_val = x2 * tan_val + one_vec;
// Equation was for tan(x)/x
tan_val *= x;
z = tan_val;
}
return x_lt_0 ? -z : z;
}
}
__declspec(safe) static inline float atan(float x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan_varying_float(x_full);
} else if (__math_lib == __math_lib_svml) {
return __svml_atanf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_atanf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const float pi_over_two_vec = 1.57079637050628662109375;
// atan(-x) = -atan(x) (so flip from negative to positive first)
// if x > 1 -> atan(x) = Pi/2 - atan(1/x)
bool x_neg = x_full < 0;
float x_flipped = x_neg ? -x_full : x_full;
bool x_gt_1 = x_flipped > 1.;
float x = x_gt_1 ? 1. / x_flipped : x_flipped;
// These coefficients approximate atan(x)/x
const float atan_c0 = 0.99999988079071044921875;
const float atan_c2 = -0.3333191573619842529296875;
const float atan_c4 = 0.199689209461212158203125;
const float atan_c6 = -0.14015688002109527587890625;
const float atan_c8 = 9.905083477497100830078125e-2;
const float atan_c10 = -5.93664981424808502197265625e-2;
const float atan_c12 = 2.417283318936824798583984375e-2;
const float atan_c14 = -4.6721356920897960662841796875e-3;
float x2 = x * x;
float result = x2 * atan_c14 + atan_c12;
result = x2 * result + atan_c10;
result = x2 * result + atan_c8;
result = x2 * result + atan_c6;
result = x2 * result + atan_c4;
result = x2 * result + atan_c2;
result = x2 * result + atan_c0;
result *= x;
result = x_gt_1 ? pi_over_two_vec - result : result;
result = x_neg ? -result : result;
return result;
}
}
__declspec(safe) static inline uniform float atan(uniform float x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_atanf(x_full);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const uniform float pi_over_two_vec = 1.57079637050628662109375;
// atan(-x) = -atan(x) (so flip from negative to positive first)
// if x > 1 -> atan(x) = Pi/2 - atan(1/x)
uniform bool x_neg = x_full < 0;
uniform float x_flipped = x_neg ? -x_full : x_full;
uniform bool x_gt_1 = x_flipped > 1.;
uniform float x = x_gt_1 ? 1. / x_flipped : x_flipped;
// These coefficients approximate atan(x)/x
const uniform float atan_c0 = 0.99999988079071044921875;
const uniform float atan_c2 = -0.3333191573619842529296875;
const uniform float atan_c4 = 0.199689209461212158203125;
const uniform float atan_c6 = -0.14015688002109527587890625;
const uniform float atan_c8 = 9.905083477497100830078125e-2;
const uniform float atan_c10 = -5.93664981424808502197265625e-2;
const uniform float atan_c12 = 2.417283318936824798583984375e-2;
const uniform float atan_c14 = -4.6721356920897960662841796875e-3;
uniform float x2 = x * x;
uniform float result = x2 * atan_c14 + atan_c12;
result = x2 * result + atan_c10;
result = x2 * result + atan_c8;
result = x2 * result + atan_c6;
result = x2 * result + atan_c4;
result = x2 * result + atan_c2;
result = x2 * result + atan_c0;
result *= x;
result = x_gt_1 ? pi_over_two_vec - result : result;
result = x_neg ? -result : result;
return result;
}
}
__declspec(safe) static inline float atan2(float y, float x) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan2_varying_float(y, x);
} else if (__math_lib == __math_lib_svml) {
return __svml_atan2f(y, x);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_atan2f(extract(y, i), extract(x, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const float pi_vec = 3.1415926536;
const float pi_over_two_vec = 1.5707963267;
// atan2(y, x) =
//
// atan2(y > 0, x = +-0) -> Pi/2
// atan2(y < 0, x = +-0) -> -Pi/2
// atan2(y = +-0, x < +0) -> +-Pi
// atan2(y = +-0, x >= +0) -> +-0
//
// atan2(y >= 0, x < 0) -> Pi + atan(y/x)
// atan2(y < 0, x < 0) -> -Pi + atan(y/x)
// atan2(y, x > 0) -> atan(y/x)
//
// and then a bunch of code for dealing with infinities.
float y_over_x = y / x;
float atan_arg = atan(y_over_x);
bool x_lt_0 = x < 0;
bool y_lt_0 = y < 0;
float offset = x_lt_0 ? (y_lt_0 ? -pi_vec : pi_vec) : 0;
return offset + atan_arg;
}
}
__declspec(safe) static inline uniform float atan2(uniform float y, uniform float x) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan2_uniform_float(y, x);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_atan2f(y, x);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
const uniform float pi_vec = 3.1415927410125732421875;
const uniform float pi_over_two_vec = 1.57079637050628662109375;
uniform float y_over_x = y / x;
uniform float atan_arg = atan(y_over_x);
uniform bool x_lt_0 = x < 0;
uniform bool y_lt_0 = y < 0;
uniform float offset = x_lt_0 ? (y_lt_0 ? -pi_vec : pi_vec) : 0;
return offset + atan_arg;
}
}
__declspec(safe) static inline float exp(float x_full) {
if (__have_native_transcendentals) {
return __exp_varying_float(x_full);
} else if (__math_lib == __math_lib_svml) {
return __svml_expf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_expf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc_fast) {
float z = floor(1.44269504088896341f * x_full + 0.5f);
int n;
x_full -= z * 0.693359375f;
x_full -= z * -2.12194440e-4f;
n = (int)z;
z = x_full * x_full;
z = (((((1.9875691500E-4f * x_full + 1.3981999507E-3f) * x_full + 8.3334519073E-3f) * x_full +
4.1665795894E-2f) *
x_full +
1.6666665459E-1f) *
x_full +
5.0000001201E-1f) *
z +
x_full + 1.f;
x_full = ldexp(z, n);
return x_full;
} else if (__math_lib == __math_lib_ispc) {
// See the uniform version for more information
const float ln2_part1 = 0.6931457519;
const float ln2_part2 = 1.4286067653e-6;
const float one_over_ln2 = 1.44269502162933349609375;
float scaled = x_full * one_over_ln2;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * ln2_part1;
x -= k_real * ln2_part2;
// These coefficients are for e^x in [0, ln(2)]
const float one = 1.;
const float c2 = 0.4999999105930328369140625;
const float c3 = 0.166668415069580078125;
const float c4 = 4.16539050638675689697265625e-2;
const float c5 = 8.378830738365650177001953125e-3;
const float c6 = 1.304379315115511417388916015625e-3;
const float c7 = 2.7555381529964506626129150390625e-4;
float result = x * c7 + c6;
result = x * result + c5;
result = x * result + c4;
result = x * result + c3;
result = x * result + c2;
result = x * result + one;
result = x * result + one;
// Compute 2^k (should differ for float and double, but I'll avoid
// it for now and just do floats)
const int fpbias = 127;
int biased_n = k + fpbias;
bool overflow = k_real > fpbias;
// Minimum exponent is -126, so if k is <= -127 (k + 127 <= 0)
// we've got underflow. -127 * ln(2) -> -88.02. So the most
// negative float input that doesn't result in zero is like -88.
bool underflow = k_real <= -fpbias;
const int InfBits = 0x7f800000;
biased_n <<= 23;
// Reinterpret this thing as float
float two_to_the_n = floatbits(biased_n);
// Handle both doubles and floats (hopefully eliding the copy for float)
float elemtype_2n = two_to_the_n;
result *= elemtype_2n;
result = overflow ? floatbits(InfBits) : result;
result = underflow ? 0. : result;
return result;
}
}
__declspec(safe) static inline uniform float exp(uniform float x_full) {
if (__have_native_transcendentals) {
return __exp_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_expf(x_full);
} else if (__math_lib == __math_lib_ispc_fast) {
uniform float z = floor(1.44269504088896341f * x_full + 0.5f);
uniform int n;
x_full -= z * 0.693359375f;
x_full -= z * -2.12194440e-4f;
n = (int)z;
z = x_full * x_full;
z = (((((1.9875691500E-4f * x_full + 1.3981999507E-3f) * x_full + 8.3334519073E-3f) * x_full +
4.1665795894E-2f) *
x_full +
1.6666665459E-1f) *
x_full +
5.0000001201E-1f) *
z +
x_full + 1.f;
x_full = ldexp(z, n);
return x_full;
} else if (__math_lib == __math_lib_ispc) {
// Precision: <5 ULP for normal numbers (possibly much higher for subnormal like exp(-87.34)).
// Support special input values like NaN, -Inf, +Inf and subnormal numbers correctly.
const uniform float ln2_part1 = 0.6931457519;
const uniform float ln2_part2 = 1.4286067653e-6;
const uniform float one_over_ln2 = 1.44269502162933349609375;
uniform float scaled = x_full * one_over_ln2;
uniform float k_real = floor(scaled);
uniform int k = (uniform int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * ln2_part1;
x -= k_real * ln2_part2;
// These coefficients are for e^x in [0, ln(2)]
const uniform float one = 1.;
const uniform float c2 = 0.4999999105930328369140625;
const uniform float c3 = 0.166668415069580078125;
const uniform float c4 = 4.16539050638675689697265625e-2;
const uniform float c5 = 8.378830738365650177001953125e-3;
const uniform float c6 = 1.304379315115511417388916015625e-3;
const uniform float c7 = 2.7555381529964506626129150390625e-4;
uniform float result = x * c7 + c6;
result = x * result + c5;
result = x * result + c4;
result = x * result + c3;
result = x * result + c2;
result = x * result + one;
result = x * result + one;
// Compute 2^k (should differ for uniform float and double, but I'll avoid
// it for now and just do uniform floats)
const uniform int fpbias = 127;
uniform int biased_n = k + fpbias;
uniform bool overflow = k_real > fpbias;
// Minimum exponent is -126, so if k is <= -127 (k + 127 <= 0)
// we've got underflow. -127 * ln(2) -> -88.02. So the most
// negative uniform float input that doesn't result in zero is like -88.
uniform bool underflow = k_real <= -fpbias;
const uniform int InfBits = 0x7f800000;
biased_n <<= 23;
// Reuniform interpret this thing as uniform float
uniform float two_to_the_n = floatbits(biased_n);
// Handle both doubles and uniform floats (hopefully eliding the copy for uniform float)
uniform float elemtype_2n = two_to_the_n;
result *= elemtype_2n;
result = overflow ? floatbits(InfBits) : result;
result = underflow ? 0. : result;
return result;
}
}
// Range reduction for logarithms takes log(x) -> log(2^n * y) -> n
// * log(2) + log(y) where y is the reduced range (usually in [1/2,
// 1)).
__declspec(safe) static inline void __range_reduce_log(float input, varying float *uniform reduced,
varying int *uniform exponent) {
int int_version = intbits(input);
// single precision = SEEE EEEE EMMM MMMM MMMM MMMM MMMM MMMM
// exponent mask = 0111 1111 1000 0000 0000 0000 0000 0000
// 0x7 0xF 0x8 0x0 0x0 0x0 0x0 0x0
// non-exponent = 1000 0000 0111 1111 1111 1111 1111 1111
// = 0x8 0x0 0x7 0xF 0xF 0xF 0xF 0xF
// const int exponent_mask(0x7F800000)
static const int nonexponent_mask = 0x807FFFFF;
// We want the reduced version to have an exponent of -1 which is -1 + 127 after biasing or 126
static const int exponent_neg1 = (126l << 23);
// NOTE(boulos): We don't need to mask anything out since we know
// the sign bit has to be 0. If it's 1, we need to return infinity/nan
// anyway (log(x), x = +-0 -> infinity, x < 0 -> NaN).
int biased_exponent = int_version >> 23; // This number is [0, 255] but it means [-127, 128]
int offset_exponent = biased_exponent + 1; // Treat the number as if it were 2^{e+1} * (1.m)/2
*exponent = offset_exponent - 127; // get the real value
// Blend the offset_exponent with the original input (do this in
// int for now, until I decide if float can have & and ¬)
int blended = (int_version & nonexponent_mask) | (exponent_neg1);
*reduced = floatbits(blended);
}
__declspec(safe) static inline void __range_reduce_log(uniform float input, uniform float *uniform reduced,
uniform int *uniform exponent) {
uniform int int_version = intbits(input);
static const uniform int nonexponent_mask = 0x807FFFFF;
static const uniform int exponent_neg1 = (126ul << 23);
uniform int biased_exponent = int_version >> 23;
uniform int offset_exponent = biased_exponent + 1;
*exponent = offset_exponent - 127; // get the real value
uniform int blended = (int_version & nonexponent_mask) | (exponent_neg1);
*reduced = floatbits(blended);
}
__declspec(safe) static inline float log(float x_full) {
if (__have_native_transcendentals) {
return __log_varying_float(x_full);
} else if (__math_lib == __math_lib_svml) {
return __svml_logf(x_full);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_logf(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc_fast) {
int e;
x_full = frexp(x_full, &e);
int x_smaller_SQRTHF = (0.707106781186547524f > x_full) ? 0xffffffff : 0;
e += x_smaller_SQRTHF;
int ix_add = intbits(x_full);
ix_add &= x_smaller_SQRTHF;
x_full += floatbits(ix_add) - 1.f;
float z = x_full * x_full;
float y = ((((((((7.0376836292E-2f * x_full + -1.1514610310E-1f) * x_full + 1.1676998740E-1f) * x_full +
-1.2420140846E-1f) *
x_full +
1.4249322787E-1f) *
x_full +
-1.6668057665E-1f) *
x_full +
2.0000714765E-1f) *
x_full +
-2.4999993993E-1f) *
x_full +
3.3333331174E-1f) *
x_full * z;
float fe = (float)e;
y += fe * -2.12194440e-4;
y -= 0.5f * z;
z = x_full + y;
return z + 0.693359375 * fe;
} else if (__math_lib == __math_lib_ispc) {
float reduced;
int exponent;
const int NaN_bits = 0x7fc00000;
const int Neg_Inf_bits = 0xFF800000;
const float NaN = floatbits(NaN_bits);
const float neg_inf = floatbits(Neg_Inf_bits);
bool use_nan = x_full < 0.;
bool use_inf = x_full == 0.;
bool exceptional = use_nan || use_inf;
const float one = 1.0;
float patched = exceptional ? one : x_full;
__range_reduce_log(patched, &reduced, &exponent);
const float ln2 = 0.693147182464599609375;
float x1 = one - reduced;
const float c1 = 0.50000095367431640625;
const float c2 = 0.33326041698455810546875;
const float c3 = 0.2519190013408660888671875;
const float c4 = 0.17541764676570892333984375;
const float c5 = 0.3424419462680816650390625;
const float c6 = -0.599632322788238525390625;
const float c7 = +1.98442304134368896484375;
const float c8 = -2.4899270534515380859375;
const float c9 = +1.7491014003753662109375;
float result = x1 * c9 + c8;
result = x1 * result + c7;
result = x1 * result + c6;
result = x1 * result + c5;
result = x1 * result + c4;
result = x1 * result + c3;
result = x1 * result + c2;
result = x1 * result + c1;
result = x1 * result + one;
// Equation was for -(ln(red)/(1-red))
result *= -x1;
result += (float)(exponent)*ln2;
return exceptional ? (use_nan ? NaN : neg_inf) : result;
}
}
__declspec(safe) static inline uniform float log(uniform float x_full) {
if (__have_native_transcendentals) {
return __log_uniform_float(x_full);
} else if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_logf(x_full);
} else if (__math_lib == __math_lib_ispc_fast) {
uniform int e;
x_full = frexp(x_full, &e);
uniform int x_smaller_SQRTHF = (0.707106781186547524f > x_full) ? 0xffffffff : 0;
e += x_smaller_SQRTHF;
uniform int ix_add = intbits(x_full);
ix_add &= x_smaller_SQRTHF;
x_full += floatbits(ix_add) - 1.f;
uniform float z = x_full * x_full;
uniform float y = ((((((((7.0376836292E-2f * x_full + -1.1514610310E-1f) * x_full + 1.1676998740E-1f) * x_full +
-1.2420140846E-1f) *
x_full +
1.4249322787E-1f) *
x_full +
-1.6668057665E-1f) *
x_full +
2.0000714765E-1f) *
x_full +
-2.4999993993E-1f) *
x_full +
3.3333331174E-1f) *
x_full * z;
uniform float fe = (uniform float)e;
y += fe * -2.12194440e-4;
y -= 0.5f * z;
z = x_full + y;
return z + 0.693359375 * fe;
} else if (__math_lib == __math_lib_ispc) {
uniform float reduced;
uniform int exponent;
const uniform int NaN_bits = 0x7fc00000;
const uniform int Neg_Inf_bits = 0xFF800000;
const uniform float NaN = floatbits(NaN_bits);
const uniform float neg_inf = floatbits(Neg_Inf_bits);
uniform bool use_nan = x_full < 0.;
uniform bool use_inf = x_full == 0.;
uniform bool exceptional = use_nan || use_inf;
const uniform float one = 1.0;
uniform float patched = exceptional ? one : x_full;
__range_reduce_log(patched, &reduced, &exponent);
const uniform float ln2 = 0.693147182464599609375;
uniform float x1 = one - reduced;
const uniform float c1 = 0.50000095367431640625;
const uniform float c2 = 0.33326041698455810546875;
const uniform float c3 = 0.2519190013408660888671875;
const uniform float c4 = 0.17541764676570892333984375;
const uniform float c5 = 0.3424419462680816650390625;
const uniform float c6 = -0.599632322788238525390625;
const uniform float c7 = +1.98442304134368896484375;
const uniform float c8 = -2.4899270534515380859375;
const uniform float c9 = +1.7491014003753662109375;
uniform float result = x1 * c9 + c8;
result = x1 * result + c7;
result = x1 * result + c6;
result = x1 * result + c5;
result = x1 * result + c4;
result = x1 * result + c3;
result = x1 * result + c2;
result = x1 * result + c1;
result = x1 * result + one;
// Equation was for -(ln(red)/(1-red))
result *= -x1;
result += (uniform float)(exponent)*ln2;
return exceptional ? (use_nan ? NaN : neg_inf) : result;
}
}
__declspec(safe) static inline float pow(float a, float b) {
if (__have_native_transcendentals) {
return __pow_varying_float(a, b);
} else if (__math_lib == __math_lib_svml) {
return __svml_powf(a, b);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_powf(extract(a, i), extract(b, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
return exp(b * log(a));
}
}
__declspec(safe) static inline uniform float pow(uniform float a, uniform float b) {
if (__have_native_transcendentals) {
return __pow_uniform_float(a, b);
}
if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_powf(a, b);
} else if (__math_lib == __math_lib_ispc || __math_lib == __math_lib_ispc_fast) {
return exp(b * log(a));
}
}
__declspec(safe) static inline uniform uint32 _fast_div3(uniform uint32 x) {
return ((uniform uint64)x * (uniform uint64)1431655766) >> (uniform uint64)32;
}
__declspec(safe) static inline varying uint32 _fast_div3(varying uint32 x) {
return ((varying uint64)x * (varying uint64)1431655766) >> (varying uint64)32;
}
// Cube root approximation using bit hacks for 32-bit float adapted from Kahan's cbrt.
// See "Computing a Real Cube Root" by W. Kahan (https://csclub.uwaterloo.ca/~pbarfuss/qbrt.pdf).
// Improvement of the algorithm provided in https://web.archive.org/20131227144655/http://metamerist.com/cbrt/cbrt.htm.
// The template is only meant to support both uniform and varying input variables in a single function.
// It only supports floats.
template <typename T> __declspec(safe) static inline T _cbrtf_base(T x) {
// Note: using memcpy is strangelly a bit faster, but also unsafe
return floatbits(_fast_div3(intbits(x)) + 709921077u);
}
// Compute a Newton-Raphson iteration (applied to the cube root function).
// Support both simple and double precision as well as both uniform and varying types.
// This method has a quadratic convergance rate (only close to the target value).
template <typename T> __declspec(safe) static inline T _cbrt_newton_iter(T a, T b) {
const uniform T third = (uniform T)1 / (uniform T)3;
return a - third * (a - b / (a * a));
}
// Compute an iteration of the Halley's method (applied to the cube root function).
// Support both simple and double precision as well as both uniform and varying types.
// See "Machine Method for the Extraction of Cube Root" by Otis E. Lancaster (https://www.jstor.org/stable/2279437).
// This method has a cubic convergance rate (only close to the target value).
template <typename T> __declspec(safe) static inline T _cbrt_halley_iter(T a, T b) {
// Note: the order of the operation matters since numbers can quickly overflow
// while computing the cubic root of really huge/tiny numbers.
const uniform T two = (uniform T)2;
const T a3 = a * a * a;
const T correction_factor = (a3 + two * b) / (two * a3 + b);
return a * correction_factor;
}
// Compute the cubic root of a given number.
// The template is only meant to support both uniform and varying input variables in a single function.
// It only supports floats.
// Support special values like NaN, Inf, -Inf, -0.0 and subnormal numbers.
// Precision for all numbers:
// - if precise is true: <5 ULP
// - if precise is false: <30 ULP
template <typename T> __declspec(safe) static inline T _cbrtf(T x, uniform bool precise) {
const T x_abs = abs(x);
T res;
// Correct the input value so it is neither too big nor too small
const T xFactor = select(x_abs < 1e-12, 1e18, select(x_abs > 1e12, 1e-18, (uniform T)1));
const T x_corr = x_abs * xFactor;
// Compute an initial guess value and iteratively improve it.
// This methods converge very quickly if the initial guess is good.
// This approach only actually work well only if the numbers are neither
// very small or very big ones.
res = _cbrtf_base(x_corr);
if (precise) {
res = _cbrt_halley_iter(res, x_corr);
res = _cbrt_halley_iter(res, x_corr);
} else {
res = _cbrt_newton_iter(res, x_corr);
res = _cbrt_newton_iter(res, x_corr);
}
// Correct the result based on the (above) input correction
const T resFactor = select(x_abs < 1e-12, 1e-6, select(x_abs > 1e12, 1e6, (uniform T)1));
res *= resFactor;
// Transfer the sign
const T signed_res = floatbits(intbits(res) | signbits(x));
// Special values are: NaN, +Inf, -Inf, -0, +0
return select((intbits(x) & 0x7F800000) == 0x7F800000 || x == 0.0f, x, signed_res);
}
__declspec(safe) static inline uniform float cbrt(uniform float x) {
if (__math_lib == __math_lib_system || __math_lib == __math_lib_svml) {
return __stdlib_cbrtf(x);
} else if (__math_lib == __math_lib_ispc_fast) {
return _cbrtf(x, false);
} else if (__math_lib == __math_lib_ispc) {
return _cbrtf(x, true);
}
}
__declspec(safe) static inline float cbrt(float x) {
if (__math_lib == __math_lib_svml) {
return __svml_cbrtf(x);
} else if (__math_lib == __math_lib_system) {
float ret;
foreach_active(i) {
uniform float r = __stdlib_cbrtf(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
} else if (__math_lib == __math_lib_ispc_fast) {
return _cbrtf(x, false);
} else if (__math_lib == __math_lib_ispc) {
return _cbrtf(x, true);
}
}
///////////////////////////////////////////////////////////////////////////
// Transcendentals (16-bit float precision)
__declspec(safe) static inline float16 sqrt(float16 v) { return __sqrt_varying_half(v); }
__declspec(safe) static inline uniform float16 sqrt(uniform float16 v) { return __sqrt_uniform_half(v); }
__declspec(safe) static inline float16 rsqrt(float16 v) {
if (__have_native_half_full_support) {
return __rsqrt_varying_half(v);
} else {
return (float16)(rcp(sqrt((float)v)));
}
}
__declspec(safe) static inline uniform float16 rsqrt(uniform float16 v) {
if (__have_native_half_full_support) {
return __rsqrt_uniform_half(v);
} else {
return (uniform float16)(rcp(sqrt((uniform float)v)));
}
}
__declspec(safe) static inline float16 ldexp(float16 x, int n) {
unsigned int16 ex = 0x7c00u;
unsigned int16 ix = intbits(x);
ex &= ix; // extract old exponent;
float16 res = float16bits((unsigned int16)((ix & ~0x7c00u) | (((int16)n << 10) + ex)));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0f16) ? x : res;
}
}
__declspec(safe) static inline uniform float16 ldexp(uniform float16 x, uniform int n) {
uniform unsigned int16 ex = 0x7c00u;
uniform unsigned int16 ix = intbits(x);
ex &= ix; // extract old exponent;
uniform float16 res = float16bits((uniform unsigned int16)((ix & ~0x7c00u) | (((int16)n << 10) + ex)));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0f16) ? x : res;
}
}
__declspec(safe) static inline float16 frexp(float16 x, varying int *uniform pw2) {
unsigned int16 ex = 0x7c00u; // exponent mask
unsigned int16 ix = intbits(x);
ex &= ix;
ix &= ~0x7c00u; // clear exponent
*pw2 = (int)(ex >> 10) - 14; // compute exponent
ix |= 0x3800u; // insert exponent +1 in x
return float16bits(ix);
}
__declspec(safe) static inline uniform float16 frexp(uniform float16 x, uniform int *uniform pw2) {
uniform unsigned int16 ex = 0x7c00u; // exponent mask
uniform unsigned int16 ix = intbits(x);
ex &= ix;
ix &= ~0x7c00u; // clear exponent
*pw2 = (uniform int)(ex >> 10) - 14; // compute exponent
ix |= 0x3800u; // insert exponent +1 in x
return float16bits(ix);
}
// If no native trigonometry support, convert to float, get asin and convert to half back
__declspec(safe) static inline float16 sin(float16 x_full) {
if (__have_native_trigonometry) {
return __sin_varying_half(x_full);
} else {
return (float16)(sin((float)x_full));
}
}
__declspec(safe) static inline uniform float16 sin(uniform float16 x_full) {
if (__have_native_trigonometry) {
return __sin_uniform_half(x_full);
} else {
return (uniform float16)(sin((uniform float)x_full));
}
}
__declspec(safe) static inline float16 asin(float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __asin_varying_half(x_full);
} else {
return (float16)(asin((float)x_full));
}
}
__declspec(safe) static inline uniform float16 asin(uniform float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __asin_uniform_half(x_full);
} else {
return (uniform float16)(asin((uniform float)x_full));
}
}
__declspec(safe) static inline float16 cos(float16 x_full) {
if (__have_native_trigonometry) {
return __cos_varying_half(x_full);
} else {
return (float16)(cos((float)x_full));
}
}
__declspec(safe) static inline uniform float16 cos(uniform float16 x_full) {
if (__have_native_trigonometry) {
return __cos_uniform_half(x_full);
} else {
return (uniform float16)(cos((uniform float)x_full));
}
}
__declspec(safe) static inline float16 tan(float16 x_full) {
if (__have_native_trigonometry) {
return __tan_varying_half(x_full);
} else {
return (float16)(tan((float)x_full));
}
}
__declspec(safe) static inline uniform float16 tan(uniform float16 x_full) {
if (__have_native_trigonometry) {
return __tan_uniform_half(x_full);
} else {
return (uniform float16)(tan((uniform float)x_full));
}
}
__declspec(safe) static inline float16 acos(float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __acos_varying_half(x_full);
} else {
return (float16)(acos((float)x_full));
}
}
__declspec(safe) static inline uniform float16 acos(uniform float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __acos_uniform_half(x_full);
} else {
return (uniform float16)(acos((uniform float)x_full));
}
}
__declspec(safe) static inline void sincos(float16 x_full, varying float16 *uniform sin_result,
varying float16 *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_varying_half(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else {
*sin_result = (float16)sin((float)x_full);
*cos_result = (float16)cos((float)x_full);
}
return;
}
__declspec(safe) static inline void sincos(uniform float16 x_full, uniform float16 *uniform sin_result,
uniform float16 *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_uniform_half(x_full, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else {
*sin_result = (uniform float16)sin((uniform float)x_full);
*cos_result = (uniform float16)cos((uniform float)x_full);
}
return;
}
__declspec(safe) static inline float16 atan(float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan_varying_half(x_full);
} else {
return (float16)(atan((float)x_full));
}
}
__declspec(safe) static inline uniform float16 atan(uniform float16 x_full) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan_uniform_half(x_full);
} else {
return (uniform float16)(atan((uniform float)x_full));
}
}
__declspec(safe) static inline float16 atan2(float16 y, float16 x) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan2_varying_half(y, x);
} else {
return (float16)(atan2((float)y, (float)x));
}
}
__declspec(safe) static inline uniform float16 atan2(uniform float16 y, uniform float16 x) {
if (__have_native_trigonometry && !__is_xe_target) {
return __atan2_uniform_half(y, x);
} else {
return (uniform float16)(atan2((uniform float)y, (uniform float)x));
}
}
__declspec(safe) static inline float16 exp(float16 x_full) { return __exp_varying_half(x_full); }
__declspec(safe) static inline uniform float16 exp(uniform float16 x_full) { return __exp_uniform_half(x_full); }
__declspec(safe) static inline float16 log(float16 x_full) { return __log_varying_half(x_full); }
__declspec(safe) static inline uniform float16 log(uniform float16 x_full) { return __log_uniform_half(x_full); }
__declspec(safe) static inline float16 pow(float16 a, float16 b) { return __pow_varying_half(a, b); }
__declspec(safe) static inline uniform float16 pow(uniform float16 a, uniform float16 b) {
return __pow_uniform_half(a, b);
}
///////////////////////////////////////////////////////////////////////////
// Transcendentals (double precision)
__declspec(safe) static inline double sqrt(double v) {
if (__math_lib == __math_lib_svml) {
return __svml_sqrtd(v);
} else {
return __sqrt_varying_double(v);
}
}
__declspec(safe) static inline uniform double sqrt(uniform double v) { return __sqrt_uniform_double(v); }
#define RSQRTD(QUAL) \
__declspec(safe) static inline QUAL double __rsqrt_iterate_##QUAL##_double(QUAL double x, QUAL double y) { \
QUAL double xh = x * 0.5d; \
y += y * (0.5d - xh * y * y); \
y += y * (0.5d - xh * y * y); \
return y; \
} \
__declspec(safe) static inline QUAL double __rsqrt_safe_##QUAL##_double(QUAL double x) { \
if (x <= 1.0e+33d && x >= 1.0e-33d) \
return __rsqrt_iterate_##QUAL##_double(x, rsqrt((QUAL float)x)); \
QUAL int64 ex = intbits(x) & 0x7fe0000000000000; \
QUAL double exp = doublebits(0x7fd0000000000000 - ex); /* 1.0d/exponent */ \
QUAL double exph = doublebits(0x5fe0000000000000 - (ex >> 1)); /* 1.0d/sqrt(exponent) */ \
QUAL double y = rsqrt((QUAL float)(x * exp)); \
return __rsqrt_iterate_##QUAL##_double(x, y * exph); \
}
RSQRTD(varying)
__declspec(safe) static inline double rsqrt(double v) {
if (__math_lib == __math_lib_svml) {
return __svml_invsqrtd(v);
} else if (__have_native_rsqrtd) {
return __rsqrt_varying_double(v);
} else {
return __rsqrt_safe_varying_double(v);
}
}
RSQRTD(uniform)
__declspec(safe) static inline uniform double rsqrt(uniform double v) {
if (__have_native_rsqrtd)
return __rsqrt_uniform_double(v);
else
return __rsqrt_safe_uniform_double(v);
}
__declspec(safe) static inline double rsqrt_fast(double v) {
if (__have_native_rsqrtd) {
return __rsqrt_fast_varying_double(v);
} else {
return __rsqrt_safe_varying_double(v);
}
}
__declspec(safe) static inline uniform double rsqrt_fast(uniform double v) {
if (__have_native_rsqrtd) {
return __rsqrt_fast_uniform_double(v);
} else {
return __rsqrt_safe_uniform_double(v);
}
}
__declspec(safe) static inline double ldexp(double x, int n) {
unsigned int64 ex = 0x7ff0000000000000;
unsigned int64 ix = intbits(x);
ex &= ix;
double res = doublebits((varying unsigned int64)((ix & ~0x7ff0000000000000) | (((int64)n << 52) + ex)));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0d) ? x : res;
}
}
__declspec(safe) static inline uniform double ldexp(uniform double x, uniform int n) {
uniform unsigned int64 ex = 0x7ff0000000000000;
uniform unsigned int64 ix = intbits(x);
ex &= ix;
uniform double res = doublebits((uniform unsigned int64)((ix & ~0x7ff0000000000000) | (((int64)n << 52) + ex)));
if (__math_lib == __math_lib_ispc_fast) {
return res;
} else {
// Return x if it is 0.0f. Otherwise clear and insert new exponent.
return (x == 0.0d) ? x : res;
}
}
__declspec(safe) static inline double frexp(double x, varying int *uniform pw2) {
unsigned int64 ex = 0x7ff0000000000000; // exponent mask
unsigned int64 ix = intbits(x);
ex &= ix;
ix &= ~0x7ff0000000000000; // clear exponent
*pw2 = (int)(ex >> 52) - 1022; // compute exponent
ix |= 0x3fe0000000000000; // insert exponent +1 in x
return doublebits(ix);
}
__declspec(safe) static inline uniform double frexp(uniform double x, uniform int *uniform pw2) {
uniform unsigned int64 ex = 0x7ff0000000000000; // exponent mask
uniform unsigned int64 ix = intbits(x);
ex &= ix;
ix &= ~0x7ff0000000000000; // clear exponent
*pw2 = (int)(ex >> 52) - 1022; // compute exponent
ix |= 0x3fe0000000000000; // insert exponent +1 in x
return doublebits(ix);
}
__declspec(safe) static inline double sin(double x) {
if (__have_native_trigonometry) {
return __sin_varying_double(x);
} else if (__math_lib == __math_lib_svml) {
return __svml_sind(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_sin(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double sin(uniform double x) {
if (__have_native_trigonometry) {
return __sin_uniform_double(x);
} else
return __stdlib_sin(x);
}
__declspec(safe) static inline uniform double asin(uniform double x) {
if (__have_native_trigonometry) {
return __asin_uniform_double(x);
} else {
return __stdlib_asin(x);
}
}
__declspec(safe) static inline double asin(const double x) {
if (__have_native_trigonometry) {
return __asin_varying_double(x);
} else if (__math_lib == __math_lib_svml) {
return __svml_asind(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_asin(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline double cos(const double x) {
if (__have_native_trigonometry) {
return __cos_varying_double(x);
}
if (__math_lib == __math_lib_svml) {
return __svml_cosd(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_cos(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double cos(uniform double x) {
if (__have_native_trigonometry) {
return __cos_uniform_double(x);
} else
return __stdlib_cos(x);
}
__declspec(safe) static inline double acos(const double v) {
if (__have_native_trigonometry) {
return __acos_varying_double(v);
} else if (__math_lib == __math_lib_svml) {
return __svml_acosd(v);
} else {
return 1.57079637050628662109375d - asin(v);
}
}
__declspec(safe) static inline uniform double acos(const uniform double v) {
if (__have_native_trigonometry)
return __acos_uniform_double(v);
else
return 1.57079637050628662109375d - asin(v);
}
__declspec(safe) static inline void sincos(double x, varying double *uniform sin_result,
varying double *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_varying_double(x, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
return;
}
if (__math_lib == __math_lib_svml) {
__svml_sincosd(x, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else {
foreach_active(i) {
uniform double sr, cr;
__stdlib_sincos(extract(x, i), (opaque_ptr_t)&sr, (opaque_ptr_t)&cr);
*sin_result = insert(*sin_result, i, sr);
*cos_result = insert(*cos_result, i, cr);
}
}
}
__declspec(safe) static inline void sincos(uniform double x, uniform double *uniform sin_result,
uniform double *uniform cos_result) {
if (__have_native_trigonometry) {
__sincos_uniform_double(x, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
} else
__stdlib_sincos(x, (opaque_ptr_t)sin_result, (opaque_ptr_t)cos_result);
}
__declspec(safe) static inline double tan(double x) {
if (__have_native_trigonometry) {
return __tan_varying_double(x);
} else if (__math_lib == __math_lib_svml) {
return __svml_tand(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_tan(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double tan(uniform double x) {
if (__have_native_trigonometry) {
return __tan_uniform_double(x);
} else
return __stdlib_tan(x);
}
__declspec(safe) static inline double atan(double x) {
if (__have_native_trigonometry) {
return __atan_varying_double(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_atan(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double atan(uniform double x) {
if (__have_native_trigonometry) {
return __atan_uniform_double(x);
} else
return __stdlib_atan(x);
}
__declspec(safe) static inline double atan2(double y, double x) {
if (__have_native_trigonometry) {
return __atan2_varying_double(y, x);
} else if (__math_lib == __math_lib_svml) {
return __svml_atan2d(y, x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_atan2(extract(y, i), extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double atan2(uniform double y, uniform double x) {
if (__have_native_trigonometry) {
return __atan2_uniform_double(y, x);
} else
return __stdlib_atan2(y, x);
}
__declspec(safe) static inline double exp(double x) {
if (__have_native_transcendentals) {
return __exp_varying_double(x);
} else if (__math_lib == __math_lib_svml) {
return __svml_expd(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_exp(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double exp(uniform double x) {
if (__have_native_transcendentals) {
return __exp_uniform_double(x);
} else
return __stdlib_exp(x);
}
__declspec(safe) static inline double log(double x) {
if (__have_native_transcendentals) {
return __log_varying_double(x);
} else if (__math_lib == __math_lib_svml) {
return __svml_logd(x);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_log(extract(x, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double log(uniform double x) {
if (__have_native_transcendentals) {
return __log_uniform_double(x);
} else
return __stdlib_log(x);
}
__declspec(safe) static inline double pow(double a, double b) {
if (__have_native_transcendentals) {
return __pow_varying_double(a, b);
} else if (__math_lib == __math_lib_svml) {
return __svml_powd(a, b);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_pow(extract(a, i), extract(b, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double pow(uniform double a, uniform double b) {
if (__have_native_transcendentals) {
return __pow_uniform_double(a, b);
} else
return __stdlib_pow(a, b);
}
__declspec(safe) static inline double cbrt(double a) {
if (__math_lib == __math_lib_svml) {
return __svml_cbrtd(a);
} else {
double ret;
foreach_active(i) {
uniform double r = __stdlib_cbrt(extract(a, i));
ret = insert(ret, i, r);
}
return ret;
}
}
__declspec(safe) static inline uniform double cbrt(uniform double a) { return __stdlib_cbrt(a); }
///////////////////////////////////////////////////////////////////////////
// half-precision floats
__declspec(safe) static inline uniform float half_to_float(uniform unsigned int16 h) {
if (__have_native_half_converts) {
return __half_to_float_uniform(h);
} else {
// https://gist.github.com/2144712
// Fabian "ryg" Giesen.
static const uniform unsigned int32 shifted_exp = 0x7c00ul << 13; // exponent mask after shift
uniform int32 o = ((int32)(h & 0x7fff)) << 13; // exponent/mantissa bits
uniform unsigned int32 exp = shifted_exp & o; // just the exponent
o += (uniform int32)(127 - 15) << 23; // exponent adjust
// handle exponent special cases
if (exp == shifted_exp) // Inf/NaN?
o += (uniform unsigned int32)(128 - 16) << 23; // extra exp adjust
else if (exp == 0) { // Zero/Denormal?
o += 1ul << 23; // extra exp adjust
o = intbits(floatbits(o) - floatbits(113ul << 23)); // renormalize
}
o |= ((int32)(h & 0x8000)) << 16; // sign bit
return floatbits(o);
}
}
__declspec(safe) static inline float half_to_float(unsigned int16 h) {
if (__have_native_half_converts) {
return __half_to_float_varying((unsigned int16)h);
} else {
// https://gist.github.com/2144712
// Fabian "ryg" Giesen.
const unsigned int32 shifted_exp = 0x7c00ul << 13; // exponent mask after shift
int32 o = ((int32)(h & 0x7ffful)) << 13; // exponent/mantissa bits
unsigned int32 exp = shifted_exp & o; // just the exponent
o += (int32)(127 - 15) << 23; // exponent adjust
int32 infnan_val = o + ((int32)(128 - 16) << 23);
int32 zerodenorm_val = intbits(floatbits(o + (1ul << 23)) - floatbits(113ul << 23));
int32 reg_val = (exp == 0) ? zerodenorm_val : o;
int32 sign_bit = ((int32)(h & 0x8000ul)) << 16;
return floatbits(((exp == shifted_exp) ? infnan_val : reg_val) | sign_bit);
}
}
__declspec(safe) static inline uniform int16 float_to_half(uniform float f) {
if (__have_native_half_converts) {
return __float_to_half_uniform(f);
} else {
// via Fabian "ryg" Giesen.
// https://gist.github.com/2156668
uniform unsigned int32 sign_mask = 0x80000000u;
uniform int32 o;
uniform int32 fint = intbits(f);
uniform int32 sign = fint & sign_mask;
fint ^= sign;
// NOTE all the integer compares in this function can be safely
// compiled into signed compares since all operands are below
// 0x80000000. Important if you want fast straight SSE2 code (since
// there's no unsigned PCMPGTD).
// Inf or NaN (all exponent bits set)
// NaN->qNaN and Inf->Inf
// unconditional assignment here, will override with right value for
// the regular case below.
uniform int32 f32infty = 255ul << 23;
o = (fint > f32infty) ? 0x7e00u : 0x7c00u;
// (De)normalized number or zero
// update fint unconditionally to save the blending; we don't need it
// anymore for the Inf/NaN case anyway.
const uniform unsigned int32 round_mask = ~0xffful;
const uniform int32 magic = 15ul << 23;
const uniform int32 f16infty = 31ul << 23;
uniform int32 fint2 = intbits(floatbits(fint & round_mask) * floatbits(magic)) - round_mask;
fint2 = (fint2 > f16infty) ? f16infty : fint2; // Clamp to signed infinity if overflowed
if (fint < f32infty)
o = fint2 >> 13; // Take the bits!
return (o | (sign >> 16));
}
}
__declspec(safe) static inline int16 float_to_half(float f) {
if (__have_native_half_converts) {
return __float_to_half_varying(f);
} else {
// via Fabian "ryg" Giesen.
// https://gist.github.com/2156668
unsigned int32 sign_mask = 0x80000000u;
int32 o;
int32 fint = intbits(f);
int32 sign = fint & sign_mask;
fint ^= sign;
// NOTE all the integer compares in this function can be safely
// compiled into signed compares since all operands are below
// 0x80000000. Important if you want fast straight SSE2 code (since
// there's no unsigned PCMPGTD).
// Inf or NaN (all exponent bits set)
// NaN->qNaN and Inf->Inf
// unconditional assignment here, will override with right value for
// the regular case below.
int32 f32infty = 255ul << 23;
o = (fint > f32infty) ? 0x7e00u : 0x7c00u;
// (De)normalized number or zero
// update fint unconditionally to save the blending; we don't need it
// anymore for the Inf/NaN case anyway.
const unsigned int32 round_mask = ~0xffful;
const int32 magic = 15ul << 23;
const int32 f16infty = 31ul << 23;
// Shift exponent down, denormalize if necessary.
// NOTE This represents half-float denormals using single precision denormals.
// The main reason to do this is that there's no shift with per-lane variable
// shifts in SSE*, which we'd otherwise need. It has some funky side effects
// though:
// - This conversion will actually respect the FTZ (Flush To Zero) flag in
// MXCSR - if it's set, no half-float denormals will be generated. I'm
// honestly not sure whether this is good or bad. It's definitely interesting.
// - If the underlying HW doesn't support denormals (not an issue with Intel
// CPUs, but might be a problem on GPUs or PS3 SPUs), you will always get
// flush-to-zero behavior. This is bad, unless you're on a CPU where you don't
// care.
// - Denormals tend to be slow. FP32 denormals are rare in practice outside of things
// like recursive filters in DSP - not a typical half-float application. Whether
// FP16 denormals are rare in practice, I don't know. Whatever slow path your HW
// may or may not have for denormals, this may well hit it.
float fscale = floatbits(fint & round_mask) * floatbits(magic);
fscale = min(fscale, floatbits((31ul << 23) - 0x1000ul));
int32 fint2 = intbits(fscale) - round_mask;
if (fint < f32infty)
o = fint2 >> 13; // Take the bits!
return (o | (sign >> 16));
}
}
__declspec(safe) static inline uniform float half_to_float_fast(uniform unsigned int16 h) {
if (__have_native_half_converts) {
return __half_to_float_uniform(h);
} else {
uniform unsigned int32 hs = h & (int32)0x8000u; // Pick off sign bit
uniform unsigned int32 hem = h & (int32)0x7fffu; // Pick off exponent-mantissa bits
uniform unsigned int32 xs = ((unsigned int32)hs) << 16;
uniform unsigned int32 xem = ((unsigned int32)hem) << 13;
xem += 0x38000000; // (127 - 15) << 23
return floatbits(xs | xem);
}
}
__declspec(safe) static inline float half_to_float_fast(unsigned int16 h) {
if (__have_native_half_converts) {
return __half_to_float_varying(h);
} else {
unsigned int32 hs = h & (int32)0x8000u; // Pick off sign bit
unsigned int32 hem = h & (int32)0x7fffu; // Pick off exponent-mantissa bits
unsigned int32 xs = ((unsigned int32)hs) << 16;
unsigned int32 xem = ((unsigned int32)hem) << 13;
return floatbits(xs | (xem + 0x38000000 /* (127 - 15) << 23 */));
}
}
__declspec(safe) static inline uniform int16 float_to_half_fast(uniform float f) {
if (__have_native_half_converts) {
return __float_to_half_uniform(f);
} else {
uniform int32 x = intbits(f);
uniform unsigned int32 xs = x & 0x80000000u; // Pick off sign bit
uniform unsigned int32 xe = x & 0x7F800000u; // Pick off exponent bits
uniform unsigned int32 xm = x & 0x007FFFFFu; // Pick off mantissa bits
uniform unsigned int32 hs = (xs >> 16); // Sign bit
// Exponent unbias the single, then bias the halfp
uniform int32 hes = ((int)(xe >> 23)) - 127 + 15;
uniform unsigned int32 he = (hes << 10); // Exponent
uniform int32 hm = (xm >> 13); // Mantissa
uniform int32 ret = (hs | he | hm);
if (xm & 0x00001000u) // Check for rounding
// Round, might overflow to inf, this is OK
ret += 1u;
return (int16)ret;
}
}
__declspec(safe) static inline int16 float_to_half_fast(float f) {
if (__have_native_half_converts) {
return __float_to_half_varying(f);
} else {
int32 x = intbits(f);
unsigned int32 xs = x & 0x80000000u; // Pick off sign bit
unsigned int32 xe = x & 0x7F800000u; // Pick off exponent bits
unsigned int32 xm = x & 0x007FFFFFu; // Pick off mantissa bits
unsigned int32 hs = (xs >> 16); // Sign bit
// Exponent unbias the single, then bias the halfp
int32 hes = ((int)(xe >> 23)) - 127 + 15;
unsigned int32 he = (hes << 10); // Exponent
int32 hm = (xm >> 13); // Mantissa
int32 ret = (hs | he | hm);
if (xm & 0x00001000u) // Check for rounding
// Round, might overflow to inf, this is OK
ret += 1u;
return (int16)ret;
}
}
__attribute__((unmangled)) unmasked uniform float16 __truncsfhf2(uniform float x) {
return float16bits(float_to_half(x));
}
__attribute__((unmangled)) unmasked uniform float __extendhfsf2(uniform float16 x) { return half_to_float(intbits(x)); }
__attribute__((unmangled)) unmasked uniform float16 __truncdfhf2(uniform double x) {
return float16bits(float_to_half((uniform float)x));
}
__attribute__((unmangled)) unmasked uniform double __extendhfdf2(uniform float16 x) {
return (uniform double)half_to_float(intbits(x));
}
///////////////////////////////////////////////////////////////////////////
// float -> srgb8
// https://gist.github.com/2246678, from Fabian "rygorous" Giesen.
//
// The basic ideas are still the same, only this time, we squeeze
// everything into the table, even the linear part of the range; since we
// are approximating the function as piecewise linear anyway, this is
// fairly easy.
//
// In the exact version of the conversion, any value that produces an
// output float less than 0.5 will be rounded to an integer of
// zero. Inverting the linear part of the transform, we get:
//
// log2(0.5 / (255 * 12.92)) =~ -12.686
//
// which in turn means that any value smaller than about 2^(-12.687) will
// return 0. What this means is that we can adapt the clamping code to
// just clamp to [2^(-13), 1-eps] and we're covered. This means our table
// needs to cover a range of 13 different exponents from -13 to -1.
//
// The table lookup, storage and interpolation works exactly the same way
// as in the code above.
//
// Max error for the whole function (integer-rounded result minus "exact"
// value, as computed in floats using the official formula): 0.544403 at
// 0x3e9f8000
__declspec(safe) static inline int float_to_srgb8(float inval) {
static const uniform unsigned int table[104] = {
0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d, 0x00a7001a,
0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a, 0x010e0033, 0x01280033,
0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033, 0x01dc0067, 0x020f0067, 0x02430067,
0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067, 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce,
0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5, 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112,
0x09940106, 0x0a1700fc, 0x0a9500f2, 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e,
0x0fbc0150, 0x10630143, 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0,
0x182401af, 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,
0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300, 0x31d105b0,
0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401, 0x44c20798, 0x488e071e,
0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559, 0x5e0c0a23, 0x631c0980, 0x67db08f6,
0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,
};
static const uniform unsigned int near_zero = 0x39000000;
static const uniform unsigned int almost_one = 0x3f7fffff;
// Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
inval = max(inval, floatbits(near_zero));
inval = min(inval, floatbits(almost_one));
// Do the table lookup and unpack bias, scale
unsigned int tab = table[(intbits(inval) - 0x39000000u) >> 20];
unsigned int bias = (tab >> 16) << 9;
unsigned int scale = tab & 0xfffful;
// Grab next-highest mantissa bits and perform linear interpolation
unsigned int t = (intbits(inval) >> 12) & 0xff;
return (bias + scale * t) >> 16;
}
__declspec(safe) static inline uniform int float_to_srgb8(uniform float inval) {
static const uniform unsigned int table[104] = {
0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d, 0x00a7001a,
0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a, 0x010e0033, 0x01280033,
0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033, 0x01dc0067, 0x020f0067, 0x02430067,
0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067, 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce,
0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5, 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112,
0x09940106, 0x0a1700fc, 0x0a9500f2, 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e,
0x0fbc0150, 0x10630143, 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0,
0x182401af, 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,
0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300, 0x31d105b0,
0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401, 0x44c20798, 0x488e071e,
0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559, 0x5e0c0a23, 0x631c0980, 0x67db08f6,
0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,
};
static const uniform unsigned int near_zero = 0x39000000;
static const uniform unsigned int almost_one = 0x3f7fffff;
// Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
inval = max(inval, floatbits(near_zero));
inval = min(inval, floatbits(almost_one));
// Do the table lookup and unpack bias, scale
uniform unsigned int tab = table[(intbits(inval) - 0x39000000u) >> 20];
uniform unsigned int bias = (tab >> 16) << 9;
uniform unsigned int scale = tab & 0xfffful;
// Grab next-highest mantissa bits and perform linear interpolation
uniform unsigned int t = (intbits(inval) >> 12) & 0xff;
return (bias + scale * t) >> 16;
}
///////////////////////////////////////////////////////////////////////////
// srgb8 -> float
//
// It uses a polynomial approximation of (x/255+0.055)/1.055)^2.4 for the range
// [9.384;265.71]. The relative error is 5e-4.
// This should be enough since the input is a 8-bit integer anyway.
// Coefficients are from sollya (with some fine-tunning so 0 <= r <= 1.0).
//
// For more precise results a (1KiB) LUT can be used instead.
// Using a LUT is generally (1~2 times) slower on AVX2/AVX-512 targets.
// However, using a LUT might be faster on SSE/Neon with a warm cache.
__declspec(safe) static inline float srgb8_to_float(int inval) {
const float x = clamp(inval, (uniform int)0, (uniform int)255);
float r = 1.0489326014e-13;
r = r * x - 8.6331650162e-11;
r = r * x + 4.2816203915e-8;
r = r * x + 7.7981030699e-6;
r = r * x + 1.3343113823e-4;
r = r * x + 8.7884825188e-4;
const float scale = 1.0 / 255 / 12.92;
return select(x < 10.31475, x * scale, r);
}
__declspec(safe) static inline uniform float srgb8_to_float(uniform int inval) {
const uniform float x = clamp(inval, (uniform int)0, (uniform int)255);
uniform float r = 1.0489326014e-13;
r = r * x - 8.6331650162e-11;
r = r * x + 4.2816203915e-8;
r = r * x + 7.7981030699e-6;
r = r * x + 1.3343113823e-4;
r = r * x + 8.7884825188e-4;
const uniform float scale = 1.0 / 255 / 12.92;
return select(x < 10.31475, x * scale, r);
}
///////////////////////////////////////////////////////////////////////////
// RNG stuff
static inline unsigned int random(varying RNGState *uniform state) {
unsigned int b;
b = ((state->z1 << 6) ^ state->z1) >> 13;
state->z1 = ((state->z1 & 4294967294U) << 18) ^ b;
b = ((state->z2 << 2) ^ state->z2) >> 27;
state->z2 = ((state->z2 & 4294967288U) << 2) ^ b;
b = ((state->z3 << 13) ^ state->z3) >> 21;
state->z3 = ((state->z3 & 4294967280U) << 7) ^ b;
b = ((state->z4 << 3) ^ state->z4) >> 12;
state->z4 = ((state->z4 & 4294967168U) << 13) ^ b;
return (state->z1 ^ state->z2 ^ state->z3 ^ state->z4);
}
static inline uniform unsigned int random(uniform RNGState *uniform state) {
uniform unsigned int b;
b = ((state->z1 << 6) ^ state->z1) >> 13;
state->z1 = ((state->z1 & 4294967294U) << 18) ^ b;
b = ((state->z2 << 2) ^ state->z2) >> 27;
state->z2 = ((state->z2 & 4294967288U) << 2) ^ b;
b = ((state->z3 << 13) ^ state->z3) >> 21;
state->z3 = ((state->z3 & 4294967280U) << 7) ^ b;
b = ((state->z4 << 3) ^ state->z4) >> 12;
state->z4 = ((state->z4 & 4294967168U) << 13) ^ b;
return (state->z1 ^ state->z2 ^ state->z3 ^ state->z4);
}
static inline float frandom(varying RNGState *uniform state) {
unsigned int irand = random(state);
irand &= (1ul << 23) - 1;
return floatbits(0x3F800000 | irand) - 1.0f;
}
static inline uniform float frandom(uniform RNGState *uniform state) {
uniform unsigned int irand = random(state);
irand &= (1ul << 23) - 1;
return floatbits(0x3F800000 | irand) - 1.0f;
}
static inline void seed_rng(varying RNGState *uniform state, unsigned int seed) {
state->z1 = seed;
state->z2 = seed ^ 0xbeeff00d;
state->z3 = ((seed & 0xfffful) << 16) | (seed >> 16);
state->z4 =
(((seed & 0xfful) << 24) | ((seed & 0xff00ul) << 8) | ((seed & 0xff0000ul) >> 8) | (seed & 0xff000000ul) >> 24);
}
static inline void seed_rng(uniform RNGState *uniform state, uniform unsigned int seed) {
state->z1 = seed;
state->z2 = seed ^ 0xbeeff00d;
state->z3 = ((seed & 0xfffful) << 16) | (seed >> 16);
state->z4 =
(((seed & 0xfful) << 24) | ((seed & 0xff00ul) << 8) | ((seed & 0xff0000ul) >> 8) | (seed & 0xff000000ul) >> 24);
}
static inline void fastmath() { __fastmath(); }
///////////////////////////////////////////////////////////////////////////
// saturation arithmetic
static inline uniform int8 saturating_add(uniform int8 a, uniform int8 b) {
if (__have_saturating_arithmetic) {
return __padds_ui8(a, b);
} else {
uniform unsigned int8 a_unsig = a, b_unsig = b;
uniform unsigned int8 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 7) + INT8_MAX;
if ((uniform int8)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline varying int8 saturating_add(varying int8 a, varying int8 b) { return __padds_vi8(a, b); }
static inline uniform int16 saturating_add(uniform int16 a, uniform int16 b) {
if (__have_saturating_arithmetic) {
return __padds_ui16(a, b);
} else {
uniform unsigned int16 a_unsig = a, b_unsig = b;
uniform unsigned int16 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 15) + INT16_MAX;
if ((uniform int16)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline varying int16 saturating_add(varying int16 a, varying int16 b) { return __padds_vi16(a, b); }
static inline uniform int32 saturating_add(uniform int32 a, uniform int32 b) {
if (__have_saturating_arithmetic) {
return __padds_ui32(a, b);
} else {
uniform unsigned int32 a_unsig = a, b_unsig = b;
uniform unsigned int32 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 31) + INT32_MAX;
if ((uniform int32)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline varying int32 saturating_add(varying int32 a, varying int32 b) {
if (__have_saturating_arithmetic) {
return __padds_vi32(a, b);
} else {
varying unsigned int32 a_unsig = a, b_unsig = b;
varying unsigned int32 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 31) + INT32_MAX;
if ((varying int32)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline uniform int64 saturating_add(uniform int64 a, uniform int64 b) {
if (__have_saturating_arithmetic) {
return __padds_ui64(a, b);
} else {
uniform unsigned int64 a_unsig = a, b_unsig = b;
uniform unsigned int64 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 63) + INT64_MAX;
if ((uniform int64)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline varying int64 saturating_add(varying int64 a, varying int64 b) {
if (__have_saturating_arithmetic) {
return __padds_vi64(a, b);
} else {
varying unsigned int64 a_unsig = a, b_unsig = b;
varying unsigned int64 result = a_unsig + b_unsig;
a_unsig = (a_unsig >> 63) + INT64_MAX;
if ((varying int64)((a_unsig ^ b_unsig) | ~(b_unsig ^ result)) >= 0)
result = a_unsig;
return result;
}
}
static inline uniform unsigned int8 saturating_add(uniform unsigned int8 a, uniform unsigned int8 b) {
if (__have_saturating_arithmetic) {
return __paddus_ui8(a, b);
} else {
uniform unsigned int8 result = a + b;
result |= (-(uniform int8)(result < a));
return result;
}
}
static inline varying unsigned int8 saturating_add(varying unsigned int8 a, varying unsigned int8 b) {
return __paddus_vi8(a, b);
}
static inline uniform unsigned int16 saturating_add(uniform unsigned int16 a, uniform unsigned int16 b) {
if (__have_saturating_arithmetic) {
return __paddus_ui16(a, b);
} else {
uniform unsigned int16 result = a + b;
result |= (-(uniform int16)(result < a));
return result;
}
}
static inline varying unsigned int16 saturating_add(varying unsigned int16 a, varying unsigned int16 b) {
return __paddus_vi16(a, b);
}
static inline uniform unsigned int32 saturating_add(uniform unsigned int32 a, uniform unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __paddus_ui32(a, b);
} else {
uniform unsigned int32 result = a + b;
result |= (-(uniform int32)(result < a));
return result;
}
}
static inline varying unsigned int32 saturating_add(varying unsigned int32 a, varying unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __paddus_vi32(a, b);
} else {
varying unsigned int32 result = a + b;
result |= (-(varying int32)(result < a));
return result;
}
}
static inline uniform unsigned int64 saturating_add(uniform unsigned int64 a, uniform unsigned int64 b) {
if (__have_saturating_arithmetic) {
return __paddus_ui64(a, b);
} else {
uniform unsigned int64 result = a + b;
result |= (-(uniform int64)(result < a));
return result;
}
}
static inline varying unsigned int64 saturating_add(varying unsigned int64 a, varying unsigned int64 b) {
if (__have_saturating_arithmetic) {
return __paddus_vi64(a, b);
} else {
varying unsigned int64 result = a + b;
result |= (-(varying int64)(result < a));
return result;
}
}
static inline uniform int8 saturating_sub(uniform int8 a, uniform int8 b) {
if (__have_saturating_arithmetic) {
return __psubs_ui8(a, b);
} else {
uniform unsigned int8 a_unsig = a, b_unsig = b;
uniform unsigned int8 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 7) + INT8_MAX;
if ((uniform int8)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline varying int8 saturating_sub(varying int8 a, varying int8 b) { return __psubs_vi8(a, b); }
static inline uniform int16 saturating_sub(uniform int16 a, uniform int16 b) {
if (__have_saturating_arithmetic) {
return __psubs_ui16(a, b);
} else {
uniform unsigned int16 a_unsig = a, b_unsig = b;
uniform unsigned int16 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 15) + INT16_MAX;
if ((uniform int16)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline varying int16 saturating_sub(varying int16 a, varying int16 b) { return __psubs_vi16(a, b); }
static inline uniform int32 saturating_sub(uniform int32 a, uniform int32 b) {
if (__have_saturating_arithmetic) {
return __psubs_ui32(a, b);
} else {
uniform unsigned int32 a_unsig = a, b_unsig = b;
uniform unsigned int32 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 31) + INT32_MAX;
if ((uniform int32)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline varying int32 saturating_sub(varying int32 a, varying int32 b) {
if (__have_saturating_arithmetic) {
return __psubs_vi32(a, b);
} else {
varying unsigned int32 a_unsig = a, b_unsig = b;
varying unsigned int32 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 31) + INT32_MAX;
if ((varying int32)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline uniform int64 saturating_sub(uniform int64 a, uniform int64 b) {
if (__have_saturating_arithmetic) {
return __psubs_ui64(a, b);
} else {
uniform unsigned int64 a_unsig = a, b_unsig = b;
uniform unsigned int64 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 63) + INT64_MAX;
if ((uniform int64)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline varying int64 saturating_sub(varying int64 a, varying int64 b) {
if (__have_saturating_arithmetic) {
return __psubs_vi64(a, b);
} else {
varying unsigned int64 a_unsig = a, b_unsig = b;
varying unsigned int64 result = a_unsig - b_unsig;
a_unsig = (a_unsig >> 63) + INT64_MAX;
if ((varying int64)((a_unsig ^ b_unsig) & (a_unsig ^ result)) < 0)
result = a_unsig;
return result;
}
}
static inline uniform unsigned int8 saturating_sub(uniform unsigned int8 a, uniform unsigned int8 b) {
if (__have_saturating_arithmetic) {
return __psubus_ui8(a, b);
} else {
uniform unsigned int8 result = a - b;
result &= (-(uniform int8)(result <= a));
return result;
}
}
static inline varying unsigned int8 saturating_sub(varying unsigned int8 a, varying unsigned int8 b) {
return __psubus_vi8(a, b);
}
static inline uniform unsigned int16 saturating_sub(uniform unsigned int16 a, uniform unsigned int16 b) {
if (__have_saturating_arithmetic) {
return __psubus_ui16(a, b);
} else {
uniform unsigned int16 result = a - b;
result &= (-(uniform int16)(result <= a));
return result;
}
}
static inline varying unsigned int16 saturating_sub(varying unsigned int16 a, varying unsigned int16 b) {
return __psubus_vi16(a, b);
}
static inline uniform unsigned int32 saturating_sub(uniform unsigned int32 a, uniform unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __psubus_ui32(a, b);
} else {
uniform unsigned int32 result = a - b;
result &= (-(uniform int32)(result <= a));
return result;
}
}
static inline varying unsigned int32 saturating_sub(varying unsigned int32 a, varying unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __psubus_vi32(a, b);
} else {
varying unsigned int32 result = a - b;
result &= (-(varying int32)(result <= a));
return result;
}
}
static inline uniform unsigned int64 saturating_sub(uniform unsigned int64 a, uniform unsigned int64 b) {
if (__have_saturating_arithmetic) {
return __psubus_ui64(a, b);
} else {
uniform unsigned int64 result = a - b;
result &= (-(uniform int64)(result <= a));
return result;
}
}
static inline varying unsigned int64 saturating_sub(varying unsigned int64 a, varying unsigned int64 b) {
if (__have_saturating_arithmetic) {
return __psubus_vi64(a, b);
} else {
varying unsigned int64 result = a - b;
result &= (-(varying int64)(result <= a));
return result;
}
}
static inline uniform int8 saturating_div(uniform int8 a, uniform int8 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((uniform unsigned int8)a + INT8_MIN));
return a / b;
}
static inline varying int8 saturating_div(varying int8 a, varying int8 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((varying unsigned int8)a + INT8_MIN));
return a / b;
}
static inline uniform int16 saturating_div(uniform int16 a, uniform int16 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((uniform unsigned int16)a + INT16_MIN));
return a / b;
}
static inline varying int16 saturating_div(varying int16 a, varying int16 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((varying unsigned int16)a + INT16_MIN));
return a / b;
}
static inline uniform int32 saturating_div(uniform int32 a, uniform int32 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((uniform unsigned int32)a + INT32_MIN));
return a / b;
}
static inline varying int32 saturating_div(varying int32 a, varying int32 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((varying unsigned int32)a + INT32_MIN));
return a / b;
}
static inline uniform int64 saturating_div(uniform int64 a, uniform int64 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((uniform unsigned int64)a + INT64_MIN));
return a / b;
}
static inline varying int64 saturating_div(varying int64 a, varying int64 b) {
/* Only one way to overflow, so test for and prevent it. */
a += !((b + 1) | ((varying unsigned int64)a + INT64_MIN));
return a / b;
}
static inline uniform unsigned int8 saturating_div(uniform unsigned int8 a, uniform unsigned int8 b) {
/* No overflow possible */
return a / b;
}
static inline varying unsigned int8 saturating_div(varying unsigned int8 a, varying unsigned int8 b) {
/* No overflow possible */
return a / b;
}
static inline uniform unsigned int16 saturating_div(uniform unsigned int16 a, uniform unsigned int16 b) {
/* No overflow possible */
return a / b;
}
static inline varying unsigned int16 saturating_div(varying unsigned int16 a, varying unsigned int16 b) {
/* No overflow possible */
return a / b;
}
static inline uniform unsigned int32 saturating_div(uniform unsigned int32 a, uniform unsigned int32 b) {
/* No overflow possible */
return a / b;
}
static inline varying unsigned int32 saturating_div(varying unsigned int32 a, varying unsigned int32 b) {
/* No overflow possible */
return a / b;
}
static inline uniform unsigned int64 saturating_div(uniform unsigned int64 a, uniform unsigned int64 b) {
/* No overflow possible */
return a / b;
}
static inline varying unsigned int64 saturating_div(varying unsigned int64 a, varying unsigned int64 b) {
/* No overflow possible */
return a / b;
}
static inline uniform int8 saturating_mul(uniform int8 a, uniform int8 b) {
if (__have_saturating_arithmetic) {
return __pmuls_ui8(a, b);
} else {
uniform int16 result = (uniform int16)a * (uniform int16)b;
uniform unsigned int8 result2 = ((uniform unsigned int8)(a ^ b) >> 7) + INT8_MAX;
uniform int8 hi = result >> 8;
uniform int8 lo = result;
if (hi != (lo >> 7))
result = result2;
return result;
}
}
static inline varying int8 saturating_mul(varying int8 a, varying int8 b) {
if (__have_saturating_arithmetic) {
return __pmuls_vi8(a, b);
} else {
varying int16 result = (varying int16)a * (varying int16)b;
varying unsigned int8 result2 = ((varying unsigned int8)(a ^ b) >> 7) + INT8_MAX;
varying int8 hi = result >> 8;
varying int8 lo = result;
if (hi != (lo >> 7))
result = result2;
return result;
}
}
static inline uniform int16 saturating_mul(uniform int16 a, uniform int16 b) {
if (__have_saturating_arithmetic) {
return __pmuls_ui16(a, b);
} else {
uniform int32 result = (uniform int32)a * (uniform int32)b;
uniform unsigned int16 result2 = ((uniform unsigned int16)(a ^ b) >> 15) + INT16_MAX;
uniform int16 hi = result >> 16;
uniform int16 lo = result;
if (hi != (lo >> 15))
result = result2;
return result;
}
}
static inline varying int16 saturating_mul(varying int16 a, varying int16 b) {
if (__have_saturating_arithmetic) {
return __pmuls_vi16(a, b);
} else {
varying int32 result = (varying int32)a * (varying int32)b;
varying unsigned int16 result2 = ((varying unsigned int16)(a ^ b) >> 15) + INT16_MAX;
varying int16 hi = result >> 16;
varying int16 lo = result;
if (hi != (lo >> 15))
result = result2;
return result;
}
}
static inline uniform int32 saturating_mul(uniform int32 a, uniform int32 b) {
if (__have_saturating_arithmetic) {
return __pmuls_ui32(a, b);
} else {
uniform int64 result = (uniform int64)a * (uniform int64)b;
uniform unsigned int32 result2 = ((uniform unsigned int32)(a ^ b) >> 31) + INT32_MAX;
uniform int32 hi = result >> 32;
uniform int32 lo = result;
if (hi != (lo >> 31))
result = result2;
return result;
}
}
static inline varying int32 saturating_mul(varying int32 a, varying int32 b) {
if (__have_saturating_arithmetic) {
return __pmuls_vi32(a, b);
} else {
varying int64 result = (varying int64)a * (varying int64)b;
varying unsigned int32 result2 = ((varying unsigned int32)(a ^ b) >> 31) + INT32_MAX;
varying int32 hi = result >> 32;
varying int32 lo = result;
if (hi != (lo >> 31))
result = result2;
return result;
}
}
static inline uniform unsigned int8 saturating_mul(uniform unsigned int8 a, uniform unsigned int8 b) {
if (__have_saturating_arithmetic) {
return __pmulus_ui8(a, b);
} else {
uniform unsigned int16 result = (uniform unsigned int16)a * (uniform unsigned int16)b;
uniform unsigned int8 hi = result >> 8;
uniform unsigned int8 lo = result;
return lo | -(uniform int8) !!hi;
}
}
static inline varying unsigned int8 saturating_mul(varying unsigned int8 a, varying unsigned int8 b) {
if (__have_saturating_arithmetic) {
return __pmulus_vi8(a, b);
} else {
varying unsigned int16 result = (varying unsigned int16)a * (varying unsigned int16)b;
varying unsigned int8 hi = result >> 8;
varying unsigned int8 lo = result;
return lo | -(varying int8) !!hi;
}
}
static inline uniform unsigned int16 saturating_mul(uniform unsigned int16 a, uniform unsigned int16 b) {
if (__have_saturating_arithmetic) {
return __pmulus_ui16(a, b);
} else {
uniform unsigned int32 result = (uniform unsigned int32)a * (uniform unsigned int32)b;
uniform unsigned int16 hi = result >> 16;
uniform unsigned int16 lo = result;
return lo | -(uniform int16) !!hi;
}
}
static inline varying unsigned int16 saturating_mul(varying unsigned int16 a, varying unsigned int16 b) {
if (__have_saturating_arithmetic) {
return __pmulus_vi16(a, b);
} else {
varying unsigned int32 result = (varying unsigned int32)a * (varying unsigned int32)b;
varying unsigned int16 hi = result >> 16;
varying unsigned int16 lo = result;
return lo | -(varying int16) !!hi;
}
}
static inline uniform unsigned int32 saturating_mul(uniform unsigned int32 a, uniform unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __pmulus_ui32(a, b);
} else {
uniform unsigned int64 result = (uniform unsigned int64)a * (uniform unsigned int64)b;
uniform unsigned int32 hi = result >> 32;
uniform unsigned int32 lo = result;
return lo | -(uniform int32) !!hi;
}
}
static inline varying unsigned int32 saturating_mul(varying unsigned int32 a, varying unsigned int32 b) {
if (__have_saturating_arithmetic) {
return __pmulus_vi32(a, b);
} else {
varying unsigned int64 result = (varying unsigned int64)a * (varying unsigned int64)b;
varying unsigned int32 hi = result >> 32;
varying unsigned int32 lo = result;
return lo | -(varying int32) !!hi;
}
}
static inline uniform int64 saturating_mul(uniform int64 a, uniform int64 b) {
uniform unsigned int64 ret = 0;
uniform int8 sign = (((a > 0) && (b > 0)) || ((a < 0) && (b < 0))) ? 1 : -1;
uniform unsigned int64 a_abs = 0;
uniform unsigned int64 b_abs = 0;
if (a == INT64_MIN)
// Operation "-" is undefined for "INT64_MIN", as it causes overflow.
// But converting INT64_MIN to unsigned type yields the correct result,
// i.e. it will be positive value -INT64_MIN.
// See 6.3.1.3 section in C99 standart for more details (ISPC follows
// C standard, unless it's specifically different in the language).
a_abs = (uniform unsigned int64)INT64_MIN;
else
a_abs = (a > 0) ? a : -a;
if (b == INT64_MIN)
b_abs = (uniform unsigned int64)INT64_MIN;
else
b_abs = (b > 0) ? b : -b;
uniform unsigned int32 a0 = a_abs & 0xFFFFFFFF;
uniform unsigned int32 b0 = b_abs & 0xFFFFFFFF;
uniform unsigned int32 a1 = a_abs >> 32;
uniform unsigned int32 b1 = b_abs >> 32;
if ((a1 != 0) && (b1 != 0)) {
if (sign > 0) {
return INT64_MAX;
} else {
return INT64_MIN;
}
} else if (a1 != 0) {
ret = saturating_add((uniform unsigned int64)saturating_mul(b0, a1) << 32, (uniform unsigned int64)(a0)*b0);
} else if (b1 != 0) {
ret = saturating_add((uniform unsigned int64)saturating_mul(a0, b1) << 32, (uniform unsigned int64)(a0)*b0);
} else {
ret = a_abs * b_abs;
}
if ((sign < 0) && (ret >= (uniform unsigned int64)INT64_MIN)) {
return INT64_MIN;
} else if ((sign > 0) && (ret >= INT64_MAX)) {
return INT64_MAX;
} else {
return ret * sign;
}
}
static inline varying int64 saturating_mul(varying int64 a, varying int64 b) {
varying unsigned int64 ret = 0;
varying int8 sign = (((a > 0) && (b > 0)) || ((a < 0) && (b < 0))) ? 1 : -1;
varying unsigned int64 a_abs = 0;
varying unsigned int64 b_abs = 0;
if (a == INT64_MIN)
// Operation "-" is undefined for "INT64_MIN", as it causes overflow.
// But converting INT64_MIN to unsigned type yields the correct result,
// i.e. it will be positive value -INT64_MIN.
// See 6.3.1.3 section in C99 standart for more details (ISPC follows
// C standard, unless it's specifically different in the language).
a_abs = (varying unsigned int64)INT64_MIN;
else
a_abs = (a > 0) ? a : -a;
if (b == INT64_MIN)
b_abs = (varying unsigned int64)INT64_MIN;
else
b_abs = (b > 0) ? b : -b;
varying unsigned int32 a0 = a_abs & 0xFFFFFFFF;
varying unsigned int32 b0 = b_abs & 0xFFFFFFFF;
varying unsigned int32 a1 = a_abs >> 32;
varying unsigned int32 b1 = b_abs >> 32;
if ((a1 != 0) && (b1 != 0)) {
if (sign > 0) {
return INT64_MAX;
} else {
return INT64_MIN;
}
} else if (a1 != 0) {
ret = saturating_add((varying unsigned int64)saturating_mul(b0, a1) << 32, (varying unsigned int64)(a0)*b0);
} else if (b1 != 0) {
ret = saturating_add((varying unsigned int64)saturating_mul(a0, b1) << 32, (varying unsigned int64)(a0)*b0);
} else {
ret = a_abs * b_abs;
}
if ((sign < 0) && (ret >= (varying unsigned int64)INT64_MIN)) {
return INT64_MIN;
} else if ((sign > 0) && (ret >= INT64_MAX)) {
return INT64_MAX;
} else {
return ret * sign;
}
}
static inline uniform unsigned int64 saturating_mul(uniform unsigned int64 a, uniform unsigned int64 b) {
uniform unsigned int32 a0 = a & 0xFFFFFFFF;
uniform unsigned int32 b0 = b & 0xFFFFFFFF;
uniform unsigned int32 a1 = a >> 32;
uniform unsigned int32 b1 = b >> 32;
if ((a1 != 0) && (b1 != 0)) {
return UINT64_MAX;
} else if (a1 != 0) {
return saturating_add((uniform unsigned int64)saturating_mul(b0, a1) << 32, (uniform unsigned int64)(a0)*b0);
} else if (b1 != 0) {
return saturating_add((uniform unsigned int64)saturating_mul(a0, b1) << 32, (uniform unsigned int64)(a0)*b0);
} else {
return a * b;
}
}
static inline varying unsigned int64 saturating_mul(varying unsigned int64 a, varying unsigned int64 b) {
varying unsigned int32 a0 = a & 0xFFFFFFFF;
varying unsigned int32 b0 = b & 0xFFFFFFFF;
varying unsigned int32 a1 = a >> 32;
varying unsigned int32 b1 = b >> 32;
if ((a1 != 0) && (b1 != 0)) {
return UINT64_MAX;
} else if (a1 != 0) {
return saturating_add((varying unsigned int64)saturating_mul(b0, a1) << 32, (varying unsigned int64)(a0)*b0);
} else if (b1 != 0) {
return saturating_add((varying unsigned int64)saturating_mul(a0, b1) << 32, (varying unsigned int64)(a0)*b0);
} else {
return a * b;
}
}
///////////////////////////////////////////////////////////////////////////
// rdrand
static inline uniform bool rdrand(float *uniform ptr) {
if (__have_native_rand == false)
return false;
else {
uniform int32 irand;
uniform bool success = __rdrand_i32((opaque_ptr_t)&irand);
if (success) {
irand &= (1ul << 23) - 1;
*ptr = floatbits(0x3F800000 | irand) - 1.0f;
}
return success;
}
}
static inline bool rdrand(varying float *uniform ptr) {
if (__have_native_rand == false)
return false;
else {
bool success = false;
foreach_active(index) {
uniform int32 irand;
if (__rdrand_i32((opaque_ptr_t)&irand)) {
// FIXME: it probably would be preferable, here and in the
// following rdrand() function, to do the int->float stuff
// in vector form. However, we need to be careful to not
// clobber any existing already-set values in *ptr with
// inactive lanes here...
irand &= (1ul << 23) - 1;
*ptr = floatbits(0x3F800000 | irand) - 1.0f;
success = true;
}
}
return success;
}
}
static inline bool rdrand(float *ptr) {
if (__have_native_rand == false)
return false;
else {
float *uniform ptrs[programCount];
ptrs[programIndex] = ptr;
bool success = false;
foreach_active(index) {
uniform int32 irand;
if (__rdrand_i32((opaque_ptr_t)&irand)) {
irand &= (1ul << 23) - 1;
*ptrs[index] = floatbits(0x3F800000 | irand) - 1.0f;
success = true;
}
}
return success;
}
}
static inline uniform bool rdrand(int16 *uniform ptr) {
if (__have_native_rand == false)
return false;
else
return __rdrand_i16((uniform int8 * uniform) ptr);
}
static inline bool rdrand(varying int16 *uniform ptr) {
if (__have_native_rand == false)
return false;
else {
bool success = false;
foreach_active(index) {
uniform int16 irand;
if (__rdrand_i16((opaque_ptr_t)&irand)) {
*ptr = irand;
success = true;
}
}
return success;
}
}
static inline bool rdrand(int16 *ptr) {
if (__have_native_rand == false)
return false;
else {
int16 *uniform ptrs[programCount];
ptrs[programIndex] = ptr;
bool success = false;
foreach_active(index) {
uniform int16 irand;
if (__rdrand_i16((opaque_ptr_t)&irand)) {
*ptrs[index] = irand;
success = true;
}
}
return success;
}
}
static inline uniform bool rdrand(int32 *uniform ptr) {
if (__have_native_rand == false)
return false;
else
return __rdrand_i32((uniform int8 * uniform) ptr);
}
static inline bool rdrand(varying int32 *uniform ptr) {
if (__have_native_rand == false)
return false;
else {
bool success = false;
foreach_active(index) {
uniform int32 irand;
if (__rdrand_i32((opaque_ptr_t)&irand)) {
*ptr = irand;
success = true;
}
}
return success;
}
}
static inline bool rdrand(int32 *ptr) {
if (__have_native_rand == false)
return false;
else {
int32 *uniform ptrs[programCount];
ptrs[programIndex] = ptr;
bool success = false;
foreach_active(index) {
uniform int32 irand;
if (__rdrand_i32((opaque_ptr_t)&irand)) {
*ptrs[index] = irand;
success = true;
}
}
return success;
}
}
static inline uniform bool rdrand(int64 *uniform ptr) {
if (__have_native_rand == false)
return false;
else
return __rdrand_i64((uniform int8 * uniform) ptr);
}
static inline bool rdrand(varying int64 *uniform ptr) {
if (__have_native_rand == false)
return false;
else {
bool success = false;
foreach_active(index) {
uniform int64 irand;
if (__rdrand_i64((opaque_ptr_t)&irand)) {
*ptr = irand;
success = true;
}
}
return success;
}
}
static inline bool rdrand(int64 *ptr) {
if (__have_native_rand == false)
return false;
else {
int64 *uniform ptrs[programCount];
ptrs[programIndex] = ptr;
bool success = false;
foreach_active(index) {
uniform int64 irand;
if (__rdrand_i64((opaque_ptr_t)&irand)) {
*ptrs[index] = irand;
success = true;
}
}
return success;
}
}
///////////////////////////////////////////////////////////////////////////
// Saturating int8/int16 ops
__declspec(safe) static unmasked inline unsigned int8 avg_up(unsigned int8 a, unsigned int8 b) {
return __avg_up_uint8(a, b);
}
__declspec(safe) static unmasked inline int8 avg_up(int8 a, int8 b) { return __avg_up_int8(a, b); }
__declspec(safe) static unmasked inline unsigned int16 avg_up(unsigned int16 a, unsigned int16 b) {
return __avg_up_uint16(a, b);
}
__declspec(safe) static unmasked inline int16 avg_up(int16 a, int16 b) { return __avg_up_int16(a, b); }
__declspec(safe) static unmasked inline unsigned int8 avg_down(unsigned int8 a, unsigned int8 b) {
return __avg_down_uint8(a, b);
}
__declspec(safe) static unmasked inline int8 avg_down(int8 a, int8 b) { return __avg_down_int8(a, b); }
__declspec(safe) static unmasked inline unsigned int16 avg_down(unsigned int16 a, unsigned int16 b) {
return __avg_down_uint16(a, b);
}
__declspec(safe) static unmasked inline int16 avg_down(int16 a, int16 b) { return __avg_down_int16(a, b); }
///////////////////////////////////////////////////////////////////////////
// Assume uniform/varying ops
__declspec(safe) static inline void assume(uniform bool test) {
__do_assume_uniform(test);
return;
}
///////////////////////////////////////////////////////////////////////////
// Dot product and accumulate
// Multiply groups of 4 adjacent pairs of unsigned 8-bit integers in a with corresponding signed
// 8-bit integers in b, producing 4 intermediate signed 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc, and return the result.
__declspec(safe) static inline varying int32 dot4add_u8i8packed(varying uint32 a, varying uint32 b, varying int32 acc) {
if (__have_intel_vnni || __have_arm_i8mm) {
return __dot4add_u8i8packed(a, b, acc);
} else {
int16 tmp1 = (int16)((a >> 24) & 0xFF) * (int16)((int8)((b >> 24) & 0xFF));
int16 tmp2 = (int16)((a >> 16) & 0xFF) * (int16)((int8)((b >> 16) & 0xFF));
int16 tmp3 = (int16)((a >> 8) & 0xFF) * (int16)((int8)((b >> 8) & 0xFF));
int16 tmp4 = (int16)(a & 0xFF) * (int16)((int8)(b & 0xFF));
return (int32)tmp1 + (int32)tmp2 + (int32)tmp3 + (int32)tmp4 + acc;
}
}
// Multiply groups of 4 adjacent pairs of unsigned 8-bit integers in a with corresponding signed
// 8-bit integers in b, producing 4 intermediate signed 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc using signed saturation, and return the result.
__declspec(safe) static inline varying int32 dot4add_u8i8packed_sat(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni) {
return __dot4add_u8i8packed_sat(a, b, acc);
} else {
int16 tmp1 = (int16)((a >> 24) & 0xFF) * (int16)((int8)((b >> 24) & 0xFF));
int16 tmp2 = (int16)((a >> 16) & 0xFF) * (int16)((int8)((b >> 16) & 0xFF));
int16 tmp3 = (int16)((a >> 8) & 0xFF) * (int16)((int8)((b >> 8) & 0xFF));
int16 tmp4 = (int16)(a & 0xFF) * (int16)((int8)(b & 0xFF));
int32 tmp1234 = ((int32)tmp1) + ((int32)tmp2) + ((int32)tmp3) + ((int32)tmp4);
return saturating_add(tmp1234, acc);
}
}
// Multiply groups of 4 adjacent pairs of unsigned 8-bit integers in a with corresponding unsigned
// 8-bit integers in b, producing 4 intermediate unsigned 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc, and return the result.
__declspec(safe) static inline varying uint32 dot4add_u8u8packed(varying uint32 a, varying uint32 b,
varying uint32 acc) {
if (__have_arm_dot_product || __have_intel_vnni_int8) {
return __dot4add_u8u8packed(a, b, acc);
} else {
int16 tmp1 = (int16)((a >> 24) & 0xFF) * (int16)((b >> 24) & 0xFF);
int16 tmp2 = (int16)((a >> 16) & 0xFF) * (int16)((b >> 16) & 0xFF);
int16 tmp3 = (int16)((a >> 8) & 0xFF) * (int16)((b >> 8) & 0xFF);
int16 tmp4 = (int16)(a & 0xFF) * (int16)(b & 0xFF);
return (uint32)tmp1 + (uint32)tmp2 + (uint32)tmp3 + (uint32)tmp4 + acc;
}
}
// Multiply groups of 4 adjacent pairs of unsigned 8-bit integers in a with corresponding unsigned
// 8-bit integers in b, producing 4 intermediate unsigned 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc using unsigned saturation, and return the result.
__declspec(safe) static inline varying uint32 dot4add_u8u8packed_sat(varying uint32 a, varying uint32 b,
varying uint32 acc) {
if (__have_intel_vnni_int8) {
return __dot4add_u8u8packed_sat(a, b, acc);
} else {
uint16 tmp1 = (uint16)((a >> 24) & 0xFF) * (uint16)((b >> 24) & 0xFF);
uint16 tmp2 = (uint16)((a >> 16) & 0xFF) * (uint16)((b >> 16) & 0xFF);
uint16 tmp3 = (uint16)((a >> 8) & 0xFF) * (uint16)((b >> 8) & 0xFF);
uint16 tmp4 = (uint16)(a & 0xFF) * (uint16)(b & 0xFF);
uint32 tmp1234 = ((uint32)tmp1) + ((uint32)tmp2) + ((uint32)tmp3) + ((uint32)tmp4);
return saturating_add(tmp1234, acc);
}
}
// Multiply groups of 4 adjacent pairs of unsigned 8-bit integers in a with corresponding signed
// 8-bit integers in b, producing 4 intermediate signed 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc, and return the result.
__declspec(safe) static inline varying int32 dot4add_i8i8packed(varying uint32 a, varying uint32 b, varying int32 acc) {
if (__have_arm_dot_product || __have_intel_vnni_int8) {
return __dot4add_i8i8packed(a, b, acc);
} else {
int16 tmp1 = (int16)((int8)((a >> 24) & 0xFF)) * (int16)((int8)((b >> 24) & 0xFF));
int16 tmp2 = (int16)((int8)((a >> 16) & 0xFF)) * (int16)((int8)((b >> 16) & 0xFF));
int16 tmp3 = (int16)((int8)((a >> 8) & 0xFF)) * (int16)((int8)((b >> 8) & 0xFF));
int16 tmp4 = (int16)((int8)(a & 0xFF)) * (int16)((int8)(b & 0xFF));
return (int32)tmp1 + (int32)tmp2 + (int32)tmp3 + (int32)tmp4 + acc;
}
}
// Multiply groups of 4 adjacent pairs of signed 8-bit integers in a with corresponding signed
// 8-bit integers in b, producing 4 intermediate signed 16-bit results.
// Sum these 4 results with the corresponding 32-bit integer in acc using signed saturation, and return the result.
__declspec(safe) static inline varying int32 dot4add_i8i8packed_sat(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni_int8) {
return __dot4add_i8i8packed_sat(a, b, acc);
} else {
int16 tmp1 = (int16)((a >> 24) & 0xFF) * (int16)((b >> 24) & 0xFF);
int16 tmp2 = (int16)((a >> 16) & 0xFF) * (int16)((b >> 16) & 0xFF);
int16 tmp3 = (int16)((a >> 8) & 0xFF) * (int16)((b >> 8) & 0xFF);
int16 tmp4 = (int16)(a & 0xFF) * (int16)(b & 0xFF);
int32 tmp1234 = ((int32)tmp1) + ((int32)tmp2) + ((int32)tmp3) + ((int32)tmp4);
return saturating_add(tmp1234, acc);
}
}
// Multiply groups of 2 adjacent pairs of signed 16-bit integers in a
// with corresponding 16-bit integers in b, producing 2 intermediate signed 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in src, and return the result.
__declspec(safe) static inline varying int32 dot2add_i16i16packed(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni) {
return __dot2add_i16i16packed(a, b, acc);
} else {
int32 tmp1 = (int32)((int16)((a >> 16) & 0xFFFF)) * (int32)((int16)((b >> 16) & 0xFFFF));
int32 tmp2 = (int32)((int16)(a & 0xFFFF)) * (int32)((int16)(b & 0xFFFF));
return tmp1 + tmp2 + acc;
}
}
// Multiply groups of 2 adjacent pairs of signed 16-bit integers in a
// with corresponding 16-bit integers in b, producing 2 intermediate signed 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in src using signed saturation, and return the result.
__declspec(safe) static inline varying int32 dot2add_i16i16packed_sat(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni) {
return __dot2add_i16i16packed_sat(a, b, acc);
} else {
int32 tmp1 = (int32)((int16)((a >> 16) & 0xFFFF)) * (int32)((int16)((b >> 16) & 0xFFFF));
int32 tmp2 = (int32)((int16)(a & 0xFFFF)) * (int32)((int16)(b & 0xFFFF));
return saturating_add(saturating_add(tmp1, tmp2), acc);
}
}
// Multiply groups of 2 adjacent pairs of unsigned 16-bit integers in a
// with corresponding unsigned 16-bit integers in b, producing 2 intermediate unsigned 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in acc, and return the result.
__declspec(safe) static inline varying uint32 dot2add_u16u16packed(varying uint32 a, varying uint32 b,
varying uint32 acc) {
if (__have_intel_vnni_int8) {
return __dot2add_u16u16packed(a, b, acc);
} else {
// Zero-extend both operands as per the pseudocode (UU version)
uint32 tmp1 = (uint32)((a >> 16) & 0xFFFF) * (uint32)((b >> 16) & 0xFFFF);
uint32 tmp2 = (uint32)(a & 0xFFFF) * (uint32)(b & 0xFFFF);
return acc + tmp1 + tmp2;
}
}
// Multiply groups of 2 adjacent pairs of unsigned 16-bit integers in a
// with corresponding unsigned 16-bit integers in b, producing 2 intermediate unsigned 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in acc using unsigned saturation, and return the result.
__declspec(safe) static inline varying uint32 dot2add_u16u16packed_sat(varying uint32 a, varying uint32 b,
varying uint32 acc) {
if (__have_intel_vnni_int8) {
return __dot2add_u16u16packed_sat(a, b, acc);
} else {
// Zero-extend both operands as per the pseudocode (UU version)
uint32 tmp1 = (uint32)((a >> 16) & 0xFFFF) * (uint32)((b >> 16) & 0xFFFF);
uint32 tmp2 = (uint32)(a & 0xFFFF) * (uint32)(b & 0xFFFF);
// Use unsigned saturation for UU version
return saturating_add(saturating_add(acc, tmp1), tmp2);
}
}
// Multiply groups of 2 adjacent pairs of unsigned 16-bit integers in a
// with corresponding signed 16-bit integers in b, producing 2 intermediate signed 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in acc, and return the result.
__declspec(safe) static inline varying int32 dot2add_u16i16packed(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni_int8) {
return __dot2add_u16i16packed(a, b, acc);
} else {
// Zero-extend operand a, sign-extend operand b as per pseudocode (US version)
int32 tmp1 = (int32)((uint16)((a >> 16) & 0xFFFF)) * (int32)((int16)((b >> 16) & 0xFFFF));
int32 tmp2 = (int32)((uint16)(a & 0xFFFF)) * (int32)((int16)(b & 0xFFFF));
return acc + tmp1 + tmp2;
}
}
// Multiply groups of 2 adjacent pairs of unsigned 16-bit integers in a
// with corresponding signed 16-bit integers in b, producing 2 intermediate signed 32-bit results.
// Sum these 2 results with the corresponding 32-bit integer in acc using signed saturation, and return the result.
__declspec(safe) static inline varying int32 dot2add_u16i16packed_sat(varying uint32 a, varying uint32 b,
varying int32 acc) {
if (__have_intel_vnni_int8) {
return __dot2add_u16i16packed_sat(a, b, acc);
} else {
// Zero-extend operand a, sign-extend operand b as per pseudocode (US version)
int32 tmp1 = (int32)((uint16)((a >> 16) & 0xFFFF)) * (int32)((int16)((b >> 16) & 0xFFFF));
int32 tmp2 = (int32)((uint16)(a & 0xFFFF)) * (int32)((int16)(b & 0xFFFF));
// Use signed saturation for US version
return saturating_add(saturating_add(acc, tmp1), tmp2);
}
}
|