4#include "native/config.h"
16 mmx, sse, sse2, sse3, ssse3, sse41,
18 bmi1, bmi2, avx512f, avx512dq, avx512bw, avx512vl,
19 avx512bf16, avx512fp16, aes, pclmul, cx16, avx512cd,
20 avx512ifma,
lzcnt, movbe, sahf,
mwaitx, waitpkg,
crc32, gfni, avx512vpopcntdq,
21 vpclmulqdq, avxvnni, avx512vnni, avxvnniint8, avxvnniint16, avx512bitalg, avx512vbmi, avx512vbmi2, sha, vaes, avxifma, adx, sha512, sm3, sm4, avxneconvert
25 neon, neon_fp16, neon_bf16, aes, sha2, sha3,
26 crc, lse, rdm, fp16fml, dotprod, complxnum,
27 jsconv, rcpc, pauth, i8mm,
pmull, sha1, sha512, ebf16, sm3, sm4
46 template<architecture Family=target_arch>
struct isa;
49 template<
class T>
concept instruction_feature=
50 std::same_as<T,x86_feature> || std::same_as<T,arm_feature> || std::same_as<T,wasm_feature>;
51 template<instruction_feature E>
inline constexpr std::size_t feature_count=
54 template<instruction_feature E>
inline constexpr architecture family_of=
55 std::same_as<E,x86_feature> ?
x86 : std::same_as<E,arm_feature> ?
arm :
wasm;
56 template<architecture Family>
struct feature_traits;
60 enum class unknown_feature : std::uint64_t {};
61 template<>
struct feature_traits<
architecture::
unknown> {
using type=unknown_feature;
static constexpr std::size_t count=0; };
65 template<architecture Family>
struct isa {
71 static constexpr auto feature_count=detail::feature_traits<Family>::count;
81 constexpr isa() noexcept=default;
86 auto i=std::uint64_t(f);
92 auto i=std::uint64_t(f);
97 auto mask=std::uint64_t{1}<<(i%64);
98 auto & word=
flags[i/64];
105 constexpr bool has(
isa other)
const noexcept {
106 for(std::size_t i=0;i<
flags.size();++i)
107 if((
flags[i]&other.flags[i])!=other.flags[i])
return false;
111 constexpr bool valid() const noexcept {
113 constexpr auto mask=used ? (std::uint64_t{1}<<used)-1 : 0;
137 constexpr isa() noexcept=default;
142 auto i=std::uint64_t(f);
148 auto i=std::uint64_t(f);
153 auto mask=std::uint64_t{1}<<(i%64);
154 auto & word=
flags[i/64];
161 constexpr bool has(
isa other)
const noexcept {
162 for(std::size_t i=0;i<
flags.size();++i)
163 if((
flags[i]&other.flags[i])!=other.flags[i])
return false;
167 constexpr bool valid() const noexcept {
169 constexpr auto mask=used ? (std::uint64_t{1}<<used)-1 : 0;
174 __declspec(property(
get=get_mmx,put=set_mmx))
bool mmx;
175 __declspec(property(
get=get_sse,put=set_sse))
bool sse;
176 __declspec(property(
get=get_sse2,put=set_sse2))
bool sse2;
177 __declspec(property(
get=get_sse3,put=set_sse3))
bool sse3;
178 __declspec(property(
get=get_ssse3,put=set_ssse3))
bool ssse3;
179 __declspec(property(
get=get_sse41,put=set_sse41))
bool sse41;
180 __declspec(property(
get=get_sse42,put=set_sse42))
bool sse42;
181 __declspec(property(
get=get_popcnt,put=set_popcnt))
bool popcnt;
182 __declspec(property(
get=get_avx,put=set_avx))
bool avx;
183 __declspec(property(
get=get_avx2,put=set_avx2))
bool avx2;
184 __declspec(property(
get=get_fma,put=set_fma))
bool fma;
185 __declspec(property(
get=get_f16c,put=set_f16c))
bool f16c;
186 __declspec(property(
get=get_bmi1,put=set_bmi1))
bool bmi1;
187 __declspec(property(
get=get_bmi2,put=set_bmi2))
bool bmi2;
188 __declspec(property(
get=get_avx512f,put=set_avx512f))
bool avx512f;
189 __declspec(property(
get=get_avx512dq,put=set_avx512dq))
bool avx512dq;
190 __declspec(property(
get=get_avx512bw,put=set_avx512bw))
bool avx512bw;
191 __declspec(property(
get=get_avx512vl,put=set_avx512vl))
bool avx512vl;
192 __declspec(property(
get=get_avx512bf16,put=set_avx512bf16))
bool avx512bf16;
193 __declspec(property(
get=get_avx512fp16,put=set_avx512fp16))
bool avx512fp16;
194 __declspec(property(
get=get_aes,put=set_aes))
bool aes;
195 __declspec(property(
get=get_pclmul,put=set_pclmul))
bool pclmul;
196 __declspec(property(
get=get_cx16,put=set_cx16))
bool cx16;
197 __declspec(property(
get=get_avx512cd,put=set_avx512cd))
bool avx512cd;
198 __declspec(property(
get=get_avx512ifma,put=set_avx512ifma))
bool avx512ifma;
199 __declspec(property(
get=get_lzcnt,put=set_lzcnt))
bool lzcnt;
200 __declspec(property(
get=get_movbe,put=set_movbe))
bool movbe;
201 __declspec(property(
get=get_sahf,put=set_sahf))
bool sahf;
202 __declspec(property(
get=get_mwaitx,put=set_mwaitx))
bool mwaitx;
203 __declspec(property(
get=get_waitpkg,put=set_waitpkg))
bool waitpkg;
204 __declspec(property(
get=get_crc32,put=set_crc32))
bool crc32;
205 __declspec(property(
get=get_gfni,put=set_gfni))
bool gfni;
206 __declspec(property(
get=get_vpclmulqdq,put=set_vpclmulqdq))
bool vpclmulqdq;
208 __declspec(property(
get=get_avxvnni,put=set_avxvnni))
bool avxvnni;
209 __declspec(property(
get=get_avx512vnni,put=set_avx512vnni))
bool avx512vnni;
213 __declspec(property(
get=get_avx512vbmi,put=set_avx512vbmi))
bool avx512vbmi;
215 __declspec(property(
get=get_sha,put=set_sha))
bool sha;
216 __declspec(property(
get=get_vaes,put=set_vaes))
bool vaes;
217 __declspec(property(
get=get_avxifma,put=set_avxifma))
bool avxifma;
218 __declspec(property(
get=get_adx,put=set_adx))
bool adx;
219 __declspec(property(
get=get_sha512,put=set_sha512))
bool sha512;
220 __declspec(property(
get=get_sm3,put=set_sm3))
bool sm3;
221 __declspec(property(
get=get_sm4,put=set_sm4))
bool sm4;
242 constexpr isa() noexcept=default;
247 auto i=std::uint64_t(f);
253 auto i=std::uint64_t(f);
258 auto mask=std::uint64_t{1}<<(i%64);
259 auto & word=
flags[i/64];
266 constexpr bool has(
isa other)
const noexcept {
267 for(std::size_t i=0;i<
flags.size();++i)
268 if((
flags[i]&other.flags[i])!=other.flags[i])
return false;
272 constexpr bool valid() const noexcept {
274 constexpr auto mask=used ? (std::uint64_t{1}<<used)-1 : 0;
279 __declspec(property(
get=get_neon,put=set_neon))
bool neon;
280 __declspec(property(
get=get_neon_fp16,put=set_neon_fp16))
bool neon_fp16;
281 __declspec(property(
get=get_neon_bf16,put=set_neon_bf16))
bool neon_bf16;
282 __declspec(property(
get=get_pmull,put=set_pmull))
bool pmull;
283 __declspec(property(
get=get_sha1,put=set_sha1))
bool sha1;
284 __declspec(property(
get=get_sha512,put=set_sha512))
bool sha512;
285 __declspec(property(
get=get_ebf16,put=set_ebf16))
bool ebf16;
288 __declspec(property(
get=get_sm3,put=set_sm3))
bool sm3;
291 __declspec(property(
get=get_sm4,put=set_sm4))
bool sm4;
292 __declspec(property(
get=get_aes,put=set_aes))
bool aes;
293 __declspec(property(
get=get_sha2,put=set_sha2))
bool sha2;
294 __declspec(property(
get=get_sha3,put=set_sha3))
bool sha3;
295 __declspec(property(
get=get_crc,put=set_crc))
bool crc;
296 __declspec(property(
get=get_lse,put=set_lse))
bool lse;
297 __declspec(property(
get=get_rdm,put=set_rdm))
bool rdm;
298 __declspec(property(
get=get_fp16fml,put=set_fp16fml))
bool fp16fml;
299 __declspec(property(
get=get_dotprod,put=set_dotprod))
bool dotprod;
300 __declspec(property(
get=get_complxnum,put=set_complxnum))
bool complxnum;
301 __declspec(property(
get=get_jsconv,put=set_jsconv))
bool jsconv;
302 __declspec(property(
get=get_rcpc,put=set_rcpc))
bool rcpc;
303 __declspec(property(
get=get_pauth,put=set_pauth))
bool pauth;
304 __declspec(property(
get=get_i8mm,put=set_i8mm))
bool i8mm;
324 constexpr isa() noexcept=default;
329 auto i=std::uint64_t(f);
335 auto i=std::uint64_t(f);
340 auto mask=std::uint64_t{1}<<(i%64);
341 auto & word=
flags[i/64];
348 constexpr bool has(
isa other)
const noexcept {
349 for(std::size_t i=0;i<
flags.size();++i)
350 if((
flags[i]&other.flags[i])!=other.flags[i])
return false;
354 constexpr bool valid() const noexcept {
356 constexpr auto mask=used ? (std::uint64_t{1}<<used)-1 : 0;
361 __declspec(property(
get=get_simd128,put=set_simd128))
bool simd128;
366 template<detail::instruction_feature E>
isa(E)->
isa<detail::family_of<E>>;
369 template<
class T>
inline constexpr bool is_isa=
false;
370 template<architecture Family>
inline constexpr bool is_isa<isa<Family>> =
true;
371 template<
class T>
struct arch_family;
372 template<instruction_feature E>
struct arch_family<E> : std::integral_constant<architecture,family_of<E>> {};
373 template<architecture Family>
struct arch_family<
isa<Family>> : std::integral_constant<architecture,Family> {};
376 template<
class T>
concept arch=detail::instruction_feature<T> || detail::is_isa<T>;
378 template<arch T>
inline constexpr architecture arch_family_v=arch_family<T>::value;
379 template<
class A,
class B>
concept same_arch=
arch<A> &&
arch<B> && arch_family_v<A> == arch_family_v<B>;
383 template<arch A,arch B>
requires detail::same_arch<A,B>
386 for(std::size_t i=0;i<result.
flags.size();++i) result.
flags[i]|=other.flags[i];
429 template<arch A,arch B>
requires detail::same_arch<A,B>
432 template<arch A,arch B>
requires detail::same_arch<A,B>
435 template<arch A,arch B>
requires detail::same_arch<A,B>
436 constexpr bool operator<(A a,B b)
noexcept {
return a<=b && !(b<=a); }
438 template<arch A,arch B>
requires detail::same_arch<A,B>
439 constexpr bool operator>(A a,B b)
noexcept {
return b<a; }
443 template<
auto A,
auto... Choices>
444 requires arch<
decltype(A)> && ((detail::same_arch<
decltype(A),
decltype(Choices)>) && ...)
445 inline constexpr
int target=[]() consteval {
446 using value_type=
isa<detail::arch_family_v<
decltype(A)>>;
447 constexpr std::array<value_type,
sizeof...(Choices)> choices{value_type(Choices)...};
449 for(std::size_t i=0;i<choices.size();++i)
450 for(std::size_t j=i+1;j<choices.size();++j)
451 if(choices[i]<=choices[j])
return false;
453 }(),
"a later target is shadowed by an earlier one");
454 for(std::size_t i=0;i<choices.size();++i)
455 if(choices[i]<=value_type(A))
return int(i);
460 template<architecture Family>
462 for(std::size_t i=0;i<a.flags.size();++i) a.flags[i]&=b.flags[i];
463 a.allow_polyfill=a.allow_polyfill && b.allow_polyfill;
466 enum class feature_register { leaf1_ecx, leaf1_edx, leaf7_ebx, leaf7_ecx, leaf7_edx, leaf7_1_eax, leaf7_1_edx, extended1_ecx, arm, wasm };
467 template<architecture Family>
struct feature_record {
469 std::string_view spelling;
471 feature_register location;
477 constexpr feature_record(
typename feature_traits<Family>::type f,std::string_view spelling,
isa<Family> implies,
478 feature_register location,
unsigned bit,
isa<Family> target_implies={},
bool targetable=
true)
noexcept:
479 value(f),spelling(spelling),implies(implies),location(location),bit(bit),index(std::size_t(f)),
480 target_implies(target_implies),targetable(targetable) {}
483 template<architecture Family>
inline constexpr auto feature_registry=std::array<feature_record<Family>,0>{};
484 template<>
inline constexpr auto feature_registry<x86> = std::to_array<feature_record<x86>>({
485 {x86_feature::mmx,
"mmx",{},feature_register::leaf1_edx,23},
486 {x86_feature::sse,
"sse",
isa(x86_feature::mmx),feature_register::leaf1_edx,25},
487 {x86_feature::sse2,
"sse2",
isa(x86_feature::sse),feature_register::leaf1_edx,26},
488 {x86_feature::sse3,
"sse3",
isa(x86_feature::sse2),feature_register::leaf1_ecx,0},
489 {x86_feature::ssse3,
"ssse3",
isa(x86_feature::sse3),feature_register::leaf1_ecx,9},
490 {x86_feature::sse41,
"sse4.1",
isa(x86_feature::ssse3),feature_register::leaf1_ecx,19},
491 {x86_feature::sse42,
"sse4.2",x86_feature::sse41&x86_feature::popcnt&x86_feature::crc32,feature_register::leaf1_ecx,20},
492 {x86_feature::popcnt,
"popcnt",{},feature_register::leaf1_ecx,23},
493 {x86_feature::avx,
"avx",
isa(x86_feature::sse42),feature_register::leaf1_ecx,28},
494 {x86_feature::avx2,
"avx2",
isa(x86_feature::avx),feature_register::leaf7_ebx,5},
495 {x86_feature::fma,
"fma",
isa(x86_feature::avx),feature_register::leaf1_ecx,12},
496 {x86_feature::f16c,
"f16c",
isa(x86_feature::avx),feature_register::leaf1_ecx,29},
497 {x86_feature::bmi1,
"bmi",{},feature_register::leaf7_ebx,3},
498 {x86_feature::bmi2,
"bmi2",{},feature_register::leaf7_ebx,8},
499 {x86_feature::mwaitx,
"mwaitx",{},feature_register::extended1_ecx,29},
500 {x86_feature::waitpkg,
"waitpkg",{},feature_register::leaf7_ecx,5},
501 {x86_feature::crc32,
"crc32",{},feature_register::leaf1_ecx,20},
502 {x86_feature::gfni,
"gfni",
isa(x86_feature::sse2),feature_register::leaf7_ecx,8},
503 {x86_feature::avx512f,
"avx512f",x86_feature::avx2&x86_feature::f16c&x86_feature::fma,feature_register::leaf7_ebx,16},
504 {x86_feature::avx512dq,
"avx512dq",
isa(x86_feature::avx512f),feature_register::leaf7_ebx,17},
505 {x86_feature::avx512bw,
"avx512bw",
isa(x86_feature::avx512f),feature_register::leaf7_ebx,30},
506 {x86_feature::avx512vl,
"avx512vl",
isa(x86_feature::avx512f),feature_register::leaf7_ebx,31},
507 {x86_feature::avx512bf16,
"avx512bf16",
isa(x86_feature::avx512bw),feature_register::leaf7_1_eax,5},
508 {x86_feature::avx512fp16,
"avx512fp16",
isa(x86_feature::avx512bw),feature_register::leaf7_edx,23},
509 {x86_feature::avx512vbmi,
"avx512vbmi",
isa(x86_feature::avx512bw),feature_register::leaf7_ecx,1},
510 {x86_feature::avx512vbmi2,
"avx512vbmi2",
isa(x86_feature::avx512bw),feature_register::leaf7_ecx,6},
511 {x86_feature::avx512bitalg,
"avx512bitalg",
isa(x86_feature::avx512bw),feature_register::leaf7_ecx,12},
512 {x86_feature::avx512vpopcntdq,
"avx512vpopcntdq",
isa(x86_feature::avx512f),feature_register::leaf7_ecx,14},
513 {x86_feature::avxvnni,
"avxvnni",
isa(x86_feature::avx2),feature_register::leaf7_1_eax,4},
514 {x86_feature::avx512vnni,
"avx512vnni",
isa(x86_feature::avx512f),feature_register::leaf7_ecx,11},
515 {x86_feature::avxvnniint8,
"avxvnniint8",
isa(x86_feature::avx2),feature_register::leaf7_1_edx,4},
516 {x86_feature::avxvnniint16,
"avxvnniint16",
isa(x86_feature::avx2),feature_register::leaf7_1_edx,10},
517 {x86_feature::aes,
"aes",
isa(x86_feature::sse2),feature_register::leaf1_ecx,25},
518 {x86_feature::pclmul,
"pclmul",
isa(x86_feature::sse2),feature_register::leaf1_ecx,1},
519 {x86_feature::sha,
"sha",
isa(x86_feature::sse2),feature_register::leaf7_ebx,29},
520 {x86_feature::vaes,
"vaes",
isa(x86_feature::avx),feature_register::leaf7_ecx,9,
521 x86_feature::aes&x86_feature::avx2},
522 {x86_feature::vpclmulqdq,
"vpclmulqdq",x86_feature::avx&x86_feature::pclmul,feature_register::leaf7_ecx,10},
523 {x86_feature::cx16,
"cx16",{},feature_register::leaf1_ecx,13},
524 {x86_feature::avx512cd,
"avx512cd",
isa(x86_feature::avx512f),feature_register::leaf7_ebx,28},
525 {x86_feature::avx512ifma,
"avx512ifma",
isa(x86_feature::avx512f),feature_register::leaf7_ebx,21},
526 {x86_feature::avxifma,
"avxifma",
isa(x86_feature::avx),feature_register::leaf7_1_eax,23,
isa(x86_feature::avx2)},
527 {x86_feature::adx,
"adx",{},feature_register::leaf7_ebx,19},
528 {x86_feature::sha512,
"sha512",
isa(x86_feature::avx),feature_register::leaf7_1_eax,0,
isa(x86_feature::avx2)},
529 {x86_feature::sm3,
"sm3",
isa(x86_feature::avx),feature_register::leaf7_1_eax,1},
530 {x86_feature::sm4,
"sm4",
isa(x86_feature::avx),feature_register::leaf7_1_eax,2,
isa(x86_feature::avx2)},
531 {x86_feature::avxneconvert,
"avxneconvert",
isa(x86_feature::avx),feature_register::leaf7_1_edx,5,
isa(x86_feature::avx2)},
532 {x86_feature::lzcnt,
"lzcnt",{},feature_register::extended1_ecx,5},
533 {x86_feature::movbe,
"movbe",{},feature_register::leaf1_ecx,22},
534 {x86_feature::sahf,
"sahf",{},feature_register::extended1_ecx,0}
536 template<>
inline constexpr auto feature_registry<arm> = std::to_array<feature_record<arm>>({
537 {arm_feature::neon,
"neon",{},feature_register::arm,0},
538 {arm_feature::neon_fp16,
"fullfp16",
isa(arm_feature::neon),feature_register::arm,1},
539 {arm_feature::neon_bf16,
"bf16",
isa(arm_feature::neon),feature_register::arm,2},
540 {arm_feature::aes,
"aes",
isa(arm_feature::neon),feature_register::arm,3,
isa(arm_feature::pmull)},
541 {arm_feature::sha2,
"sha2",
isa(arm_feature::neon),feature_register::arm,4,
isa(arm_feature::sha1)},
542 {arm_feature::sha3,
"sha3",
isa(arm_feature::neon),feature_register::arm,5,arm_feature::sha1&arm_feature::sha2&arm_feature::sha512},
543 {arm_feature::crc,
"crc",{},feature_register::arm,6},
544 {arm_feature::lse,
"lse",
isa(arm_feature::neon),feature_register::arm,7},
545 {arm_feature::rdm,
"rdm",
isa(arm_feature::neon),feature_register::arm,8},
546 {arm_feature::fp16fml,
"fp16fml",
isa(arm_feature::neon_fp16),feature_register::arm,9},
547 {arm_feature::dotprod,
"dotprod",
isa(arm_feature::neon),feature_register::arm,10},
548 {arm_feature::complxnum,
"complxnum",
isa(arm_feature::neon),feature_register::arm,11},
549 {arm_feature::jsconv,
"jsconv",
isa(arm_feature::neon),feature_register::arm,12},
550 {arm_feature::rcpc,
"rcpc",
isa(arm_feature::neon),feature_register::arm,13},
551 {arm_feature::pauth,
"pauth",
isa(arm_feature::neon),feature_register::arm,14},
552 {arm_feature::i8mm,
"i8mm",
isa(arm_feature::neon),feature_register::arm,15},
553 {arm_feature::pmull,
"pmull",
isa(arm_feature::neon),feature_register::arm,16,{},
false},
554 {arm_feature::sha1,
"sha1",
isa(arm_feature::neon),feature_register::arm,17,{},
false},
555 {arm_feature::sha512,
"sha512",
isa(arm_feature::neon),feature_register::arm,18,{},
false},
556 {arm_feature::ebf16,
"ebf16",
isa(arm_feature::neon_bf16),feature_register::arm,19,{},
false},
557 {arm_feature::sm3,
"sm3",
isa(arm_feature::neon),feature_register::arm,20,{},
false},
558 {arm_feature::sm4,
"sm4",
isa(arm_feature::neon),feature_register::arm,21,
isa(arm_feature::sm3)}
560 template<>
inline constexpr auto feature_registry<wasm> = std::to_array<feature_record<wasm>>({
561 {wasm_feature::simd128,
"simd128",{},feature_register::wasm,0},
562 {wasm_feature::relaxed_simd,
"relaxed-simd",
isa(wasm_feature::simd128),feature_register::wasm,1}
564 template<architecture Family>
inline constexpr isa<Family> known_features=[] {
566 for(
auto const & entry:feature_registry<Family>) result=result&entry.value;
569 template<instruction_feature E>
inline constexpr auto family_features=known_features<family_of<E>>;
570 inline constexpr auto arm_features=known_features<arm>;
571 inline constexpr auto x86_features=known_features<x86>;
572 inline constexpr auto wasm_features=known_features<wasm>;
573 template<architecture Family>
inline constexpr isa<Family> invalid_features=[] {
575 constexpr auto index=feature_traits<Family>::count;
576 result.flags[index/64]|=std::uint64_t{1}<<(index%64);
580 template<isa<> A>
inline constexpr isa<> source_isa=[]()
consteval {
581 static_assert(A<=known_features<target_arch>,
"source target contains an unregistered ISA feature");
589 constexpr auto family=detail::arch_family_v<A>;
593 for(
auto const & entry:detail::feature_registry<family>)
594 if(bits.
has(entry.value)) bits=bits&entry.implies;
595 }
while(previous!=bits);
599 inline constexpr isa<> scalar{};
600 inline constexpr auto avx2=
feature_closure(x86_feature::avx2&x86_feature::fma);
601 inline constexpr auto avx512=
feature_closure(avx2&x86_feature::avx512f&x86_feature::avx512dq&x86_feature::avx512bw&x86_feature::avx512vl);
602 inline constexpr auto avx512_bf16=
feature_closure(avx512&x86_feature::avx512bf16);
603 inline constexpr auto avx512_fp16=
feature_closure(avx512&x86_feature::avx512fp16);
605 inline constexpr auto neon_fp16=
feature_closure(arm_feature::neon_fp16);
606 inline constexpr auto neon_bf16=
feature_closure(arm_feature::neon_bf16);
610 template<architecture Family=target_arch>
612 if(text.empty())
return {};
614 while(!text.empty()) {
615 auto comma=text.find(
',');
616 auto token=text.substr(0,comma);
618 for(
auto const & entry:detail::feature_registry<Family>)
if(entry.targetable && token.compare(entry.spelling)==0) {
619 bits=bits&entry.value&entry.target_implies; found=
true;
break;
621 if(!found)
return detail::invalid_features<Family>;
622 if(comma==std::string_view::npos)
break;
623 text.remove_prefix(comma+1);
624 if(text.empty())
return detail::invalid_features<Family>;
631 std::uint64_t missing_xcr0=0;
632 bool invalid_features=
false;
633 bool missing_xcr0_observation=
false;
636 return missing_features==
isa<Family>{} && !missing_xcr0 && !invalid_features && !missing_xcr0_observation;
641 constexpr char const *
reason() const noexcept {
642 if(invalid_features)
return "invalid ISA features";
643 for(
auto const & entry:detail::feature_registry<Family>)
644 if(missing_features.has(entry.value))
return entry.spelling.data();
645 if(missing_xcr0_observation)
return "XCR0 unavailable";
646 if(missing_xcr0 & (1ull<<1))
return "XMM state unavailable";
647 if(missing_xcr0 & (1ull<<2))
return "YMM state unavailable";
648 if(missing_xcr0 & (1ull<<5))
return "opmask state unavailable";
649 if(missing_xcr0 & (1ull<<6))
return "ZMM high state unavailable";
650 if(missing_xcr0 & (1ull<<7))
return "high ZMM registers unavailable";
651 return admitted() ?
"admitted" :
"ISA requirements unavailable";
656 template<
class C>
concept x86_observation=
requires(C
const & c) {
657 c.max_basic_leaf; c.leaf1_ecx; c.leaf1_edx; c.leaf7_ebx;
658 c.max_leaf7_subleaf; c.leaf7_1_eax; c.leaf7_edx;
660 template<
class C>
concept arm_observation=
requires(C
const & c) {
661 c.baseline_observed; c.fp; c.asimd;
662 c.fp16_observed; c.scalar_fp16; c.vector_fp16;
663 c.bf16_observed; c.bf16;
665 template<
class C,architecture Family>
concept normalized_features=
requires(C
const & c) {
666 { c.present } -> std::same_as<isa<Family>
const &>;
667 { c.observed } -> std::same_as<isa<Family>
const &>;
669 template<
class C>
concept has_feature_observation=
requires(C
const & c) { c.present; c.observed; };
670 template<architecture Family>
struct feature_observation {
673 template<x86_observation C>
674 constexpr feature_observation<x86> decode_x86_features(C
const & cpu)
noexcept {
675 feature_observation<x86> result;
676 for(
auto const & entry:feature_registry<x86>) {
677 std::uint32_t word=0;
679 switch(entry.location) {
680 case feature_register::leaf1_ecx: observed=cpu.max_basic_leaf>=1; word=cpu.leaf1_ecx;
break;
681 case feature_register::leaf1_edx: observed=cpu.max_basic_leaf>=1; word=cpu.leaf1_edx;
break;
682 case feature_register::leaf7_ebx: observed=cpu.max_basic_leaf>=7; word=cpu.leaf7_ebx;
break;
683 case feature_register::leaf7_ecx:
684 if constexpr(
requires { cpu.leaf7_ecx; }) {
685 observed=cpu.max_basic_leaf>=7; word=cpu.leaf7_ecx;
688 case feature_register::leaf7_edx: observed=cpu.max_basic_leaf>=7; word=cpu.leaf7_edx;
break;
689 case feature_register::leaf7_1_eax:
690 observed=cpu.max_basic_leaf>=7 && cpu.max_leaf7_subleaf>=1; word=cpu.leaf7_1_eax;
break;
691 case feature_register::leaf7_1_edx:
692 if constexpr(
requires { cpu.leaf7_1_edx; }) {
693 observed=cpu.max_basic_leaf>=7 && cpu.max_leaf7_subleaf>=1; word=cpu.leaf7_1_edx;
696 case feature_register::extended1_ecx:
697 if constexpr(
requires { cpu.max_extended_leaf; cpu.extended1_ecx; }) {
698 observed=cpu.max_extended_leaf>=0x80000001u; word=cpu.extended1_ecx;
701 case feature_register::arm:
case feature_register::wasm:
continue;
704 result.observed.set(f,observed);
705 result.present.set(f,observed && (word&(std::uint32_t{1}<<entry.bit)));
709 template<arm_observation C>
710 constexpr feature_observation<arm> decode_arm_features(C
const & cpu)
noexcept {
711 feature_observation<arm> result;
712 result.observed.set(arm_feature::neon,cpu.baseline_observed);
713 result.present.set(arm_feature::neon,cpu.baseline_observed && cpu.fp && cpu.asimd);
714 result.observed.set(arm_feature::neon_fp16,cpu.fp16_observed);
715 result.present.set(arm_feature::neon_fp16,cpu.fp16_observed && cpu.scalar_fp16 && cpu.vector_fp16);
716 result.observed.set(arm_feature::neon_bf16,cpu.bf16_observed);
717 result.present.set(arm_feature::neon_bf16,cpu.bf16_observed && cpu.bf16);
718 if constexpr(
requires { cpu.ebf16_observed; cpu.ebf16; }) {
719 result.observed.set(arm_feature::ebf16,cpu.ebf16_observed);
720 result.present.set(arm_feature::ebf16,cpu.ebf16_observed && cpu.ebf16);
722 constexpr auto baseline=arm_feature::neon&arm_feature::neon_fp16&arm_feature::neon_bf16&arm_feature::ebf16;
723 if constexpr(
requires { cpu.extra_observed; cpu.extra_features; })
724 for(
auto const & entry:feature_registry<arm>) {
725 if(baseline.has(entry.value))
continue;
727 auto observed=cpu.extra_observed.has(f);
728 result.observed.set(f,observed);
729 result.present.set(f,observed && cpu.extra_features.has(f));
733 template<architecture Family>
735 isa_admission<Family> result;
736 auto known=known_features<Family>;
737 result.invalid_features=!present.valid() || !observed.valid() || !(bits<=known);
738 auto available=intersection(present,observed);
739 result.missing_features=intersection(bits,known);
740 for(std::size_t i=0;i<available.flags.size();++i)
741 result.missing_features.flags[i]&=~available.flags[i];
744 constexpr isa_admission<x86> classify_x86_features(isa<x86> present,
745 isa<x86> observed,std::uint64_t xcr0,
bool readable,isa<x86> bits)
noexcept {
746 auto result=classify_features(present,observed,bits);
747 if(bits.has(x86_feature::avx)) {
748 result.missing_xcr0_observation=!readable;
749 result.missing_xcr0=(bits.has(x86_feature::avx512f)?0xe6ull:0x6ull)&~(readable?xcr0:0ull);
758 template<
class C>
requires detail::normalized_features<C,x86> &&
requires(C
const & c) { c.xcr0; c.xcr0_observed; }
760 return detail::classify_x86_features(cpu.present,cpu.observed,cpu.xcr0,cpu.xcr0_observed,
761 feature_closure(requested&minimum));
766 template<
class C>
requires detail::normalized_features<C,arm>
768 return detail::classify_features(cpu.present,cpu.observed,feature_closure(requested&minimum));
773 template<
class C>
requires detail::normalized_features<C,wasm>
775 return detail::classify_features(runtime.present,runtime.observed,feature_closure(requested&minimum));
780 template<detail::x86_observation C>
requires (!detail::has_feature_observation<C>) &&
requires(C
const & c) { c.xcr0; c.xcr0_observed; }
782 auto features=detail::decode_x86_features(cpu);
783 bool readable=cpu.max_basic_leaf>=1 && (cpu.leaf1_ecx&(1u<<26)) &&
784 (cpu.leaf1_ecx&(1u<<27)) && cpu.xcr0_observed;
785 return detail::classify_x86_features(features.present,features.observed,cpu.xcr0,readable,
786 feature_closure(requested&minimum));
791 template<detail::arm_observation C>
requires (!detail::has_feature_observation<C>)
793 auto features=detail::decode_arm_features(cpu);
794 return detail::classify_features(features.present,features.observed,feature_closure(requested&minimum));
807 template<detail::instruction_feature E>
target_entry(E)->target_entry<detail::family_of<E>>;
809 template<arch A,arch B>
requires detail::same_arch<A,B>
811 template<
auto... Entries>
struct isa_list {};
813 template<
auto... A,
auto... B>
814 constexpr isa_list<A...,B...>
operator+(isa_list<A...>,isa_list<B...>)
noexcept {
return {}; }
817 template<auto E,
int I>
struct abi_match {
818 static constexpr auto entry=[] {
819 if constexpr(arch<std::remove_cv_t<
decltype(E)>>)
return target_entry<arch_family_v<std::remove_cv_t<
decltype(E)>>>{E};
822 static constexpr auto architecture=entry.architecture;
823 static constexpr auto minimum=entry.minimum;
824 static_assert(architecture<=known_features<
decltype(architecture)::family>,
"ABI policy contains an unregistered ISA feature");
825 static_assert(minimum<=known_features<
decltype(minimum)::family>,
"ABI policy minimum contains an unregistered ISA feature");
826 static constexpr auto required_features=
feature_closure(architecture&minimum);
827 static constexpr bool matched=
true;
828 static constexpr int index=I;
830 template<
isa A,
int I,
auto... Entries>
struct abi_lookup_impl {
831 static constexpr bool matched=
false;
832 static constexpr int index=-1;
833 static constexpr decltype(A) architecture{},minimum{},required_features{};
835 template<
isa A,
int I,
auto E,
auto... Rest>
836 struct abi_lookup_impl<A,I,E,Rest...> : std::conditional_t<
837 abi_match<E,I>::required_features<=A,
838 abi_match<E,I>,abi_lookup_impl<A,I+1,Rest...>> {};
842 template<isa A,class List> struct abi_lookup;
843 template<isa A,auto... Entries>
844 requires ((decltype(detail::abi_match<Entries,0>::architecture)::family==decltype(A)::family) && ...)
845 struct abi_lookup<A,isa_list<Entries...>> : detail::abi_lookup_impl<A,0,Entries...> {};
848 template<class C> concept capability_observation=
849 normalized_features<C,x86> || normalized_features<C,arm> || normalized_features<C,wasm> ||
850 x86_observation<C> || arm_observation<C>;
851 template<capability_observation C> inline constexpr architecture observation_family=[] {
852 if constexpr(normalized_features<C,x86>) return x86;
853 else if constexpr(normalized_features<C,arm>) return arm;
854 else if constexpr(normalized_features<C,wasm>) return wasm;
855 else if constexpr(x86_observation<C>) return x86;
860 template<auto... Entries,detail::capability_observation C,class F>
861 requires ((decltype(detail::abi_match<Entries,0>::architecture)::family==detail::observation_family<C>) && ...)
862 constexpr bool with_isa(isa_list<Entries...>,C const & cpu,F && callback,isa<detail::observation_family<C>> minimum={}) {
864 auto try_entry=[&]<auto E>() {
865 using entry=detail::abi_match<E,0>;
866 if(!selected && classify_isa(cpu,entry::architecture,minimum&entry::minimum).admitted()) {
868 std::forward<F>(callback).template operator()<entry::architecture>();
871 (try_entry.template operator()<Entries>(),...);
A feature enum or an ISA value from one architecture family.
constexpr std::uint32_t crc32(std::uint32_t accumulator, std::uint8_t value) noexcept
Update the CRC32 accumulator with exactly 8 input bits.
constexpr simd< std::uint64_t, 2, Arch > pmull(std::uint64_t a, std::uint64_t b) noexcept
std::string_view const type
typename mask_traits< std::remove_cvref_t< T > >::type mask
constexpr std::uint16_t lzcnt(std::uint16_t value) noexcept
constexpr std::uint16_t popcnt(std::uint16_t value) noexcept
constexpr simd< std::uint64_t, 2, Arch > vpclmulqdq(simd< std::uint64_t, 2, Arch > a, simd< std::uint64_t, 2, Arch > b) noexcept
Multiply selected halves of one 128-bit lane using PCLMUL and AVX.
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr std::size_t arm_feature_count
Number of named ARM feature values.
constexpr architecture arm
Select ARM feature metadata independently of the compilation target.
constexpr architecture x86
Select x86 feature metadata independently of the compilation target.
void operator>=(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
void operator>(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
x86_feature
Architecture-specific instruction features, represented by local bit indices.
architecture
Instruction-set families; an ISA value belongs to exactly one family.
void operator|(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
target_entry(isa< Family >, isa< Family >={}) -> target_entry< Family >
Deduce a source record from same-family ISA values.
constexpr auto feature_closure(A input) noexcept
constexpr architecture wasm
Select WebAssembly feature metadata independently of the compilation target.
constexpr simd< fp16, 32, Arch > fma(simd< fp16, 32, Arch > a, simd< fp16, 32, Arch > b, simd< fp16, 32, Arch > c) noexcept
void operator<=(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
constexpr std::size_t x86_feature_count
Number of named x86 feature values.
constexpr isa_admission< x86 > classify_isa(C const &cpu, isa< x86 > requested, isa< x86 > minimum={}) noexcept
@ unknown
Any other vendor string.
constexpr int target
First matching requirement, with every later choice checked for shadowing.
void operator&(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
void operator+(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
constexpr isa< Family > target_features(std::string_view text) noexcept
wasm_feature
WebAssembly validation capabilities, using local bit indices.
void operator<(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
constexpr std::size_t wasm_feature_count
Number of named Wasm feature values.
constexpr polyfill_t polyfill
Compose this permission with an ISA using A | polyfill. Used alone, it selects scalar emulation in th...
arm_feature
Independently observable ARM instruction features, using local bit indices.
isa(E) -> isa< detail::family_of< E > >
Deduce the family from a feature enum; explicit isa<> always names the host family.
bool neon
Read or update the neon feature bit through flags.
constexpr bool has(isa other) const noexcept
typename detail::feature_traits< arm >::type feature_type
Feature enum accepted by this ISA family.
static constexpr auto feature_count
Number of registered features in this family.
bool lse
Read or update the lse feature bit through flags.
bool i8mm
Read or update the i8mm feature bit through flags.
constexpr bool has(polyfill_t) const noexcept
True when semantic emulation or storage decomposition is permitted.
bool fp16fml
Read or update the fp16fml feature bit through flags.
std::array< std::uint64_t,(feature_count+1+63)/64 > flags
Public structural bits; invalid enum values and padding fail validation.
constexpr bool valid() const noexcept
True when no invalid marker or unregistered padding bit is set.
static constexpr architecture family
Architecture family, independent of the compiler target.
bool neon_bf16
Read or update the neon_bf16 feature bit through flags.
bool sha1
Read or update the sha1 feature bit through flags.
constexpr bool get(feature_type f) const noexcept
Read one feature; out-of-range values are absent.
bool pauth
Read or update the pauth feature bit through flags.
constexpr bool has(feature_type f) const noexcept
True when the feature is present.
bool aes
Read or update the aes feature bit through flags.
constexpr void set(feature_type f, bool value) noexcept
bool sha3
Read or update the sha3 feature bit through flags.
bool jsconv
Read or update the jsconv feature bit through flags.
constexpr isa() noexcept=default
Construct the empty requirement set.
constexpr isa(polyfill_t) noexcept
Construct an empty hardware requirement with emulation permitted.
bool crc
Read or update the crc feature bit through flags.
bool sha512
Read or update the sha512 feature bit through flags.
bool rcpc
Read or update the rcpc feature bit through flags.
bool sm4
Read or update the sm4 feature bit through flags.
bool allow_polyfill
Structural emulation permission; hardware feature tests do not read it.
bool sha2
Read or update the sha2 feature bit through flags.
bool neon_fp16
Read or update the neon_fp16 feature bit through flags.
bool complxnum
Read or update the complxnum feature bit through flags.
bool pmull
Read or update the pmull feature bit through flags.
bool dotprod
Read or update the dotprod feature bit through flags.
constexpr bool operator==(isa const &) const =default
Compare hardware bits and emulation permission, retaining NTTP identity.
bool rdm
Read or update the rdm feature bit through flags.
constexpr void set(feature_type f, bool value) noexcept
constexpr isa() noexcept=default
Construct the empty requirement set.
bool simd128
Read or update the simd128 feature bit through flags.
constexpr bool has(polyfill_t) const noexcept
True when semantic emulation or storage decomposition is permitted.
constexpr bool has(feature_type f) const noexcept
True when the feature is present.
constexpr bool operator==(isa const &) const =default
Compare hardware bits and emulation permission, retaining NTTP identity.
std::array< std::uint64_t,(feature_count+1+63)/64 > flags
Public structural bits; invalid enum values and padding fail validation.
bool allow_polyfill
Structural emulation permission; hardware feature tests do not read it.
constexpr bool valid() const noexcept
True when no invalid marker or unregistered padding bit is set.
constexpr isa(polyfill_t) noexcept
Construct an empty hardware requirement with emulation permitted.
static constexpr architecture family
Architecture family, independent of the compiler target.
typename detail::feature_traits< wasm >::type feature_type
Feature enum accepted by this ISA family.
static constexpr auto feature_count
Number of registered features in this family.
constexpr bool get(feature_type f) const noexcept
Read one feature; out-of-range values are absent.
bool relaxed_simd
Read or update the relaxed_simd feature bit through flags.
constexpr bool has(isa other) const noexcept
bool lzcnt
Read or update the lzcnt feature bit through flags.
bool sse42
Read or update the sse42 feature bit through flags.
bool sha512
Read or update the sha512 feature bit through flags.
bool avx512bw
Read or update the avx512bw feature bit through flags.
bool bmi1
Read or update the bmi1 feature bit through flags.
bool avx512dq
Read or update the avx512dq feature bit through flags.
typename detail::feature_traits< x86 >::type feature_type
Feature enum accepted by this ISA family.
bool crc32
Read or update the crc32 feature bit through flags.
bool allow_polyfill
Structural emulation permission; hardware feature tests do not read it.
bool sse2
Read or update the sse2 feature bit through flags.
bool avx512f
Read or update the avx512f feature bit through flags.
bool sse
Read or update the sse feature bit through flags.
bool f16c
Read or update the f16c feature bit through flags.
static constexpr auto feature_count
Number of registered features in this family.
bool avx512bitalg
Read or update the avx512bitalg feature bit through flags.
constexpr bool has(polyfill_t) const noexcept
True when semantic emulation or storage decomposition is permitted.
bool ssse3
Read or update the ssse3 feature bit through flags.
bool sm3
Read or update the sm3 feature bit through flags.
bool avxifma
Read or update the avxifma feature bit through flags.
bool avx512fp16
Read or update the avx512fp16 feature bit through flags.
bool popcnt
Read or update the popcnt feature bit through flags.
bool avxneconvert
Read or update the avxneconvert feature bit through flags.
bool adx
Read or update the adx feature bit through flags.
bool avx
Read or update the avx feature bit through flags.
bool sha
Read or update the sha feature bit through flags.
bool avx512vl
Read or update the avx512vl feature bit through flags.
bool avx512bf16
Read or update the avx512bf16 feature bit through flags.
bool avxvnniint8
Read or update the avxvnniint8 feature bit through flags.
bool avx512ifma
Read or update the avx512ifma feature bit through flags.
bool fma
Read or update the fma feature bit through flags.
bool avx512vbmi2
Read or update the avx512vbmi2 feature bit through flags.
bool avx2
Read or update the avx2 feature bit through flags.
constexpr bool operator==(isa const &) const =default
Compare hardware bits and emulation permission, retaining NTTP identity.
constexpr isa() noexcept=default
Construct the empty requirement set.
bool aes
Read or update the aes feature bit through flags.
constexpr bool valid() const noexcept
True when no invalid marker or unregistered padding bit is set.
bool avxvnniint16
Read or update the avxvnniint16 feature bit through flags.
bool cx16
Read or update the cx16 feature bit through flags.
bool sm4
Read or update the sm4 feature bit through flags.
bool mmx
Read or update the mmx feature bit through flags.
constexpr bool has(feature_type f) const noexcept
True when the feature is present.
bool vpclmulqdq
Read or update the vpclmulqdq feature bit through flags.
constexpr bool has(isa other) const noexcept
bool sse41
Read or update the sse41 feature bit through flags.
std::array< std::uint64_t,(feature_count+1+63)/64 > flags
Public structural bits; invalid enum values and padding fail validation.
bool avx512vnni
Read or update the avx512vnni feature bit through flags.
bool gfni
Read or update the gfni feature bit through flags.
static constexpr architecture family
Architecture family, independent of the compiler target.
bool mwaitx
Read or update the mwaitx feature bit through flags.
bool sahf
Read or update the sahf feature bit through flags.
constexpr bool get(feature_type f) const noexcept
Read one feature; out-of-range values are absent.
bool avx512vbmi
Read or update the avx512vbmi feature bit through flags.
bool avx512vpopcntdq
Read or update the avx512vpopcntdq feature bit through flags.
constexpr void set(feature_type f, bool value) noexcept
bool waitpkg
Read or update the waitpkg feature bit through flags.
constexpr isa(polyfill_t) noexcept
Construct an empty hardware requirement with emulation permitted.
bool pclmul
Read or update the pclmul feature bit through flags.
bool vaes
Read or update the vaes feature bit through flags.
bool avx512cd
Read or update the avx512cd feature bit through flags.
bool sse3
Read or update the sse3 feature bit through flags.
bool avxvnni
Read or update the avxvnni feature bit through flags.
bool movbe
Read or update the movbe feature bit through flags.
bool bmi2
Read or update the bmi2 feature bit through flags.
Admission result for requirements from one architecture family.
constexpr char const * reason() const noexcept
constexpr bool admitted() const noexcept
True only when every requested feature and OS state component is present.
A structural set of one family's features, without prerequisite closure.
static constexpr auto feature_count
constexpr bool has(polyfill_t) const noexcept
True when semantic emulation or storage decomposition is permitted.
constexpr bool has(feature_type f) const noexcept
True when the feature is present.
constexpr isa(polyfill_t) noexcept
Construct an empty hardware requirement with emulation permitted.
constexpr isa() noexcept=default
Construct the empty requirement set.
static constexpr architecture family
constexpr bool operator==(isa const &) const =default
Compare hardware bits and emulation permission, retaining NTTP identity.
std::array< std::uint64_t,(feature_count+1+63)/64 > flags
constexpr void set(feature_type f, bool value) noexcept
typename detail::feature_traits< Family >::type feature_type
constexpr bool has(isa other) const noexcept
constexpr bool get(feature_type f) const noexcept
Read one feature; out-of-range values are absent.
constexpr bool valid() const noexcept
True when no invalid marker or unregistered padding bit is set.
waiter using the MONITORX/MWAITX instruction feature
Permission to emulate operations or decompose unsupported SIMD shapes. This is policy,...
A source variant retains its requested ISA and inherited compiler minimum.
constexpr bool operator==(target_entry const &) const =default
Compare the requested ISA and inherited compiler minimum exactly.