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
|
// cppspmd_sse.h
// Copyright 2020-2022 Binomial LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Notes for Basis Universal:
// All of the "cppspmd" code and headers are OPTIONAL to Basis Universal. if BASISU_SUPPORT_SSE is 0, it will never be included and does not impact compilation.
// The techniques used in this code were originally demonstrated for AVX2 by Nicolas Guillemot, Jefferson Amstutz in their "CppSPMD" project.
// This is new code for use in Basis Universal, although it uses the same general SPMD techniques in SSE 2/4.
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include <utility>
#include <algorithm>
#if CPPSPMD_SSE2
#include <xmmintrin.h> // SSE
#include <emmintrin.h> // SSE2
#else
#include <xmmintrin.h> // SSE
#include <emmintrin.h> // SSE2
#include <pmmintrin.h> // SSE3
#include <tmmintrin.h> // SSSE3
#include <smmintrin.h> // SSE4.1
//#include <nmmintrin.h> // SSE4.2
#endif
#undef CPPSPMD_SSE
#undef CPPSPMD_AVX1
#undef CPPSPMD_AVX2
#undef CPPSPMD_AVX
#undef CPPSPMD_FLOAT4
#undef CPPSPMD_INT16
#define CPPSPMD_SSE 1
#define CPPSPMD_AVX 0
#define CPPSPMD_AVX1 0
#define CPPSPMD_AVX2 0
#define CPPSPMD_FLOAT4 0
#define CPPSPMD_INT16 0
#ifdef _MSC_VER
#ifndef CPPSPMD_DECL
#define CPPSPMD_DECL(type, name) __declspec(align(16)) type name
#endif
#ifndef CPPSPMD_ALIGN
#define CPPSPMD_ALIGN(v) __declspec(align(v))
#endif
#define _mm_undefined_si128 _mm_setzero_si128
#define _mm_undefined_ps _mm_setzero_ps
#else
#ifndef CPPSPMD_DECL
#define CPPSPMD_DECL(type, name) type name __attribute__((aligned(32)))
#endif
#ifndef CPPSPMD_ALIGN
#define CPPSPMD_ALIGN(v) __attribute__((aligned(v)))
#endif
#endif
#ifndef CPPSPMD_FORCE_INLINE
#ifdef _DEBUG
#define CPPSPMD_FORCE_INLINE inline
#else
#ifdef _MSC_VER
#define CPPSPMD_FORCE_INLINE __forceinline
#else
#define CPPSPMD_FORCE_INLINE inline
#endif
#endif
#endif
#undef CPPSPMD
#undef CPPSPMD_ARCH
#if CPPSPMD_SSE2
#define CPPSPMD_SSE41 0
#define CPPSPMD cppspmd_sse2
#define CPPSPMD_ARCH _sse2
#else
#define CPPSPMD_SSE41 1
#define CPPSPMD cppspmd_sse41
#define CPPSPMD_ARCH _sse41
#endif
#ifndef CPPSPMD_GLUER
#define CPPSPMD_GLUER(a, b) a##b
#endif
#ifndef CPPSPMD_GLUER2
#define CPPSPMD_GLUER2(a, b) CPPSPMD_GLUER(a, b)
#endif
#ifndef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, CPPSPMD_ARCH)
#endif
#undef VASSERT
#define VCOND(cond) ((exec_mask(vbool(cond)) & m_exec).get_movemask() == m_exec.get_movemask())
#define VASSERT(cond) assert( VCOND(cond) )
#define CPPSPMD_ALIGNMENT (16)
#define storeu_si32(p, a) (void)(*(int*)(p) = _mm_cvtsi128_si32((a)))
namespace CPPSPMD
{
const int PROGRAM_COUNT_SHIFT = 2;
const int PROGRAM_COUNT = 1 << PROGRAM_COUNT_SHIFT;
template <typename N> inline N* aligned_new() { void* p = _mm_malloc(sizeof(N), 64); new (p) N; return static_cast<N*>(p); }
template <typename N> void aligned_delete(N* p) { if (p) { p->~N(); _mm_free(p); } }
CPPSPMD_DECL(const uint32_t, g_allones_128[4]) = { UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX };
CPPSPMD_DECL(const uint32_t, g_x_128[4]) = { UINT32_MAX, 0, 0, 0 };
CPPSPMD_DECL(const float, g_onef_128[4]) = { 1.0f, 1.0f, 1.0f, 1.0f };
CPPSPMD_DECL(const uint32_t, g_oneu_128[4]) = { 1, 1, 1, 1 };
CPPSPMD_DECL(const uint32_t, g_lane_masks_128[4][4]) =
{
{ UINT32_MAX, 0, 0, 0 },
{ 0, UINT32_MAX, 0, 0 },
{ 0, 0, UINT32_MAX, 0 },
{ 0, 0, 0, UINT32_MAX },
};
#if CPPSPMD_SSE41
CPPSPMD_FORCE_INLINE __m128i _mm_blendv_epi32(__m128i a, __m128i b, __m128i c) { return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(c))); }
#endif
CPPSPMD_FORCE_INLINE __m128i blendv_epi8(__m128i a, __m128i b, __m128i mask)
{
#if CPPSPMD_SSE2
return _mm_castps_si128(_mm_or_ps(_mm_and_ps(_mm_castsi128_ps(mask), _mm_castsi128_ps(b)), _mm_andnot_ps(_mm_castsi128_ps(mask), _mm_castsi128_ps(a))));
#else
return _mm_blendv_epi8(a, b, mask);
#endif
}
CPPSPMD_FORCE_INLINE __m128 blendv_mask_ps(__m128 a, __m128 b, __m128 mask)
{
#if CPPSPMD_SSE2
// We know it's a mask, so we can just emulate the blend.
return _mm_or_ps(_mm_and_ps(mask, b), _mm_andnot_ps(mask, a));
#else
return _mm_blendv_ps(a, b, mask);
#endif
}
CPPSPMD_FORCE_INLINE __m128 blendv_ps(__m128 a, __m128 b, __m128 mask)
{
#if CPPSPMD_SSE2
// Input is not a mask, but MSB bits - so emulate _mm_blendv_ps() by replicating bit 31.
mask = _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(mask), 31));
return _mm_or_ps(_mm_and_ps(mask, b), _mm_andnot_ps(mask, a));
#else
return _mm_blendv_ps(a, b, mask);
#endif
}
CPPSPMD_FORCE_INLINE __m128i blendv_mask_epi32(__m128i a, __m128i b, __m128i mask)
{
return _mm_castps_si128(blendv_mask_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(mask)));
}
CPPSPMD_FORCE_INLINE __m128i blendv_epi32(__m128i a, __m128i b, __m128i mask)
{
return _mm_castps_si128(blendv_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(mask)));
}
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE int extract_x(const __m128i& vec) { return _mm_cvtsi128_si32(vec); }
CPPSPMD_FORCE_INLINE int extract_y(const __m128i& vec) { return _mm_cvtsi128_si32(_mm_shuffle_epi32(vec, 0x55)); }
CPPSPMD_FORCE_INLINE int extract_z(const __m128i& vec) { return _mm_cvtsi128_si32(_mm_shuffle_epi32(vec, 0xAA)); }
CPPSPMD_FORCE_INLINE int extract_w(const __m128i& vec) { return _mm_cvtsi128_si32(_mm_shuffle_epi32(vec, 0xFF)); }
// Returns float bits as int, to emulate _mm_extract_ps()
CPPSPMD_FORCE_INLINE int extract_ps_x(const __m128& vec) { float f = _mm_cvtss_f32(vec); return *(const int*)&f; }
CPPSPMD_FORCE_INLINE int extract_ps_y(const __m128& vec) { float f = _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0x55)); return *(const int*)&f; }
CPPSPMD_FORCE_INLINE int extract_ps_z(const __m128& vec) { float f = _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0xAA)); return *(const int*)&f; }
CPPSPMD_FORCE_INLINE int extract_ps_w(const __m128& vec) { float f = _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0xFF)); return *(const int*)&f; }
// Returns floats
CPPSPMD_FORCE_INLINE float extractf_ps_x(const __m128& vec) { return _mm_cvtss_f32(vec); }
CPPSPMD_FORCE_INLINE float extractf_ps_y(const __m128& vec) { return _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0x55)); }
CPPSPMD_FORCE_INLINE float extractf_ps_z(const __m128& vec) { return _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0xAA)); }
CPPSPMD_FORCE_INLINE float extractf_ps_w(const __m128& vec) { return _mm_cvtss_f32(_mm_shuffle_ps(vec, vec, 0xFF)); }
#else
CPPSPMD_FORCE_INLINE int extract_x(const __m128i& vec) { return _mm_extract_epi32(vec, 0); }
CPPSPMD_FORCE_INLINE int extract_y(const __m128i& vec) { return _mm_extract_epi32(vec, 1); }
CPPSPMD_FORCE_INLINE int extract_z(const __m128i& vec) { return _mm_extract_epi32(vec, 2); }
CPPSPMD_FORCE_INLINE int extract_w(const __m128i& vec) { return _mm_extract_epi32(vec, 3); }
// Returns float bits as int
CPPSPMD_FORCE_INLINE int extract_ps_x(const __m128& vec) { return _mm_extract_ps(vec, 0); }
CPPSPMD_FORCE_INLINE int extract_ps_y(const __m128& vec) { return _mm_extract_ps(vec, 1); }
CPPSPMD_FORCE_INLINE int extract_ps_z(const __m128& vec) { return _mm_extract_ps(vec, 2); }
CPPSPMD_FORCE_INLINE int extract_ps_w(const __m128& vec) { return _mm_extract_ps(vec, 3); }
// Returns floats
CPPSPMD_FORCE_INLINE float extractf_ps_x(const __m128& vec) { int v = extract_ps_x(vec); return *(const float*)&v; }
CPPSPMD_FORCE_INLINE float extractf_ps_y(const __m128& vec) { int v = extract_ps_y(vec); return *(const float*)&v; }
CPPSPMD_FORCE_INLINE float extractf_ps_z(const __m128& vec) { int v = extract_ps_z(vec); return *(const float*)&v; }
CPPSPMD_FORCE_INLINE float extractf_ps_w(const __m128& vec) { int v = extract_ps_w(vec); return *(const float*)&v; }
#endif
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE __m128i insert_x(const __m128i& vec, int v) { return _mm_insert_epi16(_mm_insert_epi16(vec, v, 0), (uint32_t)v >> 16U, 1); }
CPPSPMD_FORCE_INLINE __m128i insert_y(const __m128i& vec, int v) { return _mm_insert_epi16(_mm_insert_epi16(vec, v, 2), (uint32_t)v >> 16U, 3); }
CPPSPMD_FORCE_INLINE __m128i insert_z(const __m128i& vec, int v) { return _mm_insert_epi16(_mm_insert_epi16(vec, v, 4), (uint32_t)v >> 16U, 5); }
CPPSPMD_FORCE_INLINE __m128i insert_w(const __m128i& vec, int v) { return _mm_insert_epi16(_mm_insert_epi16(vec, v, 6), (uint32_t)v >> 16U, 7); }
#else
CPPSPMD_FORCE_INLINE __m128i insert_x(const __m128i& vec, int v) { return _mm_insert_epi32(vec, v, 0); }
CPPSPMD_FORCE_INLINE __m128i insert_y(const __m128i& vec, int v) { return _mm_insert_epi32(vec, v, 1); }
CPPSPMD_FORCE_INLINE __m128i insert_z(const __m128i& vec, int v) { return _mm_insert_epi32(vec, v, 2); }
CPPSPMD_FORCE_INLINE __m128i insert_w(const __m128i& vec, int v) { return _mm_insert_epi32(vec, v, 3); }
#endif
#if CPPSPMD_SSE2
inline __m128i shuffle_epi8(const __m128i& a, const __m128i& b)
{
// Just emulate _mm_shuffle_epi8. This is very slow, but what else can we do?
CPPSPMD_ALIGN(16) uint8_t av[16];
_mm_store_si128((__m128i*)av, a);
CPPSPMD_ALIGN(16) uint8_t bvi[16];
_mm_store_ps((float*)bvi, _mm_and_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(_mm_set1_epi32(0x0F0F0F0F))));
CPPSPMD_ALIGN(16) uint8_t result[16];
result[0] = av[bvi[0]];
result[1] = av[bvi[1]];
result[2] = av[bvi[2]];
result[3] = av[bvi[3]];
result[4] = av[bvi[4]];
result[5] = av[bvi[5]];
result[6] = av[bvi[6]];
result[7] = av[bvi[7]];
result[8] = av[bvi[8]];
result[9] = av[bvi[9]];
result[10] = av[bvi[10]];
result[11] = av[bvi[11]];
result[12] = av[bvi[12]];
result[13] = av[bvi[13]];
result[14] = av[bvi[14]];
result[15] = av[bvi[15]];
return _mm_andnot_si128(_mm_cmplt_epi8(b, _mm_setzero_si128()), _mm_load_si128((__m128i*)result));
}
#else
CPPSPMD_FORCE_INLINE __m128i shuffle_epi8(const __m128i& a, const __m128i& b)
{
return _mm_shuffle_epi8(a, b);
}
#endif
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE __m128i min_epi32(__m128i a, __m128i b)
{
return blendv_mask_epi32(b, a, _mm_cmplt_epi32(a, b));
}
CPPSPMD_FORCE_INLINE __m128i max_epi32(__m128i a, __m128i b)
{
return blendv_mask_epi32(b, a, _mm_cmpgt_epi32(a, b));
}
CPPSPMD_FORCE_INLINE __m128i min_epu32(__m128i a, __m128i b)
{
__m128i n = _mm_set1_epi32(0x80000000);
__m128i ac = _mm_add_epi32(a, n);
__m128i bc = _mm_add_epi32(b, n);
return blendv_mask_epi32(b, a, _mm_cmplt_epi32(ac, bc));
}
CPPSPMD_FORCE_INLINE __m128i max_epu32(__m128i a, __m128i b)
{
__m128i n = _mm_set1_epi32(0x80000000);
__m128i ac = _mm_add_epi32(a, n);
__m128i bc = _mm_add_epi32(b, n);
return blendv_mask_epi32(b, a, _mm_cmpgt_epi32(ac, bc));
}
#else
CPPSPMD_FORCE_INLINE __m128i min_epi32(__m128i a, __m128i b)
{
return _mm_min_epi32(a, b);
}
CPPSPMD_FORCE_INLINE __m128i max_epi32(__m128i a, __m128i b)
{
return _mm_max_epi32(a, b);
}
CPPSPMD_FORCE_INLINE __m128i min_epu32(__m128i a, __m128i b)
{
return _mm_min_epu32(a, b);
}
CPPSPMD_FORCE_INLINE __m128i max_epu32(__m128i a, __m128i b)
{
return _mm_max_epu32(a, b);
}
#endif
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE __m128i abs_epi32(__m128i a)
{
__m128i sign_mask = _mm_srai_epi32(a, 31);
return _mm_sub_epi32(_mm_castps_si128(_mm_xor_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(sign_mask))), sign_mask);
}
#else
CPPSPMD_FORCE_INLINE __m128i abs_epi32(__m128i a)
{
return _mm_abs_epi32(a);
}
#endif
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE __m128i mullo_epi32(__m128i a, __m128i b)
{
__m128i tmp1 = _mm_mul_epu32(a, b);
__m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4));
return _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0, 0, 2, 0)), _mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0, 0, 2, 0)));
}
#else
CPPSPMD_FORCE_INLINE __m128i mullo_epi32(__m128i a, __m128i b)
{
return _mm_mullo_epi32(a, b);
}
#endif
CPPSPMD_FORCE_INLINE __m128i mulhi_epu32(__m128i a, __m128i b)
{
__m128i tmp1 = _mm_mul_epu32(a, b);
__m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4));
return _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0, 0, 3, 1)), _mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0, 0, 3, 1)));
}
#if CPPSPMD_SSE2
inline __m128i load_rgba32(const void* p)
{
__m128i xmm = _mm_cvtsi32_si128(*(const int*)p);
xmm = _mm_unpacklo_epi8(xmm, _mm_setzero_si128());
xmm = _mm_unpacklo_epi16(xmm, _mm_setzero_si128());
return xmm;
}
#else
inline __m128i load_rgba32(const void* p)
{
return _mm_cvtepu8_epi32(_mm_castps_si128(_mm_load_ss((const float*)p)));
}
#endif
inline void transpose4x4(__m128i& x, __m128i& y, __m128i& z, __m128i& w, const __m128i& r0, const __m128i& r1, const __m128i& r2, const __m128i& r3)
{
__m128i t0 = _mm_unpacklo_epi32(r0, r1);
__m128i t1 = _mm_unpacklo_epi32(r2, r3);
__m128i t2 = _mm_unpackhi_epi32(r0, r1);
__m128i t3 = _mm_unpackhi_epi32(r2, r3);
x = _mm_unpacklo_epi64(t0, t1);
y = _mm_unpackhi_epi64(t0, t1);
z = _mm_unpacklo_epi64(t2, t3);
w = _mm_unpackhi_epi64(t2, t3);
}
const uint32_t ALL_ON_MOVEMASK = 0xF;
struct spmd_kernel
{
struct vint;
struct lint;
struct vbool;
struct vfloat;
typedef int int_t;
typedef vint vint_t;
typedef lint lint_t;
// Exec mask
struct exec_mask
{
__m128i m_mask;
exec_mask() = default;
CPPSPMD_FORCE_INLINE explicit exec_mask(const vbool& b);
CPPSPMD_FORCE_INLINE explicit exec_mask(const __m128i& mask) : m_mask(mask) { }
CPPSPMD_FORCE_INLINE void enable_lane(uint32_t lane) { m_mask = _mm_load_si128((const __m128i *)&g_lane_masks_128[lane][0]); }
static CPPSPMD_FORCE_INLINE exec_mask all_on() { return exec_mask{ _mm_load_si128((const __m128i*)g_allones_128) }; }
static CPPSPMD_FORCE_INLINE exec_mask all_off() { return exec_mask{ _mm_setzero_si128() }; }
CPPSPMD_FORCE_INLINE uint32_t get_movemask() const { return _mm_movemask_ps(_mm_castsi128_ps(m_mask)); }
};
friend CPPSPMD_FORCE_INLINE bool all(const exec_mask& e);
friend CPPSPMD_FORCE_INLINE bool any(const exec_mask& e);
CPPSPMD_FORCE_INLINE bool spmd_all() const { return all(m_exec); }
CPPSPMD_FORCE_INLINE bool spmd_any() const { return any(m_exec); }
CPPSPMD_FORCE_INLINE bool spmd_none() { return !any(m_exec); }
// true if cond is true for all active lanes - false if no active lanes
CPPSPMD_FORCE_INLINE bool spmd_all(const vbool& e) { uint32_t m = m_exec.get_movemask(); return (m != 0) && ((exec_mask(e) & m_exec).get_movemask() == m); }
// true if cond is true for any active lanes
CPPSPMD_FORCE_INLINE bool spmd_any(const vbool& e) { return (exec_mask(e) & m_exec).get_movemask() != 0; }
CPPSPMD_FORCE_INLINE bool spmd_none(const vbool& e) { return !spmd_any(e); }
friend CPPSPMD_FORCE_INLINE exec_mask operator^ (const exec_mask& a, const exec_mask& b);
friend CPPSPMD_FORCE_INLINE exec_mask operator& (const exec_mask& a, const exec_mask& b);
friend CPPSPMD_FORCE_INLINE exec_mask operator| (const exec_mask& a, const exec_mask& b);
exec_mask m_exec;
exec_mask m_kernel_exec;
exec_mask m_continue_mask;
#ifdef _DEBUG
bool m_in_loop;
#endif
CPPSPMD_FORCE_INLINE uint32_t get_movemask() const { return m_exec.get_movemask(); }
void init(const exec_mask& kernel_exec);
// Varying bool
struct vbool
{
__m128i m_value;
vbool() = default;
CPPSPMD_FORCE_INLINE vbool(bool value) : m_value(_mm_set1_epi32(value ? UINT32_MAX : 0)) { }
CPPSPMD_FORCE_INLINE explicit vbool(const __m128i& value) : m_value(value) { }
CPPSPMD_FORCE_INLINE explicit operator vfloat() const;
CPPSPMD_FORCE_INLINE explicit operator vint() const;
private:
//vbool& operator=(const vbool&);
};
friend vbool operator!(const vbool& v);
CPPSPMD_FORCE_INLINE vbool& store(vbool& dst, const vbool& src)
{
dst.m_value = blendv_mask_epi32(dst.m_value, src.m_value, m_exec.m_mask);
return dst;
}
CPPSPMD_FORCE_INLINE vbool& store_all(vbool& dst, const vbool& src)
{
dst.m_value = src.m_value;
return dst;
}
// Varying float
struct vfloat
{
__m128 m_value;
vfloat() = default;
CPPSPMD_FORCE_INLINE explicit vfloat(const __m128& v) : m_value(v) { }
CPPSPMD_FORCE_INLINE vfloat(float value) : m_value(_mm_set1_ps(value)) { }
CPPSPMD_FORCE_INLINE explicit vfloat(int value) : m_value(_mm_set1_ps((float)value)) { }
private:
//vfloat& operator=(const vfloat&);
};
CPPSPMD_FORCE_INLINE vfloat& store(vfloat& dst, const vfloat& src)
{
dst.m_value = blendv_mask_ps(dst.m_value, src.m_value, _mm_castsi128_ps(m_exec.m_mask));
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store(vfloat&& dst, const vfloat& src)
{
dst.m_value = blendv_mask_ps(dst.m_value, src.m_value, _mm_castsi128_ps(m_exec.m_mask));
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat& dst, const vfloat& src)
{
dst.m_value = src.m_value;
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat&& dst, const vfloat& src)
{
dst.m_value = src.m_value;
return dst;
}
// Linear ref to floats
struct float_lref
{
float* m_pValue;
private:
//float_lref& operator=(const float_lref&);
};
CPPSPMD_FORCE_INLINE const float_lref& store(const float_lref& dst, const vfloat& src)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm_storeu_ps(dst.m_pValue, src.m_value);
else
_mm_storeu_ps(dst.m_pValue, blendv_mask_ps(_mm_loadu_ps(dst.m_pValue), src.m_value, _mm_castsi128_ps(m_exec.m_mask)));
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store(const float_lref&& dst, const vfloat& src)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm_storeu_ps(dst.m_pValue, src.m_value);
else
_mm_storeu_ps(dst.m_pValue, blendv_mask_ps(_mm_loadu_ps(dst.m_pValue), src.m_value, _mm_castsi128_ps(m_exec.m_mask)));
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store_all(const float_lref& dst, const vfloat& src)
{
_mm_storeu_ps(dst.m_pValue, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store_all(const float_lref&& dst, const vfloat& src)
{
_mm_storeu_ps(dst.m_pValue, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE vfloat load(const float_lref& src)
{
return vfloat{ _mm_and_ps(_mm_loadu_ps(src.m_pValue), _mm_castsi128_ps(m_exec.m_mask)) };
}
// Varying ref to floats
struct float_vref
{
__m128i m_vindex;
float* m_pValue;
private:
//float_vref& operator=(const float_vref&);
};
// Varying ref to varying float
struct vfloat_vref
{
__m128i m_vindex;
vfloat* m_pValue;
private:
//vfloat_vref& operator=(const vfloat_vref&);
};
// Varying ref to varying int
struct vint_vref
{
__m128i m_vindex;
vint* m_pValue;
private:
//vint_vref& operator=(const vint_vref&);
};
CPPSPMD_FORCE_INLINE const float_vref& store(const float_vref& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store(const float_vref&& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store_all(const float_vref& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store_all(const float_vref&& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE vfloat load(const float_vref& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i *)vindex, src.m_vindex);
CPPSPMD_ALIGN(16) float loaded[4];
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
loaded[i] = src.m_pValue[vindex[i]];
}
return vfloat{ _mm_and_ps(_mm_castsi128_ps(m_exec.m_mask), _mm_load_ps((const float*)loaded)) };
}
CPPSPMD_FORCE_INLINE vfloat load_all(const float_vref& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i *)vindex, src.m_vindex);
CPPSPMD_ALIGN(16) float loaded[4];
for (int i = 0; i < 4; i++)
loaded[i] = src.m_pValue[vindex[i]];
return vfloat{ _mm_load_ps((const float*)loaded) };
}
// Linear ref to ints
struct int_lref
{
int* m_pValue;
private:
//int_lref& operator=(const int_lref&);
};
CPPSPMD_FORCE_INLINE const int_lref& store(const int_lref& dst, const vint& src)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
{
_mm_storeu_si128((__m128i *)dst.m_pValue, src.m_value);
}
else
{
CPPSPMD_ALIGN(16) int stored[4];
_mm_store_si128((__m128i *)stored, src.m_value);
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
dst.m_pValue[i] = stored[i];
}
}
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int_lref& src)
{
__m128i v = _mm_loadu_si128((const __m128i*)src.m_pValue);
v = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(v), _mm_castsi128_ps(m_exec.m_mask)));
return vint{ v };
}
// Linear ref to int16's
struct int16_lref
{
int16_t* m_pValue;
private:
//int16_lref& operator=(const int16_lref&);
};
CPPSPMD_FORCE_INLINE const int16_lref& store(const int16_lref& dst, const vint& src)
{
CPPSPMD_ALIGN(16) int stored[4];
_mm_store_si128((__m128i *)stored, src.m_value);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
dst.m_pValue[i] = static_cast<int16_t>(stored[i]);
}
return dst;
}
CPPSPMD_FORCE_INLINE const int16_lref& store_all(const int16_lref& dst, const vint& src)
{
CPPSPMD_ALIGN(16) int stored[4];
_mm_store_si128((__m128i *)stored, src.m_value);
for (int i = 0; i < 4; i++)
dst.m_pValue[i] = static_cast<int16_t>(stored[i]);
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int16_lref& src)
{
CPPSPMD_ALIGN(16) int values[4];
for (int i = 0; i < 4; i++)
values[i] = static_cast<int16_t>(src.m_pValue[i]);
__m128i t = _mm_load_si128( (const __m128i *)values );
return vint{ _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps( t ), _mm_castsi128_ps(m_exec.m_mask))) };
}
CPPSPMD_FORCE_INLINE vint load_all(const int16_lref& src)
{
CPPSPMD_ALIGN(16) int values[4];
for (int i = 0; i < 4; i++)
values[i] = static_cast<int16_t>(src.m_pValue[i]);
__m128i t = _mm_load_si128( (const __m128i *)values );
return vint{ t };
}
// Linear ref to constant ints
struct cint_lref
{
const int* m_pValue;
private:
//cint_lref& operator=(const cint_lref&);
};
CPPSPMD_FORCE_INLINE vint load(const cint_lref& src)
{
__m128i v = _mm_loadu_si128((const __m128i *)src.m_pValue);
v = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(v), _mm_castsi128_ps(m_exec.m_mask)));
return vint{ v };
}
CPPSPMD_FORCE_INLINE vint load_all(const cint_lref& src)
{
return vint{ _mm_loadu_si128((const __m128i *)src.m_pValue) };
}
// Varying ref to ints
struct int_vref
{
__m128i m_vindex;
int* m_pValue;
private:
//int_vref& operator=(const int_vref&);
};
// Varying ref to constant ints
struct cint_vref
{
__m128i m_vindex;
const int* m_pValue;
private:
//cint_vref& operator=(const cint_vref&);
};
// Varying int
struct vint
{
__m128i m_value;
vint() = default;
CPPSPMD_FORCE_INLINE explicit vint(const __m128i& value) : m_value(value) { }
CPPSPMD_FORCE_INLINE explicit vint(const lint &other) : m_value(other.m_value) { }
CPPSPMD_FORCE_INLINE vint& operator=(const lint& other) { m_value = other.m_value; return *this; }
CPPSPMD_FORCE_INLINE vint(int value) : m_value(_mm_set1_epi32(value)) { }
CPPSPMD_FORCE_INLINE explicit vint(float value) : m_value(_mm_set1_epi32((int)value)) { }
CPPSPMD_FORCE_INLINE explicit vint(const vfloat& other) : m_value(_mm_cvttps_epi32(other.m_value)) { }
CPPSPMD_FORCE_INLINE explicit operator vbool() const
{
return vbool{ _mm_xor_si128( _mm_load_si128((const __m128i*)g_allones_128), _mm_cmpeq_epi32(m_value, _mm_setzero_si128())) };
}
CPPSPMD_FORCE_INLINE explicit operator vfloat() const
{
return vfloat{ _mm_cvtepi32_ps(m_value) };
}
CPPSPMD_FORCE_INLINE int_vref operator[](int* ptr) const
{
return int_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE cint_vref operator[](const int* ptr) const
{
return cint_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE float_vref operator[](float* ptr) const
{
return float_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE vfloat_vref operator[](vfloat* ptr) const
{
return vfloat_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE vint_vref operator[](vint* ptr) const
{
return vint_vref{ m_value, ptr };
}
private:
//vint& operator=(const vint&);
};
// Load/store linear int
CPPSPMD_FORCE_INLINE void storeu_linear(int *pDst, const vint& src)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm_storeu_si128((__m128i *)pDst, src.m_value);
else
{
if (mask & 1) pDst[0] = extract_x(src.m_value);
if (mask & 2) pDst[1] = extract_y(src.m_value);
if (mask & 4) pDst[2] = extract_z(src.m_value);
if (mask & 8) pDst[3] = extract_w(src.m_value);
}
}
CPPSPMD_FORCE_INLINE void storeu_linear_all(int *pDst, const vint& src)
{
_mm_storeu_si128((__m128i*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE void store_linear_all(int *pDst, const vint& src)
{
_mm_store_si128((__m128i*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE vint loadu_linear(const int *pSrc)
{
__m128i v = _mm_loadu_si128((const __m128i*)pSrc);
v = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(v), _mm_castsi128_ps(m_exec.m_mask)));
return vint{ v };
}
CPPSPMD_FORCE_INLINE vint loadu_linear_all(const int *pSrc)
{
return vint{ _mm_loadu_si128((__m128i*)pSrc) };
}
CPPSPMD_FORCE_INLINE vint load_linear_all(const int *pSrc)
{
return vint{ _mm_load_si128((__m128i*)pSrc) };
}
// Load/store linear float
CPPSPMD_FORCE_INLINE void storeu_linear(float *pDst, const vfloat& src)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm_storeu_ps((float*)pDst, src.m_value);
else
{
int *pDstI = (int *)pDst;
if (mask & 1) pDstI[0] = extract_ps_x(src.m_value);
if (mask & 2) pDstI[1] = extract_ps_y(src.m_value);
if (mask & 4) pDstI[2] = extract_ps_z(src.m_value);
if (mask & 8) pDstI[3] = extract_ps_w(src.m_value);
}
}
CPPSPMD_FORCE_INLINE void storeu_linear_all(float *pDst, const vfloat& src)
{
_mm_storeu_ps((float*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE void store_linear_all(float *pDst, const vfloat& src)
{
_mm_store_ps((float*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE vfloat loadu_linear(const float *pSrc)
{
__m128 v = _mm_loadu_ps((const float*)pSrc);
v = _mm_and_ps(v, _mm_castsi128_ps(m_exec.m_mask));
return vfloat{ v };
}
CPPSPMD_FORCE_INLINE vfloat loadu_linear_all(const float *pSrc)
{
return vfloat{ _mm_loadu_ps((float*)pSrc) };
}
CPPSPMD_FORCE_INLINE vfloat load_linear_all(const float *pSrc)
{
return vfloat{ _mm_load_ps((float*)pSrc) };
}
CPPSPMD_FORCE_INLINE vint& store(vint& dst, const vint& src)
{
dst.m_value = blendv_mask_epi32(dst.m_value, src.m_value, m_exec.m_mask);
return dst;
}
CPPSPMD_FORCE_INLINE const int_vref& store(const int_vref& dst, const vint& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) int stored[4];
_mm_store_si128((__m128i*)stored, src.m_value);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
dst.m_pValue[vindex[i]] = stored[i];
}
return dst;
}
CPPSPMD_FORCE_INLINE vint& store_all(vint& dst, const vint& src)
{
dst.m_value = src.m_value;
return dst;
}
CPPSPMD_FORCE_INLINE const int_vref& store_all(const int_vref& dst, const vint& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) int stored[4];
_mm_store_si128((__m128i*)stored, src.m_value);
for (int i = 0; i < 4; i++)
dst.m_pValue[vindex[i]] = stored[i];
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int_vref& src)
{
CPPSPMD_ALIGN(16) int values[4];
CPPSPMD_ALIGN(16) int indices[4];
_mm_store_si128((__m128i *)indices, src.m_vindex);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
values[i] = src.m_pValue[indices[i]];
}
return vint{ _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(m_exec.m_mask), _mm_load_ps((const float*)values))) };
}
CPPSPMD_FORCE_INLINE vint load_all(const int_vref& src)
{
CPPSPMD_ALIGN(16) int values[4];
CPPSPMD_ALIGN(16) int indices[4];
_mm_store_si128((__m128i *)indices, src.m_vindex);
for (int i = 0; i < 4; i++)
values[i] = src.m_pValue[indices[i]];
return vint{ _mm_castps_si128( _mm_load_ps((const float*)values)) };
}
CPPSPMD_FORCE_INLINE vint load(const cint_vref& src)
{
CPPSPMD_ALIGN(16) int values[4];
CPPSPMD_ALIGN(16) int indices[4];
_mm_store_si128((__m128i *)indices, src.m_vindex);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
values[i] = src.m_pValue[indices[i]];
}
return vint{ _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(m_exec.m_mask), _mm_load_ps((const float*)values))) };
}
CPPSPMD_FORCE_INLINE vint load_all(const cint_vref& src)
{
CPPSPMD_ALIGN(16) int values[4];
CPPSPMD_ALIGN(16) int indices[4];
_mm_store_si128((__m128i *)indices, src.m_vindex);
for (int i = 0; i < 4; i++)
values[i] = src.m_pValue[indices[i]];
return vint{ _mm_castps_si128( _mm_load_ps((const float*)values)) };
}
CPPSPMD_FORCE_INLINE vint load_bytes_all(const cint_vref& src)
{
__m128i v0_l;
const uint8_t* pSrc = (const uint8_t*)src.m_pValue;
v0_l = insert_x(_mm_undefined_si128(), ((int*)(pSrc + extract_x(src.m_vindex)))[0]);
v0_l = insert_y(v0_l, ((int*)(pSrc + extract_y(src.m_vindex)))[0]);
v0_l = insert_z(v0_l, ((int*)(pSrc + extract_z(src.m_vindex)))[0]);
v0_l = insert_w(v0_l, ((int*)(pSrc + extract_w(src.m_vindex)))[0]);
return vint{ v0_l };
}
CPPSPMD_FORCE_INLINE vint load_words_all(const cint_vref& src)
{
__m128i v0_l;
const uint8_t* pSrc = (const uint8_t*)src.m_pValue;
v0_l = insert_x(_mm_undefined_si128(), ((int16_t*)(pSrc + 2 * extract_x(src.m_vindex)))[0]);
v0_l = insert_y(v0_l, ((int16_t*)(pSrc + 2 * extract_y(src.m_vindex)))[0]);
v0_l = insert_z(v0_l, ((int16_t*)(pSrc + 2 * extract_z(src.m_vindex)))[0]);
v0_l = insert_w(v0_l, ((int16_t*)(pSrc + 2 * extract_w(src.m_vindex)))[0]);
return vint{ v0_l };
}
CPPSPMD_FORCE_INLINE void store_strided(int *pDst, uint32_t stride, const vint &v)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask & 1) pDst[0] = extract_x(v.m_value);
if (mask & 2) pDst[stride] = extract_y(v.m_value);
if (mask & 4) pDst[stride*2] = extract_z(v.m_value);
if (mask & 8) pDst[stride*3] = extract_w(v.m_value);
}
CPPSPMD_FORCE_INLINE void store_strided(float *pDstF, uint32_t stride, const vfloat &v)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask & 1) ((int *)pDstF)[0] = extract_ps_x(v.m_value);
if (mask & 2) ((int *)pDstF)[stride] = extract_ps_y(v.m_value);
if (mask & 4) ((int *)pDstF)[stride*2] = extract_ps_z(v.m_value);
if (mask & 8) ((int *)pDstF)[stride*3] = extract_ps_w(v.m_value);
}
CPPSPMD_FORCE_INLINE void store_all_strided(int *pDst, uint32_t stride, const vint &v)
{
pDst[0] = extract_x(v.m_value);
pDst[stride] = extract_y(v.m_value);
pDst[stride*2] = extract_z(v.m_value);
pDst[stride*3] = extract_w(v.m_value);
}
CPPSPMD_FORCE_INLINE void store_all_strided(float *pDstF, uint32_t stride, const vfloat &v)
{
((int *)pDstF)[0] = extract_ps_x(v.m_value);
((int *)pDstF)[stride] = extract_ps_y(v.m_value);
((int *)pDstF)[stride*2] = extract_ps_z(v.m_value);
((int *)pDstF)[stride*3] = extract_ps_w(v.m_value);
}
CPPSPMD_FORCE_INLINE vint load_strided(const int *pSrc, uint32_t stride)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
#if CPPSPMD_SSE2
CPPSPMD_ALIGN(16) int vals[4] = { 0, 0, 0, 0 };
if (mask & 1) vals[0] = pSrc[0];
if (mask & 2) vals[1] = pSrc[stride];
if (mask & 4) vals[2] = pSrc[stride * 2];
if (mask & 8) vals[3] = pSrc[stride * 3];
return vint{ _mm_load_si128((__m128i*)vals) };
#else
const float* pSrcF = (const float*)pSrc;
__m128 v = _mm_setzero_ps();
if (mask & 1) v = _mm_load_ss(pSrcF);
if (mask & 2) v = _mm_insert_ps(v, _mm_load_ss(pSrcF + stride), 0x10);
if (mask & 4) v = _mm_insert_ps(v, _mm_load_ss(pSrcF + 2 * stride), 0x20);
if (mask & 8) v = _mm_insert_ps(v, _mm_load_ss(pSrcF + 3 * stride), 0x30);
return vint{ _mm_castps_si128(v) };
#endif
}
CPPSPMD_FORCE_INLINE vfloat load_strided(const float *pSrc, uint32_t stride)
{
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
#if CPPSPMD_SSE2
CPPSPMD_ALIGN(16) float vals[4] = { 0, 0, 0, 0 };
if (mask & 1) vals[0] = pSrc[0];
if (mask & 2) vals[1] = pSrc[stride];
if (mask & 4) vals[2] = pSrc[stride * 2];
if (mask & 8) vals[3] = pSrc[stride * 3];
return vfloat{ _mm_load_ps(vals) };
#else
__m128 v = _mm_setzero_ps();
if (mask & 1) v = _mm_load_ss(pSrc);
if (mask & 2) v = _mm_insert_ps(v, _mm_load_ss(pSrc + stride), 0x10);
if (mask & 4) v = _mm_insert_ps(v, _mm_load_ss(pSrc + 2 * stride), 0x20);
if (mask & 8) v = _mm_insert_ps(v, _mm_load_ss(pSrc + 3 * stride), 0x30);
return vfloat{ v };
#endif
}
CPPSPMD_FORCE_INLINE vint load_all_strided(const int *pSrc, uint32_t stride)
{
#if CPPSPMD_SSE2
CPPSPMD_ALIGN(16) int vals[4];
vals[0] = pSrc[0];
vals[1] = pSrc[stride];
vals[2] = pSrc[stride * 2];
vals[3] = pSrc[stride * 3];
return vint{ _mm_load_si128((__m128i*)vals) };
#else
const float* pSrcF = (const float*)pSrc;
__m128 v = _mm_load_ss(pSrcF);
v = _mm_insert_ps(v, _mm_load_ss(pSrcF + stride), 0x10);
v = _mm_insert_ps(v, _mm_load_ss(pSrcF + 2 * stride), 0x20);
v = _mm_insert_ps(v, _mm_load_ss(pSrcF + 3 * stride), 0x30);
return vint{ _mm_castps_si128(v) };
#endif
}
CPPSPMD_FORCE_INLINE vfloat load_all_strided(const float *pSrc, uint32_t stride)
{
#if CPPSPMD_SSE2
CPPSPMD_ALIGN(16) float vals[4];
vals[0] = pSrc[0];
vals[1] = pSrc[stride];
vals[2] = pSrc[stride * 2];
vals[3] = pSrc[stride * 3];
return vfloat{ _mm_load_ps(vals) };
#else
__m128 v = _mm_load_ss(pSrc);
v = _mm_insert_ps(v, _mm_load_ss(pSrc + stride), 0x10);
v = _mm_insert_ps(v, _mm_load_ss(pSrc + 2 * stride), 0x20);
v = _mm_insert_ps(v, _mm_load_ss(pSrc + 3 * stride), 0x30);
return vfloat{ v };
#endif
}
CPPSPMD_FORCE_INLINE const vfloat_vref& store(const vfloat_vref& dst, const vfloat& src)
{
// TODO: There's surely a better way
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask & 1) ((int *)(&dst.m_pValue[extract_x(dst.m_vindex)]))[0] = extract_x(_mm_castps_si128(src.m_value));
if (mask & 2) ((int *)(&dst.m_pValue[extract_y(dst.m_vindex)]))[1] = extract_y(_mm_castps_si128(src.m_value));
if (mask & 4) ((int *)(&dst.m_pValue[extract_z(dst.m_vindex)]))[2] = extract_z(_mm_castps_si128(src.m_value));
if (mask & 8) ((int *)(&dst.m_pValue[extract_w(dst.m_vindex)]))[3] = extract_w(_mm_castps_si128(src.m_value));
return dst;
}
CPPSPMD_FORCE_INLINE vfloat load(const vfloat_vref& src)
{
// TODO: There's surely a better way
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
__m128i k = _mm_setzero_si128();
if (mask & 1) k = insert_x(k, ((int *)(&src.m_pValue[extract_x(src.m_vindex)]))[0]);
if (mask & 2) k = insert_y(k, ((int *)(&src.m_pValue[extract_y(src.m_vindex)]))[1]);
if (mask & 4) k = insert_z(k, ((int *)(&src.m_pValue[extract_z(src.m_vindex)]))[2]);
if (mask & 8) k = insert_w(k, ((int *)(&src.m_pValue[extract_w(src.m_vindex)]))[3]);
return vfloat{ _mm_castsi128_ps(k) };
}
CPPSPMD_FORCE_INLINE const vint_vref& store(const vint_vref& dst, const vint& src)
{
// TODO: There's surely a better way
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
if (mask & 1) ((int *)(&dst.m_pValue[extract_x(dst.m_vindex)]))[0] = extract_x(src.m_value);
if (mask & 2) ((int *)(&dst.m_pValue[extract_y(dst.m_vindex)]))[1] = extract_y(src.m_value);
if (mask & 4) ((int *)(&dst.m_pValue[extract_z(dst.m_vindex)]))[2] = extract_z(src.m_value);
if (mask & 8) ((int *)(&dst.m_pValue[extract_w(dst.m_vindex)]))[3] = extract_w(src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const vint_vref& src)
{
// TODO: There's surely a better way
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
__m128i k = _mm_setzero_si128();
if (mask & 1) k = insert_x(k, ((int *)(&src.m_pValue[extract_x(src.m_vindex)]))[0]);
if (mask & 2) k = insert_y(k, ((int *)(&src.m_pValue[extract_y(src.m_vindex)]))[1]);
if (mask & 4) k = insert_z(k, ((int *)(&src.m_pValue[extract_z(src.m_vindex)]))[2]);
if (mask & 8) k = insert_w(k, ((int *)(&src.m_pValue[extract_w(src.m_vindex)]))[3]);
return vint{ k };
}
CPPSPMD_FORCE_INLINE vint load_all(const vint_vref& src)
{
// TODO: There's surely a better way
__m128i k = _mm_setzero_si128();
k = insert_x(k, ((int*)(&src.m_pValue[extract_x(src.m_vindex)]))[0]);
k = insert_y(k, ((int*)(&src.m_pValue[extract_y(src.m_vindex)]))[1]);
k = insert_z(k, ((int*)(&src.m_pValue[extract_z(src.m_vindex)]))[2]);
k = insert_w(k, ((int*)(&src.m_pValue[extract_w(src.m_vindex)]))[3]);
return vint{ k };
}
// Linear integer
struct lint
{
__m128i m_value;
CPPSPMD_FORCE_INLINE explicit lint(__m128i value)
: m_value(value)
{ }
CPPSPMD_FORCE_INLINE explicit operator vfloat() const
{
return vfloat{ _mm_cvtepi32_ps(m_value) };
}
CPPSPMD_FORCE_INLINE explicit operator vint() const
{
return vint{ m_value };
}
CPPSPMD_FORCE_INLINE int get_first_value() const
{
return _mm_cvtsi128_si32(m_value);
}
CPPSPMD_FORCE_INLINE float_lref operator[](float* ptr) const
{
return float_lref{ ptr + get_first_value() };
}
CPPSPMD_FORCE_INLINE int_lref operator[](int* ptr) const
{
return int_lref{ ptr + get_first_value() };
}
CPPSPMD_FORCE_INLINE int16_lref operator[](int16_t* ptr) const
{
return int16_lref{ ptr + get_first_value() };
}
CPPSPMD_FORCE_INLINE cint_lref operator[](const int* ptr) const
{
return cint_lref{ ptr + get_first_value() };
}
private:
//lint& operator=(const lint&);
};
CPPSPMD_FORCE_INLINE lint& store_all(lint& dst, const lint& src)
{
dst.m_value = src.m_value;
return dst;
}
const lint program_index = lint{ _mm_set_epi32( 3, 2, 1, 0 ) };
// SPMD condition helpers
template<typename IfBody>
CPPSPMD_FORCE_INLINE void spmd_if(const vbool& cond, const IfBody& ifBody);
CPPSPMD_FORCE_INLINE void spmd_if_break(const vbool& cond);
// No breaks, continues, etc. allowed
template<typename IfBody>
CPPSPMD_FORCE_INLINE void spmd_sif(const vbool& cond, const IfBody& ifBody);
// No breaks, continues, etc. allowed
template<typename IfBody, typename ElseBody>
CPPSPMD_FORCE_INLINE void spmd_sifelse(const vbool& cond, const IfBody& ifBody, const ElseBody &elseBody);
template<typename IfBody, typename ElseBody>
CPPSPMD_FORCE_INLINE void spmd_ifelse(const vbool& cond, const IfBody& ifBody, const ElseBody& elseBody);
template<typename WhileCondBody, typename WhileBody>
CPPSPMD_FORCE_INLINE void spmd_while(const WhileCondBody& whileCondBody, const WhileBody& whileBody);
template<typename ForInitBody, typename ForCondBody, typename ForIncrBody, typename ForBody>
CPPSPMD_FORCE_INLINE void spmd_for(const ForInitBody& forInitBody, const ForCondBody& forCondBody, const ForIncrBody& forIncrBody, const ForBody& forBody);
template<typename ForeachBody>
CPPSPMD_FORCE_INLINE void spmd_foreach(int begin, int end, const ForeachBody& foreachBody);
#ifdef _DEBUG
CPPSPMD_FORCE_INLINE void check_masks();
#else
CPPSPMD_FORCE_INLINE void check_masks() { }
#endif
CPPSPMD_FORCE_INLINE void spmd_break();
CPPSPMD_FORCE_INLINE void spmd_continue();
CPPSPMD_FORCE_INLINE void spmd_return();
template<typename UnmaskedBody>
CPPSPMD_FORCE_INLINE void spmd_unmasked(const UnmaskedBody& unmaskedBody);
template<typename SPMDKernel, typename... Args>
//CPPSPMD_FORCE_INLINE decltype(auto) spmd_call(Args&&... args);
CPPSPMD_FORCE_INLINE void spmd_call(Args&&... args);
CPPSPMD_FORCE_INLINE void swap(vint &a, vint &b) { vint temp = a; store(a, b); store(b, temp); }
CPPSPMD_FORCE_INLINE void swap(vfloat &a, vfloat &b) { vfloat temp = a; store(a, b); store(b, temp); }
CPPSPMD_FORCE_INLINE void swap(vbool &a, vbool &b) { vbool temp = a; store(a, b); store(b, temp); }
CPPSPMD_FORCE_INLINE float reduce_add(vfloat v)
{
__m128 k3210 = _mm_castsi128_ps(blendv_mask_epi32(_mm_setzero_si128(), _mm_castps_si128(v.m_value), m_exec.m_mask));
__m128 temp = _mm_add_ps(_mm_shuffle_ps(k3210, k3210, _MM_SHUFFLE(0, 1, 2, 3)), k3210);
return _mm_cvtss_f32(_mm_add_ss(_mm_movehl_ps(temp, temp), temp));
}
CPPSPMD_FORCE_INLINE int reduce_add(vint v)
{
__m128i k3210 = blendv_mask_epi32(_mm_setzero_si128(), v.m_value, m_exec.m_mask);
__m128i temp = _mm_add_epi32(_mm_shuffle_epi32(k3210, _MM_SHUFFLE(0, 1, 2, 3)), k3210);
return extract_x(_mm_add_epi32(_mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(temp), _mm_castsi128_ps(temp))), temp));
}
#include "cppspmd_math_declares.h"
}; // struct spmd_kernel
using exec_mask = spmd_kernel::exec_mask;
using vint = spmd_kernel::vint;
using int_lref = spmd_kernel::int_lref;
using cint_vref = spmd_kernel::cint_vref;
using cint_lref = spmd_kernel::cint_lref;
using int_vref = spmd_kernel::int_vref;
using lint = spmd_kernel::lint;
using vbool = spmd_kernel::vbool;
using vfloat = spmd_kernel::vfloat;
using float_lref = spmd_kernel::float_lref;
using float_vref = spmd_kernel::float_vref;
using vfloat_vref = spmd_kernel::vfloat_vref;
using vint_vref = spmd_kernel::vint_vref;
CPPSPMD_FORCE_INLINE spmd_kernel::vbool::operator vfloat() const
{
return vfloat { _mm_and_ps( _mm_castsi128_ps(m_value), *(const __m128 *)g_onef_128 ) };
}
// Returns UINT32_MAX's for true, 0 for false. (Should it return 1's?)
CPPSPMD_FORCE_INLINE spmd_kernel::vbool::operator vint() const
{
return vint { m_value };
}
CPPSPMD_FORCE_INLINE vbool operator!(const vbool& v)
{
return vbool{ _mm_castps_si128(_mm_xor_ps(_mm_load_ps((const float*)g_allones_128), _mm_castsi128_ps(v.m_value))) };
}
CPPSPMD_FORCE_INLINE exec_mask::exec_mask(const vbool& b) { m_mask = b.m_value; }
CPPSPMD_FORCE_INLINE exec_mask operator^(const exec_mask& a, const exec_mask& b) { return exec_mask{ _mm_xor_si128(a.m_mask, b.m_mask) }; }
CPPSPMD_FORCE_INLINE exec_mask operator&(const exec_mask& a, const exec_mask& b) { return exec_mask{ _mm_and_si128(a.m_mask, b.m_mask) }; }
CPPSPMD_FORCE_INLINE exec_mask operator|(const exec_mask& a, const exec_mask& b) { return exec_mask{ _mm_or_si128(a.m_mask, b.m_mask) }; }
CPPSPMD_FORCE_INLINE bool all(const exec_mask& e) { return _mm_movemask_ps(_mm_castsi128_ps(e.m_mask)) == ALL_ON_MOVEMASK; }
CPPSPMD_FORCE_INLINE bool any(const exec_mask& e) { return _mm_movemask_ps(_mm_castsi128_ps(e.m_mask)) != 0; }
// Bad pattern - doesn't factor in the current exec mask. Prefer spmd_any() instead.
CPPSPMD_FORCE_INLINE bool all(const vbool& e) { return _mm_movemask_ps(_mm_castsi128_ps(e.m_value)) == ALL_ON_MOVEMASK; }
CPPSPMD_FORCE_INLINE bool any(const vbool& e) { return _mm_movemask_ps(_mm_castsi128_ps(e.m_value)) != 0; }
CPPSPMD_FORCE_INLINE exec_mask andnot(const exec_mask& a, const exec_mask& b) { return exec_mask{ _mm_andnot_si128(a.m_mask, b.m_mask) }; }
CPPSPMD_FORCE_INLINE vbool operator||(const vbool& a, const vbool& b) { return vbool{ _mm_or_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator&&(const vbool& a, const vbool& b) { return vbool{ _mm_and_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat operator+(const vfloat& a, const vfloat& b) { return vfloat{ _mm_add_ps(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat operator-(const vfloat& a, const vfloat& b) { return vfloat{ _mm_sub_ps(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat operator+(float a, const vfloat& b) { return vfloat(a) + b; }
CPPSPMD_FORCE_INLINE vfloat operator+(const vfloat& a, float b) { return a + vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator-(const vfloat& a, const vint& b) { return a - vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator-(const vint& a, const vfloat& b) { return vfloat(a) - b; }
CPPSPMD_FORCE_INLINE vfloat operator-(const vfloat& a, int b) { return a - vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator-(int a, const vfloat& b) { return vfloat(a) - b; }
CPPSPMD_FORCE_INLINE vfloat operator-(const vfloat& a, float b) { return a - vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator-(float a, const vfloat& b) { return vfloat(a) - b; }
CPPSPMD_FORCE_INLINE vfloat operator*(const vfloat& a, const vfloat& b) { return vfloat{ _mm_mul_ps(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat operator*(const vfloat& a, float b) { return a * vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator*(float a, const vfloat& b) { return vfloat(a) * b; }
CPPSPMD_FORCE_INLINE vfloat operator*(const vfloat& a, int b) { return a * vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator*(int a, const vfloat& b) { return vfloat(a) * b; }
CPPSPMD_FORCE_INLINE vfloat operator/(const vfloat& a, const vfloat& b) { return vfloat{ _mm_div_ps(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat operator/(const vfloat& a, int b) { return a / vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator/(int a, const vfloat& b) { return vfloat(a) / b; }
CPPSPMD_FORCE_INLINE vfloat operator/(const vfloat& a, float b) { return a / vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator/(float a, const vfloat& b) { return vfloat(a) / b; }
CPPSPMD_FORCE_INLINE vfloat operator-(const vfloat& v) { return vfloat{ _mm_sub_ps(_mm_xor_ps(v.m_value, v.m_value), v.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator==(const vfloat& a, const vfloat& b) { return vbool{ _mm_castps_si128(_mm_cmpeq_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator==(const vfloat& a, float b) { return a == vfloat(b); }
CPPSPMD_FORCE_INLINE vbool operator!=(const vfloat& a, const vfloat& b) { return !vbool{ _mm_castps_si128(_mm_cmpeq_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator!=(const vfloat& a, float b) { return a != vfloat(b); }
CPPSPMD_FORCE_INLINE vbool operator<(const vfloat& a, const vfloat& b) { return vbool{ _mm_castps_si128(_mm_cmplt_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator<(const vfloat& a, float b) { return a < vfloat(b); }
CPPSPMD_FORCE_INLINE vbool operator>(const vfloat& a, const vfloat& b) { return vbool{ _mm_castps_si128(_mm_cmpgt_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator>(const vfloat& a, float b) { return a > vfloat(b); }
CPPSPMD_FORCE_INLINE vbool operator<=(const vfloat& a, const vfloat& b) { return vbool{ _mm_castps_si128(_mm_cmple_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator<=(const vfloat& a, float b) { return a <= vfloat(b); }
CPPSPMD_FORCE_INLINE vbool operator>=(const vfloat& a, const vfloat& b) { return vbool{ _mm_castps_si128(_mm_cmpge_ps(a.m_value, b.m_value)) }; }
CPPSPMD_FORCE_INLINE vbool operator>=(const vfloat& a, float b) { return a >= vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat spmd_ternaryf(const vbool& cond, const vfloat& a, const vfloat& b) { return vfloat{ blendv_mask_ps(b.m_value, a.m_value, _mm_castsi128_ps(cond.m_value)) }; }
CPPSPMD_FORCE_INLINE vint spmd_ternaryi(const vbool& cond, const vint& a, const vint& b) { return vint{ blendv_mask_epi32(b.m_value, a.m_value, cond.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat sqrt(const vfloat& v) { return vfloat{ _mm_sqrt_ps(v.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat abs(const vfloat& v) { return vfloat{ _mm_andnot_ps(_mm_set1_ps(-0.0f), v.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat max(const vfloat& a, const vfloat& b) { return vfloat{ _mm_max_ps(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat min(const vfloat& a, const vfloat& b) { return vfloat{ _mm_min_ps(a.m_value, b.m_value) }; }
#if CPPSPMD_SSE2
CPPSPMD_FORCE_INLINE vfloat round_truncate(const vfloat& a)
{
__m128i abs_a = _mm_and_si128(_mm_castps_si128(a.m_value), _mm_set1_epi32(0x7FFFFFFFU) );
__m128i has_fractional = _mm_cmplt_epi32(abs_a, _mm_castps_si128(_mm_set1_ps(8388608.0f)));
__m128i ai = _mm_cvttps_epi32(a.m_value);
__m128 af = _mm_cvtepi32_ps(ai);
return vfloat{ blendv_mask_ps(a.m_value, af, _mm_castsi128_ps(has_fractional)) };
}
CPPSPMD_FORCE_INLINE vfloat floor(const vfloat& a)
{
__m128i abs_a = _mm_and_si128(_mm_castps_si128(a.m_value), _mm_set1_epi32(0x7FFFFFFFU));
__m128i has_fractional = _mm_cmplt_epi32(abs_a, _mm_castps_si128(_mm_set1_ps(8388608.0f)));
__m128i ai = _mm_cvtps_epi32(a.m_value);
__m128 af = _mm_cvtepi32_ps(ai);
__m128 changed = _mm_cvtepi32_ps(_mm_castps_si128(_mm_cmpgt_ps(af, a.m_value)));
af = _mm_add_ps(af, changed);
return vfloat{ blendv_mask_ps(a.m_value, af, _mm_castsi128_ps(has_fractional)) };
}
CPPSPMD_FORCE_INLINE vfloat ceil(const vfloat& a)
{
__m128i abs_a = _mm_and_si128(_mm_castps_si128(a.m_value), _mm_set1_epi32(0x7FFFFFFFU));
__m128i has_fractional = _mm_cmplt_epi32(abs_a, _mm_castps_si128(_mm_set1_ps(8388608.0f)));
__m128i ai = _mm_cvtps_epi32(a.m_value);
__m128 af = _mm_cvtepi32_ps(ai);
__m128 changed = _mm_cvtepi32_ps(_mm_castps_si128(_mm_cmplt_ps(af, a.m_value)));
af = _mm_sub_ps(af, changed);
return vfloat{ blendv_mask_ps(a.m_value, af, _mm_castsi128_ps(has_fractional)) };
}
// We need to disable unsafe math optimizations for the key operations used for rounding to nearest.
// I wish there was a better way.
#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
inline __m128 add_sub(__m128 a, __m128 b) __attribute__((optimize("-fno-unsafe-math-optimizations")))
#elif defined(__clang__)
inline __m128 add_sub(__m128 a, __m128 b) __attribute__((optnone))
#elif defined (_MSC_VER)
#pragma float_control(push)
#pragma float_control(precise, on)
inline __m128 add_sub(__m128 a, __m128 b)
#else
inline __m128 add_sub(__m128 a, __m128 b)
#endif
{
return _mm_sub_ps(_mm_add_ps(a, b), b);
}
#if defined (_MSC_VER)
#pragma float_control(pop)
#endif
CPPSPMD_FORCE_INLINE vfloat round_nearest(const vfloat& a)
{
__m128i no_fract_fp_bits = _mm_castps_si128(_mm_set1_ps(8388608.0f));
__m128i sign_a = _mm_and_si128(_mm_castps_si128(a.m_value), _mm_set1_epi32(0x80000000U));
__m128 force_int = _mm_castsi128_ps(_mm_or_si128(no_fract_fp_bits, sign_a));
// Can't use individual _mm_add_ps/_mm_sub_ps - this will be optimized out with /fp:fast by clang and probably other compilers.
//__m128 temp1 = _mm_add_ps(a.m_value, force_int);
//__m128 temp2 = _mm_sub_ps(temp1, force_int);
__m128 temp2 = add_sub(a.m_value, force_int);
__m128i abs_a = _mm_and_si128(_mm_castps_si128(a.m_value), _mm_set1_epi32(0x7FFFFFFFU));
__m128i has_fractional = _mm_cmplt_epi32(abs_a, no_fract_fp_bits);
return vfloat{ blendv_mask_ps(a.m_value, temp2, _mm_castsi128_ps(has_fractional)) };
}
#else
CPPSPMD_FORCE_INLINE vfloat floor(const vfloat& v) { return vfloat{ _mm_floor_ps(v.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat ceil(const vfloat& a) { return vfloat{ _mm_ceil_ps(a.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat round_nearest(const vfloat &a) { return vfloat{ _mm_round_ps(a.m_value, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC ) }; }
CPPSPMD_FORCE_INLINE vfloat round_truncate(const vfloat &a) { return vfloat{ _mm_round_ps(a.m_value, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC ) }; }
#endif
CPPSPMD_FORCE_INLINE vfloat frac(const vfloat& a) { return a - floor(a); }
CPPSPMD_FORCE_INLINE vfloat fmod(vfloat a, vfloat b) { vfloat c = frac(abs(a / b)) * abs(b); return spmd_ternaryf(a < 0, -c, c); }
CPPSPMD_FORCE_INLINE vfloat sign(const vfloat& a) { return spmd_ternaryf(a < 0.0f, 1.0f, 1.0f); }
CPPSPMD_FORCE_INLINE vint max(const vint& a, const vint& b) { return vint{ max_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint min(const vint& a, const vint& b) { return vint{ min_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint maxu(const vint& a, const vint& b) { return vint{ max_epu32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint minu(const vint& a, const vint& b) { return vint{ min_epu32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint abs(const vint& v) { return vint{ abs_epi32(v.m_value) }; }
CPPSPMD_FORCE_INLINE vint byteswap(const vint& v) { return vint{ shuffle_epi8(v.m_value, _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3)) }; }
CPPSPMD_FORCE_INLINE vint cast_vfloat_to_vint(const vfloat& v) { return vint{ _mm_castps_si128(v.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat cast_vint_to_vfloat(const vint& v) { return vfloat{ _mm_castsi128_ps(v.m_value) }; }
CPPSPMD_FORCE_INLINE vfloat clamp(const vfloat& v, const vfloat& a, const vfloat& b)
{
return vfloat{ _mm_min_ps(b.m_value, _mm_max_ps(v.m_value, a.m_value) ) };
}
CPPSPMD_FORCE_INLINE vint clamp(const vint& v, const vint& a, const vint& b)
{
return vint{ min_epi32(b.m_value, max_epi32(v.m_value, a.m_value) ) };
}
CPPSPMD_FORCE_INLINE vfloat vfma(const vfloat& a, const vfloat& b, const vfloat& c)
{
return vfloat{ _mm_add_ps(_mm_mul_ps(a.m_value, b.m_value), c.m_value) };
}
CPPSPMD_FORCE_INLINE vfloat vfms(const vfloat& a, const vfloat& b, const vfloat& c)
{
return vfloat{ _mm_sub_ps(_mm_mul_ps(a.m_value, b.m_value), c.m_value) };
}
CPPSPMD_FORCE_INLINE vfloat vfnma(const vfloat& a, const vfloat& b, const vfloat& c)
{
return vfloat{ _mm_sub_ps(c.m_value, _mm_mul_ps(a.m_value, b.m_value)) };
}
CPPSPMD_FORCE_INLINE vfloat vfnms(const vfloat& a, const vfloat& b, const vfloat& c)
{
return vfloat{ _mm_sub_ps(_mm_sub_ps(_mm_xor_ps(a.m_value, a.m_value), _mm_mul_ps(a.m_value, b.m_value)), c.m_value) };
}
CPPSPMD_FORCE_INLINE vfloat lerp(const vfloat &x, const vfloat &y, const vfloat &s) { return vfma(y - x, s, x); }
CPPSPMD_FORCE_INLINE lint operator+(int a, const lint& b) { return lint{ _mm_add_epi32(_mm_set1_epi32(a), b.m_value) }; }
CPPSPMD_FORCE_INLINE lint operator+(const lint& a, int b) { return lint{ _mm_add_epi32(a.m_value, _mm_set1_epi32(b)) }; }
CPPSPMD_FORCE_INLINE vfloat operator+(float a, const lint& b) { return vfloat(a) + vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator+(const lint& a, float b) { return vfloat(a) + vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator*(const lint& a, float b) { return vfloat(a) * vfloat(b); }
CPPSPMD_FORCE_INLINE vfloat operator*(float b, const lint& a) { return vfloat(a) * vfloat(b); }
CPPSPMD_FORCE_INLINE vint operator&(const vint& a, const vint& b) { return vint{ _mm_and_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator&(const vint& a, int b) { return a & vint(b); }
CPPSPMD_FORCE_INLINE vint andnot(const vint& a, const vint& b) { return vint{ _mm_andnot_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator|(const vint& a, const vint& b) { return vint{ _mm_or_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator|(const vint& a, int b) { return a | vint(b); }
CPPSPMD_FORCE_INLINE vint operator^(const vint& a, const vint& b) { return vint{ _mm_xor_si128(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator^(const vint& a, int b) { return a ^ vint(b); }
CPPSPMD_FORCE_INLINE vbool operator==(const vint& a, const vint& b) { return vbool{ _mm_cmpeq_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator!=(const vint& a, const vint& b) { return !vbool{ _mm_cmpeq_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator<(const vint& a, const vint& b) { return vbool{ _mm_cmpgt_epi32(b.m_value, a.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator<=(const vint& a, const vint& b) { return !vbool{ _mm_cmpgt_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator>=(const vint& a, const vint& b) { return !vbool{ _mm_cmpgt_epi32(b.m_value, a.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator>(const vint& a, const vint& b) { return vbool{ _mm_cmpgt_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator+(const vint& a, const vint& b) { return vint{ _mm_add_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator-(const vint& a, const vint& b) { return vint{ _mm_sub_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator+(const vint& a, int b) { return a + vint(b); }
CPPSPMD_FORCE_INLINE vint operator-(const vint& a, int b) { return a - vint(b); }
CPPSPMD_FORCE_INLINE vint operator+(int a, const vint& b) { return vint(a) + b; }
CPPSPMD_FORCE_INLINE vint operator-(int a, const vint& b) { return vint(a) - b; }
CPPSPMD_FORCE_INLINE vint operator*(const vint& a, const vint& b) { return vint{ mullo_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator*(const vint& a, int b) { return a * vint(b); }
CPPSPMD_FORCE_INLINE vint operator*(int a, const vint& b) { return vint(a) * b; }
CPPSPMD_FORCE_INLINE vint mulhiu(const vint& a, const vint& b) { return vint{ mulhi_epu32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator-(const vint& v) { return vint{ _mm_sub_epi32(_mm_setzero_si128(), v.m_value) }; }
CPPSPMD_FORCE_INLINE vint operator~(const vint& a) { return vint{ -a - 1 }; }
// A few of these break the lane-based abstraction model. They are supported in SSE2, so it makes sense to support them and let the user figure it out.
CPPSPMD_FORCE_INLINE vint adds_epu8(const vint& a, const vint& b) { return vint{ _mm_adds_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint subs_epu8(const vint& a, const vint& b) { return vint{ _mm_subs_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint avg_epu8(const vint & a, const vint & b) { return vint{ _mm_avg_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint max_epu8(const vint& a, const vint& b) { return vint{ _mm_max_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint min_epu8(const vint& a, const vint& b) { return vint{ _mm_min_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint sad_epu8(const vint& a, const vint& b) { return vint{ _mm_sad_epu8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint add_epi8(const vint& a, const vint& b) { return vint{ _mm_add_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint adds_epi8(const vint& a, const vint& b) { return vint{ _mm_adds_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint sub_epi8(const vint& a, const vint& b) { return vint{ _mm_sub_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint subs_epi8(const vint& a, const vint& b) { return vint{ _mm_subs_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmpeq_epi8(const vint& a, const vint& b) { return vint{ _mm_cmpeq_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmpgt_epi8(const vint& a, const vint& b) { return vint{ _mm_cmpgt_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmplt_epi8(const vint& a, const vint& b) { return vint{ _mm_cmplt_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint unpacklo_epi8(const vint& a, const vint& b) { return vint{ _mm_unpacklo_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint unpackhi_epi8(const vint& a, const vint& b) { return vint{ _mm_unpackhi_epi8(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE int movemask_epi8(const vint& a) { return _mm_movemask_epi8(a.m_value); }
CPPSPMD_FORCE_INLINE int movemask_epi32(const vint& a) { return _mm_movemask_ps(_mm_castsi128_ps(a.m_value)); }
CPPSPMD_FORCE_INLINE vint cmple_epu8(const vint& a, const vint& b) { return vint{ _mm_cmpeq_epi8(_mm_min_epu8(a.m_value, b.m_value), a.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmpge_epu8(const vint& a, const vint& b) { return vint{ cmple_epu8(b, a) }; }
CPPSPMD_FORCE_INLINE vint cmpgt_epu8(const vint& a, const vint& b) { return vint{ _mm_andnot_si128(_mm_cmpeq_epi8(a.m_value, b.m_value), _mm_cmpeq_epi8(_mm_max_epu8(a.m_value, b.m_value), a.m_value)) }; }
CPPSPMD_FORCE_INLINE vint cmplt_epu8(const vint& a, const vint& b) { return vint{ cmpgt_epu8(b, a) }; }
CPPSPMD_FORCE_INLINE vint absdiff_epu8(const vint& a, const vint& b) { return vint{ _mm_or_si128(_mm_subs_epu8(a.m_value, b.m_value), _mm_subs_epu8(b.m_value, a.m_value)) }; }
CPPSPMD_FORCE_INLINE vint blendv_epi8(const vint& a, const vint& b, const vint &mask) { return vint{ blendv_epi8(a.m_value, b.m_value, _mm_cmplt_epi8(mask.m_value, _mm_setzero_si128())) }; }
CPPSPMD_FORCE_INLINE vint blendv_epi32(const vint& a, const vint& b, const vint &mask) { return vint{ blendv_epi32(a.m_value, b.m_value, mask.m_value) }; }
CPPSPMD_FORCE_INLINE vint add_epi16(const vint& a, const vint& b) { return vint{ _mm_add_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint adds_epi16(const vint& a, const vint& b) { return vint{ _mm_adds_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint adds_epu16(const vint& a, const vint& b) { return vint{ _mm_adds_epu16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint avg_epu16(const vint& a, const vint& b) { return vint{ _mm_avg_epu16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint sub_epi16(const vint& a, const vint& b) { return vint{ _mm_sub_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint subs_epi16(const vint& a, const vint& b) { return vint{ _mm_subs_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint subs_epu16(const vint& a, const vint& b) { return vint{ _mm_subs_epu16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint mullo_epi16(const vint& a, const vint& b) { return vint{ _mm_mullo_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint mulhi_epi16(const vint& a, const vint& b) { return vint{ _mm_mulhi_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint mulhi_epu16(const vint& a, const vint& b) { return vint{ _mm_mulhi_epu16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint min_epi16(const vint& a, const vint& b) { return vint{ _mm_min_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint max_epi16(const vint& a, const vint& b) { return vint{ _mm_max_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint madd_epi16(const vint& a, const vint& b) { return vint{ _mm_madd_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmpeq_epi16(const vint& a, const vint& b) { return vint{ _mm_cmpeq_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmpgt_epi16(const vint& a, const vint& b) { return vint{ _mm_cmpgt_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint cmplt_epi16(const vint& a, const vint& b) { return vint{ _mm_cmplt_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint packs_epi16(const vint& a, const vint& b) { return vint{ _mm_packs_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint packus_epi16(const vint& a, const vint& b) { return vint{ _mm_packus_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint uniform_shift_left_epi16(const vint& a, const vint& b) { return vint{ _mm_sll_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint uniform_arith_shift_right_epi16(const vint& a, const vint& b) { return vint{ _mm_sra_epi16(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vint uniform_shift_right_epi16(const vint& a, const vint& b) { return vint{ _mm_srl_epi16(a.m_value, b.m_value) }; }
#define VINT_SHIFT_LEFT_EPI16(a, b) vint(_mm_slli_epi16((a).m_value, b))
#define VINT_SHIFT_RIGHT_EPI16(a, b) vint(_mm_srai_epi16((a).m_value, b))
#define VUINT_SHIFT_RIGHT_EPI16(a, b) vint(_mm_srli_epi16((a).m_value, b))
CPPSPMD_FORCE_INLINE vint undefined_vint() { return vint{ _mm_undefined_si128() }; }
CPPSPMD_FORCE_INLINE vfloat undefined_vfloat() { return vfloat{ _mm_undefined_ps() }; }
CPPSPMD_FORCE_INLINE vint zero_vint() { return vint{ _mm_setzero_si128() }; }
CPPSPMD_FORCE_INLINE vfloat zero_vfloat() { return vfloat{ _mm_setzero_ps() }; }
CPPSPMD_FORCE_INLINE vint vint_lane_set(int v0, int v1, int v2, int v3) { return vint{ _mm_set_epi32(v3, v2, v1, v0) }; }
CPPSPMD_FORCE_INLINE vfloat vfloat_lane_set(float v0, float v1, float v2, float v3) { return vfloat{ _mm_set_ps(v3, v2, v1, v0) }; }
CPPSPMD_FORCE_INLINE vint vint_lane_set_r(int v3, int v2, int v1, int v0) { return vint{ _mm_set_epi32(v3, v2, v1, v0) }; }
CPPSPMD_FORCE_INLINE vfloat vfloat_lane_set_r(float v3, float v2, float v1, float v0) { return vfloat{ _mm_set_ps(v3, v2, v1, v0) }; }
// control is an 8-bit immediate value containing 4 2-bit indices which shuffles the int32's in each 128-bit lane.
#define VINT_LANE_SHUFFLE_EPI32(a, control) vint(_mm_shuffle_epi32((a).m_value, control))
#define VFLOAT_LANE_SHUFFLE_PS(a, b, control) vfloat(_mm_shuffle_ps((a).m_value, (b).m_value, control))
// control is an 8-bit immediate value containing 4 2-bit indices which shuffles the int16's in either the high or low 64-bit lane.
#define VINT_LANE_SHUFFLELO_EPI16(a, control) vint(_mm_shufflelo_epi16((a).m_value, control))
#define VINT_LANE_SHUFFLEHI_EPI16(a, control) vint(_mm_shufflehi_epi16((a).m_value, control))
#define VINT_LANE_SHUFFLE_MASK(a, b, c, d) ((a) | ((b) << 2) | ((c) << 4) | ((d) << 6))
#define VINT_LANE_SHUFFLE_MASK_R(d, c, b, a) ((a) | ((b) << 2) | ((c) << 4) | ((d) << 6))
#define VINT_LANE_SHIFT_LEFT_BYTES(a, l) vint(_mm_slli_si128((a).m_value, l))
#define VINT_LANE_SHIFT_RIGHT_BYTES(a, l) vint(_mm_srli_si128((a).m_value, l))
// Unpack and interleave 8-bit integers from the low or high half of a and b
CPPSPMD_FORCE_INLINE vint vint_lane_unpacklo_epi8(const vint& a, const vint& b) { return vint(_mm_unpacklo_epi8(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint vint_lane_unpackhi_epi8(const vint& a, const vint& b) { return vint(_mm_unpackhi_epi8(a.m_value, b.m_value)); }
// Unpack and interleave 16-bit integers from the low or high half of a and b
CPPSPMD_FORCE_INLINE vint vint_lane_unpacklo_epi16(const vint& a, const vint& b) { return vint(_mm_unpacklo_epi16(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint vint_lane_unpackhi_epi16(const vint& a, const vint& b) { return vint(_mm_unpackhi_epi16(a.m_value, b.m_value)); }
// Unpack and interleave 32-bit integers from the low or high half of a and b
CPPSPMD_FORCE_INLINE vint vint_lane_unpacklo_epi32(const vint& a, const vint& b) { return vint(_mm_unpacklo_epi32(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint vint_lane_unpackhi_epi32(const vint& a, const vint& b) { return vint(_mm_unpackhi_epi32(a.m_value, b.m_value)); }
// Unpack and interleave 64-bit integers from the low or high half of a and b
CPPSPMD_FORCE_INLINE vint vint_lane_unpacklo_epi64(const vint& a, const vint& b) { return vint(_mm_unpacklo_epi64(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint vint_lane_unpackhi_epi64(const vint& a, const vint& b) { return vint(_mm_unpackhi_epi64(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint vint_set1_epi8(int8_t a) { return vint(_mm_set1_epi8(a)); }
CPPSPMD_FORCE_INLINE vint vint_set1_epi16(int16_t a) { return vint(_mm_set1_epi16(a)); }
CPPSPMD_FORCE_INLINE vint vint_set1_epi32(int32_t a) { return vint(_mm_set1_epi32(a)); }
CPPSPMD_FORCE_INLINE vint vint_set1_epi64(int64_t a) { return vint(_mm_set1_epi64x(a)); }
CPPSPMD_FORCE_INLINE vint mul_epu32(const vint &a, const vint& b) { return vint(_mm_mul_epu32(a.m_value, b.m_value)); }
CPPSPMD_FORCE_INLINE vint div_epi32(const vint &a, const vint& b)
{
__m128d al = _mm_cvtepi32_pd(a.m_value);
__m128d ah = _mm_cvtepi32_pd(_mm_unpackhi_epi64(a.m_value, a.m_value));
__m128d bl = _mm_cvtepi32_pd(b.m_value);
__m128d bh = _mm_cvtepi32_pd(_mm_unpackhi_epi64(b.m_value, b.m_value));
__m128d rl = _mm_div_pd(al, bl);
__m128d rh = _mm_div_pd(ah, bh);
__m128i rli = _mm_cvttpd_epi32(rl);
__m128i rhi = _mm_cvttpd_epi32(rh);
return vint(_mm_unpacklo_epi64(rli, rhi));
}
CPPSPMD_FORCE_INLINE vint mod_epi32(const vint &a, const vint& b)
{
vint aa = abs(a), ab = abs(b);
vint q = div_epi32(aa, ab);
vint r = aa - q * ab;
return spmd_ternaryi(a < 0, -r, r);
}
CPPSPMD_FORCE_INLINE vint operator/ (const vint& a, const vint& b)
{
return div_epi32(a, b);
}
CPPSPMD_FORCE_INLINE vint operator/ (const vint& a, int b)
{
return div_epi32(a, vint(b));
}
CPPSPMD_FORCE_INLINE vint operator% (const vint& a, const vint& b)
{
return mod_epi32(a, b);
}
CPPSPMD_FORCE_INLINE vint operator% (const vint& a, int b)
{
return mod_epi32(a, vint(b));
}
CPPSPMD_FORCE_INLINE vint operator<< (const vint& a, const vint& b)
{
#if 0
CPPSPMD_ALIGN(32) int result[4];
result[0] = extract_x(a.m_value) << extract_x(b.m_value);
result[1] = extract_y(a.m_value) << extract_y(b.m_value);
result[2] = extract_z(a.m_value) << extract_z(b.m_value);
result[3] = extract_w(a.m_value) << extract_w(b.m_value);
return vint{ _mm_load_si128((__m128i*)result) };
#elif 0
int x = extract_x(a.m_value) << extract_x(b.m_value);
int y = extract_y(a.m_value) << extract_y(b.m_value);
int z = extract_z(a.m_value) << extract_z(b.m_value);
int w = extract_w(a.m_value) << extract_w(b.m_value);
__m128i v = insert_x(_mm_undefined_si128(), x);
v = insert_y(v, y);
v = insert_z(v, z);
return vint{ insert_w(v, w) };
#else
// What this does: shift left each b lane by 23 bits (to move the shift amount into the FP exponent position), then epi32 add to the integer rep of 1.0f, then cast that to float, then convert that to int to get fast 2^x.
return a * vint(cast_vint_to_vfloat(vint(_mm_slli_epi32(b.m_value, 23)) + cast_vfloat_to_vint(vfloat(1.0f))));
#endif
}
// uniform shift left
CPPSPMD_FORCE_INLINE vint operator<< (const vint& a, int b)
{
__m128i bv = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(_mm_set1_epi32(b)), _mm_castsi128_ps(_mm_load_si128((const __m128i *)g_x_128))));
return vint{ _mm_sll_epi32(a.m_value, bv) };
}
// uniform arithmetic shift right
CPPSPMD_FORCE_INLINE vint operator>> (const vint& a, int b)
{
__m128i bv = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(_mm_set1_epi32(b)), _mm_castsi128_ps(_mm_load_si128((const __m128i *)g_x_128))));
return vint{ _mm_sra_epi32(a.m_value, bv) };
}
// uniform shift right
CPPSPMD_FORCE_INLINE vint vuint_shift_right(const vint& a, int b)
{
__m128i bv = _mm_castps_si128(_mm_and_ps(_mm_castsi128_ps(_mm_set1_epi32(b)), _mm_castsi128_ps(_mm_load_si128((const __m128i *)g_x_128))));
return vint{ _mm_srl_epi32(a.m_value, bv) };
}
CPPSPMD_FORCE_INLINE vint vuint_shift_right(const vint& a, const vint& b)
{
#if 0
CPPSPMD_ALIGN(32) int result[4];
result[0] = ((uint32_t)extract_x(a.m_value)) >> extract_x(b.m_value);
result[1] = ((uint32_t)extract_y(a.m_value)) >> extract_y(b.m_value);
result[2] = ((uint32_t)extract_z(a.m_value)) >> extract_z(b.m_value);
result[3] = ((uint32_t)extract_w(a.m_value)) >> extract_w(b.m_value);
return vint{ _mm_load_si128((__m128i*)result) };
#elif 0
uint32_t x = ((uint32_t)extract_x(a.m_value)) >> ((uint32_t)extract_x(b.m_value));
uint32_t y = ((uint32_t)extract_y(a.m_value)) >> ((uint32_t)extract_y(b.m_value));
uint32_t z = ((uint32_t)extract_z(a.m_value)) >> ((uint32_t)extract_z(b.m_value));
uint32_t w = ((uint32_t)extract_w(a.m_value)) >> ((uint32_t)extract_w(b.m_value));
__m128i v = insert_x(_mm_undefined_si128(), x);
v = insert_y(v, y);
v = insert_z(v, z);
return vint{ insert_w(v, w) };
#else
//vint inv_shift = 32 - b;
//vfloat f = cast_vint_to_vfloat(vint(_mm_slli_epi32(inv_shift.m_value, 23)) + cast_vfloat_to_vint(vfloat(1.0f)));
// Take float rep of 1.0f (0x3f800000), subtract (32<<23), subtract (shift<<23), cast to float.
vfloat f = cast_vint_to_vfloat(vint(_mm_sub_epi32(_mm_set1_epi32(0x4f800000), _mm_slli_epi32(b.m_value, 23))));
// Now convert scale factor to integer.
vint r = vint(f);
// mulhi_epu32 (using two _mm_mul_epu32), to emulate varying shift left.
vint q(mulhi_epu32(a.m_value, r.m_value));
// Handle shift amounts of 0.
return spmd_ternaryi(b > 0, q, a);
#endif
}
CPPSPMD_FORCE_INLINE vint vuint_shift_right_not_zero(const vint& a, const vint& b)
{
//vint inv_shift = 32 - b;
//vfloat f = cast_vint_to_vfloat(vint(_mm_slli_epi32(inv_shift.m_value, 23)) + cast_vfloat_to_vint(vfloat(1.0f)));
// Take float rep of 1.0f (0x3f800000), subtract (32<<23), subtract (shift<<23), cast to float.
vfloat f = cast_vint_to_vfloat(vint(_mm_sub_epi32(_mm_set1_epi32(0x4f800000), _mm_slli_epi32(b.m_value, 23))));
// Now convert scale factor to integer.
vint r = vint(f);
// mulhi_epu32 (using two _mm_mul_epu32), to emulate varying shift left.
return vint(mulhi_epu32(a.m_value, r.m_value));
}
CPPSPMD_FORCE_INLINE vint operator>> (const vint& a, const vint& b)
{
#if 0
CPPSPMD_ALIGN(32) int result[4];
result[0] = extract_x(a.m_value) >> extract_x(b.m_value);
result[1] = extract_y(a.m_value) >> extract_y(b.m_value);
result[2] = extract_z(a.m_value) >> extract_z(b.m_value);
result[3] = extract_w(a.m_value) >> extract_w(b.m_value);
return vint{ _mm_load_si128((__m128i*)result) };
#elif 0
int x = extract_x(a.m_value) >> extract_x(b.m_value);
int y = extract_y(a.m_value) >> extract_y(b.m_value);
int z = extract_z(a.m_value) >> extract_z(b.m_value);
int w = extract_w(a.m_value) >> extract_w(b.m_value);
__m128i v = insert_x(_mm_undefined_si128(), x);
v = insert_y(v, y);
v = insert_z(v, z);
return vint{ insert_w(v, w) };
#else
vint sign_mask(_mm_cmplt_epi32(a.m_value, _mm_setzero_si128()));
vint a_shifted = vuint_shift_right(a ^ sign_mask, b) ^ sign_mask;
return a_shifted;
#endif
}
#undef VINT_SHIFT_LEFT
#undef VINT_SHIFT_RIGHT
#undef VUINT_SHIFT_RIGHT
// Shift left/right by a uniform immediate constant
#define VINT_SHIFT_LEFT(a, b) vint(_mm_slli_epi32( (a).m_value, (b) ) )
#define VINT_SHIFT_RIGHT(a, b) vint( _mm_srai_epi32( (a).m_value, (b) ) )
#define VUINT_SHIFT_RIGHT(a, b) vint( _mm_srli_epi32( (a).m_value, (b) ) )
#define VINT_ROT(x, k) (VINT_SHIFT_LEFT((x), (k)) | VUINT_SHIFT_RIGHT((x), 32 - (k)))
CPPSPMD_FORCE_INLINE vbool operator==(const lint& a, const lint& b) { return vbool{ _mm_cmpeq_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator==(const lint& a, int b) { return vint(a) == vint(b); }
CPPSPMD_FORCE_INLINE vbool operator==(int a, const lint& b) { return vint(a) == vint(b); }
CPPSPMD_FORCE_INLINE vbool operator<(const lint& a, const lint& b) { return vbool{ _mm_cmpgt_epi32(b.m_value, a.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator>(const lint& a, const lint& b) { return vbool{ _mm_cmpgt_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator<=(const lint& a, const lint& b) { return !vbool{ _mm_cmpgt_epi32(a.m_value, b.m_value) }; }
CPPSPMD_FORCE_INLINE vbool operator>=(const lint& a, const lint& b) { return !vbool{ _mm_cmpgt_epi32(b.m_value, a.m_value) }; }
CPPSPMD_FORCE_INLINE float extract(const vfloat& v, int instance) { assert(instance < 4); CPPSPMD_ALIGN(16) float values[4]; _mm_store_ps(values, v.m_value); return values[instance]; }
CPPSPMD_FORCE_INLINE int extract(const vint& v, int instance) { assert(instance < 4); CPPSPMD_ALIGN(16) int values[4]; _mm_store_si128((__m128i*)values, v.m_value); return values[instance]; }
CPPSPMD_FORCE_INLINE int extract(const lint& v, int instance) { assert(instance < 4); CPPSPMD_ALIGN(16) int values[4]; _mm_store_si128((__m128i*)values, v.m_value); return values[instance]; }
CPPSPMD_FORCE_INLINE bool extract(const vbool& v, int instance) { assert(instance < 4); CPPSPMD_ALIGN(16) int values[4]; _mm_store_si128((__m128i*)values, v.m_value); return values[instance] != 0; }
#undef VINT_EXTRACT
#undef VBOOL_EXTRACT
#undef VFLOAT_EXTRACT
#if CPPSPMD_SSE2
// Pass in an immediate constant and the compiler will optimize these expressions.
#define VINT_EXTRACT(v, instance) ( ((instance) == 0) ? extract_x((v).m_value) : (((instance) == 1) ? extract_y((v).m_value) : (((instance) == 2) ? extract_z((v).m_value) : extract_w((v).m_value))) )
#define VBOOL_EXTRACT(v, instance) ( ((instance) == 0) ? extract_x((v).m_value) : (((instance) == 1) ? extract_y((v).m_value) : (((instance) == 2) ? extract_z((v).m_value) : extract_w((v).m_value))) )
#define VFLOAT_EXTRACT(v, instance) ( ((instance) == 0) ? extractf_ps_x((v).m_value) : (((instance) == 1) ? extractf_ps_y((v).m_value) : (((instance) == 2) ? extractf_ps_z((v).m_value) : extractf_ps_w((v).m_value))) )
#else
CPPSPMD_FORCE_INLINE float cast_int_bits_as_float(int v) { return *(const float*)&v; }
#define VINT_EXTRACT(v, instance) _mm_extract_epi32((v).m_value, instance)
#define VBOOL_EXTRACT(v, instance) _mm_extract_epi32((v).m_value, instance)
#define VFLOAT_EXTRACT(v, instance) cast_int_bits_as_float(_mm_extract_ps((v).m_value, instance))
#endif
CPPSPMD_FORCE_INLINE vfloat &insert(vfloat& v, int instance, float f)
{
assert(instance < 4);
CPPSPMD_ALIGN(16) float values[4];
_mm_store_ps(values, v.m_value);
values[instance] = f;
v.m_value = _mm_load_ps(values);
return v;
}
CPPSPMD_FORCE_INLINE vint &insert(vint& v, int instance, int i)
{
assert(instance < 4);
CPPSPMD_ALIGN(16) int values[4];
_mm_store_si128((__m128i *)values, v.m_value);
values[instance] = i;
v.m_value = _mm_load_si128((__m128i *)values);
return v;
}
CPPSPMD_FORCE_INLINE vint init_lookup4(const uint8_t pTab[16])
{
__m128i l = _mm_loadu_si128((const __m128i*)pTab);
return vint{ l };
}
CPPSPMD_FORCE_INLINE vint table_lookup4_8(const vint& a, const vint& table)
{
return vint{ shuffle_epi8(table.m_value, a.m_value) };
}
CPPSPMD_FORCE_INLINE void init_lookup5(const uint8_t pTab[32], vint& table_0, vint& table_1)
{
__m128i l = _mm_loadu_si128((const __m128i*)pTab);
__m128i h = _mm_loadu_si128((const __m128i*)(pTab + 16));
table_0.m_value = l;
table_1.m_value = h;
}
CPPSPMD_FORCE_INLINE vint table_lookup5_8(const vint& a, const vint& table_0, const vint& table_1)
{
__m128i l_0 = shuffle_epi8(table_0.m_value, a.m_value);
__m128i h_0 = shuffle_epi8(table_1.m_value, a.m_value);
__m128i m_0 = _mm_slli_epi32(a.m_value, 31 - 4);
__m128 v_0 = blendv_ps(_mm_castsi128_ps(l_0), _mm_castsi128_ps(h_0), _mm_castsi128_ps(m_0));
return vint{ _mm_castps_si128(v_0) };
}
CPPSPMD_FORCE_INLINE void init_lookup6(const uint8_t pTab[64], vint& table_0, vint& table_1, vint& table_2, vint& table_3)
{
__m128i a = _mm_loadu_si128((const __m128i*)pTab);
__m128i b = _mm_loadu_si128((const __m128i*)(pTab + 16));
__m128i c = _mm_loadu_si128((const __m128i*)(pTab + 32));
__m128i d = _mm_loadu_si128((const __m128i*)(pTab + 48));
table_0.m_value = a;
table_1.m_value = b;
table_2.m_value = c;
table_3.m_value = d;
}
CPPSPMD_FORCE_INLINE vint table_lookup6_8(const vint& a, const vint& table_0, const vint& table_1, const vint& table_2, const vint& table_3)
{
__m128i m_0 = _mm_slli_epi32(a.m_value, 31 - 4);
__m128 av_0;
{
__m128i al_0 = shuffle_epi8(table_0.m_value, a.m_value);
__m128i ah_0 = shuffle_epi8(table_1.m_value, a.m_value);
av_0 = blendv_ps(_mm_castsi128_ps(al_0), _mm_castsi128_ps(ah_0), _mm_castsi128_ps(m_0));
}
__m128 bv_0;
{
__m128i bl_0 = shuffle_epi8(table_2.m_value, a.m_value);
__m128i bh_0 = shuffle_epi8(table_3.m_value, a.m_value);
bv_0 = blendv_ps(_mm_castsi128_ps(bl_0), _mm_castsi128_ps(bh_0), _mm_castsi128_ps(m_0));
}
__m128i m2_0 = _mm_slli_epi32(a.m_value, 31 - 5);
__m128 v2_0 = blendv_ps(av_0, bv_0, _mm_castsi128_ps(m2_0));
return vint{ _mm_castps_si128(v2_0) };
}
#if 0
template<typename SPMDKernel, typename... Args>
CPPSPMD_FORCE_INLINE decltype(auto) spmd_call(Args&&... args)
{
SPMDKernel kernel;
kernel.init(exec_mask::all_on());
return kernel._call(std::forward<Args>(args)...);
}
#else
template<typename SPMDKernel, typename... Args>
CPPSPMD_FORCE_INLINE void spmd_call(Args&&... args)
{
SPMDKernel kernel;
kernel.init(exec_mask::all_on());
kernel._call(std::forward<Args>(args)...);
}
#endif
CPPSPMD_FORCE_INLINE void spmd_kernel::init(const spmd_kernel::exec_mask& kernel_exec)
{
m_exec = kernel_exec;
m_kernel_exec = kernel_exec;
m_continue_mask = exec_mask::all_off();
#ifdef _DEBUG
m_in_loop = false;
#endif
}
CPPSPMD_FORCE_INLINE const float_vref& spmd_kernel::store(const float_vref& dst, const vfloat& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) float stored[4];
_mm_store_ps(stored, src.m_value);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
dst.m_pValue[vindex[i]] = stored[i];
}
return dst;
}
CPPSPMD_FORCE_INLINE const float_vref& spmd_kernel::store_all(const float_vref& dst, const vfloat& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) float stored[4];
_mm_store_ps(stored, src.m_value);
for (int i = 0; i < 4; i++)
dst.m_pValue[vindex[i]] = stored[i];
return dst;
}
CPPSPMD_FORCE_INLINE const float_vref& spmd_kernel::store(const float_vref&& dst, const vfloat& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) float stored[4];
_mm_store_ps(stored, src.m_value);
int mask = _mm_movemask_ps(_mm_castsi128_ps(m_exec.m_mask));
for (int i = 0; i < 4; i++)
{
if (mask & (1 << i))
dst.m_pValue[vindex[i]] = stored[i];
}
return dst;
}
CPPSPMD_FORCE_INLINE const float_vref& spmd_kernel::store_all(const float_vref&& dst, const vfloat& src)
{
CPPSPMD_ALIGN(16) int vindex[4];
_mm_store_si128((__m128i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(16) float stored[4];
_mm_store_ps(stored, src.m_value);
for (int i = 0; i < 4; i++)
dst.m_pValue[vindex[i]] = stored[i];
return dst;
}
#include "cppspmd_flow.h"
#include "cppspmd_math.h"
} // namespace cppspmd_sse41
|