1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
|
// Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
// 2017, 2018, 2019, 2020, 2021, 2022
// VladimÃr VondruÅ¡ <mosra@centrum.cz> and contributors
// Copyright © 2023 Robert Clausecker <fuz@FreeBSD.org>
// Copyright © 2020-2024 Dan R.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#pragma once
/** @file
@brief Namespace @ref Death::Cpu and related macros
*/
#include <type_traits>
#include "Common.h"
// Because can't use inline assembly when targeting 64-bit on MSVC, and because <intrin.h> and <immintrin.h> is just
// too damn heavy to be included in a header. Declarations copied verbatim. Clang-cl doesn't like this (undefined
// reference to __cpuidex), so using the GCC/Clang codepath on it instead.
#if defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL) && defined(DEATH_TARGET_X86)
extern "C" {
void __cpuidex(int[4], int, int);
unsigned __int64 __cdecl _xgetbv(unsigned int);
}
#endif
namespace Death { namespace Cpu {
//###==##====#=====--==~--~=~- --- -- - - - -
/**
@brief Traits class for CPU detection tag types
Useful for detecting tag properties at compile time without the need for
repeated code such as method overloading, cascaded ifs or template
specializations for all tag types. All tag types in the @ref Cpu namespace
have this class implemented.
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> struct TypeTraits {
enum : unsigned int {
/**
* Tag-specific index. Implementation-defined, is unique among all tags
* on given platform.
*/
Index
};
/**
* @brief Tag name
*
* Returns a string representation of the tag, such as @cpp "Avx2" @ce
* for @ref Avx2.
*/
static const char* name();
};
#else
template<class> struct TypeTraits;
#endif
namespace Implementation
{
// A common type used in all tag constructors to avoid ambiguous calls when using {}
struct InitT { };
constexpr InitT Init{};
enum: unsigned int { ExtraTagBitOffset = 16 };
}
/**
@brief Scalar tag type
See the @ref Cpu namespace and the @ref Scalar tag for more information.
*/
struct ScalarT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit ScalarT(Implementation::InitT) {}
#endif
};
#ifndef DOXYGEN_GENERATING_OUTPUT
// Scalar code is when nothing else is available, thus no bits set
template<> struct TypeTraits<ScalarT> {
enum: unsigned int { Index = 0 };
static const char* name() { return "Scalar"; }
};
#endif
#if defined(DEATH_TARGET_X86) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief SSE2 tag type
Available only on `DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Sse2 tag for more information.
*/
struct Sse2T : ScalarT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Sse2T(Implementation::InitT) : ScalarT{Implementation::Init} {}
#endif
};
/**
@brief SSE3 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Sse3 tag for more information.
*/
struct Sse3T : Sse2T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Sse3T(Implementation::InitT) : Sse2T{Implementation::Init} {}
#endif
};
/**
@brief SSSE3 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Ssse3 tag for more information.
*/
struct Ssse3T : Sse3T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Ssse3T(Implementation::InitT) : Sse3T{Implementation::Init} {}
#endif
};
/**
@brief SSE4.1 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Sse41T tag for more information.
*/
struct Sse41T : Ssse3T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Sse41T(Implementation::InitT) : Ssse3T{Implementation::Init} {}
#endif
};
/**
@brief SSE4.2 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Sse42T tag for more information.
*/
struct Sse42T : Sse41T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Sse42T(Implementation::InitT) : Sse41T{Implementation::Init} {}
#endif
};
/**
@brief POPCNT tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Popcnt tag for more information.
*/
struct PopcntT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit PopcntT(Implementation::InitT) {}
#endif
};
/**
@brief LZCNT tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Lzcnt tag for more information.
*/
struct LzcntT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit LzcntT(Implementation::InitT) {}
#endif
};
/**
@brief BMI1 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Bmi1 tag for more information.
*/
struct Bmi1T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Bmi1T(Implementation::InitT) {}
#endif
};
/**
@brief BMI2 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Bmi2 tag for more information.
*/
struct Bmi2T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Bmi2T(Implementation::InitT) {}
#endif
};
/**
@brief AVX tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Avx tag for more information.
*/
struct AvxT: Sse42T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit AvxT(Implementation::InitT) : Sse42T{Implementation::Init} {}
#endif
};
/**
@brief AVX F16C tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref AvxF16c tag for more information.
*/
struct AvxF16cT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit AvxF16cT(Implementation::InitT) {}
#endif
};
/**
@brief AVX FMA tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref AvxFma tag for more information.
*/
struct AvxFmaT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit AvxFmaT(Implementation::InitT) {}
#endif
};
/**
@brief AVX2 tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Avx2 tag for more information.
*/
struct Avx2T : AvxT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Avx2T(Implementation::InitT) : AvxT{Implementation::Init} {}
#endif
};
/**
@brief AVX-512 Foundation tag type
Available only on @ref DEATH_TARGET_X86 "x86". See the @ref Cpu namespace
and the @ref Avx512f tag for more information.
*/
struct Avx512fT : Avx2T {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Avx512fT(Implementation::InitT) : Avx2T{Implementation::Init} {}
#endif
};
#ifndef DOXYGEN_GENERATING_OUTPUT
// Features earlier in the hierarchy should have lower bits set
template<> struct TypeTraits<Sse2T> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Sse2"; }
};
template<> struct TypeTraits<Sse3T> {
enum: unsigned int { Index = 1 << 1 };
static const char* name() { return "Sse3"; }
};
template<> struct TypeTraits<Ssse3T> {
enum: unsigned int { Index = 1 << 2 };
static const char* name() { return "Ssse3"; }
};
template<> struct TypeTraits<Sse41T> {
enum: unsigned int { Index = 1 << 3 };
static const char* name() { return "Sse41"; }
};
template<> struct TypeTraits<Sse42T> {
enum: unsigned int { Index = 1 << 4 };
static const char* name() { return "Sse42"; }
};
template<> struct TypeTraits<AvxT> {
enum: unsigned int { Index = 1 << 5 };
static const char* name() { return "Avx"; }
};
template<> struct TypeTraits<Avx2T> {
enum: unsigned int { Index = 1 << 6 };
static const char* name() { return "Avx2"; }
};
template<> struct TypeTraits<Avx512fT> {
enum: unsigned int { Index = 1 << 7 };
static const char* name() { return "Avx512f"; }
};
// The total bit range should not be larger than ExtraTagCount
template<> struct TypeTraits<PopcntT> {
enum: unsigned int { Index = 1 << (0 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Popcnt"; }
};
template<> struct TypeTraits<LzcntT> {
enum: unsigned int { Index = 1 << (1 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Lzcnt"; }
};
template<> struct TypeTraits<Bmi1T> {
enum: unsigned int { Index = 1 << (2 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Bmi1"; }
};
template<> struct TypeTraits<Bmi2T> {
enum: unsigned int { Index = 1 << (3 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Bmi2"; }
};
template<> struct TypeTraits<AvxF16cT> {
enum: unsigned int { Index = 1 << (4 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "AvxF16c"; }
};
template<> struct TypeTraits<AvxFmaT> {
enum: unsigned int { Index = 1 << (5 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "AvxFma"; }
};
#endif
#endif
#if defined(DEATH_TARGET_ARM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief NEON tag type
Available only on @ref DEATH_TARGET_ARM "ARM". See the @ref Cpu namespace
and the @ref Neon tag for more information.
*/
struct NeonT : ScalarT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit NeonT(Implementation::InitT) : ScalarT{Implementation::Init} {}
#endif
};
/**
@brief NEON FMA tag type
Available only on @ref DEATH_TARGET_ARM "ARM". See the @ref Cpu namespace
and the @ref NeonFma tag for more information.
*/
struct NeonFmaT : NeonT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit NeonFmaT(Implementation::InitT) : NeonT{Implementation::Init} {}
#endif
};
/**
@brief NEON FP16 tag type
Available only on @ref DEATH_TARGET_ARM "ARM". See the @ref Cpu namespace
and the @ref NeonFp16 tag for more information.
*/
struct NeonFp16T : NeonFmaT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit NeonFp16T(Implementation::InitT) : NeonFmaT{Implementation::Init} {}
#endif
};
#ifndef DOXYGEN_GENERATING_OUTPUT
template<> struct TypeTraits<NeonT> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Neon"; }
};
template<> struct TypeTraits<NeonFmaT> {
enum: unsigned int { Index = 1 << 1 };
static const char* name() { return "NeonFma"; }
};
template<> struct TypeTraits<NeonFp16T> {
enum: unsigned int { Index = 1 << 2 };
static const char* name() { return "NeonFp16"; }
};
#endif
#endif
#if defined(DEATH_TARGET_WASM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief SIMD128 tag type
Available only on @ref DEATH_TARGET_WASM "WebAssembly". See the @ref Cpu
namespace and the @ref Simd128 tag for more information.
*/
struct Simd128T: ScalarT {
#ifndef DOXYGEN_GENERATING_OUTPUT
// Explicit constructor to avoid ambiguous calls when using {}
constexpr explicit Simd128T(Implementation::InitT): ScalarT{Implementation::Init} { }
#endif
};
#ifndef DOXYGEN_GENERATING_OUTPUT
template<> struct TypeTraits<Simd128T> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Simd128"; }
};
#endif
#endif
/**
@brief Scalar tag
Code that isn't explicitly optimized with any advanced CPU instruction set.
Fallback if no other CPU instruction set is chosen or available. The next most
widely supported instruction sets are @ref Sse2 on x86, @ref Neon on ARM and
@ref Simd128 on WebAssembly.
*/
constexpr ScalarT Scalar{Implementation::Init};
#if defined(DEATH_TARGET_X86) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief SSE2 tag
[Streaming SIMD Extensions 2](https://en.wikipedia.org/wiki/SSE2). Available
only on @ref DEATH_TARGET_X86 "x86", supported by all 64-bit x86 processors
and is present on majority of contemporary 32-bit x86 processors as well.
Superset of @ref Scalar, implied by @ref Sse3.
*/
constexpr Sse2T Sse2{Implementation::Init};
/**
@brief SSE3 tag
[Streaming SIMD Extensions 3](https://en.wikipedia.org/wiki/SSE3). Available
only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Sse2, implied by
@ref Ssse3.
*/
constexpr Sse3T Sse3{Implementation::Init};
/**
@brief SSSE3 tag
[Supplemental Streaming SIMD Extensions 3](https://en.wikipedia.org/wiki/SSSE3).
Available only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Sse3, implied
by @ref Sse41.
Note that certain older AMD processors have [SSE4a](https://en.wikipedia.org/wiki/SSE4#SSE4a)
but neither SSSE3 nor SSE4.1. Both can be however treated as a subset of SSE4.1
to a large extent, and it's recommended to use @ref Sse41 to handle those.
*/
constexpr Ssse3T Ssse3{Implementation::Init};
/**
@brief SSE4.1 tag
[Streaming SIMD Extensions 4.1](https://en.wikipedia.org/wiki/SSE4#SSE4.1).
Available only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Ssse3,
implied by @ref Sse42.
Note that certain older AMD processors have [SSE4a](https://en.wikipedia.org/wiki/SSE4#SSE4a)
but neither SSSE3 nor SSE4.1. Both can be however treated as a subset of SSE4.1
to a large extent, and it's recommended to use @ref Sse41 to handle those.
*/
constexpr Sse41T Sse41{Implementation::Init};
/**
@brief SSE4.2 tag
[Streaming SIMD Extensions 4.2](https://en.wikipedia.org/wiki/SSE4#SSE4.2).
Available only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Sse41,
implied by @ref Avx.
*/
constexpr Sse42T Sse42{Implementation::Init};
/**
@brief POPCNT tag
[POPCNT](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#ABM_(Advanced_Bit_Manipulation))
instructions. Available only on @ref DEATH_TARGET_X86 "x86". This instruction
set is treated as an *extra*, i.e. is neither a superset of nor implied by any
other instruction set. See @ref Cpu-usage-extra for more information.
*/
constexpr PopcntT Popcnt{Implementation::Init};
/**
@brief LZCNT tag
[LZCNT](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#ABM_(Advanced_Bit_Manipulation))
instructions. Available only on @ref DEATH_TARGET_X86 "x86". This instruction
set is treated as an *extra*, i.e. is neither a superset of nor implied by any
other instruction set. See @ref Cpu-usage-extra for more information.
Note that this instruction has encoding compatible with an earlier `BSR`
instruction which has a slightly different behavior. To avoid wrong results if
it isn't available, prefer to always detect its presence with
@ref runtimeFeatures() instead of a compile-time check.
*/
constexpr LzcntT Lzcnt{Implementation::Init};
/**
@brief BMI1 tag
[BMI1](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#BMI1_(Bit_Manipulation_Instruction_Set_1))
instructions, including `TZCNT`. Available only on
@ref DEATH_TARGET_X86 "x86". This instruction set is treated as an *extra*,
i.e. is neither a superset of nor implied by any other instruction set. See
@ref Cpu-usage-extra for more information.
Note that the `TZCNT` instruction has encoding compatible with an earlier `BSF`
instruction which has a slightly different behavior. To avoid wrong results if
it isn't available, prefer to always detect its presence with
@ref runtimeFeatures() instead of a compile-time check.
*/
constexpr Bmi1T Bmi1{Implementation::Init};
/**
@brief BMI2 tag
[BMI2](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#BMI2_(Bit_Manipulation_Instruction_Set_2))
instructions. Available only on @ref DEATH_TARGET_X86 "x86". This instruction
set is treated as an *extra*, i.e. is neither a superset of nor implied by any
other instruction set. See @ref Cpu-usage-extra for more information.
*/
constexpr Bmi2T Bmi2{Implementation::Init};
/**
@brief AVX tag
[Advanced Vector Extensions](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions).
Available only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Sse42,
implied by @ref Avx2.
*/
constexpr AvxT Avx{Implementation::Init};
/**
@brief AVX F16C tag
[F16C](https://en.wikipedia.org/wiki/F16C) instructions. Available only on
@ref DEATH_TARGET_X86 "x86". This instruction set is treated as an *extra*,
i.e. is neither a superset of nor implied by any other instruction set. See
@ref Cpu-usage-extra for more information.
*/
constexpr AvxF16cT AvxF16c{Implementation::Init};
/**
@brief AVX FMA tag
[FMA3 instruction set](https://en.wikipedia.org/wiki/FMA_instruction_set).
Available only on @ref DEATH_TARGET_X86 "x86". This instruction set is
treated as an *extra*, i.e. is neither a superset of nor implied by any other
instruction set. See @ref Cpu-usage-extra for more information.
*/
constexpr AvxFmaT AvxFma{Implementation::Init};
/**
@brief AVX2 tag
[Advanced Vector Extensions 2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#Advanced_Vector_Extensions_2).
Available only on @ref DEATH_TARGET_X86 "x86". Superset of @ref Avx,
implied by @ref Avx512f.
*/
constexpr Avx2T Avx2{Implementation::Init};
/**
@brief AVX-512 Foundation tag
[AVX-512](https://en.wikipedia.org/wiki/AVX-512) Foundation. Available only on
@ref DEATH_TARGET_X86 "x86". Superset of @ref Avx2.
*/
constexpr Avx512fT Avx512f{Implementation::Init};
#endif
#if defined(DEATH_TARGET_ARM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief NEON tag type
[ARM NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon)).
Available only on @ref DEATH_TARGET_ARM "ARM". Superset of @ref Scalar,
implied by @ref NeonFp16.
*/
constexpr NeonT Neon{Implementation::Init};
/**
@brief NEON FMA tag type
[ARM NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon))
with FMA instructions. Available only on @ref DEATH_TARGET_ARM "ARM".
Superset of @ref Neon, implied by @ref NeonFp16.
*/
constexpr NeonFmaT NeonFma{Implementation::Init};
/**
@brief NEON FP16 tag type
[ARM NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon))
with ARMv8.2-a FP16 vector arithmetic. Available only on
@ref DEATH_TARGET_ARM "ARM". Superset of @ref NeonFma.
*/
constexpr NeonFp16T NeonFp16{Implementation::Init};
#endif
#if defined(DEATH_TARGET_WASM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief SIMD128 tag type
[128-bit WebAssembly SIMD](https://github.com/webassembly/simd). Available only
on @ref DEATH_TARGET_WASM "WebAssembly". Superset of @ref Scalar.
*/
constexpr Simd128T Simd128{Implementation::Init};
#endif
namespace Implementation
{
template<unsigned i> struct Priority : Priority<i - 1> {};
template<> struct Priority<0> {};
// Count of "extra" tags that are not in the hierarchy. Should not be larger than strictly
// necessary as it deepens inheritance hierarchy when picking best overload candidate.
enum : unsigned int {
BaseTagMask = (1 << ExtraTagBitOffset) - 1,
ExtraTagMask = 0xffffffffu & ~BaseTagMask,
#if defined(DEATH_TARGET_X86)
ExtraTagCount = 6,
#else
ExtraTagCount = 0,
#endif
};
// On sane compilers (and on MSVC with /permissive-), these two could be directly in the Tag constructor enable_if expressions.
// But MSVC chokes hard on those ("C2988: unrecognizable template declaration/definition", haha), so they have to be extracted
// outside. I could also #ifdef around the workaround and extract it only for MSVC, but I don't think the code duplication would
// be worth it.
template<unsigned int value, unsigned int otherValue> struct IsTagConversionAllowed {
enum : bool {
Value =
// There should be at most one base tag set in both, if there's none then it's Cpu::Scalar
!((value & BaseTagMask) & ((value & BaseTagMask) - 1)) &&
!((otherValue & BaseTagMask) & ((otherValue & BaseTagMask) - 1)) &&
// The other base tag should be the same or derived (i.e, having same or larger value)
(otherValue & BaseTagMask) >= (value & BaseTagMask) &&
// The other extra bits should be a superset of this
((otherValue & value) & ExtraTagMask) == (value & ExtraTagMask)
};
};
template<unsigned int value, unsigned int otherIndex> struct IsSingleTagConversionAllowed {
enum : bool {
Value =
// There should be at most one base tag set in this one, the other satisfies that implicitly as it's constrained by TypeTraits
!((value & BaseTagMask) & ((value & BaseTagMask) - 1)) &&
// The other base tag should be the same or derived (i.e, having same or larger value)
(otherIndex & BaseTagMask) >= (value & BaseTagMask) &&
// The other extra bits should be a superset of this. Since a single tag can be only one bit, this condition gets satisfied
// only either if we're Cpu::Scalar or if we have no extra bits.
((otherIndex & value) & ExtraTagMask) == (value & ExtraTagMask)
};
};
// Holds a compile-time combination of tags. Kept private, since it isn't really directly
// needed in user code and it would only lead to confusion.
template<unsigned int value> struct Tags {
enum : unsigned int {
Value = value
};
// Empty initialization. Should not be needed by public code.
constexpr explicit Tags(InitT) {}
// Conversion from other tag combination, allowed only if the other is not a subset
// MSVC 2015 and 2017 cannot handle SFINAE in the template, putting it in the argument instead
#ifndef DEATH_MSVC2017_COMPATIBILITY
template<unsigned int otherValue, typename std::enable_if<IsTagConversionAllowed<value, otherValue>::Value, int>::type = 0> constexpr Tags(Tags<otherValue>) {}
#else
template<unsigned int otherValue> constexpr Tags(Tags<otherValue>, typename std::enable_if<IsTagConversionAllowed<value, otherValue>::Value>::type* = {}) {}
#endif
// Conversion from a single tag, allowed only if we're a single bit and the other is not a subset
// MSVC 2015 and 2017 cannot handle SFINAE in the template, putting it in the argument instead
#ifndef DEATH_MSVC2017_COMPATIBILITY
template<class T, typename std::enable_if<IsSingleTagConversionAllowed<Value, TypeTraits<T>::Index>::Value, int>::type = 0> constexpr Tags(T) {}
#else
template<class T> constexpr Tags(T, typename std::enable_if<IsSingleTagConversionAllowed<Value, TypeTraits<T>::Index>::Value>::type* = {}) {}
#endif
//A subset of operators on Features, excluding the assignment ones -- since they modify the type, they make no sense here
template<unsigned int otherValue> constexpr Tags<value | otherValue> operator|(Tags<otherValue>) const {
return Tags<value | otherValue>{Init};
}
template<class U> constexpr Tags<value | TypeTraits<U>::Index> operator|(U) const {
return Tags<value | TypeTraits<U>::Index>{Init};
}
template<unsigned int otherValue> constexpr Tags<value& otherValue> operator&(Tags<otherValue>) const {
return Tags<value& otherValue>{Init};
}
template<class U> constexpr Tags<value& TypeTraits<U>::Index> operator&(U) const {
return Tags<value& TypeTraits<U>::Index>{Init};
}
template<unsigned int otherValue> constexpr Tags<value^ otherValue> operator^(Tags<otherValue>) const {
return Tags<value^ otherValue>{Init};
}
template<class U> constexpr Tags<value^ TypeTraits<U>::Index> operator^(U) const {
return Tags<value^ TypeTraits<U>::Index>{Init};
}
constexpr Tags<~value> operator~() const {
return Tags<~value>{Init};
}
constexpr explicit operator bool() const {
// An explicit cast to avoid "C4305: 'return': truncation from 'unsigned int' to 'bool'" on MSVC
return bool(value);
}
constexpr operator unsigned int() const {
return value;
}
};
template<class T> constexpr Tags<TypeTraits<T>::Index> tags(T) {
return Tags<TypeTraits<T>::Index>{Init};
}
template<unsigned int value> constexpr Tags<value> tags(Tags<value> tags) {
return tags;
}
// Base-2 log, "plus one" (returns 0 for A == 0). For a base tag, which is always just one bit, returns position of that bit.
// Used for calculating distance between two tags in order to calculate overload priority. But since there's also
// the Scalar tag, which is 0, it's plus one.
template<unsigned short A> struct BitIndex {
enum : unsigned short {
Value = 1 + BitIndex<(A >> 1)>::Value
};
};
template<> struct BitIndex<0> {
enum : unsigned short {
Value = 0
};
};
// Popcount, but constexpr. No, not related to Cpu::Popcnt, in any way. Used for calculating a difference between
// two extra tag sets in order to calculate overload priority.
template<unsigned short A> struct BitCount {
enum : unsigned short {
Bits1 = 0x5555, /* 0b0101010101010101 */
Bits2 = 0x3333, /* 0b0011001100110011 */
Bits4 = 0x0f0f, /* 0b0000111100001111 */
Bits8 = 0x00ff, /* 0b0000000011111111 */
B0 = (A >> 0) & Bits1,
B1 = (A >> 1) & Bits1,
C = B0 + B1,
D0 = (C >> 0) & Bits2,
D2 = (C >> 2) & Bits2,
E = D0 + D2,
F0 = (E >> 0) & Bits4,
F4 = (E >> 4) & Bits4,
G = F0 + F4,
H0 = (G >> 0) & Bits8,
H8 = (G >> 8) & Bits8,
Value = H0 + H8
};
};
// Calculates an absolute priority index for given tag, which is either a base-2 log "plus one" of its index
// if it's a base tag, or is 1 if it's an extra tag.
//
// MSVC 2015 and 2017 need the extra () inside Priority<>, otherwise they demand that a typename is used
// for TypeTraits<T>::Index. Heh. Clang 14 warns if I don't cast because bitwise operations between
// different enums are deprecated in C++20.
template<class T> Priority<(static_cast<unsigned int>(TypeTraits<T>::Index)& ExtraTagMask ? 1 : BitIndex<static_cast<unsigned int>(TypeTraits<T>::Index)& BaseTagMask>::Value * (ExtraTagCount + 1))> constexpr priority(T) {
return {};
}
template<unsigned int value> Priority<(BitIndex<value& BaseTagMask>::Value* (ExtraTagCount + 1) + BitCount<((value& ExtraTagMask) >> ExtraTagBitOffset)>::Value)> constexpr priority(Tags<value>) {
static_assert(!((value & BaseTagMask) & ((value & BaseTagMask) - 1)), "more than one base tag used");
// GCC 4.8 loudly complains about enum comparison if I don't cast, sigh
static_assert(((value & ExtraTagMask) >> ExtraTagBitOffset) < (1 << static_cast<unsigned int>(ExtraTagCount)), "extra tag out of expected bounds");
return {};
}
}
/**
@brief Default base tag type
See the @ref DefaultBase tag for more information.
*/
typedef
#if defined(DEATH_TARGET_X86)
# if defined(DEATH_TARGET_AVX512F)
Avx512fT
# elif defined(DEATH_TARGET_AVX2)
Avx2T
# elif defined(DEATH_TARGET_AVX)
AvxT
# elif defined(DEATH_TARGET_SSE42)
Sse42T
# elif defined(DEATH_TARGET_SSE41)
Sse41T
# elif defined(DEATH_TARGET_SSSE3)
Ssse3T
# elif defined(DEATH_TARGET_SSE3)
Sse3T
# elif defined(DEATH_TARGET_SSE2)
Sse2T
#else
ScalarT
# endif
#elif defined(DEATH_TARGET_ARM)
# if defined(DEATH_TARGET_NEON_FP16)
NeonFp16T
# elif defined(DEATH_TARGET_NEON_FMA)
NeonFmaT
# elif defined(DEATH_TARGET_NEON)
NeonT
# else
ScalarT
# endif
#elif defined(DEATH_TARGET_WASM)
# if defined(DEATH_TARGET_SIMD128)
Simd128T
# else
ScalarT
# endif
#else
ScalarT
#endif
DefaultBaseT;
/**
@brief Default extra tag type
See the @ref DefaultExtra tag for more information.
*/
// Clang 14 warns if zero (an int) isn't first because bitwise operations between different enums are deprecated in C++20.
typedef Implementation::Tags<0
#if defined(DEATH_TARGET_X86)
# if defined(DEATH_TARGET_POPCNT)
| TypeTraits<PopcntT>::Index
# endif
# if defined(DEATH_TARGET_LZCNT)
| TypeTraits<LzcntT>::Index
# endif
# if defined(DEATH_TARGET_BMI1)
| TypeTraits<Bmi1T>::Index
# endif
# if defined(DEATH_TARGET_BMI2)
| TypeTraits<Bmi2T>::Index
# endif
# if defined(DEATH_TARGET_AVX_FMA)
| TypeTraits<AvxFmaT>::Index
# endif
# if defined(DEATH_TARGET_AVX_F16C)
| TypeTraits<AvxF16cT>::Index
# endif
#endif
> DefaultExtraT;
/**
@brief Default tag type
See the @ref Default tag for more information.
*/
// Clang 14 warns if I don't cast because bitwise operations between different enums are deprecated in C++20
typedef Implementation::Tags<static_cast<unsigned int>(TypeTraits<DefaultBaseT>::Index) | DefaultExtraT::Value> DefaultT;
/**
@brief Default base tag
Highest base instruction set available on given architecture with current
compiler flags. Ordered by priority, on @ref DEATH_TARGET_X86 it's one of these:
- @ref Avx512f if @ref DEATH_TARGET_AVX512F is defined
- @ref Avx2 if @ref DEATH_TARGET_AVX2 is defined
- @ref Avx if @ref DEATH_TARGET_AVX is defined
- @ref Sse42 if @ref DEATH_TARGET_SSE42 is defined
- @ref Sse41 if @ref DEATH_TARGET_SSE41 is defined
- @ref Ssse3 if @ref DEATH_TARGET_SSSE3 is defined
- @ref Sse3 if @ref DEATH_TARGET_SSE3 is defined
- @ref Sse2 if @ref DEATH_TARGET_SSE2 is defined
- @ref Scalar otherwise
On @ref DEATH_TARGET_ARM it's one of these:
- @ref NeonFp16 if @ref DEATH_TARGET_NEON_FP16 is defined
- @ref NeonFma if @ref DEATH_TARGET_NEON_FMA is defined
- @ref Neon if @ref DEATH_TARGET_NEON is defined
- @ref Scalar otherwise
On @ref DEATH_TARGET_WASM it's one of these:
- @ref Simd128 if @ref DEATH_TARGET_SIMD128 is defined
- @ref Scalar otherwise
In addition to the above, @ref DefaultExtra contains a combination of extra
instruction sets available together with the base instruction set, and
@ref Default is a combination of both. See also @ref compiledFeatures() which
returns a *combination* of base tags instead of just the highest available,
together with the extra instruction sets, and @ref runtimeFeatures() which is
capable of detecting the available CPU feature set at runtime.
*/
constexpr DefaultBaseT DefaultBase{Implementation::Init};
/**
@brief Default extra tags
Instruction sets available in addition to @ref DefaultBase on
given architecture with current compiler flags. On @ref DEATH_TARGET_X86 it's
a combination of these:
- @ref Popcnt if @ref DEATH_TARGET_POPCNT is defined
- @ref Lzcnt if @ref DEATH_TARGET_LZCNT is defined
- @ref Bmi1 if @ref DEATH_TARGET_BMI1 is defined
- @ref Bmi2 if @ref DEATH_TARGET_BMI2 is defined
- @ref AvxFma if @ref DEATH_TARGET_AVX_FMA is defined
- @ref AvxF16c if @ref DEATH_TARGET_AVX_F16C is defined
No extra instruction sets are currently defined for @ref DEATH_TARGET_ARM or
@ref DEATH_TARGET_WASM.
In addition to the above, @ref Default is a combination of both
@ref DefaultBase and the extra instruction sets. See also
@ref compiledFeatures() which returns these together with a combination of all
base instruction sets available, and @ref runtimeFeatures() which is capable of
detecting the available CPU feature set at runtime.
*/
constexpr DefaultExtraT DefaultExtra{Implementation::Init};
/**
@brief Default tags
A combination of @ref DefaultBase and @ref DefaultExtra, see their
documentation for more information.
*/
constexpr DefaultT Default{Implementation::Init};
/**
@brief Tag for a tag type
Returns a tag corresponding to tag type @p T.
*/
template<class T> constexpr T tag() {
return T{Implementation::Init};
}
#if defined(DEATH_TARGET_ARM) && ((defined(__linux__) && !(defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(__FreeBSD__))
namespace Implementation
{
// Needed for a friend declaration, implementation is at the very end of the header
Features runtimeFeatures(unsigned long caps);
}
#endif
/**
@brief Feature set
Provides storage and comparison as well as runtime detection of CPU instruction set.
See the @ref Cpu namespace for an overview and usage examples.
*/
class Features {
public:
/**
* @brief Default constructor
*
* Equivalent to @ref Scalar.
*/
constexpr explicit Features() noexcept : _data{} {}
/**
* @brief Construct from a tag
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr /*implicit*/ Features(T) noexcept : _data{TypeTraits<T>::Index} {
// GCC 4.8 loudly complains about enum comparison if I don't cast; Clang 14 warns if I don't cast
// because bitwise operations between different enums are deprecated in C++20
static_assert(((static_cast<unsigned int>(TypeTraits<T>::Index) & Implementation::ExtraTagMask) >> Implementation::ExtraTagBitOffset) < (1 << static_cast<unsigned int>(Implementation::ExtraTagCount)),
"Extra tag out of expected bounds");
}
#ifndef DOXYGEN_GENERATING_OUTPUT
// The compile-time Tags<> is an implementation detail, don't show that in the docs
template<unsigned int value> constexpr /*implicit*/ Features(Implementation::Tags<value>) noexcept : _data{value} {}
#endif
/** @brief Equality comparison */
constexpr bool operator==(Features other) const {
return _data == other._data;
}
/** @brief Non-equality comparison */
constexpr bool operator!=(Features other) const {
return _data != other._data;
}
/**
* @brief Whether @p other is a subset of this (@f$ a \supseteq o @f$)
*
* Equivalent to @cpp (a & other) == other @ce.
*/
constexpr bool operator>=(Features other) const {
return (_data & other._data) == other._data;
}
/**
* @brief Whether @p other is a superset of this (@f$ a \subseteq o @f$)
*
* Equivalent to @cpp (a & other) == a @ce.
*/
constexpr bool operator<=(Features other) const {
return (_data & other._data) == _data;
}
/** @brief Union of two feature sets */
constexpr Features operator|(Features other) const {
return Features{_data | other._data};
}
/** @brief Union two feature sets and assign */
Features& operator|=(Features other) {
_data |= other._data;
return *this;
}
/** @brief Intersection of two feature sets */
constexpr Features operator&(Features other) const {
return Features{_data & other._data};
}
/** @brief Intersect two feature sets and assign */
Features& operator&=(Features other) {
_data &= other._data;
return *this;
}
/** @brief XOR of two feature sets */
constexpr Features operator^(Features other) const {
return Features{_data ^ other._data};
}
/** @brief XOR two feature sets and assign */
Features& operator^=(Features other) {
_data ^= other._data;
return *this;
}
/** @brief Feature set complement */
constexpr Features operator~() const {
return Features{~_data};
}
/**
* @brief Boolean conversion
*
* Returns @cpp true @ce if at least one feature apart from @ref Scalar
* is present, @cpp false @ce otherwise.
*/
constexpr explicit operator bool() const {
return _data;
}
/**
* @brief Integer representation
*
* For testing purposes. @ref Cpu::Scalar is always @cpp 0 @ce, values
* corresponding to other feature tags are unspecified.
*/
constexpr explicit operator unsigned int() const {
return _data;
}
private:
template<class> friend constexpr Features features();
friend constexpr Features compiledFeatures();
#if (defined(DEATH_TARGET_X86) && (defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_GCC))) || (defined(DEATH_TARGET_ARM) && defined(DEATH_TARGET_APPLE))
friend Features runtimeFeatures();
#endif
#if defined(DEATH_TARGET_ARM) && ((defined(__linux__) && !(defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(__FreeBSD__))
friend Features Implementation::runtimeFeatures(unsigned long);
#endif
constexpr explicit Features(unsigned int data) noexcept : _data{data} {}
unsigned int _data;
};
/**
@brief Feature set for a tag type
Returns @ref Features with a tag corresponding to tag type @p T, avoiding a
need to form the tag value in order to pass it to @ref Features::Features(T).
*/
template<class T> constexpr Features features() {
return Features{TypeTraits<T>::Index};
}
/** @relates Features
@brief Equality comparison of a tag and a feature set
Same as @ref Features::operator==().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator==(T a, Features b) {
return Features(a) == b;
}
/** @relates Features
@brief Equality comparison of two tags
Same as @ref Features::operator==(). Needs to be present to avoid ambiguity in C++20.
*/
template<class T, class U, class = decltype(TypeTraits<T>::Index), class = decltype(TypeTraits<U>::Index)> constexpr bool operator==(T, U) {
// Need to cast because operations between different enums are deprecated in C++20.
return static_cast<unsigned int>(TypeTraits<T>::Index) == TypeTraits<U>::Index;
}
/** @relates Features
@brief Non-equality comparison of a tag and a feature set
Same as @ref Features::operator!=().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator!=(T a, Features b) {
return Features(a) != b;
}
/** @relates Features
@brief Non-equality comparison of two tags
Same as @ref Features::operator!=(). Needs to be present to avoid ambiguity in
C++20.
*/
template<class T, class U, class = decltype(TypeTraits<T>::Index), class = decltype(TypeTraits<U>::Index)> constexpr bool operator!=(T, U) {
// Need to cast because operations between different enums are deprecated in C++20.
return static_cast<unsigned int>(TypeTraits<T>::Index) != TypeTraits<U>::Index;
}
/** @relates Features
@brief Whether @p a is a superset of @p b (@f$ a \supseteq b @f$)
Same as @ref Features::operator>=().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator>=(T a, Features b) {
return Features(a) >= b;
}
/** @relates Features
@brief Whether @p a is a subset of @p b (@f$ a \subseteq b @f$)
Same as @ref Features::operator<=().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator<=(T a, Features b) {
return Features(a) <= b;
}
/** @relates Features
@brief Union of two feature sets
Same as @ref Features::operator|().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator|(T a, Features b) {
return b | a;
}
#ifndef DOXYGEN_GENERATING_OUTPUT
// Compared to the above, this produces a type that encodes the value instead of Features. Has to be in the same namespace
// as the tags, but since Tags<> is an implementation detail, this is hidden from plain sight as well.
//
// Clang 14 warns if I don't cast because bitwise operations between different enums are deprecated in C++20.
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) | TypeTraits<U>::Index> operator|(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) | TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index | value> operator|(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index | value>{Implementation::Init};
}
#endif
/** @relates Features
@brief Intersection of two feature sets
Same as @ref Features::operator&().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator&(T a, Features b) {
return b & a;
}
#ifndef DOXYGEN_GENERATING_OUTPUT
// Compared to the above, this produces a type that encodes the value instead of Features. Has to be in the same namespace
// as the tags, but since Tags<> is an implementation detail, this is hidden from plain sight as well.
//
// Clang 14 warns if I don't cast because bitwise operations between different enums are deprecated in C++20.
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index)& TypeTraits<U>::Index> operator&(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index)& TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index& value> operator&(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index& value>{Implementation::Init};
}
#endif
/** @relates Features
@brief XOR of two feature sets
Same as @ref Features::operator^().
*/
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator^(T a, Features b) {
return b ^ a;
}
#ifndef DOXYGEN_GENERATING_OUTPUT
// Compared to the above, this produces a type that encodes the value instead of Features. Has to be in the same namespace
// as the tags, but since Tags<> is an implementation detail, this is hidden from plain sight as well.
//
// Clang 14 warns if I don't cast because bitwise operations between different enums are deprecated in C++20.
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) ^ TypeTraits<U>::Index> operator^(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) ^ TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index^ value> operator^(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index^ value>{Implementation::Init};
}
#endif
/** @relates Features
@brief Feature set complement
Same as @ref Features::operator~().
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> constexpr Features operator~(T a);
#else
// To avoid confusion, to the docs use it's shown that the operator produces a Features, but in fact it's a type with a compile-time-encoded value
template<class T> constexpr Implementation::Tags<~TypeTraits<T>::Index> operator~(T) {
return Implementation::Tags<~TypeTraits<T>::Index>{Implementation::Init};
}
#endif
/**
@brief CPU instruction sets enabled at compile time
On @ref DEATH_TARGET_X86 "x86" returns a combination of @ref Sse2, @ref Sse3,
@ref Ssse3, @ref Sse41, @ref Sse42, @ref Popcnt, @ref Lzcnt, @ref Bmi1,
@ref Bmi2, @ref Avx, @ref AvxF16c, @ref AvxFma, @ref Avx2 and @ref Avx512f
based on what all @ref DEATH_TARGET_SSE2 etc. preprocessor variables are defined.
On @ref DEATH_TARGET_ARM "ARM", returns a combination of @ref Neon,
@ref NeonFma and @ref NeonFp16 based on what all @ref DEATH_TARGET_NEON etc.
preprocessor variables are defined.
On @ref DEATH_TARGET_WASM "WebAssembly", returns @ref Simd128 based on
whether the @ref DEATH_TARGET_SIMD128 preprocessor variable is defined.
On other platforms or if no known CPU instruction set is enabled, the returned
value is equal to @ref Scalar, which in turn is equivalent to empty (or
default-constructed) @ref Features.
*/
constexpr Features compiledFeatures() {
// Clang 14 warns if zero (an int) isn't first because bitwise operations between different enums are deprecated in C++20.
return Features{0
#if defined(DEATH_TARGET_X86)
# if defined(DEATH_TARGET_SSE2)
| TypeTraits<Sse2T>::Index
# endif
# if defined(DEATH_TARGET_SSE3)
| TypeTraits<Sse3T>::Index
# endif
# if defined(DEATH_TARGET_SSSE3)
| TypeTraits<Ssse3T>::Index
# endif
# if defined(DEATH_TARGET_SSE41)
| TypeTraits<Sse41T>::Index
# endif
# if defined(DEATH_TARGET_SSE42)
| TypeTraits<Sse42T>::Index
# endif
# if defined(DEATH_TARGET_POPCNT)
| TypeTraits<PopcntT>::Index
# endif
# if defined(DEATH_TARGET_LZCNT)
| TypeTraits<LzcntT>::Index
# endif
# if defined(DEATH_TARGET_BMI1)
| TypeTraits<Bmi1T>::Index
# endif
# if defined(DEATH_TARGET_BMI2)
| TypeTraits<Bmi2T>::Index
# endif
# if defined(DEATH_TARGET_AVX)
| TypeTraits<AvxT>::Index
# endif
# if defined(DEATH_TARGET_AVX_FMA)
| TypeTraits<AvxFmaT>::Index
# endif
# if defined(DEATH_TARGET_AVX_F16C)
| TypeTraits<AvxF16cT>::Index
# endif
# if defined(DEATH_TARGET_AVX2)
| TypeTraits<Avx2T>::Index
# endif
# if defined(DEATH_TARGET_AVX512F)
| TypeTraits<Avx512fT>::Index
# endif
#elif defined(DEATH_TARGET_ARM)
# if defined(DEATH_TARGET_NEON)
| TypeTraits<NeonT>::Index
# endif
# if defined(DEATH_TARGET_NEON_FMA)
| TypeTraits<NeonFmaT>::Index
# endif
# if defined(DEATH_TARGET_NEON_FP16)
| TypeTraits<NeonFp16T>::Index
# endif
#elif defined(DEATH_TARGET_WASM)
# if defined(DEATH_TARGET_SIMD128)
| TypeTraits<Simd128T>::Index
# endif
#endif
};
}
/**
@brief Detect available CPU instruction sets at runtime
On @ref DEATH_TARGET_X86 "x86" and GCC, Clang or MSVC uses the
[CPUID](https://en.wikipedia.org/wiki/CPUID) builtin to check for the
@ref Sse2, @ref Sse3, @ref Ssse3, @ref Sse41, @ref Sse42, @ref Popcnt,
@ref Lzcnt, @ref Bmi1, @ref Bmi2, @ref Avx, @ref AvxF16c, @ref AvxFma,
@ref Avx2 and @ref Avx512f runtime features. @ref Avx needs OS support as well,
if it's not present, no following flags including @ref Bmi1 and @ref Bmi2 are
checked either. On compilers other than GCC, Clang and MSVC the function is
@cpp constexpr @ce and delegates into @ref compiledFeatures().
On @ref DEATH_TARGET_ARM "ARM" and Linux or Android API level 18+ uses
@m_class{m-doc-external} [getauxval()](https://man.archlinux.org/man/getauxval.3),
or on ARM macOS and iOS uses @m_class{m-doc-external} [sysctlbyname()](https://developer.apple.com/documentation/kernel/1387446-sysctlbyname)
to check for the @ref Neon, @ref NeonFma and @ref NeonFp16. @ref Neon and
@ref NeonFma are implicitly supported on ARM64. On other platforms the function
is @cpp constexpr @ce and delegates into @ref compiledFeatures().
On @ref DEATH_TARGET_WASM "WebAssembly" an attempt to use SIMD instructions
without runtime support results in a WebAssembly compilation error and thus
runtime detection is largely meaningless. While this may change once the
[feature detection proposal](https://github.com/WebAssembly/feature-detection/blob/main/proposals/feature-detection/Overview.md)
is implemented, at the moment the function is @cpp constexpr @ce and delegates
into @ref compiledFeatures().
On other platforms or if no known CPU instruction set is detected, the returned
value is equal to @ref Scalar, which in turn is equivalent to empty (or
default-constructed) @ref Features.
*/
#if (defined(DEATH_TARGET_X86) && (defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_GCC))) || (defined(DEATH_TARGET_ARM) && ((defined(__linux__) && !(defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(DEATH_TARGET_APPLE) || defined(__FreeBSD__)))
Features runtimeFeatures();
#else
constexpr Features runtimeFeatures() {
return compiledFeatures();
}
#endif
/**
@brief Declare a CPU tag for a compile-time dispatch
Meant to be used to declare a function overload that uses given combination of
CPU instruction sets. The @ref DEATH_CPU_SELECT() macro is a counterpart used
to select among overloads declared with this macro. See @ref Cpu-usage-extra
for more information and usage example.
Internally, this macro expands to two function parameter declarations separated
by a comma, one that ensures only an overload matching the desired instruction
sets get picked, and one that assigns an absolute priority to this overload.
*/
#define DEATH_CPU_DECLARE(tag) decltype(Death::Cpu::Implementation::tags(tag)), decltype(Death::Cpu::Implementation::priority(tag))
/**
@brief Select a CPU tag for a compile-time dispatch
Meant to be used to select among function overloads declared with
@ref DEATH_CPU_DECLARE() that best matches given combination of CPU
instruction sets. See @ref Cpu-usage-extra for more information.
Internally, this macro expands to two function parameter values separated by a
comma, one that contains the desired instruction sets to filter the overloads
against and another that converts the sets to an absolute priority to pick the
best viable overload.
*/
#define DEATH_CPU_SELECT(tag) tag, Death::Cpu::Implementation::priority(tag)
/**
@brief Create a function for a runtime dispatch on a base CPU instruction set
Given a set of function overloads named @p function that accept a CPU tag as a
parameter, all returning a function pointer of the same type, creates a
function with signature @cpp function(Cpu::Features) @ce which will select
among the overloads using a runtime-specified
@relativeref{Death,Cpu::Features}, using the same rules as the compile-time
overload selection. For this macro to work, at the very least there has to be
an overload with a @relativeref{Death,Cpu::ScalarT} argument. See
@ref Cpu-usage-automatic-runtime-dispatch for more information.
This function works with just a single base CPU instruction tag such as
@relativeref{Death,Cpu::Avx2} or @relativeref{Death,Cpu::Neon}, but not the
extra instruction sets like @relativeref{Death,Cpu::Lzcnt} or
@relativeref{Death,Cpu::AvxFma}. For a dispatch that takes extra instruction
sets into account as well use @ref DEATH_CPU_DISPATCHER() instead.
*/
// Ideally this would reuse __DEATH_CPU_DISPATCHER_IMPLEMENTATION(), unfortunately due to MSVC not being able
// to defer macro calls unless "the new preprocessor" is enabled it wouldn't be possible to get rid of the
// DEATH_CPU_SELECT() macro from there.
#ifdef DOXYGEN_GENERATING_OUTPUT
#define DEATH_CPU_DISPATCHER_BASE(function)
#elif defined(DEATH_TARGET_X86)
#define __DEATH_CPU_DISPATCHER_BASE(function) \
decltype(function(Death::Cpu::Scalar)) function(Death::Cpu::Features features) { \
if(features & Death::Cpu::Avx512f) \
return function(Death::Cpu::Avx512f); \
if(features & Death::Cpu::Avx2) \
return function(Death::Cpu::Avx2); \
if(features & Death::Cpu::Avx) \
return function(Death::Cpu::Avx); \
if(features & Death::Cpu::Sse42) \
return function(Death::Cpu::Sse42); \
if(features & Death::Cpu::Sse41) \
return function(Death::Cpu::Sse41); \
if(features & Death::Cpu::Ssse3) \
return function(Death::Cpu::Ssse3); \
if(features & Death::Cpu::Sse3) \
return function(Death::Cpu::Sse3); \
if(features & Death::Cpu::Sse2) \
return function(Death::Cpu::Sse2); \
return function(Death::Cpu::Scalar); \
}
#elif defined(DEATH_TARGET_ARM)
#define __DEATH_CPU_DISPATCHER_BASE(function) \
decltype(function(Death::Cpu::Scalar)) function(Death::Cpu::Features features) { \
if(features & Death::Cpu::NeonFp16) \
return function(Death::Cpu::NeonFp16); \
if(features & Death::Cpu::NeonFma) \
return function(Death::Cpu::NeonFma); \
if(features & Death::Cpu::Neon) \
return function(Death::Cpu::Neon); \
return function(Death::Cpu::Scalar); \
}
#elif defined(DEATH_TARGET_WASM)
#define __DEATH_CPU_DISPATCHER_BASE(function) \
decltype(function(Death::Cpu::Scalar)) function(Death::Cpu::Features features) { \
if(features & Death::Cpu::Simd128) \
return function(Death::Cpu::Simd128); \
return function(Death::Cpu::Scalar); \
}
#else
#define __DEATH_CPU_DISPATCHER_BASE(function) \
decltype(function(Death::Cpu::Scalar)) function(Death::Cpu::Features features) { \
return function(Death::Cpu::Scalar); \
}
#endif
#ifndef DOXYGEN_GENERATING_OUTPUT
#if defined(DEATH_TARGET_X86)
#define __DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, extra) \
if(features >= (Death::Cpu::Avx512f extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Avx512f extra)); \
if(features >= (Death::Cpu::Avx2 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Avx2 extra)); \
if(features >= (Death::Cpu::Avx extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Avx extra)); \
if(features >= (Death::Cpu::Sse42 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Sse42 extra)); \
if(features >= (Death::Cpu::Sse41 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Sse41 extra)); \
if(features >= (Death::Cpu::Ssse3 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Ssse3 extra)); \
if(features >= (Death::Cpu::Sse3 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Sse3 extra)); \
if(features >= (Death::Cpu::Sse2 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Sse2 extra)); \
return function(DEATH_CPU_SELECT(Death::Cpu::Scalar extra));
#elif defined(DEATH_TARGET_ARM)
#define __DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, extra) \
if(features >= (Death::Cpu::NeonFp16 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::NeonFp16 extra)); \
if(features >= (Death::Cpu::NeonFma extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::NeonFma extra)); \
if(features >= (Death::Cpu::Neon extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Neon extra)); \
return function(DEATH_CPU_SELECT(Death::Cpu::Scalar extra));
#elif defined(DEATH_TARGET_WASM)
#define __DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, extra) \
if(features >= (Death::Cpu::Simd128 extra)) \
return function(DEATH_CPU_SELECT(Death::Cpu::Simd128 extra)); \
return function(DEATH_CPU_SELECT(Death::Cpu::Scalar extra));
#else
#define __DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, extra) \
return function(DEATH_CPU_SELECT(Death::Cpu::Scalar extra));
#endif
// __DEATH_CPU_DISPATCHER() specialization for 0 extra instruction sets. Basically equivalent to DEATH_CPU_DISPATCHER_BASE()
// except for the extra DEATH_CPU_SELECT() macro.
#define __DEATH_CPU_DISPATCHER0(function) \
decltype(function(DEATH_CPU_SELECT(Death::Cpu::Scalar))) function(Death::Cpu::Features features) { \
__DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, ) \
}
// __DEATH_CPU_DISPATCHER() specialization for 1+ extra instruction sets. On Clang this still generates quite a reasonable code
// for 2 extra sets compared to DEATH_CPU_DISPATCHER_BASE(), on GCC it's ~25% longer but also still reasonable.
// I attempted adding an "unrolled" __DEATH_CPU_DISPATCHER2() but it made everything significantly worse on both compilers,
// more than doubling the amount of generated code.
#define __DEATH_CPU_DISPATCHERn(function, ...) \
template<unsigned int value> DEATH_ALWAYS_INLINE decltype(function(DEATH_CPU_SELECT(Death::Cpu::Scalar))) function ## Internal(Death::Cpu::Features features, Death::Cpu::Implementation::Tags<value>) { \
__DEATH_CPU_DISPATCHER_IMPLEMENTATION(function, |Death::Cpu::Implementation::Tags<value>{Death::Cpu::Implementation::Init}) \
} \
template<unsigned int value, class First, class ...Next> DEATH_ALWAYS_INLINE decltype(function(DEATH_CPU_SELECT(Death::Cpu::Scalar))) function ## Internal(Death::Cpu::Features features, Death::Cpu::Implementation::Tags<value> extra, First first, Next... next) { \
static_assert(!(static_cast<unsigned int>(Death::Cpu::Implementation::tags(First{Death::Cpu::Implementation::Init})) & Death::Cpu::Implementation::BaseTagMask), \
"Only extra instruction set tags should be explicitly listed"); \
if(features & first) \
return function ## Internal(features, extra|first, next...); \
else \
return function ## Internal(features, extra, next...); \
} \
decltype(function(DEATH_CPU_SELECT(Death::Cpu::Scalar))) function(Death::Cpu::Features features) { \
return function ## Internal(features, Death::Cpu::Implementation::Tags<0>{Death::Cpu::Implementation::Init}, __VA_ARGS__); \
}
#endif
/**
@brief Create a function for a runtime dispatch on a base CPU instruction set and select extra instruction sets
Given a set of function overloads named @p function that accept a CPU tag
combination wrapped in @ref DEATH_CPU_DECLARE() as a parameter, all returning
a function pointer of the same type, creates a function with signature
@cpp function(Cpu::Features) @ce which will select among the overloads using a
runtime-specified @relativeref{Death,Cpu::Features}, using the same rules as
the compile-time overload selection. The extra instruction sets considered in
the overload selection are specified as additional parameters to the macro,
specifying none is valid as well. For this macro to work, at the very least
there has to be an overload with a
@ref Death::Cpu::Scalar "DEATH_CPU_DECLARE(Cpu::Scalar)" argument. See
@ref Cpu-usage-automatic-runtime-dispatch for more information.
For a dispatch using just the base instruction set use
@ref DEATH_CPU_DISPATCHER_BASE() instead.
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
#define DEATH_CPU_DISPATCHER(function, ...)
#else
#define __DEATH_CPU_DISPATCHER(...) \
DEATH_HELPER_EXPAND(DEATH_HELPER_PICK(__VA_ARGS__, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHERn, __DEATH_CPU_DISPATCHER0, )(__VA_ARGS__))
#endif
#ifdef DOXYGEN_GENERATING_OUTPUT
/**
@brief Create a dispatched function according to the build configuration
Assuming a @p dispatcher was defined with either @ref DEATH_CPU_DISPATCHER()
or @ref DEATH_CPU_DISPATCHER_BASE(), will automatically use @ref DEATH_CPU_DISPATCHED_POINTER(),
@ref DEATH_CPU_DISPATCHED_IFUNC() or nothing depending on the build configuration.
See @ref Cpu-usage for more information and overhead comparison.
*/
#define DEATH_CPU_DISPATCHED(dispatcher, ...)
#endif
/**
@brief Create a runtime-dispatched function pointer
Assuming a @p dispatcher was defined with either @ref DEATH_CPU_DISPATCHER()
or @ref DEATH_CPU_DISPATCHER_BASE(), defines a function pointer variable with
a signature specified in the second variadic argument. In a global constructor
the variable is assigned a function pointer returned by @p dispatcher for
@relativeref{Death,Cpu::runtimeFeatures()}.
The pointer can be changed afterwards, such as for testing purposes, See also
@ref DEATH_CPU_DISPATCHED_IFUNC() which avoids the overhead of function
pointer indirection.
See @ref Cpu-usage-automatic-cached-dispatch for more information and overhead comparison.
*/
#define DEATH_CPU_DISPATCHED_POINTER(dispatcher, ...) \
__VA_ARGS__ = dispatcher(Death::Cpu::runtimeFeatures());
/**
@brief Create a runtime-dispatched function via GNU IFUNC
Available only if @ref DEATH_CPU_USE_IFUNC is enabled. Assuming a
@p dispatcher was defined with either @ref DEATH_CPU_DISPATCHER() or
@ref DEATH_CPU_DISPATCHER_BASE(), defines a function with a signature
specified via the third variadic argument. The signature has to match @p type.
The function uses the [GNU IFUNC](https://sourceware.org/glibc/wiki/GNU_IFUNC)
mechanism, which causes the function call to be resolved to a function pointer
returned by @p dispatcher for @relativeref{Death,Cpu::runtimeFeatures()}. The
dispatch is performed by the dynamic linker during early startup and cannot be
changed afterwards.
If @ref DEATH_CPU_USE_IFUNC isn't available, is explicitly disabled or if
you need to be able to subsequently change the dispatched-to function (such as
for testing purposes), use @ref DEATH_CPU_DISPATCHED_POINTER() instead.
See @ref Cpu-usage-automatic-cached-dispatch for more information and overhead comparison.
*/
#if defined(DEATH_CPU_USE_IFUNC) || defined(DOXYGEN_GENERATING_OUTPUT)
// On ARM we get CPU features through getauxval() but it can't be called from an ifunc resolver because it's too early at that point.
// Instead, AT_HWCAPS is passed to it from outside, so there we call an internal variant with the caps parameter. On x86 calling into CPUID
// from within an ifunc resolver is no problem.
//
// Although not specifically documented anywhere, the dispatcher has to have C++ mangling disabled in order to be found by __attribute__((ifunc)),
// on both GCC and Clang. That however means it's exported even if inside an anonymous namespace, which is undesirable. To fix that,
// it's marked as static...
#if defined(DEATH_TARGET_CLANG)
// ... unfortunately the static makes Clang not find the name again, so there we can't use it. But, to drown even deeper, not using the static causes
// the -Wmissing-prototypes macro to get fired (which is enabled globally because it has obvious benefits), so to avoid noise it has to be disabled here.
#if !defined(DEATH_TARGET_ARM)
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"") \
extern "C" decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher() { \
return dispatcher(Death::Cpu::runtimeFeatures()); \
} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher))); \
_Pragma("GCC diagnostic pop")
#else
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"") \
extern "C" decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher(unsigned long caps) { \
return dispatcher(Death::Cpu::Implementation::runtimeFeatures(caps)); \
} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher))); \
_Pragma("GCC diagnostic pop")
#endif
#elif defined(DEATH_TARGET_GCC) && __GNUC__*100 + __GNUC_MINOR__ < 409
// Furthermore, due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58105 the resolver function won't work on GCC 4.8 unless it's marked with
// DEATH_NEVER_INLINE. That, however, causes GCC 4.8 to spit out a bogus warning that the dispatcher function is redeclared as noinline -- it thinks
// it's the same function as the always-inline lambda wrappers which have the same name. Despite the warning it works, but to avoid useless noise the
// ifunc dispatcher is named differently.
#if !defined(DEATH_TARGET_ARM)
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
extern "C" { DEATH_NEVER_INLINE static decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher ## Ifunc() { \
return dispatcher(Death::Cpu::runtimeFeatures()); \
}} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher "Ifunc")));
#else
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
extern "C" { DEATH_NEVER_INLINE static decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher ## Ifunc(unsigned long caps) { \
return dispatcher(Death::Cpu::Implementation::runtimeFeatures(caps)); \
}} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher "Ifunc")));
#endif
#else
// Only GCC 4.9+ has the implementation in the most minimal form.
#if !defined(DEATH_TARGET_ARM)
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
extern "C" { static decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher() { \
return dispatcher(Death::Cpu::runtimeFeatures()); \
}} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher)));
#else
#define DEATH_CPU_DISPATCHED_IFUNC(dispatcher, ...) \
extern "C" { static decltype(dispatcher(std::declval<Death::Cpu::Features>())) dispatcher(unsigned long caps) { \
return dispatcher(Death::Cpu::Implementation::runtimeFeatures(caps)); \
}} \
__VA_ARGS__ __attribute__((ifunc(#dispatcher)));
#endif
#endif
#endif
#if defined(DEATH_TARGET_X86) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief Enable SSE2 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang and @ref DEATH_TARGET_CLANG_CL "Clang-CL"
expands to @cpp __attribute__((__target__("sse2"))) @ce, allowing use of
[SSE2](https://en.wikipedia.org/wiki/SSE2) and earlier SSE instructions inside
a function annotated with this macro without having to specify `-msse2` for the
whole compilation unit. On x86 MSVC expands to nothing, as the compiler doesn't
restrict use of intrinsics in any way. Not defined on other compilers or
architectures.
As a special case, if @ref DEATH_TARGET_SSE2 is present (meaning SSE2 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Implied by @ref DEATH_ENABLE_SSE3. See @ref Cpu-usage-target-attributes for
more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsSse2.h instead of
@cpp #include <emmintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_SSE2) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SSE2
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE2
# endif
#elif defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_SSE2 __attribute__((__target__("sse2")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE2 "sse2",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_SSE2
#endif
/**
@brief Enable SSE3 for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("sse3"))) @ce, allowing use of
[SSE3](https://en.wikipedia.org/wiki/SSE3) and earlier SSE intrinsics inside a
function annotated with this macro without having to specify `-msse3` for the
whole compilation unit. On x86 MSVC expands to nothing, as the compiler doesn't
restrict use of intrinsics in any way. Not defined on other compilers or
architectures.
As a special case, if @ref DEATH_TARGET_SSE3 is present (meaning SSE3 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_SSE2, implied by @ref DEATH_ENABLE_SSSE3. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsSse3.h instead of
@cpp #include <pmmintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_SSE3) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SSE3
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE3
# endif
#elif defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
// The -msse3 option implies -msse2 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -msse3`)
# define DEATH_ENABLE_SSE3 __attribute__((__target__("sse3")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE3 "sse3",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_SSE3
#endif
/**
@brief Enable SSSE3 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang and @ref DEATH_TARGET_CLANG_CL "Clang-CL"
expands to @cpp __attribute__((__target__("ssse3"))) @ce, allowing use of
[SSSE3](https://en.wikipedia.org/wiki/SSSE3) and earlier SSE instructions
inside a function annotated with this macro without having to specify `-mssse3`
for the whole compilation unit. On x86 MSVC expands to nothing, as the compiler
doesn't restrict use of intrinsics in any way. Not defined on other compilers
or architectures.
As a special case, if @ref DEATH_TARGET_SSSE3 is present (meaning SSSE3 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_SSE3, implied by @ref DEATH_ENABLE_SSE41. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsSsse3.h instead of
@cpp #include <tmmintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_SSSE3) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SSSE3
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSSE3
# endif
#elif defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
// The -mssse3 option implies -msse2 -msse3 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -mssse3`)
# define DEATH_ENABLE_SSSE3 __attribute__((__target__("ssse3")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSSE3 "ssse3",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_SSSE3
#endif
/**
@brief Enable SSE4.1 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang and @ref DEATH_TARGET_CLANG_CL "Clang-CL"
expands to @cpp __attribute__((__target__("sse4.1"))) @ce, allowing use of
[SSE4.1](https://en.wikipedia.org/wiki/SSE4#SSE4.1) and earlier SSE
instructions inside a function annotated with this macro without having to
specify `-msse4.1` for the whole compilation unit. On x86 MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. Not
defined on other compilers or architectures.
As a special case, if @ref DEATH_TARGET_SSE41 is present (meaning SSE4.1 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_SSSE3, implied by @ref DEATH_ENABLE_SSE42.
See @ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-danger}
@par
Unless @ref DEATH_TARGET_SSE41 is present, this macro is not defined on
GCC 4.8, as SSE4.1 intrinsics only work if `-msse4.2` is specified as well
due to both SSE4.1 and 4.2 intrinsics living in the same header. You can
only use @ref DEATH_ENABLE_SSE42 in this case.
*/
#if defined(DEATH_TARGET_SSE41) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SSE41
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE41
# endif
#elif (defined(DEATH_TARGET_GCC) && __GNUC__*100 + __GNUC_MINOR__ >= 409) || defined(DEATH_TARGET_CLANG) /* also matches Clang-CL */
// The -msse4.1 option implies -msse2 -msse3 -mssse3 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -msse4.1`)
# define DEATH_ENABLE_SSE41 __attribute__((__target__("sse4.1")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE41 "sse4.1",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_SSE41
#endif
/**
@brief Enable SSE4.2 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang and @ref DEATH_TARGET_CLANG_CL "Clang-CL"
expands to @cpp __attribute__((__target__("sse4.2"))) @ce, allowing use of
[SSE4.2](https://en.wikipedia.org/wiki/SSE4#SSE4.2) and earlier SSE
instructions inside a function annotated with this macro without having to
specify `-msse4.2` for the whole compilation unit. On x86 MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. Not
defined on other compilers or architectures.
As a special case, if @ref DEATH_TARGET_SSE42 is defined (meaning SSE4.2 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_SSE41, implied by @ref DEATH_ENABLE_AVX. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsSse4.h instead of
@cpp #include <smmintrin.h> @ce and @cpp #include <nmmintrin.h> @ce to be
able to access the intrinsics on this compiler.
*/
#if defined(DEATH_TARGET_SSE42) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SSE42
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE42
# endif
#elif defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
// The -msse4.2 option implies -msse2 -msse3 -mssse3 -msse4.1 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -msse4.2`)
# define DEATH_ENABLE_SSE42 __attribute__((__target__("sse4.2")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SSE42 "sse4.2",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_SSE42
#endif
/**
@brief Enable POPCNT for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang and @ref DEATH_TARGET_CLANG_CL "Clang-CL"
expands to @cpp __attribute__((__target__("popcnt"))) @ce, allowing use of the
[POPCNT](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#ABM_(Advanced_Bit_Manipulation))
instructions inside a function annotated with this macro without having to
specify `-mpopcnt` for the whole compilation unit. On x86 MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. Not
defined on GCC 4.8, as there it's not generally possible to enable it alongside
other instruction sets without running into linker errors. Not defined on other
compilers or architectures.
As a special case, if @ref DEATH_TARGET_POPCNT is defined (meaning POCNT is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Neither a superset nor implied by any other `DEATH_ENABLE_*` macro, so you
may need to specify it together with others. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8 or Clang < 7, you may also want to use @ref IntrinsicsSse4.h instead of
@cpp #include <nmmintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_POPCNT) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_POPCNT
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_POPCNT
# endif
#elif (defined(DEATH_TARGET_GCC) && __GNUC__*100 + __GNUC_MINOR__ >= 409) || defined(DEATH_TARGET_CLANG) /* matches Clang-CL */
# define DEATH_ENABLE_POPCNT __attribute__((__target__("popcnt")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_POPCNT "popcnt",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_POPCNT
#endif
/**
@brief Enable LZCNT for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("lzcnt"))) @ce, allowing use of the
[LZCNT](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#ABM_(Advanced_Bit_Manipulation))
instructions inside a function annotated with this macro without having to
specify `-mlzcnt` for the whole compilation unit. On x86 MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. Unlike
the SSE variants and POPCNT this macro is not defined on
@ref DEATH_TARGET_CLANG_CL "Clang-CL", as there LZCNT, BMI1, BMI2, AVX and
newer intrinsics are provided only if enabled on compiler command line. Not
defined on GCC 4.8, as there it's not generally possible to enable it alongside
unrelated instruction sets without running into linker errors. Not defined on
other compilers or architectures.
As a special case, if @ref DEATH_TARGET_LZCNT is defined (meaning LZCNT is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Neither a superset nor implied by any other `DEATH_ENABLE_*` macro (not even
@ref DEATH_TARGET_BMI2, although the name would suggest that), so you may
need to specify it together with others.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_LZCNT) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_LZCNT
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_LZCNT
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
# define DEATH_ENABLE_LZCNT __attribute__((__target__("lzcnt")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_LZCNT "lzcnt",
# endif
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __LZCNT__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
# define DEATH_ENABLE_LZCNT
#endif
/**
@brief Enable BMI1 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang expands to
@cpp __attribute__((__target__("bmi"))) @ce, allowing use of the
[BMI1](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#BMI1_(Bit_Manipulation_Instruction_Set_1))
instructions inside a function annotated with this macro without having to
specify `-mbmi` for the whole compilation unit. On x86 MSVC expands to nothing,
as the compiler doesn't restrict use of intrinsics in any way. Unlike the SSE
variants and POPCNT this macro is not defined on
@ref DEATH_TARGET_CLANG_CL "Clang-CL", as there LZCNT, BMI1, AVX and newer
intrinsics are provided only if enabled on compiler command line. Not defined
on GCC 4.8, as there it's not generally possible to enable it alongside
unrelated instruction sets without running into linker errors. Not defined on
other compilers or architectures.
As a special case, if @ref DEATH_TARGET_BMI1 is defined (meaning BMI1 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Neither a superset nor implied by any other `DEATH_ENABLE_*` macro, so you
may need to specify it together with others. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_BMI1) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_BMI1
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_BMI1
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
# define DEATH_ENABLE_BMI1 __attribute__((__target__("bmi")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_BMI1 "bmi",
# endif
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __BMI__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
# define DEATH_ENABLE_BMI1
#endif
/**
@brief Enable BMI2 for given function
On @ref DEATH_TARGET_X86 "x86" GCC, Clang expands to
@cpp __attribute__((__target__("bmi2"))) @ce, allowing use of the
[BMI2](https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#BMI2_(Bit_Manipulation_Instruction_Set_2))
instructions inside a function annotated with this macro without having to
specify `-mbmi2` for the whole compilation unit. On x86 MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. Unlike
the SSE variants and POPCNT this macro is not defined on
@ref DEATH_TARGET_CLANG_CL "Clang-CL", as there LZCNT, BMI1, BMI2, AVX and
newer intrinsics are provided only if enabled on compiler command line. Not
defined on GCC 4.8, as there it's not generally possible to enable it alongside
unrelated instruction sets without running into linker errors. Not defined on
other compilers or architectures.
As a special case, if @ref DEATH_TARGET_BMI2 is defined (meaning BMI2 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Neither a superset nor implied by any other `DEATH_ENABLE_*` macro (not even
@ref DEATH_TARGET_BMI1, although the name would suggest that), so you may
need to specify it together with others.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_BMI2) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_BMI2
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_BMI2
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
# define DEATH_ENABLE_BMI2 __attribute__((__target__("bmi2")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_BMI2 "bmi2",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __BMI2__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_BMI2
#endif
/**
@brief Enable AVX for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("avx"))) @ce, allowing use of
[AVX](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) and all earlier
SSE instructions inside a function annotated with this macro without having to
specify `-mavx` for the whole compilation unit. On x86 MSVC expands to nothing,
as the compiler doesn't restrict use of intrinsics in any way. Unlike the SSE
variants this macro is not defined on @ref DEATH_TARGET_CLANG_CL "Clang-CL",
as there AVX and newer intrinsics are provided only if enabled on compiler
command line. Not defined on other compilers or architectures.
As a special case, if @ref DEATH_TARGET_AVX is present (meaning AVX is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_SSE42, implied by @ref DEATH_ENABLE_AVX2. See
@ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_AVX) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_AVX
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX
# endif
#elif defined(DEATH_TARGET_GCC) /* does not match Clang-CL */
// The -mavx option implies -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -mavx`)
# define DEATH_ENABLE_AVX __attribute__((__target__("avx")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX "avx",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __AVX__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_AVX
#endif
/**
@brief Enable AVX F16C for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("f16c"))) @ce, allowing use of
[F16C](https://en.wikipedia.org/wiki/F16C) instructions inside a function
annotated with this macro without having to specify `-mf16c` for the whole
compilation unit. On x86 MSVC expands to nothing, as the compiler doesn't
restrict use of intrinsics in any way. Unlike the SSE variants this macro is
not defined on @ref DEATH_TARGET_CLANG_CL "Clang-CL", as there AVX and newer
intrinsics are provided only if enabled on compiler command line. Not defined
on GCC 4.8, as there it's not generally possible to enable it alongside other
instruction sets without running into linker errors. Not defined on other
compilers or architectures.
As a special case, if @ref DEATH_TARGET_AVX_F16C is present (meaning AVX F16C
is enabled for the whole compilation unit), this macro is defined as empty on
all compilers.
Superset of @ref DEATH_ENABLE_AVX on both GCC and Clang. However not
portably implied by any other `DEATH_ENABLE_*` macro so you may need to
specify it together with others. See @ref Cpu-usage-target-attributes for more
information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_AVX_F16C) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_AVX_F16C
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX_F16C
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
// The -mf16c option implies -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx on both GCC and Clang (verified with `echo | gcc -dM -E - -mf16c`)
# define DEATH_ENABLE_AVX_F16C __attribute__((__target__("f16c")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX_F16C "f16c",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __F16C__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_AVX_F16C
#endif
/**
@brief Enable AVX FMA for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("fma"))) @ce, allowing use of
[FMA](https://en.wikipedia.org/wiki/FMA_instruction_set) instructions inside a
function annotated with this macro without having to specify `-mfma` for the
whole compilation unit. On x86 MSVC expands to nothing, as the compiler doesn't
restrict use of intrinsics in any way. Unlike the SSE variants this macro is
not defined on @ref DEATH_TARGET_CLANG_CL "Clang-CL", as there AVX and newer
intrinsics are provided only if enabled on compiler command line. Not defined
on GCC 4.8, as there it's not generally possible to enable it alongside other
instruction sets without running into linker errors. Not defined on other
compilers or architectures.
As a special case, if @ref DEATH_TARGET_AVX_FMA is present (meaning AVX with
FMA is enabled for the whole compilation unit), this macro is defined as empty
on all compilers.
Superset of @ref DEATH_ENABLE_AVX on both GCC and Clang. However not
portably implied by any other `DEATH_ENABLE_*` macro so you may need to
specify it together with others. See @ref Cpu-usage-target-attributes for more
information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_AVX_FMA) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_AVX_FMA
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX_FMA
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
// The -mfma option implies -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx on both GCC and Clang (verified with `echo | gcc -dM -E - -mf16c`)
# define DEATH_ENABLE_AVX_FMA __attribute__((__target__("fma")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX_FMA "fma",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __FMA__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_AVX_FMA
#endif
/**
@brief Enable AVX2 for given function
On @ref DEATH_TARGET_X86 "x86" GCC and Clang expands to
@cpp __attribute__((__target__("avx2"))) @ce, allowing use of
[AVX2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#Advanced_Vector_Extensions_2),
FMA, F16C, AVX and all earlier SSE instructions inside a function annotated
with this macro without having to specify `-mavx2` for the whole compilation
unit. On x86 MSVC expands to nothing, as the compiler doesn't restrict use of
intrinsics in any way. Unlike the SSE variants this macro is not defined on
@ref DEATH_TARGET_CLANG_CL "Clang-CL", as there AVX and newer intrinsics are
provided only if enabled on compiler command line. Not defined on other
compilers or architectures.
As a special case, if @ref DEATH_TARGET_AVX2 is present (meaning AVX2 is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers.
Superset of @ref DEATH_ENABLE_AVX, implied by @ref DEATH_ENABLE_AVX512F.
See @ref Cpu-usage-target-attributes for more information.
@m_class{m-note m-info}
@par
If you target GCC 4.8, you may also want to use @ref IntrinsicsAvx.h instead of
@cpp #include <immintrin.h> @ce to be able to access the intrinsics on this
compiler.
*/
#if defined(DEATH_TARGET_AVX2) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_AVX2
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX2
# endif
#elif defined(DEATH_TARGET_GCC) /* does not match Clang-CL */
// The -mavx2 option implies -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -mavx2`)
# define DEATH_ENABLE_AVX2 __attribute__((__target__("avx2")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX2 "avx2",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __AVX2__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_AVX2
#endif
/**
@brief Enable AVX-512 Foundation for given function
On @ref DEATH_TARGET_X86 "x86" GCC 4.9+ and Clang expands to
@cpp __attribute__((__target__("avx512f"))) @ce, allowing use of
[AVX-512](https://en.wikipedia.org/wiki/AVX-512) Foundation and all earlier AVX
and SSE instructions inside a function annotated with this macro without having
to specify `-mavx512f` for the whole compilation unit. On x86 MSVC 2017 15.3+
expands to nothing, as the compiler doesn't restrict use of intrinsics in any
way. Unlike the SSE variants this macro is not defined on
@ref DEATH_TARGET_CLANG_CL "Clang-CL", as there AVX and newer intrinsics are
provided only if enabled on compiler command line. Not defined on other
compilers, earlier compiler versions without AVX-512 support or other
architectures.
As a special case, if @ref DEATH_TARGET_AVX512F is present (meaning AVX-512
Foundation is enabled for the whole compilation unit), this macro is defined as
empty on all compilers.
Superset of @ref DEATH_ENABLE_AVX2. See @ref Cpu-usage-target-attributes for
more information.
*/
#if defined(DEATH_TARGET_AVX512F) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_AVX512F
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX512F
# endif
#elif defined(DEATH_TARGET_GCC) && (__GNUC__*100 + __GNUC_MINOR__ >= 409 || defined(DEATH_TARGET_CLANG)) /* does not match Clang-CL */
// The -mavx512 option implies -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -mavx2 on both GCC and Clang, so no need to specify those as well (verified with `echo | gcc -dM -E - -mavx512f`)
# define DEATH_ENABLE_AVX512F __attribute__((__target__("avx512f")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_AVX512F "avx512f",
# endif
// https://github.com/llvm/llvm-project/commit/379a1952b37247975d2df8d23498675c9c8cc730,
// still present in Jul 2022, meaning we can only use these if __AVX512F__ is defined. Funnily enough the older headers don't have
// this on their own, only <immintrin.h>. Also I don't think "Actually using intrinsics on Windows already requires
// the right /arch: settings" is correct.
#elif defined(DEATH_TARGET_MSVC) && _MSC_VER >= 1911 && !defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE_AVX512F
#endif
#endif
#if defined(DEATH_TARGET_ARM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief Enable NEON for given function
On 32-bit @ref DEATH_TARGET_ARM "ARM" GCC expands to
@cpp __attribute__((__target__("fpu=neon"))) @ce, allowing use of
[NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon))
instructions inside a function annotated with this macro without having to
specify `-mfpu=neon` for the whole compilation unit. On ARM MSVC expands to
nothing, as the compiler doesn't restrict use of intrinsics in any way. In
contrast to GCC, this macro is not defined on Clang, as it makes the NEON
intrinsics available only if enabled on compiler command line. Not defined on
other compilers or architectures.
As a special case, if @ref DEATH_TARGET_NEON is present (meaning NEON is
enabled for the whole compilation unit), this macro is defined as empty on all
compilers. This is also the case for ARM64, where NEON support is implicit
(and where `-mfpu=neon` is unrecognized).
Implied by @ref DEATH_ENABLE_NEON_FMA. See @ref Cpu-usage-target-attributes
for more information.
*/
#if defined(DEATH_TARGET_NEON) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_NEON
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON
# endif
// https://github.com/android/ndk/issues/1066 is the only reported (and ignored) issue I found, feels strange that people would
// just not use ifunc or target attributes on Android at all and instead put everything in separate files. Needs further
// investigation. Too bad most ARM platforms ditched GCC, where this works properly.
#elif defined(DEATH_TARGET_GCC) && !defined(DEATH_TARGET_CLANG)
# define DEATH_ENABLE_NEON __attribute__((__target__("fpu=neon")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON "fpu=neon",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_NEON
#endif
/**
@brief Enable NEON FMA for given function
On 32-bit @ref DEATH_TARGET_ARM "ARM" GCC expands to
@cpp __attribute__((__target__("fpu=neon-vfpv4"))) @ce, allowing use of
[NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon)) FMA
instructions inside a function annotated with this macro without having to
specify `-mfpu=neon-vfpv4` for the whole compilation unit. On ARM MSVC expands
to nothing, as the compiler doesn't restrict use of intrinsics in any way. In
contrast to GCC, this macro is not defined on Clang, as it makes the NEON FMA
intrinsics available only if enabled on compiler command line. Not defined on
other compilers or architectures.
As a special case, if @ref DEATH_TARGET_NEON_FMA is present (meaning NEON FMA
is enabled for the whole compilation unit), this macro is defined as empty on
all compilers. This is also the case for ARM64, where NEON support is implicit
(and where `-mfpu=neon-vfpv4` is unrecognized).
Superset of @ref DEATH_ENABLE_NEON, implied by @ref DEATH_ENABLE_NEON_FP16.
See @ref Cpu-usage-target-attributes for more information and usage example.
*/
#if defined(DEATH_TARGET_NEON_FMA) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_NEON_FMA
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON_FMA
# endif
// See DEATH_ENABLE_NEON above for details about Clang
#elif defined(DEATH_TARGET_GCC) && !defined(DEATH_TARGET_CLANG)
# define DEATH_ENABLE_NEON_FMA __attribute__((__target__("fpu=neon-vfpv4")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON_FMA "fpu=neon-vfpv4",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_NEON_FMA
#endif
/**
@brief Enable NEON FP16 for given function
On @ref DEATH_TARGET_ARM "ARM" GCC expands to
@cpp __attribute__((__target__("arch=armv8.2-a+fp16"))) @ce, allowing use of
ARMv8.2-a [NEON](https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(Neon))
FP16 vector arithmetic inside a function annotated with this macro without
having to specify `-march=armv8.2-a+fp16` for the whole compilation unit. On
ARM MSVC expands to nothing, as the compiler doesn't restrict use of intrinsics
in any way. In contrast to GCC, this macro is not defined on Clang, as it makes
the NEON FP16 intrinsics available only if enabled on compiler command line.
Not defined on other compilers or architectures.
As a special case, if @ref DEATH_TARGET_NEON_FP16 is present (meaning NEON
FP16 is enabled for the whole compilation unit), this macro is defined as empty
on all compilers.
Superset of @ref DEATH_ENABLE_NEON_FMA. See @ref Cpu-usage-target-attributes
for more information.
*/
#if defined(DEATH_TARGET_NEON_FP16) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_NEON_FP16
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON_FP16
# endif
// See DEATH_ENABLE_NEON above for details about Clang
#elif defined(DEATH_TARGET_GCC) && !defined(DEATH_TARGET_CLANG)
# define DEATH_ENABLE_NEON_FP16 __attribute__((__target__("arch=armv8.2-a+fp16")))
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_NEON_FP16 "arch=armv8.2-a+fp16",
# endif
#elif defined(DEATH_TARGET_MSVC)
# define DEATH_ENABLE_NEON_FP16
#endif
#endif
#if defined(DEATH_TARGET_WASM) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
@brief Enable SIMD128 for given function
Given that it's currently not possible to selectively use
[128-bit SIMD](https://github.com/webassembly/simd) in a WebAssembly module
without causing a compilation error on runtimes that don't support it, this
macro is only defined if @ref DEATH_TARGET_SIMD128 is present (meaning
SIMD128 is explicitly enabled for the whole compilation unit), and is always
empty, as @cpp __attribute__((__target__("simd128"))) @ce would be redundant
if `-msimd128` is passed on the command line.
The situation may change once the
[feature detection proposal](https://github.com/WebAssembly/feature-detection/blob/main/proposals/feature-detection/Overview.md)
is implemented, but likely only for instruction sets building on top of this
one.
See @ref Cpu-usage-target-attributes for more information.
*/
#if defined(DEATH_TARGET_SIMD128) || defined(DOXYGEN_GENERATING_OUTPUT)
# define DEATH_ENABLE_SIMD128
# if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
# define __DEATH_ENABLE_SIMD128
# endif
#endif
#endif
#ifndef DOXYGEN_GENERATING_OUTPUT
// GCC before version 12 and Clang before version 8 treat DEATH_ENABLE_ macros after each other. Clang since version 8 then treats
// that as if only "foo" was specified, which is different but also wrong (I didn't bother finding a commit backing this) and it's
// still broken in Clang 15. Instead, the only accepted form is __attribute__((target("foo,bar"))). Fortunately, string literal
// concatenation works here, thus with some extra macro trickery we can produce __attribute__((target("foo" "," "bar"))).
// The pieces are __DEATH_ENABLE_* variants defined if and only if a corresponding DEATH_ENABLE_* macro is defined. These can
// however be empty, thus it's not possible to just join them all with "," in between. Instead, the macros themselves have a trailing
// comma after the string literal (thus "foo",), which causes the empty macros be filtered out when passed one after another
// (without commas) from __DEATH_ENABLEn to __DEATH_ENABLE_CONCATENATE().
#if (defined(DEATH_TARGET_GCC) && __GNUC__ < 12) || defined(DEATH_TARGET_CLANG)
#define __DEATH_ENABLE_CONCATENATE0(unused)
#define __DEATH_ENABLE_CONCATENATE1(v0, unused) \
__attribute__((__target__(v0)))
#define __DEATH_ENABLE_CONCATENATE2(v0, v1, unused) \
__attribute__((__target__(v0 "," v1)))
#define __DEATH_ENABLE_CONCATENATE3(v0, v1, v2, unused) \
__attribute__((__target__(v0 "," v1 "," v2)))
#define __DEATH_ENABLE_CONCATENATE4(v0, v1, v2, v3, unused) \
__attribute__((__target__(v0 "," v1 "," v2 "," v3)))
#define __DEATH_ENABLE_CONCATENATE5(v0, v1, v2, v3, v4, unused) \
__attribute__((__target__(v0 "," v1 "," v2 "," v3 "," v4)))
#define __DEATH_ENABLE_CONCATENATE6(v0, v1, v2, v3, v4, v5, unused) \
__attribute__((__target__(v0 "," v1 "," v2 "," v3 "," v4 "," v5)))
#define __DEATH_ENABLE_CONCATENATE7(v0, v1, v2, v3, v4, v5, v6, unused) \
__attribute__((__target__(v0 "," v1 "," v2 "," v3 "," v4 "," v5 "," v6)))
#define __DEATH_ENABLE_CONCATENATE(...) \
DEATH_HELPER_PICK(__VA_ARGS__, __DEATH_ENABLE_CONCATENATE7, __DEATH_ENABLE_CONCATENATE6, __DEATH_ENABLE_CONCATENATE5, __DEATH_ENABLE_CONCATENATE4, __DEATH_ENABLE_CONCATENATE3, __DEATH_ENABLE_CONCATENATE2, __DEATH_ENABLE_CONCATENATE1, __DEATH_ENABLE_CONCATENATE0, )(__VA_ARGS__)
// No __DEATH_HELPER_PASTE() needed here, as there's enough other indirections to make that work
#define __DEATH_ENABLE1(v0) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
)
#define __DEATH_ENABLE2(v0, v1) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
)
#define __DEATH_ENABLE3(v0, v1, v2) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
__DEATH_ENABLE_ ## v2 \
)
#define __DEATH_ENABLE4(v0, v1, v2, v3) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
__DEATH_ENABLE_ ## v2 \
__DEATH_ENABLE_ ## v3 \
)
#define __DEATH_ENABLE5(v0, v1, v2, v3, v4) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
__DEATH_ENABLE_ ## v2 \
__DEATH_ENABLE_ ## v3 \
__DEATH_ENABLE_ ## v4 \
)
#define __DEATH_ENABLE6(v0, v1, v2, v3, v4, v5) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
__DEATH_ENABLE_ ## v2 \
__DEATH_ENABLE_ ## v3 \
__DEATH_ENABLE_ ## v4 \
__DEATH_ENABLE_ ## v5 \
)
#define __DEATH_ENABLE7(v0, v1, v2, v3, v4, v5, v6) \
__DEATH_ENABLE_CONCATENATE( \
__DEATH_ENABLE_ ## v0 \
__DEATH_ENABLE_ ## v1 \
__DEATH_ENABLE_ ## v2 \
__DEATH_ENABLE_ ## v3 \
__DEATH_ENABLE_ ## v4 \
__DEATH_ENABLE_ ## v5 \
__DEATH_ENABLE_ ## v6 \
)
// None of this is needed for GCC 12+, fortunately, so here the whole thing expands to just DEATH_ENABLE_FOO DEATH_ENABLE_BAR.
// I hope Clang eventually fixes this as well, thus keeping both variants so I can drop the nasty one in the future.
// As another future-proof this also gets used for any compilers other than MSVC. MSVC's preprocessor isn't able to perform
// the delayed expansion without / Zc:preprocessor, fortunately DEATH_ENABLE() isn't needed there at all.
#elif defined(DEATH_TARGET_GCC) || !defined(DEATH_TARGET_MSVC)
// Using __DEATH_PASTE() instead of DEATH_PASTE() here, as that's enough to make that work and it's less
// work for the preprocessor. Concatenating directly doesn't work, unlike in the above case for GCC.
#define __DEATH_ENABLE1(v0) \
__DEATH_PASTE(DEATH_ENABLE_, v0)
#define __DEATH_ENABLE2(v0, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1)
#define __DEATH_ENABLE3(v0, v1, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2)
#define __DEATH_ENABLE4(v0, v1, v2, v3) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v3)
#define __DEATH_ENABLE5(v0, v1, v2, v3, v4) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v3) \
__DEATH_PASTE(DEATH_ENABLE_, v4)
#define __DEATH_ENABLE6(v0, v1, v2, v3, v4, v5) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v3) \
__DEATH_PASTE(DEATH_ENABLE_, v4) \
__DEATH_PASTE(DEATH_ENABLE_, v5)
#define __DEATH_ENABLE7(v0, v1, v2, v3, v4, v5, v6) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v3) \
__DEATH_PASTE(DEATH_ENABLE_, v4) \
__DEATH_PASTE(DEATH_ENABLE_, v5) \
__DEATH_PASTE(DEATH_ENABLE_, v6)
#define __DEATH_ENABLE8(v0, v1, v2, v3, v4, v5, v6, v7) \
__DEATH_PASTE(DEATH_ENABLE_, v0) \
__DEATH_PASTE(DEATH_ENABLE_, v1) \
__DEATH_PASTE(DEATH_ENABLE_, v2) \
__DEATH_PASTE(DEATH_ENABLE_, v3) \
__DEATH_PASTE(DEATH_ENABLE_, v4) \
__DEATH_PASTE(DEATH_ENABLE_, v5) \
__DEATH_PASTE(DEATH_ENABLE_, v6) \
__DEATH_PASTE(DEATH_ENABLE_, v7)
#endif
#endif
/**
@brief Enable multiple targets for given function
Accepts a comma-separated list of `DEATH_ENABLE_*` macro suffixes,
effectively enabling given combination. For the macro to work, all
`DEATH_ENABLE_*` macros corresponding to the arguments have to be defined,
the common usage pattern is thus in combination with an @cpp #ifdef @ce. See
@ref Cpu-usage-target-attributes for more information and an example.
When multiple `DEATH_ENABLE_*` macros are specified one after another, Clang
8+ would pick only the first specified, and Clang before version 8 and GCC
before version 12 only the last specified, ignoring the others. There the macro
expands into a single combined @cpp __attribute__((__target__(...))) @ce
attribute. For GCC 12+ and other compilers except MSVC it's just a shorthand
for multiple `DEATH_ENABLE_*` macros one after another. On MSVC expands to
nothing --- there the functions aren't annotated in anyway and moreover the
default preprocessor behavior would make this extremely tricky to implement.
@attention Due to the way the attributes are combined Clang and GCC before
version 12, in certain cases the macro may silently accept even arguments
that don't have a corresponding `DEATH_ENABLE_*` macro defined. To
prevent portability issues, pay extra attention to have a matching
@cpp #ifdef @ce guard.
*/
#if !defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE(...) DEATH_HELPER_PICK(__VA_ARGS__, __DEATH_ENABLE8, __DEATH_ENABLE7, __DEATH_ENABLE6, __DEATH_ENABLE5, __DEATH_ENABLE4, __DEATH_ENABLE3, __DEATH_ENABLE2, __DEATH_ENABLE1, )(__VA_ARGS__)
#else
# define DEATH_ENABLE(...)
#endif
// x86 CPUID implementation on GCC/Clang/MSVC. Has to be inlined in the header because otherwise in the IFUNC scenario it may result
// in a cross-SO call that's unsupported on Clang and older GCC, causing a crash in the dynamic loader during early startup because
// it calls into a place that's not there yet.
//
// Because casually including <immintrin.h> leads to 37+ kLOC (!!!), I go an extra way and use inline assembly instead. Which,
// compared to using the intrinsics --- funnily enough --- reduces the amount of cursing and lengthy comments explaining compiler
// bugs and differences to an absolute minimum. If any of the following misbehaves, please check Git history for the original implementation.
#if defined(DEATH_TARGET_X86) && (defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_GCC))
namespace Implementation {
inline void cpuid(int data[4], int leaf, int count) {
// What's in GCC's / Clang's cpuid.h. Clang-cl as well, as it doesn't seem to know the __cpuidex() intrinsics.
#if defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
# if defined(DEATH_TARGET_32BIT)
asm("cpuid": \
"=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : \
"0"(leaf), "2"(count));
# else
// Clang says "x86-64 uses %rbx as the base register", GCC says "%rbx may be the PIC register",
// so probably important to preserve it or some such?
asm("xchgq %%rbx,%q1\n" \
"cpuid\n" \
"xchgq %%rbx,%q1": \
"=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : \
"0"(leaf), "2"(count));
# endif
#elif defined(DEATH_TARGET_MSVC)
// Declared at the top of the file
__cpuidex(data, leaf, count);
#else
# error
#endif
}
}
inline Features runtimeFeatures() {
union {
struct {
unsigned int ax, bx, cx, dx;
} e;
int data[4];
} cpuid{};
Implementation::cpuid(cpuid.data, 1, 0);
// https://en.wikipedia.org/wiki/CPUID#EAX=1:_Processor_Info_and_Feature_Bits
unsigned int out = 0;
if (cpuid.e.dx & (1 << 26)) out |= TypeTraits<Sse2T>::Index;
if (cpuid.e.cx & (1 << 0)) out |= TypeTraits<Sse3T>::Index;
if (cpuid.e.cx & (1 << 9)) out |= TypeTraits<Ssse3T>::Index;
if (cpuid.e.cx & (1 << 19)) out |= TypeTraits<Sse41T>::Index;
if (cpuid.e.cx & (1 << 20)) out |= TypeTraits<Sse42T>::Index;
// https://en.wikipedia.org/wiki/CPUID#EAX=80000001h:_Extended_Processor_Info_and_Feature_Bits,
// bit 5 says "ABM (lzcnt and popcnt)", but
// https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#ABM_(Advanced_Bit_Manipulation)
// says that while LZCNT is advertised in the ABM CPUID bit, POPCNT is a separate CPUID flag. Get POPCNT first, ABM later.
if (cpuid.e.cx & (1 << 23)) out |= TypeTraits<PopcntT>::Index;
// AVX needs OS support checked, as the OS needs to be capable of saving and restoring the expanded registers when switching contexts:
// https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#Operating_system_support
if ((cpuid.e.cx & (1 << 27)) && /* XSAVE/XRESTORE CPU support */
(cpuid.e.cx & (1 << 28))) /* AVX CPU support */
{
// XGETBV indicates that the registers will be properly saved and restored by the OS: https://stackoverflow.com/a/22521619.
// https://github.com/vectorclass/version2/blob/ff7450acfad9d3a7c6825d92cfb782a42ccfa71f/instrset_detect.cpp#L30-L32
// Clang-cl as well, as it doesn't seem to know the MSVC intrinsics.
#if defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG_CL)
unsigned int a, d;
__asm("xgetbv": "=a"(a), "=d"(d) : "c"(0) : );
const unsigned long long xgetbv = a | (static_cast<unsigned long long>(d) << 32);
// Declared at the top of the file
#elif defined(DEATH_TARGET_MSVC)
const unsigned long long xgetbv = _xgetbv(0);
#else
# error
#endif
// If AVX is not supported, we don't check any following flags either
if ((xgetbv & 0x06) == 0x06 /* XSTATE_SSE|XSTATE_YMM */) {
out |= TypeTraits<AvxT>::Index;
if (cpuid.e.cx & (1 << 29)) out |= TypeTraits<AvxF16cT>::Index;
if (cpuid.e.cx & (1 << 12)) out |= TypeTraits<AvxFmaT>::Index;
// https://en.wikipedia.org/wiki/CPUID#EAX=7,_ECX=0:_Extended_Features
Implementation::cpuid(cpuid.data, 7, 0);
if (cpuid.e.bx & (1 << 3)) out |= TypeTraits<Bmi1T>::Index;
if (cpuid.e.bx & (1 << 8)) out |= TypeTraits<Bmi2T>::Index;
if (cpuid.e.bx & (1 << 5)) out |= TypeTraits<Avx2T>::Index;
}
// AVX-512 needs additional state saving support
// https://patchwork.ozlabs.org/project/gcc/patch/20180329124309.GA12667@intel.com/#1884915
// https://github.com/google/highway/blob/master/hwy/targets.cc#L279
if ((cpuid.e.bx & (1 << 16)) &&
// XSTATE_SSE|XSTATE_YMM|XSTATE_OPMASK|XSTATE_ZMM|XSTATE_HI_ZMM
(xgetbv & 0xe6) == 0xe6) {
out |= TypeTraits<Avx512fT>::Index;
}
}
// And now the LZCNT bit, finally
// https://en.wikipedia.org/wiki/CPUID#EAX=80000001h:_Extended_Processor_Info_and_Feature_Bits
Implementation::cpuid(cpuid.data, 0x80000001, 0);
if (cpuid.e.cx & (1 << 5)) out |= TypeTraits<LzcntT>::Index;
return Features{out};
}
#endif
// ARM implementation on Linux and Android, inlined for the same reason as the x86 variant above -- to make IFUNC work.
// As getauxval() can't be called from within an ifunc resolver because there it's too early for an external call,
// the value of AT_HWCAP is instead passed to it from outside, on glibc 2.13+ and on Android API 30+:
// https://github.com/bminor/glibc/commit/7520ff8c744a704ca39741c165a2360d63a4f47a
// https://android.googlesource.com/platform/bionic/+/e949195f6489653ee3771535951ed06973246c3e/libc/include/sys/ifunc.h
// Which means we need a variant of runtimeFeatures() that is able to operate with a value fed from outside, which is
// then used inside such resolvers. A nice consequence of that is that we don't need any other headers.
// The public Cpu::runtimeFeatures() is deinlined, calls getauxval() and passes it into this function.
#if defined(DEATH_TARGET_ARM) && ((defined(__linux__) && !(defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(__FreeBSD__))
namespace Implementation {
inline Features runtimeFeatures(const unsigned long caps) {
unsigned int out = 0;
# if defined(DEATH_TARGET_32BIT)
if (caps & (1 << 12) /*HWCAP_NEON*/) out |= TypeTraits<NeonT>::Index;
// Since FMA is enabled by passing -mfpu=neon-vfpv4, I assume this is the flag that corresponds to it.
if (caps & (1 << 16) /*HWCAP_VFPv4*/) out |= TypeTraits<NeonFmaT>::Index;
# else
// On ARM64 NEON and NEON FMA is implicit. For extra security make use of the DEATH_TARGET_ defines (which should be always there).
out |= 0
# if defined(DEATH_TARGET_NEON)
| TypeTraits<NeonT>::Index
# endif
# if defined(DEATH_TARGET_NEON_FMA)
| TypeTraits<NeonFmaT>::Index
# endif
;
// The HWCAP flags are extremely cryptic. The only vague confirmation is in a *commit message* to the kernel hwcaps file, FFS.
// The HWCAP_FPHP seems to correspond to scalar FP16, so the other should be the vector one?
// https://github.com/torvalds/linux/blame/master/arch/arm64/include/uapi/asm/hwcap.h
// This one also isn't present on 32-bit, so I assume it's ARM64-only?
if (caps & (1 << 10) /*HWCAP_ASIMDHP*/) out |= TypeTraits<NeonFp16T>::Index;
# endif
return Features{out};
}
}
#endif
}}
|