5namespace native::detail {
6 enum class polyfill_element_kind { ordinary, binary16, bfloat16, binary64 };
9 template<
class T>
struct polyfill_element_traits {
10 static constexpr bool supported=ordinary_simd_element<T> || std::same_as<T,double>;
11 static constexpr bool arithmetic=std::same_as<T,float> || std::same_as<T,double>;
12 static constexpr auto kind=std::same_as<T,double>?polyfill_element_kind::binary64:polyfill_element_kind::ordinary;
13 using format=std::conditional_t<
sizeof(T)==8,constexpr_float::binary64,
14 std::conditional_t<
sizeof(T)==2,constexpr_float::binary16,constexpr_float::binary32>>;
17 template<isa<> A>
inline constexpr isa<> hardware_isa=[] {
19 result.allow_polyfill=
false;
25 template<
class T,std::
size_t N,isa<> A>
26 inline constexpr bool instruction_only_shape=[] {
27 if constexpr(!polyfill_element_traits<T>::supported || N<2 || N>64/
sizeof(T))
return false;
29 [[maybe_unused]]
constexpr auto bytes=
sizeof(T)*N;
31 if constexpr(!(neon<=A))
return false;
32 if constexpr(polyfill_element_traits<T>::kind==polyfill_element_kind::binary16)
33 return N==4 || (N==8 && !(neon_fp16<=A));
34 if constexpr(polyfill_element_traits<T>::kind==polyfill_element_kind::bfloat16)
35 return N==4 || (N==8 && !(neon_bf16<=A));
36 if constexpr(std::same_as<T,double>)
return N==2;
37 return simd_integer_element<T> &&
sizeof(T)<4 && bytes==8;
39 if constexpr(!A.has(x86_feature::sse2))
return false;
40 if constexpr(polyfill_element_traits<T>::kind==polyfill_element_kind::binary16)
return N==4 || N==8;
41 if constexpr(polyfill_element_traits<T>::kind==polyfill_element_kind::bfloat16)
42 return N==4 || (N==8 && !(avx512_bf16<=A));
43 if constexpr(bytes==8)
return simd_integer_element<T> &&
sizeof(T)<4;
44 if constexpr(bytes==16 || bytes==32) {
45 if constexpr(bytes==32 && !A.has(x86_feature::avx))
return false;
46 return std::same_as<T,double> || (!(avx2<=A) && (simd_integer_element<T> || std::same_as<T,float>));
48 if constexpr(bytes==64 && A.has(x86_feature::avx512f))
49 return std::same_as<T,double> || ((simd_integer_element<T> || std::same_as<T,float>) &&
50 (!(kernel_base<=A) || (
sizeof(T)<4 && !A.has(x86_feature::avx512bw))));
57 template<
class T,std::
size_t N,isa<> A>
58 concept polyfill_shape=polyfill_element_traits<T>::supported && A.has(
polyfill) && N>0 && N<=64 &&
59 !instruction_only_shape<T,N,A> && !
requires {
sizeof(simd<T,N,hardware_isa<A>>); };
61 template<
class T,std::
size_t N,isa<> A>
62 concept polyfill_operation_shape=A.has(
polyfill) && polyfill_element_traits<T>::supported &&
63 (polyfill_shape<T,N,A> || instruction_only_shape<T,N,A>);
66 template<
class T>
concept polyfill_wasm_number=simd_integer_element<T> || std::same_as<T,float> || std::same_as<T,double>;
67 template<
class T,std::
size_t N,isa<> A>
concept polyfill_wasm_shape=polyfill_wasm_number<T> && A.has(
polyfill) &&
68 (!A.has(wasm_feature::simd128) ||
sizeof(T)*N!=16) &&
requires {
sizeof(simd<T,N,A>); };
69 template<
bool Maximum,std::
floating_po
int T>
constexpr T polyfill_wasm_minmax(T x,T y)
noexcept {
70 using F=
typename polyfill_element_traits<T>::format;
using U=
typename F::bits_type;
71 auto a=std::bit_cast<U>(x),b=std::bit_cast<U>(y);
72 if(constexpr_float::is_nan<F>(a) || constexpr_float::is_nan<F>(b))
return std::bit_cast<T>(constexpr_float::default_nan<F>({}));
73 if(x==T{} && y==T{})
return std::bit_cast<T>(Maximum?U(a&b):U(a|b));
74 return Maximum?(x>y?x:y):(x<y?x:y);
79 template<
class T,std::
size_t N,isa<> A>
concept polyfill_helper_shape=polyfill_operation_shape<T,N,A>
81 || (A.has(
polyfill) && A.has(wasm_feature::simd128) &&
sizeof(T)*N==16)
85 template<
class To,
class From>
concept polyfill_chunk_compatible=
requires {
86 typename To::chunk_type;
typename From::chunk_type;
87 requires To::register_lanes==From::register_lanes;
88 requires To::register_count==From::register_count;
92 template<
class To,
class From,
class F>
requires polyfill_chunk_compatible<To,From>
93 constexpr To polyfill_transform_chunks(From value,F operation)
noexcept {
94 auto input=value.to_native();
typename To::native_type output{};
95 for(std::size_t i=0;i<From::register_count;++i)
96 output[i]=operation(From::chunk_type::from_native(input[i])).to_native();
97 return To::from_native(output);
100 template<
class T,isa<> A,std::
size_t M=64/sizeof(T)>
101 consteval std::size_t polyfill_register_lanes() noexcept {
102 if constexpr(!instruction_only_shape<T,M,A> &&
103 requires {
typename simd<T,M,hardware_isa<A>>::native_type; })
return M;
104 else if constexpr(M>1)
return polyfill_register_lanes<T,A,M/2>();
109namespace native::detail {
112 template<
class Self,std::
size_t N,isa<> A>
requires(N>0 && N<=64)
113 struct polyfill_mask {
115 static constexpr std::size_t lanes=N;
116 using native_type=std::uint64_t;
117 using mask_type=Self;
119 static constexpr bool compact=
true;
121 native_type value_{};
122 static constexpr native_type active=[] {
123 if constexpr(N==64)
return ~native_type{};
124 else return (native_type{1}<<N)-1;
128 constexpr polyfill_mask() noexcept=default;
130 explicit constexpr polyfill_mask(
bool value) noexcept : value_(value?active:0) {}
132 static constexpr Self from_bitset(native_type bits)
noexcept {
133 Self result; result.value_=bits&active;
return result;
136 static constexpr Self from_bits(native_type bits)
noexcept {
return from_bitset(bits); }
138 static constexpr Self from_native(native_type bits)
noexcept {
return from_bitset(bits); }
140 constexpr native_type to_native() const noexcept {
return value_; }
142 constexpr native_type to_bitset() const noexcept {
return value_; }
144 constexpr native_type bits() const noexcept {
return value_; }
146 friend constexpr bool any(Self value)
noexcept {
return value.value_!=0; }
148 friend constexpr bool all(Self value)
noexcept {
return value.value_==active; }
150 friend constexpr bool none(Self value)
noexcept {
return value.value_==0; }
152 friend constexpr Self
operator~(Self value)
noexcept {
return from_bitset(~value.value_); }
154 friend constexpr Self
operator!(Self value)
noexcept {
return ~value; }
156 friend constexpr Self
operator&(Self a,Self b)
noexcept {
return from_bitset(a.value_&b.value_); }
158 friend constexpr Self
operator|(Self a,Self b)
noexcept {
return from_bitset(a.value_|b.value_); }
160 friend constexpr Self
operator^(Self a,Self b)
noexcept {
return from_bitset(a.value_^b.value_); }
162 friend constexpr Self
operator==(Self a,Self b)
noexcept {
return ~(a^b); }
164 friend constexpr Self
operator!=(Self a,Self b)
noexcept {
return a^b; }
166 constexpr Self &
operator&=(Self b)
noexcept {
return static_cast<Self &
>(*this)=
static_cast<Self &
>(*this)&b; }
168 constexpr Self &
operator|=(Self b)
noexcept {
return static_cast<Self &
>(*this)=
static_cast<Self &
>(*this)|b; }
170 constexpr Self &
operator^=(Self b)
noexcept {
return static_cast<Self &
>(*this)=
static_cast<Self &
>(*this)^b; }
172 friend constexpr Self
select(Self m,Self a,Self b)
noexcept {
return (m&a)|(~m&b); }
175 template<std::
size_t N,isa<> A>
requires(N>0 && N<=64)
176 struct polyfill_predicate : polyfill_mask<polyfill_predicate<N,A>,N,A> {
177 using base=polyfill_mask<polyfill_predicate,N,A>;
179 constexpr polyfill_predicate() noexcept=default;
185 template<std::
size_t N,isa<> A>
requires(A.has(
polyfill) && A<=scalar && N>0 && N<=64)
186 struct predicate<N,A> : detail::polyfill_mask<predicate<N,A>,N,A> {
187 using base=detail::polyfill_mask<predicate,N,A>;
189 constexpr predicate() noexcept=default;
193#include "native/simd/polyfill_feature_chunk.h"
194#include "native/simd/polyfill_scalar.h"
typename mask_traits< std::remove_cvref_t< T > >::type mask
constexpr auto operator!(wide< T, N > const &a)
constexpr auto operator~(wide< T, N > const &a)
constexpr bool all(simd< T, N, A > a) noexcept
Test whether every integer lane is nonzero.
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...
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...
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...
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 simd< T, N, A > select(M mask, simd< T, N, A > a, simd< T, N, A > b) noexcept
Bridge a matching compact mask to native half's existing vector-mask selection.
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 polyfill_t polyfill
Compose this permission with an ISA using A | polyfill. Used alone, it selects scalar emulation in th...
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 bool any(simd< T, N, A > a) noexcept
Test whether at least one integer lane is nonzero.