native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
polyfill.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4
5namespace native::detail {
6 enum class polyfill_element_kind { ordinary, binary16, bfloat16, binary64 };
7 // Half storage types specialize this after native.numerics is imported. Their
8 // ownership stays with that module; the semantic implementation remains GMF.
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>>;
15 };
16 // Hardware-only lookup must never recursively select the emulated shape.
17 template<isa<> A> inline constexpr isa<> hardware_isa=[] {
18 auto result=A;
19 result.allow_polyfill=false;
20 return result;
21 }();
22 // Storage-only ordinary shapes are supplied by native.simd's instruction
23 // interfaces. Keep their availability shared without depending on whether a
24 // textual or module consumer has seen those later class definitions.
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;
28 else {
29 [[maybe_unused]] constexpr auto bytes=sizeof(T)*N;
30#if NATIVE_HOST_NEON
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;
38#elif NATIVE_HOST_X86
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>));
47 }
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))));
51 return false;
52#else
53 return false;
54#endif
55 }
56 }();
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>>); };
60
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>);
64
65#if NATIVE_HOST_WASM
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_point 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);
75 }
76#endif
77
78 // Shared helpers exist independently of the maintained native Wasm opcode surface.
79 template<class T,std::size_t N,isa<> A> concept polyfill_helper_shape=polyfill_operation_shape<T,N,A>
80#if NATIVE_HOST_WASM
81 || (A.has(polyfill) && A.has(wasm_feature::simd128) && sizeof(T)*N==16)
82#endif
83 ;
84
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;
89 };
90 // Delegate corresponding logical chunks to their hardware-only architecture.
91 // Padding stays representation-local and is never reported as a logical lane.
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);
98 }
99
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>();
105 else return 1;
106 }
107}
108
109namespace native::detail {
112 template<class Self,std::size_t N,isa<> A> requires(N>0 && N<=64)
113 struct polyfill_mask {
114 static constexpr isa<> architecture=A;
115 static constexpr std::size_t lanes=N;
116 using native_type=std::uint64_t;
117 using mask_type=Self;
118 using mask=Self;
119 static constexpr bool compact=true;
120 private:
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;
125 }();
126 public:
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;
134 }
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); }
173 };
174
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>;
178 using base::base;
179 constexpr polyfill_predicate() noexcept=default;
180 };
181}
182
183namespace native {
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>;
188 using base::base;
189 constexpr predicate() noexcept=default;
190 };
191}
192
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
Definition mask_traits.h:22
constexpr auto operator!(wide< T, N > const &a)
Definition wide.h:613
constexpr auto operator~(wide< T, N > const &a)
Definition wide.h:606
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.
Definition config.h:24
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...
Definition isa.h:43
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.