/usr/include/OpenImageIO/simd.h is in libopenimageio-dev 1.7.17~dfsg0-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
Copyright (c) 2014 Larry Gritz et al.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Sony Pictures Imageworks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/// @file simd.h
///
/// @brief Classes for SIMD processing.
///
/// Nice references for all the Intel intrinsics (SSE*, AVX*, etc.):
/// https://software.intel.com/sites/landingpage/IntrinsicsGuide/
///
/// It helped me a lot to peruse the source of these packages:
/// Syrah: https://github.com/boulos/syrah
/// Embree: https://github.com/embree
/// Vectorial: https://github.com/scoopr/vectorial
///
/// To find out which CPU features you have:
/// Linux: cat /proc/cpuinfo
/// OSX: sysctl machdep.cpu.features
///
/// Additional web resources:
/// http://www.codersnotes.com/notes/maths-lib-2016/
#pragma once
#ifndef OIIO_SIMD_H
#define OIIO_SIMD_H 1
#include <OpenImageIO/dassert.h>
#include <OpenImageIO/missing_math.h>
#include <OpenImageIO/platform.h>
#include <OpenEXR/ImathVec.h>
#include <OpenEXR/ImathMatrix.h>
#include <algorithm>
#include <cstring>
//////////////////////////////////////////////////////////////////////////
// Sort out which SIMD capabilities we have and set definitions
// appropriately. This is mostly for internal (within this file) use,
// but client applications using this header may find a few of the macros
// we define to be useful:
//
// OIIO_SIMD : Will be 0 if no hardware SIMD support is specified. If SIMD
// hardware is available, this will hold the width in number of
// float SIMD "lanes" of widest SIMD registers available. For
// example, OIIO_SIMD will be 4 if float4/int4/bool4 are
// hardware accelerated, 8 if float8/int8/bool8 are accelerated,
// etc. Using SIMD classes wider than this should work (will be
// emulated with narrower SIMD or scalar operations), but is not
// expected to have high performance.
// OIIO_SIMD_SSE : if Intel SSE is supported, this will be nonzero,
// specifically 2 for SSE2, 3 for SSSE3, 4 for SSE4.1 or
// higher (including AVX).
// OIIO_SIMD_AVX : If Intel AVX is supported, this will be nonzero, and
// specifically 1 for AVX (1.0), 2 for AVX2, 512 for AVX512f.
// OIIO_SIMD_NEON : If ARM NEON is supported, this will be nonzero.
// OIIO_SIMD_MAX_SIZE : holds the width in bytes of the widest SIMD
// available (generally will be OIIO_SIMD*4).
// OIIO_SIMD4_ALIGN : macro for best alignment of 4-wide SIMD values in mem.
// OIIO_SIMD8_ALIGN : macro for best alignment of 8-wide SIMD values in mem.
// OIIO_SIMD_HAS_MATRIX4 : nonzero if matrix44 is defined
// OIIO_SIMD_HAS_FLOAT8 : nonzero if float8, int8, bool8 are defined
#if (defined(__SSE2__) || (_MSC_VER >= 1300 && !_M_CEE_PURE)) && !defined(OIIO_NO_SSE)
# include <immintrin.h>
# if (defined(__i386__) || defined(__x86_64__)) && (OIIO_GNUC_VERSION > 40400 || __clang__)
# include <x86intrin.h>
# endif
# if (defined(__SSE4_1__) || defined(__SSE4_2__))
# define OIIO_SIMD_SSE 4
/* N.B. We consider both SSE4.1 and SSE4.2 to be "4". There are a few
* instructions specific to 4.2, but they are all related to string
* comparisons and CRCs, which don't currently seem relevant to OIIO,
* so for simplicity, we sweep this difference under the rug.
*/
# elif defined(__SSSE3__)
# define OIIO_SIMD_SSE 3
/* N.B. We only use OIIO_SIMD_SSE = 3 when fully at SSSE3. In theory,
* there are a few older architectures that are SSE3 but not SSSE3,
* and this simplification means that these particular old platforms
* will only get SSE2 goodness out of our code. So be it. Anybody who
* cares about performance is probably using a 64 bit machine that's
* SSE 4.x or AVX by now.
*/
# else
# define OIIO_SIMD_SSE 2
# endif
# define OIIO_SIMD 4
# define OIIO_SIMD_MAX_SIZE_BYTES 16
# define OIIO_SIMD_ALIGN OIIO_ALIGN(16)
# define OIIO_SIMD4_ALIGN OIIO_ALIGN(16)
# define OIIO_SSE_ALIGN OIIO_ALIGN(16)
#endif
#if defined(__AVX__) && !defined(OIIO_NO_AVX)
// N.B. Any machine with AVX will also have SSE
# if defined(__AVX2__) && !defined(OIIO_NO_AVX2)
# define OIIO_SIMD_AVX 2
# else
# define OIIO_SIMD_AVX 1
# endif
# undef OIIO_SIMD
# define OIIO_SIMD 8
# undef OIIO_SIMD_MAX_SIZE_BYTES
# define OIIO_SIMD_MAX_SIZE_BYTES 32
# define OIIO_SIMD8_ALIGN OIIO_ALIGN(32)
# define OIIO_AVX_ALIGN OIIO_ALIGN(32)
# if defined(__AVX512__) && !defined(OIIO_NO_AVX512)
# define OIIO_SIMD_AVX 512
# undef OIIO_SIMD_MAX_SIZE_BYTES
# define OIIO_SIMD_MAX_SIZE_BYTES 64
# define OIIO_SIMD16_ALIGN OIIO_ALIGN(64)
# define OIIO_AVX512_ALIGN OIIO_ALIGN(64)
# endif
#endif
#if defined(__FMA__)
# define OIIO_FMA_ENABLED 1
#endif
// FIXME Future: support ARM Neon
// Uncomment this when somebody with Neon can verify it works
#if 0 && defined(__ARM_NEON__) && !defined(OIIO_NO_NEON)
# include <arm_neon.h>
# define OIIO_SIMD 1
# define OIIO_SIMD_NEON 1
# define OIIO_SIMD_MAX_SIZE_BYTES 16
# define OIIO_SIMD_ALIGN OIIO_ALIGN(16)
# define OIIO_SIMD4_ALIGN OIIO_ALIGN(16)
# define OIIO_SSE_ALIGN OIIO_ALIGN(16)
#endif
#ifndef OIIO_SIMD
// No SIMD available
# define OIIO_SIMD 0
# define OIIO_SIMD_ALIGN
# define OIIO_SIMD4_ALIGN
# define OIIO_SIMD8_ALIGN
# define OIIO_SIMD_MAX_SIZE_BYTES 16
#endif
// General features that client apps may want to test for, for conditional
// compilation. Will add to this over time as needed. Note that just
// because a feature is present doesn't mean it's fast -- HAS_FLOAT8 means
// the float8 class (and friends) are in this version of simd.h, but that's
// different from OIIO_SIMD >= 8, which means it's supported in hardware.
#define OIIO_SIMD_HAS_MATRIX4 1 /* matrix44 defined */
#define OIIO_SIMD_HAS_FLOAT8 1 /* float8, int8, bool8 defined */
OIIO_NAMESPACE_BEGIN
namespace simd {
//////////////////////////////////////////////////////////////////////////
// Forward declarations of our main SIMD classes
class bool4;
class int4;
class float4;
class float3;
class matrix44;
typedef bool4 mask4; // old name
class bool8;
class int8;
class float8;
//////////////////////////////////////////////////////////////////////////
// Template magic to determine the raw SIMD types involved, and other
// things helpful for metaprogramming.
template <typename T, int N> struct simd_raw_t { struct type { T val[N]; }; };
template <int N> struct simd_bool_t { struct type { int val[N]; }; };
#if OIIO_SIMD_SSE
template<> struct simd_raw_t<int,4> { typedef __m128i type; };
template<> struct simd_raw_t<float,4> { typedef __m128 type; };
template<> struct simd_bool_t<4> { typedef __m128 type; };
#endif
#if OIIO_SIMD_AVX
template<> struct simd_raw_t<int,8> { typedef __m256i type; };
template<> struct simd_raw_t<float,8> { typedef __m256 type; };
template<> struct simd_bool_t<8> { typedef __m256 type; };
#endif
#if OIIO_SIMD_AVX >= 512
template<> struct simd_raw_t<int,16> { typedef __m512i type; };
template<> struct simd_raw_t<float,16> { typedef __m512 type; };
template<> struct simd_bool_t<16> { typedef __m512 type; };
#endif
#if OIIO_SIMD_NEON
template<> struct simd_raw_t<int,4> { typedef int32x4 type; };
template<> struct simd_raw_t<float,4> { typedef float32x4_t type; };
template<> struct simd_bool_t<4> { typedef int32x4 type; };
#endif
/// Template to retrieve the vector type from the scalar. For example,
/// simd::VecType<int,4> will be float4.
template<typename T,int elements> struct VecType {};
template<> struct VecType<int,1> { typedef int type; };
template<> struct VecType<float,1> { typedef float type; };
template<> struct VecType<int,4> { typedef int4 type; };
template<> struct VecType<float,3> { typedef float3 type; };
template<> struct VecType<bool,4> { typedef bool4 type; };
template<> struct VecType<int,8> { typedef int8 type; };
template<> struct VecType<float,8> { typedef float8 type; };
template<> struct VecType<bool,8> { typedef bool8 type; };
/// Template to retrieve the SIMD size of a SIMD type. Rigged to be 1 for
/// anything but our SIMD types.
template<typename T> struct SimdSize { static const int size = 1; };
template<> struct SimdSize<int4> { static const int size = 4; };
template<> struct SimdSize<float4> { static const int size = 4; };
template<> struct SimdSize<float3> { static const int size = 4; };
template<> struct SimdSize<bool4> { static const int size = 4; };
template<> struct SimdSize<int8> { static const int size = 8; };
template<> struct SimdSize<float8> { static const int size = 8; };
template<> struct SimdSize<bool8> { static const int size = 8; };
/// Template to retrieve the number of elements size of a SIMD type. Rigged
/// to be 1 for anything but our SIMD types.
template<typename T> struct SimdElements { static const int size = SimdSize<T>::size; };
template<> struct SimdElements<float3> { static const int size = 3; };
/// Template giving a printable name for each type
template<typename T> struct SimdTypeName { static const char *name() { return "unknown"; } };
template<> struct SimdTypeName<float4> { static const char *name() { return "float4"; } };
template<> struct SimdTypeName<int4> { static const char *name() { return "int4"; } };
template<> struct SimdTypeName<bool4> { static const char *name() { return "bool4"; } };
template<> struct SimdTypeName<float8> { static const char *name() { return "float8"; } };
template<> struct SimdTypeName<int8> { static const char *name() { return "int8"; } };
template<> struct SimdTypeName<bool8> { static const char *name() { return "bool8"; } };
//////////////////////////////////////////////////////////////////////////
// Macros helpful for making static constants in code.
# define OIIO_SIMD_FLOAT4_CONST(name,val) \
static const OIIO_SIMD4_ALIGN float name[4] = { (val), (val), (val), (val) }
# define OIIO_SIMD_FLOAT4_CONST4(name,v0,v1,v2,v3) \
static const OIIO_SIMD4_ALIGN float name[4] = { (v0), (v1), (v2), (v3) }
# define OIIO_SIMD_INT4_CONST(name,val) \
static const OIIO_SIMD4_ALIGN int name[4] = { (val), (val), (val), (val) }
# define OIIO_SIMD_INT4_CONST4(name,v0,v1,v2,v3) \
static const OIIO_SIMD4_ALIGN int name[4] = { (v0), (v1), (v2), (v3) }
# define OIIO_SIMD_UINT4_CONST(name,val) \
static const OIIO_SIMD4_ALIGN uint32_t name[4] = { (val), (val), (val), (val) }
# define OIIO_SIMD_UINT4_CONST4(name,v0,v1,v2,v3) \
static const OIIO_SIMD4_ALIGN uint32_t name[4] = { (v0), (v1), (v2), (v3) }
# define OIIO_SIMD_FLOAT8_CONST(name,val) \
static const OIIO_SIMD8_ALIGN float name[8] = { (val), (val), (val), (val), \
(val), (val), (val), (val) }
# define OIIO_SIMD_FLOAT8_CONST8(name,v0,v1,v2,v3,v4,v5,v6,v7) \
static const OIIO_SIMD8_ALIGN float name[8] = { (v0), (v1), (v2), (v3), \
(v4), (v5), (v6), (v7) }
# define OIIO_SIMD_INT8_CONST(name,val) \
static const OIIO_SIMD8_ALIGN int name[8] = { (val), (val), (val), (val), \
(val), (val), (val), (val) }
# define OIIO_SIMD_INT8_CONST8(name,v0,v1,v2,v3,v4,v5,v6,v7) \
static const OIIO_SIMD8_ALIGN int name[8] = { (v0), (v1), (v2), (v3), \
(v4), (v5), (v6), (v7) }
# define OIIO_SIMD_UINT8_CONST(name,val) \
static const OIIO_SIMD8_ALIGN uint32_t name[8] = { (val), (val), (val), (val), \
(val), (val), (val), (val) }
# define OIIO_SIMD_UINT8_CONST8(name,v0,v1,v2,v3,v4,v5,v6,v7) \
static const OIIO_SIMD8_ALIGN uint32_t name[8] = { (v0), (v1), (v2), (v3), \
(v4), (v5), (v6), (v7) }
//////////////////////////////////////////////////////////////////////////
// Some macros just for use in this file (#undef-ed at the end) making
// it more succinct to express per-element operations.
#define SIMD_DO(x) for (int i = 0; i < elements; ++i) x
#define SIMD_CONSTRUCT(x) for (int i = 0; i < elements; ++i) m_val[i] = (x)
#define SIMD_CONSTRUCT_PAD(x) for (int i = 0; i < elements; ++i) m_val[i] = (x); \
for (int i = elements; i < paddedelements; ++i) m_val[i] = 0
#define SIMD_RETURN(T,x) T r; for (int i = 0; i < r.elements; ++i) r[i] = (x); return r
#define SIMD_RETURN_REDUCE(T,init,op) T r = init; for (int i = 0; i < v.elements; ++i) op; return r
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// The public declarations of the main SIMD classes follow: boolN, intN,
// floatN, matrix44.
//
// These class declarations are intended to be brief and self-documenting,
// and give all the information that users or client applications need to
// know to use these classes.
//
// No implementations are given inline except for the briefest, completely
// generic methods that don't have any architecture-specific overloads.
// After the class defintions, there will be an immense pile of full
// implementation definitions, which casual users are not expected to
// understand.
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/// bool4: An 4-vector whose elements act mostly like bools, accelerated by
/// SIMD instructions when available. This is what is naturally produced by
/// SIMD comparison operators on the float4 and int4 types.
class bool4 {
public:
static const char* type_name() { return "bool4"; }
typedef bool value_t; ///< Underlying equivalent scalar value type
enum { elements = 4 }; ///< Number of scalar elements
enum { paddedelements = 4 }; ///< Number of scalar elements for full pad
enum { bits = elements*32 }; ///< Total number of bits
typedef simd_bool_t<4>::type simd_t; ///< the native SIMD type used
/// Default constructor (contents undefined)
bool4 () { }
/// Construct from a single value (store it in all slots)
bool4 (bool a) { load(a); }
explicit bool4 (const bool *a);
/// Construct from 4 values
bool4 (bool a, bool b, bool c, bool d) { load (a, b, c, d); }
/// Copy construct from another bool4
bool4 (const bool4 &other) { m_simd = other.m_simd; }
/// Construct from a SIMD int (is each element nonzero?)
bool4 (const int4 &i);
/// Construct from the underlying SIMD type
bool4 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Set all components to false
void clear ();
/// Return a bool4 the is 'false' for all values
static const bool4 False ();
/// Return a bool4 the is 'true' for all values
static const bool4 True ();
/// Assign one value to all components
const bool4 & operator= (bool a) { load(a); return *this; }
/// Assignment of another bool4
const bool4 & operator= (const bool4 & other);
/// Component access (get)
int operator[] (int i) const;
/// Component access (set).
void setcomp (int i, bool value);
/// Component access (set).
/// NOTE: avoid this unsafe construct. It will go away some day.
int& operator[] (int i);
/// Helper: load a single value into all components.
void load (bool a);
/// Helper: load separate values into each component.
void load (bool a, bool b, bool c, bool d);
/// Helper: store the values into memory as bools.
void store (bool *values) const;
/// Store the first n values into memory.
void store (bool *values, int n) const;
/// Logical/bitwise operators, component-by-component
friend bool4 operator! (const bool4& a);
friend bool4 operator& (const bool4& a, const bool4& b);
friend bool4 operator| (const bool4& a, const bool4& b);
friend bool4 operator^ (const bool4& a, const bool4& b);
friend bool4 operator~ (const bool4& a);
friend const bool4& operator&= (bool4& a, const bool4& b);
friend const bool4& operator|= (bool4& a, const bool4& b);
friend const bool4& operator^= (bool4& a, const bool4& b);
/// Comparison operators, component by component
friend bool4 operator== (const bool4& a, const bool4& b);
friend bool4 operator!= (const bool4& a, const bool4& b);
/// Stream output
friend std::ostream& operator<< (std::ostream& cout, const bool4 & a);
private:
// The actual data representation
union {
simd_t m_simd;
int m_val[paddedelements];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3> bool4 shuffle (const bool4& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> bool4 shuffle (const bool4& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> bool extract (const bool4& a);
/// Helper: substitute val for a[i]
template<int i> bool4 insert (const bool4& a, bool val);
/// Logical reduction across all components.
bool reduce_and (const bool4& v);
bool reduce_or (const bool4& v);
// Are all/any/no components true?
bool all (const bool4& v);
bool any (const bool4& v);
bool none (const bool4& v);
/// bool8: An 8-vector whose elements act mostly like bools, accelerated by
/// SIMD instructions when available. This is what is naturally produced by
/// SIMD comparison operators on the float8 and int8 types.
class bool8 {
public:
static const char* type_name() { return "bool8"; }
typedef bool value_t; ///< Underlying equivalent scalar value type
enum { elements = 8 }; ///< Number of scalar elements
enum { paddedelements = 8 }; ///< Number of scalar elements for full pad
enum { bits = elements*32 }; ///< Total number of bits
typedef simd_bool_t<8>::type simd_t; ///< the native SIMD type used
/// Default constructor (contents undefined)
bool8 () { }
/// Construct from a single value (store it in all slots)
bool8 (bool a) { load (a); }
explicit bool8 (const bool *values);
/// Construct from 8 values
bool8 (bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h);
/// Copy construct from another bool8
bool8 (const bool8 &other) { m_simd = other.m_simd; }
/// Construct from a SIMD int (is each element nonzero?)
bool8 (const int8 &i);
/// Construct from two bool4's
bool8 (const bool4 &lo, const bool4 &hi);
/// Construct from the underlying SIMD type
bool8 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Set all components to false
void clear ();
/// Return a bool8 the is 'false' for all values
static const bool8 False ();
/// Return a bool8 the is 'true' for all values
static const bool8 True ();
/// Assign one value to all components
const bool8 & operator= (bool a);
/// Assignment of another bool8
const bool8 & operator= (const bool8 & other);
/// Component access (get)
int operator[] (int i) const;
/// Component access (set).
void setcomp (int i, bool value);
/// Component access (set).
/// NOTE: avoid this unsafe construct. It will go away some day.
int& operator[] (int i);
/// Extract the lower percision bool4
bool4 lo () const;
/// Extract the higher percision bool4
bool4 hi () const;
/// Helper: load a single value into all components.
void load (bool a);
/// Helper: load separate values into each component.
void load (bool a, bool b, bool c, bool d,
bool e, bool f, bool g, bool h);
/// Helper: store the values into memory as bools.
void store (bool *values) const;
/// Store the first n values into memory.
void store (bool *values, int n) const;
/// Logical/bitwise operators, component-by-component
friend bool8 operator! (const bool8& a);
friend bool8 operator& (const bool8& a, const bool8& b);
friend bool8 operator| (const bool8& a, const bool8& b);
friend bool8 operator^ (const bool8& a, const bool8& b);
friend bool8 operator~ (const bool8& a);
friend const bool8& operator&= (bool8& a, const bool8& b);
friend const bool8& operator|= (bool8& a, const bool8& b);
friend const bool8& operator^= (bool8& a, const bool8& b);
/// Comparison operators, component by component
friend bool8 operator== (const bool8& a, const bool8& b);
friend bool8 operator!= (const bool8& a, const bool8& b);
/// Stream output
friend std::ostream& operator<< (std::ostream& cout, const bool8 & a);
private:
// The actual data representation
union {
simd_t m_simd;
int m_val[paddedelements];
simd_bool_t<4>::type m_4[2];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
bool8 shuffle (const bool8& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> bool8 shuffle (const bool8& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> bool extract (const bool8& a);
/// Helper: substitute val for a[i]
template<int i> bool8 insert (const bool8& a, bool val);
/// Logical reduction across all components.
bool reduce_and (const bool8& v);
bool reduce_or (const bool8& v);
// Are all/any/no components true?
bool all (const bool8& v);
bool any (const bool8& v);
bool none (const bool8& v);
/// Integer 4-vector, accelerated by SIMD instructions when available.
class int4 {
public:
static const char* type_name() { return "int4"; }
typedef int value_t; ///< Underlying equivalent scalar value type
enum { elements = 4 }; ///< Number of scalar elements
enum { paddedelements =4 }; ///< Number of scalar elements for full pad
enum { bits = 128 }; ///< Total number of bits
typedef simd_raw_t<int,elements>::type simd_t; ///< the native SIMD type used
typedef bool4 bool_t; ///< bool type of the same length
typedef float4 float_t; ///< float type of the same length
/// Default constructor (contents undefined)
int4 () { }
/// Construct from a single value (store it in all slots)
int4 (int a);
/// Construct from 2 values -- (a,a,b,b)
int4 (int a, int b);
/// Construct from 4 values
int4 (int a, int b, int c, int d);
/// Construct from a pointer to values
int4 (const int *vals);
/// Construct from a pointer to unsigned short values
explicit int4 (const unsigned short *vals);
/// Construct from a pointer to signed short values
explicit int4 (const short *vals);
/// Construct from a pointer to unsigned char values (0 - 255)
explicit int4 (const unsigned char *vals);
/// Construct from a pointer to signed char values (-128 - 127)
explicit int4 (const char *vals);
/// Copy construct from another int4
int4 (const int4 & other) { m_simd = other.m_simd; }
/// Convert a float_t to an int4. Equivalent to i = (int)f;
explicit int4 (const float_t& f); // implementation below
/// Construct from the underlying SIMD type
int4 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Sset all components to 0
void clear () ;
/// Return an int4 with all components set to 0
static const int4 Zero ();
/// Return an int4 with all components set to 1
static const int4 One ();
/// Return an int4 with all components set to -1 (aka 0xffffffff)
static const int4 NegOne ();
/// Return an int4 with incremented components (e.g., 0,1,2,3).
/// Optional argument can give a non-zero starting point.
static const int4 Iota (int start=0, int step=1);
/// Assign one value to all components.
const int4 & operator= (int a);
/// Assignment from another int4
const int4 & operator= (const int4& other) ;
/// Component access (set)
int& operator[] (int i) ;
/// Component access (get)
int operator[] (int i) const;
value_t x () const;
value_t y () const;
value_t z () const;
value_t w () const;
void set_x (value_t val);
void set_y (value_t val);
void set_z (value_t val);
void set_w (value_t val);
/// Helper: load a single int into all components
void load (int a);
/// Helper: load separate values into each component.
void load (int a, int b, int c, int d);
/// Load from an array of 4 values
void load (const int *values);
void load (const int *values, int n) ;
/// Load from an array of 4 unsigned short values, convert to int4
void load (const unsigned short *values) ;
/// Load from an array of 4 unsigned short values, convert to int4
void load (const short *values);
/// Load from an array of 4 unsigned char values, convert to int4
void load (const unsigned char *values);
/// Load from an array of 4 unsigned char values, convert to int4
void load (const char *values);
/// Store the values into memory
void store (int *values) const;
/// Store the first n values into memory
void store (int *values, int n) const;
/// Store the least significant 16 bits of each element into adjacent
/// unsigned shorts.
void store (unsigned short *values) const;
/// Store the least significant 8 bits of each element into adjacent
/// unsigned chars.
void store (unsigned char *values) const;
// Arithmetic operators (component-by-component)
friend int4 operator+ (const int4& a, const int4& b);
friend int4 operator- (const int4& a);
friend int4 operator- (const int4& a, const int4& b);
friend int4 operator* (const int4& a, const int4& b);
friend int4 operator/ (const int4& a, const int4& b);
friend int4 operator% (const int4& a, const int4& b);
friend const int4 & operator+= (int4& a, const int4& b);
friend const int4 & operator-= (int4& a, const int4& b);
friend const int4 & operator*= (int4& a, const int4& b);
friend const int4 & operator/= (int4& a, const int4& b);
friend const int4 & operator%= (int4& a, const int4& b);
// Bitwise operators (component-by-component)
friend int4 operator& (const int4& a, const int4& b);
friend int4 operator| (const int4& a, const int4& b);
friend int4 operator^ (const int4& a, const int4& b);
friend const int4& operator&= (int4& a, const int4& b);
friend const int4& operator|= (int4& a, const int4& b);
friend const int4& operator^= (int4& a, const int4& b);
friend int4 operator~ (const int4& a);
friend int4 operator<< (const int4& a, unsigned int bits);
friend int4 operator>> (const int4& a, unsigned int bits);
friend const int4& operator<<= (int4& a, unsigned int bits);
friend const int4& operator>>= (int4& a, unsigned int bits);
// Comparison operators (component-by-component)
friend bool4 operator== (const int4& a, const int4& b);
friend bool4 operator!= (const int4& a, const int4& b);
friend bool4 operator< (const int4& a, const int4& b);
friend bool4 operator> (const int4& a, const int4& b);
friend bool4 operator>= (const int4& a, const int4& b);
friend bool4 operator<= (const int4& a, const int4& b);
/// Stream output
friend std::ostream& operator<< (std::ostream& cout, const int4 & a);
private:
// The actual data representation
union {
simd_t m_simd;
value_t m_val[elements];
};
};
// Shift right logical -- unsigned shift. This differs from operator>>
// in how it handles the sign bit. (1<<31) >> 1 == (1<<31), but
// srl((1<<31),1) == 1<<30.
int4 srl (const int4& val, const unsigned int bits);
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3> int4 shuffle (const int4& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> int4 shuffle (const int4& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> int extract (const int4& v);
/// The sum of all components, returned in all components.
int4 vreduce_add (const int4& v);
// Reduction across all components
int reduce_add (const int4& v);
int reduce_and (const int4& v);
int reduce_or (const int4& v);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// and b (if mask[i] is true), i.e., mask[i] ? b[i] : a[i].
int4 blend (const int4& a, const int4& b, const bool4& mask);
/// Use a bool mask to select between `a` (if mask[i] is true) or 0 if
/// mask[i] is false), i.e., mask[i] ? a[i] : 0. Equivalent to
/// blend(0,a,mask).
int4 blend0 (const int4& a, const bool4& mask);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// or 0 (if mask[i] is true), i.e., mask[i] ? 0 : a[i]. Equivalent to
/// blend(0,a,!mask), or blend(a,0,mask).
int4 blend0not (const int4& a, const bool4& mask);
/// Select 'a' where mask is true, 'b' where mask is false. Sure, it's a
/// synonym for blend with arguments rearranged, but this is more clear
/// because the arguments are symmetric to scalar (cond ? a : b).
int4 select (const bool4& mask, const int4& a, const int4& b);
// Per-element math
int4 abs (const int4& a);
int4 min (const int4& a, const int4& b);
int4 max (const int4& a, const int4& b);
// Circular bit rotate by k bits, for N values at once.
int4 rotl32 (const int4& x, const unsigned int k);
/// andnot(a,b) returns ((~a) & b)
int4 andnot (const int4& a, const int4& b);
/// Bitcast back and forth to intN (not a convert -- move the bits!)
int4 bitcast_to_int (const bool4& x);
int4 bitcast_to_int (const float4& x);
float4 bitcast_to_float (const int4& x);
void transpose (int4 &a, int4 &b, int4 &c, int4 &d);
void transpose (const int4& a, const int4& b, const int4& c, const int4& d,
int4 &r0, int4 &r1, int4 &r2, int4 &r3);
int4 AxBxCxDx (const int4& a, const int4& b, const int4& c, const int4& d);
/// Integer 8-vector, accelerated by SIMD instructions when available.
class int8 {
public:
static const char* type_name() { return "int8"; }
typedef int value_t; ///< Underlying equivalent scalar value type
enum { elements = 8 }; ///< Number of scalar elements
enum { paddedelements =8 }; ///< Number of scalar elements for full pad
enum { bits = elements*32 }; ///< Total number of bits
typedef simd_raw_t<int,elements>::type simd_t; ///< the native SIMD type used
typedef bool8 bool_t; ///< bool type of the same length
typedef float8 float_t; ///< float type of the same length
/// Default constructor (contents undefined)
int8 () { }
/// Construct from a single value (store it in all slots)
int8 (int a);
/// Construct from 2 values -- (a,a,b,b)
int8 (int a, int b);
/// Construct from 8 values (won't work for int8)
int8 (int a, int b, int c, int d, int e, int f, int g, int h);
/// Construct from a pointer to values
int8 (const int *vals);
/// Construct from a pointer to unsigned short values
explicit int8 (const unsigned short *vals);
/// Construct from a pointer to signed short values
explicit int8 (const short *vals);
/// Construct from a pointer to unsigned char values (0 - 255)
explicit int8 (const unsigned char *vals);
/// Construct from a pointer to signed char values (-128 - 127)
explicit int8 (const char *vals);
/// Copy construct from another int8
int8 (const int8 & other) { m_simd = other.m_simd; }
/// Convert a float8 to an int8. Equivalent to i = (int)f;
explicit int8 (const float8& f); // implementation below
/// Construct from two int4's
int8 (const int4 &lo, const int4 &hi);
/// Construct from the underlying SIMD type
int8 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Sset all components to 0
void clear () ;
/// Return an int8 with all components set to 0
static const int8 Zero ();
/// Return an int8 with all components set to 1
static const int8 One ();
/// Return an int8 with all components set to -1 (aka 0xffffffff)
static const int8 NegOne ();
/// Return an int8 with incremented components (e.g., 0,1,2,3).
/// Optional argument can give a non-zero starting point.
static const int8 Iota (int start=0, int step=1);
/// Assign one value to all components.
const int8 & operator= (int a);
/// Assignment from another int8
const int8 & operator= (const int8& other) ;
/// Component access (set)
int& operator[] (int i) ;
/// Component access (get)
int operator[] (int i) const;
value_t x () const;
value_t y () const;
value_t z () const;
value_t w () const;
void set_x (value_t val);
void set_y (value_t val);
void set_z (value_t val);
void set_w (value_t val);
/// Extract the lower percision int4
int4 lo () const;
/// Extract the higher percision int4
int4 hi () const;
/// Helper: load a single int into all components
void load (int a);
/// Load separate values into each component. (doesn't work for int8)
void load (int a, int b, int c, int d, int e, int f, int g, int h);
/// Load from an array of 8 values
void load (const int *values);
void load (const int *values, int n) ;
/// Load from an array of 8 unsigned short values, convert to int8
void load (const unsigned short *values) ;
/// Load from an array of 8 unsigned short values, convert to int8
void load (const short *values);
/// Load from an array of 8 unsigned char values, convert to int8
void load (const unsigned char *values);
/// Load from an array of 8 unsigned char values, convert to int8
void load (const char *values);
/// Store the values into memory
void store (int *values) const;
/// Store the first n values into memory
void store (int *values, int n) const;
/// Store the least significant 16 bits of each element into adjacent
/// unsigned shorts.
void store (unsigned short *values) const;
/// Store the least significant 8 bits of each element into adjacent
/// unsigned chars.
void store (unsigned char *values) const;
// Arithmetic operators (component-by-component)
friend int8 operator+ (const int8& a, const int8& b);
friend int8 operator- (const int8& a);
friend int8 operator- (const int8& a, const int8& b);
friend int8 operator* (const int8& a, const int8& b);
friend int8 operator/ (const int8& a, const int8& b);
friend int8 operator% (const int8& a, const int8& b);
friend const int8 & operator+= (int8& a, const int8& b);
friend const int8 & operator-= (int8& a, const int8& b);
friend const int8 & operator*= (int8& a, const int8& b);
friend const int8 & operator/= (int8& a, const int8& b);
friend const int8 & operator%= (int8& a, const int8& b);
// Bitwise operators (component-by-component)
friend int8 operator& (const int8& a, const int8& b);
friend int8 operator| (const int8& a, const int8& b);
friend int8 operator^ (const int8& a, const int8& b);
friend const int8& operator&= (int8& a, const int8& b);
friend const int8& operator|= (int8& a, const int8& b);
friend const int8& operator^= (int8& a, const int8& b);
friend int8 operator~ (const int8& a);
friend int8 operator<< (const int8& a, unsigned int bits);
friend int8 operator>> (const int8& a, unsigned int bits);
friend const int8& operator<<= (int8& a, unsigned int bits);
friend const int8& operator>>= (int8& a, unsigned int bits);
// Comparison operators (component-by-component)
friend bool8 operator== (const int8& a, const int8& b);
friend bool8 operator!= (const int8& a, const int8& b);
friend bool8 operator< (const int8& a, const int8& b);
friend bool8 operator> (const int8& a, const int8& b);
friend bool8 operator>= (const int8& a, const int8& b);
friend bool8 operator<= (const int8& a, const int8& b);
/// Stream output
friend std::ostream& operator<< (std::ostream& cout, const int8& a);
private:
// The actual data representation
union {
simd_t m_simd;
value_t m_val[elements];
simd_raw_t<int,4>::type m_4[2];
};
};
// Shift right logical -- unsigned shift. This differs from operator>>
// in how it handles the sign bit. (1<<31) >> 1 == (1<<31), but
// srl((1<<31),1) == 1<<30.
int8 srl (const int8& val, const unsigned int bits);
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3,
int i4, int i5, int i6, int i7> int8 shuffle (const int8& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> int8 shuffle (const int8& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> int extract (const int8& v);
/// Helper: substitute val for a[i]
template<int i> int8 insert (const int8& a, int val);
/// The sum of all components, returned in all components.
int8 vreduce_add (const int8& v);
// Reduction across all components
int reduce_add (const int8& v);
int reduce_and (const int8& v);
int reduce_or (const int8& v);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// and b (if mask[i] is true), i.e., mask[i] ? b[i] : a[i].
int8 blend (const int8& a, const int8& b, const bool8& mask);
/// Use a bool mask to select between `a` (if mask[i] is true) or 0 if
/// mask[i] is false), i.e., mask[i] ? a[i] : 0. Equivalent to
/// blend(0,a,mask).
int8 blend0 (const int8& a, const bool8& mask);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// or 0 (if mask[i] is true), i.e., mask[i] ? 0 : a[i]. Equivalent to
/// blend(0,a,!mask), or blend(a,0,mask).
int8 blend0not (const int8& a, const bool8& mask);
/// Select 'a' where mask is true, 'b' where mask is false. Sure, it's a
/// synonym for blend with arguments rearranged, but this is more clear
/// because the arguments are symmetric to scalar (cond ? a : b).
int8 select (const bool8& mask, const int8& a, const int8& b);
// Per-element math
int8 abs (const int8& a);
int8 min (const int8& a, const int8& b);
int8 max (const int8& a, const int8& b);
// Circular bit rotate by k bits, for N values at once.
int8 rotl32 (const int8& x, const unsigned int k);
/// andnot(a,b) returns ((~a) & b)
int8 andnot (const int8& a, const int8& b);
/// Bitcast back and forth to intN (not a convert -- move the bits!)
int8 bitcast_to_int (const bool8& x);
int8 bitcast_to_int (const float8& x);
float8 bitcast_to_float (const int8& x);
/// Floating point 4-vector, accelerated by SIMD instructions when
/// available.
class float4 {
public:
static const char* type_name() { return "float4"; }
typedef float value_t; ///< Underlying equivalent scalar value type
typedef int4 int_t; ///< SIMD int type
typedef bool4 bool_t; ///< SIMD bool type
enum { elements = 4 }; ///< Number of scalar elements
enum { paddedelements = 4 }; ///< Number of scalar elements for full pad
enum { bits = elements*32 }; ///< Total number of bits
typedef simd_raw_t<float,4>::type simd_t; ///< the native SIMD type used
/// Default constructor (contents undefined)
float4 () { }
/// Construct from a single value (store it in all slots)
float4 (float a) { load(a); }
/// Construct from 3 or 4 values
float4 (float a, float b, float c, float d=0.0f) { load(a,b,c,d); }
/// Construct from a pointer to 4 values
float4 (const float *f) { load (f); }
/// Copy construct from another float4
float4 (const float4 &other) { m_simd = other.m_simd; }
/// Construct from an int4 (promoting all components to float)
explicit float4 (const int4& ival);
/// Construct from the underlying SIMD type
float4 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Construct from a Imath::V3f
float4 (const Imath::V3f &v) { load (v[0], v[1], v[2]); }
/// Cast to a Imath::V3f
const Imath::V3f& V3f () const { return *(const Imath::V3f*)this; }
#if defined(ILMBASE_VERSION_MAJOR) && ILMBASE_VERSION_MAJOR >= 2
// V4f is not defined for older Ilmbase. It's certainly safe for 2.x.
/// Construct from a Imath::V4f
float4 (const Imath::V4f &v) { load ((const float *)&v); }
/// Cast to a Imath::V4f
const Imath::V4f& V4f () const { return *(const Imath::V4f*)this; }
#endif
/// Construct from a pointer to 4 unsigned short values
explicit float4 (const unsigned short *vals) { load(vals); }
/// Construct from a pointer to 4 short values
explicit float4 (const short *vals) { load(vals); }
/// Construct from a pointer to 4 unsigned char values
explicit float4 (const unsigned char *vals) { load(vals); }
/// Construct from a pointer to 4 char values
explicit float4 (const char *vals) { load(vals); }
#ifdef _HALF_H_
/// Construct from a pointer to 4 half (16 bit float) values
explicit float4 (const half *vals) { load(vals); }
#endif
/// Assign a single value to all components
const float4 & operator= (float a) { load(a); return *this; }
/// Assign a float4
const float4 & operator= (float4 other) {
m_simd = other.m_simd;
return *this;
}
/// Return a float4 with all components set to 0.0
static const float4 Zero ();
/// Return a float4 with all components set to 1.0
static const float4 One ();
/// Return a float4 with incremented components (e.g., 0.0,1.0,2.0,3.0).
/// Optional argument can give a non-zero starting point and non-1 step.
static const float4 Iota (float start=0.0f, float step=1.0f);
/// Set all components to 0.0
void clear ();
#if defined(ILMBASE_VERSION_MAJOR) && ILMBASE_VERSION_MAJOR >= 2
/// Assign from a Imath::V4f
const float4 & operator= (const Imath::V4f &v);
#endif
/// Assign from a Imath::V3f
const float4 & operator= (const Imath::V3f &v);
/// Component access (set)
float& operator[] (int i);
/// Component access (get)
float operator[] (int i) const;
value_t x () const;
value_t y () const;
value_t z () const;
value_t w () const;
void set_x (value_t val);
void set_y (value_t val);
void set_z (value_t val);
void set_w (value_t val);
/// Helper: load a single value into all components
void load (float val);
/// Helper: load 3 or 4 values. (If 3 are supplied, the 4th will be 0.)
void load (float a, float b, float c, float d=0.0f);
/// Load from an array of 4 values
void load (const float *values);
/// Load from a partial array of <=4 values. Unassigned values are
/// undefined.
void load (const float *values, int n);
/// Load from an array of 4 unsigned short values, convert to float
void load (const unsigned short *values);
/// Load from an array of 4 short values, convert to float
void load (const short *values);
/// Load from an array of 4 unsigned char values, convert to float
void load (const unsigned char *values);
/// Load from an array of 4 char values, convert to float
void load (const char *values);
#ifdef _HALF_H_
/// Load from an array of 4 half values, convert to float
void load (const half *values);
#endif /* _HALF_H_ */
void store (float *values) const;
/// Store the first n values into memory
void store (float *values, int n) const;
#ifdef _HALF_H_
void store (half *values) const;
#endif
// Arithmetic operators
friend float4 operator+ (const float4& a, const float4& b);
const float4 & operator+= (const float4& a);
float4 operator- () const;
friend float4 operator- (const float4& a, const float4& b);
const float4 & operator-= (const float4& a);
friend float4 operator* (const float4& a, const float4& b);
const float4 & operator*= (const float4& a);
const float4 & operator*= (float val);
friend float4 operator/ (const float4& a, const float4& b);
const float4 & operator/= (const float4& a);
const float4 & operator/= (float val);
// Comparison operations
friend bool4 operator== (const float4& a, const float4& b);
friend bool4 operator!= (const float4& a, const float4& b);
friend bool4 operator< (const float4& a, const float4& b);
friend bool4 operator> (const float4& a, const float4& b);
friend bool4 operator>= (const float4& a, const float4& b);
friend bool4 operator<= (const float4& a, const float4& b);
// Some oddball items that are handy
/// Combine the first two components of A with the first two components
/// of B.
friend float4 AxyBxy (const float4& a, const float4& b);
/// Combine the first two components of A with the first two components
/// of B, but interleaved.
friend float4 AxBxAyBy (const float4& a, const float4& b);
/// Return xyz components, plus 0 for w
float4 xyz0 () const;
/// Return xyz components, plus 1 for w
float4 xyz1 () const;
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const float4& val);
protected:
// The actual data representation
union {
simd_t m_simd;
value_t m_val[paddedelements];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3> float4 shuffle (const float4& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> float4 shuffle (const float4& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> float extract (const float4& a);
/// Helper: substitute val for a[i]
template<int i> float4 insert (const float4& a, float val);
/// The sum of all components, returned in all components.
float4 vreduce_add (const float4& v);
/// The sum of all components, returned as a scalar.
float reduce_add (const float4& v);
/// Return the float dot (inner) product of a and b in every component.
float4 vdot (const float4 &a, const float4 &b);
/// Return the float dot (inner) product of a and b.
float dot (const float4 &a, const float4 &b);
/// Return the float 3-component dot (inner) product of a and b in
/// all components.
float4 vdot3 (const float4 &a, const float4 &b);
/// Return the float 3-component dot (inner) product of a and b.
float dot3 (const float4 &a, const float4 &b);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// and b (if mask[i] is true), i.e., mask[i] ? b[i] : a[i].
float4 blend (const float4& a, const float4& b, const bool4& mask);
/// Use a bool mask to select between `a` (if mask[i] is true) or 0 if
/// mask[i] is false), i.e., mask[i] ? a[i] : 0. Equivalent to
/// blend(0,a,mask).
float4 blend0 (const float4& a, const bool4& mask);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// or 0 (if mask[i] is true), i.e., mask[i] ? 0 : a[i]. Equivalent to
/// blend(0,a,!mask), or blend(a,0,mask).
float4 blend0not (const float4& a, const bool4& mask);
/// "Safe" divide of float4/float4 -- for any component of the divisor
/// that is 0, return 0 rather than Inf.
float4 safe_div (const float4 &a, const float4 &b);
/// Homogeneous divide to turn a float4 into a float3.
float3 hdiv (const float4 &a);
/// Select 'a' where mask is true, 'b' where mask is false. Sure, it's a
/// synonym for blend with arguments rearranged, but this is more clear
/// because the arguments are symmetric to scalar (cond ? a : b).
float4 select (const bool4& mask, const float4& a, const float4& b);
// Per-element math
float4 abs (const float4& a); ///< absolute value (float)
float4 sign (const float4& a); ///< 1.0 when value >= 0, -1 when negative
float4 ceil (const float4& a);
float4 floor (const float4& a);
int4 floori (const float4& a); ///< (int)floor
/// Per-element round to nearest integer (rounding away from 0 in cases
/// that are exactly half way).
float4 round (const float4& a);
/// Per-element round to nearest integer (rounding away from 0 in cases
/// that are exactly half way).
int4 rint (const float4& a);
float4 sqrt (const float4 &a);
float4 rsqrt (const float4 &a); ///< Fully accurate 1/sqrt
float4 rsqrt_fast (const float4 &a); ///< Fast, approximate 1/sqrt
float4 min (const float4& a, const float4& b); ///< Per-element min
float4 max (const float4& a, const float4& b); ///< Per-element max
template <typename T> T exp (const T& v); // template for all SIMD variants
template <typename T> T log (const T& v);
/// andnot(a,b) returns ((~a) & b)
float4 andnot (const float4& a, const float4& b);
// Fused multiply and add (or subtract):
float4 madd (const float4& a, const float4& b, const float4& c); // a*b + c
float4 msub (const float4& a, const float4& b, const float4& c); // a*b - c
float4 nmadd (const float4& a, const float4& b, const float4& c); // -a*b + c
float4 nmsub (const float4& a, const float4& b, const float4& c); // -a*b - c
/// Transpose the rows and columns of the 4x4 matrix [a b c d].
/// In the end, a will have the original (a[0], b[0], c[0], d[0]),
/// b will have the original (a[1], b[1], c[1], d[1]), and so on.
void transpose (float4 &a, float4 &b, float4 &c, float4 &d);
void transpose (const float4& a, const float4& b, const float4& c, const float4& d,
float4 &r0, float4 &r1, float4 &r2, float4 &r3);
/// Make a float4 consisting of the first element of each of 4 float4's.
float4 AxBxCxDx (const float4& a, const float4& b,
const float4& c, const float4& d);
/// Floating point 3-vector, aligned to be internally identical to a float4.
/// The way it differs from float4 is that all of he load functions only
/// load three values, and all the stores only store 3 values. The vast
/// majority of ops just fall back to the float4 version, and so will
/// operate on the 4th component, but we won't care about that results.
class float3 : public float4 {
public:
static const char* type_name() { return "float3"; }
enum { elements = 3 }; ///< Number of scalar elements
enum { paddedelements = 4 }; ///< Number of scalar elements for full pad
/// Default constructor (contents undefined)
float3 () { }
/// Construct from a single value (store it in all slots)
float3 (float a) { load(a); }
/// Construct from 3 or 4 values
float3 (float a, float b, float c) { float4::load(a,b,c); }
/// Construct from a pointer to 4 values
float3 (const float *f) { load (f); }
/// Copy construct from another float3
float3 (const float3 &other);
explicit float3 (const float4 &other);
#if OIIO_SIMD
/// Construct from the underlying SIMD type
explicit float3 (const simd_t& m) : float4(m) { }
#endif
/// Construct from a Imath::V3f
float3 (const Imath::V3f &v) : float4(v) { }
/// Cast to a Imath::V3f
const Imath::V3f& V3f () const { return *(const Imath::V3f*)this; }
/// Construct from a pointer to 4 unsigned short values
explicit float3 (const unsigned short *vals) { load(vals); }
/// Construct from a pointer to 4 short values
explicit float3 (const short *vals) { load(vals); }
/// Construct from a pointer to 4 unsigned char values
explicit float3 (const unsigned char *vals) { load(vals); }
/// Construct from a pointer to 4 char values
explicit float3 (const char *vals) { load(vals); }
#ifdef _HALF_H_
/// Construct from a pointer to 4 half (16 bit float) values
explicit float3 (const half *vals) { load(vals); }
#endif
/// Assign a single value to all components
const float3 & operator= (float a) { load(a); return *this; }
/// Return a float3 with all components set to 0.0
static const float3 Zero ();
/// Return a float3 with all components set to 1.0
static const float3 One ();
/// Return a float3 with incremented components (e.g., 0.0,1.0,2.0).
/// Optional argument can give a non-zero starting point and non-1 step.
static const float3 Iota (float start=0.0f, float step=1.0f);
/// Helper: load a single value into all components
void load (float val);
/// Load from an array of 4 values
void load (const float *values);
/// Load from an array of 4 values
void load (const float *values, int n);
/// Load from an array of 4 unsigned short values, convert to float
void load (const unsigned short *values);
/// Load from an array of 4 short values, convert to float
void load (const short *values);
/// Load from an array of 4 unsigned char values, convert to float
void load (const unsigned char *values);
/// Load from an array of 4 char values, convert to float
void load (const char *values);
#ifdef _HALF_H_
/// Load from an array of 4 half values, convert to float
void load (const half *values);
#endif /* _HALF_H_ */
void store (float *values) const;
void store (float *values, int n) const;
#ifdef _HALF_H_
void store (half *values) const;
#endif
/// Store into an Imath::V3f reference.
void store (Imath::V3f &vec) const;
// Math operators -- define in terms of float3.
friend float3 operator+ (const float3& a, const float3& b);
const float3 & operator+= (const float3& a);
float3 operator- () const;
friend float3 operator- (const float3& a, const float3& b);
const float3 & operator-= (const float3& a);
friend float3 operator* (const float3& a, const float3& b);
const float3 & operator*= (const float3& a);
const float3 & operator*= (float a);
friend float3 operator/ (const float3& a, const float3& b);
const float3 & operator/= (const float3& a);
const float3 & operator/= (float a);
float3 normalized () const;
float3 normalized_fast () const;
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const float3& val);
};
/// SIMD-based 4x4 matrix. This is guaranteed to have memory layout (when
/// not in registers) isomorphic to Imath::M44f.
class matrix44 {
public:
// Uninitialized
OIIO_FORCEINLINE matrix44 ()
#ifndef OIIO_SIMD_SSE
: m_mat(Imath::UNINITIALIZED)
#endif
{ }
/// Construct from a reference to an Imath::M44f
OIIO_FORCEINLINE matrix44 (const Imath::M44f &M) {
#if OIIO_SIMD_SSE
m_row[0].load (M[0]);
m_row[1].load (M[1]);
m_row[2].load (M[2]);
m_row[3].load (M[3]);
#else
m_mat = M;
#endif
}
/// Construct from a float array
OIIO_FORCEINLINE explicit matrix44 (const float *f) {
#if OIIO_SIMD_SSE
m_row[0].load (f+0);
m_row[1].load (f+4);
m_row[2].load (f+8);
m_row[3].load (f+12);
#else
memcpy (&m_mat, f, 16*sizeof(float));
#endif
}
/// Construct from 4 float4 rows
OIIO_FORCEINLINE explicit matrix44 (const float4& a, const float4& b,
const float4& c, const float4& d) {
#if OIIO_SIMD_SSE
m_row[0] = a; m_row[1] = b; m_row[2] = c; m_row[3] = d;
#else
a.store (m_mat[0]);
b.store (m_mat[1]);
c.store (m_mat[2]);
d.store (m_mat[3]);
#endif
}
/// Construct from 4 float[4] rows
OIIO_FORCEINLINE explicit matrix44 (const float *a, const float *b,
const float *c, const float *d) {
#if OIIO_SIMD_SSE
m_row[0].load(a); m_row[1].load(b); m_row[2].load(c); m_row[3].load(d);
#else
memcpy (m_mat[0], a, 4*sizeof(float));
memcpy (m_mat[1], b, 4*sizeof(float));
memcpy (m_mat[2], c, 4*sizeof(float));
memcpy (m_mat[3], d, 4*sizeof(float));
#endif
}
/// Present as an Imath::M44f
const Imath::M44f& M44f() const;
/// Return one row
float4 operator[] (int i) const;
/// Return the transposed matrix
matrix44 transposed () const;
/// Transform 3-point V by 4x4 matrix M.
float3 transformp (const float3 &V) const;
/// Transform 3-vector V by 4x4 matrix M.
float3 transformv (const float3 &V) const;
/// Transform 3-vector V by the transpose of 4x4 matrix M.
float3 transformvT (const float3 &V) const;
bool operator== (const matrix44& m) const;
bool operator== (const Imath::M44f& m) const ;
friend bool operator== (const Imath::M44f& a, const matrix44 &b);
bool operator!= (const matrix44& m) const;
bool operator!= (const Imath::M44f& m) const;
friend bool operator!= (const Imath::M44f& a, const matrix44 &b);
/// Return the inverse of the matrix.
matrix44 inverse() const;
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const matrix44 &M);
private:
#if OIIO_SIMD_SSE
float4 m_row[4];
#else
Imath::M44f m_mat;
#endif
};
/// Transform 3-point V by 4x4 matrix M.
float3 transformp (const matrix44 &M, const float3 &V);
float3 transformp (const Imath::M44f &M, const float3 &V);
/// Transform 3-vector V by 4x4 matrix M.
float3 transformv (const matrix44 &M, const float3 &V);
float3 transformv (const Imath::M44f &M, const float3 &V);
// Transform 3-vector by the transpose of 4x4 matrix M.
float3 transformvT (const matrix44 &M, const float3 &V);
float3 transformvT (const Imath::M44f &M, const float3 &V);
/// Floating point 8-vector, accelerated by SIMD instructions when
/// available.
class float8 {
public:
enum { elements = 8 }; ///< Number of scalar elements
enum { paddedelements = 8 }; ///< Number of scalar elements for full pad
enum { bits = elements*32 }; ///< Total number of bits
typedef float value_t; ///< Underlying equivalent scalar value type
typedef simd_raw_t<float,8>::type simd_t; ///< the native SIMD type used
typedef bool8 bool_t; ///< bool type of the same length
typedef int8 int_t; ///< int type of the same length
static const char* type_name() { return "float8"; }
/// Default constructor (contents undefined)
float8 () { }
/// Construct from a single value (store it in all slots)
float8 (float a) { load(a); }
/// Construct from 8 values
float8 (float a, float b, float c, float d,
float e, float f, float g, float h) { load(a,b,c,d,e,f,g,h); }
/// Construct from a pointer to 8 values
float8 (const float *f) { load (f); }
/// Copy construct from another float8
float8 (const float8 &other) { m_simd = other.m_simd; }
/// Construct from an int vector (promoting all components to float)
explicit float8 (const int8& ival);
/// Construct from two float4's
float8 (const float4 &lo, const float4 &hi);
/// Construct from the underlying SIMD type
float8 (const simd_t& m) : m_simd(m) { }
/// Return the raw SIMD type
operator simd_t () const { return m_simd; }
simd_t simd () const { return m_simd; }
/// Construct from a pointer to unsigned short values
explicit float8 (const unsigned short *vals) { load(vals); }
/// Construct from a pointer to short values
explicit float8 (const short *vals) { load(vals); }
/// Construct from a pointer to unsigned char values
explicit float8 (const unsigned char *vals) { load(vals); }
/// Construct from a pointer to char values
explicit float8 (const char *vals) { load(vals); }
#ifdef _HALF_H_
/// Construct from a pointer to half (16 bit float) values
explicit float8 (const half *vals) { load(vals); }
#endif
/// Assign a single value to all components
const float8& operator= (float a) { load(a); return *this; }
/// Assign a float8
const float8& operator= (float8 other) {
m_simd = other.m_simd;
return *this;
}
/// Return a float8 with all components set to 0.0
static const float8 Zero ();
/// Return a float8 with all components set to 1.0
static const float8 One ();
/// Return a float8 with incremented components (e.g., 0,1,2,3,...)
/// Optional argument can give a non-zero starting point and non-1 step.
static const float8 Iota (float start=0.0f, float step=1.0f);
/// Set all components to 0.0
void clear ();
/// Component access (set)
float& operator[] (int i);
/// Component access (get)
float operator[] (int i) const;
value_t x () const;
value_t y () const;
value_t z () const;
value_t w () const;
void set_x (value_t val);
void set_y (value_t val);
void set_z (value_t val);
void set_w (value_t val);
/// Extract the lower percision float4
float4 lo () const;
/// Extract the higher percision float4
float4 hi () const;
/// Helper: load a single value into all components
void load (float val);
/// Helper: load 8 values
void load (float a, float b, float c, float d,
float e, float f, float g, float h);
/// Load from an array of values
void load (const float *values);
/// Load from a partial array of <=8 values. Unassigned values are
/// undefined.
void load (const float *values, int n);
/// Load from an array of 8 unsigned short values, convert to float
void load (const unsigned short *values);
/// Load from an array of 8 short values, convert to float
void load (const short *values);
/// Load from an array of 8 unsigned char values, convert to float
void load (const unsigned char *values);
/// Load from an array of 8 char values, convert to float
void load (const char *values);
#ifdef _HALF_H_
/// Load from an array of 8 half values, convert to float
void load (const half *values);
#endif /* _HALF_H_ */
void store (float *values) const;
/// Store the first n values into memory
void store (float *values, int n) const;
#ifdef _HALF_H_
void store (half *values) const;
#endif
// Arithmetic operators
friend float8 operator+ (const float8& a, const float8& b);
const float8 & operator+= (const float8& a);
float8 operator- () const;
friend float8 operator- (const float8& a, const float8& b);
const float8 & operator-= (const float8& a);
friend float8 operator* (const float8& a, const float8& b);
const float8 & operator*= (const float8& a);
const float8 & operator*= (float val);
friend float8 operator/ (const float8& a, const float8& b);
const float8 & operator/= (const float8& a);
const float8 & operator/= (float val);
// Comparison operations
friend bool8 operator== (const float8& a, const float8& b);
friend bool8 operator!= (const float8& a, const float8& b);
friend bool8 operator< (const float8& a, const float8& b);
friend bool8 operator> (const float8& a, const float8& b);
friend bool8 operator>= (const float8& a, const float8& b);
friend bool8 operator<= (const float8& a, const float8& b);
// Some oddball items that are handy
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const float8& val);
protected:
// The actual data representation
union {
simd_t m_simd;
value_t m_val[paddedelements];
simd_raw_t<float,4>::type m_4[2];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
float8 shuffle (const float8& a);
/// shuffle<i>(a) is the same as shuffle<i,i,i,i,...>(a)
template<int i> float8 shuffle (const float8& a);
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i> float extract (const float8& a);
/// Helper: substitute val for a[i]
template<int i> float8 insert (const float8& a, float val);
/// The sum of all components, returned in all components.
float8 vreduce_add (const float8& v);
/// The sum of all components, returned as a scalar.
float reduce_add (const float8& v);
/// Return the float dot (inner) product of a and b in every component.
float8 vdot (const float8 &a, const float8 &b);
/// Return the float dot (inner) product of a and b.
float dot (const float8 &a, const float8 &b);
/// Return the float 3-component dot (inner) product of a and b in
/// all components.
float8 vdot3 (const float8 &a, const float8 &b);
/// Return the float 3-component dot (inner) product of a and b.
float dot3 (const float8 &a, const float8 &b);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// and b (if mask[i] is true), i.e., mask[i] ? b[i] : a[i].
float8 blend (const float8& a, const float8& b, const bool8& mask);
/// Use a bool mask to select between `a` (if mask[i] is true) or 0 if
/// mask[i] is false), i.e., mask[i] ? a[i] : 0. Equivalent to
/// blend(0,a,mask).
float8 blend0 (const float8& a, const bool8& mask);
/// Use a bool mask to select between components of a (if mask[i] is false)
/// or 0 (if mask[i] is true), i.e., mask[i] ? 0 : a[i]. Equivalent to
/// blend(0,a,!mask), or blend(a,0,mask).
float8 blend0not (const float8& a, const bool8& mask);
/// "Safe" divide of float8/float8 -- for any component of the divisor
/// that is 0, return 0 rather than Inf.
float8 safe_div (const float8 &a, const float8 &b);
/// Select 'a' where mask is true, 'b' where mask is false. Sure, it's a
/// synonym for blend with arguments rearranged, but this is more clear
/// because the arguments are symmetric to scalar (cond ? a : b).
float8 select (const bool8& mask, const float8& a, const float8& b);
// Per-element math
float8 abs (const float8& a); ///< absolute value (float)
float8 sign (const float8& a); ///< 1.0 when value >= 0, -1 when negative
float8 ceil (const float8& a);
float8 floor (const float8& a);
int8 floori (const float8& a); ///< (int)floor
/// Per-element round to nearest integer (rounding away from 0 in cases
/// that are exactly half way).
float8 round (const float8& a);
/// Per-element round to nearest integer (rounding away from 0 in cases
/// that are exactly half way).
int8 rint (const float8& a);
float8 sqrt (const float8 &a);
float8 rsqrt (const float8 &a); ///< Fully accurate 1/sqrt
float8 rsqrt_fast (const float8 &a); ///< Fast, approximate 1/sqrt
float8 min (const float8& a, const float8& b); ///< Per-element min
float8 max (const float8& a, const float8& b); ///< Per-element max
// float8 exp (const float8& v); // See template with float4
// float8 log (const float8& v); // See template with float4
/// andnot(a,b) returns ((~a) & b)
float8 andnot (const float8& a, const float8& b);
// Fused multiply and add (or subtract):
float8 madd (const float8& a, const float8& b, const float8& c); // a*b + c
float8 msub (const float8& a, const float8& b, const float8& c); // a*b - c
float8 nmadd (const float8& a, const float8& b, const float8& c); // -a*b + c
float8 nmsub (const float8& a, const float8& b, const float8& c); // -a*b - c
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// Gory implementation details follow.
//
// ^^^ All declarations and documention is above ^^^
//
// vvv Below is the implementation, often considerably cluttered with
// #if's for each architeture, and unapologitic use of intrinsics and
// every manner of dirty trick we can think of to make things fast.
// Some of this isn't pretty. We won't recapitulate comments or
// documentation of what the functions are supposed to do, please
// consult the declarations above for that.
//
// Here be dragons.
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// bool4 implementation
OIIO_FORCEINLINE int bool4::operator[] (int i) const {
DASSERT(i >= 0 && i < elements);
#if OIIO_SIMD_SSE
return ((_mm_movemask_ps(m_simd) >> i) & 1) ? -1 : 0;
#else
return m_val[i];
#endif
}
OIIO_FORCEINLINE int& bool4::operator[] (int i) {
DASSERT(i >= 0 && i < elements);
return m_val[i];
}
OIIO_FORCEINLINE void bool4::setcomp (int i, bool value) {
DASSERT(i >= 0 && i < elements);
m_val[i] = value ? -1 : 0;
}
OIIO_FORCEINLINE std::ostream& operator<< (std::ostream& cout, const bool4& a) {
cout << a[0];
for (int i = 1; i < a.elements; ++i)
cout << ' ' << a[i];
return cout;
}
OIIO_FORCEINLINE void bool4::load (bool a) {
#if OIIO_SIMD_SSE
m_simd = _mm_castsi128_ps(_mm_set1_epi32(-int(a)));
#else
int val = -int(a);
SIMD_CONSTRUCT (val);
#endif
}
OIIO_FORCEINLINE void bool4::load (bool a, bool b, bool c, bool d) {
#if OIIO_SIMD_SSE
// N.B. -- we need to reverse the order because of our convention
// of storing a,b,c,d in the same order in memory.
m_simd = _mm_castsi128_ps(_mm_set_epi32(-int(d), -int(c), -int(b), -int(a)));
#else
m_val[0] = -int(a);
m_val[1] = -int(b);
m_val[2] = -int(c);
m_val[3] = -int(d);
#endif
}
OIIO_FORCEINLINE bool4::bool4 (const bool *a) {
load (a[0], a[1], a[2], a[3]);
}
OIIO_FORCEINLINE const bool4& bool4::operator= (const bool4 & other) {
m_simd = other.m_simd;
return *this;
}
OIIO_FORCEINLINE void bool4::clear () {
#if OIIO_SIMD_SSE
m_simd = _mm_setzero_ps();
#else
*this = false;
#endif
}
OIIO_FORCEINLINE const bool4 bool4::False () {
#if OIIO_SIMD_SSE
return _mm_setzero_ps();
#else
return false;
#endif
}
OIIO_FORCEINLINE const bool4 bool4::True () {
// Fastest way to fill with all 1 bits is to cmp any value to itself.
#if OIIO_SIMD_SSE
# if OIIO_SIMD_AVX && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128();
# else
__m128i anyval = _mm_setzero_si128();
# endif
return _mm_castsi128_ps (_mm_cmpeq_epi8 (anyval, anyval));
#else
return true;
#endif
}
OIIO_FORCEINLINE void bool4::store (bool *values) const {
SIMD_DO (values[i] = m_val[i] ? true : false);
}
OIIO_FORCEINLINE void bool4::store (bool *values, int n) const {
DASSERT (n >= 0 && n <= elements);
for (int i = 0; i < n; ++i)
values[i] = m_val[i] ? true : false;
}
OIIO_FORCEINLINE bool4 operator! (const bool4 & a) {
#if OIIO_SIMD_SSE
return _mm_xor_ps (a.simd(), bool4::True());
#else
SIMD_RETURN (bool4, a[i] ^ (-1));
#endif
}
OIIO_FORCEINLINE bool4 operator& (const bool4 & a, const bool4 & b) {
#if OIIO_SIMD_SSE
return _mm_and_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool4, a[i] & b[i]);
#endif
}
OIIO_FORCEINLINE bool4 operator| (const bool4 & a, const bool4 & b) {
#if OIIO_SIMD_SSE
return _mm_or_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool4, a[i] | b[i]);
#endif
}
OIIO_FORCEINLINE bool4 operator^ (const bool4& a, const bool4& b) {
#if OIIO_SIMD_SSE
return _mm_xor_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool4, a[i] ^ b[i]);
#endif
}
OIIO_FORCEINLINE const bool4& operator&= (bool4& a, const bool4 &b) {
return a = a & b;
}
OIIO_FORCEINLINE const bool4& operator|= (bool4& a, const bool4& b) {
return a = a | b;
}
OIIO_FORCEINLINE const bool4& operator^= (bool4& a, const bool4& b) {
return a = a ^ b;
}
OIIO_FORCEINLINE bool4 operator~ (const bool4& a) {
#if OIIO_SIMD_SSE
// Fastest way to bit-complement in SSE is to xor with 0xffffffff.
return _mm_xor_ps (a.simd(), bool4::True());
#else
SIMD_RETURN (bool4, ~a[i]);
#endif
}
OIIO_FORCEINLINE bool4 operator== (const bool4 & a, const bool4 & b) {
#if OIIO_SIMD_SSE
return _mm_castsi128_ps (_mm_cmpeq_epi32 (_mm_castps_si128 (a), _mm_castps_si128(b)));
#else
SIMD_RETURN (bool4, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator!= (const bool4 & a, const bool4 & b) {
#if OIIO_SIMD_SSE
return _mm_xor_ps (a, b);
#else
SIMD_RETURN (bool4, a[i] != b[i] ? -1 : 0);
#endif
}
#if OIIO_SIMD_SSE
// Shuffling. Use like this: x = shuffle<3,2,1,0>(b)
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE __m128i shuffle_sse (__m128i v) {
return _mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0));
}
#endif
#if OIIO_SIMD_SSE >= 3
// SSE3 has intrinsics for a few special cases
template<> OIIO_FORCEINLINE __m128i shuffle_sse<0, 0, 2, 2> (__m128i a) {
return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(a)));
}
template<> OIIO_FORCEINLINE __m128i shuffle_sse<1, 1, 3, 3> (__m128i a) {
return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(a)));
}
template<> OIIO_FORCEINLINE __m128i shuffle_sse<0, 1, 0, 1> (__m128i a) {
return _mm_castpd_si128(_mm_movedup_pd(_mm_castsi128_pd(a)));
}
#endif
#if OIIO_SIMD_SSE
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE __m128 shuffle_sse (__m128 a) {
return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(a), _MM_SHUFFLE(i3, i2, i1, i0)));
}
#endif
#if OIIO_SIMD_SSE >= 3
// SSE3 has intrinsics for a few special cases
template<> OIIO_FORCEINLINE __m128 shuffle_sse<0, 0, 2, 2> (__m128 a) {
return _mm_moveldup_ps(a);
}
template<> OIIO_FORCEINLINE __m128 shuffle_sse<1, 1, 3, 3> (__m128 a) {
return _mm_movehdup_ps(a);
}
template<> OIIO_FORCEINLINE __m128 shuffle_sse<0, 1, 0, 1> (__m128 a) {
return _mm_castpd_ps(_mm_movedup_pd(_mm_castps_pd(a)));
}
#endif
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(bool4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE bool4 shuffle (const bool4& a) {
#if OIIO_SIMD_SSE
return shuffle_sse<i0,i1,i2,i3> (a.simd());
#else
return bool4 (a[i0], a[i1], a[i2], a[i3]);
#endif
}
/// shuffle<i>(a) is the same as shuffle<i,i,i,i>(a)
template<int i> OIIO_FORCEINLINE bool4 shuffle (const bool4& a) {
return shuffle<i,i,i,i>(a);
}
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i>
OIIO_FORCEINLINE bool extract (const bool4& a) {
#if OIIO_SIMD_SSE >= 4
return _mm_extract_epi32(_mm_castps_si128(a.simd()), i); // SSE4.1 only
#else
return a[i];
#endif
}
/// Helper: substitute val for a[i]
template<int i>
OIIO_FORCEINLINE bool4 insert (const bool4& a, bool val) {
#if OIIO_SIMD_SSE >= 4
int ival = -int(val);
return _mm_castsi128_ps (_mm_insert_epi32 (_mm_castps_si128(a), ival, i));
#else
bool4 tmp = a;
tmp[i] = -int(val);
return tmp;
#endif
}
OIIO_FORCEINLINE bool reduce_and (const bool4& v) {
#if OIIO_SIMD_AVX
return _mm_testc_ps (v, bool4(true)) != 0;
#elif OIIO_SIMD_SSE
return _mm_movemask_ps(v.simd()) == 0xf;
#else
SIMD_RETURN_REDUCE (bool, true, r &= (v[i] != 0));
#endif
}
OIIO_FORCEINLINE bool reduce_or (const bool4& v) {
#if OIIO_SIMD_AVX
return ! _mm_testz_ps (v, v);
#elif OIIO_SIMD_SSE
return _mm_movemask_ps(v) != 0;
#else
SIMD_RETURN_REDUCE (bool, false, r |= (v[i] != 0));
#endif
}
OIIO_FORCEINLINE bool all (const bool4& v) { return reduce_and(v) == true; }
OIIO_FORCEINLINE bool any (const bool4& v) { return reduce_or(v) == true; }
OIIO_FORCEINLINE bool none (const bool4& v) { return reduce_or(v) == false; }
//////////////////////////////////////////////////////////////////////
// bool8 implementation
OIIO_FORCEINLINE int bool8::operator[] (int i) const {
DASSERT(i >= 0 && i < elements);
#if OIIO_SIMD_AVX
return ((_mm256_movemask_ps(m_simd) >> i) & 1) ? -1 : 0;
#else
return m_val[i];
#endif
}
OIIO_FORCEINLINE void bool8::setcomp (int i, bool value) {
DASSERT(i >= 0 && i < elements);
m_val[i] = value ? -1 : 0;
}
OIIO_FORCEINLINE int& bool8::operator[] (int i) {
DASSERT(i >= 0 && i < elements);
return m_val[i];
}
OIIO_FORCEINLINE std::ostream& operator<< (std::ostream& cout, const bool8& a) {
cout << a[0];
for (int i = 1; i < a.elements; ++i)
cout << ' ' << a[i];
return cout;
}
OIIO_FORCEINLINE void bool8::load (bool a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_castsi256_ps(_mm256_set1_epi32(-int(a)));
#else
int val = -int(a);
SIMD_CONSTRUCT (val);
#endif
}
OIIO_FORCEINLINE void bool8::load (bool a, bool b, bool c, bool d,
bool e, bool f, bool g, bool h) {
#if OIIO_SIMD_AVX
// N.B. -- we need to reverse the order because of our convention
// of storing a,b,c,d in the same order in memory.
m_simd = _mm256_castsi256_ps(_mm256_set_epi32(-int(h), -int(g), -int(f), -int(e),
-int(d), -int(c), -int(b), -int(a)));
#else
m_val[0] = -int(a);
m_val[1] = -int(b);
m_val[2] = -int(c);
m_val[3] = -int(d);
m_val[4] = -int(e);
m_val[5] = -int(f);
m_val[6] = -int(g);
m_val[7] = -int(h);
#endif
}
OIIO_FORCEINLINE bool8::bool8 (bool a, bool b, bool c, bool d,
bool e, bool f, bool g, bool h) {
load (a, b, c, d, e, f, g, h);
}
OIIO_FORCEINLINE bool8::bool8 (const bool *a) {
load (a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}
OIIO_FORCEINLINE const bool8& bool8::operator= (bool a) {
load(a);
return *this;
}
OIIO_FORCEINLINE const bool8& bool8::operator= (const bool8 & other) {
m_simd = other.m_simd;
return *this;
}
OIIO_FORCEINLINE void bool8::clear () {
#if OIIO_SIMD_AVX
m_simd = _mm256_setzero_ps();
#else
*this = false;
#endif
}
OIIO_FORCEINLINE const bool8 bool8::False () {
#if OIIO_SIMD_AVX
return _mm256_setzero_ps();
#else
return false;
#endif
}
OIIO_FORCEINLINE const bool8 bool8::True () {
#if OIIO_SIMD_AVX
# if OIIO_SIMD_AVX >= 2 && (OIIO_GNUC_VERSION > 50000)
// Fastest way to fill with all 1 bits is to cmp any value to itself.
__m256i anyval = _mm256_undefined_si256();
return _mm256_castsi256_ps (_mm256_cmpeq_epi8 (anyval, anyval));
# else
return _mm256_castsi256_ps (_mm256_set1_epi32 (-1));
# endif
#else
return true;
#endif
}
OIIO_FORCEINLINE void bool8::store (bool *values) const {
SIMD_DO (values[i] = m_val[i] ? true : false);
}
OIIO_FORCEINLINE void bool8::store (bool *values, int n) const {
DASSERT (n >= 0 && n <= elements);
for (int i = 0; i < n; ++i)
values[i] = m_val[i] ? true : false;
}
OIIO_FORCEINLINE bool4 bool8::lo () const {
#if OIIO_SIMD_AVX
return _mm256_castps256_ps128 (simd());
#else
return m_4[0];
#endif
}
OIIO_FORCEINLINE bool4 bool8::hi () const {
#if OIIO_SIMD_AVX
return _mm256_extractf128_ps (simd(), 1);
#else
return m_4[1];
#endif
}
OIIO_FORCEINLINE bool8::bool8 (const bool4& lo, const bool4 &hi) {
#if OIIO_SIMD_AVX
__m256 r = _mm256_castps128_ps256 (lo);
m_simd = _mm256_insertf128_ps (r, hi, 1);
// N.B. equivalent, if available: m_simd = _mm256_set_m128 (hi, lo);
#else
m_4[0] = lo;
m_4[1] = hi;
#endif
}
OIIO_FORCEINLINE bool8 operator! (const bool8 & a) {
#if OIIO_SIMD_AVX
return _mm256_xor_ps (a.simd(), bool8::True());
#else
SIMD_RETURN (bool8, a[i] ^ (-1));
#endif
}
OIIO_FORCEINLINE bool8 operator& (const bool8 & a, const bool8 & b) {
#if OIIO_SIMD_AVX
return _mm256_and_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool8, a[i] & b[i]);
#endif
}
OIIO_FORCEINLINE bool8 operator| (const bool8 & a, const bool8 & b) {
#if OIIO_SIMD_AVX
return _mm256_or_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool8, a[i] | b[i]);
#endif
}
OIIO_FORCEINLINE bool8 operator^ (const bool8& a, const bool8& b) {
#if OIIO_SIMD_AVX
return _mm256_xor_ps (a.simd(), b.simd());
#else
SIMD_RETURN (bool8, a[i] ^ b[i]);
#endif
}
OIIO_FORCEINLINE const bool8& operator&= (bool8& a, const bool8 &b) {
return a = a & b;
}
OIIO_FORCEINLINE const bool8& operator|= (bool8& a, const bool8& b) {
return a = a | b;
}
OIIO_FORCEINLINE const bool8& operator^= (bool8& a, const bool8& b) {
return a = a ^ b;
}
OIIO_FORCEINLINE bool8 operator~ (const bool8& a) {
#if OIIO_SIMD_AVX
// Fastest way to bit-complement in SSE is to xor with 0xffffffff.
return _mm256_xor_ps (a.simd(), bool8::True());
#else
SIMD_RETURN (bool8, ~a[i]);
#endif
}
OIIO_FORCEINLINE bool8 operator== (const bool8 & a, const bool8 & b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_castsi256_ps (_mm256_cmpeq_epi32 (_mm256_castps_si256 (a), _mm256_castps_si256(b)));
#elif OIIO_SIMD_AVX
return _mm256_cmp_ps (a, b, _CMP_EQ_UQ);
#else
SIMD_RETURN (bool8, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator!= (const bool8 & a, const bool8 & b) {
#if OIIO_SIMD_AVX
return _mm256_xor_ps (a, b);
#else
SIMD_RETURN (bool8, a[i] != b[i] ? -1 : 0);
#endif
}
template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
OIIO_FORCEINLINE bool8 shuffle (const bool8& a) {
#if OIIO_SIMD_AVX >= 2
int8 index (i0, i1, i2, i3, i4, i5, i6, i7);
return _mm256_permutevar8x32_ps (a.simd(), index.simd());
#else
return bool8 (a[i0], a[i1], a[i2], a[i3], a[i4], a[i5], a[i6], a[i7]);
#endif
}
template<int i> OIIO_FORCEINLINE bool8 shuffle (const bool8& a) {
return shuffle<i,i,i,i,i,i,i,i>(a);
}
template<int i>
OIIO_FORCEINLINE bool extract (const bool8& a) {
#if OIIO_SIMD_AVX && !_WIN32
return _mm256_extract_epi32(_mm256_castps_si256(a.simd()), i); // SSE4.1 only
#else
return a[i];
#endif
}
template<int i>
OIIO_FORCEINLINE bool8 insert (const bool8& a, bool val) {
#if OIIO_SIMD_AVX && !_WIN32
int ival = -int(val);
return _mm256_castsi256_ps (_mm256_insert_epi32 (_mm256_castps_si256(a.simd()), ival, i));
#else
bool8 tmp = a;
tmp[i] = -int(val);
return tmp;
#endif
}
OIIO_FORCEINLINE bool reduce_and (const bool8& v) {
#if OIIO_SIMD_AVX
return _mm256_testc_ps (v, bool8(true)) != 0;
// return _mm256_movemask_ps(v.simd()) == 0xff;
#else
SIMD_RETURN_REDUCE (bool, true, r &= bool(v[i]));
#endif
}
OIIO_FORCEINLINE bool reduce_or (const bool8& v) {
#if OIIO_SIMD_AVX
return ! _mm256_testz_ps (v, v); // FIXME? Not in all immintrin.h !
// return _mm256_movemask_ps(v) != 0;
#else
SIMD_RETURN_REDUCE (bool, false, r |= bool(v[i]));
#endif
}
OIIO_FORCEINLINE bool all (const bool8& v) { return reduce_and(v) == true; }
OIIO_FORCEINLINE bool any (const bool8& v) { return reduce_or(v) == true; }
OIIO_FORCEINLINE bool none (const bool8& v) { return reduce_or(v) == false; }
//////////////////////////////////////////////////////////////////////
// int4 implementation
OIIO_FORCEINLINE const int4 & int4::operator= (const int4& other) {
m_simd = other.m_simd;
return *this;
}
OIIO_FORCEINLINE int& int4::operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE int int4::operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE void int4::load (int a) {
#if OIIO_SIMD_SSE
m_simd = _mm_set1_epi32 (a);
#else
SIMD_CONSTRUCT (a);
#endif
}
OIIO_FORCEINLINE void int4::load (int a, int b, int c, int d) {
#if OIIO_SIMD_SSE
m_simd = _mm_set_epi32 (d, c, b, a);
#else
m_val[0] = a;
m_val[1] = b;
m_val[2] = c;
m_val[3] = d;
#endif
}
// OIIO_FORCEINLINE void int4::load (int a, int b, int c, int d,
// int e, int f, int g, int h) {
// load (a, b, c, d);
// }
OIIO_FORCEINLINE void int4::load (const int *values) {
#if OIIO_SIMD_SSE
m_simd = _mm_loadu_si128 ((const simd_t *)values);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void int4::load (const int *values, int n)
{
#if OIIO_SIMD_SSE
switch (n) {
case 1:
m_simd = _mm_castps_si128 (_mm_load_ss ((const float *)values));
break;
case 2:
// Trickery: load one double worth of bits!
m_simd = _mm_castpd_si128 (_mm_load_sd ((const double*)values));
break;
case 3:
// Trickery: load one double worth of bits, then a float,
// and combine, casting to ints.
m_simd = _mm_castps_si128 (_mm_movelh_ps(_mm_castpd_ps(_mm_load_sd((const double*)values)),
_mm_load_ss ((const float *)values + 2)));
break;
case 4:
m_simd = _mm_loadu_si128 ((const simd_t *)values);
break;
default:
break;
}
#else
for (int i = 0; i < n; ++i)
m_val[i] = values[i];
for (int i = n; i < elements; ++i)
m_val[i] = 0;
#endif
}
OIIO_FORCEINLINE void int4::load (const unsigned short *values) {
#if OIIO_SIMD_SSE >= 4
// Trickery: load one double worth of bits = 4 uint16's!
simd_t a = _mm_castpd_si128 (_mm_load_sd ((const double *)values));
m_simd = _mm_cvtepu16_epi32 (a);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void int4::load (const short *values) {
#if OIIO_SIMD_SSE >= 4
// Trickery: load one double worth of bits = 4 int16's!
simd_t a = _mm_castpd_si128 (_mm_load_sd ((const double *)values));
m_simd = _mm_cvtepi16_epi32 (a);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void int4::load (const unsigned char *values) {
#if OIIO_SIMD_SSE >= 4
// Trickery: load one float worth of bits = 4 uint8's!
simd_t a = _mm_castps_si128 (_mm_load_ss ((const float *)values));
m_simd = _mm_cvtepu8_epi32 (a);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void int4::load (const char *values) {
#if OIIO_SIMD_SSE >= 4
// Trickery: load one float worth of bits = 4 uint8's!
simd_t a = _mm_castps_si128 (_mm_load_ss ((const float *)values));
m_simd = _mm_cvtepi8_epi32 (a);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE int4::int4 (int a) { load(a); }
OIIO_FORCEINLINE int4::int4 (int a, int b) { load(a,a,b,b); }
OIIO_FORCEINLINE int4::int4 (int a, int b, int c, int d) { load(a,b,c,d); }
// OIIO_FORCEINLINE int4::int4 (int a, int b, int c, int d,
// int e, int f, int g, int h) {
// load(a,b,c,d,e,f,g,h);
// }
OIIO_FORCEINLINE int4::int4 (const int *vals) { load (vals); }
OIIO_FORCEINLINE int4::int4 (const unsigned short *vals) { load(vals); }
OIIO_FORCEINLINE int4::int4 (const short *vals) { load(vals); }
OIIO_FORCEINLINE int4::int4 (const unsigned char *vals) { load(vals); }
OIIO_FORCEINLINE int4::int4 (const char *vals) { load(vals); }
OIIO_FORCEINLINE const int4 & int4::operator= (int a) { load(a); return *this; }
OIIO_FORCEINLINE void int4::store (int *values) const {
#if OIIO_SIMD_SSE
// Use an unaligned store -- it's just as fast when the memory turns
// out to be aligned, nearly as fast even when unaligned. Not worth
// the headache of using stores that require alignment.
_mm_storeu_si128 ((simd_t *)values, m_simd);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void int4::clear () {
#if OIIO_SIMD_SSE
m_simd = _mm_setzero_si128();
#else
*this = 0;
#endif
}
OIIO_FORCEINLINE const int4 int4::Zero () {
#if OIIO_SIMD_SSE
return _mm_setzero_si128();
#else
return 0;
#endif
}
OIIO_FORCEINLINE const int4 int4::One () { return int4(1); }
OIIO_FORCEINLINE const int4 int4::NegOne () {
#if OIIO_SIMD_SSE
// Fastest way to fill an __m128 with all 1 bits is to cmpeq_epi8
// any value to itself.
# if OIIO_SIMD_AVX && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128();
# else
__m128i anyval = _mm_setzero_si128();
# endif
return _mm_cmpeq_epi8 (anyval, anyval);
#else
return int4(-1);
#endif
}
OIIO_FORCEINLINE const int4 int4::Iota (int start, int step) {
return int4 (start+0*step, start+1*step, start+2*step, start+3*step);
}
OIIO_FORCEINLINE int4 operator+ (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_add_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] + b[i]);
#endif
}
OIIO_FORCEINLINE const int4& operator+= (int4& a, const int4& b) {
return a = a + b;
}
OIIO_FORCEINLINE int4 operator- (const int4& a) {
#if OIIO_SIMD_SSE
return _mm_sub_epi32 (_mm_setzero_si128(), a);
#else
SIMD_RETURN (int4, -a[i]);
#endif
}
OIIO_FORCEINLINE int4 operator- (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_sub_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] - b[i]);
#endif
}
OIIO_FORCEINLINE const int4 &operator-= (int4& a, const int4& b) {
return a = a - b;
}
#if OIIO_SIMD_SSE
// Shamelessly lifted from Syrah which lifted from Manta which lifted it
// from intel.com
OIIO_FORCEINLINE __m128i mul_epi32 (__m128i a, __m128i b) {
#if OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_mullo_epi32(a, b);
#else
// Prior to SSE 4.1, there is no _mm_mullo_epi32 instruction, so we have
// to fake it.
__m128i t0;
__m128i t1;
t0 = _mm_mul_epu32 (a, b);
t1 = _mm_mul_epu32 (_mm_shuffle_epi32 (a, 0xB1),
_mm_shuffle_epi32 (b, 0xB1));
t0 = _mm_shuffle_epi32 (t0, 0xD8);
t1 = _mm_shuffle_epi32 (t1, 0xD8);
return _mm_unpacklo_epi32 (t0, t1);
#endif
}
#endif
OIIO_FORCEINLINE int4 operator* (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return mul_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] * b[i]);
#endif
}
OIIO_FORCEINLINE const int4& operator*= (int4& a, const int4& b) { return a = a * b; }
OIIO_FORCEINLINE const int4& operator*= (int4& a, int b) { return a = a * b; }
OIIO_FORCEINLINE int4 operator/ (const int4& a, const int4& b) {
// NO INTEGER DIVISION IN SSE!
SIMD_RETURN (int4, a[i] / b[i]);
}
OIIO_FORCEINLINE const int4& operator/= (int4& a, const int4& b) { return a = a / b; }
OIIO_FORCEINLINE int4 operator% (const int4& a, const int4& b) {
// NO INTEGER MODULUS IN SSE!
SIMD_RETURN (int4, a[i] % b[i]);
}
OIIO_FORCEINLINE const int4& operator%= (int4& a, const int4& b) { return a = a % b; }
OIIO_FORCEINLINE int4 operator% (const int4& a, int w) {
// NO INTEGER MODULUS in SSE!
SIMD_RETURN (int4, a[i] % w);
}
OIIO_FORCEINLINE const int4& operator%= (int4& a, int b) { return a = a % b; }
OIIO_FORCEINLINE int4 operator& (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_and_si128 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] & b[i]);
#endif
}
OIIO_FORCEINLINE const int4& operator&= (int4& a, const int4& b) { return a = a & b; }
OIIO_FORCEINLINE int4 operator| (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_or_si128 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] | b[i]);
#endif
}
OIIO_FORCEINLINE const int4& operator|= (int4& a, const int4& b) { return a = a | b; }
OIIO_FORCEINLINE int4 operator^ (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_xor_si128 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, a[i] ^ b[i]);
#endif
}
OIIO_FORCEINLINE const int4& operator^= (int4& a, const int4& b) { return a = a ^ b; }
OIIO_FORCEINLINE int4 operator~ (const int4& a) {
#if OIIO_SIMD_SSE
return a ^ a.NegOne();
#else
SIMD_RETURN (int4, ~a[i]);
#endif
}
OIIO_FORCEINLINE int4 operator<< (const int4& a, unsigned int bits) {
#if OIIO_SIMD_SSE
return _mm_slli_epi32 (a, bits);
#else
SIMD_RETURN (int4, a[i] << bits);
#endif
}
OIIO_FORCEINLINE const int4& operator<<= (int4& a, const unsigned int bits) {
return a = a << bits;
}
OIIO_FORCEINLINE int4 operator>> (const int4& a, const unsigned int bits) {
#if OIIO_SIMD_SSE
return _mm_srai_epi32 (a, bits);
#else
SIMD_RETURN (int4, a[i] >> bits);
#endif
}
OIIO_FORCEINLINE const int4& operator>>= (int4& a, const unsigned int bits) {
return a = a >> bits;
}
OIIO_FORCEINLINE int4 srl (const int4& a, const unsigned int bits) {
#if OIIO_SIMD_SSE
return _mm_srli_epi32 (a, bits);
#else
SIMD_RETURN (int4, int ((unsigned int)(a[i]) >> bits));
#endif
}
OIIO_FORCEINLINE bool4 operator== (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_castsi128_ps(_mm_cmpeq_epi32 (a, b));
#else
SIMD_RETURN (bool4, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator!= (const int4& a, const int4& b) {
return ! (a == b);
}
OIIO_FORCEINLINE bool4 operator> (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_castsi128_ps(_mm_cmpgt_epi32 (a, b));
#else
SIMD_RETURN (bool4, a[i] > b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator< (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_castsi128_ps(_mm_cmplt_epi32 (a, b));
#else
SIMD_RETURN (bool4, a[i] < b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator>= (const int4& a, const int4& b) {
return (b < a) | (a == b);
}
OIIO_FORCEINLINE bool4 operator<= (const int4& a, const int4& b) {
return (b > a) | (a == b);
}
inline std::ostream& operator<< (std::ostream& cout, const int4& val) {
cout << val[0];
for (int i = 1; i < val.elements; ++i)
cout << ' ' << val[i];
return cout;
}
// FIXME(SSE,AVX): is there a faster way to do a partial store? 512!
OIIO_FORCEINLINE void int4::store (int *values, int n) const {
DASSERT (n >= 0 && n <= elements);
#if defined(OIIO_SIMD)
// For full SIMD, there is a speed advantage to storing all components.
if (n == elements)
store (values);
else
#endif
for (int i = 0; i < n; ++i)
values[i] = m_val[i];
}
// FIXME(SSE,AVX): is there a faster way to do a partial store? 512!
OIIO_FORCEINLINE void int4::store (unsigned short *values) const {
#if OIIO_SIMD_SSE
// Expressed as half-words and considering little endianness, we
// currently have xAxBxCxD (the 'x' means don't care).
int4 clamped = m_val & int4(0xffff); // A0B0C0D0
int4 low = _mm_shufflelo_epi16 (clamped, (0<<0) | (2<<2) | (1<<4) | (1<<6));
// low = AB00xxxx
int4 high = _mm_shufflehi_epi16 (clamped, (1<<0) | (1<<2) | (0<<4) | (2<<6));
// high = xxxx00CD
int4 highswapped = shuffle_sse<2,3,0,1>(high); // 00CDxxxx
int4 result = low | highswapped; // ABCDxxxx
_mm_storel_pd ((double *)values, _mm_castsi128_pd(result));
// At this point, values[] should hold A,B,C,D
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void int4::store (unsigned char *values) const {
#if OIIO_SIMD_SSE
// Expressed as bytes and considering little endianness, we
// currently have xAxBxCxD (the 'x' means don't care).
int4 clamped = m_val & int4(0xff); // A000 B000 C000 D000
int4 swapped = shuffle_sse<1,0,3,2>(clamped); // B000 A000 D000 C000
int4 shifted = swapped << 8; // 0B00 0A00 0D00 0C00
int4 merged = clamped | shifted; // AB00 xxxx CD00 xxxx
int4 merged2 = shuffle_sse<2,2,2,2>(merged); // CD00 ...
int4 shifted2 = merged2 << 16; // 00CD ...
int4 result = merged | shifted2; // ABCD ...
*(int*)values = result[0]; //extract<0>(result);
// At this point, values[] should hold A,B,C,D
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE int4 shuffle (const int4& a) {
#if OIIO_SIMD_SSE
return shuffle_sse<i0,i1,i2,i3> (__m128i(a));
#else
return int4(a[i0], a[i1], a[i2], a[i3]);
#endif
}
template<int i> OIIO_FORCEINLINE int4 shuffle (const int4& a) { return shuffle<i,i,i,i>(a); }
template<int i>
OIIO_FORCEINLINE int extract (const int4& v) {
#if OIIO_SIMD_SSE >= 4
return _mm_extract_epi32(v.simd(), i); // SSE4.1 only
#else
return v[i];
#endif
}
#if OIIO_SIMD_SSE
template<> OIIO_FORCEINLINE int extract<0> (const int4& v) {
return _mm_cvtsi128_si32(v.simd());
}
#endif
template<int i>
OIIO_FORCEINLINE int4 insert (const int4& a, int val) {
#if OIIO_SIMD_SSE >= 4
return _mm_insert_epi32 (a.simd(), val, i);
#else
int4 tmp = a;
tmp[i] = val;
return tmp;
#endif
}
OIIO_FORCEINLINE int int4::x () const { return extract<0>(*this); }
OIIO_FORCEINLINE int int4::y () const { return extract<1>(*this); }
OIIO_FORCEINLINE int int4::z () const { return extract<2>(*this); }
OIIO_FORCEINLINE int int4::w () const { return extract<3>(*this); }
OIIO_FORCEINLINE void int4::set_x (int val) { *this = insert<0>(*this, val); }
OIIO_FORCEINLINE void int4::set_y (int val) { *this = insert<1>(*this, val); }
OIIO_FORCEINLINE void int4::set_z (int val) { *this = insert<2>(*this, val); }
OIIO_FORCEINLINE void int4::set_w (int val) { *this = insert<3>(*this, val); }
OIIO_FORCEINLINE int4 bitcast_to_int (const bool4& x)
{
#if OIIO_SIMD_SSE
return _mm_castps_si128 (x.simd());
#else
return *(int4 *)&x;
#endif
}
// Old names:
inline int4 bitcast_to_int4 (const bool4& x) { return bitcast_to_int(x); }
OIIO_FORCEINLINE int4 vreduce_add (const int4& v) {
#if OIIO_SIMD_SSE >= 3
// People seem to agree that SSE3 does add reduction best with 2
// horizontal adds.
// suppose v = (a, b, c, d)
simd::int4 ab_cd = _mm_hadd_epi32 (v.simd(), v.simd());
// ab_cd = (a+b, c+d, a+b, c+d)
simd::int4 abcd = _mm_hadd_epi32 (ab_cd.simd(), ab_cd.simd());
// all abcd elements are a+b+c+d, return an element as fast as possible
return abcd;
#elif OIIO_SIMD_SSE >= 2
// I think this is the best we can do for SSE2, and I'm still not sure
// it's faster than the default scalar operation. But anyway...
// suppose v = (a, b, c, d)
int4 ab_ab_cd_cd = shuffle<1,0,3,2>(v) + v;
// ab_ab_cd_cd = (b,a,d,c) + (a,b,c,d) = (a+b,a+b,c+d,c+d)
int4 cd_cd_ab_ab = shuffle<2,3,0,1>(ab_ab_cd_cd);
// cd_cd_ab_ab = (c+d,c+d,a+b,a+b)
int4 abcd = ab_ab_cd_cd + cd_cd_ab_ab; // a+b+c+d in all components
return abcd;
#else
SIMD_RETURN_REDUCE (int4, 0, r += v[i]);
#endif
}
OIIO_FORCEINLINE int reduce_add (const int4& v) {
#if OIIO_SIMD_SSE
return extract<0> (vreduce_add(v));
#else
SIMD_RETURN_REDUCE (int, 0, r += v[i]);
#endif
}
OIIO_FORCEINLINE int reduce_and (const int4& v) {
#if OIIO_SIMD_SSE
int4 ab = v & shuffle<1,1,3,3>(v); // ab bb cd dd
int4 abcd = ab & shuffle<2>(ab);
return extract<0>(abcd);
#else
SIMD_RETURN_REDUCE (int, -1, r &= v[i]);
#endif
}
OIIO_FORCEINLINE int reduce_or (const int4& v) {
#if OIIO_SIMD_SSE
int4 ab = v | shuffle<1,1,3,3>(v); // ab bb cd dd
int4 abcd = ab | shuffle<2>(ab);
return extract<0>(abcd);
#else
SIMD_RETURN_REDUCE (int, 0, r |= v[i]);
#endif
}
OIIO_FORCEINLINE int4 blend (const int4& a, const int4& b, const bool4& mask) {
#if OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_castps_si128 (_mm_blendv_ps (_mm_castsi128_ps(a.simd()),
_mm_castsi128_ps(b.simd()), mask));
#elif OIIO_SIMD_SSE
return _mm_or_si128 (_mm_and_si128(_mm_castps_si128(mask.simd()), b.simd()),
_mm_andnot_si128(_mm_castps_si128(mask.simd()), a.simd()));
#else
SIMD_RETURN (int4, mask[i] ? b[i] : a[i]);
#endif
}
OIIO_FORCEINLINE int4 blend0 (const int4& a, const bool4& mask) {
#if OIIO_SIMD_SSE
return _mm_and_si128(_mm_castps_si128(mask), a.simd());
#else
SIMD_RETURN (int4, mask[i] ? a[i] : 0.0f);
#endif
}
OIIO_FORCEINLINE int4 blend0not (const int4& a, const bool4& mask) {
#if OIIO_SIMD_SSE
return _mm_andnot_si128(_mm_castps_si128(mask), a.simd());
#else
SIMD_RETURN (int4, mask[i] ? 0.0f : a[i]);
#endif
}
OIIO_FORCEINLINE int4 select (const bool4& mask, const int4& a, const int4& b) {
return blend (b, a, mask);
}
OIIO_FORCEINLINE int4 abs (const int4& a) {
#if OIIO_SIMD_SSE >= 3
return _mm_abs_epi32(a.simd());
#else
SIMD_RETURN (int4, std::abs(a[i]));
#endif
}
OIIO_FORCEINLINE int4 min (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_min_epi32 (a, b);
#else
SIMD_RETURN (int4, std::min(a[i], b[i]));
#endif
}
OIIO_FORCEINLINE int4 max (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_max_epi32 (a, b);
#else
SIMD_RETURN (int4, std::max(a[i], b[i]));
#endif
}
OIIO_FORCEINLINE int4 rotl32 (const int4& x, const unsigned int k) {
return (x<<k) | srl(x,32-k);
}
OIIO_FORCEINLINE int4 andnot (const int4& a, const int4& b) {
#if OIIO_SIMD_SSE
return _mm_andnot_si128 (a.simd(), b.simd());
#else
SIMD_RETURN (int4, ~(a[i]) & b[i]);
#endif
}
// Implementation had to be after the definition of int4::Zero.
OIIO_FORCEINLINE bool4::bool4 (const int4& ival) {
m_simd = (ival != int4::Zero());
}
//////////////////////////////////////////////////////////////////////
// int8 implementation
OIIO_FORCEINLINE const int8 & int8::operator= (const int8& other) {
m_simd = other.m_simd;
return *this;
}
OIIO_FORCEINLINE int& int8::operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE int int8::operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE void int8::load (int a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_set1_epi32 (a);
#else
SIMD_CONSTRUCT (a);
#endif
}
OIIO_FORCEINLINE void int8::load (int a, int b, int c, int d,
int e, int f, int g, int h) {
#if OIIO_SIMD_AVX
m_simd = _mm256_set_epi32 (h, g, f, e, d, c, b, a);
#else
m_val[0] = a;
m_val[1] = b;
m_val[2] = c;
m_val[3] = d;
m_val[4] = e;
m_val[5] = f;
m_val[6] = g;
m_val[7] = h;
#endif
}
OIIO_FORCEINLINE void int8::load (const int *values) {
#if OIIO_SIMD_AVX
m_simd = _mm256_loadu_si256 ((const simd_t *)values);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void int8::load (const int *values, int n)
{
// FIXME: is this faster with AVX masked loads?
#if OIIO_SIMD_SSE
if (n > 0 && n <= 4) {
int4 l; l.load (values, n);
m_simd = int8(l, int4::Zero());
} else if (n > 4 && n <= 8) {
int4 h; h.load (values+4, n-4);
m_simd = int8(int4(values), h);
}
else
clear();
#else
for (int i = 0; i < n; ++i)
m_val[i] = values[i];
for (int i = n; i < elements; ++i)
m_val[i] = 0;
#endif
}
// FIXME(AVX): fast load from unsigned short, short, unsigned char, char
OIIO_FORCEINLINE void int8::load (const short *values) {
SIMD_CONSTRUCT (values[i]);
}
OIIO_FORCEINLINE void int8::load (const unsigned short *values) {
SIMD_CONSTRUCT (values[i]);
}
OIIO_FORCEINLINE void int8::load (const char *values) {
SIMD_CONSTRUCT (values[i]);
}
OIIO_FORCEINLINE void int8::load (const unsigned char *values) {
SIMD_CONSTRUCT (values[i]);
}
OIIO_FORCEINLINE int8::int8 (int a) { load(a); }
// OIIO_FORCEINLINE int8::int8 (int a, int b, int c, int d) { load(a,b,c,d); }
OIIO_FORCEINLINE int8::int8 (int a, int b, int c, int d,
int e, int f, int g, int h) {
load(a,b,c,d,e,f,g,h);
}
OIIO_FORCEINLINE int8::int8 (const int *vals) { load (vals); }
OIIO_FORCEINLINE int8::int8 (const unsigned short *vals) { load(vals); }
OIIO_FORCEINLINE int8::int8 (const short *vals) { load(vals); }
OIIO_FORCEINLINE int8::int8 (const unsigned char *vals) { load(vals); }
OIIO_FORCEINLINE int8::int8 (const char *vals) { load(vals); }
OIIO_FORCEINLINE const int8 & int8::operator= (int a) { load(a); return *this; }
OIIO_FORCEINLINE void int8::store (int *values) const {
#if OIIO_SIMD_AVX
// Use an unaligned store -- it's just as fast when the memory turns
// out to be aligned, nearly as fast even when unaligned. Not worth
// the headache of using stores that require alignment.
_mm256_storeu_si256 ((simd_t *)values, m_simd);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void int8::clear () {
#if OIIO_SIMD_AVX
m_simd = _mm256_setzero_si256();
#else
*this = 0;
#endif
}
OIIO_FORCEINLINE const int8 int8::Zero () {
#if OIIO_SIMD_AVX
return _mm256_setzero_si256();
#else
return 0;
#endif
}
OIIO_FORCEINLINE const int8 int8::One () { return int8(1); }
OIIO_FORCEINLINE const int8 int8::NegOne () { return int8(-1); }
OIIO_FORCEINLINE const int8 int8::Iota (int start, int step) {
return int8 (start+0*step, start+1*step, start+2*step, start+3*step,
start+4*step, start+5*step, start+6*step, start+7*step);
}
OIIO_FORCEINLINE int4 int8::lo () const {
#if OIIO_SIMD_AVX
return _mm256_castsi256_si128 (simd());
#else
return m_4[0];
#endif
}
OIIO_FORCEINLINE int4 int8::hi () const {
#if OIIO_SIMD_AVX
return _mm256_extractf128_si256 (simd(), 1);
#else
return m_4[1];
#endif
}
OIIO_FORCEINLINE int8::int8 (const int4& lo, const int4 &hi) {
#if OIIO_SIMD_AVX
__m256i r = _mm256_castsi128_si256 (lo);
m_simd = _mm256_insertf128_si256 (r, hi, 1);
// N.B. equivalent, if available: m_simd = _mm256_set_m128i (hi, lo);
#else
m_4[0] = lo;
m_4[1] = hi;
#endif
}
OIIO_FORCEINLINE int8 operator+ (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_add_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] + b[i]);
#endif
}
OIIO_FORCEINLINE const int8& operator+= (int8& a, const int8& b) {
return a = a + b;
}
OIIO_FORCEINLINE int8 operator- (const int8& a) {
#if OIIO_SIMD_AVX >= 2
return _mm256_sub_epi32 (_mm256_setzero_si256(), a);
#else
SIMD_RETURN (int8, -a[i]);
#endif
}
OIIO_FORCEINLINE int8 operator- (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_sub_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] - b[i]);
#endif
}
OIIO_FORCEINLINE const int8 &operator-= (int8& a, const int8& b) {
return a = a - b;
}
OIIO_FORCEINLINE int8 operator* (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_mullo_epi32 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] * b[i]);
#endif
}
OIIO_FORCEINLINE const int8& operator*= (int8& a, const int8& b) { return a = a * b; }
OIIO_FORCEINLINE const int8& operator*= (int8& a, int b) { return a = a * b; }
OIIO_FORCEINLINE int8 operator/ (const int8& a, const int8& b) {
// NO INTEGER DIVISION IN SSE or AVX!
SIMD_RETURN (int8, a[i] / b[i]);
}
OIIO_FORCEINLINE const int8& operator/= (int8& a, const int8& b) { return a = a / b; }
OIIO_FORCEINLINE int8 operator% (const int8& a, const int8& b) {
// NO INTEGER MODULUS IN SSE or AVX!
SIMD_RETURN (int8, a[i] % b[i]);
}
OIIO_FORCEINLINE const int8& operator%= (int8& a, const int8& b) { return a = a % b; }
OIIO_FORCEINLINE int8 operator% (const int8& a, int w) {
// NO INTEGER MODULUS in SSE or AVX!
SIMD_RETURN (int8, a[i] % w);
}
OIIO_FORCEINLINE const int8& operator%= (int8& a, int b) { return a = a % b; }
OIIO_FORCEINLINE int8 operator& (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_and_si256 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] & b[i]);
#endif
}
OIIO_FORCEINLINE const int8& operator&= (int8& a, const int8& b) { return a = a & b; }
OIIO_FORCEINLINE int8 operator| (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_or_si256 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] | b[i]);
#endif
}
OIIO_FORCEINLINE const int8& operator|= (int8& a, const int8& b) { return a = a | b; }
OIIO_FORCEINLINE int8 operator^ (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_xor_si256 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, a[i] ^ b[i]);
#endif
}
OIIO_FORCEINLINE const int8& operator^= (int8& a, const int8& b) { return a = a ^ b; }
OIIO_FORCEINLINE int8 operator~ (const int8& a) {
#if OIIO_SIMD_AVX >= 2
return a ^ a.NegOne();
#else
SIMD_RETURN (int8, ~a[i]);
#endif
}
OIIO_FORCEINLINE int8 operator<< (const int8& a, unsigned int bits) {
#if OIIO_SIMD_AVX >= 2
return _mm256_slli_epi32 (a, bits);
#elif OIIO_SIMD_SSE
return int8 (a.lo() << bits, a.hi() << bits);
#else
SIMD_RETURN (int8, a[i] << bits);
#endif
}
OIIO_FORCEINLINE const int8& operator<<= (int8& a, const unsigned int bits) {
return a = a << bits;
}
OIIO_FORCEINLINE int8 operator>> (const int8& a, const unsigned int bits) {
#if OIIO_SIMD_AVX >= 2
return _mm256_srai_epi32 (a, bits);
#elif OIIO_SIMD_SSE
return int8 (a.lo() >> bits, a.hi() >> bits);
#else
SIMD_RETURN (int8, a[i] >> bits);
#endif
}
OIIO_FORCEINLINE const int8& operator>>= (int8& a, const unsigned int bits) {
return a = a >> bits;
}
OIIO_FORCEINLINE int8 srl (const int8& a, const unsigned int bits) {
#if OIIO_SIMD_AVX >= 2
return _mm256_srli_epi32 (a, bits);
#else
SIMD_RETURN (int8, int ((unsigned int)(a[i]) >> bits));
#endif
}
OIIO_FORCEINLINE bool8 operator== (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_castsi256_ps(_mm256_cmpeq_epi32 (a.m_simd, b.m_simd));
#elif OIIO_SIMD_SSE /* Fall back to 4-wide */
return bool8 (a.lo() == b.lo(), a.hi() == b.hi());
#else
SIMD_RETURN (bool8, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator!= (const int8& a, const int8& b) {
return ! (a == b);
}
OIIO_FORCEINLINE bool8 operator> (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_castsi256_ps(_mm256_cmpgt_epi32 (a, b));
#elif OIIO_SIMD_SSE /* Fall back to 4-wide */
return bool8 (a.lo() > b.lo(), a.hi() > b.hi());
#else
SIMD_RETURN (bool8, a[i] > b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator< (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
// No lt or lte!
return (b > a);
#elif OIIO_SIMD_SSE /* Fall back to 4-wide */
return bool8 (a.lo() < b.lo(), a.hi() < b.hi());
#else
SIMD_RETURN (bool8, a[i] < b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator>= (const int8& a, const int8& b) {
return (a > b) | (a == b);
}
OIIO_FORCEINLINE bool8 operator<= (const int8& a, const int8& b) {
return (b > a) | (a == b);
}
inline std::ostream& operator<< (std::ostream& cout, const int8& val) {
cout << val[0];
for (int i = 1; i < val.elements; ++i)
cout << ' ' << val[i];
return cout;
}
OIIO_FORCEINLINE void int8::store (int *values, int n) const {
DASSERT (n >= 0 && n <= elements);
// FIXME: is this faster with AVX masked stores?
#if OIIO_SIMD_SSE
if (n <= 4) {
lo().store (values, n);
} else if (n <= 8) {
lo().store (values);
hi().store (values+4, n-4);
}
#endif
for (int i = 0; i < n; ++i)
values[i] = m_val[i];
}
// FIXME(AVX): fast int8 store to unsigned short, unsigned char
OIIO_FORCEINLINE void int8::store (unsigned short *values) const {
#if 0 && OIIO_SIMD_AVX >= 2
// FIXME -- try to determine if this is faster:
lo().store (values);
hi().store (values+4);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void int8::store (unsigned char *values) const {
#if OIIO_SIMD_SSE
lo().store (values);
hi().store (values+4);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
OIIO_FORCEINLINE int8 shuffle (const int8& a) {
#if OIIO_SIMD_AVX >= 2
int8 index (i0, i1, i2, i3, i4, i5, i6, i7);
return _mm256_castps_si256 (_mm256_permutevar8x32_ps (_mm256_castsi256_ps(a.simd()), index.simd()));
#else
return int8 (a[i0], a[i1], a[i2], a[i3], a[i4], a[i5], a[i6], a[i7]);
#endif
}
template<int i> OIIO_FORCEINLINE int8 shuffle (const int8& a) {
return shuffle<i,i,i,i,i,i,i,i>(a);
}
template<int i>
OIIO_FORCEINLINE int extract (const int8& v) {
#if OIIO_SIMD_AVX && !_WIN32
return _mm256_extract_epi32(v.simd(), i);
#else
return v[i];
#endif
}
template<int i>
OIIO_FORCEINLINE int8 insert (const int8& a, int val) {
#if OIIO_SIMD_AVX && !_WIN32
return _mm256_insert_epi32 (a.simd(), val, i);
#else
int8 tmp = a;
tmp[i] = val;
return tmp;
#endif
}
OIIO_FORCEINLINE int int8::x () const { return extract<0>(*this); }
OIIO_FORCEINLINE int int8::y () const { return extract<1>(*this); }
OIIO_FORCEINLINE int int8::z () const { return extract<2>(*this); }
OIIO_FORCEINLINE int int8::w () const { return extract<3>(*this); }
OIIO_FORCEINLINE void int8::set_x (int val) { *this = insert<0>(*this, val); }
OIIO_FORCEINLINE void int8::set_y (int val) { *this = insert<1>(*this, val); }
OIIO_FORCEINLINE void int8::set_z (int val) { *this = insert<2>(*this, val); }
OIIO_FORCEINLINE void int8::set_w (int val) { *this = insert<3>(*this, val); }
OIIO_FORCEINLINE int8 bitcast_to_int (const bool8& x)
{
#if OIIO_SIMD_AVX
return _mm256_castps_si256 (x.simd());
#else
return *(int8 *)&x;
#endif
}
OIIO_FORCEINLINE int8 vreduce_add (const int8& v) {
#if OIIO_SIMD_AVX >= 2
// From Syrah:
int8 ab_cd_0_0_ef_gh_0_0 = _mm256_hadd_epi32(v.simd(), _mm256_setzero_si256());
int8 abcd_0_0_0_efgh_0_0_0 = _mm256_hadd_epi32(ab_cd_0_0_ef_gh_0_0, _mm256_setzero_si256());
// get efgh in the 0-idx slot
int8 efgh = shuffle<4>(abcd_0_0_0_efgh_0_0_0);
int8 final_sum = abcd_0_0_0_efgh_0_0_0 + efgh;
return shuffle<0>(final_sum);
#elif OIIO_SIMD_SSE
int4 hadd4 = vreduce_add(v.lo()) + vreduce_add(v.hi());
return int8(hadd4, hadd4);
#else
SIMD_RETURN_REDUCE (int8, 0, r += v[i]);
#endif
}
OIIO_FORCEINLINE int reduce_add (const int8& v) {
#if OIIO_SIMD_SSE
return extract<0> (vreduce_add(v));
#else
SIMD_RETURN_REDUCE (int, 0, r += v[i]);
#endif
}
OIIO_FORCEINLINE int reduce_and (const int8& v) {
#if OIIO_SSE_AVX >= 2
int8 ab = v & shuffle<1,1,3,3,5,5,7,7>(v); // ab bb cd dd ef ff gh hh
int8 abcd = ab & shuffle<2,2,2,2,6,6,6,6>(ab); // abcd x x x efgh x x x
int8 abcdefgh = abcd & shuffle<4>(abcdefgh); // abcdefgh x x x x x x x
return extract<0> (abcdefgh);
#else
// AVX 1.0 or less -- use SSE
return reduce_and(v.lo() & v.hi());
#endif
}
OIIO_FORCEINLINE int reduce_or (const int8& v) {
#if OIIO_SSE_AVX >= 2
int8 ab = v | shuffle<1,1,3,3,5,5,7,7>(v); // ab bb cd dd ef ff gh hh
int8 abcd = ab | shuffle<2,2,2,2,6,6,6,6>(ab); // abcd x x x efgh x x x
int8 abcdefgh = abcd | shuffle<4>(abcdefgh); // abcdefgh x x x x x x x
return extract<0> (abcdefgh);
#else
// AVX 1.0 or less -- use SSE
return reduce_or(v.lo() | v.hi());
#endif
}
OIIO_FORCEINLINE int8 blend (const int8& a, const int8& b, const bool8& mask) {
#if OIIO_SIMD_AVX
return _mm256_castps_si256 (_mm256_blendv_ps (_mm256_castsi256_ps(a.simd()),
_mm256_castsi256_ps(b.simd()), mask));
#else
SIMD_RETURN (int8, mask[i] ? b[i] : a[i]);
#endif
}
OIIO_FORCEINLINE int8 blend0 (const int8& a, const bool8& mask) {
#if OIIO_SIMD_AVX
return _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(a.simd()), mask));
#else
SIMD_RETURN (int8, mask[i] ? a[i] : 0.0f);
#endif
}
OIIO_FORCEINLINE int8 blend0not (const int8& a, const bool8& mask) {
#if OIIO_SIMD_AVX
return _mm256_castps_si256 (_mm256_andnot_ps (mask.simd(), _mm256_castsi256_ps(a.simd())));
#else
SIMD_RETURN (int8, mask[i] ? 0.0f : a[i]);
#endif
}
OIIO_FORCEINLINE int8 select (const bool8& mask, const int8& a, const int8& b) {
return blend (b, a, mask);
}
OIIO_FORCEINLINE int8 abs (const int8& a) {
#if OIIO_SIMD_AVX >= 2
return _mm256_abs_epi32(a.simd());
#else
SIMD_RETURN (int8, std::abs(a[i]));
#endif
}
OIIO_FORCEINLINE int8 min (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_min_epi32 (a, b);
#else
SIMD_RETURN (int8, std::min(a[i], b[i]));
#endif
}
OIIO_FORCEINLINE int8 max (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_max_epi32 (a, b);
#else
SIMD_RETURN (int8, std::max(a[i], b[i]));
#endif
}
OIIO_FORCEINLINE int8 rotl32 (const int8& x, const unsigned int k) {
return (x<<k) | srl(x,32-k);
}
OIIO_FORCEINLINE int8 andnot (const int8& a, const int8& b) {
#if OIIO_SIMD_AVX >= 2
return _mm256_andnot_si256 (a.simd(), b.simd());
#else
SIMD_RETURN (int8, ~(a[i]) & b[i]);
#endif
}
// Implementation had to be after the definition of int8::Zero.
OIIO_FORCEINLINE bool8::bool8 (const int8& ival) {
m_simd = (ival != int8::Zero());
}
//////////////////////////////////////////////////////////////////////
// float4 implementation
OIIO_FORCEINLINE float4::float4 (const int4& ival) {
#if OIIO_SIMD_SSE
m_simd = _mm_cvtepi32_ps (ival.simd());
#else
SIMD_CONSTRUCT (float(ival[i]));
#endif
}
OIIO_FORCEINLINE const float4 float4::Zero () {
#if OIIO_SIMD_SSE
return _mm_setzero_ps();
#else
return float4(0.0f);
#endif
}
OIIO_FORCEINLINE const float4 float4::One () {
return float4(1.0f);
}
OIIO_FORCEINLINE const float4 float4::Iota (float start, float step) {
return float4 (start+0.0f*step, start+1.0f*step, start+2.0f*step, start+3.0f*step);
}
/// Set all components to 0.0
OIIO_FORCEINLINE void float4::clear () {
#if OIIO_SIMD_SSE
m_simd = _mm_setzero_ps();
#else
load (0.0f);
#endif
}
#if defined(ILMBASE_VERSION_MAJOR) && ILMBASE_VERSION_MAJOR >= 2
OIIO_FORCEINLINE const float4 & float4::operator= (const Imath::V4f &v) {
load ((const float *)&v);
return *this;
}
#endif
OIIO_FORCEINLINE const float4 & float4::operator= (const Imath::V3f &v) {
load (v[0], v[1], v[2], 0.0f);
return *this;
}
OIIO_FORCEINLINE float& float4::operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE float float4::operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE void float4::load (float val) {
#if OIIO_SIMD_SSE
m_simd = _mm_set1_ps (val);
#elif defined(OIIO_SIMD_NEON)
m_simd = vdupq_n_f32 (val);
#else
SIMD_CONSTRUCT (val);
#endif
}
OIIO_FORCEINLINE void float4::load (float a, float b, float c, float d) {
#if OIIO_SIMD_SSE
m_simd = _mm_set_ps (d, c, b, a);
#elif defined(OIIO_SIMD_NEON)
float values[4] = { a, b, c, d };
m_simd = vld1q_f32 (values);
#else
m_val[0] = a;
m_val[1] = b;
m_val[2] = c;
m_val[3] = d;
#endif
}
/// Load from an array of 4 values
OIIO_FORCEINLINE void float4::load (const float *values) {
#if OIIO_SIMD_SSE
m_simd = _mm_loadu_ps (values);
#elif defined(OIIO_SIMD_NEON)
m_simd = vld1q_f32 (values);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float4::load (const float *values, int n) {
#if OIIO_SIMD_SSE
switch (n) {
case 1:
m_simd = _mm_load_ss (values);
break;
case 2:
// Trickery: load one double worth of bits!
m_simd = _mm_castpd_ps (_mm_load_sd ((const double*)values));
break;
case 3:
m_simd = _mm_setr_ps (values[0], values[1], values[2], 0.0f);
// This looks wasteful, but benchmarks show that it's the
// fastest way to set 3 values with the 4th getting zero.
// Actually, gcc and clang both turn it into something more
// efficient than _mm_setr_ps. The version below looks smart,
// but was much more expensive as the _mm_setr_ps!
// __m128 xy = _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)values));
// m_simd = _mm_movelh_ps(xy, _mm_load_ss (values + 2));
break;
case 4:
m_simd = _mm_loadu_ps (values);
break;
default:
break;
}
#elif defined(OIIO_SIMD_NEON)
switch (n) {
case 1: m_simd = vdupq_n_f32 (val); break;
case 2: load (values[0], values[1], 0.0f, 0.0f); break;
case 3: load (values[0], values[1], values[2], 0.0f); break;
case 4: m_simd = vld1q_f32 (values); break;
default: break;
}
#else
for (int i = 0; i < n; ++i)
m_val[i] = values[i];
for (int i = n; i < paddedelements; ++i)
m_val[i] = 0;
#endif
}
OIIO_FORCEINLINE void float4::load (const unsigned short *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_simd = _mm_cvtepi32_ps (int4(values).simd());
// You might guess that the following is faster, but it's NOT:
// NO! m_simd = _mm_cvtpu16_ps (*(__m64*)values);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float4::load (const short *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_simd = _mm_cvtepi32_ps (int4(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float4::load (const unsigned char *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_simd = _mm_cvtepi32_ps (int4(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
/// Load from an array of 4 char values, convert to float
OIIO_FORCEINLINE void float4::load (const char *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_simd = _mm_cvtepi32_ps (int4(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float4::load (const half *values) {
#if defined(__F16C__) && defined(OIIO_SIMD_SSE)
/* Enabled 16 bit float instructions! */
__m128i a = _mm_castpd_si128 (_mm_load_sd ((const double *)values));
m_simd = _mm_cvtph_ps (a);
#elif defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
// SSE half-to-float by Fabian "ryg" Giesen. Public domain.
// https://gist.github.com/rygorous/2144712
int4 h ((const unsigned short *)values);
# define CONSTI(name) *(const __m128i *)&name
# define CONSTF(name) *(const __m128 *)&name
OIIO_SIMD_UINT4_CONST(mask_nosign, 0x7fff);
OIIO_SIMD_UINT4_CONST(magic, (254 - 15) << 23);
OIIO_SIMD_UINT4_CONST(was_infnan, 0x7bff);
OIIO_SIMD_UINT4_CONST(exp_infnan, 255 << 23);
__m128i mnosign = CONSTI(mask_nosign);
__m128i expmant = _mm_and_si128(mnosign, h);
__m128i justsign = _mm_xor_si128(h, expmant);
__m128i expmant2 = expmant; // copy (just here for counting purposes)
__m128i shifted = _mm_slli_epi32(expmant, 13);
__m128 scaled = _mm_mul_ps(_mm_castsi128_ps(shifted), *(const __m128 *)&magic);
__m128i b_wasinfnan = _mm_cmpgt_epi32(expmant2, CONSTI(was_infnan));
__m128i sign = _mm_slli_epi32(justsign, 16);
__m128 infnanexp = _mm_and_ps(_mm_castsi128_ps(b_wasinfnan), CONSTF(exp_infnan));
__m128 sign_inf = _mm_or_ps(_mm_castsi128_ps(sign), infnanexp);
__m128 final = _mm_or_ps(scaled, sign_inf);
// ~11 SSE2 ops.
m_simd = final;
# undef CONSTI
# undef CONSTF
#else /* No SIMD defined: */
SIMD_CONSTRUCT (values[i]);
#endif
}
#endif /* _HALF_H_ */
OIIO_FORCEINLINE void float4::store (float *values) const {
#if OIIO_SIMD_SSE
// Use an unaligned store -- it's just as fast when the memory turns
// out to be aligned, nearly as fast even when unaligned. Not worth
// the headache of using stores that require alignment.
_mm_storeu_ps (values, m_simd);
#elif defined(OIIO_SIMD_NEON)
vst1q_f32 (values, m_simd);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void float4::store (float *values, int n) const {
DASSERT (n >= 0 && n <= 4);
#if OIIO_SIMD_SSE
switch (n) {
case 1:
_mm_store_ss (values, m_simd);
break;
case 2:
// Trickery: store two floats as a double worth of bits
_mm_store_sd ((double*)values, _mm_castps_pd(m_simd));
break;
case 3:
values[0] = m_val[0];
values[1] = m_val[1];
values[2] = m_val[2];
// This looks wasteful, but benchmarks show that it's the
// fastest way to store 3 values, in benchmarks was faster than
// this, below:
// _mm_store_sd ((double*)values, _mm_castps_pd(m_simd));
// _mm_store_ss (values + 2, _mm_movehl_ps(m_simd,m_simd));
break;
case 4:
store (values);
break;
default:
break;
}
#elif defined(OIIO_SIMD_NEON)
switch (n) {
case 1:
vst1q_lane_f32 (values, m_simd, 0);
break;
case 2:
vst1q_lane_f32 (values++, m_simd, 0);
vst1q_lane_f32 (values, m_simd, 1);
break;
case 3:
vst1q_lane_f32 (values++, m_simd, 0);
vst1q_lane_f32 (values++, m_simd, 1);
vst1q_lane_f32 (values, m_simd, 2);
break;
case 4:
vst1q_f32 (values, m_simd); break;
default:
break;
}
#else
for (int i = 0; i < n; ++i)
values[i] = m_val[i];
#endif
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float4::store (half *values) const {
#if defined(__F16C__) && defined(OIIO_SIMD_SSE)
__m128i h = _mm_cvtps_ph (m_simd, (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC));
_mm_store_sd ((double *)values, _mm_castsi128_pd(h));
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
#endif
OIIO_FORCEINLINE float4 operator+ (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_add_ps (a.m_simd, b.m_simd);
#elif defined(OIIO_SIMD_NEON)
return vaddq_f32 (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float4, a[i] + b[i]);
#endif
}
OIIO_FORCEINLINE const float4 & float4::operator+= (const float4& a) {
#if OIIO_SIMD_SSE
m_simd = _mm_add_ps (m_simd, a.m_simd);
#elif defined(OIIO_SIMD_NEON)
m_simd = vaddq_f32 (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] += a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE float4 float4::operator- () const {
#if OIIO_SIMD_SSE
return _mm_sub_ps (_mm_setzero_ps(), m_simd);
#elif defined(OIIO_SIMD_NEON)
return vsubq_f32 (Zero(), m_simd);
#else
SIMD_RETURN (float4, -m_val[i]);
#endif
}
OIIO_FORCEINLINE float4 operator- (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_sub_ps (a.m_simd, b.m_simd);
#elif defined(OIIO_SIMD_NEON)
return vsubq_f32 (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float4, a[i] - b[i]);
#endif
}
OIIO_FORCEINLINE const float4 & float4::operator-= (const float4& a) {
#if OIIO_SIMD_SSE
m_simd = _mm_sub_ps (m_simd, a.m_simd);
#elif defined(OIIO_SIMD_NEON)
m_simd = vsubq_f32 (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] -= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE float4 operator* (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_mul_ps (a.m_simd, b.m_simd);
#elif defined(OIIO_SIMD_NEON)
return vmulq_f32 (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float4, a[i] * b[i]);
#endif
}
OIIO_FORCEINLINE const float4 & float4::operator*= (const float4& a) {
#if OIIO_SIMD_SSE
m_simd = _mm_mul_ps (m_simd, a.m_simd);
#elif defined(OIIO_SIMD_NEON)
m_simd = vmulq_f32 (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] *= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE const float4 & float4::operator*= (float val) {
#if OIIO_SIMD_SSE
m_simd = _mm_mul_ps (m_simd, _mm_set1_ps(val));
#elif defined(OIIO_SIMD_NEON)
m_simd = vmulq_f32 (m_simd, vdupq_n_f32(val));
#else
SIMD_DO (m_val[i] *= val);
#endif
return *this;
}
OIIO_FORCEINLINE float4 operator/ (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_div_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float4, a[i] / b[i]);
#endif
}
OIIO_FORCEINLINE const float4 & float4::operator/= (const float4& a) {
#if OIIO_SIMD_SSE
m_simd = _mm_div_ps (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] /= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE const float4 & float4::operator/= (float val) {
#if OIIO_SIMD_SSE
m_simd = _mm_div_ps (m_simd, _mm_set1_ps(val));
#else
SIMD_DO (m_val[i] /= val);
#endif
return *this;
}
OIIO_FORCEINLINE bool4 operator== (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmpeq_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator!= (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmpneq_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] != b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator< (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmplt_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] < b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator> (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmpgt_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] > b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator>= (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmpge_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] >= b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool4 operator<= (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_cmple_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (bool4, a[i] <= b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE float4 AxyBxy (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_movelh_ps (a.m_simd, b.m_simd);
#else
return float4 (a[0], a[1], b[0], b[1]);
#endif
}
OIIO_FORCEINLINE float4 AxBxAyBy (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_unpacklo_ps (a.m_simd, b.m_simd);
#else
return float4 (a[0], b[0], a[1], b[1]);
#endif
}
OIIO_FORCEINLINE float4 float4::xyz0 () const {
return insert<3>(*this, 0.0f);
}
OIIO_FORCEINLINE float4 float4::xyz1 () const {
return insert<3>(*this, 1.0f);
}
inline std::ostream& operator<< (std::ostream& cout, const float4& val) {
cout << val[0];
for (int i = 1; i < val.elements; ++i)
cout << ' ' << val[i];
return cout;
}
// Implementation had to be after the definition of float4.
OIIO_FORCEINLINE int4::int4 (const float4& f)
{
#if OIIO_SIMD_SSE
m_simd = _mm_cvttps_epi32(f.simd());
#else
SIMD_CONSTRUCT ((int) f[i]);
#endif
}
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE float4 shuffle (const float4& a) {
#if OIIO_SIMD_SSE
return shuffle_sse<i0,i1,i2,i3> (__m128(a));
#else
return float4(a[i0], a[i1], a[i2], a[i3]);
#endif
}
template<int i> OIIO_FORCEINLINE float4 shuffle (const float4& a) { return shuffle<i,i,i,i>(a); }
#if defined(OIIO_SIMD_NEON)
template<> OIIO_FORCEINLINE float4 shuffle<0> (const float4& a) {
float32x2_t t = vget_low_f32(a.m_simd); return vdupq_lane_f32(t,0);
}
template<> OIIO_FORCEINLINE float4 shuffle<1> (const float4& a) {
float32x2_t t = vget_low_f32(a.m_simd); return vdupq_lane_f32(t,1);
}
template<> OIIO_FORCEINLINE float4 shuffle<2> (const float4& a) {
float32x2_t t = vget_high_f32(a.m_simd); return vdupq_lane_f32(t,0);
}
template<> OIIO_FORCEINLINE float4 shuffle<3> (const float4& a) {
float32x2_t t = vget_high_f32(a.m_simd); return vdupq_lane_f32(t,1);
}
#endif
/// Helper: as rapid as possible extraction of one component, when the
/// index is fixed.
template<int i>
OIIO_FORCEINLINE float extract (const float4& a) {
#if OIIO_SIMD_SSE
return _mm_cvtss_f32(shuffle_sse<i,i,i,i>(a.simd()));
#else
return a[i];
#endif
}
#if OIIO_SIMD_SSE
template<> OIIO_FORCEINLINE float extract<0> (const float4& a) {
return _mm_cvtss_f32(a.simd());
}
#endif
/// Helper: substitute val for a[i]
template<int i>
OIIO_FORCEINLINE float4 insert (const float4& a, float val) {
#if OIIO_SIMD_SSE >= 4
return _mm_insert_ps (a, _mm_set_ss(val), i<<4);
#else
float4 tmp = a;
tmp[i] = val;
return tmp;
#endif
}
#if OIIO_SIMD_SSE
// Slightly faster special cases for SSE
template<> OIIO_FORCEINLINE float4 insert<0> (const float4& a, float val) {
return _mm_move_ss (a.simd(), _mm_set_ss(val));
}
#endif
OIIO_FORCEINLINE float float4::x () const { return extract<0>(*this); }
OIIO_FORCEINLINE float float4::y () const { return extract<1>(*this); }
OIIO_FORCEINLINE float float4::z () const { return extract<2>(*this); }
OIIO_FORCEINLINE float float4::w () const { return extract<3>(*this); }
OIIO_FORCEINLINE void float4::set_x (float val) { *this = insert<0>(*this, val); }
OIIO_FORCEINLINE void float4::set_y (float val) { *this = insert<1>(*this, val); }
OIIO_FORCEINLINE void float4::set_z (float val) { *this = insert<2>(*this, val); }
OIIO_FORCEINLINE void float4::set_w (float val) { *this = insert<3>(*this, val); }
OIIO_FORCEINLINE int4 bitcast_to_int (const float4& x)
{
#if OIIO_SIMD_SSE
return _mm_castps_si128 (x.simd());
#else
return *(int4 *)&x;
#endif
}
OIIO_FORCEINLINE float4 bitcast_to_float (const int4& x)
{
#if OIIO_SIMD_SSE
return _mm_castsi128_ps (x.simd());
#else
return *(float4 *)&x;
#endif
}
// Old names:
inline int4 bitcast_to_int4 (const float4& x) { return bitcast_to_int(x); }
inline float4 bitcast_to_float4 (const int4& x) { return bitcast_to_float(x); }
OIIO_FORCEINLINE float4 vreduce_add (const float4& v) {
#if OIIO_SIMD_SSE >= 3
// People seem to agree that SSE3 does add reduction best with 2
// horizontal adds.
// suppose v = (a, b, c, d)
simd::float4 ab_cd = _mm_hadd_ps (v.simd(), v.simd());
// ab_cd = (a+b, c+d, a+b, c+d)
simd::float4 abcd = _mm_hadd_ps (ab_cd.simd(), ab_cd.simd());
// all abcd elements are a+b+c+d
return abcd;
#elif defined(OIIO_SIMD_SSE)
// I think this is the best we can do for SSE2, and I'm still not sure
// it's faster than the default scalar operation. But anyway...
// suppose v = (a, b, c, d)
float4 ab_ab_cd_cd = shuffle<1,0,3,2>(v) + v;
// now x = (b,a,d,c) + (a,b,c,d) = (a+b,a+b,c+d,c+d)
float4 cd_cd_ab_ab = shuffle<2,3,0,1>(ab_ab_cd_cd);
// now y = (c+d,c+d,a+b,a+b)
float4 abcd = ab_ab_cd_cd + cd_cd_ab_ab; // a+b+c+d in all components
return abcd;
#else
return float4 (v[0] + v[1] + v[2] + v[3]);
#endif
}
OIIO_FORCEINLINE float reduce_add (const float4& v) {
#if OIIO_SIMD_SSE
return _mm_cvtss_f32(vreduce_add (v));
#else
return v[0] + v[1] + v[2] + v[3];
#endif
}
OIIO_FORCEINLINE float4 vdot (const float4 &a, const float4 &b) {
#if OIIO_SIMD_SSE >= 4
return _mm_dp_ps (a.simd(), b.simd(), 0xff);
#elif OIIO_SIMD_NEON
float32x4_t ab = vmulq_f32(a, b);
float32x4_t sum1 = vaddq_f32(ab, vrev64q_f32(ab));
return vaddq_f32(sum1, vcombine_f32(vget_high_f32(sum1), vget_low_f32(sum1)));
#else
return vreduce_add (a*b);
#endif
}
OIIO_FORCEINLINE float dot (const float4 &a, const float4 &b) {
#if OIIO_SIMD_SSE >= 4
return _mm_cvtss_f32 (_mm_dp_ps (a.simd(), b.simd(), 0xff));
#else
return reduce_add (a*b);
#endif
}
OIIO_FORCEINLINE float4 vdot3 (const float4 &a, const float4 &b) {
#if OIIO_SIMD_SSE >= 4
return _mm_dp_ps (a.simd(), b.simd(), 0x7f);
#else
return vreduce_add((a*b).xyz0());
#endif
}
OIIO_FORCEINLINE float dot3 (const float4 &a, const float4 &b) {
#if OIIO_SIMD_SSE >= 4
return _mm_cvtss_f32 (_mm_dp_ps (a.simd(), b.simd(), 0x77));
#else
return reduce_add ((a*b).xyz0());
#endif
}
OIIO_FORCEINLINE float4 blend (const float4& a, const float4& b, const bool4& mask)
{
#if OIIO_SIMD_SSE >= 4
// SSE >= 4.1 only
return _mm_blendv_ps (a.simd(), b.simd(), mask.simd());
#elif defined(OIIO_SIMD_SSE)
// Trick for SSE < 4.1
return _mm_or_ps (_mm_and_ps(mask.simd(), b.simd()),
_mm_andnot_ps(mask.simd(), a.simd()));
#else
return float4 (mask[0] ? b[0] : a[0],
mask[1] ? b[1] : a[1],
mask[2] ? b[2] : a[2],
mask[3] ? b[3] : a[3]);
#endif
}
OIIO_FORCEINLINE float4 blend0 (const float4& a, const bool4& mask)
{
#if OIIO_SIMD_SSE
return _mm_and_ps(mask.simd(), a.simd());
#else
return float4 (mask[0] ? a[0] : 0.0f,
mask[1] ? a[1] : 0.0f,
mask[2] ? a[2] : 0.0f,
mask[3] ? a[3] : 0.0f);
#endif
}
OIIO_FORCEINLINE float4 blend0not (const float4& a, const bool4& mask)
{
#if OIIO_SIMD_SSE
return _mm_andnot_ps(mask.simd(), a.simd());
#else
return float4 (mask[0] ? 0.0f : a[0],
mask[1] ? 0.0f : a[1],
mask[2] ? 0.0f : a[2],
mask[3] ? 0.0f : a[3]);
#endif
}
OIIO_FORCEINLINE float4 safe_div (const float4 &a, const float4 &b) {
#if OIIO_SIMD_SSE
return blend0not (a/b, b == float4::Zero());
#else
return float4 (b[0] == 0.0f ? 0.0f : a[0] / b[0],
b[1] == 0.0f ? 0.0f : a[1] / b[1],
b[2] == 0.0f ? 0.0f : a[2] / b[2],
b[3] == 0.0f ? 0.0f : a[3] / b[3]);
#endif
}
OIIO_FORCEINLINE float3 hdiv (const float4 &a)
{
#if OIIO_SIMD_SSE
return float3(safe_div(a, shuffle<3>(a)).xyz0());
#else
float d = a[3];
return d == 0.0f ? float3 (0.0f) : float3 (a[0]/d, a[1]/d, a[2]/d);
#endif
}
OIIO_FORCEINLINE float4 select (const bool4& mask, const float4& a, const float4& b)
{
return blend (b, a, mask);
}
OIIO_FORCEINLINE float4 abs (const float4& a)
{
#if OIIO_SIMD_SSE
// Just clear the sign bit for cheap fabsf
return _mm_and_ps (a.simd(), _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)));
#else
SIMD_RETURN (float4, fabsf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 sign (const float4& a)
{
float4 one(1.0f);
return blend (one, -one, a < float4::Zero());
}
OIIO_FORCEINLINE float4 ceil (const float4& a)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_ceil_ps (a);
#else
SIMD_RETURN (float4, ceilf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 floor (const float4& a)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_floor_ps (a);
#else
SIMD_RETURN (float4, floorf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 round (const float4& a)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_round_ps (a, (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC));
#else
SIMD_RETURN (float4, roundf(a[i]));
#endif
}
OIIO_FORCEINLINE int4 floori (const float4& a)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return int4(floor(a));
#elif defined(OIIO_SIMD_SSE) /* SSE2/3 */
int4 i (a); // truncates
int4 isneg = bitcast_to_int (a < float4::Zero());
return i + isneg;
// The trick here (thanks, Cycles, for letting me spy on your code) is
// that the comparison will return (int)-1 for components that are less
// than zero, and adding that is the same as subtracting one!
#else
SIMD_RETURN (int4, (int)floorf(a[i]));
#endif
}
OIIO_FORCEINLINE int4 rint (const float4& a)
{
return int4 (round(a));
}
OIIO_FORCEINLINE float4 sqrt (const float4 &a)
{
#if OIIO_SIMD_SSE
return _mm_sqrt_ps (a.simd());
#else
SIMD_RETURN (float4, sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 rsqrt (const float4 &a)
{
#if OIIO_SIMD_SSE
return _mm_div_ps (_mm_set1_ps(1.0f), _mm_sqrt_ps (a.simd()));
#else
SIMD_RETURN (float4, 1.0f/sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 rsqrt_fast (const float4 &a)
{
#if OIIO_SIMD_SSE
return _mm_rsqrt_ps (a.simd());
#else
SIMD_RETURN (float4, 1.0f/sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float4 min (const float4& a, const float4& b)
{
#if OIIO_SIMD_SSE
return _mm_min_ps (a, b);
#else
SIMD_RETURN (float4, std::min (a[i], b[i]));
#endif
}
OIIO_FORCEINLINE float4 max (const float4& a, const float4& b)
{
#if OIIO_SIMD_SSE
return _mm_max_ps (a, b);
#else
SIMD_RETURN (float4, std::max (a[i], b[i]));
#endif
}
OIIO_FORCEINLINE float4 andnot (const float4& a, const float4& b) {
#if OIIO_SIMD_SSE
return _mm_andnot_ps (a.simd(), b.simd());
#else
const int *ai = (const int *)&a;
const int *bi = (const int *)&b;
return bitcast_to_float (int4(~(ai[0]) & bi[0],
~(ai[1]) & bi[1],
~(ai[2]) & bi[2],
~(ai[3]) & bi[3]));
#endif
}
OIIO_FORCEINLINE float4 madd (const simd::float4& a, const simd::float4& b,
const simd::float4& c)
{
#if OIIO_SIMD_SSE && OIIO_FMA_ENABLED
// If we are sure _mm_fmadd_ps intrinsic is available, use it.
return _mm_fmadd_ps (a, b, c);
#elif OIIO_SIMD_SSE && !defined(_MSC_VER)
// If we directly access the underlying __m128, on some platforms and
// compiler flags, it will turn into fma anyway, even if we don't use
// the intrinsic.
return a.simd() * b.simd() + c.simd();
#else
// Fallback: just use regular math and hope for the best.
return a * b + c;
#endif
}
OIIO_FORCEINLINE float4 msub (const simd::float4& a, const simd::float4& b,
const simd::float4& c)
{
#if OIIO_SIMD_SSE && OIIO_FMA_ENABLED
// If we are sure _mm_fnmsub_ps intrinsic is available, use it.
return _mm_fmsub_ps (a, b, c);
#elif OIIO_SIMD_SSE && !defined(_MSC_VER)
// If we directly access the underlying __m128, on some platforms and
// compiler flags, it will turn into fma anyway, even if we don't use
// the intrinsic.
return a.simd() * b.simd() - c.simd();
#else
// Fallback: just use regular math and hope for the best.
return a * b - c;
#endif
}
OIIO_FORCEINLINE float4 nmadd (const simd::float4& a, const simd::float4& b,
const simd::float4& c)
{
#if OIIO_SIMD_SSE && OIIO_FMA_ENABLED
// If we are sure _mm_fnmadd_ps intrinsic is available, use it.
return _mm_fnmadd_ps (a, b, c);
#elif OIIO_SIMD_SSE && !defined(_MSC_VER)
// If we directly access the underlying __m128, on some platforms and
// compiler flags, it will turn into fma anyway, even if we don't use
// the intrinsic.
return c.simd() - a.simd() * b.simd();
#else
// Fallback: just use regular math and hope for the best.
return c - a * b;
#endif
}
OIIO_FORCEINLINE float4 nmsub (const simd::float4& a, const simd::float4& b,
const simd::float4& c)
{
#if OIIO_SIMD_SSE && OIIO_FMA_ENABLED
// If we are sure _mm_fnmsub_ps intrinsic is available, use it.
return _mm_fnmsub_ps (a, b, c);
#elif OIIO_SIMD_SSE && !defined(_MSC_VER)
// If we directly access the underlying __m128, on some platforms and
// compiler flags, it will turn into fma anyway, even if we don't use
// the intrinsic.
return -(a.simd() * b.simd()) - c.simd();
#else
// Fallback: just use regular math and hope for the best.
return -(a * b) - c;
#endif
}
// Full precision exp() of all components of a SIMD vector.
template<typename T>
OIIO_FORCEINLINE T exp (const T& v)
{
#if OIIO_SIMD_SSE
// Implementation inspired by:
// https://github.com/embree/embree/blob/master/common/simd/sse_special.h
// Which is listed as Copyright (C) 2007 Julien Pommier and distributed
// under the zlib license.
typedef typename T::int_t int_t;
T x = v;
const float exp_hi (88.3762626647949f);
const float exp_lo (-88.3762626647949f);
const float cephes_LOG2EF (1.44269504088896341f);
const float cephes_exp_C1 (0.693359375f);
const float cephes_exp_C2 (-2.12194440e-4f);
const float cephes_exp_p0 (1.9875691500E-4f);
const float cephes_exp_p1 (1.3981999507E-3f);
const float cephes_exp_p2 (8.3334519073E-3f);
const float cephes_exp_p3 (4.1665795894E-2f);
const float cephes_exp_p4 (1.6666665459E-1f);
const float cephes_exp_p5 (5.0000001201E-1f);
T tmp (0.0f);
T one (1.0f);
x = min (x, T(exp_hi));
x = max (x, T(exp_lo));
T fx = madd (x, T(cephes_LOG2EF), T(0.5f));
int_t emm0 = int_t(fx);
tmp = T(emm0);
T mask = bitcast_to_float (bitcast_to_int(tmp > fx) & bitcast_to_int(one));
fx = tmp - mask;
tmp = fx * cephes_exp_C1;
T z = fx * cephes_exp_C2;
x = x - tmp;
x = x - z;
z = x * x;
T y = cephes_exp_p0;
y = madd (y, x, cephes_exp_p1);
y = madd (y, x, cephes_exp_p2);
y = madd (y, x, cephes_exp_p3);
y = madd (y, x, cephes_exp_p4);
y = madd (y, x, cephes_exp_p5);
y = madd (y, z, x);
y = y + one;
emm0 = (int_t(fx) + int_t(0x7f)) << 23;
T pow2n = bitcast_to_float(emm0);
y = y * pow2n;
return y;
#else
SIMD_RETURN (T, expf(v[i]));
#endif
}
// Full precision log() of all components of a SIMD vector.
template<typename T>
OIIO_FORCEINLINE T log (const T& v)
{
#if OIIO_SIMD_SSE
// Implementation inspired by:
// https://github.com/embree/embree/blob/master/common/simd/sse_special.h
// Which is listed as Copyright (C) 2007 Julien Pommier and distributed
// under the zlib license.
typedef typename T::int_t int_t;
typedef typename T::bool_t bool_t;
T x = v;
int_t emm0;
T zero (T::Zero());
T one (1.0f);
bool_t invalid_mask = (x <= zero);
const int min_norm_pos ((int)0x00800000);
const int inv_mant_mask ((int)~0x7f800000);
x = max(x, bitcast_to_float(int_t(min_norm_pos))); /* cut off denormalized stuff */
emm0 = srl (bitcast_to_int(x), 23);
/* keep only the fractional part */
x = bitcast_to_float (bitcast_to_int(x) & int_t(inv_mant_mask));
x = bitcast_to_float (bitcast_to_int(x) | bitcast_to_int(T(0.5f)));
emm0 = emm0 - int_t(0x7f);
T e (emm0);
e = e + one;
// OIIO_SIMD_FLOAT4_CONST (cephes_SQRTHF, 0.707106781186547524f);
const float cephes_SQRTHF (0.707106781186547524f);
bool_t mask = (x < T(cephes_SQRTHF));
T tmp = bitcast_to_float (bitcast_to_int(x) & bitcast_to_int(mask));
x = x - one;
e = e - bitcast_to_float (bitcast_to_int(one) & bitcast_to_int(mask));
x = x + tmp;
T z = x * x;
const float cephes_log_p0 (7.0376836292E-2f);
const float cephes_log_p1 (- 1.1514610310E-1f);
const float cephes_log_p2 (1.1676998740E-1f);
const float cephes_log_p3 (- 1.2420140846E-1f);
const float cephes_log_p4 (+ 1.4249322787E-1f);
const float cephes_log_p5 (- 1.6668057665E-1f);
const float cephes_log_p6 (+ 2.0000714765E-1f);
const float cephes_log_p7 (- 2.4999993993E-1f);
const float cephes_log_p8 (+ 3.3333331174E-1f);
const float cephes_log_q1 (-2.12194440e-4f);
const float cephes_log_q2 (0.693359375f);
T y = cephes_log_p0;
y = madd (y, x, T(cephes_log_p1));
y = madd (y, x, T(cephes_log_p2));
y = madd (y, x, T(cephes_log_p3));
y = madd (y, x, T(cephes_log_p4));
y = madd (y, x, T(cephes_log_p5));
y = madd (y, x, T(cephes_log_p6));
y = madd (y, x, T(cephes_log_p7));
y = madd (y, x, T(cephes_log_p8));
y = y * x;
y = y * z;
y = madd(e, T(cephes_log_q1), y);
y = nmadd (z, 0.5f, y);
x = x + y;
x = madd (e, T(cephes_log_q2), x);
x = bitcast_to_float (bitcast_to_int(x) | bitcast_to_int(invalid_mask)); // negative arg will be NAN
return x;
#else
SIMD_RETURN (T, logf(v[i]));
#endif
}
OIIO_FORCEINLINE void transpose (float4 &a, float4 &b, float4 &c, float4 &d)
{
#if OIIO_SIMD_SSE
_MM_TRANSPOSE4_PS (a, b, c, d);
#else
float4 A (a[0], b[0], c[0], d[0]);
float4 B (a[1], b[1], c[1], d[1]);
float4 C (a[2], b[2], c[2], d[2]);
float4 D (a[3], b[3], c[3], d[3]);
a = A; b = B; c = C; d = D;
#endif
}
OIIO_FORCEINLINE void transpose (const float4& a, const float4& b, const float4& c, const float4& d,
float4 &r0, float4 &r1, float4 &r2, float4 &r3)
{
#if OIIO_SIMD_SSE
//_MM_TRANSPOSE4_PS (a, b, c, d);
float4 l02 = _mm_unpacklo_ps (a, c);
float4 h02 = _mm_unpackhi_ps (a, c);
float4 l13 = _mm_unpacklo_ps (b, d);
float4 h13 = _mm_unpackhi_ps (b, d);
r0 = _mm_unpacklo_ps (l02, l13);
r1 = _mm_unpackhi_ps (l02, l13);
r2 = _mm_unpacklo_ps (h02, h13);
r3 = _mm_unpackhi_ps (h02, h13);
#else
r0.load (a[0], b[0], c[0], d[0]);
r1.load (a[1], b[1], c[1], d[1]);
r2.load (a[2], b[2], c[2], d[2]);
r3.load (a[3], b[3], c[3], d[3]);
#endif
}
OIIO_FORCEINLINE void transpose (int4 &a, int4 &b, int4 &c, int4 &d)
{
#if OIIO_SIMD_SSE
__m128 A = _mm_castsi128_ps (a);
__m128 B = _mm_castsi128_ps (b);
__m128 C = _mm_castsi128_ps (c);
__m128 D = _mm_castsi128_ps (d);
_MM_TRANSPOSE4_PS (A, B, C, D);
a = _mm_castps_si128 (A);
b = _mm_castps_si128 (B);
c = _mm_castps_si128 (C);
d = _mm_castps_si128 (D);
#else
int4 A (a[0], b[0], c[0], d[0]);
int4 B (a[1], b[1], c[1], d[1]);
int4 C (a[2], b[2], c[2], d[2]);
int4 D (a[3], b[3], c[3], d[3]);
a = A; b = B; c = C; d = D;
#endif
}
OIIO_FORCEINLINE void transpose (const int4& a, const int4& b, const int4& c, const int4& d,
int4 &r0, int4 &r1, int4 &r2, int4 &r3)
{
#if OIIO_SIMD_SSE
//_MM_TRANSPOSE4_PS (a, b, c, d);
__m128 A = _mm_castsi128_ps (a);
__m128 B = _mm_castsi128_ps (b);
__m128 C = _mm_castsi128_ps (c);
__m128 D = _mm_castsi128_ps (d);
_MM_TRANSPOSE4_PS (A, B, C, D);
r0 = _mm_castps_si128 (A);
r1 = _mm_castps_si128 (B);
r2 = _mm_castps_si128 (C);
r3 = _mm_castps_si128 (D);
#else
r0.load (a[0], b[0], c[0], d[0]);
r1.load (a[1], b[1], c[1], d[1]);
r2.load (a[2], b[2], c[2], d[2]);
r3.load (a[3], b[3], c[3], d[3]);
#endif
}
OIIO_FORCEINLINE float4 AxBxCxDx (const float4& a, const float4& b,
const float4& c, const float4& d)
{
#if OIIO_SIMD_SSE
float4 l02 = _mm_unpacklo_ps (a, c);
float4 l13 = _mm_unpacklo_ps (b, d);
return _mm_unpacklo_ps (l02, l13);
#else
return float4 (a[0], b[0], c[0], d[0]);
#endif
}
OIIO_FORCEINLINE int4 AxBxCxDx (const int4& a, const int4& b,
const int4& c, const int4& d)
{
#if OIIO_SIMD_SSE
int4 l02 = _mm_unpacklo_epi32 (a, c);
int4 l13 = _mm_unpacklo_epi32 (b, d);
return _mm_unpacklo_epi32 (l02, l13);
#else
return int4 (a[0], b[0], c[0], d[0]);
#endif
}
//////////////////////////////////////////////////////////////////////
// float3 implementation
OIIO_FORCEINLINE float3::float3 (const float3 &other) {
#if defined(OIIO_SIMD_SSE) || defined(OIIO_SIMD_NEON)
m_simd = other.m_simd;
#else
SIMD_CONSTRUCT_PAD (other[i]);
#endif
}
OIIO_FORCEINLINE float3::float3 (const float4 &other) {
#if defined(OIIO_SIMD_SSE) || defined(OIIO_SIMD_NEON)
m_simd = other.simd();
#else
SIMD_CONSTRUCT_PAD (other[i]);
m_val[3] = 0.0f;
#endif
}
OIIO_FORCEINLINE const float3 float3::Zero () { return float3(float4::Zero()); }
OIIO_FORCEINLINE const float3 float3::One () { return float3(1.0f); }
OIIO_FORCEINLINE const float3 float3::Iota (float start, float step) {
return float3 (start+0.0f*step, start+1.0f*step, start+2.0f*step);
}
OIIO_FORCEINLINE void float3::load (float val) { float4::load (val, val, val, 0.0f); }
OIIO_FORCEINLINE void float3::load (const float *values) { float4::load (values, 3); }
OIIO_FORCEINLINE void float3::load (const float *values, int n) {
float4::load (values, n);
}
OIIO_FORCEINLINE void float3::load (const unsigned short *values) {
float4::load (float(values[0]), float(values[1]), float(values[2]));
}
OIIO_FORCEINLINE void float3::load (const short *values) {
float4::load (float(values[0]), float(values[1]), float(values[2]));
}
OIIO_FORCEINLINE void float3::load (const unsigned char *values) {
float4::load (float(values[0]), float(values[1]), float(values[2]));
}
OIIO_FORCEINLINE void float3::load (const char *values) {
float4::load (float(values[0]), float(values[1]), float(values[2]));
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float3::load (const half *values) {
float4::load (float(values[0]), float(values[1]), float(values[2]));
}
#endif /* _HALF_H_ */
OIIO_FORCEINLINE void float3::store (float *values) const {
float4::store (values, 3);
}
OIIO_FORCEINLINE void float3::store (float *values, int n) const {
float4::store (values, n);
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float3::store (half *values) const {
SIMD_DO (values[i] = m_val[i]);
}
#endif
OIIO_FORCEINLINE void float3::store (Imath::V3f &vec) const {
store ((float *)&vec);
}
OIIO_FORCEINLINE float3 operator+ (const float3& a, const float3& b) {
return float3 (float4(a) + float4(b));
}
OIIO_FORCEINLINE const float3 & float3::operator+= (const float3& a) {
*this = *this + a; return *this;
}
OIIO_FORCEINLINE float3 float3::operator- () const {
return float3 (-float4(*this));
}
OIIO_FORCEINLINE float3 operator- (const float3& a, const float3& b) {
return float3 (float4(a) - float4(b));
}
OIIO_FORCEINLINE const float3 & float3::operator-= (const float3& a) {
*this = *this - a; return *this;
}
OIIO_FORCEINLINE float3 operator* (const float3& a, const float3& b) {
return float3 (float4(a) * float4(b));
}
OIIO_FORCEINLINE const float3 & float3::operator*= (const float3& a) {
*this = *this * a; return *this;
}
OIIO_FORCEINLINE const float3 & float3::operator*= (float a) {
*this = *this * a; return *this;
}
OIIO_FORCEINLINE float3 operator/ (const float3& a, const float3& b) {
return float3 (float4(a) / b.xyz1()); // Avoid divide by zero!
}
OIIO_FORCEINLINE const float3 & float3::operator/= (const float3& a) {
*this = *this / a; return *this;
}
OIIO_FORCEINLINE const float3 & float3::operator/= (float a) {
*this = *this / a; return *this;
}
inline std::ostream& operator<< (std::ostream& cout, const float3& val) {
cout << val[0];
for (int i = 1; i < val.elements; ++i)
cout << ' ' << val[i];
return cout;
}
OIIO_FORCEINLINE float3 vreduce_add (const float3& v) {
#if OIIO_SIMD_SSE
return float3 ((vreduce_add(float4(v))).xyz0());
#else
return float3 (v[0] + v[1] + v[2]);
#endif
}
OIIO_FORCEINLINE float3 vdot (const float3 &a, const float3 &b) {
#if OIIO_SIMD_SSE >= 4
return float3(_mm_dp_ps (a.simd(), b.simd(), 0x77));
#else
return vreduce_add (a*b);
#endif
}
OIIO_FORCEINLINE float dot (const float3 &a, const float3 &b) {
#if OIIO_SIMD_SSE >= 4
return _mm_cvtss_f32 (_mm_dp_ps (a.simd(), b.simd(), 0x77));
#else
return reduce_add (a*b);
#endif
}
OIIO_FORCEINLINE float3 vdot3 (const float3 &a, const float3 &b) {
#if OIIO_SIMD_SSE >= 4
return float3(_mm_dp_ps (a.simd(), b.simd(), 0x77));
#else
return float3 (vreduce_add((a*b).xyz0()).xyz0());
#endif
}
OIIO_FORCEINLINE float3 float3::normalized () const
{
#if OIIO_SIMD
float3 len2 = vdot3 (*this, *this);
return float3 (safe_div (*this, sqrt(len2)));
#else
float len2 = dot (*this, *this);
return len2 > 0.0f ? (*this) / sqrtf(len2) : float3::Zero();
#endif
}
OIIO_FORCEINLINE float3 float3::normalized_fast () const
{
#if OIIO_SIMD
float3 len2 = vdot3 (*this, *this);
float4 invlen = blend0not (rsqrt_fast (len2), len2 == float4::Zero());
return float3 ((*this) * invlen);
#else
float len2 = dot (*this, *this);
return len2 > 0.0f ? (*this) / sqrtf(len2) : float3::Zero();
#endif
}
//////////////////////////////////////////////////////////////////////
// matrix44 implementation
OIIO_FORCEINLINE const Imath::M44f& matrix44::M44f() const {
return *(Imath::M44f*)this;
}
OIIO_FORCEINLINE float4 matrix44::operator[] (int i) const {
#if OIIO_SIMD_SSE
return m_row[i];
#else
return float4 (m_mat[i]);
#endif
}
OIIO_FORCEINLINE matrix44 matrix44::transposed () const {
matrix44 T;
#if OIIO_SIMD_SSE
simd::transpose (m_row[0], m_row[1], m_row[2], m_row[3],
T.m_row[0], T.m_row[1], T.m_row[2], T.m_row[3]);
#else
T = m_mat.transposed();
#endif
return T;
}
OIIO_FORCEINLINE float3 matrix44::transformp (const float3 &V) const {
#if OIIO_SIMD_SSE
float4 R = shuffle<0>(V) * m_row[0] + shuffle<1>(V) * m_row[1] +
shuffle<2>(V) * m_row[2] + m_row[3];
R = R / shuffle<3>(R);
return float3 (R.xyz0());
#else
Imath::V3f R;
m_mat.multVecMatrix (*(Imath::V3f *)&V, R);
return float3(R);
#endif
}
OIIO_FORCEINLINE float3 matrix44::transformv (const float3 &V) const {
#if OIIO_SIMD_SSE
float4 R = shuffle<0>(V) * m_row[0] + shuffle<1>(V) * m_row[1] +
shuffle<2>(V) * m_row[2];
return float3 (R.xyz0());
#else
Imath::V3f R;
m_mat.multDirMatrix (*(Imath::V3f *)&V, R);
return float3(R);
#endif
}
OIIO_FORCEINLINE float3 matrix44::transformvT (const float3 &V) const {
#if OIIO_SIMD_SSE
matrix44 T = transposed();
float4 R = shuffle<0>(V) * T[0] + shuffle<1>(V) * T[1] +
shuffle<2>(V) * T[2];
return float3 (R.xyz0());
#else
Imath::V3f R;
m_mat.transposed().multDirMatrix (*(Imath::V3f *)&V, R);
return float3(R);
#endif
}
OIIO_FORCEINLINE bool matrix44::operator== (const matrix44& m) const {
#if OIIO_SIMD_SSE
bool4 b0 = (m_row[0] == m[0]);
bool4 b1 = (m_row[1] == m[1]);
bool4 b2 = (m_row[2] == m[2]);
bool4 b3 = (m_row[3] == m[3]);
return simd::all (b0 & b1 & b2 & b3);
#else
return memcmp(this, &m, 16*sizeof(float)) == 0;
#endif
}
OIIO_FORCEINLINE bool matrix44::operator== (const Imath::M44f& m) const {
return memcmp(this, &m, 16*sizeof(float)) == 0;
}
OIIO_FORCEINLINE bool operator== (const Imath::M44f& a, const matrix44 &b) {
return (b == a);
}
OIIO_FORCEINLINE bool matrix44::operator!= (const matrix44& m) const {
#if OIIO_SIMD_SSE
bool4 b0 = (m_row[0] != m[0]);
bool4 b1 = (m_row[1] != m[1]);
bool4 b2 = (m_row[2] != m[2]);
bool4 b3 = (m_row[3] != m[3]);
return simd::any (b0 | b1 | b2 | b3);
#else
return memcmp(this, &m, 16*sizeof(float)) != 0;
#endif
}
OIIO_FORCEINLINE bool matrix44::operator!= (const Imath::M44f& m) const {
return memcmp(this, &m, 16*sizeof(float)) != 0;
}
OIIO_FORCEINLINE bool operator!= (const Imath::M44f& a, const matrix44 &b) {
return (b != a);
}
OIIO_FORCEINLINE matrix44 matrix44::inverse() const {
#if OIIO_SIMD_SSE
// Adapted from this code from Intel:
// ftp://download.intel.com/design/pentiumiii/sml/24504301.pdf
float4 minor0, minor1, minor2, minor3;
float4 row0, row1, row2, row3;
float4 det, tmp1;
const float *src = (const float *)this;
float4 zero = float4::Zero();
tmp1 = _mm_loadh_pi(_mm_loadl_pi(zero, (__m64*)(src)), (__m64*)(src+ 4));
row1 = _mm_loadh_pi(_mm_loadl_pi(zero, (__m64*)(src+8)), (__m64*)(src+12));
row0 = _mm_shuffle_ps(tmp1, row1, 0x88);
row1 = _mm_shuffle_ps(row1, tmp1, 0xDD);
tmp1 = _mm_loadh_pi(_mm_loadl_pi(tmp1, (__m64*)(src+ 2)), (__m64*)(src+ 6));
row3 = _mm_loadh_pi(_mm_loadl_pi(zero, (__m64*)(src+10)), (__m64*)(src+14));
row2 = _mm_shuffle_ps(tmp1, row3, 0x88);
row3 = _mm_shuffle_ps(row3, tmp1, 0xDD);
// -----------------------------------------------
tmp1 = row2 * row3;
tmp1 = shuffle<1,0,3,2>(tmp1);
minor0 = row1 * tmp1;
minor1 = row0 * tmp1;
tmp1 = shuffle<2,3,0,1>(tmp1);
minor0 = (row1 * tmp1) - minor0;
minor1 = (row0 * tmp1) - minor1;
minor1 = shuffle<2,3,0,1>(minor1);
// -----------------------------------------------
tmp1 = row1 * row2;
tmp1 = shuffle<1,0,3,2>(tmp1);
minor0 = (row3 * tmp1) + minor0;
minor3 = row0 * tmp1;
tmp1 = shuffle<2,3,0,1>(tmp1);
minor0 = minor0 - (row3 * tmp1);
minor3 = (row0 * tmp1) - minor3;
minor3 = shuffle<2,3,0,1>(minor3);
// -----------------------------------------------
tmp1 = shuffle<2,3,0,1>(row1) * row3;
tmp1 = shuffle<1,0,3,2>(tmp1);
row2 = shuffle<2,3,0,1>(row2);
minor0 = (row2 * tmp1) + minor0;
minor2 = row0 * tmp1;
tmp1 = shuffle<2,3,0,1>(tmp1);
minor0 = minor0 - (row2 * tmp1);
minor2 = (row0 * tmp1) - minor2;
minor2 = shuffle<2,3,0,1>(minor2);
// -----------------------------------------------
tmp1 = row0 * row1;
tmp1 = shuffle<1,0,3,2>(tmp1);
minor2 = (row3 * tmp1) + minor2;
minor3 = (row2 * tmp1) - minor3;
tmp1 = shuffle<2,3,0,1>(tmp1);
minor2 = (row3 * tmp1) - minor2;
minor3 = minor3 - (row2 * tmp1);
// -----------------------------------------------
tmp1 = row0 * row3;
tmp1 = shuffle<1,0,3,2>(tmp1);
minor1 = minor1 - (row2 * tmp1);
minor2 = (row1 * tmp1) + minor2;
tmp1 = shuffle<2,3,0,1>(tmp1);
minor1 = (row2 * tmp1) + minor1;
minor2 = minor2 - (row1 * tmp1);
// -----------------------------------------------
tmp1 = row0 * row2;
tmp1 = shuffle<1,0,3,2>(tmp1);
minor1 = (row3 * tmp1) + minor1;
minor3 = minor3 - (row1 * tmp1);
tmp1 = shuffle<2,3,0,1>(tmp1);
minor1 = minor1 - (row3 * tmp1);
minor3 = (row1 * tmp1) + minor3;
// -----------------------------------------------
det = row0 * minor0;
det = shuffle<2,3,0,1>(det) + det;
det = _mm_add_ss(shuffle<1,0,3,2>(det), det);
tmp1 = _mm_rcp_ss(det);
det = _mm_sub_ss(_mm_add_ss(tmp1, tmp1), _mm_mul_ss(det, _mm_mul_ss(tmp1, tmp1)));
det = shuffle<0>(det);
return matrix44 (det*minor0, det*minor1, det*minor2, det*minor3);
#else
return m_mat.inverse();
#endif
}
inline std::ostream& operator<< (std::ostream& cout, const matrix44 &M) {
const float *m = (const float *)&M;
cout << m[0];
for (int i = 1; i < 16; ++i)
cout << ' ' << m[i];
return cout;
}
OIIO_FORCEINLINE float3 transformp (const matrix44 &M, const float3 &V) {
return M.transformp (V);
}
OIIO_FORCEINLINE float3 transformp (const Imath::M44f &M, const float3 &V)
{
#if OIIO_SIMD
return matrix44(M).transformp (V);
#else
Imath::V3f R;
M.multVecMatrix (*(Imath::V3f *)&V, R);
return float3(R);
#endif
}
OIIO_FORCEINLINE float3 transformv (const matrix44 &M, const float3 &V) {
return M.transformv (V);
}
OIIO_FORCEINLINE float3 transformv (const Imath::M44f &M, const float3 &V)
{
#if OIIO_SIMD
return matrix44(M).transformv (V);
#else
Imath::V3f R;
M.multDirMatrix (*(Imath::V3f *)&V, R);
return float3(R);
#endif
}
OIIO_FORCEINLINE float3 transformvT (const matrix44 &M, const float3 &V)
{
return M.transformvT (V);
}
OIIO_FORCEINLINE float3 transformvT (const Imath::M44f &M, const float3 &V)
{
#if OIIO_SIMD
return matrix44(M).transformvT(V);
#else
return transformv (M.transposed(), V);
#endif
}
//////////////////////////////////////////////////////////////////////
// float8 implementation
OIIO_FORCEINLINE float& float8::operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
OIIO_FORCEINLINE float float8::operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
inline std::ostream& operator<< (std::ostream& cout, const float8& val) {
cout << val[0];
for (int i = 1; i < val.elements; ++i)
cout << ' ' << val[i];
return cout;
}
OIIO_FORCEINLINE float4 float8::lo () const {
#if OIIO_SIMD_AVX
return _mm256_castps256_ps128 (simd());
#else
return m_4[0];
#endif
}
OIIO_FORCEINLINE float4 float8::hi () const {
#if OIIO_SIMD_AVX
return _mm256_extractf128_ps (simd(), 1);
#else
return m_4[1];
#endif
}
OIIO_FORCEINLINE float8::float8 (const float4& lo, const float4 &hi) {
#if OIIO_SIMD_AVX
__m256 r = _mm256_castps128_ps256 (lo);
m_simd = _mm256_insertf128_ps (r, hi, 1);
// N.B. equivalent, if available: m_simd = _mm256_set_m128 (hi, lo);
#else
m_4[0] = lo;
m_4[1] = hi;
#endif
}
OIIO_FORCEINLINE float8::float8 (const int8& ival) {
#if OIIO_SIMD_AVX
m_simd = _mm256_cvtepi32_ps (ival);
#else
SIMD_CONSTRUCT (float(ival[i]));
#endif
}
OIIO_FORCEINLINE const float8 float8::Zero () {
#if OIIO_SIMD_AVX
return _mm256_setzero_ps();
#else
return float8(0.0f);
#endif
}
OIIO_FORCEINLINE const float8 float8::One () {
return float8(1.0f);
}
OIIO_FORCEINLINE const float8 float8::Iota (float start, float step) {
return float8 (start+0.0f*step, start+1.0f*step, start+2.0f*step, start+3.0f*step,
start+4.0f*step, start+5.0f*step, start+6.0f*step, start+7.0f*step);
}
/// Set all components to 0.0
OIIO_FORCEINLINE void float8::clear () {
#if OIIO_SIMD_AVX
m_simd = _mm256_setzero_ps();
#else
load (0.0f);
#endif
}
OIIO_FORCEINLINE void float8::load (float val) {
#if OIIO_SIMD_AVX
m_simd = _mm256_set1_ps (val);
#else
SIMD_CONSTRUCT (val);
#endif
}
OIIO_FORCEINLINE void float8::load (float a, float b, float c, float d,
float e, float f, float g, float h) {
#if OIIO_SIMD_AVX
m_simd = _mm256_set_ps (h, g, f, e, d, c, b, a);
#else
m_val[0] = a;
m_val[1] = b;
m_val[2] = c;
m_val[3] = d;
m_val[4] = e;
m_val[5] = f;
m_val[6] = g;
m_val[7] = h;
#endif
}
OIIO_FORCEINLINE void float8::load (const float *values) {
#if OIIO_SIMD_AVX
m_simd = _mm256_loadu_ps (values);
#elif OIIO_SIMD_SSE
m_4[0] = float4(values);
m_4[1] = float4(values+4);
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float8::load (const float *values, int n) {
// FIXME: is this faster with AVX masked loads?
#if OIIO_SIMD_SSE
if (n > 0 && n <= 4) {
float4 l; l.load (values, n);
m_simd = float8(l, float4::Zero());
} else if (n > 4 && n <= 8) {
float4 h; h.load (values+4, n-4);
m_simd = float8(float4(values), h);
}
else
clear();
#else
for (int i = 0; i < n; ++i)
m_val[i] = values[i];
for (int i = n; i < paddedelements; ++i)
m_val[i] = 0;
#endif
}
OIIO_FORCEINLINE void float8::load (const unsigned short *values) {
#if OIIO_SIMD_AVX
// Rely on the uint16->int conversion, then convert to float
m_simd = _mm256_cvtepi32_ps (int8(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float8::load (const short *values) {
#if OIIO_SIMD_AVX
// Rely on the int16->int conversion, then convert to float
m_simd = _mm256_cvtepi32_ps (int8(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float8::load (const unsigned char *values) {
#if OIIO_SIMD_AVX
m_simd = _mm256_cvtepi32_ps (int8(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
OIIO_FORCEINLINE void float8::load (const char *values) {
#if OIIO_SIMD_AVX
m_simd = _mm256_cvtepi32_ps (int8(values).simd());
#else
SIMD_CONSTRUCT (values[i]);
#endif
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float8::load (const half *values) {
#if OIIO_SIMD_AVX && defined(__F16C__)
/* Enabled 16 bit float instructions! */
int4 a ((const int *)values);
m_simd = _mm256_cvtph_ps (a);
#elif OIIO_SIMD_SSE >= 2
m_4[0] = float4(values);
m_4[1] = float4(values+4);
#else /* No SIMD defined: */
SIMD_CONSTRUCT (values[i]);
#endif
}
#endif /* _HALF_H_ */
OIIO_FORCEINLINE void float8::store (float *values) const {
#if OIIO_SIMD_AVX
// Use an unaligned store -- it's just as fast when the memory turns
// out to be aligned, nearly as fast even when unaligned. Not worth
// the headache of using stores that require alignment.
_mm256_storeu_ps (values, m_simd);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
OIIO_FORCEINLINE void float8::store (float *values, int n) const {
DASSERT (n >= 0 && n <= elements);
// FIXME: is this faster with AVX masked stores?
#if OIIO_SIMD_SSE
if (n <= 4) {
lo().store (values, n);
} else if (n <= 8) {
lo().store (values);
hi().store (values+4, n-4);
}
#else
for (int i = 0; i < n; ++i)
values[i] = m_val[i];
#endif
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void float8::store (half *values) const {
#if OIIO_SIMD_AVX && defined(__F16C__)
__m128i h = _mm256_cvtps_ph (m_simd, (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC));
_mm_storeu_si128 ((__m128i *)values, h);
#else
SIMD_DO (values[i] = m_val[i]);
#endif
}
#endif
OIIO_FORCEINLINE float8 operator+ (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_add_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float8, a[i] + b[i]);
#endif
}
OIIO_FORCEINLINE const float8 & float8::operator+= (const float8& a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_add_ps (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] += a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE float8 float8::operator- () const {
#if OIIO_SIMD_AVX
return _mm256_sub_ps (_mm256_setzero_ps(), m_simd);
#else
SIMD_RETURN (float8, -m_val[i]);
#endif
}
OIIO_FORCEINLINE float8 operator- (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_sub_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float8, a[i] - b[i]);
#endif
}
OIIO_FORCEINLINE const float8 & float8::operator-= (const float8& a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_sub_ps (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] -= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE float8 operator* (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_mul_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float8, a[i] * b[i]);
#endif
}
OIIO_FORCEINLINE const float8 & float8::operator*= (const float8& a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_mul_ps (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] *= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE const float8 & float8::operator*= (float val) {
#if OIIO_SIMD_AVX
m_simd = _mm256_mul_ps (m_simd, _mm256_set1_ps(val));
#else
SIMD_DO (m_val[i] *= val);
#endif
return *this;
}
OIIO_FORCEINLINE float8 operator/ (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_div_ps (a.m_simd, b.m_simd);
#else
SIMD_RETURN (float8, a[i] / b[i]);
#endif
}
OIIO_FORCEINLINE const float8 & float8::operator/= (const float8& a) {
#if OIIO_SIMD_AVX
m_simd = _mm256_div_ps (m_simd, a.m_simd);
#else
SIMD_DO (m_val[i] /= a[i]);
#endif
return *this;
}
OIIO_FORCEINLINE const float8 & float8::operator/= (float val) {
#if OIIO_SIMD_AVX
m_simd = _mm256_div_ps (m_simd, _mm256_set1_ps(val));
#else
SIMD_DO (m_val[i] /= val);
#endif
return *this;
}
OIIO_FORCEINLINE bool8 operator== (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_EQ_OQ);
#else
SIMD_RETURN (bool8, a[i] == b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator!= (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_NEQ_OQ);
#else
SIMD_RETURN (bool8, a[i] != b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator< (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_LT_OQ);
#else
SIMD_RETURN (bool8, a[i] < b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator> (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_GT_OQ);
#else
SIMD_RETURN (bool8, a[i] > b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator>= (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_GE_OQ);
#else
SIMD_RETURN (bool8, a[i] >= b[i] ? -1 : 0);
#endif
}
OIIO_FORCEINLINE bool8 operator<= (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_cmp_ps (a.m_simd, b.m_simd, _CMP_LE_OQ);
#else
SIMD_RETURN (bool8, a[i] <= b[i] ? -1 : 0);
#endif
}
// Implementation had to be after the definition of float8.
OIIO_FORCEINLINE int8::int8 (const float8& f)
{
#if OIIO_SIMD_AVX
m_simd = _mm256_cvttps_epi32(f);
#else
SIMD_CONSTRUCT ((int) f[i]);
#endif
}
template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
OIIO_FORCEINLINE float8 shuffle (const float8& a) {
#if OIIO_SIMD_AVX >= 2
int8 index (i0, i1, i2, i3, i4, i5, i6, i7);
return _mm256_permutevar8x32_ps (a, index);
#else
return float8 (a[i0], a[i1], a[i2], a[i3], a[i4], a[i5], a[i6], a[i7]);
#endif
}
template<int i> OIIO_FORCEINLINE float8 shuffle (const float8& a) {
#if OIIO_SIMD_AVX >= 2
return _mm256_permutevar8x32_ps (a, int8(i));
#else
return shuffle<i,i,i,i,i,i,i,i>(a);
#endif
}
template<int i>
OIIO_FORCEINLINE float extract (const float8& v) {
#if OIIO_SIMD_AVX_NO_FIXME
// Looks like the fastest we can do it is to extract a float4,
// shuffle its one element everywhere, then extract element 0.
_m128 f4 = _mm256_extractf128_ps (i >> 2);
int j = i & 3;
return _mm_cvtss_f32(shuffle_sse<j,j,j,j>(a.simd()));
#else
return v[i];
#endif
}
template<int i>
OIIO_FORCEINLINE float8 insert (const float8& a, float val) {
#if OIIO_SIMD_AVX_NO_FIXME
return _mm256_insert_epi32 (a, val, i);
#else
float8 tmp = a;
tmp[i] = val;
return tmp;
#endif
}
OIIO_FORCEINLINE float float8::x () const { return extract<0>(*this); }
OIIO_FORCEINLINE float float8::y () const { return extract<1>(*this); }
OIIO_FORCEINLINE float float8::z () const { return extract<2>(*this); }
OIIO_FORCEINLINE float float8::w () const { return extract<3>(*this); }
OIIO_FORCEINLINE void float8::set_x (float val) { *this = insert<0>(*this, val); }
OIIO_FORCEINLINE void float8::set_y (float val) { *this = insert<1>(*this, val); }
OIIO_FORCEINLINE void float8::set_z (float val) { *this = insert<2>(*this, val); }
OIIO_FORCEINLINE void float8::set_w (float val) { *this = insert<3>(*this, val); }
OIIO_FORCEINLINE int8 bitcast_to_int (const float8& x)
{
#if OIIO_SIMD_AVX
return _mm256_castps_si256 (x.simd());
#else
return *(int8 *)&x;
#endif
}
OIIO_FORCEINLINE float8 bitcast_to_float (const int8& x)
{
#if OIIO_SIMD_AVX
return _mm256_castsi256_ps (x.simd());
#else
return *(float8 *)&x;
#endif
}
OIIO_FORCEINLINE float8 vreduce_add (const float8& v) {
#if OIIO_SIMD_AVX
// From Syrah:
float8 ab_cd_0_0_ef_gh_0_0 = _mm256_hadd_ps(v.simd(), _mm256_setzero_ps());
float8 abcd_0_0_0_efgh_0_0_0 = _mm256_hadd_ps(ab_cd_0_0_ef_gh_0_0, _mm256_setzero_ps());
// get efgh in the 0-idx slot
float8 efgh = shuffle<4>(abcd_0_0_0_efgh_0_0_0);
float8 final_sum = abcd_0_0_0_efgh_0_0_0 + efgh;
return shuffle<0>(final_sum);
#else
float4 hadd4 = vreduce_add(v.lo()) + vreduce_add(v.hi());
return float8(hadd4, hadd4);
#endif
}
OIIO_FORCEINLINE float reduce_add (const float8& v) {
#if OIIO_SIMD_AVX >= 2
return extract<0>(vreduce_add(v));
#else
return reduce_add(v.lo()) + reduce_add(v.hi());
#endif
}
OIIO_FORCEINLINE float8 blend (const float8& a, const float8& b, const bool8& mask)
{
#if OIIO_SIMD_AVX
return _mm256_blendv_ps (a, b, mask);
#elif defined(OIIO_SIMD_SSE)
return float8 (blend (a.lo(), b.lo(), mask.lo()),
blend (a.hi(), b.hi(), mask.hi()));
#else
SIMD_RETURN (float8, mask[i] ? b[i] : a[i]);
#endif
}
OIIO_FORCEINLINE float8 blend0 (const float8& a, const bool8& mask)
{
#if OIIO_SIMD_AVX
return _mm256_and_ps(mask, a);
#elif defined(OIIO_SIMD_SSE)
return float8 (blend0 (a.lo(), mask.lo()),
blend0 (a.hi(), mask.hi()));
#else
SIMD_RETURN (float8, mask[i] ? a[i] : 0.0f);
#endif
}
OIIO_FORCEINLINE float8 blend0not (const float8& a, const bool8& mask)
{
#if OIIO_SIMD_AVX
return _mm256_andnot_ps(mask, a);
#elif defined(OIIO_SIMD_SSE)
return float8 (blend0not (a.lo(), mask.lo()),
blend0not (a.hi(), mask.hi()));
#else
SIMD_RETURN (float8, mask[i] ? 0.0f : a[i]);
#endif
}
OIIO_FORCEINLINE float8 safe_div (const float8 &a, const float8 &b) {
#if OIIO_SIMD_SSE
return blend0not (a/b, b == float8::Zero());
#else
SIMD_RETURN (float8, b[i] == 0.0f ? 0.0f : a[i] / b[i]);
#endif
}
OIIO_FORCEINLINE float8 select (const bool8& mask, const float8& a, const float8& b)
{
return blend (b, a, mask);
}
OIIO_FORCEINLINE float8 abs (const float8& a)
{
#if OIIO_SIMD_AVX
// Just clear the sign bit for cheap fabsf
return _mm256_and_ps (a.simd(), _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff)));
#else
SIMD_RETURN (float8, fabsf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 sign (const float8& a)
{
float8 one(1.0f);
return blend (one, -one, a < float8::Zero());
}
OIIO_FORCEINLINE float8 ceil (const float8& a)
{
#if OIIO_SIMD_AVX
return _mm256_ceil_ps (a);
#else
SIMD_RETURN (float8, ceilf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 floor (const float8& a)
{
#if OIIO_SIMD_AVX
return _mm256_floor_ps (a);
#else
SIMD_RETURN (float8, floorf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 round (const float8& a)
{
#if OIIO_SIMD_AVX
return _mm256_round_ps (a, (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC));
#else
SIMD_RETURN (float8, roundf(a[i]));
#endif
}
OIIO_FORCEINLINE int8 floori (const float8& a)
{
#if OIIO_SIMD_AVX
return int8(floor(a));
#elif defined(OIIO_SIMD_SSE) /* SSE2/3 */
int8 i (a); // truncates
int8 isneg = bitcast_to_int (a < float8::Zero());
return i + isneg;
// The trick here (thanks, Cycles, for letting me spy on your code) is
// that the comparison will return (int)-1 for components that are less
// than zero, and adding that is the same as subtracting one!
#else
SIMD_RETURN (int8, (int)floorf(a[i]));
#endif
}
OIIO_FORCEINLINE int8 rint (const float8& a)
{
return int8 (round(a));
}
OIIO_FORCEINLINE float8 sqrt (const float8 &a)
{
#if OIIO_SIMD_AVX
return _mm256_sqrt_ps (a.simd());
#else
SIMD_RETURN (float8, sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 rsqrt (const float8 &a)
{
#if OIIO_SIMD_AVX
return _mm256_div_ps (_mm256_set1_ps(1.0f), _mm256_sqrt_ps (a.simd()));
#else
SIMD_RETURN (float8, 1.0f/sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 rsqrt_fast (const float8 &a)
{
#if OIIO_SIMD_AVX
return _mm256_rsqrt_ps (a.simd());
#else
SIMD_RETURN (float8, 1.0f/sqrtf(a[i]));
#endif
}
OIIO_FORCEINLINE float8 min (const float8& a, const float8& b)
{
#if OIIO_SIMD_AVX
return _mm256_min_ps (a, b);
#else
SIMD_RETURN (float8, std::min (a[i], b[i]));
#endif
}
OIIO_FORCEINLINE float8 max (const float8& a, const float8& b)
{
#if OIIO_SIMD_AVX
return _mm256_max_ps (a, b);
#else
SIMD_RETURN (float8, std::max (a[i], b[i]));
#endif
}
OIIO_FORCEINLINE float8 andnot (const float8& a, const float8& b) {
#if OIIO_SIMD_AVX
return _mm256_andnot_ps (a.simd(), b.simd());
#else
const int *ai = (const int *)&a;
const int *bi = (const int *)&b;
return bitcast_to_float (int8(~(ai[0]) & bi[0],
~(ai[1]) & bi[1],
~(ai[2]) & bi[2],
~(ai[3]) & bi[3],
~(ai[4]) & bi[4],
~(ai[5]) & bi[5],
~(ai[6]) & bi[6],
~(ai[7]) & bi[7]));
#endif
}
OIIO_FORCEINLINE float8 madd (const simd::float8& a, const simd::float8& b,
const simd::float8& c)
{
#if OIIO_SIMD_AVX && OIIO_FMA_ENABLED
// If we are sure _mm256_fmadd_ps intrinsic is available, use it.
return _mm256_fmadd_ps (a, b, c);
#else
// Fallback: just use regular math and hope for the best.
return a * b + c;
#endif
}
OIIO_FORCEINLINE float8 msub (const simd::float8& a, const simd::float8& b,
const simd::float8& c)
{
#if OIIO_SIMD_AVX && OIIO_FMA_ENABLED
// If we are sure _mm256_fnmsub_ps intrinsic is available, use it.
return _mm256_fmsub_ps (a, b, c);
#else
// Fallback: just use regular math and hope for the best.
return a * b - c;
#endif
}
OIIO_FORCEINLINE float8 nmadd (const simd::float8& a, const simd::float8& b,
const simd::float8& c)
{
#if OIIO_SIMD_AVX && OIIO_FMA_ENABLED
// If we are sure _mm256_fnmadd_ps intrinsic is available, use it.
return _mm256_fnmadd_ps (a, b, c);
#else
// Fallback: just use regular math and hope for the best.
return c - a * b;
#endif
}
OIIO_FORCEINLINE float8 nmsub (const simd::float8& a, const simd::float8& b,
const simd::float8& c)
{
#if OIIO_SIMD_AVX && OIIO_FMA_ENABLED
// If we are sure _mm256_fnmsub_ps intrinsic is available, use it.
return _mm256_fnmsub_ps (a, b, c);
#else
// Fallback: just use regular math and hope for the best.
return -(a * b) - c;
#endif
}
} // end namespace simd
OIIO_NAMESPACE_END
#undef SIMD_DO
#undef SIMD_CONSTRUCT
#undef SIMD_CONSTRUCT_PAD
#undef SIMD_RETURN
#undef SIMD_RETURN_REDUCE
#endif /* OIIO_SIMD_H */
|