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
|
#ifndef VC_VC_
#define VC_VC_
#ifndef VC_IO_
#define VC_IO_
#include <iostream>
#if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
#define Vc_HACK_OSTREAM_FOR_TTY 1
#endif
#ifdef Vc_HACK_OSTREAM_FOR_TTY
#include <unistd.h>
#include <ext/stdio_sync_filebuf.h>
#endif
namespace Vc_VERSIONED_NAMESPACE
{
namespace
{
#ifdef Vc_HACK_OSTREAM_FOR_TTY
class hacked_ostream : public std::ostream
{
public:
using std::ostream::_M_streambuf;
};
bool mayUseColor(const std::ostream &os) __attribute__((__const__));
bool mayUseColor(const std::ostream &os)
{
std::basic_streambuf<char> *hack1 =
const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
__gnu_cxx::stdio_sync_filebuf<char> *hack =
dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
if (!hack) {
return false;
}
FILE *file = hack->file();
return 1 == isatty(fileno(file));
}
#else
bool mayUseColor(const std::ostream &) { return false; }
#endif
}
namespace AnsiColor
{
struct Type
{
const char *data;
};
static const Type green = {"\033[1;40;32m"};
static const Type yellow = {"\033[1;40;33m"};
static const Type blue = {"\033[1;40;34m"};
static const Type normal = {"\033[0m"};
inline std::ostream &operator<<(std::ostream &out, const Type &c)
{
if (mayUseColor(out)) {
out << c.data;
}
return out;
}
}
template <typename T, typename Abi>
inline std::ostream &operator<<(std::ostream &out, const Vc::Vector<T, Abi> &v)
{
using TT = typename std::conditional<std::is_same<T, char>::value ||
std::is_same<T, unsigned char>::value ||
std::is_same<T, signed char>::value,
int,
T>::type;
out << AnsiColor::green << '[';
out << TT(v[0]);
for (size_t i = 1; i < v.Size; ++i) {
out << ", " << TT(v[i]);
}
out << ']' << AnsiColor::normal;
return out;
}
template <typename T, typename Abi>
inline std::ostream &operator<<(std::ostream &out, const Vc::Mask<T, Abi> &m)
{
out << AnsiColor::blue << "m[";
for (unsigned int i = 0; i < m.Size; ++i) {
if (i > 0 && (i % 4) == 0) {
out << ' ';
}
if (m[i]) {
out << AnsiColor::yellow << '1';
} else {
out << AnsiColor::blue << '0';
}
}
out << AnsiColor::blue << ']' << AnsiColor::normal;
return out;
}
namespace Common
{
#ifdef DOXYGEN
template<typename V, typename Parent, typename Dimension, typename RM>
inline std::ostream &operator<<(std::ostream &s, const Vc::MemoryBase<V, Parent, Dimension, RM> &m);
#endif
template<typename V, typename Parent, typename RM>
inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 1, RM> &m )
{
out << AnsiColor::blue << '{' << AnsiColor::normal;
for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
out << V(m.vector(i));
}
out << AnsiColor::blue << '}' << AnsiColor::normal;
return out;
}
template<typename V, typename Parent, typename RM>
inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 2, RM> &m )
{
out << AnsiColor::blue << '{' << AnsiColor::normal;
for (size_t i = 0; i < m.rowsCount(); ++i) {
if (i > 0) {
out << "\n ";
}
const size_t vcount = m[i].vectorsCount();
for (size_t j = 0; j < vcount; ++j) {
out << V(m[i].vector(j));
}
}
out << AnsiColor::blue << '}' << AnsiColor::normal;
return out;
}
}
template<typename T, std::size_t N>
inline std::ostream &operator<<(std::ostream &out, const SimdArray<T, N> &v)
{
out << AnsiColor::green << '<' << v[0];
for (size_t i = 1; i < N; ++i) {
if (i % 4 == 0) out << " |";
out << ' ' << v[i];
}
return out << '>' << AnsiColor::normal;
}
template<typename T, std::size_t N>
inline std::ostream &operator<<(std::ostream &out, const SimdMaskArray<T, N> &m)
{
out << AnsiColor::blue << "«";
for (size_t i = 0; i < N; ++i) {
if (i > 0 && (i % 4) == 0) {
out << ' ';
}
if ( m[i] ) {
out << AnsiColor::yellow << '1';
} else {
out << AnsiColor::blue << '0';
}
}
return out << AnsiColor::blue << "»" << AnsiColor::normal;
}
}
#endif
#ifndef VC_MEMORY_
#define VC_MEMORY_
#ifndef VC_COMMON_MEMORY_H_
#define VC_COMMON_MEMORY_H_
#ifndef VC_COMMON_MEMORYBASE_H_
#define VC_COMMON_MEMORYBASE_H_
#include <assert.h>
#include <type_traits>
#include <iterator>
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
#define Vc_MEM_OPERATOR_EQ(op) \
template<typename T> \
Vc_ALWAYS_INLINE enable_if_mutable<T, MemoryVector &> operator op##=(const T &x) { \
const V v = value() op x; \
v.store(&m_data[0], Flags()); \
return *this; \
}
template<typename _V, typename Flags> class MemoryVector
{
typedef typename std::remove_cv<_V>::type V;
template<typename T, typename R> using enable_if_mutable =
typename std::enable_if<std::is_same<T, T>::value && !std::is_const<_V>::value, R>::type;
using EntryType =
typename std::conditional<std::is_const<_V>::value, const typename V::EntryType,
typename V::EntryType>::type;
typedef typename V::Mask Mask;
EntryType m_data[V::Size];
public:
Vc_INTRINSIC MemoryVector() = default;
MemoryVector(const MemoryVector &) = delete;
MemoryVector(MemoryVector &&) = delete;
Vc_ALWAYS_INLINE Vc_PURE V value() const { return V(&m_data[0], Flags()); }
Vc_ALWAYS_INLINE Vc_PURE operator V() const { return value(); }
template<typename T>
Vc_ALWAYS_INLINE enable_if_mutable<T, MemoryVector &> operator=(const T &x) {
V v;
v = x;
v.store(&m_data[0], Flags());
return *this;
}
Vc_ALL_BINARY(Vc_MEM_OPERATOR_EQ);
Vc_ALL_ARITHMETICS(Vc_MEM_OPERATOR_EQ);
Vc_ALWAYS_INLINE EntryType &operator[](size_t i) { return m_data[i]; }
Vc_ALWAYS_INLINE const EntryType &operator[](size_t i) const { return m_data[i]; }
};
template<typename _V, typename Flags> class MemoryVectorIterator
{
typedef typename std::remove_cv<_V>::type V;
template<typename T, typename R> using enable_if_mutable =
typename std::enable_if<std::is_same<T, T>::value && !std::is_const<_V>::value, R>::type;
using iterator_traits = std::iterator_traits<MemoryVector<_V, Flags> *>;
MemoryVector<_V, Flags> *d;
public:
typedef typename iterator_traits::difference_type difference_type;
typedef typename iterator_traits::value_type value_type;
typedef typename iterator_traits::pointer pointer;
typedef typename iterator_traits::reference reference;
typedef typename iterator_traits::iterator_category iterator_category;
constexpr MemoryVectorIterator(MemoryVector<_V, Flags> *dd) : d(dd) {}
constexpr MemoryVectorIterator(const MemoryVectorIterator &) = default;
constexpr MemoryVectorIterator(MemoryVectorIterator &&) = default;
Vc_ALWAYS_INLINE MemoryVectorIterator &operator=(const MemoryVectorIterator &) = default;
Vc_ALWAYS_INLINE void *orderBy() const { return d; }
Vc_ALWAYS_INLINE difference_type operator-(const MemoryVectorIterator &rhs) const { return d - rhs.d; }
Vc_ALWAYS_INLINE reference operator[](size_t i) const { return d[i]; }
Vc_ALWAYS_INLINE reference operator*() const { return *d; }
Vc_ALWAYS_INLINE pointer operator->() const { return d; }
Vc_ALWAYS_INLINE MemoryVectorIterator &operator++() { ++d; return *this; }
Vc_ALWAYS_INLINE MemoryVectorIterator operator++(int) { MemoryVectorIterator r(*this); ++d; return r; }
Vc_ALWAYS_INLINE MemoryVectorIterator &operator--() { --d; return *this; }
Vc_ALWAYS_INLINE MemoryVectorIterator operator--(int) { MemoryVectorIterator r(*this); --d; return r; }
Vc_ALWAYS_INLINE MemoryVectorIterator &operator+=(size_t n) { d += n; return *this; }
Vc_ALWAYS_INLINE MemoryVectorIterator &operator-=(size_t n) { d -= n; return *this; }
Vc_ALWAYS_INLINE MemoryVectorIterator operator+(size_t n) const { return MemoryVectorIterator(d + n); }
Vc_ALWAYS_INLINE MemoryVectorIterator operator-(size_t n) const { return MemoryVectorIterator(d - n); }
};
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator==(const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() == r.orderBy();
}
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator!=(const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() != r.orderBy();
}
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator>=(const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() >= r.orderBy();
}
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator<=(const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() <= r.orderBy();
}
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator> (const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() > r.orderBy();
}
template<typename V, typename FlagsL, typename FlagsR>
Vc_ALWAYS_INLINE bool operator< (const MemoryVectorIterator<V, FlagsL> &l, const MemoryVectorIterator<V, FlagsR> &r)
{
return l.orderBy() < r.orderBy();
}
#undef Vc_MEM_OPERATOR_EQ
#define Vc_VPH_OPERATOR(op) \
template <typename V1, typename Flags1, typename V2, typename Flags2> \
decltype(std::declval<V1>() op std::declval<V2>()) operator op( \
const MemoryVector<V1, Flags1> &x, const MemoryVector<V2, Flags2> &y) \
{ \
return x.value() op y.value(); \
}
Vc_ALL_ARITHMETICS(Vc_VPH_OPERATOR);
Vc_ALL_BINARY (Vc_VPH_OPERATOR);
Vc_ALL_COMPARES (Vc_VPH_OPERATOR);
#undef Vc_VPH_OPERATOR
template<typename V, typename Parent, typename Flags = Prefetch<>> class MemoryRange
{
Parent *m_parent;
size_t m_first;
size_t m_last;
public:
MemoryRange(Parent *p, size_t firstIndex, size_t lastIndex)
: m_parent(p), m_first(firstIndex), m_last(lastIndex)
{}
MemoryVectorIterator<V, Flags> begin() const { return &m_parent->vector(m_first , Flags()); }
MemoryVectorIterator<V, Flags> end() const { return &m_parent->vector(m_last + 1, Flags()); }
};
template<typename V, typename Parent, int Dimension, typename RowMemory> class MemoryDimensionBase;
template<typename V, typename Parent, typename RowMemory> class MemoryDimensionBase<V, Parent, 1, RowMemory>
{
private:
Parent *p() { return static_cast<Parent *>(this); }
const Parent *p() const { return static_cast<const Parent *>(this); }
public:
typedef typename V::EntryType EntryType;
Vc_ALWAYS_INLINE Vc_PURE EntryType *entries() { return &p()->m_mem[0]; }
Vc_ALWAYS_INLINE Vc_PURE const EntryType *entries() const { return &p()->m_mem[0]; }
Vc_ALWAYS_INLINE Vc_PURE EntryType &scalar(size_t i) { return entries()[i]; }
Vc_ALWAYS_INLINE Vc_PURE const EntryType scalar(size_t i) const { return entries()[i]; }
#ifdef DOXYGEN
Vc_ALWAYS_INLINE Vc_PURE operator EntryType*() { return entries(); }
Vc_ALWAYS_INLINE Vc_PURE operator const EntryType*() const { return entries(); }
#else
template <typename T,
typename std::enable_if<
std::is_same<typename std::remove_const<T>::type, EntryType *>::value ||
std::is_same<typename std::remove_const<T>::type, void *>::value,
int>::type = 0>
Vc_ALWAYS_INLINE Vc_PURE operator T()
{
return entries();
}
template <typename T,
typename std::enable_if<std::is_same<T, const EntryType *>::value ||
std::is_same<T, const void *>::value,
int>::type = 0>
Vc_ALWAYS_INLINE Vc_PURE operator T() const
{
return entries();
}
#endif
template<typename Flags>
Vc_ALWAYS_INLINE MemoryRange<V, Parent, Flags> range(size_t firstIndex, size_t lastIndex, Flags) {
return MemoryRange<V, Parent, Flags>(p(), firstIndex, lastIndex);
}
Vc_ALWAYS_INLINE MemoryRange<V, Parent> range(size_t firstIndex, size_t lastIndex) {
return MemoryRange<V, Parent>(p(), firstIndex, lastIndex);
}
template<typename Flags>
Vc_ALWAYS_INLINE MemoryRange<const V, Parent, Flags> range(size_t firstIndex, size_t lastIndex, Flags) const {
return MemoryRange<const V, Parent, Flags>(p(), firstIndex, lastIndex);
}
Vc_ALWAYS_INLINE MemoryRange<const V, Parent> range(size_t firstIndex, size_t lastIndex) const {
return MemoryRange<const V, Parent>(p(), firstIndex, lastIndex);
}
Vc_ALWAYS_INLINE EntryType &operator[](size_t i) { return entries()[i]; }
Vc_ALWAYS_INLINE const EntryType &operator[](size_t i) const { return entries()[i]; }
template<typename IndexT> Vc_ALWAYS_INLINE Vc_PURE V operator[](Vector<IndexT> i) const
{
return V(entries(), i);
}
};
template<typename V, typename Parent, typename RowMemory> class MemoryDimensionBase<V, Parent, 2, RowMemory>
{
private:
Parent *p() { return static_cast<Parent *>(this); }
const Parent *p() const { return static_cast<const Parent *>(this); }
public:
typedef typename V::EntryType EntryType;
static constexpr size_t rowCount() { return Parent::RowCount; }
Vc_ALWAYS_INLINE Vc_PURE EntryType *entries(size_t x = 0) { return &p()->m_mem[x][0]; }
Vc_ALWAYS_INLINE Vc_PURE const EntryType *entries(size_t x = 0) const { return &p()->m_mem[x][0]; }
Vc_ALWAYS_INLINE Vc_PURE EntryType &scalar(size_t i, size_t j) { return entries(i)[j]; }
Vc_ALWAYS_INLINE Vc_PURE const EntryType scalar(size_t i, size_t j) const { return entries(i)[j]; }
Vc_ALWAYS_INLINE Vc_PURE RowMemory &operator[](size_t i) {
return p()->m_mem[i];
}
Vc_ALWAYS_INLINE Vc_PURE const RowMemory &operator[](size_t i) const {
return p()->m_mem[i];
}
Vc_ALWAYS_INLINE Vc_PURE size_t rowsCount() const { return p()->rowsCount(); }
};
template<typename V, typename Parent, int Dimension, typename RowMemory> class MemoryBase : public MemoryDimensionBase<V, Parent, Dimension, RowMemory>
{
static_assert((V::size() * sizeof(typename V::EntryType)) % V::MemoryAlignment == 0,
"Vc::Memory can only be used for data-parallel types storing a number "
"of values that's a multiple of the memory alignment.");
private:
Parent *p() { return static_cast<Parent *>(this); }
const Parent *p() const { return static_cast<const Parent *>(this); }
template <class Flags>
using vector_reference = MayAlias<MemoryVector<V, Flags>> &;
template <class Flags>
using const_vector_reference = const MayAlias<MemoryVector<const V, Flags>> &;
public:
typedef typename V::EntryType EntryType;
Vc_ALWAYS_INLINE Vc_PURE size_t entriesCount() const { return p()->entriesCount(); }
Vc_ALWAYS_INLINE Vc_PURE size_t vectorsCount() const { return p()->vectorsCount(); }
using MemoryDimensionBase<V, Parent, Dimension, RowMemory>::entries;
using MemoryDimensionBase<V, Parent, Dimension, RowMemory>::scalar;
template<typename Flags = AlignedTag>
Vc_ALWAYS_INLINE MemoryVectorIterator< V, Flags> begin(Flags flags = Flags()) { return &firstVector(flags); }
template<typename Flags = AlignedTag>
Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags> begin(Flags flags = Flags()) const { return &firstVector(flags); }
template<typename Flags = AlignedTag>
Vc_ALWAYS_INLINE MemoryVectorIterator< V, Flags> end(Flags flags = Flags()) { return &lastVector(flags) + 1; }
template<typename Flags = AlignedTag>
Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags> end(Flags flags = Flags()) const { return &lastVector(flags) + 1; }
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE
typename std::enable_if<!std::is_convertible<Flags, int>::value,
vector_reference<Flags>>::type
vector(size_t i, Flags = Flags())
{
return *aliasing_cast<MemoryVector<V, Flags>>(&entries()[i * V::Size]);
}
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE
typename std::enable_if<!std::is_convertible<Flags, int>::value,
const_vector_reference<Flags>>::type
vector(size_t i, Flags = Flags()) const
{
return *aliasing_cast<MemoryVector<const V, Flags>>(&entries()[i * V::Size]);
}
template <typename Flags = UnalignedTag>
Vc_ALWAYS_INLINE Vc_PURE vector_reference<Flags> vectorAt(size_t i,
Flags flags = Flags())
{
return *aliasing_cast<MemoryVector<V, Flags>>(&entries()[i]);
}
template <typename Flags = UnalignedTag>
Vc_ALWAYS_INLINE Vc_PURE const_vector_reference<Flags> vectorAt(
size_t i, Flags flags = Flags()) const
{
return *aliasing_cast<MemoryVector<const V, Flags>>(&entries()[i]);
}
template <typename ShiftT, typename Flags = decltype(Unaligned)>
Vc_ALWAYS_INLINE Vc_PURE typename std::enable_if<
std::is_convertible<ShiftT, int>::value,
vector_reference<decltype(std::declval<Flags>() | Unaligned)>>::type
vector(size_t i, ShiftT shift, Flags = Flags())
{
return *aliasing_cast<
MemoryVector<V, decltype(std::declval<Flags>() | Unaligned)>>(
&entries()[i * V::Size + shift]);
}
template <typename ShiftT, typename Flags = decltype(Unaligned)>
Vc_ALWAYS_INLINE Vc_PURE typename std::enable_if<
std::is_convertible<ShiftT, int>::value,
const_vector_reference<decltype(std::declval<Flags>() | Unaligned)>>::type
vector(size_t i, ShiftT shift, Flags = Flags()) const
{
return *aliasing_cast<
MemoryVector<const V, decltype(std::declval<Flags>() | Unaligned)>>(
&entries()[i * V::Size + shift]);
}
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE vector_reference<Flags> firstVector(Flags f = Flags())
{
return vector(0, f);
}
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE const_vector_reference<Flags> firstVector(
Flags f = Flags()) const
{
return vector(0, f);
}
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE vector_reference<Flags> lastVector(Flags f = Flags())
{
return vector(vectorsCount() - 1, f);
}
template <typename Flags = AlignedTag>
Vc_ALWAYS_INLINE Vc_PURE const_vector_reference<Flags> lastVector(
Flags f = Flags()) const
{
return vector(vectorsCount() - 1, f);
}
Vc_ALWAYS_INLINE Vc_PURE V gather(const unsigned char *indexes) const { return V(entries(), typename V::IndexType(indexes, Vc::Unaligned)); }
Vc_ALWAYS_INLINE Vc_PURE V gather(const unsigned short *indexes) const { return V(entries(), typename V::IndexType(indexes, Vc::Unaligned)); }
Vc_ALWAYS_INLINE Vc_PURE V gather(const unsigned int *indexes) const { return V(entries(), typename V::IndexType(indexes, Vc::Unaligned)); }
Vc_ALWAYS_INLINE Vc_PURE V gather(const unsigned long *indexes) const { return V(entries(), typename V::IndexType(indexes, Vc::Unaligned)); }
Vc_ALWAYS_INLINE void setZero() {
V zero(Vc::Zero);
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) = zero;
}
}
template<typename U>
Vc_ALWAYS_INLINE Parent &operator=(U &&x) {
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) = std::forward<U>(x);
}
}
template<typename P2, typename RM>
inline Parent &operator+=(const MemoryBase<V, P2, Dimension, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) += rhs.vector(i);
}
return static_cast<Parent &>(*this);
}
template<typename P2, typename RM>
inline Parent &operator-=(const MemoryBase<V, P2, Dimension, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) -= rhs.vector(i);
}
return static_cast<Parent &>(*this);
}
template<typename P2, typename RM>
inline Parent &operator*=(const MemoryBase<V, P2, Dimension, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) *= rhs.vector(i);
}
return static_cast<Parent &>(*this);
}
template<typename P2, typename RM>
inline Parent &operator/=(const MemoryBase<V, P2, Dimension, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) /= rhs.vector(i);
}
return static_cast<Parent &>(*this);
}
inline Parent &operator+=(EntryType rhs) {
V v(rhs);
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) += v;
}
return static_cast<Parent &>(*this);
}
inline Parent &operator-=(EntryType rhs) {
V v(rhs);
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) -= v;
}
return static_cast<Parent &>(*this);
}
inline Parent &operator*=(EntryType rhs) {
V v(rhs);
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) *= v;
}
return static_cast<Parent &>(*this);
}
inline Parent &operator/=(EntryType rhs) {
V v(rhs);
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) /= v;
}
return static_cast<Parent &>(*this);
}
template<typename P2, typename RM>
inline bool operator==(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) == V(rhs.vector(i))).isFull()) {
return false;
}
}
return true;
}
template<typename P2, typename RM>
inline bool operator!=(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) == V(rhs.vector(i))).isEmpty()) {
return false;
}
}
return true;
}
template<typename P2, typename RM>
inline bool operator<(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) < V(rhs.vector(i))).isFull()) {
return false;
}
}
return true;
}
template<typename P2, typename RM>
inline bool operator<=(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) <= V(rhs.vector(i))).isFull()) {
return false;
}
}
return true;
}
template<typename P2, typename RM>
inline bool operator>(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) > V(rhs.vector(i))).isFull()) {
return false;
}
}
return true;
}
template<typename P2, typename RM>
inline bool operator>=(const MemoryBase<V, P2, Dimension, RM> &rhs) const {
assert(vectorsCount() == rhs.vectorsCount());
for (size_t i = 0; i < vectorsCount(); ++i) {
if (!(V(vector(i)) >= V(rhs.vector(i))).isFull()) {
return false;
}
}
return true;
}
};
namespace Detail
{
template <typename V,
typename ParentL,
typename ParentR,
int Dimension,
typename RowMemoryL,
typename RowMemoryR>
inline void copyVectors(MemoryBase<V, ParentL, Dimension, RowMemoryL> &dst,
const MemoryBase<V, ParentR, Dimension, RowMemoryR> &src)
{
const size_t vectorsCount = dst.vectorsCount();
size_t i = 3;
for (; i < vectorsCount; i += 4) {
const V tmp3 = src.vector(i - 3);
const V tmp2 = src.vector(i - 2);
const V tmp1 = src.vector(i - 1);
const V tmp0 = src.vector(i - 0);
dst.vector(i - 3) = tmp3;
dst.vector(i - 2) = tmp2;
dst.vector(i - 1) = tmp1;
dst.vector(i - 0) = tmp0;
}
for (i -= 3; i < vectorsCount; ++i) {
dst.vector(i) = src.vector(i);
}
}
}
}
}
#endif
#include <assert.h>
#include <algorithm>
#include <cstring>
#include <cstddef>
#include <initializer_list>
#ifndef VC_COMMON_MALLOC_H_
#define VC_COMMON_MALLOC_H_
#ifndef Vc_VECTOR_DECLARED_
#error "Incorrect inclusion order. This header must be included from Vc/vector.h only."
#endif
#if defined _WIN32 || defined _WIN64
#include <malloc.h>
#else
#include <cstdlib>
#endif
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
template <size_t X> static constexpr size_t nextMultipleOf(size_t value)
{
return (value % X) > 0 ? value + X - (value % X) : value;
}
template <std::size_t alignment> Vc_INTRINSIC void *aligned_malloc(std::size_t n)
{
#ifdef __MIC__
return _mm_malloc(nextMultipleOf<alignment>(n), alignment);
#elif defined(_WIN32)
# ifdef __GNUC__
return __mingw_aligned_malloc(nextMultipleOf<alignment>(n), alignment);
# else
return _aligned_malloc(nextMultipleOf<alignment>(n), alignment);
# endif
#else
void *ptr = nullptr;
if (0 == posix_memalign(&ptr, alignment < sizeof(void *) ? sizeof(void *) : alignment,
nextMultipleOf<alignment>(n))) {
return ptr;
}
return ptr;
#endif
}
template <Vc::MallocAlignment A> Vc_ALWAYS_INLINE void *malloc(size_t n)
{
switch (A) {
case Vc::AlignOnVector:
return aligned_malloc<Vc::VectorAlignment>(n);
case Vc::AlignOnCacheline:
return aligned_malloc<64>(n);
case Vc::AlignOnPage:
return aligned_malloc<4096>(n);
}
return nullptr;
}
Vc_ALWAYS_INLINE void free(void *p)
{
#ifdef __MIC__
_mm_free(p);
#elif defined(_WIN32)
# ifdef __GNUC__
return __mingw_aligned_free(p);
# else
return _aligned_free(p);
# endif
#else
std::free(p);
#endif
}
}
template<typename T, Vc::MallocAlignment A>
Vc_ALWAYS_INLINE T *malloc(size_t n)
{
return static_cast<T *>(Common::malloc<A>(n * sizeof(T)));
}
template<typename T>
Vc_ALWAYS_INLINE void free(T *p)
{
Common::free(p);
}
}
#endif
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
template<typename V, size_t Size> struct _MemorySizeCalculation
{
enum AlignmentCalculations {
Alignment = V::Size,
AlignmentMask = Alignment - 1,
MaskedSize = Size & AlignmentMask,
Padding = Alignment - MaskedSize,
PaddedSize = MaskedSize == 0 ? Size : Size + Padding
};
};
template <typename V, size_t Size1, size_t Size2, bool InitPadding>
class Memory : public MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
Memory<V, Size2, 0, InitPadding>>
{
public:
typedef typename V::EntryType EntryType;
private:
using RowMemory = Memory<V, Size2, 0, InitPadding>;
typedef MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, RowMemory> Base;
friend class MemoryBase<V, Memory<V, Size1, Size2, InitPadding>, 2, RowMemory>;
friend class MemoryDimensionBase<V, Memory<V, Size1, Size2, InitPadding>, 2,
RowMemory>;
enum : size_t {
Alignment = V::MemoryAlignment,
PaddedSize2 = _MemorySizeCalculation<V, Size2>::PaddedSize
};
alignas(static_cast<size_t>(Alignment))
RowMemory m_mem[Size1];
public:
using Base::vector;
enum Constants {
RowCount = Size1,
VectorsCount = PaddedSize2 / V::Size
};
Memory() = default;
static constexpr size_t rowsCount() { return RowCount; }
static constexpr size_t entriesCount() { return Size1 * Size2; }
static constexpr size_t vectorsCount() { return VectorsCount * Size1; }
template<typename Parent, typename RM>
Vc_ALWAYS_INLINE Memory &operator=(const MemoryBase<V, Parent, 2, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
Detail::copyVectors(*this, rhs);
return *this;
}
Vc_ALWAYS_INLINE Memory &operator=(const Memory &rhs) {
Detail::copyVectors(*this, rhs);
return *this;
}
inline Memory &operator=(const V &v) {
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) = v;
}
return *this;
}
};
template <typename V, size_t Size, bool InitPadding>
class Memory<V, Size, 0u, InitPadding> :
public MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>
{
public:
typedef typename V::EntryType EntryType;
private:
typedef MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void> Base;
friend class MemoryBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>;
friend class MemoryDimensionBase<V, Memory<V, Size, 0u, InitPadding>, 1, void>;
enum : size_t {
Alignment = V::MemoryAlignment,
MaskedSize = Size & (V::Size - 1),
Padding = V::Size - MaskedSize,
PaddedSize = MaskedSize == 0 ? Size : Size + Padding
};
alignas(static_cast<size_t>(Alignment))
EntryType m_mem[PaddedSize];
public:
using Base::vector;
enum Constants {
EntriesCount = Size,
VectorsCount = PaddedSize / V::Size
};
Memory()
{
if (InitPadding) {
Base::lastVector() = V::Zero();
}
}
Memory(std::initializer_list<EntryType> init)
{
Vc_ASSERT(init.size() <= Size);
Base::lastVector() = V::Zero();
std::copy(init.begin(), init.end(), &m_mem[0]);
}
static Vc_ALWAYS_INLINE Vc_CONST Memory<V, Size, 0u, false> &fromRawData(EntryType *ptr)
{
char *addr = reinterpret_cast<char *>(ptr);
typedef Memory<V, Size, 0u, false> MM;
addr -= offsetof(MM, m_mem);
return *new(addr) MM;
}
static constexpr size_t entriesCount() { return EntriesCount; }
static constexpr size_t vectorsCount() { return VectorsCount; }
inline Memory(const Memory &rhs)
{
Detail::copyVectors(*this, rhs);
}
template <size_t S> inline Memory(const Memory<V, S> &rhs)
{
assert(vectorsCount() == rhs.vectorsCount());
Detail::copyVectors(*this, rhs);
}
inline Memory &operator=(const Memory &rhs)
{
Detail::copyVectors(*this, rhs);
return *this;
}
template <size_t S> inline Memory &operator=(const Memory<V, S> &rhs)
{
assert(vectorsCount() == rhs.vectorsCount());
Detail::copyVectors(*this, rhs);
return *this;
}
Vc_ALWAYS_INLINE Memory &operator=(const EntryType *rhs) {
std::memcpy(m_mem, rhs, entriesCount() * sizeof(EntryType));
return *this;
}
inline Memory &operator=(const V &v) {
for (size_t i = 0; i < vectorsCount(); ++i) {
vector(i) = v;
}
return *this;
}
};
template<typename V> class Memory<V, 0u, 0u, true> : public MemoryBase<V, Memory<V, 0u, 0u, true>, 1, void>
{
public:
typedef typename V::EntryType EntryType;
private:
typedef MemoryBase<V, Memory<V>, 1, void> Base;
friend class MemoryBase<V, Memory<V>, 1, void>;
friend class MemoryDimensionBase<V, Memory<V>, 1, void>;
enum InternalConstants {
Alignment = V::Size,
AlignmentMask = Alignment - 1
};
size_t m_entriesCount;
size_t m_vectorsCount;
EntryType *m_mem;
size_t calcPaddedEntriesCount(size_t x)
{
size_t masked = x & AlignmentMask;
return (masked == 0 ? x : x + (Alignment - masked));
}
public:
using Base::vector;
Vc_ALWAYS_INLINE Memory(size_t size)
: m_entriesCount(size),
m_vectorsCount(calcPaddedEntriesCount(m_entriesCount)),
m_mem(Vc::malloc<EntryType, Vc::AlignOnVector>(m_vectorsCount))
{
m_vectorsCount /= V::Size;
Base::lastVector() = V::Zero();
}
template<typename Parent, typename RM>
Vc_ALWAYS_INLINE Memory(const MemoryBase<V, Parent, 1, RM> &rhs)
: m_entriesCount(rhs.entriesCount()),
m_vectorsCount(rhs.vectorsCount()),
m_mem(Vc::malloc<EntryType, Vc::AlignOnVector>(m_vectorsCount * V::Size))
{
Detail::copyVectors(*this, rhs);
}
Vc_ALWAYS_INLINE Memory(const Memory &rhs)
: m_entriesCount(rhs.entriesCount()),
m_vectorsCount(rhs.vectorsCount()),
m_mem(Vc::malloc<EntryType, Vc::AlignOnVector>(m_vectorsCount * V::Size))
{
Detail::copyVectors(*this, rhs);
}
Vc_ALWAYS_INLINE ~Memory()
{
Vc::free(m_mem);
}
inline void swap(Memory &rhs) {
std::swap(m_mem, rhs.m_mem);
std::swap(m_entriesCount, rhs.m_entriesCount);
std::swap(m_vectorsCount, rhs.m_vectorsCount);
}
Vc_ALWAYS_INLINE Vc_PURE size_t entriesCount() const { return m_entriesCount; }
Vc_ALWAYS_INLINE Vc_PURE size_t vectorsCount() const { return m_vectorsCount; }
template<typename Parent, typename RM>
Vc_ALWAYS_INLINE Memory &operator=(const MemoryBase<V, Parent, 1, RM> &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
Detail::copyVectors(*this, rhs);
return *this;
}
Vc_ALWAYS_INLINE Memory &operator=(const Memory &rhs) {
assert(vectorsCount() == rhs.vectorsCount());
Detail::copyVectors(*this, rhs);
return *this;
}
Vc_ALWAYS_INLINE Memory &operator=(const EntryType *rhs) {
std::memcpy(m_mem, rhs, entriesCount() * sizeof(EntryType));
return *this;
}
};
Vc_ALWAYS_INLINE void prefetchForOneRead(const void *addr)
{
Vc::Detail::prefetchForOneRead(addr, VectorAbi::Best<float>());
}
Vc_ALWAYS_INLINE void prefetchForModify(const void *addr)
{
Vc::Detail::prefetchForModify(addr, VectorAbi::Best<float>());
}
Vc_ALWAYS_INLINE void prefetchClose(const void *addr)
{
Vc::Detail::prefetchClose(addr, VectorAbi::Best<float>());
}
Vc_ALWAYS_INLINE void prefetchMid(const void *addr)
{
Vc::Detail::prefetchMid(addr, VectorAbi::Best<float>());
}
Vc_ALWAYS_INLINE void prefetchFar(const void *addr)
{
Vc::Detail::prefetchFar(addr, VectorAbi::Best<float>());
}
}
using Common::Memory;
using Common::prefetchForOneRead;
using Common::prefetchForModify;
using Common::prefetchClose;
using Common::prefetchMid;
using Common::prefetchFar;
}
namespace std
{
template<typename V> Vc_ALWAYS_INLINE void swap(Vc::Memory<V> &a, Vc::Memory<V> &b) { a.swap(b); }
}
#endif
#ifndef VC_COMMON_MAKE_UNIQUE_H_
#define VC_COMMON_MAKE_UNIQUE_H_
#include <memory>
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
template<typename T> struct Deleter
{
Vc_ALWAYS_INLINE void operator()(T *ptr) {
ptr->~T();
Vc::free(ptr);
}
};
template<class T, MallocAlignment A = Vc::AlignOnVector, class... Args>
inline std::unique_ptr<T, Deleter<T>> make_unique(Args&&... args)
{
return std::unique_ptr<T, Deleter<T>>(new(Vc::malloc<T, A>(1)) T(std::forward<Args>(args)...));
}
}
}
#endif
namespace Vc_VERSIONED_NAMESPACE
{
using Common::make_unique;
}
#endif
#ifndef VC_UTILS_
#define VC_UTILS_
#ifdef Vc_IMPL_Scalar
#define VECTOR_NAMESPACE Scalar
#else
#define VECTOR_NAMESPACE SSE
#endif
#ifndef VC_COMMON_DEINTERLEAVE_H_
#define VC_COMMON_DEINTERLEAVE_H_
namespace Vc_VERSIONED_NAMESPACE
{
template<typename V, typename M, typename A> Vc_ALWAYS_INLINE void deinterleave(V *a, V *b,
const M *memory, A align)
{
Detail::deinterleave(*a, *b, memory, align);
}
template<typename V, typename M> Vc_ALWAYS_INLINE void deinterleave(V *a, V *b,
const M *memory)
{
Detail::deinterleave(*a, *b, memory, Aligned);
}
}
#endif
#ifndef VC_COMMON_MAKECONTAINER_H_
#define VC_COMMON_MAKECONTAINER_H_
#include <initializer_list>
namespace Vc_VERSIONED_NAMESPACE
{
namespace
{
template<typename Container, typename T> struct make_container_helper
{
static constexpr Container help(std::initializer_list<T> list) { return { list }; }
};
template <typename T_, typename Abi, typename Alloc,
template <class, class> class Container>
struct make_container_helper<Container<Vector<T_, Abi>, Alloc>,
typename Vector<T_, Abi>::EntryType> {
typedef Vector<T_, Abi> V;
typedef typename V::EntryType T;
typedef Container<V, Alloc> C;
static inline C help(std::initializer_list<T> list) {
const std::size_t size = (list.size() + (V::Size - 1)) / V::Size;
C v(size);
auto containerIt = v.begin();
auto init = std::begin(list);
const auto initEnd = std::end(list);
for (std::size_t i = 0; i < size - 1; ++i) {
*containerIt++ = V(init, Vc::Unaligned);
init += V::Size;
}
Vc_ASSERT(all_of(*containerIt == V::Zero()));
int j = 0;
while (init != initEnd) {
(*containerIt)[j++] = *init++;
}
return v;
}
};
template <typename T_, typename Abi, std::size_t N,
template <class, std::size_t> class Container>
struct make_container_helper<Container<Vector<T_, Abi>, N>,
typename Vector<T_, Abi>::EntryType> {
typedef Vector<T_, Abi> V;
typedef typename V::EntryType T;
static constexpr std::size_t size = (N + (V::Size - 1)) / V::Size;
typedef Container<
V,
#if defined Vc_CLANG && Vc_CLANG < 0x30700
(size == 1 && std::is_same<Abi, VectorAbi::Avx>::value) ? 2 :
#endif
size> C;
static inline C help(std::initializer_list<T> list) {
Vc_ASSERT(N == list.size())
Vc_ASSERT(size == (list.size() + (V::Size - 1)) / V::Size)
C v;
auto containerIt = v.begin();
auto init = std::begin(list);
const auto initEnd = std::end(list);
for (std::size_t i = 0; i < size - 1; ++i) {
*containerIt++ = V(init, Vc::Unaligned);
init += V::Size;
}
Vc_ASSERT(all_of(*containerIt == V::Zero()));
int j = 0;
while (init != initEnd) {
(*containerIt)[j++] = *init++;
}
return v;
}
};
}
template<typename Container, typename T>
constexpr auto makeContainer(std::initializer_list<T> list) -> decltype(make_container_helper<Container, T>::help(list))
{
return make_container_helper<Container, T>::help(list);
}
template<typename Container, typename T>
constexpr auto make_container(std::initializer_list<T> list) -> decltype(makeContainer<Container, T>(list))
{
return makeContainer<Container, T>(list);
}
}
#endif
#endif
#ifndef VC_INCLUDE_VC_ITERATORS_H_
#define VC_INCLUDE_VC_ITERATORS_H_
#ifndef VC_COMMON_ITERATORS_H_
#define VC_COMMON_ITERATORS_H_
#include <array>
#include <iterator>
#ifdef Vc_MSVC
#include <intrin.h>
#endif
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
template<typename _V, typename Flags> class MemoryVector;
template<typename _V, typename Flags> class MemoryVectorIterator;
template <typename V> class Iterator;
template <typename V, bool> class IteratorBase;
template <typename V> class IteratorBase<V, true>
{
public:
using iterator_category = std::input_iterator_tag;
using value_type = typename V::value_type;
using difference_type = int;
using reference = value_type;
Vc_ALWAYS_INLINE reference operator*() const { return v()[i()]; }
Vc_ALWAYS_INLINE reference operator[](difference_type i2) const { return v()[i2]; }
private:
Vc_INTRINSIC V &v() const { return *static_cast<const Iterator<V> *>(this)->v; }
Vc_INTRINSIC difference_type i() const
{
return static_cast<const Iterator<V> *>(this)->i;
}
};
template <typename V> class IteratorBase<V, false>
{
public:
using iterator_category = std::input_iterator_tag;
using value_type = typename V::value_type;
using difference_type = int;
using reference = Vc::Detail::ElementReference<V, IteratorBase>;
Vc_ALWAYS_INLINE reference operator*() const { return {*v(), i()}; }
Vc_ALWAYS_INLINE reference operator[](difference_type i2) const { return {*v(), i2}; }
private:
Vc_INTRINSIC V *v() const { return static_cast<const Iterator<V> *>(this)->v; }
Vc_INTRINSIC difference_type i() const
{
return static_cast<const Iterator<V> *>(this)->i;
}
friend reference;
static Vc_INTRINSIC value_type get(const V &o, int i)
{
return o[i];
}
template <typename T> static Vc_INTRINSIC void set(V &o, int i, T &&v)
{
o[i] = std::forward<T>(v);
}
};
template <typename V> class Iterator : public IteratorBase<V, std::is_const<V>::value>
{
using Base = IteratorBase<V, std::is_const<V>::value>;
friend Base;
public:
using typename Base::iterator_category;
using typename Base::value_type;
using typename Base::difference_type;
using pointer = const Iterator *;
using typename Base::reference;
constexpr Iterator() = default;
constexpr Iterator(V &_v, difference_type _i) : v(&_v), i(_i) {}
Vc_ALWAYS_INLINE pointer operator->() const { return this; }
using Base::operator*;
Vc_ALWAYS_INLINE Iterator &operator++() { ++i; return *this; }
Vc_ALWAYS_INLINE Iterator operator++(int) { Iterator tmp = *this; ++i; return tmp; }
Vc_ALWAYS_INLINE Iterator &operator--() { --i; return *this; }
Vc_ALWAYS_INLINE Iterator operator--(int) { Iterator tmp = *this; --i; return tmp; }
using Base::operator[];
Vc_ALWAYS_INLINE Iterator &operator+=(difference_type d) { i += d; return *this; }
Vc_ALWAYS_INLINE Iterator &operator-=(difference_type d) { i -= d; return *this; }
Vc_ALWAYS_INLINE Iterator operator+(difference_type d) const { return {*v, i + d}; }
Vc_ALWAYS_INLINE Iterator operator-(difference_type d) const { return {*v, i - d}; }
Vc_ALWAYS_INLINE difference_type operator-(const Iterator &rhs) const { return i - rhs.i; }
friend Vc_ALWAYS_INLINE Iterator operator+(difference_type d, const Iterator &rhs)
{
return {*rhs.v, rhs.i + d};
}
Vc_ALWAYS_INLINE bool operator==(const Iterator<V> &rhs) const { return v == rhs.v && i == rhs.i; }
Vc_ALWAYS_INLINE bool operator!=(const Iterator<V> &rhs) const { return v == rhs.v && i != rhs.i; }
Vc_ALWAYS_INLINE bool operator< (const Iterator<V> &rhs) const { return v == rhs.v && i < rhs.i; }
Vc_ALWAYS_INLINE bool operator<=(const Iterator<V> &rhs) const { return v == rhs.v && i <= rhs.i; }
Vc_ALWAYS_INLINE bool operator> (const Iterator<V> &rhs) const { return v == rhs.v && i > rhs.i; }
Vc_ALWAYS_INLINE bool operator>=(const Iterator<V> &rhs) const { return v == rhs.v && i >= rhs.i; }
private:
V *v = nullptr;
difference_type i = 0;
};
template <typename V> using ConstIterator = Iterator<const V>;
class BitmaskIterator
{
#ifdef Vc_MSVC
unsigned long mask;
unsigned long bit;
#else
size_t mask;
size_t bit;
#endif
void nextBit()
{
#ifdef Vc_GNU_ASM
bit = __builtin_ctzl(mask);
#elif defined(Vc_MSVC)
_BitScanForward(&bit, mask);
#else
#error "Not implemented yet. Please contact vc-devel@compeng.uni-frankfurt.de"
#endif
}
void resetLsb()
{
mask &= (mask - 1);
}
public:
BitmaskIterator(decltype(mask) m) : mask(m) { nextBit(); }
BitmaskIterator(const BitmaskIterator &) = default;
BitmaskIterator(BitmaskIterator &&) = default;
Vc_ALWAYS_INLINE size_t operator->() const { return bit; }
Vc_ALWAYS_INLINE size_t operator*() const { return bit; }
Vc_ALWAYS_INLINE BitmaskIterator &operator++() { resetLsb(); nextBit(); return *this; }
Vc_ALWAYS_INLINE BitmaskIterator operator++(int) { BitmaskIterator tmp = *this; resetLsb(); nextBit(); return tmp; }
Vc_ALWAYS_INLINE bool operator==(const BitmaskIterator &rhs) const { return mask == rhs.mask; }
Vc_ALWAYS_INLINE bool operator!=(const BitmaskIterator &rhs) const { return mask != rhs.mask; }
};
template <typename T>
Vc_ALWAYS_INLINE
enable_if<Traits::is_simd_vector<T>::value || Traits::is_simd_mask<T>::value,
Iterator<typename std::remove_reference<T>::type>>
begin(T &&x)
{
return {std::forward<T>(x), 0};
}
template <typename T>
Vc_ALWAYS_INLINE
enable_if<Traits::is_simd_vector<T>::value || Traits::is_simd_mask<T>::value,
Iterator<typename std::remove_reference<T>::type>>
end(T &&x)
{
using TT = typename std::decay<T>::type;
return {std::forward<T>(x), int(TT::size())};
}
template <typename T>
Vc_ALWAYS_INLINE enable_if<
Traits::is_simd_mask<T>::value || Traits::is_simd_vector<T>::value, ConstIterator<T>>
cbegin(const T &v)
{
return {v, 0};
}
template <typename T>
Vc_ALWAYS_INLINE enable_if<
Traits::is_simd_mask<T>::value || Traits::is_simd_vector<T>::value, ConstIterator<T>>
cend(const T &v)
{
return {v, int(T::size())};
}
template<typename M> Vc_ALWAYS_INLINE BitmaskIterator begin(const WhereImpl::WhereMask<M> &w)
{
return w.mask.toInt();
}
template<typename M> Vc_ALWAYS_INLINE BitmaskIterator end(const WhereImpl::WhereMask<M> &)
{
return 0;
}
template<typename V, typename Flags, typename T> Vc_ALWAYS_INLINE MemoryVectorIterator<V, Flags>
makeIterator(T *mem, Flags)
{
return new(mem) MemoryVector<V, Flags>;
}
template<typename V, typename Flags, typename T> Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags>
makeIterator(const T *mem, Flags)
{
return new(const_cast<T *>(mem)) MemoryVector<const V, Flags>;
}
template<typename V, typename Flags, typename FlagsX> Vc_ALWAYS_INLINE MemoryVectorIterator<V, Flags>
makeIterator(MemoryVector<V, FlagsX> &mv, Flags)
{
return new(&mv) MemoryVector<V, Flags>;
}
template<typename V, typename Flags, typename FlagsX> Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags>
makeIterator(MemoryVector<const V, FlagsX> &mv, Flags)
{
return new(&mv) MemoryVector<const V, Flags>;
}
}
using Common::begin;
using Common::end;
using Common::cbegin;
using Common::cend;
using Common::makeIterator;
}
#endif
namespace Vc_VERSIONED_NAMESPACE
{
using ::Vc::Common::begin;
using ::Vc::Common::end;
using ::Vc::Common::makeIterator;
}
#endif
#ifndef VC_INCLUDE_VC_ARRAY_
#define VC_INCLUDE_VC_ARRAY_
#include <type_traits>
#include <utility>
#include <iterator>
#include <algorithm>
#include <stdexcept>
#ifndef VC_COMMON_SUBSCRIPT_H_
#define VC_COMMON_SUBSCRIPT_H_
#include <initializer_list>
#include <type_traits>
#include <vector>
#include <assert.h>
namespace Vc_VERSIONED_NAMESPACE
{
namespace Common
{
template <typename Base> class AdaptSubscriptOperator : public Base
{
public:
template <typename... Args>
Vc_ALWAYS_INLINE AdaptSubscriptOperator(Args &&... arguments)
: Base(std::forward<Args>(arguments)...)
{
}
template <typename T>
Vc_ALWAYS_INLINE AdaptSubscriptOperator(std::initializer_list<T> l)
: Base(l)
{
}
using Base::operator[];
template <typename I,
typename = enable_if<!std::is_arithmetic<
typename std::decay<I>::type>::value>
>
Vc_ALWAYS_INLINE auto operator[](I &&arg_)
-> decltype(subscript_operator(*this, std::forward<I>(arg_)))
{
return subscript_operator(*this, std::forward<I>(arg_));
}
template <typename I, typename = enable_if<
!std::is_arithmetic<typename std::decay<I>::type>::value>>
Vc_ALWAYS_INLINE auto operator[](I &&arg_) const
-> decltype(subscript_operator(*this, std::forward<I>(arg_)))
{
return subscript_operator(*this, std::forward<I>(arg_));
}
};
template <class T, class = decltype(convertIndexVector(std::declval<T>()))>
std::true_type is_valid_indexvector(T &&);
std::false_type is_valid_indexvector(...);
template <class IndexVector, class Test = decltype(is_valid_indexvector(
std::declval<const IndexVector &>()))>
struct is_valid_indexvector_ : public std::integral_constant<bool, Test::value> {
};
static_assert(!is_valid_indexvector_<const int *>::value,
"Pointer is incorrectly classified as valid index vector type");
static_assert(is_valid_indexvector_<const int[4]>::value,
"C-Array is incorrectly classified as invalid index vector type");
template <typename Scale, typename T>
Vc_ALWAYS_INLINE enable_if<Scale::num == Scale::den, Traits::decay<T>> applyScale(T &&x)
{
return std::forward<T>(x);
}
template <typename Scale, typename T>
Vc_ALWAYS_INLINE enable_if<
Scale::num != Scale::den && Traits::has_multiply_operator<T, int>::value,
Traits::decay<T>>
applyScale(T &&x)
{
static_assert(Scale::num % Scale::den == 0,
"Non-integral index scaling requested. This typically happens only for "
"Vc::Scalar on 32-bit for gathers on double. You can work around the "
"issue by ensuring that all doubles in the structure are aligned on 8 "
"Bytes.");
constexpr int value = Scale::num / Scale::den;
Vc_ASSERT(Vc::all_of((x * value) / value == x));
return std::forward<T>(x) * value;
}
template <typename Scale, typename T>
Vc_ALWAYS_INLINE enable_if<
Scale::num != Scale::den && !Traits::has_multiply_operator<T, int>::value,
T>
applyScale(T x)
{
static_assert(Scale::num % Scale::den == 0,
"Non-integral index scaling requested. This typically happens only for "
"Vc::Scalar on 32-bit for gathers on double. You can work around the "
"issue by ensuring that all doubles in the structure are aligned on 8 "
"Bytes.");
constexpr int value = Scale::num / Scale::den;
for (size_t i = 0; i < x.size(); ++i) {
Vc_ASSERT((x[i] * value) / value == x[i]);
x[i] *= value;
}
return x;
}
template <typename Scale, typename T, typename U,
typename = enable_if<Traits::has_multiply_operator<T, int>::value &&
Traits::has_addition_operator<T, U>::value>>
Vc_ALWAYS_INLINE typename std::decay<T>::type applyScaleAndAdd(T &&x, U &&y)
{
constexpr int value = Scale::num / Scale::den;
if (value == 1) {
return std::forward<T>(x) + std::forward<U>(y);
}
return std::forward<T>(x) * value + std::forward<U>(y);
}
template <
typename Scale, typename T, typename U,
typename = enable_if<
!(Traits::has_multiply_operator<T &, int>::value &&
Traits::has_addition_operator<T &, decltype(std::declval<U>()[0])>::value) &&
Traits::has_subscript_operator<U>::value>>
Vc_ALWAYS_INLINE T applyScaleAndAdd(T x, U &&y)
{
constexpr int value = Scale::num / Scale::den;
for (size_t i = 0; i < x.size(); ++i) {
if (value == 1) {
x[i] = x[i] + y[i];
} else {
x[i] = x[i] * value + y[i];
}
}
return x;
}
template <typename Scale, typename T, typename U>
Vc_ALWAYS_INLINE enable_if<!(Traits::has_multiply_operator<T &, int>::value &&
Traits::has_addition_operator<T &, U>::value) &&
!Traits::has_subscript_operator<U>::value,
T>
applyScaleAndAdd(T x, U &&y)
{
constexpr int value = Scale::num / Scale::den;
for (size_t i = 0; i < x.size(); ++i) {
if (value == 1) {
x[i] = x[i] + y;
} else {
x[i] = x[i] * value + y;
}
}
return x;
}
template <std::size_t MinSize,
typename IndexT,
bool = Traits::is_simd_vector<IndexT>::value>
struct IndexVectorSizeMatches
: public std::true_type
{
};
template <std::size_t MinSize, typename V>
struct IndexVectorSizeMatches<MinSize,
V,
true> : public std::integral_constant<bool, (MinSize <= V::Size)>
{
};
template <std::size_t MinSize, typename T, std::size_t ArraySize>
struct IndexVectorSizeMatches<MinSize,
T[ArraySize],
false> : public std::integral_constant<bool, (MinSize <= ArraySize)>
{
};
template <std::size_t MinSize, typename T, std::size_t ArraySize>
struct IndexVectorSizeMatches<MinSize,
std::array<T, ArraySize>,
false> : public std::integral_constant<bool, (MinSize <= ArraySize)>
{
};
template <std::size_t MinSize, typename T, std::size_t ArraySize>
struct IndexVectorSizeMatches<MinSize,
Vc::array<T, ArraySize>,
false> : public std::integral_constant<bool, (MinSize <= ArraySize)>
{
};
template <std::size_t MinSize, typename T, std::ptrdiff_t N>
struct IndexVectorSizeMatches<MinSize, Vc::Common::span<T, N>, false>
: public std::integral_constant<bool, (N == -1 || static_cast<std::ptrdiff_t>(MinSize) <= N)> {
};
template <
typename T, typename IndexVector, typename Scale = std::ratio<1, 1>,
bool = is_valid_indexvector_<IndexVector>::value>
class SubscriptOperation
{
const IndexVector m_indexes;
T *const m_address;
using ScalarType = typename std::decay<T>::type;
using IndexVectorScaled = Traits::decay<decltype(convertIndexVector(std::declval<const IndexVector &>()))>;
public:
SubscriptOperation &operator=(const SubscriptOperation &) = delete;
SubscriptOperation(const SubscriptOperation &) = delete;
#ifndef __cpp_guaranteed_copy_elision
constexpr SubscriptOperation(SubscriptOperation &&) = default;
#endif
template <typename U,
typename = enable_if<((std::is_convertible<const U &, IndexVector>::value ||
std::is_same<U, IndexVector>::value) &&
std::is_copy_constructible<IndexVector>::value)>>
constexpr Vc_ALWAYS_INLINE SubscriptOperation(T *address, const U &indexes)
: m_indexes(indexes), m_address(address)
{
}
template <std::size_t... Indexes>
constexpr Vc_ALWAYS_INLINE SubscriptOperation(T *address, const IndexVector &indexes,
index_sequence<Indexes...>)
: m_indexes{indexes[Indexes]...}, m_address(address)
{}
template <typename U>
constexpr Vc_ALWAYS_INLINE SubscriptOperation(
T *address, const U &indexes,
enable_if<((std::is_convertible<const U &, IndexVector>::value ||
std::is_same<U, IndexVector>::value) &&
!std::is_copy_constructible<IndexVector>::value &&
std::is_array<IndexVector>::value &&
std::extent<IndexVector>::value > 0)> = nullarg)
: SubscriptOperation(address, indexes,
make_index_sequence<std::extent<IndexVector>::value>())
{
}
static constexpr bool need_explicit_scaling =
Scale::num % Scale::den != 0 || Scale::num / Scale::den * sizeof(T) > 8;
Vc_ALWAYS_INLINE
GatherArguments<typename std::remove_cv<T>::type, IndexVectorScaled,
(need_explicit_scaling ? 1 : Scale::num / Scale::den)>
gatherArguments() &&
{
static_assert(std::is_arithmetic<ScalarType>::value,
"Incorrect type for a SIMD vector gather. Must be an arithmetic type.");
return {applyScale<typename std::conditional<need_explicit_scaling, Scale,
std::ratio<1, 1>>::type>(
convertIndexVector(m_indexes)),
m_address};
}
Vc_ALWAYS_INLINE ScatterArguments<T, IndexVectorScaled> scatterArguments() &&
{
static_assert(std::is_arithmetic<ScalarType>::value,
"Incorrect type for a SIMD vector scatter. Must be an arithmetic type.");
return {applyScale<Scale>(convertIndexVector(m_indexes)), m_address};
}
template <typename V,
typename = enable_if<(std::is_arithmetic<ScalarType>::value &&Traits::is_simd_vector<
V>::value &&IndexVectorSizeMatches<V::Size, IndexVector>::value)>>
Vc_INTRINSIC operator V() &&
{
return V(static_cast<SubscriptOperation &&>(*this).gatherArguments());
}
template <typename V,
typename = enable_if<(std::is_arithmetic<ScalarType>::value &&Traits::is_simd_vector<
V>::value &&IndexVectorSizeMatches<V::Size, IndexVector>::value)>>
Vc_ALWAYS_INLINE SubscriptOperation &operator=(const V &rhs) &&
{
static_assert(std::is_arithmetic<ScalarType>::value,
"Incorrect type for a SIMD vector scatter. Must be an arithmetic type.");
const auto indexes = applyScale<Scale>(convertIndexVector(m_indexes));
rhs.scatter(m_address, indexes);
return *this;
}
template <
typename U,
typename S,
typename = enable_if<std::is_same<S, typename std::remove_cv<T>::type>::value &&(
std::is_class<T>::value || std::is_union<T>::value)>>
Vc_ALWAYS_INLINE auto operator[](U S::*member) &&
-> SubscriptOperation<
typename std::conditional<std::is_const<T>::value,
const typename std::remove_reference<U>::type,
typename std::remove_reference<U>::type>::type,
IndexVector,
std::ratio_multiply<Scale, std::ratio<sizeof(S), sizeof(U)>>>
{
static_assert(std::is_same<Traits::decay<decltype(m_address->*member)>,
Traits::decay<U>>::value,
"Type mismatch that should be impossible.");
return {&(m_address->*member), m_indexes};
}
private:
template <intmax_t N, intmax_t D> struct make_ratio {
using type = std::ratio<N, D == 0 ? 1 : D>;
};
public:
template <typename U>
Vc_ALWAYS_INLINE auto operator[](U index) && -> typename std::enable_if<
#ifndef Vc_IMPROVE_ERROR_MESSAGES
Traits::has_no_allocated_data<T>::value &&
#endif
std::is_convertible<U, size_t>::value,
SubscriptOperation<
typename std::remove_reference<decltype(m_address[0][index])>::type,
IndexVector,
std::ratio_multiply<
Scale,
typename make_ratio<sizeof(T), sizeof(m_address[0][index])>::type>>>::type
{
static_assert(Traits::has_subscript_operator<T>::value,
"The subscript operator was called on a type that does not implement it.\n");
static_assert(Traits::has_no_allocated_data<T>::value,
"Invalid container type in gather/scatter operation.\nYou may only use "
"nested containers that store the data inside the object (such as builtin "
"arrays or std::array) but not containers that store data in allocated "
"memory (such as std::vector).\nSince this feature cannot be queried "
"generically at compile time you need to spezialize the "
"Vc::Traits::has_no_allocated_data_impl<T> type-trait for custom types that "
"meet the requirements.\n");
static_assert(std::is_lvalue_reference<decltype(m_address[0][index])>::value,
"The container does not return an lvalue reference to the data at "
"the requested offset. This makes it impossible to execute a "
"gather operation.\n");
return {&(m_address[0][index]), m_indexes};
}
template <typename IT>
Vc_ALWAYS_INLINE typename std::enable_if<
#ifndef Vc_IMPROVE_ERROR_MESSAGES
Traits::has_no_allocated_data<T>::value &&
Traits::has_subscript_operator<T>::value &&
#endif
Traits::has_subscript_operator<IT>::value,
SubscriptOperation<typename std::remove_reference<decltype(
m_address[0][std::declval<
const IT &>()[0]]
)>::type,
IndexVectorScaled,
std::ratio<1, 1>
>>::type
operator[](const IT &index) &&
{
static_assert(Traits::has_subscript_operator<T>::value,
"The subscript operator was called on a type that does not implement it.\n");
static_assert(Traits::has_no_allocated_data<T>::value,
"Invalid container type in gather/scatter operation.\nYou may only use "
"nested containers that store the data inside the object (such as builtin "
"arrays or std::array) but not containers that store data in allocated "
"memory (such as std::vector).\nSince this feature cannot be queried "
"generically at compile time you need to spezialize the "
"Vc::Traits::has_no_allocated_data_impl<T> type-trait for custom types that "
"meet the requirements.\n");
return {&(m_address[0][0]),
applyScaleAndAdd<std::ratio_multiply<
Scale, std::ratio<sizeof(T), sizeof(m_address[0][0])>>>(
convertIndexVector(m_indexes), index)};
}
};
template <typename T, typename IndexVector, typename Scale>
class SubscriptOperation<T, IndexVector, Scale, false>;
template <
typename Container,
typename IndexVector,
typename = enable_if<
Traits::has_subscript_operator<IndexVector>::value
&&Traits::has_contiguous_storage<Container>::value
&&std::is_lvalue_reference<decltype(*begin(std::declval<
Container>()))>::value
>>
Vc_ALWAYS_INLINE SubscriptOperation<
typename std::remove_reference<decltype(*begin(std::declval<Container>()))>::
type,
typename std::remove_const<typename std::remove_reference<
IndexVector>::type>::type
> subscript_operator(Container &&c, IndexVector &&indexes)
{
Vc_ASSERT(std::addressof(*begin(c)) + 1 ==
std::addressof(*(begin(c) + 1)));
return {std::addressof(*begin(c)), std::forward<IndexVector>(indexes)};
}
template <typename Container, typename I>
Vc_ALWAYS_INLINE Vc::Common::SubscriptOperation<
typename std::remove_reference<decltype(std::declval<Container>()[0])>::type,
const std::initializer_list<I> &> subscript_operator(Container &&vec,
const std::initializer_list<I> &indexes)
{
return {&vec[0], indexes};
}
}
using Common::subscript_operator;
}
#endif
namespace Vc_VERSIONED_NAMESPACE
{
template <class T, size_t Size> struct array {
typedef array self_;
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
value_type elems_[Size > 0 ? Size : 1];
void fill(const value_type& u_) { std::fill_n(elems_, Size, u_); }
void swap(array& a_) noexcept(std::swap(std::declval<T &>(), std::declval<T &>()))
{
std::swap_ranges(elems_, elems_ + Size, a_.elems_);
}
iterator begin() noexcept { return iterator(elems_); }
const_iterator begin() const noexcept { return const_iterator(elems_); }
iterator end() noexcept { return iterator(elems_ + Size); }
const_iterator end() const noexcept { return const_iterator(elems_ + Size); }
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end());
}
reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(begin());
}
const_iterator cbegin() const noexcept { return begin(); }
const_iterator cend() const noexcept { return end(); }
const_reverse_iterator crbegin() const noexcept { return rbegin(); }
const_reverse_iterator crend() const noexcept { return rend(); }
constexpr size_type size() const noexcept { return Size; }
constexpr size_type max_size() const noexcept { return Size; }
constexpr bool empty() const noexcept { return Size == 0; }
reference operator[](size_type n_) { return elems_[n_]; }
constexpr const_reference operator[](size_type n_) const { return elems_[n_]; }
template <typename I>
Vc_ALWAYS_INLINE auto operator[](I&& arg_)
-> decltype(subscript_operator(*this, std::forward<I>(arg_)))
{
return subscript_operator(*this, std::forward<I>(arg_));
}
template <typename I>
Vc_ALWAYS_INLINE auto operator[](I&& arg_) const
-> decltype(subscript_operator(*this, std::forward<I>(arg_)))
{
return subscript_operator(*this, std::forward<I>(arg_));
}
reference at(size_type n_);
constexpr const_reference at(size_type n_) const;
reference front() { return elems_[0]; }
constexpr const_reference front() const { return elems_[0]; }
reference back() { return elems_[Size > 0 ? Size - 1 : 0]; }
constexpr const_reference back() const { return elems_[Size > 0 ? Size - 1 : 0]; }
value_type* data() noexcept { return elems_; }
const value_type* data() const noexcept { return elems_; }
};
template <class T, size_t Size>
typename array<T, Size>::reference array<T, Size>::at(size_type n_)
{
if (n_ >= Size) {
throw std::out_of_range("array::at");
}
return elems_[n_];
}
template <class T, size_t Size>
constexpr typename array<T, Size>::const_reference array<T, Size>::at(size_type n_) const
{
return n_ >= Size ? (throw std::out_of_range("array::at"), elems_[0]) : elems_[n_];
}
template <class T, size_t Size>
inline bool operator==(const array<T, Size>& x_, const array<T, Size>& y_)
{
return std::equal(x_.elems_, x_.elems_ + Size, y_.elems_);
}
template <class T, size_t Size>
inline bool operator!=(const array<T, Size>& x_, const array<T, Size>& y_)
{
return !(x_ == y_);
}
template <class T, size_t Size>
inline bool operator<(const array<T, Size>& x_, const array<T, Size>& y_)
{
return std::lexicographical_compare(x_.elems_, x_.elems_ + Size, y_.elems_,
y_.elems_ + Size);
}
template <class T, size_t Size>
inline bool operator>(const array<T, Size>& x_, const array<T, Size>& y_)
{
return y_ < x_;
}
template <class T, size_t Size>
inline bool operator<=(const array<T, Size>& x_, const array<T, Size>& y_)
{
return !(y_ < x_);
}
template <class T, size_t Size>
inline bool operator>=(const array<T, Size>& x_, const array<T, Size>& y_)
{
return !(x_ < y_);
}
template <typename T, std::size_t N>
inline auto begin(array<T, N>& arr) -> decltype(arr.begin())
{
return arr.begin();
}
template <typename T, std::size_t N>
inline auto begin(const array<T, N>& arr) -> decltype(arr.begin())
{
return arr.begin();
}
template <typename T, std::size_t N>
inline auto end(array<T, N>& arr) -> decltype(arr.end())
{
return arr.end();
}
template <typename T, std::size_t N>
inline auto end(const array<T, N>& arr) -> decltype(arr.end())
{
return arr.end();
}
namespace Traits
{
template <typename T, std::size_t N>
struct has_no_allocated_data_impl<Vc::array<T, N>> : public std::true_type
{
};
template <typename T, std::size_t N>
struct has_contiguous_storage_impl<Vc::array<T, N>> : public std::true_type
{
};
}
}
namespace std
{
template <class T, size_t Size>
inline
#ifdef Vc_MSVC
void
#else
typename enable_if<is_same<void, decltype(swap(declval<T&>(), declval<T&>()))>::value,
void>::type
#endif
swap(const Vc::array<T, Size>& x_,
const Vc::array<T, Size>& y_) noexcept(swap(declval<T&>(), declval<T&>()))
{
x_.swap(y_);
}
template <class T, size_t Size>
class tuple_size<Vc::array<T, Size>> : public integral_constant<size_t, Size>
{
};
template <size_t I, class T, size_t Size> class tuple_element<I, Vc::array<T, Size>>
{
public:
typedef T type;
};
template <size_t I, class T, size_t Size>
inline constexpr typename std::enable_if<(I < Size), T&>::type get(
Vc::array<T, Size>& a_) noexcept
{
return a_.elems_[I];
}
template <size_t I, class T, size_t Size>
inline constexpr typename std::enable_if<(I < Size), const T&>::type get(
const Vc::array<T, Size>& a_) noexcept
{
return a_.elems_[I];
}
template <size_t I, class T, size_t Size>
inline constexpr typename std::enable_if<(I < Size), T&&>::type get(
Vc::array<T, Size>&& a_) noexcept
{
return std::move(a_.elems_[I]);
}
}
#endif
#ifndef VC_COMMON_SPAN_H_
#define VC_COMMON_SPAN_H_
#include <array>
#include <cstddef>
#include <cstddef>
#include <iterator>
#include <type_traits>
namespace Vc_VERSIONED_NAMESPACE
{
#ifdef __cpp_inline_variables
inline
#endif
constexpr ptrdiff_t dynamic_extent = -1;
namespace Common
{
template <typename T, ptrdiff_t Extent = dynamic_extent> class span;
template <typename T, ptrdiff_t Extent>
constexpr auto begin(const span<T, Extent>& s) noexcept -> decltype(s.begin())
{
return s.begin();
}
template <typename T, ptrdiff_t Extent>
constexpr auto end(const span<T, Extent>& s) noexcept -> decltype(s.end())
{
return s.end();
}
template <class T> struct _is_span_impl : public std::false_type {
};
template <class T, ptrdiff_t Extent>
struct _is_span_impl<span<T, Extent>> : public std::true_type {
};
template <class T>
struct _is_span : public _is_span_impl<typename std::remove_cv<T>::type> {
};
template <class T> struct _is_std_array_impl : public std::false_type {
};
template <class T, size_t Sz>
struct _is_std_array_impl<array<T, Sz>> : public std::true_type {
};
template <class T>
struct _is_std_array : public _is_std_array_impl<typename std::remove_cv<T>::type> {
};
template <class T, class ElementType, class = void>
struct _is_span_compatible_container : public std::false_type {
};
template <class... Ts> using _void_t = void;
template <class C> constexpr auto _std_data(C& c) -> decltype(c.data())
{
return c.data();
}
template <class C> constexpr auto _std_data(const C& c) -> decltype(c.data())
{
return c.data();
}
template <class T, std::size_t N> constexpr T* _std_data(T (&array)[N]) noexcept
{
return array;
}
template <class E> constexpr const E* _std_data(std::initializer_list<E> il) noexcept
{
return il.begin();
}
template <class C> constexpr auto _std_size(const C& c) -> decltype(c.size())
{
return c.size();
}
template <class T, std::size_t N>
constexpr std::size_t _std_size(const T (&array)[N]) noexcept
{
return N;
}
template <class T, class ElementType>
struct _is_span_compatible_container<
T, ElementType,
_void_t<
typename std::enable_if<!_is_span<T>::value, std::nullptr_t>::type,
typename std::enable_if<!_is_std_array<T>::value, std::nullptr_t>::type,
typename std::enable_if<!std::is_array<T>::value, std::nullptr_t>::type,
decltype(data(std::declval<T>())), decltype(size(std::declval<T>())),
typename std::enable_if<
std::is_convertible<typename std::remove_pointer<decltype(
data(std::declval<T&>()))>::type (*)[],
ElementType (*)[]>::value,
std::nullptr_t>::type>> : public std::true_type {
};
#if defined Vc_MSVC || (defined Vc_GCC && Vc_GCC < 0x50100) || defined Vc_ICC || !defined __cpp_constexpr || __cpp_constexpr < 201304
#define Vc_CONSTEXPR
#else
#define Vc_CONSTEXPR constexpr
#endif
template <typename T, ptrdiff_t Extent> class span
{
public:
using element_type = T;
using value_type = typename std::remove_cv<T>::type;
using index_type = ptrdiff_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = Extent;
static_assert(Extent >= 0, "Can't have a span with an extent < 0");
Vc_CONSTEXPR span() noexcept : data_{nullptr}
{
static_assert(Extent == 0,
"Can't default construct a statically sized span with size > 0");
}
Vc_CONSTEXPR span(const span&) noexcept = default;
Vc_CONSTEXPR span& operator=(const span&) noexcept = default;
Vc_CONSTEXPR span(pointer _ptr, index_type _count) : data_{_ptr}
{
(void)_count;
Vc_ASSERT(((void)"size mismatch in span's constructor (ptr, len)", Extent == _count));
}
Vc_CONSTEXPR span(pointer _f, pointer _l) : data_{_f}
{
(void)_l;
Vc_ASSERT(((void)"size mismatch in span's constructor (ptr, ptr)",
Extent == distance(_f, _l)));
}
Vc_CONSTEXPR span(element_type (&_arr)[Extent]) noexcept : data_{_arr} {}
Vc_CONSTEXPR span(array<value_type, Extent>& _arr) noexcept : data_{_arr.data()} {}
Vc_CONSTEXPR span(const array<value_type, Extent>& _arr) noexcept : data_{_arr.data()} {}
template <class Container>
inline Vc_CONSTEXPR span(
Container& _c,
typename std::enable_if<_is_span_compatible_container<Container, T>::value,
std::nullptr_t>::type = nullptr)
: data_{_std_data(_c)}
{
Vc_ASSERT(("size mismatch in span's constructor (container))",
Extent == _std_size(_c)));
}
template <class Container>
inline Vc_CONSTEXPR span(
const Container& _c,
typename std::enable_if<_is_span_compatible_container<const Container, T>::value,
std::nullptr_t>::type = nullptr)
: data_{_std_data(_c)}
{
Vc_ASSERT(("size mismatch in span's constructor (const container)",
Extent == _std_size(_c)));
}
template <class OtherElementType>
inline Vc_CONSTEXPR span(
const span<OtherElementType, Extent>& _other,
typename std::enable_if<
std::is_convertible<OtherElementType (*)[], element_type (*)[]>::value,
std::nullptr_t>::type = nullptr)
: data_{_other.data()}
{
}
template <class OtherElementType>
inline Vc_CONSTEXPR span(
const span<OtherElementType, dynamic_extent>& _other,
typename std::enable_if<
std::is_convertible<OtherElementType (*)[], element_type (*)[]>::value,
std::nullptr_t>::type = nullptr) noexcept
: data_{_other.data()}
{
Vc_ASSERT(("size mismatch in span's constructor (other span)",
Extent == _other.size()));
}
template <ptrdiff_t Count>
inline Vc_CONSTEXPR span<element_type, Count> first() const noexcept
{
static_assert(Count >= 0, "Count must be >= 0 in span::first()");
static_assert(Count <= Extent, "Count out of range in span::first()");
return {data(), Count};
}
template <ptrdiff_t Count>
inline Vc_CONSTEXPR span<element_type, Count> last() const noexcept
{
static_assert(Count >= 0, "Count must be >= 0 in span::last()");
static_assert(Count <= Extent, "Count out of range in span::last()");
return {data() + size() - Count, Count};
}
Vc_CONSTEXPR span<element_type, dynamic_extent> first(index_type _count) const noexcept
{
Vc_ASSERT(("Count out of range in span::first(count)",
_count >= 0 && _count <= size()));
return {data(), _count};
}
Vc_CONSTEXPR span<element_type, dynamic_extent> last(index_type _count) const noexcept
{
Vc_ASSERT(
("Count out of range in span::last(count)", _count >= 0 && _count <= size()));
return {data() + size() - _count, _count};
}
#ifndef Vc_MSVC
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
inline Vc_CONSTEXPR auto subspan() const noexcept
-> span<element_type, Count != dynamic_extent ? Count : Extent - Offset>
{
Vc_ASSERT(
("Offset out of range in span::subspan()", Offset >= 0 && Offset <= size()));
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
}
inline Vc_CONSTEXPR span<element_type, dynamic_extent> subspan(
index_type offset, index_type count = dynamic_extent) const noexcept
{
Vc_ASSERT(("Offset out of range in span::subspan(offset, count)",
offset >= 0 && offset <= size()));
Vc_ASSERT(("Count out of range in span::subspan(offset, count)",
(count >= 0 && count <= size()) || count == dynamic_extent));
if (count == dynamic_extent) {
return {data() + offset, size() - offset};
}
Vc_ASSERT(("count + offset out of range in span::subspan(offset, count)",
offset + count <= size()));
return {data() + offset, count};
}
#endif
Vc_CONSTEXPR index_type size() const noexcept { return Extent; }
Vc_CONSTEXPR index_type size_bytes() const noexcept
{
return Extent * sizeof(element_type);
}
Vc_CONSTEXPR bool empty() const noexcept { return Extent == 0; }
Vc_CONSTEXPR reference operator[](index_type _idx) const noexcept
{
Vc_ASSERT(("span<T,N>[] index out of bounds", _idx >= 0 && _idx < size()));
return data_[_idx];
}
Vc_CONSTEXPR reference operator()(index_type _idx) const noexcept
{
Vc_ASSERT(("span<T,N>() index out of bounds", _idx >= 0 && _idx < size()));
return data_[_idx];
}
Vc_CONSTEXPR pointer data() const noexcept { return data_; }
Vc_CONSTEXPR iterator begin() const noexcept { return iterator(data()); }
Vc_CONSTEXPR iterator end() const noexcept { return iterator(data() + size()); }
Vc_CONSTEXPR const_iterator cbegin() const noexcept { return const_iterator(data()); }
Vc_CONSTEXPR const_iterator cend() const noexcept
{
return const_iterator(data() + size());
}
Vc_CONSTEXPR reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
Vc_CONSTEXPR reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Vc_CONSTEXPR const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(cend());
}
Vc_CONSTEXPR const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(cbegin());
}
Vc_CONSTEXPR void swap(span& _other) noexcept
{
pointer _p = data_;
data_ = _other.data_;
_other.data_ = _p;
}
#ifdef __cpp_lib_byte
span<const std::byte, Extent * sizeof(element_type)> _as_bytes() const noexcept
{
return {reinterpret_cast<const std::byte*>(data()), size_bytes()};
}
span<std::byte, Extent * sizeof(element_type)> _as_writeable_bytes() const noexcept
{
return {reinterpret_cast<std::byte*>(data()), size_bytes()};
}
#endif
private:
pointer data_;
};
template <typename T> class span<T, dynamic_extent>
{
private:
public:
using element_type = T;
using value_type = typename std::remove_cv<T>::type;
using index_type = ptrdiff_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = dynamic_extent;
Vc_CONSTEXPR span() noexcept : data_{nullptr}, size_{0} {}
Vc_CONSTEXPR span(const span&) noexcept = default;
Vc_CONSTEXPR span& operator=(const span&) noexcept = default;
Vc_CONSTEXPR span(pointer _ptr, index_type _count) : data_{_ptr}, size_{_count} {}
Vc_CONSTEXPR span(pointer _f, pointer _l) : data_{_f}, size_{distance(_f, _l)} {}
template <size_t Sz>
inline Vc_CONSTEXPR span(element_type (&_arr)[Sz]) noexcept : data_{_arr}, size_{Sz}
{
}
template <size_t Sz>
inline Vc_CONSTEXPR span(array<value_type, Sz>& _arr) noexcept
: data_{_arr.data()}, size_{Sz}
{
}
template <size_t Sz>
inline Vc_CONSTEXPR span(const array<value_type, Sz>& _arr) noexcept
: data_{_arr.data()}, size_{Sz}
{
}
template <class Container>
inline Vc_CONSTEXPR span(
Container& _c,
typename std::enable_if<_is_span_compatible_container<Container, T>::value,
std::nullptr_t>::type = nullptr)
: data_{_std_data(_c)}, size_{index_type(_std_size(_c))}
{
}
template <class Container>
inline Vc_CONSTEXPR span(
const Container& _c,
typename std::enable_if<_is_span_compatible_container<const Container, T>::value,
std::nullptr_t>::type = nullptr)
: data_{_std_data(_c)}, size_{index_type(_std_size(_c))}
{
}
template <class OtherElementType, ptrdiff_t OtherExtent>
inline Vc_CONSTEXPR span(
const span<OtherElementType, OtherExtent>& _other,
typename std::enable_if<
std::is_convertible<OtherElementType (*)[], element_type (*)[]>::value,
std::nullptr_t>::type = nullptr) noexcept
: data_{_other.data()}, size_{_other.size()}
{
}
template <ptrdiff_t Count>
inline Vc_CONSTEXPR span<element_type, Count> first() const noexcept
{
static_assert(Count >= 0, "");
Vc_ASSERT(("Count out of range in span::first()", Count <= size()));
return {data(), Count};
}
template <ptrdiff_t Count>
inline Vc_CONSTEXPR span<element_type, Count> last() const noexcept
{
static_assert(Count >= 0, "");
Vc_ASSERT(("Count out of range in span::last()", Count <= size()));
return {data() + size() - Count, Count};
}
Vc_CONSTEXPR span<element_type, dynamic_extent> first(index_type _count) const noexcept
{
Vc_ASSERT(("Count out of range in span::first(count)",
_count >= 0 && _count <= size()));
return {data(), _count};
}
Vc_CONSTEXPR span<element_type, dynamic_extent> last(index_type _count) const noexcept
{
Vc_ASSERT(
("Count out of range in span::last(count)", _count >= 0 && _count <= size()));
return {data() + size() - _count, _count};
}
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
inline Vc_CONSTEXPR span<T, dynamic_extent> subspan() const noexcept
{
Vc_ASSERT(
("Offset out of range in span::subspan()", Offset >= 0 && Offset <= size()));
Vc_ASSERT(("Count out of range in span::subspan()",
Count == dynamic_extent || Offset + Count <= size()));
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
}
Vc_CONSTEXPR span<element_type, dynamic_extent> inline subspan(
index_type _offset, index_type _count = dynamic_extent) const noexcept
{
Vc_ASSERT(("Offset out of range in span::subspan(offset, count)",
_offset >= 0 && _offset <= size()));
Vc_ASSERT(("count out of range in span::subspan(offset, count)",
(_count >= 0 && _count <= size()) || _count == dynamic_extent));
if (_count == dynamic_extent)
return {data() + _offset, size() - _offset};
Vc_ASSERT(("Offset + count out of range in span::subspan(offset, count)",
_offset + _count <= size()));
return {data() + _offset, _count};
}
Vc_CONSTEXPR index_type size() const noexcept { return size_; }
Vc_CONSTEXPR index_type size_bytes() const noexcept
{
return size_ * sizeof(element_type);
}
Vc_CONSTEXPR bool empty() const noexcept { return size_ == 0; }
Vc_CONSTEXPR reference operator[](index_type _idx) const noexcept
{
Vc_ASSERT(("span<T>[] index out of bounds", _idx >= 0 && _idx < size()));
return data_[_idx];
}
Vc_CONSTEXPR reference operator()(index_type _idx) const noexcept
{
Vc_ASSERT(("span<T>() index out of bounds", _idx >= 0 && _idx < size()));
return data_[_idx];
}
Vc_CONSTEXPR pointer data() const noexcept { return data_; }
Vc_CONSTEXPR iterator begin() const noexcept { return iterator(data()); }
Vc_CONSTEXPR iterator end() const noexcept { return iterator(data() + size()); }
Vc_CONSTEXPR const_iterator cbegin() const noexcept { return const_iterator(data()); }
Vc_CONSTEXPR const_iterator cend() const noexcept
{
return const_iterator(data() + size());
}
Vc_CONSTEXPR reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
Vc_CONSTEXPR reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Vc_CONSTEXPR const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(cend());
}
Vc_CONSTEXPR const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(cbegin());
}
Vc_CONSTEXPR void swap(span& _other) noexcept
{
pointer _p = data_;
data_ = _other.data_;
_other.data_ = _p;
index_type _sz = size_;
size_ = _other.size_;
_other.size_ = _sz;
}
#ifdef __cpp_lib_byte
#if _MSC_VER > 1928
span<const std::byte, dynamic_extent> _as_bytes() const noexcept
{
return {reinterpret_cast<const std::byte*>(data()), size_bytes()};
}
span<std::byte, dynamic_extent> _as_writeable_bytes() const noexcept
{
return {reinterpret_cast<std::byte*>(data()), size_bytes()};
}
#endif
#endif
private:
pointer data_;
index_type size_;
};
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator==(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator!=(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return !(rhs == lhs);
}
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator<(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator<=(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return !(rhs < lhs);
}
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator>(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return rhs < lhs;
}
template <class T1, ptrdiff_t Extent1, class T2, ptrdiff_t Extent2>
Vc_CONSTEXPR bool operator>=(const span<T1, Extent1>& lhs, const span<T2, Extent2>& rhs)
{
return !(lhs < rhs);
}
template <class T, ptrdiff_t Extent>
auto as_bytes(span<T, Extent> _s) noexcept -> decltype(_s._as_bytes())
{
return _s._as_bytes();
}
template <class T, ptrdiff_t Extent>
auto as_writeable_bytes(span<T, Extent> _s) noexcept ->
typename std::enable_if<!std::is_const<T>::value,
decltype(_s._as_writeable_bytes())>::type
{
return _s._as_writeable_bytes();
}
template <class T, ptrdiff_t Extent>
Vc_CONSTEXPR void swap(span<T, Extent>& lhs, span<T, Extent>& rhs) noexcept
{
lhs.swap(rhs);
}
#undef Vc_CONSTEXPR
#ifdef __cpp_deduction_guides
template <class T, size_t Sz> span(T (&)[Sz])->span<T, Sz>;
template <class T, size_t Sz> span(array<T, Sz>&)->span<T, Sz>;
template <class T, size_t Sz> span(const array<T, Sz>&)->span<const T, Sz>;
template <class Container> span(Container&)->span<typename Container::value_type>;
template <class Container>
span(const Container&)->span<const typename Container::value_type>;
#endif
}
template <typename T, ptrdiff_t Extent = dynamic_extent>
using span = Common::AdaptSubscriptOperator<Common::span<T, Extent>>;
namespace Traits
{
template <typename T, ptrdiff_t Extent>
struct has_contiguous_storage_impl<Vc::span<T, Extent>> : public std::true_type {
};
template <typename T, ptrdiff_t Extent>
struct has_contiguous_storage_impl<Vc::Common::span<T, Extent>> : public std::true_type {
};
}
}
#endif
#ifndef VC_VECTOR_
#define VC_VECTOR_
#include <vector>
namespace Vc_VERSIONED_NAMESPACE
{
template <typename T, typename Allocator = std::allocator<T>>
using vector = Common::AdaptSubscriptOperator<std::vector<T, Allocator>>;
namespace Traits
{
template <typename T, typename A>
struct has_contiguous_storage_impl<Vc::vector<T, A>> : public std::true_type {};
}
}
#endif
#endif
|