5namespace native::detail::NATIVE_BACKEND {
6 template<
class V>
constexpr auto wasm_polyfill_lanes(V value)
noexcept {
7 std::array<typename V::value_type,V::lanes> lanes{}; value.store(lanes.data());
return lanes;
9 template<
class V,
class Native,
class Scalar,
class... W>
10 constexpr V wasm_polyfill_map(Native native_operation,Scalar scalar_operation,V value,W... other)
noexcept {
11 if constexpr(V::architecture.has(wasm_feature::simd128) &&
requires {
typename V::chunk_type;
requires V::register_lanes*
sizeof(
typename V::value_type)==16; }) {
12 auto inputs=std::tuple{value.to_native(),other.to_native()...};
typename V::native_type result{};
13 for(std::size_t i=0;i<V::register_count;++i) std::apply([&](
auto const &... parts) {
14 result[i]=native_operation(V::chunk_type::from_native(parts[i])...).to_native();
16 return V::from_native(result);
18 auto inputs=std::tuple{wasm_polyfill_lanes(value),wasm_polyfill_lanes(other)...};
19 std::array<typename V::value_type,V::lanes> result{};
20 for(std::size_t i=0;i<V::lanes;++i) std::apply([&](
auto const &... lanes) { result[i]=scalar_operation(lanes[i]...); },inputs);
21 return V::load(result.data());
24 template<
class T>
constexpr T wasm_polyfill_saturate(std::int64_t value)
noexcept {
25 return T(std::clamp(value,std::int64_t(std::numeric_limits<T>::min()),std::int64_t(std::numeric_limits<T>::max())));
27 template<
class T>
using wasm_polyfill_wide_unsigned=std::conditional_t<
sizeof(T)==1,std::uint16_t,
28 std::conditional_t<
sizeof(T)==2,std::uint32_t,std::uint64_t>>;
29 template<
class T>
using wasm_polyfill_wide=std::conditional_t<std::is_signed_v<T>,
30 std::make_signed_t<wasm_polyfill_wide_unsigned<T>>,wasm_polyfill_wide_unsigned<T>>;
31 template<
bool High,
class T,std::
size_t N,isa<> A>
32 constexpr auto wasm_polyfill_extend(simd<T,N,A> value)
noexcept {
33 using W=wasm_polyfill_wide<T>;
using R=simd<W,N/2,A>;
34 auto source=wasm_polyfill_lanes(value); std::array<W,N/2> output{};
35 if constexpr(A.has(wasm_feature::simd128)) {
36 using C=simd<T,16/
sizeof(T),hardware_isa<A>>;
constexpr auto count=C::lanes/2;
37 for(std::size_t i=0;i<N/2;i+=count) {
38 std::array<T,C::lanes> part{};
39 for(std::size_t j=0;j<std::min(count,N/2-i);++j) part[j]=source[i+j+(High?N/2:0)];
40 std::array<W,count> widened{};
extend_low(C::load(part.data())).store(widened.data());
41 for(std::size_t j=0;j<std::min(count,N/2-i);++j) output[i+j]=widened[j];
43 }
else for(std::size_t i=0;i<N/2;++i) output[i]=W(source[i+(High?N/2:0)]);
44 return R::load(output.data());
46 template<
class T>
constexpr T wasm_polyfill_read(T
const * p)
noexcept {
47 if consteval {
return *p; }
48 else { T result; std::memcpy(&result,
reinterpret_cast<unsigned char const *
>(p),
sizeof(T));
return result; }
53 template<std::size_t... I,
class T,std::size_t N,
isa<> A>
54 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (
sizeof...(I)==N && ((I<2*N) && ...))
56 auto x=detail::NATIVE_BACKEND::wasm_polyfill_lanes(a),y=detail::NATIVE_BACKEND::wasm_polyfill_lanes(b);
57 std::array<T,N> output{(I<N?x[I]:y[I-N])...};
return simd<T,N,A>::load(output.data());
60 template<isa<> A>
requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<std::uint8_t,16,A>
62 auto x=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value),index=detail::NATIVE_BACKEND::wasm_polyfill_lanes(indices);
63 std::array<std::uint8_t,16> output{};
64 for(std::size_t i=0;i<16;++i) output[i]=index[i]<16?x[index[i]]:0;
67#define NATIVE_WASM_POLYFILL_SAT(NAME,OP) \
69 template<simd_integer_element T,std::size_t N,isa<> A> \
70 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (sizeof(T)<=2) \
71 constexpr simd<T,N,A> NAME(simd<T,N,A> a,simd<T,N,A> b) noexcept { \
72 return detail::NATIVE_BACKEND::wasm_polyfill_map([](auto x,auto y) { return NAME(x,y); }, \
73 [](T x,T y) { return detail::NATIVE_BACKEND::wasm_polyfill_saturate<T>(std::int64_t(x) OP std::int64_t(y)); },a,b); \
75 NATIVE_WASM_POLYFILL_SAT(
add_sat,+)
76 NATIVE_WASM_POLYFILL_SAT(
sub_sat,-)
77#undef NATIVE_WASM_POLYFILL_SAT
79 template<simd_
integer_element T,std::
size_t N,isa<> A>
80 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (
sizeof(T)<=2 && std::is_unsigned_v<T>)
82 return detail::NATIVE_BACKEND::wasm_polyfill_map([](
auto x,
auto y) {
return average_round(x,y); },
83 [](T x,T y) {
return T((
unsigned(x)+
unsigned(y)+1)/2); },a,b);
86 template<simd_
integer_element T,std::
size_t N,isa<> A>
87 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && std::is_signed_v<T>
89 return detail::NATIVE_BACKEND::wasm_polyfill_map([](
auto x) {
return abs(x); },[](T x) {
90 using U=std::make_unsigned_t<T>;
return x<0?std::bit_cast<T>(U(U(0)-U(x))):x;
93#define NATIVE_WASM_POLYFILL_INTEGER_MINMAX(NAME,OP) \
95 template<simd_integer_element T,std::size_t N,isa<> A> \
96 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> \
97 constexpr simd<T,N,A> NAME(simd<T,N,A> a,simd<T,N,A> b) noexcept { \
98 return detail::NATIVE_BACKEND::wasm_polyfill_map([](auto x,auto y) { return NAME(x,y); },[](T x,T y) { return x OP y?x:y; },a,b); \
100 NATIVE_WASM_POLYFILL_INTEGER_MINMAX(
min,<)
101 NATIVE_WASM_POLYFILL_INTEGER_MINMAX(
max,>)
102#undef NATIVE_WASM_POLYFILL_INTEGER_MINMAX
103#define NATIVE_WASM_POLYFILL_FLOAT_MINMAX(NAME,MAXIMUM) \
105 template<std::floating_point T,std::size_t N,isa<> A> \
106 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> \
107 constexpr simd<T,N,A> NAME(simd<T,N,A> a,simd<T,N,A> b) noexcept { \
108 return detail::NATIVE_BACKEND::wasm_polyfill_map([](auto x,auto y) { return NAME(x,y); }, \
109 [](T x,T y) { return detail::polyfill_wasm_minmax<MAXIMUM>(x,y); },a,b); \
111 NATIVE_WASM_POLYFILL_FLOAT_MINMAX(
min,
false)
112 NATIVE_WASM_POLYFILL_FLOAT_MINMAX(
max,true)
113#undef NATIVE_WASM_POLYFILL_FLOAT_MINMAX
114#define NATIVE_WASM_POLYFILL_PMINMAX(NAME,OP) \
116 template<std::floating_point T,std::size_t N,isa<> A> \
117 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> \
118 constexpr simd<T,N,A> NAME(simd<T,N,A> a,simd<T,N,A> b) noexcept { \
119 return detail::NATIVE_BACKEND::wasm_polyfill_map([](auto x,auto y) { return NAME(x,y); },[](T x,T y) { return y OP x?y:x; },a,b); \
121 NATIVE_WASM_POLYFILL_PMINMAX(
pmin,<)
122 NATIVE_WASM_POLYFILL_PMINMAX(
pmax,>)
123#undef NATIVE_WASM_POLYFILL_PMINMAX
125 template<simd_
integer_element T,std::
size_t N,isa<> A>
126 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (N%2==0 &&
sizeof(T)<=4)
127 constexpr auto extend_low(
simd<T,N,A> value)
noexcept {
return detail::NATIVE_BACKEND::wasm_polyfill_extend<false>(value); }
129 template<simd_
integer_element T,std::
size_t N,isa<> A>
130 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (N%2==0 &&
sizeof(T)<=4)
131 constexpr auto extend_high(
simd<T,N,A> value)
noexcept {
return detail::NATIVE_BACKEND::wasm_polyfill_extend<true>(value); }
133 template<simd_
integer_element T,std::
size_t N,isa<> A>
134 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (N%2==0 &&
sizeof(T)<=4)
137 template<simd_
integer_element T,std::
size_t N,isa<> A>
138 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (N%2==0 &&
sizeof(T)<=4)
141 template<simd_
integer_element To,simd_
integer_element From,std::
size_t N,isa<> A>
142 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<From,N,A> &&
143 (
sizeof(From)==2*
sizeof(To) &&
sizeof(To)<=2 && std::is_signed_v<From> && N<=32)
145 auto x=detail::NATIVE_BACKEND::wasm_polyfill_lanes(a),y=detail::NATIVE_BACKEND::wasm_polyfill_lanes(b);
146 std::array<To,2*N> output{};
147 for(std::size_t i=0;i<N;++i) { output[i]=detail::NATIVE_BACKEND::wasm_polyfill_saturate<To>(x[i]); output[N+i]=detail::NATIVE_BACKEND::wasm_polyfill_saturate<To>(y[i]); }
151 template<isa<> A>
requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<std::int16_t,8,A>
153 return detail::NATIVE_BACKEND::wasm_polyfill_map([](
auto x,
auto y) {
return q15mulr_sat(x,y); },[](std::int16_t x,std::int16_t y) {
154 return detail::NATIVE_BACKEND::wasm_polyfill_saturate<std::int16_t>((std::int64_t(x)*y+16384)>>15);
158 template<isa<> A>
requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<std::int16_t,8,A>
160 auto x=detail::NATIVE_BACKEND::wasm_polyfill_lanes(a),y=detail::NATIVE_BACKEND::wasm_polyfill_lanes(b);
161 std::array<std::int32_t,4> output{};
162 for(std::size_t i=0;i<4;++i) output[i]=std::bit_cast<std::int32_t>(std::uint32_t(std::int64_t(x[2*i])*y[2*i]+std::int64_t(x[2*i+1])*y[2*i+1]));
166 template<simd_
integer_element To,std::
floating_po
int From,std::
size_t N,isa<> A>
167 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<From,N,A> && (
sizeof(From)*N==16 &&
sizeof(To)==4)
169 auto input=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value); std::array<To,4> output{};
170 for(std::size_t i=0;i<N;++i) {
172 if(x!=x) output[i]=0;
173 else if(x<=
double(std::numeric_limits<To>::min())) output[i]=std::numeric_limits<To>::min();
174 else if(x>=
double(std::numeric_limits<To>::max())) output[i]=std::numeric_limits<To>::max();
175 else output[i]=To(x);
180 template<std::
floating_po
int To,detail::polyfill_wasm_number From,std::
size_t N,isa<> A>
181 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<From,N,A> &&
182 (
sizeof(From)*N==16 && (
sizeof(From)==4 || std::floating_point<From>) && !std::same_as<To,From>)
184 auto input=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value); std::array<To,16/
sizeof(To)> output{};
185 for(std::size_t i=0;i<std::min(N,output.size());++i) {
187 if constexpr(std::floating_point<From>) {
188 using F=
typename detail::polyfill_element_traits<From>::format;
using G=
typename detail::polyfill_element_traits<To>::format;
189 output[i]=std::bit_cast<To>(detail::constexpr_float::convert_bits<G,F>(std::bit_cast<typename F::bits_type>(input[i])));
190 }
else output[i]=
static_cast<To
>(input[i]);
191 }
else { output[i]=
static_cast<To
>(input[i]); }
193 return simd<To,16/
sizeof(To),A>::load(output.data());
196 template<
class V>
requires NATIVE_ARCH_REQUIRES(V::architecture) && detail::polyfill_wasm_shape<typename V::value_type,V::lanes,V::architecture>
197 constexpr V
load_splat(
typename V::value_type
const * p)
noexcept {
return V(detail::NATIVE_BACKEND::wasm_polyfill_read(p)); }
199 template<std::
size_t I,
class T,std::
size_t N,isa<> A>
200 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (I<N)
202 auto lanes=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value); lanes[I]=detail::NATIVE_BACKEND::wasm_polyfill_read(p);
206 template<std::
size_t I,
class T,std::
size_t N,isa<> A>
207 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (I<N)
209 auto lanes=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value);
210 if consteval { *p=lanes[I]; }
211 else { std::memcpy(
reinterpret_cast<unsigned char *
>(p),lanes.data()+I,
sizeof(T)); }
214 template<
class V>
requires NATIVE_ARCH_REQUIRES(V::architecture) &&
215 detail::polyfill_wasm_shape<typename V::value_type,V::lanes,V::architecture> && (
sizeof(
typename V::value_type)>=4)
216 constexpr V
load_zero(
typename V::value_type
const * p)
noexcept {
217 std::array<typename V::value_type,V::lanes> output{}; output[0]=detail::NATIVE_BACKEND::wasm_polyfill_read(p);
return V::load(output.data());
220 template<simd_
integer_element To,simd_
integer_element From,isa<> A>
221 requires NATIVE_ARCH_REQUIRES(A) && (A.has(
polyfill) && !A.has(wasm_feature::simd128)) &&
222 (
sizeof(To)==2*
sizeof(From) &&
sizeof(To)<=8 && std::is_signed_v<To> ==std::is_signed_v<From>)
224 std::array<From,8/
sizeof(From)> input{};
225 if consteval {
for(std::size_t i=0;i<input.size();++i) input[i]=p[i]; }
226 else { std::memcpy(input.data(),
reinterpret_cast<unsigned char const *
>(p),8); }
227 std::array<To,input.size()> output{};
for(std::size_t i=0;i<input.size();++i) output[i]=To(input[i]);
228 return simd<To,output.size(),A>::load(output.data());
231 template<simd_
integer_element T,std::
size_t N,isa<> A>
232 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A> && (std::is_signed_v<T> &&
sizeof(T)<=2 && N%2==0)
234 using W=detail::NATIVE_BACKEND::wasm_polyfill_wide<T>;
235 auto input=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value); std::array<W,N/2> output{};
236 for(std::size_t i=0;i<N/2;++i) output[i]=W(input[2*i])+W(input[2*i+1]);
237 return simd<W,N/2,A>::load(output.data());
240 template<simd_
integer_element T,std::
size_t N,isa<> A>
241 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A>
243 using R=std::conditional_t<(N<=32),std::uint32_t,std::uint64_t>;
244 auto input=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value); R bits{};
245 for(std::size_t i=0;i<N;++i) bits|=R((std::make_unsigned_t<T>(input[i])>>(
sizeof(T)*8-1))&1)<<i;
249 template<simd_
integer_element T,std::
size_t N,isa<> A>
250 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A>
252 for(
auto lane:detail::NATIVE_BACKEND::wasm_polyfill_lanes(value))
if(lane!=0)
return true;
return false;
255 template<simd_
integer_element T,std::
size_t N,isa<> A>
256 requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_wasm_shape<T,N,A>
258 for(
auto lane:detail::NATIVE_BACKEND::wasm_polyfill_lanes(value))
if(lane==0)
return false;
return true;
261 template<isa<> A>
requires NATIVE_ARCH_REQUIRES(A) && (A.has(
polyfill) && A.has(wasm_feature::simd128))
263 auto lanes=detail::NATIVE_BACKEND::wasm_polyfill_lanes(value);
264 for(
auto & lane:lanes) lane=std::bit_cast<float>(std::uint32_t(std::int32_t(lane)+127)<<23);
268 template<std::
floating_po
int T,std::
size_t N,isa<> A>
269 requires NATIVE_ARCH_REQUIRES(A) && (A.has(
polyfill) && A.has(wasm_feature::simd128) &&
sizeof(T)*N==16)
270 constexpr simd<T,N,A> fma(
simd<T,N,A> a,
simd<T,N,A> b,
simd<T,N,A> c)
noexcept {
271 auto x=detail::NATIVE_BACKEND::wasm_polyfill_lanes(a),y=detail::NATIVE_BACKEND::wasm_polyfill_lanes(b),z=detail::NATIVE_BACKEND::wasm_polyfill_lanes(c);
272 using F=
typename detail::polyfill_element_traits<T>::format;
using U=
typename F::bits_type;
273 for(std::size_t i=0;i<N;++i) {
274 if consteval { x[i]=std::bit_cast<T>(detail::constexpr_float::fma_bits<F>(std::bit_cast<U>(x[i]),std::bit_cast<U>(y[i]),std::bit_cast<U>(z[i]))); }
275 else { x[i]=std::fma(x[i],y[i],z[i]); }
constexpr auto abs(simd< float, N, Arch > a) noexcept
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr simd< std::int16_t, 8, A > q15mulr_sat(simd< std::int16_t, 8, A > a, simd< std::int16_t, 8, A > b) noexcept
Multiply signed Q15 lanes, round by adding 2^14, shift by 15 and saturate.
constexpr simd< T, N, A > average_round(simd< T, N, A > a, simd< T, N, A > b) noexcept
Rounded unsigned average: (a+b+1)/2 without intermediate overflow.
constexpr auto multiply_widened_high(simd< T, N, A > a, simd< T, N, A > b) noexcept
Multiply widened upper integer lanes; the complete product fits.
constexpr bool all(simd< T, N, A > a) noexcept
Test whether every integer lane is nonzero.
constexpr simd< To, N *2, A > narrow_sat(simd< From, N, A > a, simd< From, N, A > b) noexcept
Saturating concatenate from signed source lanes, including unsigned destinations.
constexpr simd< T, N, A > pmax(simd< T, N, A > a, simd< T, N, A > b) noexcept
Pseudo minimum/maximum selects the first operand for unordered or equal lanes.
constexpr simd< T, N, A > shuffle(simd< T, N, A > a, simd< T, N, A > b) noexcept
Select N lanes from the concatenation of two inputs, using constant indices.
constexpr simd< T, N, A > add_sat(simd< T, N, A > a, simd< T, N, A > b) noexcept
Saturate signed or unsigned byte/halfword arithmetic at the lane limits.
constexpr auto extend_high(simd< T, N, A > a) noexcept
Widen the upper half of integer lanes, preserving signedness.
constexpr simd< T, N, A > pmin(simd< T, N, A > a, simd< T, N, A > b) noexcept
Pseudo minimum/maximum selects the first operand for unordered or equal lanes.
constexpr auto multiply_widened_low(simd< T, N, A > a, simd< T, N, A > b) noexcept
Multiply widened lower integer lanes; the complete product fits.
constexpr simd< fp16, 32, Arch > fma(simd< fp16, 32, Arch > a, simd< fp16, 32, Arch > b, simd< fp16, 32, Arch > c) noexcept
constexpr simd< To, 4, A > trunc_sat(simd< From, N, A > a) noexcept
constexpr simd< std::uint8_t, 16, A > swizzle(simd< std::uint8_t, 16, A > v, simd< std::uint8_t, 16, A > indices) noexcept
Look up byte indices 0..15; every other index produces zero.
constexpr simd< T, N, A > max(simd< T, N, A > a, simd< T, N, A > b) noexcept
Minimum/maximum; floating NaNs propagate and signed zeros follow WebAssembly rules.
constexpr std::uint32_t bitmask(simd< T, N, A > a) noexcept
Gather each integer lane's sign bit into bit i of the scalar result.
constexpr auto pairwise_add_widened(simd< T, N, Arch > value) noexcept
constexpr V load_splat(typename V::value_type const *p) noexcept
Read one scalar and broadcast it; the access is exactly sizeof(T) bytes.
constexpr simd< std::int32_t, 4, A > dot(simd< std::int16_t, 8, A > a, simd< std::int16_t, 8, A > b) noexcept
Sum adjacent signed halfword products modulo 2^32.
constexpr auto extend_low(simd< T, N, A > a) noexcept
Widen the lower half of integer lanes, preserving signedness.
constexpr simd< To, 16/sizeof(To), A > load_widened(From const *p) noexcept
Read exactly eight bytes of source lanes and widen them with their signedness.
constexpr void store_lane(T *p, simd< T, N, A > a) noexcept
Write only lane I to one scalar object.
constexpr V load_zero(typename V::value_type const *p) noexcept
Read one 32- or 64-bit lane and zero the other lanes.
constexpr simd< T, N, A > sub_sat(simd< T, N, A > a, simd< T, N, A > b) noexcept
Saturate signed or unsigned byte/halfword arithmetic at the lane limits.
constexpr simd< To, N, A > convert(simd< From, N, A > value) noexcept
constexpr polyfill_t polyfill
Compose this permission with an ISA using A | polyfill. Used alone, it selects scalar emulation in th...
constexpr bool any(simd< T, N, A > a) noexcept
Test whether at least one integer lane is nonzero.
constexpr simd< T, N, A > load_lane(T const *p, simd< T, N, A > a) noexcept
Read one scalar into lane I, preserving the other lanes.
constexpr simd< T, N, A > min(simd< T, N, A > a, simd< T, N, A > b) noexcept
Minimum/maximum; floating NaNs propagate and signed zeros follow WebAssembly rules.
Omitted architecture arguments use the native.simd provider's baseline.