/usr/include/OpenImageIO/simd.h is in libopenimageio-dev 1.6.17~dfsg0-1+b2.
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 | /*
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
#pragma once
#include <OpenImageIO/dassert.h>
#include <OpenImageIO/missing_math.h>
#include <OpenImageIO/platform.h>
#include <OpenEXR/ImathVec.h>
#if defined(_MSC_VER)
#include <algorithm>
#endif
#if (defined(__SSE2__) || (_MSC_VER >= 1300 && !_M_CEE_PURE)) && !defined(OIIO_NO_SSE)
#if OIIO_GNUC_VERSION >= 40400 || __clang__
# include <immintrin.h>
#else /* this clause can go away when GCC 4.8 is the minimum: */
# include <xmmintrin.h>
# include <emmintrin.h>
# if defined(__SSE3__)
# include <pmmintrin.h>
# endif
# if defined(__SSSE3__)
# include <tmmintrin.h>
# endif
# if (defined(__SSE4_1__) || defined(__SSE4_2__))
# include <smmintrin.h>
# endif
#endif
# 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 1
# define OIIO_SIMD_MASK_BYTE_WIDTH 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
// FIXME Future: support AVX
#if defined(__AVX__) && !defined(OIIO_NO_AVX)
// N.B. Any machine with AVX will also have SSE
# define OIIO_SIMD_AVX 1
// # define OIIO_SIMD_MASK_BYTE_WIDTH 4
// # undef OIIO_SIMD_MAX_SIZE_BYTES
// # define OIIO_SIMD_MAX_SIZE_BYTES 32
// # undef OIIO_SIMD_ALIGN
// # define OIIO_SIMD_ALIGN OIIO_ALIGN(32)
// # define OIIO_AVX_ALIGN OIIO_ALIGN(32)
#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_MASK_BYTE_WIDTH 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
#ifndef OIIO_SIMD
// No SIMD available
# define OIIO_SIMD 0
# define OIIO_SIMD_ALIGN
# define OIIO_SIMD4_ALIGN
# define OIIO_SIMD_MAX_SIZE_BYTES 16
#endif
#include <algorithm>
OIIO_NAMESPACE_BEGIN
namespace simd {
class int4;
class float4;
class mask4;
# 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) }
//
// Additional private intrinsic wrappers
//
#if defined(OIIO_SIMD_SSE)
// Shamelessly lifted from Syrah which lifted from Manta which lifted it
// from intel.com
OIIO_FORCEINLINE __m128i mm_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
}
// 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));
}
#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
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)));
}
#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
#endif
/// mask4: A mask 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 mask4 {
public:
static const char* type_name() { return "mask4"; }
typedef bool value_t; ///< Underlying equivalent scalar value type
enum { elements = 4 }; ///< Number of scalar elements
enum { bits = 128 }; ///< Total number of bits
/// simd_t is the native SIMD type used
#if defined(OIIO_SIMD_SSE)
typedef __m128 simd_t;
#else
typedef int simd_t[elements];
#endif
/// Default constructor (contents undefined)
OIIO_FORCEINLINE mask4 () { }
/// Destructor
OIIO_FORCEINLINE ~mask4 () { }
/// Construct from a single value (store it in all slots)
OIIO_FORCEINLINE mask4 (bool a) { load(a); }
/// Construct from 4 values
OIIO_FORCEINLINE mask4 (bool a, bool b, bool c, bool d) { load(a,b,c,d); }
/// Copy construct from another mask4
OIIO_FORCEINLINE mask4 (const mask4 &other) {
#if defined(OIIO_SIMD_SSE)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
}
/// Construct from an int4 (is each element nonzero?)
OIIO_FORCEINLINE mask4 (const int4 &i);
#if OIIO_SIMD
/// Construct from the underlying SIMD type
OIIO_FORCEINLINE mask4 (const simd_t& m) : m_vec(m) { }
/// Return the raw SIMD type
OIIO_FORCEINLINE operator simd_t () const { return m_vec; }
OIIO_FORCEINLINE simd_t simd () const { return m_vec; }
#endif
/// Set all components to false
OIIO_FORCEINLINE void clear () {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_setzero_ps();
#else
*this = false;
#endif
}
/// Return a mask4 the is 'false' for all values
static OIIO_FORCEINLINE const mask4 False () {
#if defined(OIIO_SIMD_SSE)
return _mm_setzero_ps();
#else
return mask4(false);
#endif
}
/// Return a mask4 the is 'true' for all values
static OIIO_FORCEINLINE const mask4 True () {
#if defined(OIIO_SIMD_SSE)
// Fastest way to fill an __m128 with all 1 bits is to cmpeq_epi8
// any value to itself.
# if defined(OIIO_SIMD_AVX) && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128();
# else
__m128i anyval = _mm_castps_si128 (_mm_setzero_ps());
# endif
return _mm_castsi128_ps (_mm_cmpeq_epi8 (anyval, anyval));
#else
return mask4(true);
#endif
}
/// Assign one value to all components
OIIO_FORCEINLINE const mask4 & operator= (bool a) { load(a); return *this; }
/// Assignment of another mask4
OIIO_FORCEINLINE const mask4 & operator= (const mask4 & other) {
#if defined(OIIO_SIMD_SSE)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
return *this;
}
/// Component access (get)
OIIO_FORCEINLINE bool operator[] (int i) const {
DASSERT(i >= 0 && i < 4);
#if defined(OIIO_SIMD_SSE)
return (_mm_movemask_ps(m_vec) >> i) & 1;
#else
return bool(m_val[i]);
#endif
}
/// Component access (set).
/// NOTE: use with caution. The implementation sets the integer
/// value, which may not have the same bit pattern as the bool returned
/// by operator[]const.
OIIO_FORCEINLINE int& operator[] (int i) {
DASSERT(i >= 0 && i < 4);
return m_val[i];
}
/// Helper: load a single value into all components.
OIIO_FORCEINLINE void load (bool a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_castsi128_ps(_mm_set1_epi32(a ? -1 : 0));
#else
int val = a ? -1 : 0;
for (int i = 0; i < elements; ++i)
m_val[i] = val;
#endif
}
/// Helper: load separate values into each component.
OIIO_FORCEINLINE void load (bool a, bool b, bool c, bool d) {
#if defined(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_vec = _mm_castsi128_ps(_mm_set_epi32(d ? -1 : 0,
c ? -1 : 0,
b ? -1 : 0,
a ? -1 : 0));
#else
m_val[0] = a ? -1 : 0;
m_val[1] = b ? -1 : 0;
m_val[2] = c ? -1 : 0;
m_val[3] = d ? -1 : 0;
#endif
}
/// Helper: store the values into memory as bools.
OIIO_FORCEINLINE void store (bool *values) const {
#if 0 && defined(OIIO_SIMD_SSE)
// FIXME: is there a more efficient way to do this?
#else
for (int i = 0; i < elements; ++i)
values[i] = m_val[i] ? true : false;
#endif
}
/// Store the first n values into memory.
OIIO_FORCEINLINE void 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;
}
/// Logical "not", component-by-component
friend OIIO_FORCEINLINE mask4 operator! (const mask4 & a) {
#if defined(OIIO_SIMD_SSE)
return _mm_xor_ps (a.m_vec, True());
#else
return mask4 (a.m_val[0] ^ (-1), a.m_val[1] ^ (-1),
a.m_val[2] ^ (-1), a.m_val[3] ^ (-1));
#endif
}
/// Logical "and", component-by-component
friend OIIO_FORCEINLINE mask4 operator& (const mask4 & a, const mask4 & b) {
#if defined(OIIO_SIMD_SSE)
return _mm_and_ps (a.m_vec, b.m_vec);
#else
return mask4 (a.m_val[0] & b.m_val[0],
a.m_val[1] & b.m_val[1],
a.m_val[2] & b.m_val[2],
a.m_val[3] & b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const mask4& operator&= (const mask4 & b) {
return *this = *this & b;
}
/// Logical "or" component-by-component
friend OIIO_FORCEINLINE mask4 operator| (const mask4 & a, const mask4 & b) {
#if defined(OIIO_SIMD_SSE)
return _mm_or_ps (a.m_vec, b.m_vec);
#else
return mask4 (a.m_val[0] | b.m_val[0],
a.m_val[1] | b.m_val[1],
a.m_val[2] | b.m_val[2],
a.m_val[3] | b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const mask4& operator|= (const mask4 & a) {
return *this = *this | a;
}
friend OIIO_FORCEINLINE mask4 operator^ (const mask4& a, const mask4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_xor_ps (a.m_vec, b.m_vec);
#else
return mask4 (a.m_val[0] ^ b.m_val[0],
a.m_val[1] ^ b.m_val[1],
a.m_val[2] ^ b.m_val[2],
a.m_val[3] ^ b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const mask4 & operator^= (const mask4& a) {
return *this = *this ^ a;
}
OIIO_FORCEINLINE mask4 operator~ () {
#if defined(OIIO_SIMD_SSE)
// Fastest way to bit-complement in SSE is to xor with 0xffffffff.
// Fastest way to fill an __m128 with all 1 bits is to cmpeq_epi8
// any value to itself.
# if defined(OIIO_SIMD_AVX) && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128(); // AVX only, sigh
# else
__m128i anyval = _mm_castps_si128 (m_vec);
# endif
__m128 all_one_bits = _mm_castsi128_ps (_mm_cmpeq_epi8 (anyval, anyval));
return _mm_xor_ps (m_vec, all_one_bits);
#else
return mask4 (~(m_val[0]),
~(m_val[1]),
~(m_val[2]),
~(m_val[3]));
#endif
}
/// Equality comparison, component by component
friend OIIO_FORCEINLINE const mask4 operator== (const mask4 & a, const mask4 & b) {
#if defined(OIIO_SIMD_SSE)
return _mm_castsi128_ps (_mm_cmpeq_epi32 (_mm_castps_si128 (a.m_vec), _mm_castps_si128(b.m_vec)));
#else
return mask4 (a[0] == b[0],
a[1] == b[1],
a[2] == b[2],
a[3] == b[3]);
#endif
}
/// Inequality comparison, component by component
friend OIIO_FORCEINLINE const mask4 operator!= (const mask4 & a, const mask4 & b) {
#if defined(OIIO_SIMD_SSE)
return _mm_xor_ps (a.m_vec, b.m_vec);
#else
return mask4 (a[0] != b[0],
a[1] != b[1],
a[2] != b[2],
a[3] != b[3]);
#endif
}
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const mask4 & a) {
return cout << a[0] << ' ' << a[1] << ' ' << a[2] << ' ' << a[3];
}
private:
// The actual data representation
union {
#if OIIO_SIMD
simd_t m_vec;
#endif
int m_val[elements];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(mask4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE mask4 shuffle (const mask4& a) {
#if defined(OIIO_SIMD_SSE)
return shuffle_sse<i0,i1,i2,i3> (a.simd());
#else
return mask4(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 mask4 shuffle (const mask4& 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 mask4& a) {
#if defined(OIIO_SIMD_SSE) && 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 mask4 insert (const mask4& a, bool val) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4
int ival = val ? -1 : 0;
return _mm_castsi128_ps (_mm_insert_epi32 (_mm_castps_si128(a), ival, i));
#else
mask4 tmp = a;
tmp[i] = val ? -1 : 0;
return tmp;
#endif
}
/// Logical "and" reduction, i.e., 'and' all components together, resulting
/// in a single bool.
OIIO_FORCEINLINE bool reduce_and (const mask4& v) {
#if defined(OIIO_SIMD_SSE)
return _mm_movemask_ps(v.simd()) == 0xf;
#else
return v[0] & v[1] & v[2] & v[3];
#endif
}
/// Logical "or" reduction, i.e., 'or' all components together, resulting
/// in a single bool.
OIIO_FORCEINLINE bool reduce_or (const mask4& v) {
#if defined(OIIO_SIMD_SSE)
return _mm_movemask_ps(v) != 0;
#else
return v[0] | v[1] | v[2] | v[3];
#endif
}
/// Are all components true?
OIIO_FORCEINLINE bool all (const mask4& v) { return reduce_and(v) == true; }
/// Are any components true?
OIIO_FORCEINLINE bool any (const mask4& v) { return reduce_or(v) == true; }
/// Are all components false:
OIIO_FORCEINLINE bool none (const mask4& v) { return reduce_or(v) == false; }
/// 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 { bits = 128 }; ///< Total number of bits
/// simd_t is the native SIMD type used
#if defined(OIIO_SIMD_SSE)
typedef __m128i simd_t;
#else
typedef int4 simd_t[elements];
#endif
/// Default constructor (contents undefined)
OIIO_FORCEINLINE int4 () { }
/// Destructor
OIIO_FORCEINLINE ~int4 () { }
/// Construct from a single value (store it in all slots)
OIIO_FORCEINLINE int4 (int a) { load(a); }
/// Construct from 2 values -- (a,a,b,b)
OIIO_FORCEINLINE int4 (int a, int b) { load(a,a,b,b); }
/// Construct from 4 values
OIIO_FORCEINLINE int4 (int a, int b, int c, int d) { load(a,b,c,d); }
/// Construct from a pointer to 4 values
OIIO_FORCEINLINE int4 (const int *vals) { load (vals); }
/// Construct from a pointer to 4 unsigned short values
OIIO_FORCEINLINE explicit int4 (const unsigned short *vals) { load(vals); }
/// Construct from a pointer to 4 signed short values
OIIO_FORCEINLINE explicit int4 (const short *vals) { load(vals); }
/// Construct from a pointer to 4 unsigned char values (0 - 255)
OIIO_FORCEINLINE explicit int4 (const unsigned char *vals) { load(vals); }
/// Construct from a pointer to 4 signed char values (-128 - 127)
OIIO_FORCEINLINE explicit int4 (const char *vals) { load(vals); }
/// Copy construct from another int4
OIIO_FORCEINLINE int4 (const int4 & other) {
#if defined(OIIO_SIMD_SSE)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
}
/// Convert a float4 to an int4. Equivalent to i = (int)f;
OIIO_FORCEINLINE explicit int4 (const float4& f); // implementation below
#if OIIO_SIMD
/// Construct from the underlying SIMD type
OIIO_FORCEINLINE int4 (const simd_t& m) : m_vec(m) { }
/// Return the raw SIMD type
OIIO_FORCEINLINE operator simd_t () const { return m_vec; }
OIIO_FORCEINLINE simd_t simd () const { return m_vec; }
#endif
/// Sset all components to 0
OIIO_FORCEINLINE void clear () {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_setzero_si128();
#else
*this = 0.0f;
#endif
}
/// Return an int4 with all components set to 0
static OIIO_FORCEINLINE const int4 Zero () {
#if defined(OIIO_SIMD_SSE)
return _mm_setzero_si128();
#else
return int4(0);
#endif
}
/// Return an int4 with all components set to 1
static OIIO_FORCEINLINE const int4 One () { return int4(1); }
/// Return an int4 with all components set to -1 (aka 0xffffffff)
static OIIO_FORCEINLINE const int4 NegOne () {
#if defined(OIIO_SIMD_SSE)
// Fastest way to fill an __m128 with all 1 bits is to cmpeq_epi8
// any value to itself.
# if defined(OIIO_SIMD_AVX) && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128();
# else
__m128i anyval = _mm_castps_si128 (_mm_setzero_ps());
# endif
return _mm_cmpeq_epi8 (anyval, anyval);
#else
return int4(-1);
#endif
}
/// Return an int4 with incremented components (e.g., 0,1,2,3).
/// Optional argument can give a non-zero starting point.
static OIIO_FORCEINLINE const int4 Iota (int value=0) {
return int4(value,value+1,value+2,value+3);
}
/// Assign one value to all components.
OIIO_FORCEINLINE const int4 & operator= (int a) { load(a); return *this; }
/// Assignment from another int4
OIIO_FORCEINLINE const int4 & operator= (int4 other) {
#if defined(OIIO_SIMD_SSE)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
return *this;
}
/// Component access (set)
OIIO_FORCEINLINE int& operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
/// Component access (get)
OIIO_FORCEINLINE int operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
/// Helper: load a single int into all components
OIIO_FORCEINLINE void load (int a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_set1_epi32 (a);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = a;
#endif
}
/// Helper: load separate values into each component.
OIIO_FORCEINLINE void load (int a, int b, int c, int d) {
#if defined(OIIO_SIMD_SSE)
m_vec = _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
}
/// Load from an array of 4 values
OIIO_FORCEINLINE void load (const int *values) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_loadu_si128 ((const simd_t *)values);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
OIIO_FORCEINLINE void load (const int *values, int n) {
#if defined(OIIO_SIMD_SSE)
switch (n) {
case 1:
m_vec = _mm_castps_si128 (_mm_load_ss ((const float *)values));
break;
case 2:
// Trickery: load one double worth of bits!
m_vec = _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_vec = _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_vec = _mm_loadu_si128 ((const simd_t *)values);
break;
default:
break;
}
#endif
for (int i = 0; i < n; ++i)
m_val[i] = values[i];
for (int i = n; i < elements; ++i)
m_val[i] = 0;
}
/// Load from an array of 4 unsigned short values, convert to int4
OIIO_FORCEINLINE void load (const unsigned short *values) {
#if defined(OIIO_SIMD_SSE) && 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_vec = _mm_cvtepu16_epi32 (a);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 unsigned short values, convert to int4
OIIO_FORCEINLINE void load (const short *values) {
#if defined(OIIO_SIMD_SSE) && 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_vec = _mm_cvtepi16_epi32 (a);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 unsigned char values, convert to int4
OIIO_FORCEINLINE void load (const unsigned char *values) {
#if defined(OIIO_SIMD_SSE) && 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_vec = _mm_cvtepu8_epi32 (a);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 unsigned char values, convert to int4
OIIO_FORCEINLINE void load (const char *values) {
#if defined(OIIO_SIMD_SSE) && 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_vec = _mm_cvtepi8_epi32 (a);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Store the values into memory
OIIO_FORCEINLINE void store (int *values) const {
#if defined(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_vec);
#else
for (int i = 0; i < elements; ++i)
values[i] = m_val[i];
#endif
}
/// Store the first n values into memory
OIIO_FORCEINLINE void 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];
}
/// Store the least significant 16 bits of each element into adjacent
/// unsigned shorts.
OIIO_FORCEINLINE void store (unsigned short *values) const {
#if defined(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
for (int i = 0; i < elements; ++i)
values[i] = m_val[i];
#endif
}
/// Store the least significant 8 bits of each element into adjacent
/// unsigned chars.
OIIO_FORCEINLINE void store (unsigned char *values) const {
#if defined(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
for (int i = 0; i < elements; ++i)
values[i] = m_val[i];
#endif
}
friend OIIO_FORCEINLINE int4 operator+ (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_add_epi32 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] + b.m_val[0],
a.m_val[1] + b.m_val[1],
a.m_val[2] + b.m_val[2],
a.m_val[3] + b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator+= (const int4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_add_epi32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] += a.m_val[i];
#endif
return *this;
}
OIIO_FORCEINLINE int4 operator- () const {
#if defined(OIIO_SIMD_SSE)
return _mm_sub_epi32 (_mm_setzero_si128(), m_vec);
#else
return int4 (-m_val[0], -m_val[1], -m_val[2], -m_val[3]);
#endif
}
friend OIIO_FORCEINLINE int4 operator- (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_sub_epi32 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] - b.m_val[0],
a.m_val[1] - b.m_val[1],
a.m_val[2] - b.m_val[2],
a.m_val[3] - b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator-= (const int4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_sub_epi32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] -= a.m_val[i];
#endif
return *this;
}
friend OIIO_FORCEINLINE int4 operator* (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return mm_mul_epi32 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] * b.m_val[0],
a.m_val[1] * b.m_val[1],
a.m_val[2] * b.m_val[2],
a.m_val[3] * b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator*= (const int4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = mm_mul_epi32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] *= a.m_val[i];
#endif
return *this;
}
OIIO_FORCEINLINE const int4 & operator*= (int val) {
#if defined(OIIO_SIMD_SSE)
m_vec = mm_mul_epi32 (m_vec, _mm_set1_epi32(val));
#else
for (int i = 0; i < elements; ++i)
m_val[i] *= val;
#endif
return *this;
}
friend OIIO_FORCEINLINE int4 operator/ (const int4& a, const int4& b) {
// NO INTEGER DIVISION IN SSE!
return int4 (a.m_val[0] / b.m_val[0],
a.m_val[1] / b.m_val[1],
a.m_val[2] / b.m_val[2],
a.m_val[3] / b.m_val[3]);
}
OIIO_FORCEINLINE const int4 & operator/= (const int4& a) {
// NO INTEGER DIVISION IN SSE!
for (int i = 0; i < elements; ++i)
m_val[i] /= a.m_val[i];
return *this;
}
OIIO_FORCEINLINE const int4 & operator/= (int val) {
// NO INTEGER DIVISION IN SSE!
for (int i = 0; i < elements; ++i)
m_val[i] /= val;
return *this;
}
friend OIIO_FORCEINLINE int4 operator% (const int4& a, const int4& b) {
// NO INTEGER MODULUS in SSE!
return int4 (a.m_val[0] % b.m_val[0],
a.m_val[1] % b.m_val[1],
a.m_val[2] % b.m_val[2],
a.m_val[3] % b.m_val[3]);
}
OIIO_FORCEINLINE const int4 & operator%= (const int4& a) {
// NO INTEGER MODULUS in SSE!
for (int i = 0; i < elements; ++i)
m_val[i] %= a.m_val[i];
return *this;
}
friend OIIO_FORCEINLINE int4 operator% (const int4& a, int w) {
// NO INTEGER MODULUS in SSE!
return int4 (a.m_val[0] % w,
a.m_val[1] % w,
a.m_val[2] % w,
a.m_val[3] % w);
}
OIIO_FORCEINLINE const int4 & operator%= (int a) {
// NO INTEGER MODULUS IN SSE!
for (int i = 0; i < elements; ++i)
m_val[i] %= a;
return *this;
}
friend OIIO_FORCEINLINE int4 operator% (int a, const int4& b) {
// NO INTEGER MODULUS in SSE!
return int4 (a % b.m_val[0],
a % b.m_val[1],
a % b.m_val[2],
a % b.m_val[3]);
}
friend OIIO_FORCEINLINE int4 operator& (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_and_si128 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] & b.m_val[0],
a.m_val[1] & b.m_val[1],
a.m_val[2] & b.m_val[2],
a.m_val[3] & b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator&= (const int4& a) {
return *this = *this & a;
}
friend OIIO_FORCEINLINE int4 operator| (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_or_si128 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] | b.m_val[0],
a.m_val[1] | b.m_val[1],
a.m_val[2] | b.m_val[2],
a.m_val[3] | b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator|= (const int4& a) {
return *this = *this | a;
}
friend OIIO_FORCEINLINE int4 operator^ (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_xor_si128 (a.m_vec, b.m_vec);
#else
return int4 (a.m_val[0] ^ b.m_val[0],
a.m_val[1] ^ b.m_val[1],
a.m_val[2] ^ b.m_val[2],
a.m_val[3] ^ b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const int4 & operator^= (const int4& a) {
return *this = *this ^ a;
}
OIIO_FORCEINLINE int4 operator~ () {
#if defined(OIIO_SIMD_SSE)
// Fastest way to bit-complement in SSE is to xor with 0xffffffff.
// Fastest way to fill an __m128 with all 1 bits is to cmpeq_epi8
// any value to itself.
# if defined(OIIO_SIMD_AVX) && (OIIO_GNUC_VERSION > 50000)
__m128i anyval = _mm_undefined_si128(); // AVX only, sigh
__m128i all_one_bits = _mm_cmpeq_epi8 (anyval, anyval);
# else
__m128i all_one_bits = _mm_cmpeq_epi8 (m_vec, m_vec);
# endif
return _mm_xor_si128 (m_vec, all_one_bits);
#else
return int4 (~(m_val[0]),
~(m_val[1]),
~(m_val[2]),
~(m_val[3]));
#endif
}
OIIO_FORCEINLINE int4 operator<< (const unsigned int bits) const {
#if defined(OIIO_SIMD_SSE)
return _mm_slli_epi32 (m_vec, bits);
#else
return int4 (m_val[0] << bits,
m_val[1] << bits,
m_val[2] << bits,
m_val[3] << bits);
#endif
}
OIIO_FORCEINLINE const int4 & operator<<= (const unsigned int bits) {
return *this = *this << bits;
}
// Arithmetic shift right (matches int>>, in that it preserves the
// sign bit).
OIIO_FORCEINLINE int4 operator>> (const unsigned int bits) {
#if defined(OIIO_SIMD_SSE)
return _mm_srai_epi32 (m_vec, bits);
#else
return int4 (m_val[0] >> bits,
m_val[1] >> bits,
m_val[2] >> bits,
m_val[3] >> bits);
#endif
}
OIIO_FORCEINLINE const int4 & operator>>= (const unsigned int bits) {
return *this = *this >> bits;
}
// 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.
OIIO_FORCEINLINE friend int4 srl (const int4& val, const unsigned int bits) {
#if defined(OIIO_SIMD_SSE)
return _mm_srli_epi32 (val.m_vec, bits);
#else
return int4 (int ((unsigned int)(val.m_val[0]) >> bits),
int ((unsigned int)(val.m_val[1]) >> bits),
int ((unsigned int)(val.m_val[2]) >> bits),
int ((unsigned int)(val.m_val[3]) >> bits));
#endif
}
friend OIIO_FORCEINLINE mask4 operator== (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_castsi128_ps(_mm_cmpeq_epi32 (a.m_vec, b.m_vec));
#else
return mask4 (a[0] == b[0], a[1] == b[1], a[2] == b[2], a[3] == b[3]);
#endif
}
friend OIIO_FORCEINLINE mask4 operator!= (const int4& a, const int4& b) {
return ! (a == b);
}
friend OIIO_FORCEINLINE mask4 operator< (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_castsi128_ps(_mm_cmplt_epi32 (a.m_vec, b.m_vec));
#else
return mask4 (a[0] < b[0], a[1] < b[1], a[2] < b[2], a[3] < b[3]);
#endif
}
friend OIIO_FORCEINLINE mask4 operator> (const int4& a, const int4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_castsi128_ps(_mm_cmpgt_epi32 (a.m_vec, b.m_vec));
#else
return mask4 (a[0] > b[0], a[1] > b[1], a[2] > b[2], a[3] > b[3]);
#endif
}
friend OIIO_FORCEINLINE mask4 operator>= (const int4& a, const int4& b) {
return !(a < b);
}
friend OIIO_FORCEINLINE mask4 operator<= (const int4& a, const int4& b) {
return !(a > b);
}
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const int4& val) {
return cout << val[0] << ' ' << val[1] << ' ' << val[2] << ' ' << val[3];
}
private:
// The actual data representation
union {
#if OIIO_SIMD
simd_t m_vec;
#endif
value_t m_val[4];
};
};
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(mask4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE int4 shuffle (const int4& a) {
#if defined(OIIO_SIMD_SSE)
return shuffle_sse<i0,i1,i2,i3> (__m128i(a));
#else
return int4(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 int4 shuffle (const int4& 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 int extract (const int4& v) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4
return _mm_extract_epi32(v.simd(), i); // SSE4.1 only
#else
return v[i];
#endif
}
/// Helper: substitute val for a[i]
template<int i>
OIIO_FORCEINLINE int4 insert (const int4& a, int val) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4
return _mm_insert_epi32 (a, val, i);
#else
int4 tmp = a;
tmp[i] = val;
return tmp;
#endif
}
/// The sum of all components, returned in all components.
OIIO_FORCEINLINE int4 vreduce_add (const int4& v) {
#if defined(OIIO_SIMD_SSE) && 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 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)
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
return int4(v[0] + v[1] + v[2] + v[3]);
#endif
}
/// The sum of all components, returned as a scalar.
OIIO_FORCEINLINE int reduce_add (const int4& v) {
#if defined(OIIO_SIMD_SSE)
return _mm_cvtsi128_si32(vreduce_add(v));
#else
return v[0] + v[1] + v[2] + v[3];
#endif
}
/// Bitwise "and" of all components.
OIIO_FORCEINLINE int reduce_and (const int4& v) {
#if defined(OIIO_SIMD_SSE)
// I think this is the best we can do for SSE, and I'm still not sure
// it's faster than the default scalar operation. But anyway...
// suppose v = (a, b, c, d)
int4 x = shuffle<1,0,3,2>(v) & v;
return extract<0>(x) & extract<2>(x);
#else
return v[0] & v[1] & v[2] & v[3];
#endif
}
/// Bitwise "or" of all components.
OIIO_FORCEINLINE int reduce_or (const int4& v) {
#if defined(OIIO_SIMD_SSE)
// I think this is the best we can do for SSE, and I'm still not sure
// it's faster than the default scalar operation. But anyway...
// suppose v = (a, b, c, d)
int4 x = shuffle<1,0,3,2>(v) | v;
return extract<0>(x) | extract<2>(x);
#else
return v[0] | v[1] | v[2] | v[3];
#endif
}
/// Use a mask to select between components of a (if mask[i] is false) and
/// b (if mask[i] is true).
OIIO_FORCEINLINE int4 blend (const int4& a, const int4& b, const mask4& mask)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_blendv_epi8 (a.simd(), b.simd(), _mm_castps_si128(mask));
#elif defined(OIIO_SIMD_SSE) /* SSE2 */
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
return int4 (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
}
/// Use a mask to select between components of a (if mask[i] is true) or
/// 0 (if mask[i] is true).
OIIO_FORCEINLINE int4 blend0 (const int4& a, const mask4& mask)
{
#if defined(OIIO_SIMD_SSE)
return _mm_and_si128(_mm_castps_si128(mask), a.simd());
#else
return int4 (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
}
/// Use a mask to select between components of a (if mask[i] is FALSE) or
/// 0 (if mask[i] is TRUE).
OIIO_FORCEINLINE int4 blend0not (const int4& a, const mask4& mask)
{
#if defined(OIIO_SIMD_SSE)
return _mm_andnot_si128(_mm_castps_si128(mask), a.simd());
#else
return int4 (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
}
/// 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).
OIIO_FORCEINLINE int4 select (const mask4& mask, const int4& a, const int4& b)
{
return blend (b, a, mask);
}
/// Per-element absolute value.
OIIO_FORCEINLINE int4 abs (const int4& a)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 3
return _mm_abs_epi32(a.simd());
#else
return int4 (std::abs(a[0]), std::abs(a[1]), std::abs(a[2]), std::abs(a[3]));
#endif
}
/// Per-element min
OIIO_FORCEINLINE int4 min (const int4& a, const int4& b)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_min_epi32 (a, b);
#else
return int4 (std::min (a[0], b[0]),
std::min (a[1], b[1]),
std::min (a[2], b[2]),
std::min (a[3], b[3]));
#endif
}
/// Per-element max
OIIO_FORCEINLINE int4 max (const int4& a, const int4& b)
{
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4 /* SSE >= 4.1 */
return _mm_max_epi32 (a, b);
#else
return int4 (std::max (a[0], b[0]),
std::max (a[1], b[1]),
std::max (a[2], b[2]),
std::max (a[3], b[3]));
#endif
}
// Circular bit rotate by k bits, for 4 values at once.
OIIO_FORCEINLINE int4 rotl32 (const int4& x, const unsigned int k)
{
return (x<<k) | srl(x,32-k);
}
/// 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 mask4 mask_t; ///< SIMD mask type
enum { elements = 4 }; ///< Number of scalar elements
enum { bits = 128 }; ///< Total number of bits
/// simd_t is the native SIMD type used
#if defined(OIIO_SIMD_SSE)
typedef __m128 simd_t;
#elif defined(OIIO_SIMD_NEON)
typedef float32x4_t simd_t;
#else
typedef float simd_t[elements];
#endif
/// Default constructor (contents undefined)
OIIO_FORCEINLINE float4 () { }
/// Destructor
OIIO_FORCEINLINE ~float4 () { }
/// Construct from a single value (store it in all slots)
OIIO_FORCEINLINE float4 (float a) { load(a); }
/// Construct from 3 or 4 values
OIIO_FORCEINLINE float4 (float a, float b, float c, float d=0.0f) { load(a,b,c,d); }
/// Construct from a pointer to 4 values
OIIO_FORCEINLINE float4 (const float *f) { load (f); }
/// Copy construct from another float4
OIIO_FORCEINLINE float4 (const float4 &other) {
#if defined(OIIO_SIMD_SSE) || defined(OIIO_SIMD_NEON)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
}
/// Construct from an int4 (promoting all components to float)
OIIO_FORCEINLINE explicit float4 (const int4& ival) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_cvtepi32_ps (ival.simd());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = float(ival[i]);
#endif
}
#if OIIO_SIMD
/// Construct from the underlying SIMD type
OIIO_FORCEINLINE float4 (const simd_t& m) : m_vec(m) { }
/// Return the raw SIMD type
OIIO_FORCEINLINE operator simd_t () const { return m_vec; }
OIIO_FORCEINLINE simd_t simd () const { return m_vec; }
#endif
/// Construct from a Imath::V3f
OIIO_FORCEINLINE float4 (const Imath::V3f &v) { load (v[0], v[1], v[2], 0.0f); }
/// Cast to a Imath::V3f
OIIO_FORCEINLINE 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
OIIO_FORCEINLINE float4 (const Imath::V4f &v) { load ((const float *)&v); }
/// Cast to a Imath::V4f
OIIO_FORCEINLINE const Imath::V4f& V4f () const { return *(const Imath::V4f*)this; }
#endif
/// Construct from a pointer to 4 unsigned short values
OIIO_FORCEINLINE explicit float4 (const unsigned short *vals) { load(vals); }
/// Construct from a pointer to 4 short values
OIIO_FORCEINLINE explicit float4 (const short *vals) { load(vals); }
/// Construct from a pointer to 4 unsigned char values
OIIO_FORCEINLINE explicit float4 (const unsigned char *vals) { load(vals); }
/// Construct from a pointer to 4 char values
OIIO_FORCEINLINE explicit float4 (const char *vals) { load(vals); }
#ifdef _HALF_H_
/// Construct from a pointer to 4 half (16 bit float) values
OIIO_FORCEINLINE explicit float4 (const half *vals) { load(vals); }
#endif
/// Assign a single value to all components
OIIO_FORCEINLINE const float4 & operator= (float a) { load(a); return *this; }
/// Assign a float4
OIIO_FORCEINLINE const float4 & operator= (float4 other) {
#if defined(OIIO_SIMD_SSE) || defined(OIIO_SIMD_NEON)
m_vec = other.m_vec;
#else
for (int i = 0; i < elements; ++i)
m_val[i] = other.m_val[i];
#endif
return *this;
}
/// Return a float4 with all components set to 0.0
static OIIO_FORCEINLINE const float4 Zero () {
#if defined(OIIO_SIMD_SSE)
return _mm_setzero_ps();
#else
return float4(0.0f);
#endif
}
/// Return a float4 with all components set to 1.0
static OIIO_FORCEINLINE const float4 One () { return float4(1.0f); }
/// 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.
static OIIO_FORCEINLINE const float4 Iota (float value=0.0f) {
return float4(value,value+1.0f,value+2.0f,value+3.0f);
}
/// Set all components to 0.0
OIIO_FORCEINLINE void clear () {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_setzero_ps();
#else
load (0.0f);
#endif
}
#if defined(ILMBASE_VERSION_MAJOR) && ILMBASE_VERSION_MAJOR >= 2
/// Assign from a Imath::V4f
OIIO_FORCEINLINE const float4 & operator= (const Imath::V4f &v) {
load ((const float *)&v);
return *this;
}
#endif
/// Assign from a Imath::V3f
OIIO_FORCEINLINE const float4 & operator= (const Imath::V3f &v) {
load (v[0], v[1], v[2], 0.0f);
return *this;
}
/// Component access (set)
OIIO_FORCEINLINE float& operator[] (int i) {
DASSERT(i<elements);
return m_val[i];
}
/// Component access (get)
OIIO_FORCEINLINE float operator[] (int i) const {
DASSERT(i<elements);
return m_val[i];
}
/// Helper: load a single value into all components
OIIO_FORCEINLINE void load (float val) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_set1_ps (val);
#elif defined(OIIO_SIMD_NEON)
m_vec = vdupq_n_f32 (val);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = val;
#endif
}
/// Helper: load 3 or 4 values. (If 3 are supplied, the 4th will be 0.)
OIIO_FORCEINLINE void load (float a, float b, float c, float d=0.0f) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_set_ps (d, c, b, a);
#elif defined(OIIO_SIMD_NEON)
float values[4] = { a, b, c, d };
m_vec = 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 load (const float *values) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_loadu_ps (values);
#elif defined(OIIO_SIMD_NEON)
m_vec = vld1q_f32 (values);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from a partial array of <=4 values. Unassigned values are
/// undefined.
OIIO_FORCEINLINE void load (const float *values, int n) {
#if defined(OIIO_SIMD_SSE)
switch (n) {
case 1:
m_vec = _mm_load_ss (values);
break;
case 2:
// Trickery: load one double worth of bits!
m_vec = _mm_castpd_ps (_mm_load_sd ((const double*)values));
break;
case 3:
m_vec = _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_vec = _mm_movelh_ps(xy, _mm_load_ss (values + 2));
break;
case 4:
m_vec = _mm_loadu_ps (values);
break;
default:
break;
}
#elif defined(OIIO_SIMD_NEON)
switch (n) {
case 1: m_vec = 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_vec = vld1q_f32 (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
}
/// Load from an array of 4 unsigned short values, convert to float
OIIO_FORCEINLINE void load (const unsigned short *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_vec = _mm_cvtepi32_ps (int4(values).simd());
// You might guess that the following is faster, but it's NOT:
// NO! m_vec = _mm_cvtpu16_ps (*(__m64*)values);
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 short values, convert to float
OIIO_FORCEINLINE void load (const short *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_vec = _mm_cvtepi32_ps (int4(values).simd());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 unsigned char values, convert to float
OIIO_FORCEINLINE void load (const unsigned char *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_vec = _mm_cvtepi32_ps (int4(values).simd());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
/// Load from an array of 4 char values, convert to float
OIIO_FORCEINLINE void load (const char *values) {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 2
m_vec = _mm_cvtepi32_ps (int4(values).simd());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
#ifdef _HALF_H_
/// Load from an array of 4 half values, convert to float
OIIO_FORCEINLINE void 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_vec = _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 CONST(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 = CONST(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, CONST(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_vec = final;
# undef CONST
# undef CONSTF
#else /* No SIMD defined: */
for (int i = 0; i < elements; ++i)
m_val[i] = values[i];
#endif
}
#endif /* _HALF_H_ */
OIIO_FORCEINLINE void store (float *values) const {
#if defined(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_vec);
#elif defined(OIIO_SIMD_NEON)
vst1q_f32 (values, m_vec);
#else
for (int i = 0; i < elements; ++i)
values[i] = m_val[i];
#endif
}
/// Store the first n values into memory
OIIO_FORCEINLINE void store (float *values, int n) const {
DASSERT (n >= 0 && n <= 4);
#if defined(OIIO_SIMD_SSE)
switch (n) {
case 1:
_mm_store_ss (values, m_vec);
break;
case 2:
// Trickery: store two floats as a double worth of bits
_mm_store_sd ((double*)values, _mm_castps_pd(m_vec));
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_vec));
// _mm_store_ss (values + 2, _mm_movehl_ps(m_vec,m_vec));
break;
case 4:
store (values);
break;
default:
break;
}
#elif defined(OIIO_SIMD_NEON)
switch (n) {
case 1:
vst1q_lane_f32 (values, m_vec, 0);
break;
case 2:
vst1q_lane_f32 (values++, m_vec, 0);
vst1q_lane_f32 (values, m_vec, 1);
break;
case 3:
vst1q_lane_f32 (values++, m_vec, 0);
vst1q_lane_f32 (values++, m_vec, 1);
vst1q_lane_f32 (values, m_vec, 2);
break;
case 4:
vst1q_f32 (values, m_vec); break;
default:
break;
}
#else
for (int i = 0; i < n; ++i)
values[i] = m_val[i];
#endif
}
#ifdef _HALF_H_
OIIO_FORCEINLINE void store (half *values) const {
#if defined(__F16C__) && defined(OIIO_SIMD_SSE)
__m128i h = _mm_cvtps_ph (m_vec, (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC));
_mm_store_sd ((double *)values, _mm_castsi128_pd(h));
#else
for (int i = 0; i < elements; ++i)
values[i] = m_val[i];
#endif
}
#endif
friend OIIO_FORCEINLINE float4 operator+ (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_add_ps (a.m_vec, b.m_vec);
#elif defined(OIIO_SIMD_NEON)
return vaddq_f32 (a.m_vec, b.m_vec);
#else
return float4 (a.m_val[0] + b.m_val[0],
a.m_val[1] + b.m_val[1],
a.m_val[2] + b.m_val[2],
a.m_val[3] + b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const float4 & operator+= (const float4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_add_ps (m_vec, a.m_vec);
#elif defined(OIIO_SIMD_NEON)
m_vec = vaddq_f32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] += a.m_val[i];
#endif
return *this;
}
OIIO_FORCEINLINE float4 operator- () const {
#if defined(OIIO_SIMD_SSE)
return _mm_sub_ps (_mm_setzero_ps(), m_vec);
#elif defined(OIIO_SIMD_NEON)
return vsubq_f32 (Zero(), m_vec);
#else
return float4 (-m_val[0], -m_val[1], -m_val[2], -m_val[3]);
#endif
}
friend OIIO_FORCEINLINE float4 operator- (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_sub_ps (a.m_vec, b.m_vec);
#elif defined(OIIO_SIMD_NEON)
return vsubq_f32 (a.m_vec, b.m_vec);
#else
return float4 (a.m_val[0] - b.m_val[0],
a.m_val[1] - b.m_val[1],
a.m_val[2] - b.m_val[2],
a.m_val[3] - b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const float4 & operator-= (const float4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_sub_ps (m_vec, a.m_vec);
#elif defined(OIIO_SIMD_NEON)
m_vec = vsubq_f32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] -= a.m_val[i];
#endif
return *this;
}
friend OIIO_FORCEINLINE float4 operator* (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_mul_ps (a.m_vec, b.m_vec);
#elif defined(OIIO_SIMD_NEON)
return vmulq_f32 (a.m_vec, b.m_vec);
#else
return float4 (a.m_val[0] * b.m_val[0],
a.m_val[1] * b.m_val[1],
a.m_val[2] * b.m_val[2],
a.m_val[3] * b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const float4 & operator*= (const float4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_mul_ps (m_vec, a.m_vec);
#elif defined(OIIO_SIMD_NEON)
m_vec = vmulq_f32 (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] *= a.m_val[i];
#endif
return *this;
}
OIIO_FORCEINLINE const float4 & operator*= (float val) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_mul_ps (m_vec, _mm_set1_ps(val));
#elif defined(OIIO_SIMD_NEON)
m_vec = vmulq_f32 (m_vec, vdupq_n_f32(val));
#else
for (int i = 0; i < elements; ++i)
m_val[i] *= val;
#endif
return *this;
}
friend OIIO_FORCEINLINE float4 operator/ (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_div_ps (a.m_vec, b.m_vec);
#else
return float4 (a.m_val[0] / b.m_val[0],
a.m_val[1] / b.m_val[1],
a.m_val[2] / b.m_val[2],
a.m_val[3] / b.m_val[3]);
#endif
}
OIIO_FORCEINLINE const float4 & operator/= (const float4& a) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_div_ps (m_vec, a.m_vec);
#else
for (int i = 0; i < elements; ++i)
m_val[i] /= a.m_val[i];
#endif
return *this;
}
OIIO_FORCEINLINE const float4 & operator/= (float val) {
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_div_ps (m_vec, _mm_set1_ps(val));
#else
for (int i = 0; i < elements; ++i)
m_val[i] /= val;
#endif
return *this;
}
friend OIIO_FORCEINLINE mask_t operator== (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmpeq_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] == b[0], a[1] == b[1], a[2] == b[2], a[3] == b[3]);
#endif
}
friend OIIO_FORCEINLINE mask_t operator!= (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmpneq_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] != b[0], a[1] != b[1], a[2] != b[2], a[3] != b[3]);
#endif
}
friend OIIO_FORCEINLINE mask_t operator< (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmplt_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] < b[0], a[1] < b[1], a[2] < b[2], a[3] < b[3]);
#endif
}
friend OIIO_FORCEINLINE mask_t operator> (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmpgt_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] > b[0], a[1] > b[1], a[2] > b[2], a[3] > b[3]);
#endif
}
friend OIIO_FORCEINLINE mask_t operator>= (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmpge_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] >= b[0], a[1] >= b[1], a[2] >= b[2], a[3] >= b[3]);
#endif
}
friend OIIO_FORCEINLINE mask_t operator<= (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_cmple_ps (a.m_vec, b.m_vec);
#else
return mask_t (a[0] <= b[0], a[1] <= b[1], a[2] <= b[2], a[3] <= b[3]);
#endif
}
// Some oddball items that are handy
/// Combine the first two components of A with the first two components
/// of B.
friend OIIO_FORCEINLINE float4 AxyBxy (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_movelh_ps (a.m_vec, b.m_vec);
#else
return float4 (a[0], a[1], b[0], b[1]);
#endif
}
/// Combine the first two components of A with the first two components
/// of B, but interleaved.
friend OIIO_FORCEINLINE float4 AxBxAyBy (const float4& a, const float4& b) {
#if defined(OIIO_SIMD_SSE)
return _mm_unpacklo_ps (a.m_vec, b.m_vec);
#else
return float4 (a[0], b[0], a[1], b[1]);
#endif
}
/// Return xyz components, plus 0 for w
float4 xyz0 () const {
#if defined(OIIO_SIMD_SSE) && OIIO_SIMD_SSE >= 4
return _mm_insert_ps (m_vec, _mm_set_ss(0.0f), 3<<4);
#elif defined(OIIO_SIMD_SSE) /* SSE2 */
float4 tmp = m_vec;
tmp[3] = 0.0f;
return tmp;
#else
return float4 (m_val[0], m_val[1], m_val[2], 0.0f);
#endif
}
/// Stream output
friend inline std::ostream& operator<< (std::ostream& cout, const float4& val) {
return cout << val[0] << ' ' << val[1] << ' ' << val[2] << ' ' << val[3];
}
private:
// The actual data representation
union {
#if OIIO_SIMD
simd_t m_vec;
#endif
value_t m_val[elements];
};
};
// Implementation had to be after the definition of int4.
OIIO_FORCEINLINE mask4::mask4 (const int4& ival)
{
#if defined(OIIO_SIMD_SSE)
m_vec = (ival != int4::Zero());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = ival[i] ? -1 : 0;
#endif
}
// Implementation had to be after the definition of float4.
OIIO_FORCEINLINE int4::int4 (const float4& f)
{
#if defined(OIIO_SIMD_SSE)
m_vec = _mm_cvttps_epi32(f.simd());
#else
for (int i = 0; i < elements; ++i)
m_val[i] = (int) f[i];
#endif
}
/// Helper: shuffle/swizzle with constant (templated) indices.
/// Example: shuffle<1,1,2,2>(mask4(a,b,c,d)) returns (b,b,c,c)
template<int i0, int i1, int i2, int i3>
OIIO_FORCEINLINE float4 shuffle (const float4& a) {
#if defined(OIIO_SIMD_SSE)
return shuffle_sse<i0,i1,i2,i3> (__m128(a));
#else
return float4(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 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_vec); return vdupq_lane_f32(t,0);
}
template<> OIIO_FORCEINLINE float4 shuffle<1> (const float4& a) {
float32x2_t t = vget_low_f32(a.m_vec); return vdupq_lane_f32(t,1);
}
template<> OIIO_FORCEINLINE float4 shuffle<2> (const float4& a) {
float32x2_t t = vget_high_f32(a.m_vec); return vdupq_lane_f32(t,0);
}
template<> OIIO_FORCEINLINE float4 shuffle<3> (const float4& a) {
float32x2_t t = vget_high_f32(a.m_vec); 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 defined(OIIO_SIMD_SSE)
return _mm_cvtss_f32(shuffle_sse<i,i,i,i>(a.simd()));
#else
return a[i];
#endif
}
#if defined(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 defined(OIIO_SIMD_SSE) && 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
}
OIIO_FORCEINLINE int4 bitcast_to_int4 (const mask4& x)
{
#if defined(OIIO_SIMD_SSE)
return _mm_castps_si128 (x.simd());
#else
return *(int4 *)&x;
#endif
}
OIIO_FORCEINLINE int4 bitcast_to_int4 (const float4& x)
{
#if defined(OIIO_SIMD_SSE)
return _mm_castps_si128 (x.simd());
#else
return *(int4 *)&x;
#endif
}
OIIO_FORCEINLINE float4 bitcast_to_float4 (const int4& x)
{
#if defined(OIIO_SIMD_SSE)
return _mm_castsi128_ps (x.simd());
#else
return *(float4 *)&x;
#endif
}
/// The sum of all components, returned in all components.
OIIO_FORCEINLINE float4 vreduce_add (const float4& v) {
#if defined(OIIO_SIMD_SSE) && 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
}
/// The sum of all components, returned as a scalar.
OIIO_FORCEINLINE float reduce_add (const float4& v) {
#if defined(OIIO_SIMD_SSE)
return _mm_cvtss_f32(vreduce_add (v));
#else
return v[0] + v[1] + v[2] + v[3];
#endif
}
/// Return the float dot (inner) product of a and b.
OIIO_FORCEINLINE float dot (const float4 &a, const float4 &b) {
return reduce_add (a*b);
}
/// Return the float dot (inner) product of the first three components of
/// a and b.
OIIO_FORCEINLINE float dot3 (const float4 &a, const float4 &b) {
return reduce_add (insert<3>(a*b, 0.0f));
}
/// Return the dot (inner) product of a and b in every component of a
/// float4.
OIIO_FORCEINLINE float4 vdot (const float4 &a, const float4 &b) {
return vreduce_add (a*b);
}
/// Return the dot (inner) product of the first three components of
/// a and b, in every product of a float4.
OIIO_FORCEINLINE float4 vdot3 (const float4 &a, const float4 &b) {
return vreduce_add (insert<3>(a*b, 0.0f));
}
/// Use a mask to select between components of a (if mask[i] is false) and
/// b (if mask[i] is true).
OIIO_FORCEINLINE float4 blend (const float4& a, const float4& b, const mask4& mask)
{
#if defined(OIIO_SIMD_SSE) && 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
}
/// Use a mask to select between components of a (if mask[i] is true) or
/// 0 (if mask[i] is true).
OIIO_FORCEINLINE float4 blend0 (const float4& a, const mask4& mask)
{
#if defined(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
}
/// Use a mask to select between components of a (if mask[i] is FALSE) or
/// 0 (if mask[i] is TRUE).
OIIO_FORCEINLINE float4 blend0not (const float4& a, const mask4& mask)
{
#if defined(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
}
/// 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).
OIIO_FORCEINLINE float4 select (const mask4& mask, const float4& a, const float4& b)
{
return blend (b, a, mask);
}
/// Per-element absolute value.
OIIO_FORCEINLINE float4 abs (const float4& a)
{
#if defined(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
return float4 (fabsf(a[0]), fabsf(a[1]), fabsf(a[2]), fabsf(a[3]));
#endif
}
/// Per-element ceil.
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
return float4 (ceilf(a[0]), ceilf(a[1]), ceilf(a[2]), ceilf(a[3]));
#endif
}
/// Per-element floor.
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
return float4 (floorf(a[0]), floorf(a[1]), floorf(a[2]), floorf(a[3]));
#endif
}
/// Per-element round to nearest integer (rounding away from 0 in cases
/// that are exactly half way).
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
return float4 (roundf(a[0]), roundf(a[1]), roundf(a[2]), roundf(a[3]));
#endif
}
/// Per-element (int)floor.
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_int4 (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
return int4 ((int)floorf(a[0]), (int)floorf(a[1]),
(int)floorf(a[2]), (int)floorf(a[3]));
#endif
}
/// Per-element min
OIIO_FORCEINLINE float4 min (const float4& a, const float4& b)
{
#if defined(OIIO_SIMD_SSE)
return _mm_min_ps (a, b);
#else
return float4 (std::min (a[0], b[0]),
std::min (a[1], b[1]),
std::min (a[2], b[2]),
std::min (a[3], b[3]));
#endif
}
/// Per-element max
OIIO_FORCEINLINE float4 max (const float4& a, const float4& b)
{
#if defined(OIIO_SIMD_SSE)
return _mm_max_ps (a, b);
#else
return float4 (std::max (a[0], b[0]),
std::max (a[1], b[1]),
std::max (a[2], b[2]),
std::max (a[3], b[3]));
#endif
}
/// 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.
OIIO_FORCEINLINE void transpose (float4 &a, float4 &b, float4 &c, float4 &d)
{
#if defined(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 defined(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
}
/// 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.
OIIO_FORCEINLINE void transpose (int4 &a, int4 &b, int4 &c, int4 &d)
{
#if defined(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 defined(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
}
/// Make a float4 consisting of the first element of each of 4 float4's.
OIIO_FORCEINLINE float4 AxBxCxDx (const float4& a, const float4& b,
const float4& c, const float4& d)
{
#if defined(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
}
/// Make an int4 consisting of the first element of each of 4 int4's.
OIIO_FORCEINLINE int4 AxBxCxDx (const int4& a, const int4& b,
const int4& c, const int4& d)
{
#if defined(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
}
/// 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,4> { typedef int4 type; };
template<> struct VecType<float,4> { typedef float4 type; };
template<> struct VecType<bool,4> { typedef mask4 type; };
} // end namespace
OIIO_NAMESPACE_END
|