5#include "native/simd/common_body.h"
7namespace native::detail::NATIVE_BACKEND {
8 template<
class T>
using swizzle_word = std::conditional_t<
sizeof(T)==1,std::uint8_t,
9 std::conditional_t<
sizeof(T)==2,std::uint16_t,std::conditional_t<
sizeof(T)==4,std::uint32_t,std::uint64_t>>>;
10 template<
class T>
using swizzle_native = swizzle_word<T> __attribute__((ext_vector_type(4)));
11 template<std::size_t... I>
struct swizzle {
12 static constexpr std::size_t size=
sizeof...(I);
13 template<
class Self>
using result = std::conditional_t<size==1,
typename Self::value_type,
14 simd<typename Self::value_type,size,Self::architecture>>;
15 static constexpr bool unique=[] {
16 constexpr std::size_t indices[]{I...};
17 for(std::size_t i=0;i<size;++i)
18 for(std::size_t j=0;j<i;++j)
if(indices[i]==indices[j])
return false;
21 template<std::
size_t D>
static consteval int slot() {
22 constexpr std::size_t indices[]{I...};
23 for(std::size_t i=0;i<size;++i)
if(indices[i]==D)
return int(4+i);
26 template<
class Self>
static consteval bool readable() {
27 using T=
typename Self::value_type;
28 if constexpr(!((I<Self::lanes)&&...) || !std::is_trivially_copyable_v<T> ||
29 !std::default_initializable<T> || !(
sizeof(T)==1 ||
sizeof(T)==2 ||
sizeof(T)==4 ||
sizeof(T)==8))
return false;
30 else if constexpr(!
requires(Self
const & self,T * p) { self.template store_memory<1>(p); })
return false;
31 else if constexpr(size==1)
return std::copy_constructible<T>;
32 else return requires(T
const * p) {
34 { result<Self>::template load_memory<1>(p) } -> std::same_as<result<Self>>;
37 template<
class Self>
static consteval bool writable() {
38 if constexpr(std::is_const_v<Self> || !unique || !readable<Self>())
return false;
39 else if constexpr(size>1 && !
requires(result<Self>
const & rhs,
typename Self::value_type * p) { rhs.template store_memory<1>(p); })
return false;
40 else return std::is_copy_assignable_v<typename Self::value_type> &&
41 std::is_copy_constructible_v<result<Self>> &&
requires(Self & self,
typename Self::value_type
const * p) {
42 { Self::template load_memory<1>(p) } -> std::same_as<Self>;
43 self=Self::template load_memory<1>(p);
46 template<
class V>
static constexpr bool native_words=[] {
47 using T=
typename V::value_type;
48 if constexpr(simd_custom_element<T>)
return false;
49 else if constexpr(
requires {
typename V::native_type; })
return sizeof(
typename V::native_type)==
sizeof(swizzle_native<T>);
52 template<
class Self>
static hint_inline
constexpr auto words(Self
const & self) {
53 using T=
typename Self::value_type;
54 if constexpr(native_words<Self>) {
55 return __builtin_bit_cast(swizzle_native<T>,self.to_native());
57 std::array<T,4> values{};
58 self.template store_memory<1>(values.data());
59 return __builtin_bit_cast(swizzle_native<T>,values);
62 template<
class V>
static hint_inline
constexpr V from_words(swizzle_native<typename V::value_type> words) {
63 using T=
typename V::value_type;
64 if constexpr(native_words<V>) {
65 return V::from_native(__builtin_bit_cast(
typename V::native_type,words));
67 auto values=__builtin_bit_cast(std::array<T,4>,words);
68 return V::template load_memory<1>(values.data());
72 [[nodiscard]]
static hint_inline
constexpr result<Self> read(Self
const & self) {
73 using T=
typename Self::value_type;
74 auto value=words(self);
75 if constexpr(size==1) {
76 constexpr std::size_t indices[]{I...};
77 return std::bit_cast<T>(swizzle_word<T>(value[indices[0]]));
79 constexpr int indices[]{int(I)...};
80 auto shuffled=__builtin_shufflevector(value,value,
81 indices[0],indices[1],indices[2%size],indices[3%size]);
82 return from_words<result<Self>>(shuffled);
86 hint_inline
constexpr static result<Self> write(Self & self,result<Self> rhs) {
87 using T=
typename Self::value_type;
88 auto before=words(self);
89 auto replacement=[&] {
90 if constexpr(size==1)
return swizzle_native<T>{std::bit_cast<swizzle_word<T>>(rhs),0,0,0};
91 else return words(rhs);
93 auto shuffled=__builtin_shufflevector(before,replacement,slot<0>(),slot<1>(),slot<2>(),slot<3>());
94 self=from_words<Self>(shuffled);
99namespace native::detail {
102 template<
class T,std::
size_t N,::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) &&(N<=4)
103 struct swizzle_access<T,N,Arch> {
104 template<std::
size_t K>
using result = std::conditional_t<K==1,T,simd<T,K,Arch>>;
106#define NATIVE_SWIZZLE_FIELD(NAME, K, ...) \
107 template<class Self> \
108 requires(::NATIVE_BACKEND_NAMESPACE::swizzle<__VA_ARGS__>::template readable<Self>()) \
109 [[nodiscard]] hint_inline constexpr result<K> get_##NAME(this Self const & self) { \
110 return ::NATIVE_BACKEND_NAMESPACE::swizzle<__VA_ARGS__>::read(self); \
112 template<class Self> \
113 requires(::NATIVE_BACKEND_NAMESPACE::swizzle<__VA_ARGS__>::template writable<Self>()) \
114 hint_inline constexpr result<K> set_##NAME(this Self & self, result<K> rhs) { \
115 return ::NATIVE_BACKEND_NAMESPACE::swizzle<__VA_ARGS__>::write(self, rhs); \
117 __declspec(property(get=get_##NAME, put=set_##NAME)) result<K> NAME;
119#define NATIVE_SWIZZLE_ROW2(A, I, X, Y, Z, W) \
120 NATIVE_SWIZZLE_FIELD(A##X, 2, I, 0) \
121 NATIVE_SWIZZLE_FIELD(A##Y, 2, I, 1) \
122 NATIVE_SWIZZLE_FIELD(A##Z, 2, I, 2) \
123 NATIVE_SWIZZLE_FIELD(A##W, 2, I, 3)
125#define NATIVE_SWIZZLE_ROW3(A, I, B, J, X, Y, Z, W) \
126 NATIVE_SWIZZLE_FIELD(A##B##X, 3, I, J, 0) \
127 NATIVE_SWIZZLE_FIELD(A##B##Y, 3, I, J, 1) \
128 NATIVE_SWIZZLE_FIELD(A##B##Z, 3, I, J, 2) \
129 NATIVE_SWIZZLE_FIELD(A##B##W, 3, I, J, 3)
131#define NATIVE_SWIZZLE_PLANE3(A, I, X, Y, Z, W) \
132 NATIVE_SWIZZLE_ROW3(A, I, X, 0, X, Y, Z, W) \
133 NATIVE_SWIZZLE_ROW3(A, I, Y, 1, X, Y, Z, W) \
134 NATIVE_SWIZZLE_ROW3(A, I, Z, 2, X, Y, Z, W) \
135 NATIVE_SWIZZLE_ROW3(A, I, W, 3, X, Y, Z, W)
137#define NATIVE_SWIZZLE_ROW4(A, I, B, J, C, K, X, Y, Z, W) \
138 NATIVE_SWIZZLE_FIELD(A##B##C##X, 4, I, J, K, 0) \
139 NATIVE_SWIZZLE_FIELD(A##B##C##Y, 4, I, J, K, 1) \
140 NATIVE_SWIZZLE_FIELD(A##B##C##Z, 4, I, J, K, 2) \
141 NATIVE_SWIZZLE_FIELD(A##B##C##W, 4, I, J, K, 3)
143#define NATIVE_SWIZZLE_PLANE4(A, I, B, J, X, Y, Z, W) \
144 NATIVE_SWIZZLE_ROW4(A, I, B, J, X, 0, X, Y, Z, W) \
145 NATIVE_SWIZZLE_ROW4(A, I, B, J, Y, 1, X, Y, Z, W) \
146 NATIVE_SWIZZLE_ROW4(A, I, B, J, Z, 2, X, Y, Z, W) \
147 NATIVE_SWIZZLE_ROW4(A, I, B, J, W, 3, X, Y, Z, W)
149#define NATIVE_SWIZZLE_CUBE4(A, I, X, Y, Z, W) \
150 NATIVE_SWIZZLE_PLANE4(A, I, X, 0, X, Y, Z, W) \
151 NATIVE_SWIZZLE_PLANE4(A, I, Y, 1, X, Y, Z, W) \
152 NATIVE_SWIZZLE_PLANE4(A, I, Z, 2, X, Y, Z, W) \
153 NATIVE_SWIZZLE_PLANE4(A, I, W, 3, X, Y, Z, W)
155#define NATIVE_SWIZZLE4(X, Y, Z, W) \
156 NATIVE_SWIZZLE_FIELD(X, 1, 0) \
157 NATIVE_SWIZZLE_FIELD(Y, 1, 1) \
158 NATIVE_SWIZZLE_FIELD(Z, 1, 2) \
159 NATIVE_SWIZZLE_FIELD(W, 1, 3) \
160 NATIVE_SWIZZLE_ROW2(X, 0, X, Y, Z, W) \
161 NATIVE_SWIZZLE_ROW2(Y, 1, X, Y, Z, W) \
162 NATIVE_SWIZZLE_ROW2(Z, 2, X, Y, Z, W) \
163 NATIVE_SWIZZLE_ROW2(W, 3, X, Y, Z, W) \
164 NATIVE_SWIZZLE_PLANE3(X, 0, X, Y, Z, W) \
165 NATIVE_SWIZZLE_PLANE3(Y, 1, X, Y, Z, W) \
166 NATIVE_SWIZZLE_PLANE3(Z, 2, X, Y, Z, W) \
167 NATIVE_SWIZZLE_PLANE3(W, 3, X, Y, Z, W) \
168 NATIVE_SWIZZLE_CUBE4(X, 0, X, Y, Z, W) \
169 NATIVE_SWIZZLE_CUBE4(Y, 1, X, Y, Z, W) \
170 NATIVE_SWIZZLE_CUBE4(Z, 2, X, Y, Z, W) \
171 NATIVE_SWIZZLE_CUBE4(W, 3, X, Y, Z, W)
173 NATIVE_SWIZZLE4(x, y, z, w)
174#undef NATIVE_SWIZZLE4
175#undef NATIVE_SWIZZLE_CUBE4
176#undef NATIVE_SWIZZLE_PLANE4
177#undef NATIVE_SWIZZLE_ROW4
178#undef NATIVE_SWIZZLE_PLANE3
179#undef NATIVE_SWIZZLE_ROW3
180#undef NATIVE_SWIZZLE_ROW2
181#undef NATIVE_SWIZZLE_FIELD
186 namespace detail::NATIVE_BACKEND {
188 template<std::
size_t N>
inline constexpr bool float_shape = N==1
189#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON
190 || N==2 || N==3 || N==4
195#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
199 template <
class T>
inline constexpr std::size_t mask_lane_bytes = [] {
200 if constexpr (::native::simd_custom_element<T>)
return sizeof(typename ::native::simd_traits<T>::storage_type);
201 else return sizeof(T);
203 template <
class T>
concept mask_element = ::native::simd_custom_element<T> ||
204 (std::is_arithmetic_v<T> && !std::is_const_v<T> && !std::is_volatile_v<T> &&
205 (
sizeof(T)==1 ||
sizeof(T)==2 ||
sizeof(T)==4 ||
sizeof(T)==8));
206 template <std::
size_t B,std::
size_t N>
inline constexpr bool mask_compact =
207#if NATIVE_HAS_AVX512F
209#if NATIVE_HAS_AVX512BW
213#
if NATIVE_HAS_AVX512VL
214 || B*N==16 || B*N==32
220 template <std::
size_t B,std::
size_t N>
inline constexpr bool mask_shape = N==1 || (B*N==64 && mask_compact<B,N>)
222 || B*N==16 || B*N==32
224#
if NATIVE_HAS_ARM_NEON
228 template <std::
size_t N>
inline constexpr std::uint64_t mask_low_bits = [] {
229 if constexpr (N==64)
return ~std::uint64_t(0);
230 else return (std::uint64_t(1)<<N)-1;
232 template <std::
size_t B>
using mask_word = std::conditional_t<B==1,std::uint8_t,
233 std::conditional_t<B==2,std::uint16_t,std::conditional_t<B==4,std::uint32_t,std::uint64_t>>>;
235 template<
class U>
struct mask_scalar_ops {
237 static hint_inline hint_const
constexpr native_type normalize(native_type x)
noexcept {
return x?native_type(~U(0)):U(0); }
238 static hint_inline hint_const
constexpr native_type
broadcast(
bool x)
noexcept {
return normalize(U(x)); }
239 static hint_inline hint_const
constexpr native_type bit_and(native_type a,native_type b)
noexcept {
return U(a&b); }
240 static hint_inline hint_const
constexpr native_type bit_or(native_type a,native_type b)
noexcept {
return U(a|b); }
241 static hint_inline hint_const
constexpr native_type bit_xor(native_type a,native_type b)
noexcept {
return U(a^b); }
242 static hint_inline hint_const
constexpr native_type bit_not(native_type a)
noexcept {
return U(~a); }
243 static hint_inline hint_const
constexpr bool any(native_type a)
noexcept {
return a!=0; }
244 static hint_inline hint_const
constexpr bool all(native_type a)
noexcept {
return a==U(~U(0)); }
245 static hint_inline hint_const
constexpr std::uint64_t bits(native_type a)
noexcept {
return a?1:0; }
246 static hint_inline hint_const
constexpr native_type from_bits(std::uint64_t a)
noexcept {
return broadcast((a&1)!=0); }
248 template <std::
size_t N>
struct mask_compact_ops {
249 using native_type = std::conditional_t<(N<=8),std::uint8_t,std::conditional_t<(N<=16),std::uint16_t,
250 std::conditional_t<(N<=32),std::uint32_t,std::uint64_t>>>;
254 static hint_inline hint_const
constexpr native_type normalize(native_type a)
noexcept {
255 if constexpr(N==8 || N==16 || N==32 || N==64)
return a;
256 else return native_type(std::uint64_t(a)&mask_low_bits<N>);
258 static hint_inline hint_const
constexpr native_type
broadcast(
bool a)
noexcept {
return a?native_type(mask_low_bits<N>):native_type(0); }
259 static hint_inline hint_const
constexpr native_type bit_and(native_type a,native_type b)
noexcept {
260 if (std::is_constant_evaluated())
return native_type(a&b);
261#if NATIVE_HAS_AVX512F
262#if NATIVE_HAS_AVX512DQ
263 if constexpr(N<=8)
return _kand_mask8(a,b);
266 if constexpr(N<=16)
return native_type(_kand_mask16(a,b));
267#if NATIVE_HAS_AVX512BW
268 else if constexpr(N<=32)
return _kand_mask32(a,b);
269 else return _kand_mask64(a,b);
271 else return native_type(a&b);
274 return native_type(a&b);
277 static hint_inline hint_const
constexpr native_type bit_or(native_type a,native_type b)
noexcept {
278 if (std::is_constant_evaluated())
return native_type(a|b);
279#if NATIVE_HAS_AVX512F
280#if NATIVE_HAS_AVX512DQ
281 if constexpr(N<=8)
return _kor_mask8(a,b);
284 if constexpr(N<=16)
return native_type(_kor_mask16(a,b));
285#if NATIVE_HAS_AVX512BW
286 else if constexpr(N<=32)
return _kor_mask32(a,b);
287 else return _kor_mask64(a,b);
289 else return native_type(a|b);
292 return native_type(a|b);
295 static hint_inline hint_const
constexpr native_type bit_xor(native_type a,native_type b)
noexcept {
296 if (std::is_constant_evaluated())
return native_type(a^b);
297#if NATIVE_HAS_AVX512F
298#if NATIVE_HAS_AVX512DQ
299 if constexpr(N<=8)
return _kxor_mask8(a,b);
302 if constexpr(N<=16)
return native_type(_kxor_mask16(a,b));
303#if NATIVE_HAS_AVX512BW
304 else if constexpr(N<=32)
return _kxor_mask32(a,b);
305 else return _kxor_mask64(a,b);
307 else return native_type(a^b);
310 return native_type(a^b);
313 static hint_inline hint_const
constexpr native_type bit_not(native_type a)
noexcept {
314 if(std::is_constant_evaluated())
return native_type((~std::uint64_t(a))&mask_low_bits<N>);
316 if constexpr(N<8)
return bit_xor(a,native_type(mask_low_bits<N>));
317#if NATIVE_HAS_AVX512F
318#if NATIVE_HAS_AVX512DQ
319 else if constexpr(N==8)
return _knot_mask8(a);
321 else if constexpr(N<=16)
return native_type(_knot_mask16(a));
322#if NATIVE_HAS_AVX512BW
323 else if constexpr(N<=32)
return _knot_mask32(a);
324 else return _knot_mask64(a);
326 else return native_type(~a);
329 else return native_type(~a);
332 static hint_inline hint_const
constexpr bool any(native_type a)
noexcept {
return a!=0; }
333 static hint_inline hint_const
constexpr bool all(native_type a)
noexcept {
return a==native_type(mask_low_bits<N>); }
334 static hint_inline hint_const
constexpr std::uint64_t bits(native_type a)
noexcept {
return a; }
335 static hint_inline hint_const
constexpr native_type from_bits(std::uint64_t a)
noexcept {
return normalize(native_type(a)); }
337 template <std::
size_t B,std::
size_t N>
struct mask_vector_ops {
339 using native_type = std::conditional_t<B*N==16,__m128i,__m256i>;
340 static hint_inline
constexpr hint_const native_type
broadcast(
bool a)
noexcept {
342 std::array<mask_word<B>,N> lanes{};
343 lanes.fill(a?mask_word<B>(~mask_word<B>(0)):0);
344 return __builtin_bit_cast(native_type,lanes);
346 if constexpr (B*N==16)
return _mm_set1_epi32(a?-1:0);
347 else return _mm256_set1_epi32(a?-1:0);
349 static hint_inline
constexpr hint_const native_type bit_and(native_type a,native_type b)
noexcept {
351 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
352 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
353 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]&y[i]);
354 return __builtin_bit_cast(native_type,x);
356 if constexpr (B*N==16)
return _mm_and_si128(a,b);
357 else return _mm256_and_si256(a,b);
359 static hint_inline
constexpr hint_const native_type bit_or(native_type a,native_type b)
noexcept {
361 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
362 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
363 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]|y[i]);
364 return __builtin_bit_cast(native_type,x);
366 if constexpr (B*N==16)
return _mm_or_si128(a,b);
367 else return _mm256_or_si256(a,b);
369 static hint_inline
constexpr hint_const native_type bit_xor(native_type a,native_type b)
noexcept {
371 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
372 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
373 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]^y[i]);
374 return __builtin_bit_cast(native_type,x);
376 if constexpr (B*N==16)
return _mm_xor_si128(a,b);
377 else return _mm256_xor_si256(a,b);
379 static hint_inline
constexpr hint_const native_type bit_not(native_type a)
noexcept {
return bit_xor(a,
broadcast(
true)); }
380 static hint_inline
constexpr hint_const native_type normalize(native_type a)
noexcept {
382 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
383 for(
auto & lane:lanes) lane=lane?mask_word<B>(~mask_word<B>(0)):0;
384 return __builtin_bit_cast(native_type,lanes);
387 if constexpr (B*N==16) {
388 if constexpr (B==1)
return bit_not(_mm_cmpeq_epi8(a,z));
389 else if constexpr (B==2)
return bit_not(_mm_cmpeq_epi16(a,z));
390 else if constexpr (B==4)
return bit_not(_mm_cmpeq_epi32(a,z));
391 else return bit_not(_mm_cmpeq_epi64(a,z));
393 if constexpr (B==1)
return bit_not(_mm256_cmpeq_epi8(a,z));
394 else if constexpr (B==2)
return bit_not(_mm256_cmpeq_epi16(a,z));
395 else if constexpr (B==4)
return bit_not(_mm256_cmpeq_epi32(a,z));
396 else return bit_not(_mm256_cmpeq_epi64(a,z));
399 static hint_inline
constexpr hint_const
bool any(native_type a)
noexcept {
401 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
402 for(
auto lane:lanes)
if(lane!=0)
return true;
405 if constexpr (B*N==16)
return _mm_testz_si128(a,a)==0;
406 else return _mm256_testz_si256(a,a)==0;
408 static hint_inline
constexpr hint_const
bool all(native_type a)
noexcept {
410 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
411 for(
auto lane:lanes)
if(lane!=mask_word<B>(~mask_word<B>(0)))
return false;
414 if constexpr (B*N==16)
return _mm_movemask_epi8(a)==0xffff;
415 else return _mm256_movemask_epi8(a)==-1;
417 static hint_inline
constexpr hint_const std::uint64_t bits(native_type a)
noexcept {
419 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
420 std::uint64_t result=0;
421 for(std::size_t i=0;i<N;++i) result|=std::uint64_t(lanes[i]!=0)<<i;
425 if constexpr (B*N==16) bytes=std::uint32_t(_mm_movemask_epi8(a));
426 else bytes=std::uint32_t(_mm256_movemask_epi8(a));
427 std::uint64_t result=0;
428 for(std::size_t i=0;i<N;++i) result|=std::uint64_t((bytes>>(i*B))&1)<<i;
431 static hint_inline
constexpr hint_const native_type from_bits(std::uint64_t bits)
noexcept {
433 std::array<mask_word<B>,N> lanes{};
434 for(std::size_t i=0;i<N;++i) lanes[i]=((bits>>i)&1)?mask_word<B>(~mask_word<B>(0)):0;
435 return __builtin_bit_cast(native_type,lanes);
437 std::array<mask_word<B>,N> a{};
438 for(std::size_t i=0;i<N;++i) a[i]=((bits>>i)&1)?mask_word<B>(~mask_word<B>(0)):mask_word<B>(0);
439 if constexpr (B*N==16)
return _mm_loadu_si128(
reinterpret_cast<__m128i
const *
>(a.data()));
440 else return _mm256_loadu_si256(
reinterpret_cast<__m256i
const *
>(a.data()));
442#elif NATIVE_HAS_ARM_NEON
443 using native_type = uint8x16_t;
444 static hint_inline
constexpr hint_const native_type
broadcast(
bool a)
noexcept {
446 std::array<mask_word<B>,N> lanes{};
447 lanes.fill(a?mask_word<B>(~mask_word<B>(0)):0);
448 return __builtin_bit_cast(native_type,lanes);
450 return vdupq_n_u8(a?0xff:0);
452 static hint_inline
constexpr hint_const native_type bit_and(native_type a,native_type b)
noexcept {
454 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
455 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
456 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]&y[i]);
457 return __builtin_bit_cast(native_type,x);
459 return vandq_u8(a,b);
461 static hint_inline
constexpr hint_const native_type bit_or(native_type a,native_type b)
noexcept {
463 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
464 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
465 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]|y[i]);
466 return __builtin_bit_cast(native_type,x);
468 return vorrq_u8(a,b);
470 static hint_inline
constexpr hint_const native_type bit_xor(native_type a,native_type b)
noexcept {
472 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
473 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
474 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]^y[i]);
475 return __builtin_bit_cast(native_type,x);
477 return veorq_u8(a,b);
479 static hint_inline
constexpr hint_const native_type bit_not(native_type a)
noexcept {
if consteval {
return bit_xor(a,
broadcast(
true)); }
return vmvnq_u8(a); }
480 static hint_inline
constexpr hint_const native_type normalize(native_type a)
noexcept {
482 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
483 for(
auto & lane:lanes) lane=lane?mask_word<B>(~mask_word<B>(0)):0;
484 return __builtin_bit_cast(native_type,lanes);
486 if constexpr(B==1)
return bit_not(vceqq_u8(a,vdupq_n_u8(0)));
487 else if constexpr(B==2)
return bit_not(vreinterpretq_u8_u16(vceqq_u16(vreinterpretq_u16_u8(a),vdupq_n_u16(0))));
488 else if constexpr(B==4)
return bit_not(vreinterpretq_u8_u32(vceqq_u32(vreinterpretq_u32_u8(a),vdupq_n_u32(0))));
489 else return bit_not(vreinterpretq_u8_u64(vceqq_u64(vreinterpretq_u64_u8(a),vdupq_n_u64(0))));
491 static hint_inline
constexpr hint_const
bool any(native_type a)
noexcept {
493 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
494 for(
auto lane:lanes)
if(lane!=0)
return true;
497 return vmaxvq_u8(a)!=0;
499 static hint_inline
constexpr hint_const
bool all(native_type a)
noexcept {
501 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
502 for(
auto lane:lanes)
if(lane!=mask_word<B>(~mask_word<B>(0)))
return false;
505 return vminvq_u8(a)==0xff;
507 static hint_inline
constexpr hint_const std::uint64_t bits(native_type a)
noexcept {
509 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
510 std::uint64_t result=0;
511 for(std::size_t i=0;i<N;++i) result|=std::uint64_t(lanes[i]!=0)<<i;
514 std::array<std::uint8_t,16> bytes{};vst1q_u8(bytes.data(),a);
515 std::uint64_t result=0;
516 for(std::size_t i=0;i<N;++i) result|=std::uint64_t(bytes[i*B]!=0)<<i;
519 static hint_inline
constexpr hint_const native_type from_bits(std::uint64_t bits)
noexcept {
521 std::array<mask_word<B>,N> lanes{};
522 for(std::size_t i=0;i<N;++i) lanes[i]=((bits>>i)&1)?mask_word<B>(~mask_word<B>(0)):0;
523 return __builtin_bit_cast(native_type,lanes);
525 std::array<std::uint8_t,16> a{};
526 for(std::size_t i=0;i<N;++i)
for(std::size_t j=0;j<B;++j) a[i*B+j]=((bits>>i)&1)?0xff:0;
527 return vld1q_u8(a.data());
531 template<std::
size_t B,std::
size_t N>
struct mask_vector512_ops {
532#if NATIVE_HAS_AVX512F
533 using native_type=__m512i;
534 static hint_inline
constexpr hint_const native_type
broadcast(
bool a)
noexcept {
536 std::array<mask_word<B>,N> lanes{};
537 lanes.fill(a?mask_word<B>(~mask_word<B>(0)):0);
538 return __builtin_bit_cast(native_type,lanes);
540 return _mm512_set1_epi32(a?-1:0);
542 static hint_inline
constexpr hint_const native_type bit_and(native_type a,native_type b)
noexcept {
544 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
545 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
546 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]&y[i]);
547 return __builtin_bit_cast(native_type,x);
549 return _mm512_and_si512(a,b);
551 static hint_inline
constexpr hint_const native_type bit_or(native_type a,native_type b)
noexcept {
553 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
554 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
555 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]|y[i]);
556 return __builtin_bit_cast(native_type,x);
558 return _mm512_or_si512(a,b);
560 static hint_inline
constexpr hint_const native_type bit_xor(native_type a,native_type b)
noexcept {
562 auto x=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
563 auto y=__builtin_bit_cast(std::array<mask_word<B>,N>,b);
564 for(std::size_t i=0;i<N;++i) x[i]=mask_word<B>(x[i]^y[i]);
565 return __builtin_bit_cast(native_type,x);
567 return _mm512_xor_si512(a,b);
569 static hint_inline
constexpr hint_const native_type bit_not(native_type a)
noexcept {
return bit_xor(a,
broadcast(
true)); }
570 static hint_inline
constexpr hint_const std::uint64_t bits(native_type a)
noexcept {
572 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
573 std::uint64_t result=0;
574 for(std::size_t i=0;i<N;++i) result|=std::uint64_t(lanes[i]!=0)<<i;
577 if constexpr(B==1)
return _mm512_cmpneq_epi8_mask(a,_mm512_setzero_si512());
578 else if constexpr(B==2)
return _mm512_cmpneq_epi16_mask(a,_mm512_setzero_si512());
579 else if constexpr(B==4)
return _mm512_cmpneq_epi32_mask(a,_mm512_setzero_si512());
580 else return _mm512_cmpneq_epi64_mask(a,_mm512_setzero_si512());
582 static hint_inline
constexpr hint_const native_type from_bits(std::uint64_t a)
noexcept {
584 std::array<mask_word<B>,N> lanes{};
585 for(std::size_t i=0;i<N;++i) lanes[i]=((a>>i)&1)?mask_word<B>(~mask_word<B>(0)):0;
586 return __builtin_bit_cast(native_type,lanes);
588 if constexpr(B==1)
return _mm512_maskz_set1_epi8(__mmask64(a),-1);
589 else if constexpr(B==2)
return _mm512_maskz_set1_epi16(__mmask32(a),-1);
590 else if constexpr(B==4)
return _mm512_maskz_set1_epi32(__mmask16(a),-1);
591 else return _mm512_maskz_set1_epi64(__mmask8(a),-1);
593 static hint_inline
constexpr hint_const native_type normalize(native_type a)
noexcept {
595 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
596 for(
auto & lane:lanes) lane=lane?mask_word<B>(~mask_word<B>(0)):0;
597 return __builtin_bit_cast(native_type,lanes);
599 return from_bits(bits(a));
601 static hint_inline
constexpr hint_const
bool any(native_type a)
noexcept {
603 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
604 for(
auto lane:lanes)
if(lane!=0)
return true;
609 static hint_inline
constexpr hint_const
bool all(native_type a)
noexcept {
611 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
612 for(
auto lane:lanes)
if(lane!=mask_word<B>(~mask_word<B>(0)))
return false;
615 return bits(a)==mask_low_bits<N>;
619 template<std::
size_t B,std::
size_t N>
using mask_full_ops=std::conditional_t<N==1,mask_scalar_ops<mask_word<B>>,
620 std::conditional_t<B*N==64,mask_vector512_ops<B,N>,mask_vector_ops<B,N>>>;
621 template<std::
size_t N>
inline constexpr bool predicate_shape=
622#if NATIVE_HAS_AVX512F
623 (N==1 || N==2 || N==3 || N==4 || N==8 || N==16
624#if NATIVE_HAS_AVX512BW
633 template<
class U,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::mask_shape<
sizeof(U),N>
638 struct alignas(typename ::NATIVE_BACKEND_NAMESPACE::mask_full_ops<
sizeof(U),N>::native_type)
simd<mask_lane<U>,N,Arch> : detail::swizzle_access<mask_lane<U>,N,Arch> {
642 template <std::
size_t A = 1>
643 [[nodiscard]]
static hint_inline
constexpr simd load_memory(
mask_lane<U> const * p)
noexcept {
return load(p); }
645 template <std::
size_t A = 1>
646 hint_inline
constexpr void store_memory(
mask_lane<U> * p)
const noexcept { store(p); }
649 using storage_type=U;
650 using ops=::NATIVE_BACKEND_NAMESPACE::mask_full_ops<
sizeof(U),N>;
651 using native_type=
typename ops::native_type;
652 using mask_type=
simd;
653 using mask = mask_type;
655 static constexpr std::size_t lanes=N;
656 static constexpr bool compact=
false;
658 hint_inline
constexpr simd() noexcept : value_(ops::
broadcast(false)) {}
660 explicit hint_inline
constexpr simd(
bool value) noexcept : value_(ops::broadcast(value)) {}
662 hint_inline
constexpr simd(value_type value) noexcept :
simd(value.to_bool()) {}
664 template<
class... X>
requires(N>1 &&
sizeof...(X)==N) && (std::same_as<X,value_type>&&...)
665 hint_inline constexpr
simd(X... values) noexcept :
simd(std::array<value_type,N>{values...}) {}
667 explicit hint_inline
constexpr simd(std::array<value_type,N>
const & values) noexcept :
simd(load(values.data())) {}
670 [[nodiscard]]
static hint_inline hint_const
constexpr simd from_native(native_type value)
noexcept {
return simd(raw{},ops::normalize(value)); }
674 [[nodiscard]]
static hint_inline hint_const
constexpr simd unsafe_from_native(native_type value)
noexcept {
return simd(raw{},value); }
676 [[nodiscard]] hint_inline hint_pure
constexpr native_type to_native() const noexcept {
return value_; }
678 [[nodiscard]]
static hint_inline hint_const
constexpr simd from_bitset(std::uint64_t value)
noexcept {
return simd(raw{},ops::from_bits(value)); }
680 [[nodiscard]] hint_inline hint_pure
constexpr std::uint64_t to_bitset() const noexcept {
return ops::bits(value_); }
682 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load(value_type
const * p)
noexcept {
684 std::uint64_t bits=0;
685 for(std::size_t i=0;i<N;++i) bits|=std::uint64_t(p[i].
to_bool())<<i;
686 return from_bitset(bits);
688 native_type value;std::memcpy(&value,
static_cast<void const *
>(p),
sizeof(value));
return unsafe_from_native(value);
691 hint_inline
constexpr void store(value_type * p)
const noexcept {
693 auto bits=to_bitset();
694 for(std::size_t i=0;i<N;++i) p[i]=value_type(((bits>>i)&1)!=0);
697 std::memcpy(
static_cast<void *
>(p),&value_,
sizeof(value_));
700 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator~(
simd a)
noexcept {
return simd(raw{},ops::bit_not(a.value_)); }
702 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator!(
simd a)
noexcept {
return ~a; }
704 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator&(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_and(a.value_,b.value_)); }
706 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator|(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_or(a.value_,b.value_)); }
708 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator^(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_xor(a.value_,b.value_)); }
710 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator==(
simd a,
simd b)
noexcept {
return ~(a^b); }
712 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator!=(
simd a,
simd b)
noexcept {
return a^b; }
720 [[nodiscard]]
friend hint_inline hint_const
constexpr bool any(
simd a)
noexcept {
return ops::any(a.value_); }
722 [[nodiscard]]
friend hint_inline hint_const
constexpr bool all(
simd a)
noexcept {
return ops::all(a.value_); }
724 [[nodiscard]]
friend hint_inline hint_const
constexpr bool none(
simd a)
noexcept {
return !
any(a); }
729 hint_inline
constexpr simd(raw,native_type value) noexcept : value_(value) {}
737 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N>
739 static constexpr isa<> architecture=Arch;
740 using ops=::NATIVE_BACKEND_NAMESPACE::mask_compact_ops<N>;
741 using native_type=
typename ops::native_type;
742 static constexpr std::size_t lanes=N;
743 static constexpr bool compact=
true;
762 [[nodiscard]] hint_inline hint_pure
constexpr native_type
to_native() const noexcept {
return value_; }
773 [[nodiscard]] hint_inline hint_pure
constexpr std::uint64_t
to_bitset() const noexcept {
return value_; }
795 [[nodiscard]]
friend hint_inline hint_const
constexpr bool any(
predicate a)
noexcept {
return a.value_!=0; }
797 [[nodiscard]]
friend hint_inline hint_const
constexpr bool all(
predicate a)
noexcept {
return ops::all(a.value_); }
799 [[nodiscard]]
friend hint_inline hint_const
constexpr bool none(
predicate a)
noexcept {
return !
any(a); }
807 hint_inline
constexpr predicate(raw,native_type value) noexcept : value_(value) {}
808 native_type value_=0;
811 namespace detail::NATIVE_BACKEND {
812 template<
class T>
using mask_lane_for=mask_lane<mask_word<mask_lane_bytes<T>>>;
813 template<
class T,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
using comparison_mask=std::conditional_t<mask_compact<mask_lane_bytes<T>,N>,
814 predicate<N,Arch>,simd<mask_lane_for<T>,N,Arch>>;
816 namespace detail::NATIVE_BACKEND {
817 template<::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
818 requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && std::same_as<T,U> &&
requires {
typename simd<T,N,Arch>::native_type; }
819 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd(U
const * p,simd_memory<A,Access> = {})
noexcept {
return simd<T,N,Arch>::load(p); }
820 template<
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
821 requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && std::same_as<T,U> &&
requires {
typename simd<T,N,Arch>::native_type; }
822 hint_inline
constexpr void store_simd(U * p,simd<T,N,Arch> value,simd_memory<A,Access> = {})
noexcept { value.store(p); }
823 template<::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
824 requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && std::same_as<T,U> &&
requires {
typename simd<T,N,Arch>::native_type; }
825 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd_partial(U
const * p,std::size_t count,T fill=T{},simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
826 std::array<T,N> a;a.fill(fill);
for(std::size_t i=0;i<count;++i)a[i]=p[i];return simd<T,N,Arch>::load(a.data());
828 template<
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
829 requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && std::same_as<T,U> &&
requires {
typename simd<T,N,Arch>::native_type; }
830 hint_inline
constexpr void store_simd_partial(U * p,simd<T,N,Arch> value,std::size_t count,simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
831 std::array<T,N> a;value.store(a.data());
for(std::size_t i=0;i<count;++i)p[i]=a[i];
836 template<
class U,
class T,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<U> && simd_mask_element<T> &&
844 template<
class T,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N> &&
requires {
typename simd<T,N,Arch>::native_type; }
847#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
848 if constexpr(
sizeof(T)*N==16 && ::NATIVE_BACKEND_NAMESPACE::mask_compact<
sizeof(T),N>) {
849 auto x=value.to_native();
auto z=_mm_setzero_si128();
854 }
else if constexpr(
sizeof(T)*N==32 && ::NATIVE_BACKEND_NAMESPACE::mask_compact<
sizeof(T),N>) {
855 auto x=value.to_native();
auto z=_mm256_setzero_si256();
866 template<
class T,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N> &&
requires {
typename simd<T,N,Arch>::native_type; }
869#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
870 if constexpr(
sizeof(T)*N==16 && ::NATIVE_BACKEND_NAMESPACE::mask_compact<
sizeof(T),N>) {
871 auto k=value.to_native();
876 }
else if constexpr(
sizeof(T)*N==32 && ::NATIVE_BACKEND_NAMESPACE::mask_compact<
sizeof(T),N>) {
877 auto k=value.to_native();
889 namespace detail::NATIVE_BACKEND {
890 template<std::
size_t N> [[nodiscard]] hint_inline hint_const
constexpr auto bool_ones() noexcept {
892 using V=
typename mask_full_ops<1,N>::native_type;
893 std::array<std::uint8_t,N> lanes{}; lanes.fill(1);
894 return __builtin_bit_cast(V,lanes);
896 if constexpr(N==1)
return std::uint8_t(1);
898 else if constexpr(N==16)
return _mm_set1_epi8(1);
899 else if constexpr(N==32)
return _mm256_set1_epi8(1);
901#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512BW
902 else if constexpr(N==64)
return _mm512_set1_epi8(1);
904#if NATIVE_HAS_ARM_NEON
905 else return vdupq_n_u8(1);
912 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::mask_shape<1,N>
913 struct simd<bool,N,Arch> : detail::swizzle_access<bool,N,Arch> {
914 static constexpr isa<> architecture=Arch;
917 template <std::
size_t A = 1>
920 template <std::
size_t A = 1>
923 using value_type=bool;
924 using storage_type=std::uint8_t;
925 using ops=::NATIVE_BACKEND_NAMESPACE::mask_full_ops<1,N>;
926 using native_type=
typename ops::native_type;
928 using mask_type=::NATIVE_BACKEND_NAMESPACE::comparison_mask<bool,N,Arch>;
929 using mask = mask_type;
931 static constexpr std::size_t lanes=N;
935 explicit hint_inline
constexpr simd(
bool value) noexcept : value_(value?::NATIVE_BACKEND_NAMESPACE::bool_ones<N>():ops::broadcast(
false)) {}
937 template<
class... X>
requires(N>1 &&
sizeof...(X)==N) && (std::same_as<X,bool>&&...)
938 hint_inline constexpr
simd(X... value) noexcept :
simd(
std::array<
bool,N>{value...}) {}
940 explicit hint_inline
constexpr simd(std::array<bool,N>
const & value) noexcept :
simd(
load(value.data())) {}
942 [[nodiscard]]
static hint_inline hint_const
constexpr simd from_native(native_type value)
noexcept {
943 return simd(raw{},ops::bit_and(ops::normalize(value),::NATIVE_BACKEND_NAMESPACE::bool_ones<N>()));
949 [[nodiscard]] hint_inline hint_pure
constexpr native_type
to_native() const noexcept {
return value_; }
951 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load(
bool const * p)
noexcept {
952 std::array<std::uint8_t,N> bytes{};
953 for(std::size_t i=0;i<N;++i) bytes[i]=p[i]?1:0;
955 native_type value;std::memcpy(&value,bytes.data(),
sizeof(value));
return unsafe_from_native(value);
958 hint_inline
constexpr void store(
bool * p)
const noexcept {
959 std::array<std::uint8_t,N> bytes{};
960 if consteval { bytes=__builtin_bit_cast(
decltype(bytes),value_); }
961 else { std::memcpy(bytes.data(),&value_,
sizeof(value_)); }
962 for(std::size_t i=0;i<N;++i) p[i]=bytes[i]!=0;
966 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_partial(
bool const * p,std::size_t count,
bool fill=
false) noexcept hint_diagnose_if(count >
simd::lanes,
"partial SIMD count exceeds the lane count") {
967 std::array<bool,N> values;values.fill(fill);
968 for(std::size_t i=0;i<count;++i) values[i]=p[i];
969 return load(values.data());
972 hint_inline
constexpr void store_partial(
bool * p,std::size_t count)
const noexcept hint_diagnose_if(count > simd::lanes,
"partial SIMD count exceeds the lane count") {
973 std::array<bool,N> values;
store(values.data());
974 for(std::size_t i=0;i<count;++i) p[i]=values[i];
977 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator!(
simd a)
noexcept {
return simd(raw{},ops::bit_xor(a.value_,::NATIVE_BACKEND_NAMESPACE::bool_ones<N>())); }
979 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator~(
simd a)
noexcept {
return !a; }
981 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator&(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_and(a.value_,b.value_)); }
983 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator|(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_or(a.value_,b.value_)); }
985 [[nodiscard]]
friend hint_inline hint_const
constexpr simd operator^(
simd a,
simd b)
noexcept {
return simd(raw{},ops::bit_xor(a.value_,b.value_)); }
988 auto x=ops::bit_xor(a.value_,b.value_);
989 if consteval {
return mask_type::from_bitset(vector_mask_type::from_native(x).to_bitset()); }
990#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512BW
991 if constexpr(N==64)
return mask_type::from_native(_mm512_cmpneq_epi8_mask(x,_mm512_setzero_si512()));
992#if NATIVE_HAS_AVX512VL
993 else if constexpr(N==32)
return mask_type::from_native(_mm256_cmpneq_epi8_mask(x,_mm256_setzero_si256()));
994 else if constexpr(N==16)
return mask_type::from_native(_mm_cmpneq_epi8_mask(x,_mm_setzero_si128()));
998 return vector_mask_type::from_native(x);
1001 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator==(
simd a,
simd b)
noexcept {
return ~(a!=b); }
1009 [[nodiscard]]
friend hint_inline hint_const
constexpr bool any(
simd a)
noexcept {
return ops::any(a.value_); }
1011 [[nodiscard]]
friend hint_inline hint_const
constexpr bool all(
simd a)
noexcept {
return none(!a); }
1013 [[nodiscard]]
friend hint_inline hint_const
constexpr bool none(
simd a)
noexcept {
return !
any(a); }
1015 template<
class M>
requires(std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
1018 auto mask=vector_mask_type::from_bitset(m.to_bitset()).to_native();
1019 return simd(raw{},ops::bit_or(ops::bit_and(mask,a.value_),ops::bit_and(ops::bit_not(mask),b.value_)));
1021#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512BW
1022 if constexpr(M::compact) {
1023 if constexpr(N==64)
return simd(raw{},_mm512_mask_blend_epi8(m.to_native(),b.value_,a.value_));
1024#if NATIVE_HAS_AVX512VL
1025 else if constexpr(N==32)
return simd(raw{},_mm256_mask_blend_epi8(m.to_native(),b.value_,a.value_));
1026 else return simd(raw{},_mm_mask_blend_epi8(m.to_native(),b.value_,a.value_));
1030 return simd(raw{},ops::bit_or(ops::bit_and(m.to_native(),a.value_),ops::bit_and(ops::bit_not(m.to_native()),b.value_)));
1034 hint_inline
constexpr simd(raw,native_type value) noexcept : value_(value) {}
1038 namespace detail::NATIVE_BACKEND {
1039 template<::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
1040 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,bool> && std::same_as<U,bool> &&
requires {
typename simd<T,N,Arch>::native_type; }
1041 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd(U
const * p,simd_memory<A,Access> = {})
noexcept {
return simd<T,N,Arch>::load(p); }
1042 template<
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
1043 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,bool> && std::same_as<U,bool> &&
requires {
typename simd<T,N,Arch>::native_type; }
1044 hint_inline
constexpr void store_simd(U * p,simd<T,N,Arch> value,simd_memory<A,Access> = {})
noexcept { value.store(p); }
1045 template<::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
1046 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,bool> && std::same_as<U,bool> &&
requires {
typename simd<T,N,Arch>::native_type; }
1047 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd_partial(U
const * p,std::size_t count,T fill=
false,simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
1048 return simd<T,N,Arch>::load_partial(p,count,fill);
1050 template<
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
1051 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,bool> && std::same_as<U,bool> &&
requires {
typename simd<T,N,Arch>::native_type; }
1052 hint_inline
constexpr void store_simd_partial(U * p,simd<T,N,Arch> value,std::size_t count,simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") { value.store_partial(p,count); }
1062 template<
class T,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && simd_mask_element<T> &&
requires {
typename simd<bool,N,Arch>::native_type;
typename simd<T,N,Arch>::native_type; }
1072 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N> &&
requires {
typename simd<bool,N,Arch>::native_type; }
1075 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N> &&
requires {
typename simd<bool,N,Arch>::native_type; }
1078 if constexpr(
decltype(result)::compact)
return result;
1083namespace NATIVE_BACKEND_NAMESPACE {
1084 template <simd_
integer_element T> [[nodiscard]] hint_inline
constexpr auto integer_word(T value)
noexcept {
1085 return std::bit_cast<std::make_unsigned_t<T>>(value);
1087 template <simd_
integer_element T,
class U>
1088 [[nodiscard]] hint_inline
constexpr T integer_wrap(U value)
noexcept {
1089 return std::bit_cast<T>(
static_cast<std::make_unsigned_t<T>
>(value));
1091 template <simd_
integer_element T>
1092 using integer_work_word = std::conditional_t<(
sizeof(T) < 4), uint32_t, std::make_unsigned_t<T>>;
1093 template <simd_
integer_element T>
1094 [[nodiscard]] hint_inline
constexpr T integer_scalar_add(T a, T b)
noexcept {
1095 using U = integer_work_word<T>;
1096 return integer_wrap<T>(U(integer_word(a)) + U(integer_word(b)));
1098 template <simd_
integer_element T>
1099 [[nodiscard]] hint_inline
constexpr T integer_scalar_sub(T a, T b)
noexcept {
1100 using U = integer_work_word<T>;
1101 return integer_wrap<T>(U(integer_word(a)) - U(integer_word(b)));
1103 template <simd_
integer_element T>
1104 [[nodiscard]] hint_inline
constexpr T integer_scalar_mul(T a, T b)
noexcept {
1105 using U = integer_work_word<T>;
1106 return integer_wrap<T>(U(integer_word(a)) * U(integer_word(b)));
1110 template <simd_
integer_element T>
1111 [[nodiscard]] hint_inline hint_const __m128i integer_broadcast_16(T x)
noexcept {
1112 if constexpr (
sizeof(T) == 1)
1113 return _mm_set1_epi8(std::bit_cast<int8_t>(x));
1114 else if constexpr (
sizeof(T) == 2)
1115 return _mm_set1_epi16(std::bit_cast<int16_t>(x));
1116 else if constexpr (
sizeof(T) == 4)
1117 return _mm_set1_epi32(std::bit_cast<int32_t>(x));
1118 else if constexpr (
sizeof(T) == 8)
1119 return _mm_set1_epi64x(std::bit_cast<int64_t>(x));
1121 template <simd_
integer_element T>
1122 [[nodiscard]] hint_inline hint_const __m128i integer_add(__m128i a, __m128i b)
noexcept {
1123 if constexpr (
sizeof(T) == 1)
1124 return _mm_add_epi8(a, b);
1125 else if constexpr (
sizeof(T) == 2)
1126 return _mm_add_epi16(a, b);
1127 else if constexpr (
sizeof(T) == 4)
1128 return _mm_add_epi32(a, b);
1129 else if constexpr (
sizeof(T) == 8)
1130 return _mm_add_epi64(a, b);
1132 template <simd_
integer_element T>
1133 [[nodiscard]] hint_inline hint_const __m128i integer_sub(__m128i a, __m128i b)
noexcept {
1134 if constexpr (
sizeof(T) == 1)
1135 return _mm_sub_epi8(a, b);
1136 else if constexpr (
sizeof(T) == 2)
1137 return _mm_sub_epi16(a, b);
1138 else if constexpr (
sizeof(T) == 4)
1139 return _mm_sub_epi32(a, b);
1140 else if constexpr (
sizeof(T) == 8)
1141 return _mm_sub_epi64(a, b);
1143 template <simd_
integer_element T>
1144 [[nodiscard]] hint_inline hint_const __m128i integer_mul(__m128i a, __m128i b)
noexcept {
1145 if constexpr (
sizeof(T) == 1) {
1147 auto low = _mm_mullo_epi16(a, b);
1148 auto high = _mm_mullo_epi16(_mm_srli_epi16(a, 8), _mm_srli_epi16(b, 8));
1149 return _mm_or_si128(_mm_and_si128(low, _mm_set1_epi16(255)), _mm_slli_epi16(high, 8));
1150 }
else if constexpr (
sizeof(T) == 2)
1151 return _mm_mullo_epi16(a, b);
1152 if constexpr (
sizeof(T) == 4)
1153 return _mm_mullo_epi32(a, b);
1154 else if constexpr (
sizeof(T) == 8) {
1155#if NATIVE_HAS_AVX512DQ && NATIVE_HAS_AVX512VL
1156 return _mm_mullo_epi64(a, b);
1160 _mm_add_epi64(_mm_mul_epu32(a, _mm_srli_epi64(b, 32)), _mm_mul_epu32(_mm_srli_epi64(a, 32), b));
1161 return _mm_add_epi64(_mm_mul_epu32(a, b), _mm_slli_epi64(cross, 32));
1165 template <simd_
integer_element T,
unsigned S>
1166 requires(S <
sizeof(T) * 8)
1167 [[nodiscard]] hint_inline hint_const __m128i integer_left(__m128i a)
noexcept {
1168 if constexpr (S == 0)
1171 if constexpr (
sizeof(T) == 1)
1172 return _mm_and_si128(_mm_slli_epi16(a, S),
1173 _mm_set1_epi8(std::bit_cast<int8_t>(uint8_t((255u << S) & 255u))));
1174 else if constexpr (
sizeof(T) == 2)
1175 return _mm_slli_epi16(a, S);
1176 if constexpr (
sizeof(T) == 4)
1177 return _mm_slli_epi32(a, S);
1178 else if constexpr (
sizeof(T) == 8)
1179 return _mm_slli_epi64(a, S);
1182 template <simd_
integer_element T,
unsigned S>
1183 requires(S <
sizeof(T) * 8)
1184 [[nodiscard]] hint_inline hint_const __m128i integer_right(__m128i a)
noexcept {
1185 if constexpr (S == 0)
1188 if constexpr (
sizeof(T) == 1) {
1189 auto low = _mm_and_si128(_mm_srli_epi16(a, S), _mm_set1_epi8(int8_t(255u >> S)));
1190 if constexpr (std::is_unsigned_v<T>)
1193 auto negative = _mm_cmpgt_epi8(_mm_setzero_si128(), a);
1194 return _mm_or_si128(
1195 low, _mm_and_si128(negative, _mm_set1_epi8(std::bit_cast<int8_t>(uint8_t(255u ^ (255u >> S))))));
1197 }
else if constexpr (
sizeof(T) == 2) {
1198 if constexpr (std::is_signed_v<T>)
1199 return _mm_srai_epi16(a, S);
1201 return _mm_srli_epi16(a, S);
1203 if constexpr (
sizeof(T) == 4) {
1204 if constexpr (std::is_signed_v<T>)
1205 return _mm_srai_epi32(a, S);
1207 return _mm_srli_epi32(a, S);
1208 }
else if constexpr (
sizeof(T) == 8) {
1209 if constexpr (std::is_unsigned_v<T>)
1210 return _mm_srli_epi64(a, S);
1212#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
1213 return _mm_srai_epi64(a, S);
1215 auto negative = _mm_cmpgt_epi64(_mm_setzero_si128(), a);
1216 return _mm_or_si128(
1217 _mm_srli_epi64(a, S),
1218 _mm_and_si128(negative, _mm_set1_epi64x(std::bit_cast<int64_t>(~uint64_t(0) << (64 - S)))));
1227 template <simd_
integer_element T>
1228 [[nodiscard]] hint_inline hint_const __m256i integer_broadcast_32(T x)
noexcept {
1229 if constexpr (
sizeof(T) == 1)
1230 return _mm256_set1_epi8(std::bit_cast<int8_t>(x));
1231 else if constexpr (
sizeof(T) == 2)
1232 return _mm256_set1_epi16(std::bit_cast<int16_t>(x));
1233 else if constexpr (
sizeof(T) == 4)
1234 return _mm256_set1_epi32(std::bit_cast<int32_t>(x));
1235 else if constexpr (
sizeof(T) == 8)
1236 return _mm256_set1_epi64x(std::bit_cast<int64_t>(x));
1238 template <simd_
integer_element T>
1239 [[nodiscard]] hint_inline hint_const __m256i integer_add(__m256i a, __m256i b)
noexcept {
1240 if constexpr (
sizeof(T) == 1)
1241 return _mm256_add_epi8(a, b);
1242 else if constexpr (
sizeof(T) == 2)
1243 return _mm256_add_epi16(a, b);
1244 else if constexpr (
sizeof(T) == 4)
1245 return _mm256_add_epi32(a, b);
1246 else if constexpr (
sizeof(T) == 8)
1247 return _mm256_add_epi64(a, b);
1249 template <simd_
integer_element T>
1250 [[nodiscard]] hint_inline hint_const __m256i integer_sub(__m256i a, __m256i b)
noexcept {
1251 if constexpr (
sizeof(T) == 1)
1252 return _mm256_sub_epi8(a, b);
1253 else if constexpr (
sizeof(T) == 2)
1254 return _mm256_sub_epi16(a, b);
1255 else if constexpr (
sizeof(T) == 4)
1256 return _mm256_sub_epi32(a, b);
1257 else if constexpr (
sizeof(T) == 8)
1258 return _mm256_sub_epi64(a, b);
1260 template <simd_
integer_element T>
1261 [[nodiscard]] hint_inline hint_const __m256i integer_mul(__m256i a, __m256i b)
noexcept {
1262 if constexpr (
sizeof(T) == 1) {
1264 auto low = _mm256_mullo_epi16(a, b);
1265 auto high = _mm256_mullo_epi16(_mm256_srli_epi16(a, 8), _mm256_srli_epi16(b, 8));
1266 return _mm256_or_si256(_mm256_and_si256(low, _mm256_set1_epi16(255)), _mm256_slli_epi16(high, 8));
1267 }
else if constexpr (
sizeof(T) == 2)
1268 return _mm256_mullo_epi16(a, b);
1269 if constexpr (
sizeof(T) == 4)
1270 return _mm256_mullo_epi32(a, b);
1271 else if constexpr (
sizeof(T) == 8) {
1272#if NATIVE_HAS_AVX512DQ && NATIVE_HAS_AVX512VL
1273 return _mm256_mullo_epi64(a, b);
1276 auto cross = _mm256_add_epi64(_mm256_mul_epu32(a, _mm256_srli_epi64(b, 32)),
1277 _mm256_mul_epu32(_mm256_srli_epi64(a, 32), b));
1278 return _mm256_add_epi64(_mm256_mul_epu32(a, b), _mm256_slli_epi64(cross, 32));
1282 template <simd_
integer_element T,
unsigned S>
1283 requires(S <
sizeof(T) * 8)
1284 [[nodiscard]] hint_inline hint_const __m256i integer_left(__m256i a)
noexcept {
1285 if constexpr (S == 0)
1288 if constexpr (
sizeof(T) == 1)
1289 return _mm256_and_si256(_mm256_slli_epi16(a, S),
1290 _mm256_set1_epi8(std::bit_cast<int8_t>(uint8_t((255u << S) & 255u))));
1291 else if constexpr (
sizeof(T) == 2)
1292 return _mm256_slli_epi16(a, S);
1293 if constexpr (
sizeof(T) == 4)
1294 return _mm256_slli_epi32(a, S);
1295 else if constexpr (
sizeof(T) == 8)
1296 return _mm256_slli_epi64(a, S);
1299 [[nodiscard]] hint_inline hint_const __m256i integer_shift_left_variable(__m256i a,__m256i counts)
noexcept {
1300 return _mm256_sllv_epi32(a,counts);
1302 template <simd_
integer_element T,
unsigned S>
1303 requires(S <
sizeof(T) * 8)
1304 [[nodiscard]] hint_inline hint_const __m256i integer_right(__m256i a)
noexcept {
1305 if constexpr (S == 0)
1308 if constexpr (
sizeof(T) == 1) {
1309 auto low = _mm256_and_si256(_mm256_srli_epi16(a, S), _mm256_set1_epi8(int8_t(255u >> S)));
1310 if constexpr (std::is_unsigned_v<T>)
1313 auto negative = _mm256_cmpgt_epi8(_mm256_setzero_si256(), a);
1314 return _mm256_or_si256(
1316 _mm256_and_si256(negative, _mm256_set1_epi8(std::bit_cast<int8_t>(uint8_t(255u ^ (255u >> S))))));
1318 }
else if constexpr (
sizeof(T) == 2) {
1319 if constexpr (std::is_signed_v<T>)
1320 return _mm256_srai_epi16(a, S);
1322 return _mm256_srli_epi16(a, S);
1324 if constexpr (
sizeof(T) == 4) {
1325 if constexpr (std::is_signed_v<T>)
1326 return _mm256_srai_epi32(a, S);
1328 return _mm256_srli_epi32(a, S);
1329 }
else if constexpr (
sizeof(T) == 8) {
1330 if constexpr (std::is_unsigned_v<T>)
1331 return _mm256_srli_epi64(a, S);
1333#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
1334 return _mm256_srai_epi64(a, S);
1336 auto negative = _mm256_cmpgt_epi64(_mm256_setzero_si256(), a);
1337 return _mm256_or_si256(
1338 _mm256_srli_epi64(a, S),
1339 _mm256_and_si256(negative, _mm256_set1_epi64x(std::bit_cast<int64_t>(~uint64_t(0) << (64 - S)))));
1347#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
1348 template <simd_
integer_element T>
1349 [[nodiscard]] hint_inline hint_const __m512i integer_broadcast_64(T x)
noexcept {
1350#if NATIVE_HAS_AVX512BW
1351 if constexpr (
sizeof(T) == 1)
1352 return _mm512_set1_epi8(std::bit_cast<int8_t>(x));
1353 else if constexpr (
sizeof(T) == 2)
1354 return _mm512_set1_epi16(std::bit_cast<int16_t>(x));
1356 if constexpr (
sizeof(T) == 4)
1357 return _mm512_set1_epi32(std::bit_cast<int32_t>(x));
1358 else if constexpr (
sizeof(T) == 8)
1359 return _mm512_set1_epi64(std::bit_cast<int64_t>(x));
1361 template <simd_
integer_element T>
1362 [[nodiscard]] hint_inline hint_const __m512i integer_add(__m512i a, __m512i b)
noexcept {
1363#if NATIVE_HAS_AVX512BW
1364 if constexpr (
sizeof(T) == 1)
1365 return _mm512_add_epi8(a, b);
1366 else if constexpr (
sizeof(T) == 2)
1367 return _mm512_add_epi16(a, b);
1369 if constexpr (
sizeof(T) == 4)
1370 return _mm512_add_epi32(a, b);
1371 else if constexpr (
sizeof(T) == 8)
1372 return _mm512_add_epi64(a, b);
1374 template <simd_
integer_element T>
1375 [[nodiscard]] hint_inline hint_const __m512i integer_sub(__m512i a, __m512i b)
noexcept {
1376#if NATIVE_HAS_AVX512BW
1377 if constexpr (
sizeof(T) == 1)
1378 return _mm512_sub_epi8(a, b);
1379 else if constexpr (
sizeof(T) == 2)
1380 return _mm512_sub_epi16(a, b);
1382 if constexpr (
sizeof(T) == 4)
1383 return _mm512_sub_epi32(a, b);
1384 else if constexpr (
sizeof(T) == 8)
1385 return _mm512_sub_epi64(a, b);
1387 template <simd_
integer_element T>
1388 [[nodiscard]] hint_inline hint_const __m512i integer_mul(__m512i a, __m512i b)
noexcept {
1389#if NATIVE_HAS_AVX512BW
1390 if constexpr (
sizeof(T) == 1) {
1392 auto low = _mm512_mullo_epi16(a, b);
1393 auto high = _mm512_mullo_epi16(_mm512_srli_epi16(a, 8), _mm512_srli_epi16(b, 8));
1394 return _mm512_or_si512(_mm512_and_si512(low, _mm512_set1_epi16(255)), _mm512_slli_epi16(high, 8));
1395 }
else if constexpr (
sizeof(T) == 2)
1396 return _mm512_mullo_epi16(a, b);
1398 if constexpr (
sizeof(T) == 4)
1399 return _mm512_mullo_epi32(a, b);
1400 else if constexpr (
sizeof(T) == 8) {
1401 return _mm512_mullo_epi64(a, b);
1404 template <simd_
integer_element T,
unsigned S>
1405 requires(S <
sizeof(T) * 8)
1406 [[nodiscard]] hint_inline hint_const __m512i integer_left(__m512i a)
noexcept {
1407 if constexpr (S == 0)
1410#if NATIVE_HAS_AVX512BW
1411 if constexpr (
sizeof(T) == 1)
1412 return _mm512_and_si512(_mm512_slli_epi16(a, S),
1413 _mm512_set1_epi8(std::bit_cast<int8_t>(uint8_t((255u << S) & 255u))));
1414 else if constexpr (
sizeof(T) == 2)
1415 return _mm512_slli_epi16(a, S);
1417 if constexpr (
sizeof(T) == 4)
1418 return _mm512_slli_epi32(a, S);
1419 else if constexpr (
sizeof(T) == 8)
1420 return _mm512_slli_epi64(a, S);
1423 [[nodiscard]] hint_inline hint_const __m512i integer_shift_left_variable(__m512i a,__m512i counts)
noexcept {
1424 return _mm512_sllv_epi32(a,counts);
1426 template <simd_
integer_element T,
unsigned S>
1427 requires(S <
sizeof(T) * 8)
1428 [[nodiscard]] hint_inline hint_const __m512i integer_right(__m512i a)
noexcept {
1429 if constexpr (S == 0)
1432#if NATIVE_HAS_AVX512BW
1433 if constexpr (
sizeof(T) == 1) {
1434 auto low = _mm512_and_si512(_mm512_srli_epi16(a, S), _mm512_set1_epi8(int8_t(255u >> S)));
1435 if constexpr (std::is_unsigned_v<T>)
1438 auto negative = _mm512_movm_epi8(_mm512_cmp_epi8_mask(a, _mm512_setzero_si512(), _MM_CMPINT_LT));
1439 return _mm512_or_si512(
1441 _mm512_and_si512(negative, _mm512_set1_epi8(std::bit_cast<int8_t>(uint8_t(255u ^ (255u >> S))))));
1443 }
else if constexpr (
sizeof(T) == 2) {
1444 if constexpr (std::is_signed_v<T>)
1445 return _mm512_srai_epi16(a, S);
1447 return _mm512_srli_epi16(a, S);
1450 if constexpr (
sizeof(T) == 4) {
1451 if constexpr (std::is_signed_v<T>)
1452 return _mm512_srai_epi32(a, S);
1454 return _mm512_srli_epi32(a, S);
1455 }
else if constexpr (
sizeof(T) == 8) {
1456 if constexpr (std::is_unsigned_v<T>)
1457 return _mm512_srli_epi64(a, S);
1459 return _mm512_srai_epi64(a, S);
1466 [[nodiscard]] hint_inline hint_const __m128i integer_and(__m128i a, __m128i b)
noexcept {
1467 return _mm_and_si128(a, b);
1469 [[nodiscard]] hint_inline hint_const __m128i integer_or(__m128i a, __m128i b)
noexcept {
1470 return _mm_or_si128(a, b);
1472 [[nodiscard]] hint_inline hint_const __m128i integer_xor(__m128i a, __m128i b)
noexcept {
1473 return _mm_xor_si128(a, b);
1475 template <simd_
integer_element T,
bool Greater>
1476 [[nodiscard]] hint_inline hint_const
auto integer_compare(__m128i a, __m128i b)
noexcept {
1477 if constexpr (mask_compact<
sizeof(T), 16 /
sizeof(T)>) {
1478 if constexpr (
sizeof(T) == 1) {
1479 if constexpr (Greater) {
1480 if constexpr (std::is_signed_v<T>)
1481 return _mm_cmp_epi8_mask(a, b, _MM_CMPINT_GT);
1483 return _mm_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1485 return _mm_cmp_epi8_mask(a, b, _MM_CMPINT_EQ);
1486 }
else if constexpr (
sizeof(T) == 2) {
1487 if constexpr (Greater) {
1488 if constexpr (std::is_signed_v<T>)
1489 return _mm_cmp_epi16_mask(a, b, _MM_CMPINT_GT);
1491 return _mm_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1493 return _mm_cmp_epi16_mask(a, b, _MM_CMPINT_EQ);
1494 }
else if constexpr (
sizeof(T) == 4) {
1495 if constexpr (Greater) {
1496 if constexpr (std::is_signed_v<T>)
1497 return _mm_cmp_epi32_mask(a, b, _MM_CMPINT_GT);
1499 return _mm_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1501 return _mm_cmp_epi32_mask(a, b, _MM_CMPINT_EQ);
1502 }
else if constexpr (
sizeof(T) == 8) {
1503 if constexpr (Greater) {
1504 if constexpr (std::is_signed_v<T>)
1505 return _mm_cmp_epi64_mask(a, b, _MM_CMPINT_GT);
1507 return _mm_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1509 return _mm_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1512 if constexpr (
sizeof(T) == 1) {
1513 if constexpr (!Greater)
1514 return _mm_cmpeq_epi8(a, b);
1515 else if constexpr (std::is_signed_v<T>)
1516 return _mm_cmpgt_epi8(a, b);
1518 auto bias = _mm_set1_epi8(int8_t(-128));
1519 return _mm_cmpgt_epi8(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1521 }
else if constexpr (
sizeof(T) == 2) {
1522 if constexpr (!Greater)
1523 return _mm_cmpeq_epi16(a, b);
1524 else if constexpr (std::is_signed_v<T>)
1525 return _mm_cmpgt_epi16(a, b);
1527 auto bias = _mm_set1_epi16(int16_t(-32768));
1528 return _mm_cmpgt_epi16(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1530 }
else if constexpr (
sizeof(T) == 4) {
1531 if constexpr (!Greater)
1532 return _mm_cmpeq_epi32(a, b);
1533 else if constexpr (std::is_signed_v<T>)
1534 return _mm_cmpgt_epi32(a, b);
1536 auto bias = _mm_set1_epi32(std::bit_cast<int32_t>(uint32_t(0x80000000u)));
1537 return _mm_cmpgt_epi32(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1539 }
else if constexpr (
sizeof(T) == 8) {
1540 if constexpr (!Greater)
1541 return _mm_cmpeq_epi64(a, b);
1542 else if constexpr (std::is_signed_v<T>)
1543 return _mm_cmpgt_epi64(a, b);
1545 auto bias = _mm_set1_epi64x(std::bit_cast<int64_t>(uint64_t(1) << 63));
1546 return _mm_cmpgt_epi64(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1551 template <simd_
integer_element T,
class M>
1552 [[nodiscard]] hint_inline hint_const __m128i integer_select(M m, __m128i a, __m128i b)
noexcept {
1553 if constexpr (M::compact) {
1554 if constexpr (
sizeof(T) == 1)
1555 return _mm_mask_blend_epi8(m.to_native(), b, a);
1556 else if constexpr (
sizeof(T) == 2)
1557 return _mm_mask_blend_epi16(m.to_native(), b, a);
1558 else if constexpr (
sizeof(T) == 4)
1559 return _mm_mask_blend_epi32(m.to_native(), b, a);
1560 else if constexpr (
sizeof(T) == 8)
1561 return _mm_mask_blend_epi64(m.to_native(), b, a);
1563 return _mm_or_si128(_mm_and_si128(m.to_native(), a), _mm_andnot_si128(m.to_native(), b));
1565 template <simd_
integer_element T,
class M>
1566 [[nodiscard]] hint_inline hint_const __m128i integer_masked_add(M m, __m128i prior, __m128i a,
1567 __m128i b)
noexcept {
1568 if constexpr (M::compact) {
1569 if constexpr (
sizeof(T) == 1)
1570 return _mm_mask_add_epi8(prior, m.to_native(), a, b);
1571 else if constexpr (
sizeof(T) == 2)
1572 return _mm_mask_add_epi16(prior, m.to_native(), a, b);
1573 else if constexpr (
sizeof(T) == 4)
1574 return _mm_mask_add_epi32(prior, m.to_native(), a, b);
1575 else if constexpr (
sizeof(T) == 8)
1576 return _mm_mask_add_epi64(prior, m.to_native(), a, b);
1578 return integer_select<T>(m, integer_add<T>(a, b), prior);
1580 template <simd_
integer_element T,
class M>
1581 [[nodiscard]] hint_inline hint_const __m128i integer_masked_sub(M m, __m128i prior, __m128i a,
1582 __m128i b)
noexcept {
1583 if constexpr (M::compact) {
1584 if constexpr (
sizeof(T) == 1)
1585 return _mm_mask_sub_epi8(prior, m.to_native(), a, b);
1586 else if constexpr (
sizeof(T) == 2)
1587 return _mm_mask_sub_epi16(prior, m.to_native(), a, b);
1588 else if constexpr (
sizeof(T) == 4)
1589 return _mm_mask_sub_epi32(prior, m.to_native(), a, b);
1590 else if constexpr (
sizeof(T) == 8)
1591 return _mm_mask_sub_epi64(prior, m.to_native(), a, b);
1593 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1598 [[nodiscard]] hint_inline hint_const __m256i integer_and(__m256i a, __m256i b)
noexcept {
1599 return _mm256_and_si256(a, b);
1601 [[nodiscard]] hint_inline hint_const __m256i integer_or(__m256i a, __m256i b)
noexcept {
1602 return _mm256_or_si256(a, b);
1604 [[nodiscard]] hint_inline hint_const __m256i integer_xor(__m256i a, __m256i b)
noexcept {
1605 return _mm256_xor_si256(a, b);
1607 template <simd_
integer_element T,
bool Greater>
1608 [[nodiscard]] hint_inline hint_const
auto integer_compare(__m256i a, __m256i b)
noexcept {
1609 if constexpr (mask_compact<
sizeof(T), 32 /
sizeof(T)>) {
1610 if constexpr (
sizeof(T) == 1) {
1611 if constexpr (Greater) {
1612 if constexpr (std::is_signed_v<T>)
1613 return _mm256_cmp_epi8_mask(a, b, _MM_CMPINT_GT);
1615 return _mm256_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1617 return _mm256_cmp_epi8_mask(a, b, _MM_CMPINT_EQ);
1618 }
else if constexpr (
sizeof(T) == 2) {
1619 if constexpr (Greater) {
1620 if constexpr (std::is_signed_v<T>)
1621 return _mm256_cmp_epi16_mask(a, b, _MM_CMPINT_GT);
1623 return _mm256_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1625 return _mm256_cmp_epi16_mask(a, b, _MM_CMPINT_EQ);
1626 }
else if constexpr (
sizeof(T) == 4) {
1627 if constexpr (Greater) {
1628 if constexpr (std::is_signed_v<T>)
1629 return _mm256_cmp_epi32_mask(a, b, _MM_CMPINT_GT);
1631 return _mm256_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1633 return _mm256_cmp_epi32_mask(a, b, _MM_CMPINT_EQ);
1634 }
else if constexpr (
sizeof(T) == 8) {
1635 if constexpr (Greater) {
1636 if constexpr (std::is_signed_v<T>)
1637 return _mm256_cmp_epi64_mask(a, b, _MM_CMPINT_GT);
1639 return _mm256_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1641 return _mm256_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1644 if constexpr (
sizeof(T) == 1) {
1645 if constexpr (!Greater)
1646 return _mm256_cmpeq_epi8(a, b);
1647 else if constexpr (std::is_signed_v<T>)
1648 return _mm256_cmpgt_epi8(a, b);
1650 auto bias = _mm256_set1_epi8(int8_t(-128));
1651 return _mm256_cmpgt_epi8(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1653 }
else if constexpr (
sizeof(T) == 2) {
1654 if constexpr (!Greater)
1655 return _mm256_cmpeq_epi16(a, b);
1656 else if constexpr (std::is_signed_v<T>)
1657 return _mm256_cmpgt_epi16(a, b);
1659 auto bias = _mm256_set1_epi16(int16_t(-32768));
1660 return _mm256_cmpgt_epi16(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1662 }
else if constexpr (
sizeof(T) == 4) {
1663 if constexpr (!Greater)
1664 return _mm256_cmpeq_epi32(a, b);
1665 else if constexpr (std::is_signed_v<T>)
1666 return _mm256_cmpgt_epi32(a, b);
1668 auto bias = _mm256_set1_epi32(std::bit_cast<int32_t>(uint32_t(0x80000000u)));
1669 return _mm256_cmpgt_epi32(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1671 }
else if constexpr (
sizeof(T) == 8) {
1672 if constexpr (!Greater)
1673 return _mm256_cmpeq_epi64(a, b);
1674 else if constexpr (std::is_signed_v<T>)
1675 return _mm256_cmpgt_epi64(a, b);
1677 auto bias = _mm256_set1_epi64x(std::bit_cast<int64_t>(uint64_t(1) << 63));
1678 return _mm256_cmpgt_epi64(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1683 template <simd_
integer_element T,
class M>
1684 [[nodiscard]] hint_inline hint_const __m256i integer_select(M m, __m256i a, __m256i b)
noexcept {
1685 if constexpr (M::compact) {
1686 if constexpr (
sizeof(T) == 1)
1687 return _mm256_mask_blend_epi8(m.to_native(), b, a);
1688 else if constexpr (
sizeof(T) == 2)
1689 return _mm256_mask_blend_epi16(m.to_native(), b, a);
1690 else if constexpr (
sizeof(T) == 4)
1691 return _mm256_mask_blend_epi32(m.to_native(), b, a);
1692 else if constexpr (
sizeof(T) == 8)
1693 return _mm256_mask_blend_epi64(m.to_native(), b, a);
1695 return _mm256_or_si256(_mm256_and_si256(m.to_native(), a), _mm256_andnot_si256(m.to_native(), b));
1697 template <simd_
integer_element T,
class M>
1698 [[nodiscard]] hint_inline hint_const __m256i integer_masked_add(M m, __m256i prior, __m256i a,
1699 __m256i b)
noexcept {
1700 if constexpr (M::compact) {
1701 if constexpr (
sizeof(T) == 1)
1702 return _mm256_mask_add_epi8(prior, m.to_native(), a, b);
1703 else if constexpr (
sizeof(T) == 2)
1704 return _mm256_mask_add_epi16(prior, m.to_native(), a, b);
1705 else if constexpr (
sizeof(T) == 4)
1706 return _mm256_mask_add_epi32(prior, m.to_native(), a, b);
1707 else if constexpr (
sizeof(T) == 8)
1708 return _mm256_mask_add_epi64(prior, m.to_native(), a, b);
1710 return integer_select<T>(m, integer_add<T>(a, b), prior);
1712 template <simd_
integer_element T,
class M>
1713 [[nodiscard]] hint_inline hint_const __m256i integer_masked_sub(M m, __m256i prior, __m256i a,
1714 __m256i b)
noexcept {
1715 if constexpr (M::compact) {
1716 if constexpr (
sizeof(T) == 1)
1717 return _mm256_mask_sub_epi8(prior, m.to_native(), a, b);
1718 else if constexpr (
sizeof(T) == 2)
1719 return _mm256_mask_sub_epi16(prior, m.to_native(), a, b);
1720 else if constexpr (
sizeof(T) == 4)
1721 return _mm256_mask_sub_epi32(prior, m.to_native(), a, b);
1722 else if constexpr (
sizeof(T) == 8)
1723 return _mm256_mask_sub_epi64(prior, m.to_native(), a, b);
1725 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1729#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
1730 [[nodiscard]] hint_inline hint_const __m512i integer_and(__m512i a, __m512i b)
noexcept {
1731 return _mm512_and_si512(a, b);
1733 [[nodiscard]] hint_inline hint_const __m512i integer_or(__m512i a, __m512i b)
noexcept {
1734 return _mm512_or_si512(a, b);
1736 [[nodiscard]] hint_inline hint_const __m512i integer_xor(__m512i a, __m512i b)
noexcept {
1737 return _mm512_xor_si512(a, b);
1739 template <simd_
integer_element T,
bool Greater>
1740 [[nodiscard]] hint_inline hint_const
auto integer_compare(__m512i a, __m512i b)
noexcept {
1741 if constexpr (mask_compact<
sizeof(T), 64 /
sizeof(T)>) {
1742#if NATIVE_HAS_AVX512BW
1743 if constexpr (
sizeof(T) == 1) {
1744 if constexpr (Greater) {
1745 if constexpr (std::is_signed_v<T>)
1746 return _mm512_cmp_epi8_mask(a, b, _MM_CMPINT_GT);
1748 return _mm512_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1750 return _mm512_cmp_epi8_mask(a, b, _MM_CMPINT_EQ);
1751 }
else if constexpr (
sizeof(T) == 2) {
1752 if constexpr (Greater) {
1753 if constexpr (std::is_signed_v<T>)
1754 return _mm512_cmp_epi16_mask(a, b, _MM_CMPINT_GT);
1756 return _mm512_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1758 return _mm512_cmp_epi16_mask(a, b, _MM_CMPINT_EQ);
1761 if constexpr (
sizeof(T) == 4) {
1762 if constexpr (Greater) {
1763 if constexpr (std::is_signed_v<T>)
1764 return _mm512_cmp_epi32_mask(a, b, _MM_CMPINT_GT);
1766 return _mm512_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1768 return _mm512_cmp_epi32_mask(a, b, _MM_CMPINT_EQ);
1769 }
else if constexpr (
sizeof(T) == 8) {
1770 if constexpr (Greater) {
1771 if constexpr (std::is_signed_v<T>)
1772 return _mm512_cmp_epi64_mask(a, b, _MM_CMPINT_GT);
1774 return _mm512_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1776 return _mm512_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1779 static_assert(
sizeof(T) == 0,
"512-bit integer comparisons require native predicates");
1782 template <simd_
integer_element T,
class M>
1783 [[nodiscard]] hint_inline hint_const __m512i integer_select(M m, __m512i a, __m512i b)
noexcept {
1784 if constexpr (M::compact) {
1785#if NATIVE_HAS_AVX512BW
1786 if constexpr (
sizeof(T) == 1)
1787 return _mm512_mask_blend_epi8(m.to_native(), b, a);
1788 else if constexpr (
sizeof(T) == 2)
1789 return _mm512_mask_blend_epi16(m.to_native(), b, a);
1791 if constexpr (
sizeof(T) == 4)
1792 return _mm512_mask_blend_epi32(m.to_native(), b, a);
1793 else if constexpr (
sizeof(T) == 8)
1794 return _mm512_mask_blend_epi64(m.to_native(), b, a);
1796 return _mm512_or_si512(_mm512_and_si512(m.to_native(), a), _mm512_andnot_si512(m.to_native(), b));
1798 template <simd_
integer_element T,
class M>
1799 [[nodiscard]] hint_inline hint_const __m512i integer_masked_add(M m, __m512i prior, __m512i a,
1800 __m512i b)
noexcept {
1801 if constexpr (M::compact) {
1802#if NATIVE_HAS_AVX512BW
1803 if constexpr (
sizeof(T) == 1)
1804 return _mm512_mask_add_epi8(prior, m.to_native(), a, b);
1805 else if constexpr (
sizeof(T) == 2)
1806 return _mm512_mask_add_epi16(prior, m.to_native(), a, b);
1808 if constexpr (
sizeof(T) == 4)
1809 return _mm512_mask_add_epi32(prior, m.to_native(), a, b);
1810 else if constexpr (
sizeof(T) == 8)
1811 return _mm512_mask_add_epi64(prior, m.to_native(), a, b);
1813 return integer_select<T>(m, integer_add<T>(a, b), prior);
1815 template <simd_
integer_element T,
class M>
1816 [[nodiscard]] hint_inline hint_const __m512i integer_masked_sub(M m, __m512i prior, __m512i a,
1817 __m512i b)
noexcept {
1818 if constexpr (M::compact) {
1819#if NATIVE_HAS_AVX512BW
1820 if constexpr (
sizeof(T) == 1)
1821 return _mm512_mask_sub_epi8(prior, m.to_native(), a, b);
1822 else if constexpr (
sizeof(T) == 2)
1823 return _mm512_mask_sub_epi16(prior, m.to_native(), a, b);
1825 if constexpr (
sizeof(T) == 4)
1826 return _mm512_mask_sub_epi32(prior, m.to_native(), a, b);
1827 else if constexpr (
sizeof(T) == 8)
1828 return _mm512_mask_sub_epi64(prior, m.to_native(), a, b);
1830 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1834#if NATIVE_HAS_ARM_NEON
1835 [[nodiscard]] hint_inline hint_const uint8x16_t integer_and(uint8x16_t a, uint8x16_t b)
noexcept {
1836 return vandq_u8(a, b);
1838 [[nodiscard]] hint_inline hint_const uint8x16_t integer_or(uint8x16_t a, uint8x16_t b)
noexcept {
1839 return vorrq_u8(a, b);
1841 [[nodiscard]] hint_inline hint_const uint8x16_t integer_xor(uint8x16_t a, uint8x16_t b)
noexcept {
1842 return veorq_u8(a, b);
1844 template <simd_
integer_element T>
1845 [[nodiscard]] hint_inline hint_const uint8x16_t integer_add(uint8x16_t a, uint8x16_t b)
noexcept {
1846 if constexpr (
sizeof(T) == 1) {
1847 return vaddq_u8(a, b);
1848 }
else if constexpr (
sizeof(T) == 2) {
1849 return vreinterpretq_u8_u16(vaddq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b)));
1850 }
else if constexpr (
sizeof(T) == 4) {
1851 return vreinterpretq_u8_u32(vaddq_u32(vreinterpretq_u32_u8(a), vreinterpretq_u32_u8(b)));
1852 }
else if constexpr (
sizeof(T) == 8) {
1853 return vreinterpretq_u8_u64(vaddq_u64(vreinterpretq_u64_u8(a), vreinterpretq_u64_u8(b)));
1856 template <simd_
integer_element T>
1857 [[nodiscard]] hint_inline hint_const uint8x16_t integer_sub(uint8x16_t a, uint8x16_t b)
noexcept {
1858 if constexpr (
sizeof(T) == 1) {
1859 return vsubq_u8(a, b);
1860 }
else if constexpr (
sizeof(T) == 2) {
1861 return vreinterpretq_u8_u16(vsubq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b)));
1862 }
else if constexpr (
sizeof(T) == 4) {
1863 return vreinterpretq_u8_u32(vsubq_u32(vreinterpretq_u32_u8(a), vreinterpretq_u32_u8(b)));
1864 }
else if constexpr (
sizeof(T) == 8) {
1865 return vreinterpretq_u8_u64(vsubq_u64(vreinterpretq_u64_u8(a), vreinterpretq_u64_u8(b)));
1868 template <simd_
integer_element T>
1869 [[nodiscard]] hint_inline hint_const uint8x16_t integer_mul(uint8x16_t a, uint8x16_t b)
noexcept {
1870 if constexpr (
sizeof(T) == 1) {
1871 return vmulq_u8(a, b);
1872 }
else if constexpr (
sizeof(T) == 2) {
1873 return vreinterpretq_u8_u16(vmulq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b)));
1874 }
else if constexpr (
sizeof(T) == 4) {
1875 return vreinterpretq_u8_u32(vmulq_u32(vreinterpretq_u32_u8(a), vreinterpretq_u32_u8(b)));
1876 }
else if constexpr (
sizeof(T) == 8) {
1877 auto x = vreinterpretq_u64_u8(a), y = vreinterpretq_u64_u8(b);
1878 auto xl = vmovn_u64(x), yl = vmovn_u64(y);
1880 vadd_u32(vmul_u32(xl, vmovn_u64(vshrq_n_u64(y, 32))), vmul_u32(vmovn_u64(vshrq_n_u64(x, 32)), yl));
1881 return vreinterpretq_u8_u64(vaddq_u64(vmull_u32(xl, yl), vshlq_n_u64(vmovl_u32(cross), 32)));
1884 template <simd_
integer_element T>
1885 [[nodiscard]] hint_inline hint_const uint8x16_t integer_broadcast_16(T x)
noexcept {
1886 if constexpr (
sizeof(T) == 1) {
1887 return vdupq_n_u8(integer_word(x));
1888 }
else if constexpr (
sizeof(T) == 2) {
1889 return vreinterpretq_u8_u16(vdupq_n_u16(integer_word(x)));
1890 }
else if constexpr (
sizeof(T) == 4) {
1891 return vreinterpretq_u8_u32(vdupq_n_u32(integer_word(x)));
1892 }
else if constexpr (
sizeof(T) == 8) {
1893 return vreinterpretq_u8_u64(vdupq_n_u64(integer_word(x)));
1896 template <simd_
integer_element T,
unsigned S>
1897 requires(S <
sizeof(T) * 8)
1898 [[nodiscard]] hint_inline hint_const uint8x16_t integer_left(uint8x16_t a)
noexcept {
1899 if constexpr (S == 0)
1902 if constexpr (
sizeof(T) == 1) {
1903 return vshlq_n_u8(a, S);
1904 }
else if constexpr (
sizeof(T) == 2) {
1905 return vreinterpretq_u8_u16(vshlq_n_u16(vreinterpretq_u16_u8(a), S));
1906 }
else if constexpr (
sizeof(T) == 4) {
1907 return vreinterpretq_u8_u32(vshlq_n_u32(vreinterpretq_u32_u8(a), S));
1908 }
else if constexpr (
sizeof(T) == 8) {
1909 return vreinterpretq_u8_u64(vshlq_n_u64(vreinterpretq_u64_u8(a), S));
1913 [[nodiscard]] hint_inline hint_const uint8x16_t integer_shift_left_variable(uint8x16_t a,uint8x16_t counts)
noexcept {
1914 return vreinterpretq_u8_u32(vshlq_u32(vreinterpretq_u32_u8(a),vreinterpretq_s32_u32(counts)));
1916 template <simd_
integer_element T,
unsigned S>
1917 requires(S <
sizeof(T) * 8)
1918 [[nodiscard]] hint_inline hint_const uint8x16_t integer_right(uint8x16_t a)
noexcept {
1919 if constexpr (S == 0)
1922 if constexpr (
sizeof(T) == 1) {
1923 if constexpr (std::is_unsigned_v<T>)
1924 return vshrq_n_u8(a, S);
1926 return vreinterpretq_u8_s8(vshrq_n_s8(vreinterpretq_s8_u8(a), S));
1927 }
else if constexpr (
sizeof(T) == 2) {
1928 if constexpr (std::is_unsigned_v<T>)
1929 return vreinterpretq_u8_u16(vshrq_n_u16(vreinterpretq_u16_u8(a), S));
1931 return vreinterpretq_u8_s16(vshrq_n_s16(vreinterpretq_s16_u8(a), S));
1932 }
else if constexpr (
sizeof(T) == 4) {
1933 if constexpr (std::is_unsigned_v<T>)
1934 return vreinterpretq_u8_u32(vshrq_n_u32(vreinterpretq_u32_u8(a), S));
1936 return vreinterpretq_u8_s32(vshrq_n_s32(vreinterpretq_s32_u8(a), S));
1937 }
else if constexpr (
sizeof(T) == 8) {
1938 if constexpr (std::is_unsigned_v<T>)
1939 return vreinterpretq_u8_u64(vshrq_n_u64(vreinterpretq_u64_u8(a), S));
1941 return vreinterpretq_u8_s64(vshrq_n_s64(vreinterpretq_s64_u8(a), S));
1945 template <simd_
integer_element T,
bool Greater>
1946 [[nodiscard]] hint_inline hint_const uint8x16_t integer_compare(uint8x16_t a, uint8x16_t b)
noexcept {
1947 if constexpr (
sizeof(T) == 1) {
1948 if constexpr (!Greater)
1949 return vceqq_u8(a, b);
1950 else if constexpr (std::is_unsigned_v<T>)
1951 return vcgtq_u8(a, b);
1953 return vcgtq_s8(vreinterpretq_s8_u8(a), vreinterpretq_s8_u8(b));
1954 }
else if constexpr (
sizeof(T) == 2) {
1955 if constexpr (!Greater)
1956 return vreinterpretq_u8_u16(vceqq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b)));
1957 else if constexpr (std::is_unsigned_v<T>)
1958 return vreinterpretq_u8_u16(vcgtq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b)));
1960 return vreinterpretq_u8_u16(vcgtq_s16(vreinterpretq_s16_u8(a), vreinterpretq_s16_u8(b)));
1961 }
else if constexpr (
sizeof(T) == 4) {
1962 if constexpr (!Greater)
1963 return vreinterpretq_u8_u32(vceqq_u32(vreinterpretq_u32_u8(a), vreinterpretq_u32_u8(b)));
1964 else if constexpr (std::is_unsigned_v<T>)
1965 return vreinterpretq_u8_u32(vcgtq_u32(vreinterpretq_u32_u8(a), vreinterpretq_u32_u8(b)));
1967 return vreinterpretq_u8_u32(vcgtq_s32(vreinterpretq_s32_u8(a), vreinterpretq_s32_u8(b)));
1968 }
else if constexpr (
sizeof(T) == 8) {
1969 if constexpr (!Greater)
1970 return vreinterpretq_u8_u64(vceqq_u64(vreinterpretq_u64_u8(a), vreinterpretq_u64_u8(b)));
1971 else if constexpr (std::is_unsigned_v<T>)
1972 return vreinterpretq_u8_u64(vcgtq_u64(vreinterpretq_u64_u8(a), vreinterpretq_u64_u8(b)));
1974 return vreinterpretq_u8_u64(vcgtq_s64(vreinterpretq_s64_u8(a), vreinterpretq_s64_u8(b)));
1977 template <simd_
integer_element T,
class M>
1978 [[nodiscard]] hint_inline hint_const uint8x16_t integer_select(M m, uint8x16_t a, uint8x16_t b)
noexcept {
1979 return vbslq_u8(m.to_native(), a, b);
1981 template <simd_
integer_element T,
class M>
1982 [[nodiscard]] hint_inline hint_const uint8x16_t integer_masked_add(M m, uint8x16_t prior, uint8x16_t a,
1983 uint8x16_t b)
noexcept {
1984 return integer_select<T>(m, integer_add<T>(a, b), prior);
1986 template <simd_
integer_element T,
class M>
1987 [[nodiscard]] hint_inline hint_const uint8x16_t integer_masked_sub(M m, uint8x16_t prior, uint8x16_t a,
1988 uint8x16_t b)
noexcept {
1989 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1994 template <simd_
integer_element T,
class M>
1995 [[nodiscard]] hint_inline hint_const __m128i integer_masked_mul(M m, __m128i prior, __m128i a,
1996 __m128i b)
noexcept {
1997 if constexpr (M::compact &&
sizeof(T) > 1) {
1998 if constexpr (
sizeof(T) == 2)
1999 return _mm_mask_mullo_epi16(prior, m.to_native(), a, b);
2000 if constexpr (
sizeof(T) == 4)
2001 return _mm_mask_mullo_epi32(prior, m.to_native(), a, b);
2002 else if constexpr (
sizeof(T) == 8)
2003 return _mm_mask_mullo_epi64(prior, m.to_native(), a, b);
2005 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2009 template <simd_
integer_element T,
class M>
2010 [[nodiscard]] hint_inline hint_const __m256i integer_masked_mul(M m, __m256i prior, __m256i a,
2011 __m256i b)
noexcept {
2012 if constexpr (M::compact &&
sizeof(T) > 1) {
2013 if constexpr (
sizeof(T) == 2)
2014 return _mm256_mask_mullo_epi16(prior, m.to_native(), a, b);
2015 if constexpr (
sizeof(T) == 4)
2016 return _mm256_mask_mullo_epi32(prior, m.to_native(), a, b);
2017 else if constexpr (
sizeof(T) == 8)
2018 return _mm256_mask_mullo_epi64(prior, m.to_native(), a, b);
2020 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2023#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2024 template <simd_
integer_element T,
class M>
2025 [[nodiscard]] hint_inline hint_const __m512i integer_masked_mul(M m, __m512i prior, __m512i a,
2026 __m512i b)
noexcept {
2027 if constexpr (M::compact &&
sizeof(T) > 1) {
2028#if NATIVE_HAS_AVX512BW
2029 if constexpr (
sizeof(T) == 2)
2030 return _mm512_mask_mullo_epi16(prior, m.to_native(), a, b);
2032 if constexpr (
sizeof(T) == 4)
2033 return _mm512_mask_mullo_epi32(prior, m.to_native(), a, b);
2034 else if constexpr (
sizeof(T) == 8)
2035 return _mm512_mask_mullo_epi64(prior, m.to_native(), a, b);
2037 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2040#if NATIVE_HAS_ARM_NEON
2041 template <simd_
integer_element T,
class M>
2042 [[nodiscard]] hint_inline hint_const uint8x16_t integer_masked_mul(M m, uint8x16_t prior, uint8x16_t a,
2043 uint8x16_t b)
noexcept {
2044 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2048 [[nodiscard]] hint_inline hint_const __m128i integer_bit_select(__m128i m,__m128i a,__m128i b)
noexcept {
2049 return _mm_or_si128(_mm_and_si128(m,a),_mm_andnot_si128(m,b));
2053 [[nodiscard]] hint_inline hint_const __m256i integer_bit_select(__m256i m,__m256i a,__m256i b)
noexcept {
2054 return _mm256_or_si256(_mm256_and_si256(m,a),_mm256_andnot_si256(m,b));
2057#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2058 [[nodiscard]] hint_inline hint_const __m512i integer_bit_select(__m512i m,__m512i a,__m512i b)
noexcept {
2059 return _mm512_or_si512(_mm512_and_si512(m,a),_mm512_andnot_si512(m,b));
2062#if NATIVE_HAS_ARM_NEON
2063 [[nodiscard]] hint_inline hint_const uint8x16_t integer_bit_select(uint8x16_t m,uint8x16_t a,uint8x16_t b)
noexcept {
return vbslq_u8(m,a,b); }
2068 namespace detail::NATIVE_BACKEND {
2069 template <std::
size_t Bytes>
struct integer_storage;
2071 template <>
struct integer_storage<16> {
2072 using type = __m128i;
2074 template <>
struct integer_storage<32> {
2075 using type = __m256i;
2078#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2079 template <>
struct integer_storage<64> {
2080 using type = __m512i;
2083#if NATIVE_HAS_ARM_NEON
2084 template <>
struct integer_storage<16> {
2085 using type = uint8x16_t;
2088 template <
class T, std::
size_t N>
2089 inline constexpr bool integer_shape =
2090 simd_integer_element<T> && (N == 1
2092 ||
sizeof(T) * N == 16 ||
sizeof(T) * N == 32
2094#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2095 || (
sizeof(T) * N == 64 && (
sizeof(T) >= 4
2096#if NATIVE_HAS_AVX512BW
2101#if NATIVE_HAS_ARM_NEON
2102 ||
sizeof(T) * N == 16
2107 namespace detail::NATIVE_BACKEND {
2108 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N, std::
size_t A = 1,
2109 simd_access Access = simd_access::ordinary>
2110 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
2111 [[nodiscard]] hint_inline hint_pure
constexpr simd<T, N,Arch>
load_simd(T
const *p, simd_memory<A, Access> = {})
noexcept;
2112 template <simd_integer_element T, std::size_t N, std::size_t A = 1,
2113 simd_access Access = simd_access::ordinary, ::native::isa<> Arch>
2114 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
2115 hint_inline
constexpr void store_simd(T *p, simd<T, N,Arch> v, simd_memory<A, Access> = {})
noexcept;
2116 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N, std::
size_t A = 1,
2117 simd_access Access = simd_access::ordinary>
2118 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
2119 [[nodiscard]] hint_inline
constexpr hint_pure simd<T, N,Arch>
load_simd_partial(T
const *p, std::size_t count,
2120 simd_memory<A, Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count");
2121 template <simd_integer_element T, std::size_t N, std::size_t A = 1,
2122 simd_access Access = simd_access::ordinary, ::native::isa<> Arch>
2123 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
2124 hint_inline
constexpr void store_simd_partial(T *p, simd<T, N,Arch> v, std::size_t count,
2125 simd_memory<A, Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count");
2128 template <simd_
integer_element T, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct simd<T, 1,Arch> : detail::swizzle_access<T,1,Arch> {
2130 template <
class U>
using rebind = simd<U,1,Arch>;
2131 template <std::
size_t A = 1>
2133 [[nodiscard]]
static hint_inline
constexpr simd load_memory(T
const * p)
noexcept {
2134 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T,1>(p,simd_memory<A>{});
2136 template <std::
size_t A = 1>
2138 hint_inline
constexpr void store_memory(T * p)
const noexcept {
2139 ::NATIVE_BACKEND_NAMESPACE::store_simd(p,*
this,simd_memory<A>{});
2142 using value_type = T;
2143 using native_type = T;
2144 using mask_type = ::NATIVE_BACKEND_NAMESPACE::comparison_mask<T, 1,Arch>;
2145 using mask = mask_type;
2146 using predicate_type = predicate<1,Arch>;
2147 using unsigned_register_tag = void;
2148 static constexpr std::size_t lanes = 1;
2151 constexpr simd() noexcept = default;
2153 template <simd_integer_element U> constexpr simd(U x) noexcept : value(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(x)) {}
2155 constexpr simd(std::array<T, 1>
const &x) noexcept : value(x[0]) {}
2157 [[nodiscard]] hint_inline
constexpr operator T() const noexcept {
return value; }
2159 [[nodiscard]] hint_inline
static constexpr simd from_native(T x)
noexcept {
return simd(x); }
2161 [[nodiscard]] hint_inline
constexpr T to_native() const noexcept {
return value; }
2163 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator+(simd a, simd b)
noexcept {
2164 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_scalar_add(a.value, b.value));
2167 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator-(simd a, simd b)
noexcept {
2168 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_scalar_sub(a.value, b.value));
2171 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator*(simd a, simd b)
noexcept {
2172 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_scalar_mul(a.value, b.value));
2175 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator&(simd a, simd b)
noexcept {
2177 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) & ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2180 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator|(simd a, simd b)
noexcept {
2182 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) | ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2185 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator^(simd a, simd b)
noexcept {
2187 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) ^ ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2190 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator~(simd a)
noexcept {
2191 return a ^ simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(~std::make_unsigned_t<T>(0)));
2194 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator-(simd a)
noexcept {
return simd(T(0)) - a; }
2196 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator+(simd a)
noexcept {
return a; }
2198 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator==(simd a, simd b)
noexcept {
2199 return mask_type(a.value == b.value);
2202 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>(simd a, simd b)
noexcept {
2203 return mask_type(a.value > b.value);
2206 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator!=(simd a, simd b)
noexcept {
2210 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<(simd a, simd b)
noexcept {
2214 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<=(simd a, simd b)
noexcept {
2218 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>=(simd a, simd b)
noexcept {
2222 template <simd_
integer_element U>
2223 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator+(simd a, U b)
noexcept {
2224 return a + simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2227 template <simd_
integer_element U>
2228 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator+(U a, simd b)
noexcept {
2229 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) + b;
2232 template <simd_
integer_element U>
2233 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator-(simd a, U b)
noexcept {
2234 return a - simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2237 template <simd_
integer_element U>
2238 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator-(U a, simd b)
noexcept {
2239 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) - b;
2242 template <simd_
integer_element U>
2243 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator*(simd a, U b)
noexcept {
2244 return a * simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2247 template <simd_
integer_element U>
2248 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator*(U a, simd b)
noexcept {
2249 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) * b;
2252 template <simd_
integer_element U>
2253 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator&(simd a, U b)
noexcept {
2254 return a & simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2257 template <simd_
integer_element U>
2258 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator&(U a, simd b)
noexcept {
2259 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) & b;
2262 template <simd_
integer_element U>
2263 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator|(simd a, U b)
noexcept {
2264 return a | simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2267 template <simd_
integer_element U>
2268 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator|(U a, simd b)
noexcept {
2269 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) | b;
2272 template <simd_
integer_element U>
2273 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator^(simd a, U b)
noexcept {
2274 return a ^ simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2277 template <simd_
integer_element U>
2278 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator^(U a, simd b)
noexcept {
2279 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) ^ b;
2282 template <simd_
integer_element U>
2283 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator==(simd a, U b)
noexcept {
2284 return a == simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2287 template <simd_
integer_element U>
2288 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator==(U a, simd b)
noexcept {
2289 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) == b;
2292 template <simd_
integer_element U>
2293 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator!=(simd a, U b)
noexcept {
2294 return a != simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2297 template <simd_
integer_element U>
2298 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator!=(U a, simd b)
noexcept {
2299 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) != b;
2302 template <simd_
integer_element U>
2303 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<(simd a, U b)
noexcept {
2304 return a < simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2307 template <simd_
integer_element U>
2308 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<(U a, simd b)
noexcept {
2309 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) < b;
2312 template <simd_
integer_element U>
2313 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>(simd a, U b)
noexcept {
2314 return a > simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2317 template <simd_
integer_element U>
2318 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>(U a, simd b)
noexcept {
2319 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) > b;
2322 template <simd_
integer_element U>
2323 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<=(simd a, U b)
noexcept {
2324 return a <= simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2327 template <simd_
integer_element U>
2328 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator<=(U a, simd b)
noexcept {
2329 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) <= b;
2332 template <simd_
integer_element U>
2333 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>=(simd a, U b)
noexcept {
2334 return a >= simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2337 template <simd_
integer_element U>
2338 [[nodiscard]]
friend hint_inline hint_const
constexpr mask_type
operator>=(U a, simd b)
noexcept {
2339 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) >= b;
2342 hint_inline
constexpr simd &
operator+=(simd b)
noexcept {
return *
this = *
this + b; }
2344 template <simd_
integer_element U> hint_inline
constexpr simd &
operator+=(U b)
noexcept {
return *
this = *
this + b; }
2346 hint_inline
constexpr simd &
operator-=(simd b)
noexcept {
return *
this = *
this - b; }
2348 template <simd_
integer_element U> hint_inline
constexpr simd &
operator-=(U b)
noexcept {
return *
this = *
this - b; }
2350 hint_inline
constexpr simd &
operator*=(simd b)
noexcept {
return *
this = *
this * b; }
2352 template <simd_
integer_element U> hint_inline
constexpr simd &
operator*=(U b)
noexcept {
return *
this = *
this * b; }
2354 hint_inline
constexpr simd &
operator&=(simd b)
noexcept {
return *
this = *
this & b; }
2356 template <simd_
integer_element U> hint_inline
constexpr simd &
operator&=(U b)
noexcept {
return *
this = *
this & b; }
2358 hint_inline
constexpr simd &
operator|=(simd b)
noexcept {
return *
this = *
this | b; }
2360 template <simd_
integer_element U> hint_inline
constexpr simd &
operator|=(U b)
noexcept {
return *
this = *
this | b; }
2362 hint_inline
constexpr simd &
operator^=(simd b)
noexcept {
return *
this = *
this ^ b; }
2364 template <simd_
integer_element U> hint_inline
constexpr simd &
operator^=(U b)
noexcept {
return *
this = *
this ^ b; }
2366 template <
unsigned K>
2367 requires(K <
sizeof(T) * 8)
2368 [[nodiscard]] hint_inline hint_const
constexpr simd left() const noexcept {
2369 using W = ::NATIVE_BACKEND_NAMESPACE::integer_work_word<T>;
2370 auto u = ::NATIVE_BACKEND_NAMESPACE::integer_word(value);
2371 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(W(u) << K));
2374 template <
unsigned K>
2375 requires(K <
sizeof(T) * 8)
2376 [[nodiscard]] hint_inline hint_const
constexpr simd right() const noexcept {
2377 using U = std::make_unsigned_t<T>;
2378 using W = ::NATIVE_BACKEND_NAMESPACE::integer_work_word<T>;
2379 auto u = ::NATIVE_BACKEND_NAMESPACE::integer_word(value);
2380 if constexpr (K == 0)
2383 U result = U(W(u) >> K);
2384 if constexpr (std::is_signed_v<T>) {
2386 result = U(result | U(std::numeric_limits<U>::max() ^ (std::numeric_limits<U>::max() >> K)));
2388 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(result));
2392 template <std::
size_t K>
2393 requires(K <
sizeof(T) * 8)
2394 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator<<(simd a, imm_t<K>)
noexcept {
2395 return a.template left<K>();
2398 template <std::
size_t K>
2399 requires(K <
sizeof(T) * 8)
2400 [[nodiscard]]
friend hint_inline hint_const
constexpr simd
operator>>(simd a, imm_t<K>)
noexcept {
2401 return a.template right<K>();
2404 [[nodiscard]] hint_inline
static hint_pure
constexpr simd load(T
const *p)
noexcept {
2405 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(p);
2408 [[nodiscard]] hint_inline
static hint_pure
constexpr simd loadu(T
const *p)
noexcept {
2409 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(p);
2412 hint_inline
constexpr void store(T *p)
const noexcept { ::NATIVE_BACKEND_NAMESPACE::store_simd(p, *
this); }
2414 hint_inline
constexpr void storeu(T *p)
const noexcept { ::NATIVE_BACKEND_NAMESPACE::store_simd(p, *
this); }
2416 [[nodiscard]] hint_inline
constexpr static hint_pure simd load_partial(T
const *p, std::size_t n,
2417 T fill = T(0)) noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
2419 std::array<T, lanes> data;
2421 if consteval {
for(std::size_t i=0;i<n;++i) data[i]=p[i]; }
2422 else {
if (n) std::memcpy(data.data(),
static_cast<void const *
>(p), n *
sizeof(T)); }
2423 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(data.data());
2426 hint_inline
constexpr void store_partial(T *p, std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") { ::NATIVE_BACKEND_NAMESPACE::store_simd_partial(p, *
this, n); }
2428 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator+(simd, U) =
delete;
2430 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator+(U, simd) =
delete;
2432 template <simd_
integer_element U, std::
size_t M>
2433 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2434 friend void operator+(simd, ::native::simd<U, M,Arch>) =
delete;
2436 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator-(simd, U) =
delete;
2438 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator-(U, simd) =
delete;
2440 template <simd_
integer_element U, std::
size_t M>
2441 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2442 friend void operator-(simd, ::native::simd<U, M,Arch>) =
delete;
2444 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator*(simd, U) =
delete;
2446 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator*(U, simd) =
delete;
2448 template <simd_
integer_element U, std::
size_t M>
2449 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2450 friend void operator*(simd, ::native::simd<U, M,Arch>) =
delete;
2452 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator/(simd, U) =
delete;
2454 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator/(U, simd) =
delete;
2456 template <simd_
integer_element U, std::
size_t M>
2457 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2458 friend void operator/(simd, ::native::simd<U, M,Arch>) =
delete;
2460 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator%(simd, U) =
delete;
2462 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator%(U, simd) =
delete;
2464 template <simd_
integer_element U, std::
size_t M>
2465 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2466 friend void operator%(simd, ::native::simd<U, M,Arch>) =
delete;
2468 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator&(simd, U) =
delete;
2470 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator&(U, simd) =
delete;
2472 template <simd_
integer_element U, std::
size_t M>
2473 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2474 friend void operator&(simd, ::native::simd<U, M,Arch>) =
delete;
2476 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator|(simd, U) =
delete;
2478 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator|(U, simd) =
delete;
2480 template <simd_
integer_element U, std::
size_t M>
2481 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2482 friend void operator|(simd, ::native::simd<U, M,Arch>) =
delete;
2484 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator^(simd, U) =
delete;
2486 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator^(U, simd) =
delete;
2488 template <simd_
integer_element U, std::
size_t M>
2489 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2490 friend void operator^(simd, ::native::simd<U, M,Arch>) =
delete;
2492 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator==(simd, U) =
delete;
2494 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator==(U, simd) =
delete;
2496 template <simd_
integer_element U, std::
size_t M>
2497 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2498 friend void operator==(simd, ::native::simd<U, M,Arch>) =
delete;
2500 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator!=(simd, U) =
delete;
2502 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator!=(U, simd) =
delete;
2504 template <simd_
integer_element U, std::
size_t M>
2505 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2506 friend void operator!=(simd, ::native::simd<U, M,Arch>) =
delete;
2508 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<(simd, U) =
delete;
2510 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<(U, simd) =
delete;
2512 template <simd_
integer_element U, std::
size_t M>
2513 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2514 friend void operator<(simd, ::native::simd<U, M,Arch>) =
delete;
2516 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>(simd, U) =
delete;
2518 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>(U, simd) =
delete;
2520 template <simd_
integer_element U, std::
size_t M>
2521 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2522 friend void operator>(simd, ::native::simd<U, M,Arch>) =
delete;
2524 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<=(simd, U) =
delete;
2526 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<=(U, simd) =
delete;
2528 template <simd_
integer_element U, std::
size_t M>
2529 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2530 friend void operator<=(simd, ::native::simd<U, M,Arch>) =
delete;
2532 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>=(simd, U) =
delete;
2534 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>=(U, simd) =
delete;
2536 template <simd_
integer_element U, std::
size_t M>
2537 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
2538 friend void operator>=(simd, ::native::simd<U, M,Arch>) =
delete;
2540 friend simd
operator/(simd, simd) =
delete;
2542 friend simd
operator%(simd, simd) =
delete;
2544 template <simd_
integer_element U>
friend simd
operator/(simd, U) =
delete;
2546 template <simd_
integer_element U>
friend simd
operator/(U, simd) =
delete;
2548 template <simd_
integer_element U>
friend simd
operator%(simd, U) =
delete;
2550 template <simd_
integer_element U>
friend simd
operator%(U, simd) =
delete;
2552 template <simd_
integer_element U>
friend simd
operator<<(simd, U) =
delete;
2554 template <simd_
integer_element U>
friend simd
operator>>(simd, U) =
delete;
2557#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
2561 template <simd_
integer_element T, std::
size_t N, ::native::isa<> Arch>
2562 requires NATIVE_ARCH_REQUIRES(Arch) &&(N > 1 && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>)
2566 template <std::
size_t A = 1>
2569 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T,N>(p,
simd_memory<A>{});
2571 template <std::
size_t A = 1>
2574 ::NATIVE_BACKEND_NAMESPACE::store_simd(p,*
this,
simd_memory<A>{});
2577 using value_type = T;
2578 using native_type = typename ::NATIVE_BACKEND_NAMESPACE::integer_storage<
sizeof(T) * N>
::type;
2579 using mask_type = ::NATIVE_BACKEND_NAMESPACE::comparison_mask<T, N,Arch>;
2580 using mask = mask_type;
2582 using unsigned_register_tag = void;
2583 static constexpr std::size_t lanes = N;
2584 native_type value{};
2586 constexpr simd() noexcept = default;
2588 template <simd_integer_element U> hint_inline constexpr
simd(U input) noexcept {
2589 T x = ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(input);
2591 std::array<T,N> data;
2593 value=__builtin_bit_cast(native_type,data);
2595 if constexpr (
sizeof(T) * N == 16)
2596 value = ::NATIVE_BACKEND_NAMESPACE::integer_broadcast_16(x);
2598 else if constexpr (
sizeof(T) * N == 32)
2599 value = ::NATIVE_BACKEND_NAMESPACE::integer_broadcast_32(x);
2601#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2602 else if constexpr (
sizeof(T) * N == 64)
2603 value = ::NATIVE_BACKEND_NAMESPACE::integer_broadcast_64(x);
2609 hint_inline
constexpr hint_target(
"sse2")
simd(native_type x) noexcept requires(sizeof(native_type)==16) : value(x) {}
2611 hint_inline
constexpr hint_target(
"avx")
simd(native_type x) noexcept requires(sizeof(native_type)==32) : value(x) {}
2613 hint_inline
constexpr hint_target(
"avx512f")
simd(native_type x) noexcept requires(sizeof(native_type)==64) : value(x) {}
2616 hint_inline
constexpr simd(native_type x) noexcept : value(x) {}
2619 template <
class... U>
2620 requires(
sizeof...(U) == N && (simd_integer_element<U> && ...))
2621 hint_inline constexpr
simd(U... xs) noexcept {
2622 std::array<T, N> data{::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(xs)...};
2623 if consteval { value=__builtin_bit_cast(native_type,data); }
2624 else { std::memcpy(&value, data.data(),
sizeof(value)); }
2627 hint_inline
constexpr simd(std::array<T, N>
const &data)
noexcept {
2628 if consteval { value=__builtin_bit_cast(native_type,data); }
2629 else { std::memcpy(&value, data.data(),
sizeof(value)); }
2633 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"sse2")
2634 operator native_type() const noexcept requires(sizeof(native_type)==16) {
return value; }
2636 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"sse2")
2639 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"sse2")
2640 native_type
to_native() const noexcept requires(sizeof(native_type)==16) {
return value; }
2642 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"avx")
2643 operator native_type() const noexcept requires(sizeof(native_type)==32) {
return value; }
2645 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"avx")
2648 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"avx")
2649 native_type
to_native() const noexcept requires(sizeof(native_type)==32) {
return value; }
2651 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"avx512f")
2652 operator native_type() const noexcept requires(sizeof(native_type)==64) {
return value; }
2654 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"avx512f")
2657 [[nodiscard]] hint_inline
constexpr hint_const hint_target(
"avx512f")
2658 native_type
to_native() const noexcept requires(sizeof(native_type)==64) {
return value; }
2661 [[nodiscard]] hint_inline
constexpr hint_const
operator native_type() const noexcept {
return value; }
2663 [[nodiscard]] hint_inline
constexpr static hint_const simd from_native(native_type x)
noexcept {
return simd(x); }
2665 [[nodiscard]] hint_inline
constexpr hint_const native_type to_native() const noexcept {
return value; }
2670 using U=std::make_unsigned_t<T>;
2671 using V=U __attribute__((ext_vector_type(N)));
2672 return from_native(__builtin_bit_cast(native_type,
2673 __builtin_bit_cast(V,a.value) + __builtin_bit_cast(V,b.value)));
2675 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_add<T>(a.value, b.value));
2680 using U=std::make_unsigned_t<T>;
2681 using V=U __attribute__((ext_vector_type(N)));
2682 return from_native(__builtin_bit_cast(native_type,
2683 __builtin_bit_cast(V,a.value) - __builtin_bit_cast(V,b.value)));
2685 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_sub<T>(a.value, b.value));
2690 using U=std::make_unsigned_t<T>;
2691 using V=U __attribute__((ext_vector_type(N)));
2692 return from_native(__builtin_bit_cast(native_type,
2693 __builtin_bit_cast(V,a.value) * __builtin_bit_cast(V,b.value)));
2695 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_mul<T>(a.value, b.value));
2700 using U=std::make_unsigned_t<T>;
2701 using V=U __attribute__((ext_vector_type(N)));
2702 return from_native(__builtin_bit_cast(native_type,
2703 __builtin_bit_cast(V,a.value) & __builtin_bit_cast(V,b.value)));
2705 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_and(a.value, b.value));
2710 using U=std::make_unsigned_t<T>;
2711 using V=U __attribute__((ext_vector_type(N)));
2712 return from_native(__builtin_bit_cast(native_type,
2713 __builtin_bit_cast(V,a.value) | __builtin_bit_cast(V,b.value)));
2715 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_or(a.value, b.value));
2720 using U=std::make_unsigned_t<T>;
2721 using V=U __attribute__((ext_vector_type(N)));
2722 return from_native(__builtin_bit_cast(native_type,
2723 __builtin_bit_cast(V,a.value) ^ __builtin_bit_cast(V,b.value)));
2725 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_xor(a.value, b.value));
2729 return a ^
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(~std::make_unsigned_t<T>(0)));
2738 auto x=__builtin_bit_cast(std::array<T,N>,a.value);
2739 auto y=__builtin_bit_cast(std::array<T,N>,b.value);
2740 std::uint64_t
bits=0;
2741 for(std::size_t i=0;i<N;++i)
bits|=std::uint64_t(x[i] == y[i])<<i;
2742 return mask_type::from_bitset(
bits);
2744 return mask_type::unsafe_from_native(::NATIVE_BACKEND_NAMESPACE::integer_compare<T, false>(a.value, b.value));
2747 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>(
simd a,
simd b)
noexcept {
2749 auto x=__builtin_bit_cast(std::array<T,N>,a.value);
2750 auto y=__builtin_bit_cast(std::array<T,N>,b.value);
2751 std::uint64_t
bits=0;
2752 for(std::size_t i=0;i<N;++i)
bits|=std::uint64_t(x[i] > y[i])<<i;
2753 return mask_type::from_bitset(
bits);
2755 return mask_type::unsafe_from_native(::NATIVE_BACKEND_NAMESPACE::integer_compare<T, true>(a.value, b.value));
2762 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<(
simd a,
simd b)
noexcept {
2774 template <simd_
integer_element U>
2776 return a +
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2779 template <simd_
integer_element U>
2781 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) + b;
2784 template <simd_
integer_element U>
2786 return a -
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2789 template <simd_
integer_element U>
2791 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) - b;
2794 template <simd_
integer_element U>
2796 return a *
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2799 template <simd_
integer_element U>
2801 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) * b;
2804 template <simd_
integer_element U>
2806 return a &
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2809 template <simd_
integer_element U>
2811 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) & b;
2814 template <simd_
integer_element U>
2816 return a |
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2819 template <simd_
integer_element U>
2821 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) | b;
2824 template <simd_
integer_element U>
2826 return a ^
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2829 template <simd_
integer_element U>
2831 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) ^ b;
2834 template <simd_
integer_element U>
2835 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator==(
simd a, U b)
noexcept {
2836 return a ==
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2839 template <simd_
integer_element U>
2840 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator==(U a,
simd b)
noexcept {
2841 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) == b;
2844 template <simd_
integer_element U>
2845 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(
simd a, U b)
noexcept {
2846 return a !=
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2849 template <simd_
integer_element U>
2850 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(U a,
simd b)
noexcept {
2851 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) != b;
2854 template <simd_
integer_element U>
2855 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<(
simd a, U b)
noexcept {
2856 return a < simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2859 template <simd_
integer_element U>
2860 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<(U a,
simd b)
noexcept {
2861 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) < b;
2864 template <simd_
integer_element U>
2865 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>(
simd a, U b)
noexcept {
2866 return a >
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2869 template <simd_
integer_element U>
2870 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>(U a,
simd b)
noexcept {
2871 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) > b;
2874 template <simd_
integer_element U>
2875 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(
simd a, U b)
noexcept {
2876 return a <= simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2879 template <simd_
integer_element U>
2880 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(U a,
simd b)
noexcept {
2881 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) <= b;
2884 template <simd_
integer_element U>
2885 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(
simd a, U b)
noexcept {
2886 return a >=
simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2889 template <simd_
integer_element U>
2890 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(U a,
simd b)
noexcept {
2891 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) >= b;
2896 template <simd_
integer_element U> hint_inline
constexpr simd &
operator+=(U b)
noexcept {
return *
this = *
this + b; }
2900 template <simd_
integer_element U> hint_inline
constexpr simd &
operator-=(U b)
noexcept {
return *
this = *
this - b; }
2904 template <simd_
integer_element U> hint_inline
constexpr simd &
operator*=(U b)
noexcept {
return *
this = *
this * b; }
2908 template <simd_
integer_element U> hint_inline
constexpr simd &
operator&=(U b)
noexcept {
return *
this = *
this & b; }
2912 template <simd_
integer_element U> hint_inline
constexpr simd &
operator|=(U b)
noexcept {
return *
this = *
this | b; }
2916 template <simd_
integer_element U> hint_inline
constexpr simd &
operator^=(U b)
noexcept {
return *
this = *
this ^ b; }
2918 template <
unsigned K>
2919 requires(K <
sizeof(T) * 8)
2920 [[nodiscard]] hint_inline
constexpr hint_const
simd left()
const noexcept {
2922 using E=std::make_unsigned_t<T>;
2923 using V=E __attribute__((ext_vector_type(N)));
2924 return from_native(__builtin_bit_cast(native_type,__builtin_bit_cast(V,value) << K));
2926 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_left<T, K>(value));
2929 template <
unsigned K>
2930 requires(K <
sizeof(T) * 8)
2931 [[nodiscard]] hint_inline
constexpr hint_const
simd right()
const noexcept {
2934 using V=E __attribute__((ext_vector_type(N)));
2935 return from_native(__builtin_bit_cast(native_type,__builtin_bit_cast(V,value) >> K));
2937 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_right<T, K>(value));
2940 template <std::
size_t K>
2941 requires(K <
sizeof(T) * 8)
2942 [[nodiscard]]
friend hint_inline
constexpr hint_const
simd operator<<(
simd a,
imm_t<K>)
noexcept {
2947 requires std::same_as<T,std::uint32_t> &&
requires(native_type x) {
2948 ::NATIVE_BACKEND_NAMESPACE::integer_shift_left_variable(x,x);
2952 auto values=__builtin_bit_cast(std::array<std::uint32_t,N>,a.value);
2953 auto shifts=__builtin_bit_cast(std::array<std::uint32_t,N>,counts.value);
2954 for(std::size_t i=0;i<N;++i) values[i]=shifts[i]<32 ? values[i]<<shifts[i] : 0;
2955 return simd(values);
2957 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_shift_left_variable(a.value,counts.value));
2960 template <std::
size_t K>
2961 requires(K <
sizeof(T) * 8)
2962 [[nodiscard]]
friend hint_inline
constexpr hint_const
simd operator>>(
simd a,
imm_t<K>)
noexcept {
2966 [[nodiscard]] hint_inline
static hint_pure
constexpr simd load(T
const *p)
noexcept {
2967 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(p);
2970 [[nodiscard]] hint_inline
static hint_pure
constexpr simd loadu(T
const *p)
noexcept {
2971 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(p);
2974 hint_inline
constexpr void store(T *p)
const noexcept { ::NATIVE_BACKEND_NAMESPACE::store_simd(p, *
this); }
2976 hint_inline
constexpr void storeu(T *p)
const noexcept { ::NATIVE_BACKEND_NAMESPACE::store_simd(p, *
this); }
2978 [[nodiscard]] hint_inline
constexpr static hint_pure
simd load_partial(T
const *p, std::size_t n,
2979 T fill = T(0)) noexcept hint_diagnose_if(n >
simd::lanes,
"partial SIMD count exceeds the lane count") {
2982 std::array<T,N> data; data.fill(fill);
2983 for(std::size_t i=0;i<n;++i) data[i]=p[i];
2986 if (n == lanes)
return load(p);
2987 if (!n)
return simd(fill);
2990#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2991 if constexpr (
sizeof(T) == 4 ||
sizeof(T) == 8) {
2992 auto active = std::uint64_t((std::uint64_t(1) << n) - 1);
2993 if constexpr (
sizeof(T) * N == 64) {
2994 if constexpr (
sizeof(T) == 4)
2995 return from_native(_mm512_mask_loadu_epi32(
simd(fill).value, __mmask16(active), p));
2996 else return from_native(_mm512_mask_loadu_epi64(
simd(fill).value, __mmask8(active), p));
2998#if NATIVE_HAS_AVX512VL
2999 else if constexpr (
sizeof(T) * N == 32) {
3000 if constexpr (
sizeof(T) == 4)
3001 return from_native(_mm256_mask_loadu_epi32(
simd(fill).value, __mmask8(active), p));
3002 else return from_native(_mm256_mask_loadu_epi64(
simd(fill).value, __mmask8(active), p));
3004 if constexpr (
sizeof(T) == 4)
3005 return from_native(_mm_mask_loadu_epi32(
simd(fill).value, __mmask8(active), p));
3006 else return from_native(_mm_mask_loadu_epi64(
simd(fill).value, __mmask8(active), p));
3012 if constexpr ((
sizeof(T) == 4 ||
sizeof(T) == 8) && !mask_type::compact &&
sizeof(T) * N <= 32) {
3013 native_type active, loaded;
3014 if constexpr (
sizeof(T) * N == 32) {
3015 if constexpr (
sizeof(T) == 4) {
3016 active = _mm256_cmpgt_epi32(_mm256_set1_epi32(
int(n)), _mm256_setr_epi32(0,1,2,3,4,5,6,7));
3017 loaded = _mm256_maskload_epi32(
reinterpret_cast<int const *
>(p), active);
3019 active = _mm256_cmpgt_epi64(_mm256_set1_epi64x(
static_cast<long long>(n)), _mm256_setr_epi64x(0,1,2,3));
3020 loaded = _mm256_maskload_epi64(
reinterpret_cast<long long const *
>(p), active);
3022 }
else if constexpr (
sizeof(T) * N == 16) {
3023 if constexpr (
sizeof(T) == 4) {
3024 active = _mm_cmpgt_epi32(_mm_set1_epi32(
int(n)), _mm_setr_epi32(0,1,2,3));
3025 loaded = _mm_maskload_epi32(
reinterpret_cast<int const *
>(p), active);
3027 active = _mm_cmpgt_epi64(_mm_set1_epi64x(
static_cast<long long>(n)), _mm_set_epi64x(1,0));
3028 loaded = _mm_maskload_epi64(
reinterpret_cast<long long const *
>(p), active);
3031 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_bit_select(active, loaded,
simd(fill).value));
3034#if NATIVE_HAS_ARM_NEON
3035 if constexpr (
sizeof(T) == 4) {
3036 auto result = vreinterpretq_u32_u8(
simd(fill).value);
3038 auto bytes =
reinterpret_cast<unsigned char const *
>(p);
3040 case 3: std::memcpy(&word, bytes + 8, 4); result = vsetq_lane_u32(word, result, 2); [[fallthrough]];
3041 case 2: std::memcpy(&word, bytes + 4, 4); result = vsetq_lane_u32(word, result, 1); [[fallthrough]];
3042 case 1: std::memcpy(&word, bytes, 4); result = vsetq_lane_u32(word, result, 0);
3045 }
else if constexpr (
sizeof(T) == 8) {
3047 std::memcpy(&word,
static_cast<void const *
>(p), 8);
3048 return from_native(vreinterpretq_u8_u64(vsetq_lane_u64(word, vreinterpretq_u64_u8(
simd(fill).value), 0)));
3051 std::array<T, lanes> data;
3054 std::memcpy(data.data(),
static_cast<void const *
>(p), n *
sizeof(T));
3055 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, lanes>(data.data());
3058 hint_inline
constexpr void store_partial(T *p, std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
3061 auto data=__builtin_bit_cast(std::array<T,N>,value);
3062 for(std::size_t i=0;i<n;++i) p[i]=data[i];
3064 } ::NATIVE_BACKEND_NAMESPACE::store_simd_partial(p, *
this, n); }
3066 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator+(
simd, U) =
delete;
3068 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator+(U,
simd) =
delete;
3070 template <simd_
integer_element U, std::
size_t M>
3071 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3074 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator-(
simd, U) =
delete;
3076 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator-(U,
simd) =
delete;
3078 template <simd_
integer_element U, std::
size_t M>
3079 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3082 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator*(
simd, U) =
delete;
3084 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator*(U,
simd) =
delete;
3086 template <simd_
integer_element U, std::
size_t M>
3087 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3090 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator/(
simd, U) =
delete;
3092 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator/(U,
simd) =
delete;
3094 template <simd_
integer_element U, std::
size_t M>
3095 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3098 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator%(
simd, U) =
delete;
3100 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator%(U,
simd) =
delete;
3102 template <simd_
integer_element U, std::
size_t M>
3103 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3106 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator&(
simd, U) =
delete;
3108 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator&(U,
simd) =
delete;
3110 template <simd_
integer_element U, std::
size_t M>
3111 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3114 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator|(
simd, U) =
delete;
3116 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator|(U,
simd) =
delete;
3118 template <simd_
integer_element U, std::
size_t M>
3119 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3122 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator^(
simd, U) =
delete;
3124 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator^(U,
simd) =
delete;
3126 template <simd_
integer_element U, std::
size_t M>
3127 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3130 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator==(
simd, U) =
delete;
3132 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator==(U,
simd) =
delete;
3134 template <simd_
integer_element U, std::
size_t M>
3135 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3138 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator!=(
simd, U) =
delete;
3140 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator!=(U,
simd) =
delete;
3142 template <simd_
integer_element U, std::
size_t M>
3143 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3146 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<(
simd, U) =
delete;
3148 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<(U,
simd) =
delete;
3150 template <simd_
integer_element U, std::
size_t M>
3151 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3154 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>(
simd, U) =
delete;
3156 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>(U,
simd) =
delete;
3158 template <simd_
integer_element U, std::
size_t M>
3159 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3162 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<=(
simd, U) =
delete;
3164 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator<=(U,
simd) =
delete;
3166 template <simd_
integer_element U, std::
size_t M>
3167 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3170 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>=(
simd, U) =
delete;
3172 template <
class U>
requires (std::is_arithmetic_v<U> && !simd_integer_element<U>)
friend void operator>=(U,
simd) =
delete;
3174 template <simd_
integer_element U, std::
size_t M>
3175 requires(!std::same_as<simd, ::native::simd<U, M,Arch>>)
3197 namespace detail::NATIVE_BACKEND {
3198 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N, std::
size_t A, simd_access Access>
3199 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3203 if constexpr(N==1)
return V(*p);
3205 std::array<T,N> data;
3206 for(std::size_t i=0;i<N;++i) data[i]=p[i];
3210 typename V::native_type value;
3214#if defined(__GNUC__) || defined(__clang__)
3215 if constexpr (A > 1)
3216 p =
static_cast<T
const *
>(__builtin_assume_aligned(p, A));
3217#elif defined(_MSC_VER)
3218 if constexpr (A > 1)
3219 __assume((
reinterpret_cast<std::uintptr_t
>(p) & (A - 1)) == 0);
3221 std::memcpy(&value,
static_cast<void const *
>(p),
sizeof(value));
3222 return V::from_native(value);
3224 template <simd_
integer_element T, std::
size_t N, std::
size_t A, simd_access Access, ::native::isa<> Arch>
3225 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3226 hint_inline
constexpr void store_simd(T *p, simd<T, N,Arch> v, simd_memory<A, Access>)
noexcept {
3228 if constexpr(N==1) *p=v.value;
3230 auto data=__builtin_bit_cast(std::array<T,N>,v.value);
3231 for(std::size_t i=0;i<N;++i) p[i]=data[i];
3235#if defined(__GNUC__) || defined(__clang__)
3236 if constexpr (A > 1)
3237 p =
static_cast<T *
>(__builtin_assume_aligned(p, A));
3238#elif defined(_MSC_VER)
3239 if constexpr (A > 1)
3240 __assume((
reinterpret_cast<std::uintptr_t
>(p) & (A - 1)) == 0);
3242 std::memcpy(
static_cast<void *
>(p), &v.value,
sizeof(v.value));
3244 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N, std::
size_t A, simd_access Access>
3245 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3246 [[nodiscard]] hint_inline
constexpr hint_pure simd<T, N,Arch>
load_simd_partial(T
const *p, std::size_t count,
3247 simd_memory<A, Access>)
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
3249 std::array<T, N> data{};
3250 if consteval {
for(std::size_t i=0;i<count;++i) data[i]=p[i]; }
3251 else {
if (count) std::memcpy(data.data(),
static_cast<void const *
>(p), count *
sizeof(T)); }
3252 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, N>(data.data());
3254 template <simd_
integer_element T, std::
size_t N, std::
size_t A, simd_access Access, ::native::isa<> Arch>
3255 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3256 hint_inline
constexpr void store_simd_partial(T *p, simd<T, N,Arch> v, std::size_t count,
3257 simd_memory<A, Access>)
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
3259 std::array<T, N> data;
3261 if consteval {
for(std::size_t i=0;i<count;++i) p[i]=data[i]; }
3262 else {
if (count) std::memcpy(
static_cast<void *
>(p), data.data(), count *
sizeof(T)); }
3264 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N>
3265 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3266 [[nodiscard]] hint_inline
constexpr hint_pure simd<T, N,Arch>
load_simd(std::array<T, N>
const &p)
noexcept {
3267 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, N>(p.data());
3269 template <::native::isa<> Arch, simd_
integer_element T, std::
size_t N>
3270 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3271 [[nodiscard]] hint_inline
constexpr hint_pure simd<T, N,Arch>
load_simd(std::span<T const, N> p)
noexcept {
3272 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T, N>(p.data());
3276 namespace detail::NATIVE_BACKEND {
3277 template <
class M,
class T, std::
size_t N, isa<> Arch>
3278 concept integer_mask_for =
3279 std::same_as<M, typename simd<T, N,Arch>::mask_type> || std::same_as<M, simd<::NATIVE_BACKEND_NAMESPACE::mask_lane_for<T>, N,Arch>>;
3283 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3284 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3287 std::array<T,N> first{},second{};
3288 a.store(first.data()); b.store(second.data());
3289 auto bits=m.to_bitset();
3290 for(std::size_t i=0;i<N;++i)
if(!((bits>>i)&1)) first[i]=second[i];
3293 if constexpr (N == 1)
3294 return any(m) ? a : b;
3295#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
3302 template <simd_
integer_element T, std::
size_t N, ::native::isa<> Arch>
3303 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3306 if consteval {
return (bits & a) | (~bits & b); }
3307 if constexpr(N==1)
return (bits & a) | (~bits & b);
3308#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
3315 template <simd_
integer_element T, std::
size_t N, ::native::isa<> Arch>
3316 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N>
3323 template <simd_mask_element M, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
3325 using U =
typename M::storage_type;
3327 !std::same_as<typename simd<U,N,Arch>::native_type,
decltype(m.to_native())>)) {
3328 std::array<U,N> words{};
auto bits=m.to_bitset();
3329 for(std::size_t i=0;i<N;++i) words[i]=((bits>>i)&1)?~U{}:U{};
3335 template <simd_
integer_element T, simd_mask_element M, std::
size_t N, ::native::isa<> Arch>
3336 requires NATIVE_ARCH_REQUIRES(Arch) &&(
sizeof(T) ==
sizeof(M))
3342 template <simd_
integer_element T, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) &&
3343 requires(predicate<N,Arch> m) { to_vector_mask<::NATIVE_BACKEND_NAMESPACE::mask_lane_for<T>>(m); }
3349 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3350 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3353 if consteval {
return select(m,a + b,prior); }
3354 if constexpr (N == 1)
3355 return select(m, a + b, prior);
3356#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
3363 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3364 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3370 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3371 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3374 if consteval {
return select(m,a - b,prior); }
3375 if constexpr (N == 1)
3376 return select(m, a - b, prior);
3377#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
3384 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3385 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3391 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3392 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3395 if consteval {
return select(m,a * b,prior); }
3396 if constexpr (N == 1)
3397 return select(m, a * b, prior);
3398#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
3405 template <simd_
integer_element T, std::
size_t N,
class M, ::native::isa<> Arch>
3406 requires NATIVE_ARCH_REQUIRES(Arch) &&(::NATIVE_BACKEND_NAMESPACE::integer_shape<T, N> && ::NATIVE_BACKEND_NAMESPACE::integer_mask_for<M, T, N, Arch>)
3414 template<simd_
integer_element T, std::
size_t N, std::
size_t K, ::native::isa<> Arch>
3415 requires NATIVE_ARCH_REQUIRES(Arch) && (K >=
sizeof(T) * 8) && (!NATIVE_HOST_WASM || !Arch.has(polyfill))
3416 void operator<<(simd<T, N,Arch>, imm_t<K>) =
delete;
3418 template<simd_
integer_element T, std::
size_t N, std::
size_t K, ::native::isa<> Arch>
3419 requires NATIVE_ARCH_REQUIRES(Arch) && (K >=
sizeof(T) * 8) && (!NATIVE_HOST_WASM || !Arch.has(polyfill))
3420 void operator>>(simd<T, N,Arch>, imm_t<K>) =
delete;
3423namespace NATIVE_BACKEND_NAMESPACE::native {
3425 using uint32x1=::native::simd<::native::uint32_t,1,NATIVE_DEFAULT_ARCH>;
3426#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON
3427 using uint32x4=::native::simd<::native::uint32_t,4,NATIVE_DEFAULT_ARCH>;
3430 using uint32x8=::native::simd<::native::uint32_t,8,NATIVE_DEFAULT_ARCH>;
3432#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
3433 using uint32x16=::native::simd<::native::uint32_t,16,NATIVE_DEFAULT_ARCH>;
3435 template<
class V>
concept unsigned_register =
requires {
3436 typename V::unsigned_register_tag;
3437 typename V::value_type;
3438 } && std::same_as<typename V::value_type,::native::uint32_t>;
3439 template<
unsigned S,
unsigned_register V>
requires(S<32)
3440 [[nodiscard]] hint_inline hint_const V shift_left(V x)
noexcept {
return x.template left<S>(); }
3441 template<
unsigned S,
unsigned_register V>
requires(S<32)
3442 [[nodiscard]] hint_inline hint_const V shift_right(V x)
noexcept {
return x.template right<S>(); }
3445 struct integer_target {
3446#if defined(NATIVE_INTEGER_SCALAR)
3447 using unsigned_type=uint32x1;
3448#elif NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
3449 using unsigned_type=uint32x16;
3450#elif NATIVE_HAS_AVX2
3451 using unsigned_type=uint32x8;
3452#elif NATIVE_HAS_ARM_NEON
3453 using unsigned_type=uint32x4;
3455 using unsigned_type=uint32x1;
3457 static constexpr std::size_t lanes=unsigned_type::lanes;
3462 namespace detail::NATIVE_BACKEND {
3463 template <
class V,std::
size_t Alignment> [[nodiscard]] hint_artificial hint_inline
constexpr hint_pure V simd_load_native(hint_noescape
float const *)
noexcept;
3464 template <
class V,std::
size_t Alignment> hint_artificial hint_inline
constexpr void simd_store_native(hint_noescape
float *,V)
noexcept;
3466 namespace detail::NATIVE_BACKEND {
3467 template <
class T>
inline constexpr bool custom_argument = ::native::simd_custom_element<std::remove_cvref_t<T>>;
3468 template <
class T, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
inline constexpr bool custom_argument<simd<T,N,Arch>> = ::native::simd_custom_element<T>;
3470 namespace detail::NATIVE_BACKEND {
3472 using ::native::detail::register_memory;
3474 template <::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct hint_empty_bases
simd<float, 1,Arch> : ::NATIVE_BACKEND_NAMESPACE::register_memory<simd<float,1,Arch>, 1>, detail::swizzle_access<float,1,Arch> {
3476 template <
class T>
using rebind = simd<T,1,Arch>;
3477 using vector_mask_type=simd<mask32,1,Arch>;
3478 using mask_type=::NATIVE_BACKEND_NAMESPACE::comparison_mask<float,1,Arch>;
3479 using mask = mask_type;
3480 using predicate_type = predicate<1,Arch>;
3483 hint_inline simd() =
default;
3485 hint_inline
constexpr simd(
float x) : value(x) {}
3487 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
3489 hint_inline
constexpr void store(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
3491 [[nodiscard]]
friend hint_inline
constexpr hint_pure simd operator+(simd a, simd b) {
3492 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::add,a,b); }
3493 return simd(a.value + b.value);
3496 [[nodiscard]]
friend hint_inline
constexpr hint_pure simd operator-(simd a, simd b) {
3497 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::subtract,a,b); }
3498 return simd(a.value - b.value);
3501 [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator*(simd a, simd b) {
3502 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply,a,b); }
3503 return simd(a.value * b.value);
3506 [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator/(simd a, simd b) {
3507 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::divide,a,b); }
3508 return simd(a.value / b.value);
3511 [[nodiscard]]
friend hint_inline
constexpr hint_const simd operator-(simd a) {
3512 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::negate,a); }
3513 return simd(-a.value);
3516 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<(simd a, simd b) {
3517 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,a,b); }
3518 return mask_type::from_native(a.value < b.value ? ~std::uint32_t(0) : 0u);
3521 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>(simd a, simd b) {
3522 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,b,a); }
3523 return mask_type::from_native(a.value > b.value ? ~std::uint32_t(0) : 0u);
3526 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator==(simd a, simd b) {
3527 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::equal,a,b); }
3528 return mask_type::from_native(a.value == b.value ? ~std::uint32_t(0) : 0u);
3531 template<
class M>
requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
3532 [[nodiscard]]
friend hint_inline
constexpr hint_const simd select(M m,simd a,simd b) {
3533 if consteval { return ::native::detail::float_constant::select(m,a,b); }
3534 return m.to_native()!=0 ? a : b;
3537 [[nodiscard]]
friend hint_inline
constexpr simd
fma(simd a, simd b, simd c) {
3538 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply_add,a,b,c); }
3539 return simd(std::fma(a.value, b.value, c.value));
3542 [[nodiscard]]
friend hint_inline
constexpr simd
sqrt(simd a) {
3543 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::square_root,a); }
3544 return simd(std::sqrt(a.value));
3547 [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
round_even(simd a) {
3548 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::nearest,a); }
3549 if (!std::isfinite(a.value) || std::abs(a.value) >= 0x1p23f)
return a;
3550 float lo = std::floor(a.value), delta = a.value - lo;
3551 float r = lo + float(delta > .5f || (delta == .5f && std::fmod(lo, 2.f) != 0));
3552 return simd(r == 0 ? std::copysign(0.f, a.value) : r);
3556 [[nodiscard]]
friend hint_inline
constexpr hint_const simd normal_pow2(simd n) {
3557 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::power_of_two,n); }
3558 return simd(std::bit_cast<float>(std::uint32_t(
int(n.value) + 127) << 23));
3561 template <std::
size_t Alignment = 1>
3563 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_memory(
float const * p)
noexcept {
3564 return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,Alignment>(p);
3566 template <std::
size_t Alignment = 1>
3568 hint_inline
constexpr void store_memory(
float * p)
const noexcept {
3569 ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,Alignment>(p,*
this);
3571 using value_type = float;
3572 using register_type = simd;
3573 using native_type = float;
3574 using bits_type = simd<uint32_t,1,Arch>;
3576 [[nodiscard]] hint_inline
constexpr hint_pure
operator native_type() const noexcept {
return value; }
3578 [[nodiscard]] hint_inline
constexpr hint_pure native_type to_native() const noexcept {
return value; }
3580 [[nodiscard]] hint_inline
constexpr hint_pure bits_type bits() const noexcept {
return bits_type(std::bit_cast<std::uint32_t>(value)); }
3582 [[nodiscard]] hint_inline
constexpr hint_pure bits_type to_bits() const noexcept {
return bits(); }
3584 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(bits_type bits)
noexcept {
return simd(std::bit_cast<float>(bits.value)); }
3586 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(std::uint32_t bits)
noexcept {
return from_bits(bits_type(bits)); }
3588 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_float(
float x)
noexcept {
return simd(x); }
3590 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_native(native_type x)
noexcept {
return simd(x); }
3592 [[nodiscard]]
static hint_inline
constexpr hint_const simd unsafe_from_float32(native_type x)
noexcept {
return simd(x); }
3594 [[nodiscard]]
static hint_inline
constexpr hint_pure simd loadu(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
3596 hint_inline
constexpr void storeu(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
3598 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits(hint_noescape std::uint32_t
const * p)
noexcept {
return from_bits(bits_type::load(p)); }
3600 hint_inline
constexpr void store_bits(hint_noescape std::uint32_t * p)
const noexcept { bits().store(p); }
3602 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits_partial(hint_noescape std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
3604 hint_inline
constexpr void store_bits_partial(hint_noescape std::uint32_t * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") { bits().store_partial(p,n); }
3606 hint_inline
constexpr simd(std::array<float,1>
const & values) noexcept : simd(loadu(values.data())) {}
3608 hint_inline
constexpr simd &
operator+=(simd b)
noexcept {
return *
this=*
this+b; }
3610 hint_inline
constexpr simd &
operator-=(simd b)
noexcept {
return *
this=*
this-b; }
3612 hint_inline
constexpr simd &
operator*=(simd b)
noexcept {
return *
this=*
this*b; }
3614 hint_inline
constexpr simd &
operator/=(simd b)
noexcept {
return *
this=*
this/b; }
3616 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(simd a,simd b)
noexcept {
return ~(a==b); }
3618 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(simd a,simd b)
noexcept {
return (a<b)|(a==b); }
3620 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(simd a,simd b)
noexcept {
return (a>b)|(a==b); }
3628 template <::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct hint_empty_bases
simd<float, 4,Arch> : detail::register_memory<simd<float,4,Arch>, 4>, detail::swizzle_access<float,4,Arch> {
3629 static constexpr isa<> architecture=Arch;
3633 using mask = mask_type;
3643 hint_inline
constexpr simd(
float x) {
3644 if consteval { std::array<float,
sizeof(native_type)/
sizeof(
float)> values{}; values.fill(x); value=__builtin_bit_cast(native_type,values); }
3645 else { value=_mm_set1_ps(x); }
3648 hint_inline hint_target(
"sse") constexpr
simd(__m128 x) : value(x) {}
3655 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::add,a,b); }
3656 return simd(_mm_add_ps(a.value, b.value));
3660 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::subtract,a,b); }
3661 return simd(_mm_sub_ps(a.value, b.value));
3665 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply,a,b); }
3666 return simd(_mm_mul_ps(a.value, b.value));
3670 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::divide,a,b); }
3671 return simd(_mm_div_ps(a.value, b.value));
3675 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::negate,a); }
3676 return simd(_mm_xor_ps(a.value, _mm_set1_ps(-0.f)));
3680 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,a,b); }
3681 if constexpr(bool(NATIVE_HAS_AVX512VL))
3682 return mask_type::from_native(_mm_cmp_ps_mask(a.value,b.value,_CMP_LT_OQ));
3684 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_LT_OQ)));
3688 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,b,a); }
3689 if constexpr(bool(NATIVE_HAS_AVX512VL))
3690 return mask_type::from_native(_mm_cmp_ps_mask(a.value,b.value,_CMP_GT_OQ));
3692 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_GT_OQ)));
3696 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::equal,a,b); }
3697 if constexpr(bool(NATIVE_HAS_AVX512VL))
3698 return mask_type::from_native(_mm_cmp_ps_mask(a.value,b.value,_CMP_EQ_OQ));
3700 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_EQ_OQ)));
3703 template<
class M>
requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
3705 if consteval { return ::native::detail::float_constant::select(m,a,b); }
3706 if constexpr(M::compact)
return simd(_mm_mask_blend_ps(m.to_native(),b.value,a.value));
3708 return simd(_mm_blendv_ps(b.value,a.value,_mm_castsi128_ps(m.to_native()))); }
3711 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply_add,a,b,c); }
3712 return simd(_mm_fmadd_ps(a.value, b.value, c.value));
3715 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure
simd sqrt(
simd a) {
3716 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::square_root,a); }
3717 return simd(_mm_sqrt_ps(a.value));
3721 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::nearest,a); }
3722 return simd(_mm_round_ps(a.value, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
3726 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::power_of_two,n); }
3727 return simd(_mm_castsi128_ps(_mm_slli_epi32(_mm_add_epi32(_mm_cvttps_epi32(n.value), _mm_set1_epi32(127)), 23)));
3730 template <std::
size_t Alignment = 1>
3732 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_memory(
float const * p)
noexcept {
3734 std::array<float,
sizeof(native_type)/
sizeof(
float)> values{};
3735 for (std::size_t i=0;i<
sizeof(native_type)/
sizeof(float);++i) values[i]=p[i];
3736 return from_native(__builtin_bit_cast(native_type,values));
3738 if constexpr(Alignment>=16)
return simd(_mm_load_ps(p));
3739 else return simd(_mm_loadu_ps(p));
3741 template <std::
size_t Alignment = 1>
3745 auto values=__builtin_bit_cast(std::array<
float,
sizeof(native_type)/
sizeof(
float)>,value);
3746 for (std::size_t i=0;i<
sizeof(native_type)/
sizeof(float);++i) p[i]=values[i];
3749 if constexpr(Alignment>=16) _mm_store_ps(p,value);
3750 else _mm_storeu_ps(p,value);
3752 using value_type = float;
3753 using register_type =
simd;
3754 using native_type = __m128;
3757 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"sse") operator native_type() const noexcept {
return value; }
3759 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"sse") native_type
to_native() const noexcept {
return value; }
3761 hint_artificial [[nodiscard]] hint_inline
constexpr hint_pure bits_type
bits() const noexcept {
if consteval {
return bits_type::from_native(__builtin_bit_cast(
typename bits_type::native_type,value)); }
return bits_type::from_native(_mm_castps_si128(value)); }
3763 [[nodiscard]] hint_inline
constexpr hint_pure bits_type
to_bits() const noexcept {
return bits(); }
3765 hint_artificial [[nodiscard]]
static hint_inline
constexpr hint_const
simd from_bits(bits_type
bits)
noexcept {
if consteval {
return simd(__builtin_bit_cast(native_type,
bits.to_native())); }
return simd(_mm_castsi128_ps(
bits.value)); }
3769 [[nodiscard]]
static hint_inline
constexpr hint_const
simd from_float(
float x)
noexcept {
return simd(x); }
3771 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"sse")
simd from_native(native_type x) noexcept {
return simd(x); }
3779 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_bits(hint_noescape std::uint32_t
const * p)
noexcept {
return from_bits(bits_type::load(p)); }
3781 hint_inline
constexpr void store_bits(hint_noescape std::uint32_t * p)
const noexcept {
bits().store(p); }
3783 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_bits_partial(hint_noescape std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept hint_diagnose_if(n >
simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
3785 hint_inline
constexpr void store_bits_partial(hint_noescape std::uint32_t * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
bits().store_partial(p,n); }
3787 hint_inline
constexpr simd(std::array<float,4>
const & values) noexcept :
simd(
loadu(values.data())) {}
3788#if defined(__clang__)
3790 template <
class... X>
requires (
sizeof...(X)==4) && (std::convertible_to<X,float> && ...)
3791 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : value{
static_cast<float>(x)...} {}
3794 template <
class... X>
requires (
sizeof...(X)==4) && (std::convertible_to<X,float> && ...)
3795 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) :
simd(
loadu(std::array<float,4>{
static_cast<float>(x)...}.data())) {}
3806 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(
simd a,
simd b)
noexcept {
return ~(a==b); }
3808 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(
simd a,
simd b)
noexcept {
return (a<b)|(a==b); }
3810 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(
simd a,
simd b)
noexcept {
return (a>b)|(a==b); }
3815 template <::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct simd<float, 8,Arch> : detail::register_memory<simd<float,8,Arch>, 8> {
3816 static constexpr isa<> architecture=Arch;
3820 using mask = mask_type;
3830 hint_inline
constexpr simd(
float x) {
3831 if consteval { std::array<float,
sizeof(native_type)/
sizeof(
float)> values{}; values.fill(x); value=__builtin_bit_cast(native_type,values); }
3832 else { value=_mm256_set1_ps(x); }
3835 hint_inline hint_target(
"avx") constexpr
simd(__m256 x) : value(x) {}
3842 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::add,a,b); }
3843 return simd(_mm256_add_ps(a.value, b.value));
3847 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::subtract,a,b); }
3848 return simd(_mm256_sub_ps(a.value, b.value));
3852 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply,a,b); }
3853 return simd(_mm256_mul_ps(a.value, b.value));
3857 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::divide,a,b); }
3858 return simd(_mm256_div_ps(a.value, b.value));
3862 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::negate,a); }
3863 return simd(_mm256_xor_ps(a.value, _mm256_set1_ps(-0.f)));
3867 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,a,b); }
3868 if constexpr(bool(NATIVE_HAS_AVX512VL))
3869 return mask_type::from_native(_mm256_cmp_ps_mask(a.value,b.value,_CMP_LT_OQ));
3871 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_LT_OQ)));
3875 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,b,a); }
3876 if constexpr(bool(NATIVE_HAS_AVX512VL))
3877 return mask_type::from_native(_mm256_cmp_ps_mask(a.value,b.value,_CMP_GT_OQ));
3879 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_GT_OQ)));
3883 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::equal,a,b); }
3884 if constexpr(bool(NATIVE_HAS_AVX512VL))
3885 return mask_type::from_native(_mm256_cmp_ps_mask(a.value,b.value,_CMP_EQ_OQ));
3887 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_EQ_OQ)));
3890 template<
class M>
requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
3892 if consteval { return ::native::detail::float_constant::select(m,a,b); }
3893 if constexpr(M::compact)
return simd(_mm256_mask_blend_ps(m.to_native(),b.value,a.value));
3895 return simd(_mm256_blendv_ps(b.value,a.value,_mm256_castsi256_ps(m.to_native()))); }
3898 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply_add,a,b,c); }
3899 return simd(_mm256_fmadd_ps(a.value, b.value, c.value));
3902 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure
simd sqrt(
simd a) {
3903 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::square_root,a); }
3904 return simd(_mm256_sqrt_ps(a.value));
3908 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::nearest,a); }
3909 return simd(_mm256_round_ps(a.value, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
3913 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::power_of_two,n); }
3914 return simd(_mm256_castsi256_ps(_mm256_slli_epi32(_mm256_add_epi32(_mm256_cvttps_epi32(n.value), _mm256_set1_epi32(127)), 23)));
3917 template <std::
size_t Alignment = 1>
3919 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_memory(
float const * p)
noexcept {
3921 std::array<float,
sizeof(native_type)/
sizeof(
float)> values{};
3922 for (std::size_t i=0;i<
sizeof(native_type)/
sizeof(float);++i) values[i]=p[i];
3923 return from_native(__builtin_bit_cast(native_type,values));
3925 if constexpr(Alignment>=32)
return simd(_mm256_load_ps(p));
3926 else return simd(_mm256_loadu_ps(p));
3928 template <std::
size_t Alignment = 1>
3932 auto values=__builtin_bit_cast(std::array<
float,
sizeof(native_type)/
sizeof(
float)>,value);
3933 for (std::size_t i=0;i<
sizeof(native_type)/
sizeof(float);++i) p[i]=values[i];
3936 if constexpr(Alignment>=32) _mm256_store_ps(p,value);
3937 else _mm256_storeu_ps(p,value);
3939 using value_type = float;
3940 using register_type =
simd;
3941 using native_type = __m256;
3944 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"avx") operator native_type() const noexcept {
return value; }
3946 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"avx") native_type
to_native() const noexcept {
return value; }
3948 hint_artificial [[nodiscard]] hint_inline
constexpr hint_pure bits_type
bits() const noexcept {
if consteval {
return bits_type::from_native(__builtin_bit_cast(
typename bits_type::native_type,value)); }
return bits_type::from_native(_mm256_castps_si256(value)); }
3950 [[nodiscard]] hint_inline
constexpr hint_pure bits_type
to_bits() const noexcept {
return bits(); }
3952 hint_artificial [[nodiscard]]
static hint_inline
constexpr hint_const
simd from_bits(bits_type
bits)
noexcept {
if consteval {
return simd(__builtin_bit_cast(native_type,
bits.to_native())); }
return simd(_mm256_castsi256_ps(
bits.value)); }
3956 [[nodiscard]]
static hint_inline
constexpr hint_const
simd from_float(
float x)
noexcept {
return simd(x); }
3958 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"avx")
simd from_native(native_type x) noexcept {
return simd(x); }
3966 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_bits(hint_noescape std::uint32_t
const * p)
noexcept {
return from_bits(bits_type::load(p)); }
3968 hint_inline
constexpr void store_bits(hint_noescape std::uint32_t * p)
const noexcept {
bits().store(p); }
3970 [[nodiscard]]
static hint_inline
constexpr hint_pure
simd load_bits_partial(hint_noescape std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept hint_diagnose_if(n >
simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
3972 hint_inline
constexpr void store_bits_partial(hint_noescape std::uint32_t * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
bits().store_partial(p,n); }
3974 hint_inline
constexpr simd(std::array<float,8>
const & values) noexcept :
simd(
loadu(values.data())) {}
3975#if defined(__clang__)
3977 template <
class... X>
requires (
sizeof...(X)==8) && (std::convertible_to<X,float> && ...)
3978 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : value{
static_cast<float>(x)...} {}
3981 template <
class... X>
requires (
sizeof...(X)==8) && (std::convertible_to<X,float> && ...)
3982 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) :
simd(
loadu(std::array<float,8>{
static_cast<float>(x)...}.data())) {}
3993 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(
simd a,
simd b)
noexcept {
return ~(a==b); }
3995 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(
simd a,
simd b)
noexcept {
return (a<b)|(a==b); }
3997 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(
simd a,
simd b)
noexcept {
return (a>b)|(a==b); }
4004#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4005 template <::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct simd<float, 16,Arch> : ::NATIVE_BACKEND_NAMESPACE::register_memory<simd<float,16,Arch>, 16> {
4007 template <
class T>
using rebind = simd<T,16,Arch>;
4008 using vector_mask_type=simd<mask32,16,Arch>;
4009 using mask_type=::NATIVE_BACKEND_NAMESPACE::comparison_mask<float,16,Arch>;
4010 using mask = mask_type;
4011 using predicate_type = predicate<16,Arch>;
4014 hint_inline simd() =
default;
4016 hint_inline
constexpr simd(simd
const &) =
default;
4018 hint_reinitializes hint_inline
constexpr simd & operator=(simd
const &) =
default;
4020 hint_inline
constexpr simd(
float x) {
4021 if consteval { std::array<float,
sizeof(native_type)/
sizeof(
float)> values{}; values.fill(x); value=__builtin_bit_cast(native_type,values); }
4022 else { value=_mm512_set1_ps(x); }
4025 hint_inline hint_target(
"avx512f") constexpr simd(__m512 x) : value(x) {}
4027 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
4029 hint_inline
constexpr void store(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
4031 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator+(simd a, simd b) {
4032 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::add,a,b); }
4033 return simd(_mm512_add_ps(a.value, b.value));
4036 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator-(simd a, simd b) {
4037 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::subtract,a,b); }
4038 return simd(_mm512_sub_ps(a.value, b.value));
4041 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator*(simd a, simd b) {
4042 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply,a,b); }
4043 return simd(_mm512_mul_ps(a.value, b.value));
4046 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator/(simd a, simd b) {
4047 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::divide,a,b); }
4048 return simd(_mm512_div_ps(a.value, b.value));
4051 [[nodiscard]]
friend hint_inline
constexpr hint_const simd
operator-(simd a) {
4052 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::negate,a); }
4053 return simd(_mm512_xor_ps(a.value, _mm512_set1_ps(-0.f)));
4056 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<(simd a, simd b) {
4057 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,a,b); }
4058 return mask_type::from_native(_mm512_cmp_ps_mask(a.value,b.value,_CMP_LT_OQ));
4061 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>(simd a, simd b) {
4062 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,b,a); }
4063 return mask_type::from_native(_mm512_cmp_ps_mask(a.value,b.value,_CMP_GT_OQ));
4066 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator==(simd a, simd b) {
4067 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::equal,a,b); }
4068 return mask_type::from_native(_mm512_cmp_ps_mask(a.value,b.value,_CMP_EQ_OQ));
4071 template<
class M>
requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
4072 [[nodiscard]]
friend hint_inline
constexpr hint_const simd
select(M m,simd a,simd b) {
4073 if consteval { return ::native::detail::float_constant::select(m,a,b); }
4074 if constexpr(M::compact)
return simd(_mm512_mask_blend_ps(m.to_native(),b.value,a.value));
4075 else return simd(_mm512_castsi512_ps(_mm512_or_si512(_mm512_and_si512(m.to_native(),_mm512_castps_si512(a.value)),
4076 _mm512_andnot_si512(m.to_native(),_mm512_castps_si512(b.value))))); }
4078 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
fma(simd a, simd b, simd c) {
4079 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply_add,a,b,c); }
4080 return simd(_mm512_fmadd_ps(a.value, b.value, c.value));
4083 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
sqrt(simd a) {
4084 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::square_root,a); }
4085 return simd(_mm512_sqrt_ps(a.value));
4088 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
round_even(simd a) {
4089 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::nearest,a); }
4090 return simd(_mm512_roundscale_ps(a.value, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
4093 [[nodiscard]]
friend hint_inline
constexpr hint_const simd normal_pow2(simd n) {
4094 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::power_of_two,n); }
4095 return simd(_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_add_epi32(_mm512_cvttps_epi32(n.value), _mm512_set1_epi32(127)), 23)));
4098 template <std::
size_t Alignment = 1>
4100 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_memory(
float const * p)
noexcept {
4101 return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,Alignment>(p);
4103 template <std::
size_t Alignment = 1>
4105 hint_inline
constexpr void store_memory(
float * p)
const noexcept {
4106 ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,Alignment>(p,*
this);
4108 using value_type = float;
4109 using register_type = simd;
4110 using native_type = __m512;
4111 using bits_type = simd<uint32_t,16,Arch>;
4113 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"avx512f") operator native_type() const noexcept {
return value; }
4115 [[nodiscard]] hint_inline
constexpr hint_pure hint_target(
"avx512f") native_type to_native() const noexcept {
return value; }
4117 hint_artificial [[nodiscard]] hint_inline
constexpr hint_pure bits_type bits() const noexcept {
if consteval {
return bits_type::from_native(__builtin_bit_cast(
typename bits_type::native_type,value)); }
return bits_type::from_native(_mm512_castps_si512(value)); }
4119 [[nodiscard]] hint_inline
constexpr hint_pure bits_type to_bits() const noexcept {
return bits(); }
4121 hint_artificial [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(bits_type bits)
noexcept {
if consteval {
return simd(__builtin_bit_cast(native_type,bits.to_native())); }
return simd(_mm512_castsi512_ps(bits.value)); }
4123 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(std::uint32_t bits)
noexcept {
return from_bits(bits_type(bits)); }
4125 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_float(
float x)
noexcept {
return simd(x); }
4127 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"avx512f") simd from_native(native_type x) noexcept {
return simd(x); }
4129 [[nodiscard]]
static hint_inline
constexpr hint_const hint_target(
"avx512f") simd unsafe_from_float32(native_type x) noexcept {
return simd(x); }
4131 [[nodiscard]]
static hint_inline
constexpr hint_pure simd loadu(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
4133 hint_inline
constexpr void storeu(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
4135 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits(hint_noescape std::uint32_t
const * p)
noexcept {
return from_bits(bits_type::load(p)); }
4137 hint_inline
constexpr void store_bits(hint_noescape std::uint32_t * p)
const noexcept { bits().store(p); }
4139 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits_partial(hint_noescape std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
4141 hint_inline
constexpr void store_bits_partial(hint_noescape std::uint32_t * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") { bits().store_partial(p,n); }
4143 hint_inline
constexpr simd(std::array<float,16>
const & values) noexcept : simd(loadu(values.data())) {}
4144#if defined(__clang__)
4146 template <
class... X>
requires (
sizeof...(X)==16) && (std::convertible_to<X,float> && ...)
4147 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : value{
static_cast<float>(x)...} {}
4150 template <
class... X>
requires (
sizeof...(X)==16) && (std::convertible_to<X,float> && ...)
4151 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : simd(loadu(std::array<float,16>{
static_cast<float>(x)...}.data())) {}
4154 hint_inline
constexpr simd &
operator+=(simd b)
noexcept {
return *
this=*
this+b; }
4156 hint_inline
constexpr simd &
operator-=(simd b)
noexcept {
return *
this=*
this-b; }
4158 hint_inline
constexpr simd &
operator*=(simd b)
noexcept {
return *
this=*
this*b; }
4160 hint_inline
constexpr simd &
operator/=(simd b)
noexcept {
return *
this=*
this/b; }
4162 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator!=(simd a,simd b)
noexcept {
return ~(a==b); }
4164 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator<=(simd a,simd b)
noexcept {
return (a<b)|(a==b); }
4166 [[nodiscard]]
friend hint_inline
constexpr hint_const mask_type
operator>=(simd a,simd b)
noexcept {
return (a>b)|(a==b); }
4169#if NATIVE_HAS_ARM_NEON
4170 template <::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
struct alignas(float32x4_t) hint_empty_bases simd<float, 4,Arch> : ::NATIVE_BACKEND_NAMESPACE::register_memory<simd<float,4,Arch>, 4>, detail::swizzle_access<float,4,Arch> {
4172 template <
class T>
using rebind = simd<T,4,Arch>;
4173 using vector_mask_type=simd<mask32,4,Arch>;
4174 using mask_type=::NATIVE_BACKEND_NAMESPACE::comparison_mask<float,4,Arch>;
4175 using mask = mask_type;
4176 using predicate_type = predicate<4,Arch>;
4179 hint_inline simd() =
default;
4181 hint_inline
constexpr simd(simd
const &) =
default;
4183 hint_reinitializes hint_inline
constexpr simd & operator=(simd
const &) =
default;
4185 hint_inline
constexpr simd(
float x) {
4186 if consteval { std::array<float,
sizeof(native_type)/
sizeof(
float)> values{}; values.fill(x); value=__builtin_bit_cast(native_type,values); }
4187 else { value=vdupq_n_f32(x); }
4190 hint_inline
constexpr simd(float32x4_t x) : value(x) {}
4192 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
4194 hint_inline
constexpr void store(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
4196 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd operator+(simd a, simd b) {
4197 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::add,a,b); }
4198 return simd(vaddq_f32(a.value, b.value));
4201 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd operator-(simd a, simd b) {
4202 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::subtract,a,b); }
4203 return simd(vsubq_f32(a.value, b.value));
4206 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator*(simd a, simd b) {
4207 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply,a,b); }
4208 return simd(vmulq_f32(a.value, b.value));
4211 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
operator/(simd a, simd b) {
4212 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::divide,a,b); }
4213 return simd(vdivq_f32(a.value, b.value));
4216 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_const simd operator-(simd a) {
4217 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::negate,a); }
4218 return simd(vnegq_f32(a.value));
4224 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator<(simd a, simd b) {
4228 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator>(simd a, simd b) {
4229 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::less,b,a); }
4230 auto x=::native::detail::arm_register_order(a.value);
4231 auto y=::native::detail::arm_register_order(b.value);
4233 asm volatile(
"fcmgt %0.4s, %1.4s, %2.4s" :
"=w"(bits) :
"w"(x),
"w"(y) :
"memory");
4234 return mask_type::unsafe_from_native(vreinterpretq_u8_u32(::native::detail::arm_register_order(bits)));
4237 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator==(simd a, simd b) {
4238 if consteval { return ::native::detail::float_constant::compare(::native::detail::float_constant::equal,a,b); }
4239 auto x=::native::detail::arm_register_order(a.value);
4240 auto y=::native::detail::arm_register_order(b.value);
4242 asm volatile(
"fcmeq %0.4s, %1.4s, %2.4s" :
"=w"(bits) :
"w"(x),
"w"(y) :
"memory");
4243 return mask_type::unsafe_from_native(vreinterpretq_u8_u32(::native::detail::arm_register_order(bits)));
4246 template<
class M>
requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
4247 [[nodiscard]]
friend hint_inline
constexpr hint_const simd select(M m,simd a,simd b) {
4248 if consteval { return ::native::detail::float_constant::select(m,a,b); }
4249 return simd(vbslq_f32(vreinterpretq_u32_u8(m.to_native()),a.value,b.value));
4252 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
fma(simd a, simd b, simd c) {
4253 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::multiply_add,a,b,c); }
4254 return simd(vfmaq_f32(c.value, a.value, b.value));
4257 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_pure simd
sqrt(simd a) {
4258 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::square_root,a); }
4259 return simd(vsqrtq_f32(a.value));
4262 hint_artificial [[nodiscard]]
friend hint_inline
constexpr hint_const simd
round_even(simd a) {
4263 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::nearest,a); }
4264 return simd(vrndnq_f32(a.value));
4267 [[nodiscard]]
friend hint_inline
constexpr hint_const simd normal_pow2(simd n) {
4268 if consteval { return ::native::detail::float_constant::map(::native::detail::float_constant::power_of_two,n); }
4269 return simd(vreinterpretq_f32_s32(vshlq_n_s32(vaddq_s32(vcvtq_s32_f32(n.value), vdupq_n_s32(127)), 23)));
4272 template <std::
size_t Alignment = 1>
4274 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_memory(
float const * p)
noexcept {
4275 return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,Alignment>(p);
4277 template <std::
size_t Alignment = 1>
4279 hint_inline
constexpr void store_memory(
float * p)
const noexcept {
4280 ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,Alignment>(p,*
this);
4282 using value_type = float;
4283 using register_type = simd;
4284 using native_type = float32x4_t;
4285 using bits_type = simd<uint32_t,4,Arch>;
4287 [[nodiscard]] hint_inline
constexpr hint_pure
operator native_type() const noexcept {
return value; }
4289 [[nodiscard]] hint_inline
constexpr hint_pure native_type to_native() const noexcept {
return value; }
4291 hint_artificial [[nodiscard]] hint_inline
constexpr hint_pure bits_type bits() const noexcept {
if consteval {
return bits_type::from_native(__builtin_bit_cast(
typename bits_type::native_type,value)); }
return bits_type::from_native(vreinterpretq_u8_f32(value)); }
4293 [[nodiscard]] hint_inline
constexpr hint_pure bits_type to_bits() const noexcept {
return bits(); }
4295 hint_artificial [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(bits_type bits)
noexcept {
if consteval {
return simd(__builtin_bit_cast(native_type,bits.to_native())); }
return simd(vreinterpretq_f32_u8(bits.value)); }
4297 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_bits(std::uint32_t bits)
noexcept {
return from_bits(bits_type(bits)); }
4299 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_float(
float x)
noexcept {
return simd(x); }
4301 [[nodiscard]]
static hint_inline
constexpr hint_const simd from_native(native_type x)
noexcept {
return simd(x); }
4303 [[nodiscard]]
static hint_inline
constexpr hint_const simd unsafe_from_float32(native_type x)
noexcept {
return simd(x); }
4305 [[nodiscard]]
static hint_inline
constexpr hint_pure simd loadu(hint_noescape
float const * p) { return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd,1>(p); }
4307 hint_inline
constexpr void storeu(hint_noescape
float * p)
const { ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd,1>(p,*
this); }
4309 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits(hint_noescape std::uint32_t
const * p)
noexcept {
return from_bits(bits_type::load(p)); }
4311 hint_inline
constexpr void store_bits(hint_noescape std::uint32_t * p)
const noexcept { bits().store(p); }
4313 [[nodiscard]]
static hint_inline
constexpr hint_pure simd load_bits_partial(hint_noescape std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
4315 hint_inline
constexpr void store_bits_partial(hint_noescape std::uint32_t * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") { bits().store_partial(p,n); }
4317 hint_inline
constexpr simd(std::array<float,4>
const & values) noexcept : simd(loadu(values.data())) {}
4318#if defined(__clang__)
4320 template <
class... X>
requires (
sizeof...(X)==4) && (std::convertible_to<X,float> && ...)
4321 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : value{
static_cast<float>(x)...} {}
4323 template <
class... X>
requires (
sizeof...(X)==4) && (std::convertible_to<X,float> && ...)
4325 hint_inline
constexpr simd(X... x)
noexcept((
noexcept(
static_cast<float>(x)) && ...)) : simd(loadu(std::array<float,4>{
static_cast<float>(x)...}.data())) {}
4328 hint_inline
constexpr simd &
operator+=(simd b)
noexcept {
return *
this=*
this+b; }
4330 hint_inline
constexpr simd &
operator-=(simd b)
noexcept {
return *
this=*
this-b; }
4332 hint_inline
constexpr simd &
operator*=(simd b)
noexcept {
return *
this=*
this*b; }
4334 hint_inline
constexpr simd &
operator/=(simd b)
noexcept {
return *
this=*
this/b; }
4336 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator!=(simd a,simd b)
noexcept {
return ~(a==b); }
4338 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator<=(simd a,simd b)
noexcept {
return b>=a; }
4340 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator>=(simd a,simd b)
noexcept {
4342 return ::native::detail::float_constant::compare([](
auto x,
auto y) {
4343 return ::native::detail::float_constant::less(y,x) || ::native::detail::float_constant::equal(x,y);
4346 auto x=::native::detail::arm_register_order(a.value);
4347 auto y=::native::detail::arm_register_order(b.value);
4349 asm volatile(
"fcmge %0.4s, %1.4s, %2.4s" :
"=w"(bits) :
"w"(x),
"w"(y) :
"memory");
4350 return mask_type::unsafe_from_native(vreinterpretq_u8_u32(::native::detail::arm_register_order(bits)));
4354 namespace detail::NATIVE_BACKEND {
4358 template<std::
size_t N>
inline constexpr bool native_scaleb_shape =
4359#if NATIVE_HAS_AVX512F
4361#if NATIVE_HAS_AVX512VL
4362 || N==2 || N==3 || N==4 || N==8
4379 template <std::
size_t N,
class M, ::native::isa<> Arch>
4380 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::native_scaleb_shape<N> &&(std::same_as<M,typename simd<float,N,Arch>::mask_type> ||
4381 std::same_as<M,typename simd<float,N,Arch>::vector_mask_type>)
4385 auto a=::native::detail::float_constant::words(value),b=::native::detail::float_constant::words(exponent);
4386 auto result=::native::detail::float_constant::words(prior);
4387 auto active=
mask.to_bitset();
4388 for(std::size_t i=0;i<N;++i)
if((active>>i)&1) result[i]=::native::detail::float_constant::scale(a[i],b[i]);
4391#if NATIVE_HAS_AVX512F
4392 if constexpr(N==2 || N==3) {
4396 return __builtin_bit_cast(V,_mm_mask_scalef_ps(__builtin_bit_cast(__m128,prior.to_native()),
4397 __mmask8(
mask.to_bitset()),__builtin_bit_cast(__m128,value.to_native()),
4398 __builtin_bit_cast(__m128,exponent.to_native())));
4400 auto native_mask=[&] {
if constexpr(M::compact)
return mask.to_native();
else return to_predicate(
mask).to_native();};
4402 _mm_set_ss(prior.value),__mmask8(native_mask()),_mm_set_ss(value.value),_mm_set_ss(exponent.value))));
4403 else if constexpr(N==16)
return simd<float,N,Arch>(_mm512_mask_scalef_ps(prior.value,native_mask(),value.value,exponent.value));
4404#if NATIVE_HAS_AVX512VL
4405 else if constexpr(N==4)
return simd<float,N,Arch>(_mm_mask_scalef_ps(prior.value,native_mask(),value.value,exponent.value));
4406 else if constexpr(N==8)
return simd<float,N,Arch>(_mm256_mask_scalef_ps(prior.value,native_mask(),value.value,exponent.value));
4413 template <std::
size_t N,
class M, ::native::isa<> Arch>
4414 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::native_scaleb_shape<N> &&(std::same_as<M,typename simd<float,N,Arch>::mask_type> ||
4415 std::same_as<M,typename simd<float,N,Arch>::vector_mask_type>)
4419#if NATIVE_HAS_AVX512F
4420 auto native_mask=[&] {
if constexpr(M::compact)
return mask.to_native();
else return to_predicate(
mask).to_native();};
4422 __mmask8(native_mask()),_mm_set_ss(value.value),_mm_set_ss(exponent.value))));
4423 else if constexpr(N==16)
return simd<float,N,Arch>(_mm512_maskz_scalef_ps(native_mask(),value.value,exponent.value));
4424#if NATIVE_HAS_AVX512VL
4425 else if constexpr(N==2 || N==3)
return __builtin_bit_cast(
simd<float,N,Arch>,_mm_maskz_scalef_ps(
4426 __mmask8(
mask.to_bitset()),__builtin_bit_cast(__m128,value.to_native()),__builtin_bit_cast(__m128,exponent.to_native())));
4427 else if constexpr(N==4)
return simd<float,N,Arch>(_mm_maskz_scalef_ps(native_mask(),value.value,exponent.value));
4428 else if constexpr(N==8)
return simd<float,N,Arch>(_mm256_maskz_scalef_ps(native_mask(),value.value,exponent.value));
4435 template <std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::native_scaleb_shape<N>
4441 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4444 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4447 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4450 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4453 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4456 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4459 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4462 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4465 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4468 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4471 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4474 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4477 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4480 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4483 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4486 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4489 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4492 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4495 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4498 template <std::
size_t N,
class U, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<U>) && (!std::same_as<U,simd<float,N,Arch>>) && std::convertible_to<U,simd<float,N,Arch>>
4501 template <std::
size_t N,
class A,
class B, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<A> && !::NATIVE_BACKEND_NAMESPACE::custom_argument<B>) && std::convertible_to<A,simd<float,N,Arch>> && std::convertible_to<B,simd<float,N,Arch>>
4502 [[nodiscard]] hint_inline
constexpr auto fma(
simd<float,N,Arch> a,A b,B c)
noexcept(
noexcept(
simd<float,N,Arch>(b)) &&
noexcept(
simd<float,N,Arch>(c))) {
return fma(a,
simd<float,N,Arch>(b),
simd<float,N,Arch>(c)); }
4504 template <std::
size_t N,
class A,
class B, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<A> && !::NATIVE_BACKEND_NAMESPACE::custom_argument<B>) && (!std::same_as<A,simd<float,N,Arch>>) && std::convertible_to<A,simd<float,N,Arch>> && std::convertible_to<B,simd<float,N,Arch>>
4505 [[nodiscard]] hint_inline
constexpr auto fma(A a,
simd<float,N,Arch> b,B c)
noexcept(
noexcept(
simd<float,N,Arch>(a)) &&
noexcept(
simd<float,N,Arch>(c))) {
return fma(
simd<float,N,Arch>(a),b,
simd<float,N,Arch>(c)); }
4507 template <std::
size_t N,
class A,
class B, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && (!::NATIVE_BACKEND_NAMESPACE::custom_argument<A> && !::NATIVE_BACKEND_NAMESPACE::custom_argument<B>) && (!std::same_as<A,simd<float,N,Arch>>) && (!std::same_as<B,simd<float,N,Arch>>) && std::convertible_to<A,simd<float,N,Arch>> && std::convertible_to<B,simd<float,N,Arch>>
4508 [[nodiscard]] hint_inline
constexpr auto fma(A a,B b,
simd<float,N,Arch> c)
noexcept(
noexcept(
simd<float,N,Arch>(a)) &&
noexcept(
simd<float,N,Arch>(b))) {
return fma(
simd<float,N,Arch>(a),
simd<float,N,Arch>(b),c); }
4511 template <std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
4521 template<std::
size_t N, ::native::isa<> Arch>
4522 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
4523 [[nodiscard]] hint_inline
constexpr simd<std::int32_t,N,Arch>
4524 fcvtzs(simd<float,N,Arch> x)
noexcept {
4525 using I = simd<std::int32_t,N,Arch>;
4527 std::array<float,N> values{};
4528 std::array<std::int32_t,N> result{};
4529 x.store(values.data());
4530 for (std::size_t i = 0; i < N; ++i)
4531 result[i] = detail::float_constant::fcvtzs(std::bit_cast<std::uint32_t>(values[i]));
4532 return I::load(result.data());
4534 if constexpr (N == 1)
return I(vcvts_s32_f32(x.value));
4535 else if constexpr (N == 2 || N == 3) {
4538 result.value = __builtin_bit_cast(
typename I::native_type,
4539 fcvtzs(x.to_storage()).to_native());
4542#if NATIVE_HAS_ARM_NEON
4543 else if constexpr (N == 4)
4544 return I::from_native(vreinterpretq_u8_s32(vcvtq_s32_f32(x.value)));
4550 template<::native::isa<> Arch = NATIVE_BA
SELINE,
class T>
4551 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,float>
4552 [[nodiscard]] hint_inline
constexpr std::int32_t fcvtzs(T x)
noexcept {
4553 return fcvtzs(simd<float,1,Arch>(x)).value;
4559 template<std::
size_t N, ::native::isa<> Arch>
4560 requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
4561 [[nodiscard]] hint_inline
constexpr simd<std::uint32_t,N,Arch>
4562 fcvtzu(simd<float,N,Arch> x)
noexcept {
4563 using I = simd<std::uint32_t,N,Arch>;
4565 std::array<float,N> values{};
4566 std::array<std::uint32_t,N> result{};
4567 x.store(values.data());
4568 for (std::size_t i = 0; i < N; ++i)
4569 result[i] = detail::float_constant::fcvtzu(std::bit_cast<std::uint32_t>(values[i]));
4570 return I::load(result.data());
4572 if constexpr (N == 1)
return I(vcvts_u32_f32(x.value));
4573 else if constexpr (N == 2 || N == 3) {
4575 result.value = __builtin_bit_cast(
typename I::native_type,
4576 fcvtzu(x.to_storage()).to_native());
4579#if NATIVE_HAS_ARM_NEON
4580 else if constexpr (N == 4)
4581 return I::from_native(vreinterpretq_u8_u32(vcvtq_u32_f32(x.value)));
4587 template<::native::isa<> Arch = NATIVE_BA
SELINE,
class T>
4588 requires NATIVE_ARCH_REQUIRES(Arch) && std::same_as<T,float>
4589 [[nodiscard]] hint_inline
constexpr std::uint32_t fcvtzu(T x)
noexcept {
4590 return fcvtzu(simd<float,1,Arch>(x)).value;
4598 template <
class To, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && std::same_as<To,std::int32_t>
4601 std::array<float,N> a{}; std::array<To,N> b{}; x.store(a.data());
4602 for(std::size_t i=0;i<N;++i) b[i]=static_cast<To>(a[i]);
4606 else if constexpr (N==1)
return simd<To,N,Arch>(
static_cast<std::int32_t
>(x.value));
4611#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4614#if NATIVE_HAS_ARM_NEON
4621 template <
class To, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N> && std::same_as<To,float>
4624 std::array<std::int32_t,N> a{}; std::array<To,N> b{}; x.store(a.data());
4625 for(std::size_t i=0;i<N;++i) b[i]=static_cast<To>(a[i]);
4629 else if constexpr (N==1)
return simd<To,N,Arch>(
static_cast<float>(x.value));
4631 else if constexpr (N==4)
return simd<To,N,Arch>(_mm_cvtepi32_ps(x.value));
4632 else if constexpr (N==8)
return simd<To,N,Arch>(_mm256_cvtepi32_ps(x.value));
4634#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4635 else if constexpr (N==16)
return simd<To,N,Arch>(_mm512_cvtepi32_ps(x.value));
4637#if NATIVE_HAS_ARM_NEON
4638 else if constexpr (N==4)
return simd<To,N,Arch>(vcvtq_f32_s32(vreinterpretq_s32_u8(x.value)));
4642 namespace detail::NATIVE_BACKEND {
4643 template <
class V,std::
size_t Alignment> [[nodiscard]] hint_artificial hint_inline
constexpr hint_pure V simd_load_native(hint_noescape
float const * p)
noexcept {
4645 std::array<float,V::lanes> values{};
4646 for (std::size_t i=0;i<V::lanes;++i) values[i]=p[i];
4647 return V::from_native(__builtin_bit_cast(
typename V::native_type,values));
4649 if constexpr(V::lanes==1)
return V(*p);
4651 else if constexpr(V::lanes==4) {
if constexpr(Alignment>=16)
return V(_mm_load_ps(p));
else return V(_mm_loadu_ps(p)); }
4652 else if constexpr(V::lanes==8) {
if constexpr(Alignment>=32)
return V(_mm256_load_ps(p));
else return V(_mm256_loadu_ps(p)); }
4654#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4655 else if constexpr(V::lanes==16) {
if constexpr(Alignment>=64)
return V(_mm512_load_ps(p));
else return V(_mm512_loadu_ps(p)); }
4657#if NATIVE_HAS_ARM_NEON
4658 else if constexpr(V::lanes==4)
return V(vld1q_f32(p));
4661 template <
class V,std::
size_t Alignment> hint_artificial hint_inline
constexpr void simd_store_native(hint_noescape
float * p,V value)
noexcept {
4663 auto values=__builtin_bit_cast(std::array<float,V::lanes>,value.to_native());
4664 for (std::size_t i=0;i<V::lanes;++i) p[i]=values[i];
4667 if constexpr(V::lanes==1)*p=value.value;
4669 else if constexpr(V::lanes==4) {
if constexpr(Alignment>=16)_mm_store_ps(p,value.value);
else _mm_storeu_ps(p,value.value);}
4670 else if constexpr(V::lanes==8) {
if constexpr(Alignment>=32)_mm256_store_ps(p,value.value);
else _mm256_storeu_ps(p,value.value);}
4672#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4673 else if constexpr(V::lanes==16) {
if constexpr(Alignment>=64)_mm512_store_ps(p,value.value);
else _mm512_storeu_ps(p,value.value);}
4675#if NATIVE_HAS_ARM_NEON
4676 else if constexpr(V::lanes==4)vst1q_f32(p,value.value);
4680 namespace detail::NATIVE_BACKEND {
4683 template <::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
4684 requires NATIVE_ARCH_REQUIRES(Arch) && (std::same_as<U,float> || ::native::simd_custom_element<U>) &&
4685 (std::same_as<T,float> || ::native::simd_custom_element<T>)
4686 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd(hint_noescape U
const * p,simd_memory<A,Access> = {})
noexcept {
4687 if constexpr (::native::simd_custom_element<T>)
return simd<T,N,Arch>::template load_memory<A>(p);
4688 else if constexpr (::native::simd_custom_element<U>)
return simd<U,N,Arch>::template load_memory<A>(p).to_native();
4689 else return ::NATIVE_BACKEND_NAMESPACE::simd_load_native<simd<float,N,Arch>,A>(p);
4691 template <
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
4692 requires NATIVE_ARCH_REQUIRES(Arch) && (std::same_as<U,float> || ::native::simd_custom_element<U>) &&
4693 (std::same_as<T,float> || ::native::simd_custom_element<T>)
4694 hint_inline
constexpr void store_simd(hint_noescape U * p,simd<T,N,Arch> value,simd_memory<A,Access> = {})
noexcept {
4695 if constexpr (::native::simd_custom_element<U>) simd<U,N,Arch>(value).template store_memory<A>(p);
4697 else if constexpr (::native::simd_custom_element<T>) value.to_native().template store_memory<A>(p);
4698 else ::NATIVE_BACKEND_NAMESPACE::simd_store_native<simd<float,N,Arch>,A>(p,value);
4700 template <::native::isa<> Arch,
class T,std::
size_t N,
class U,std::
size_t A=1,simd_access Access=simd_access::ordinary>
4701 requires NATIVE_ARCH_REQUIRES(Arch) && (std::same_as<U,float> || ::native::simd_custom_element<U>) &&
4702 (std::same_as<T,float> || ::native::simd_custom_element<T>)
4703 [[nodiscard]] hint_inline
constexpr hint_pure simd<T,N,Arch>
load_simd_partial(hint_noescape U
const * p,std::size_t count,
4704 T fill=T{},simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
4705 std::array<U,N> temporary;temporary.fill(U(fill));
4706 for(std::size_t i=0;i<count;++i)temporary[i]=p[i];
4707 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T,N>(temporary.data());
4709 template <
class U,
class T,std::
size_t N,std::
size_t A=1,simd_access Access=simd_access::ordinary, ::native::isa<> Arch>
4710 requires NATIVE_ARCH_REQUIRES(Arch) && (std::same_as<U,float> || ::native::simd_custom_element<U>) &&
4711 (std::same_as<T,float> || ::native::simd_custom_element<T>)
4712 hint_inline
constexpr void store_simd_partial(hint_noescape U * p,simd<T,N,Arch> value,std::size_t count,simd_memory<A,Access> = {})
noexcept hint_diagnose_if(count > N,
"partial SIMD count exceeds the lane count") {
4713 std::array<U,N> temporary;
store_simd(temporary.data(),value);
4714 for(std::size_t i=0;i<count;++i)p[i]=temporary[i];
4716 template <::native::isa<> Arch,
class T,std::
size_t N>
requires NATIVE_ARCH_REQUIRES(Arch)
4717 [[nodiscard]] hint_inline
constexpr hint_pure
auto load_simd(std::array<T,N>
const & values)
noexcept { return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T,N>(values.data()); }
4718 template <::native::isa<> Arch,
class T,std::
size_t N>
requires NATIVE_ARCH_REQUIRES(Arch) && (N!=std::dynamic_extent)
4719 [[nodiscard]] hint_inline
constexpr hint_pure
auto load_simd(std::span<T,N> values)
noexcept {
return load_simd<Arch,std::remove_cv_t<T>,N>(values.data()); }
4723namespace NATIVE_BACKEND_NAMESPACE::native {
4724 template <
class V,std::
size_t N>
using register_memory = ::NATIVE_BACKEND_NAMESPACE::register_memory<V,N>;
4725 using fp32x1 = ::native::simd<float,1,NATIVE_DEFAULT_ARCH>;
4726#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON
4727 using fp32x4 = ::native::simd<float,4,NATIVE_DEFAULT_ARCH>;
4730 using fp32x8 = ::native::simd<float,8,NATIVE_DEFAULT_ARCH>;
4732#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4733 using fp32x16 = ::native::simd<float,16,NATIVE_DEFAULT_ARCH>;
4735 template<
class V>
concept float_register =
requires { V::lanes; V::architecture;
typename V::value_type; } &&
4736 std::same_as<typename V::value_type,float> && ::NATIVE_BACKEND_NAMESPACE::float_shape<V::lanes> && (::native::abi_lookup<V::architecture,::native::detail::raw_kernel_policies>::index == NATIVE_RAW_TARGET);
4739 template<
float_register V> [[nodiscard]] hint_inline
constexpr V min(V a, V b) {
return select(a < b, a, b); }
4740 template<
float_register V> [[nodiscard]] hint_inline
constexpr V max(V a, V b) {
return select(a > b, a, b); }
4741 template<
float_register V> [[nodiscard]] hint_inline hint_pure
float reduce_add(V v) {
4742 std::array<float, V::lanes> a; v.storeu(a.data());
float sum = 0;
4743 for (
float x : a) sum += x;
4746 template<
float_register V> [[nodiscard]] hint_inline V acos_scalar_lanes(V v) {
4747 std::array<float, V::lanes> a; v.storeu(a.data());
4748 for (
float & x : a) x = std::acos(x);
4749 return V::loadu(a.data());
4751 struct hint_target {
4752#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4753 using float_type = fp32x16;
4754 static constexpr std::size_t preferred_registers = 6;
4755#elif NATIVE_HAS_AVX2
4756 using float_type = fp32x8;
4757 static constexpr std::size_t preferred_registers = 6;
4758#elif NATIVE_HAS_ARM_NEON
4759 using float_type = fp32x4;
4761 static constexpr std::size_t preferred_registers = 15;
4763 using float_type = fp32x1;
4764 static constexpr std::size_t preferred_registers = 1;
4767 static constexpr std::size_t lanes = float_type::lanes;
4771#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON
4772#if defined(__x86_64__) || defined(_M_X64)
4773#elif defined(__aarch64__) || defined(_M_ARM64)
4777 namespace detail::NATIVE_BACKEND {
4778 template<
class T>
concept short_element = std::same_as<T,float> ||
4779 std::same_as<T,std::int32_t> || std::same_as<T,std::uint32_t> || std::same_as<T,mask32>;
4780 template<
class T>
using short_lane = std::conditional_t<simd_mask_element<T>,std::uint32_t,T>;
4781 template<
class T>
using short_native = T __attribute__((ext_vector_type(4)));
4782 template<
class T>
constexpr short_lane<T> short_word(T value)
noexcept {
4783 if constexpr(simd_mask_element<T>)
return value.to_bits();
4795 template<::NATIVE_BACKEND_NAMESPACE::
short_element T,std::
size_t N,::native::isa<> Arch>
4799 struct alignas(typename simd<T,4,Arch>::native_type)
simd<T,N,Arch> : detail::swizzle_access<T,N,Arch> {
4802 using storage_type=simd<T,4,Arch>;
4803 using native_type=::NATIVE_BACKEND_NAMESPACE::short_native<::NATIVE_BACKEND_NAMESPACE::short_lane<T>>;
4804 using register_type=simd;
4805 using unsigned_register_tag=void;
4806 using bits_type=simd<std::uint32_t,N,Arch>;
4807 using vector_mask_type=simd<mask32,N,Arch>;
4808 using mask_type=std::conditional_t<simd_mask_element<T>,simd,
4809 std::conditional_t<bool(NATIVE_HAS_AVX512VL),predicate<N,Arch>,vector_mask_type>>;
4810 using mask=mask_type;
4811 using predicate_type=predicate<N,Arch>;
4812 template<
class U>
using rebind=simd<U,N,Arch>;
4813 static constexpr std::size_t lanes=N;
4814 static constexpr std::size_t storage_lanes=4;
4815 static constexpr bool compact=
false;
4816 static constexpr std::uint64_t lane_mask=(std::uint64_t(1)<<N)-1;
4820 hint_inline
constexpr simd() =
default;
4826 hint_inline
constexpr simd(T x) noexcept : value{::NATIVE_BACKEND_NAMESPACE::short_word(x),::NATIVE_BACKEND_NAMESPACE::short_word(x),
4827 N==3?::NATIVE_BACKEND_NAMESPACE::short_word(x):NATIVE_BACKEND_NAMESPACE::short_lane<T>(0),0} {}
4829 explicit hint_inline
constexpr simd(
bool x)
noexcept requires simd_mask_element<T> :
simd(T(x)) {}
4831 hint_inline
constexpr simd(native_type x) noexcept
4832 : value(__builtin_shufflevector(x,native_type{},0,1,N==3?2:4,4)) {}
4834 template<
class... X>
requires(
sizeof...(X)==N) && (std::convertible_to<X,T> && ...)
4835 hint_inline constexpr
simd(X... x) noexcept((noexcept(static_cast<T>(x)) && ...))
4836 : value{::NATIVE_BACKEND_NAMESPACE::short_word(
static_cast<T
>(x))...} {}
4838 hint_inline
constexpr simd(std::array<T,N>
const & values) noexcept :
simd(
load(values.data())) {}
4841 [[nodiscard]] hint_inline
constexpr native_type
to_native() const noexcept {
return value; }
4843 [[nodiscard]] hint_inline
constexpr operator native_type() const noexcept requires(!simd_mask_element<T>) {
return value; }
4846 if constexpr(simd_mask_element<T>)
return from_storage(storage_type::from_native(__builtin_bit_cast(
typename storage_type::native_type,x)));
4847 else return simd(x);
4852 [[nodiscard]] hint_inline
constexpr storage_type
to_storage() const noexcept {
4853 if constexpr(simd_mask_element<T>)
return storage_type::unsafe_from_native(__builtin_bit_cast(
typename storage_type::native_type,value));
4854 else return storage_type::from_native(__builtin_bit_cast(
typename storage_type::native_type,value));
4858 return simd(__builtin_bit_cast(native_type,x.to_native()));
4863 template<std::
size_t Alignment=1>
4866 native_type words{};
4867 for (std::size_t i=0;i<N;++i) words[i]=::NATIVE_BACKEND_NAMESPACE::short_word(p[i]);
4868 return simd(unchecked{},words);
4870#if defined(__x86_64__) || defined(_M_X64)
4871 if constexpr(bool(NATIVE_HAS_AVX2)) {
4872 if constexpr(N==2)
return simd(unchecked{},__builtin_bit_cast(native_type,_mm_loadl_epi64(
reinterpret_cast<__m128i
const *
>(p))));
4873 else if constexpr(bool(NATIVE_HAS_AVX512VL)) {
4874 if constexpr(std::same_as<T,float>)
return simd(unchecked{},__builtin_bit_cast(native_type,_mm_maskz_loadu_ps(7,p)));
4875 else return simd(unchecked{},__builtin_bit_cast(native_type,_mm_maskz_loadu_epi32(7,p)));
4877 auto active=_mm_set_epi32(0,-1,-1,-1);
4878 if constexpr(std::same_as<T,float>)
return simd(unchecked{},__builtin_bit_cast(native_type,_mm_maskload_ps(p,active)));
4879 else return simd(unchecked{},__builtin_bit_cast(native_type,_mm_maskload_epi32(
reinterpret_cast<int const *
>(p),active)));
4882#elif defined(__aarch64__) || defined(_M_ARM64)
4883 if constexpr((::native::arm_feature::neon <= Arch)) {
4884 if constexpr(std::same_as<T,float>) {
4885 auto x=vcombine_f32(vld1_f32(p),vdup_n_f32(0.f));
4886 if constexpr(N==3) x=vld1q_lane_f32(p+2,x,2);
4887 return simd(unchecked{},__builtin_bit_cast(native_type,x));
4889 auto q=
reinterpret_cast<std::uint32_t
const *
>(p);
4890 auto x=vcombine_u32(vld1_u32(q),vdup_n_u32(0));
4891 if constexpr(N==3) x=vld1q_lane_u32(q+2,x,2);
4892 return simd(unchecked{},__builtin_bit_cast(native_type,x));
4898 template<std::
size_t Alignment=1>
4901 for (std::size_t i=0;i<N;++i) {
4902 if constexpr(simd_mask_element<T>) p[i]=T::from_bits(value[i]);
4907#if defined(__x86_64__) || defined(_M_X64)
4908 if constexpr(bool(NATIVE_HAS_AVX2)) {
4909 if constexpr(N==2) _mm_storel_epi64(
reinterpret_cast<__m128i *
>(p),__builtin_bit_cast(__m128i,value));
4910 else if constexpr(bool(NATIVE_HAS_AVX512VL)) {
4911 if constexpr(std::same_as<T,float>) _mm_mask_storeu_ps(p,7,__builtin_bit_cast(__m128,value));
4912 else _mm_mask_storeu_epi32(p,7,__builtin_bit_cast(__m128i,value));
4914 auto active=_mm_set_epi32(0,-1,-1,-1);
4915 if constexpr(std::same_as<T,float>) _mm_maskstore_ps(p,active,__builtin_bit_cast(__m128,value));
4916 else _mm_maskstore_epi32(
reinterpret_cast<int *
>(p),active,__builtin_bit_cast(__m128i,value));
4919#elif defined(__aarch64__) || defined(_M_ARM64)
4920 if constexpr((::native::arm_feature::neon <= Arch)) {
4921 if constexpr(std::same_as<T,float>) {
4922 auto x=__builtin_bit_cast(float32x4_t,value);vst1_f32(p,vget_low_f32(x));
4923 if constexpr(N==3) vst1q_lane_f32(p+2,x,2);
4925 auto q=
reinterpret_cast<std::uint32_t *
>(p);
auto x=__builtin_bit_cast(uint32x4_t,value);
4926 vst1_u32(q,vget_low_u32(x));
4927 if constexpr(N==3) vst1q_lane_u32(q+2,x,2);
4941 [[nodiscard]]
static hint_inline
constexpr simd load_partial(T
const * p,std::size_t n,T fill={})
noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
4942 std::array<T,N> values;values.fill(fill);
4943 if consteval {
for (std::size_t i=0;i<n;++i) values[i]=p[i]; }
4944 else {
if(n) std::memcpy(values.data(),p,n*
sizeof(T)); }
4945 return load(values.data());
4948 hint_inline
constexpr void store_partial(T * p,std::size_t n)
const noexcept hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
4949 std::array<T,N> values;
store(values.data());
4950 if consteval {
for (std::size_t i=0;i<n;++i) p[i]=values[i]; }
4951 else {
if(n) std::memcpy(p,values.data(),n*
sizeof(T)); }
4954 [[nodiscard]] hint_inline
constexpr bits_type
bits() const noexcept requires
std::same_as<T,
float> {
4958 [[nodiscard]] hint_inline
constexpr bits_type
to_bits() const noexcept requires
std::same_as<T,
float> {
return bits(); }
4960 [[nodiscard]]
static hint_inline
constexpr simd from_bits(bits_type x)
noexcept requires std::same_as<T,float> {
4961 return from_storage(storage_type::from_bits(x.to_storage()));
4964 [[nodiscard]]
static hint_inline
constexpr simd from_bits(std::uint32_t x)
noexcept requires std::same_as<T,float> {
return from_bits(bits_type(x)); }
4966 [[nodiscard]]
static hint_inline
constexpr simd from_float(
float x)
noexcept requires std::same_as<T,float> {
return simd(x); }
4970 [[nodiscard]]
static hint_inline
constexpr simd load_bits(std::uint32_t
const * p)
noexcept requires std::same_as<T,float> {
return from_bits(bits_type::load(p)); }
4972 hint_inline
constexpr void store_bits(std::uint32_t * p)
const noexcept requires std::same_as<T,float> {
bits().store(p); }
4974 [[nodiscard]]
static hint_inline
constexpr simd load_bits_partial(std::uint32_t
const * p,std::size_t n,std::uint32_t fill=0) noexcept requires
std::same_as<T,
float> hint_diagnose_if(n >
simd::lanes,
"partial SIMD count exceeds the lane count") {
return from_bits(bits_type::load_partial(p,n,fill)); }
4976 hint_inline
constexpr void store_bits_partial(std::uint32_t * p,std::size_t n)
const noexcept requires std::same_as<T,float> hint_diagnose_if(n > simd::lanes,
"partial SIMD count exceeds the lane count") {
bits().store_partial(p,n); }
4980 [[nodiscard]] hint_inline
constexpr std::uint64_t
to_bitset() const noexcept requires simd_mask_element<T> {
return to_storage().to_bitset()&lane_mask; }
4982 [[nodiscard]]
friend hint_inline
constexpr bool any(
simd x)
noexcept requires simd_mask_element<T> {
return x.to_bitset()!=0; }
4984 [[nodiscard]]
friend hint_inline
constexpr bool all(
simd x)
noexcept requires simd_mask_element<T> {
return x.to_bitset()==lane_mask; }
4986 [[nodiscard]]
friend hint_inline
constexpr bool none(
simd x)
noexcept requires simd_mask_element<T> {
return !
any(x); }
4990 [[nodiscard]]
friend hint_inline
constexpr simd operator+(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return clean(a.to_storage()+b.to_storage()); }
4993 [[nodiscard]]
friend hint_inline
constexpr simd operator-(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return clean(a.to_storage()-b.to_storage()); }
4996 [[nodiscard]]
friend hint_inline
constexpr simd operator*(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return clean(a.to_storage()*b.to_storage()); }
5000 auto padded=__builtin_shufflevector(b.value,native_type{1.f,1.f,1.f,1.f},0,1,N==3?2:4,4);
5001 auto divisor=storage_type::from_native(__builtin_bit_cast(
typename storage_type::native_type,padded));
5002 return clean(a.to_storage()/divisor);
5015 [[nodiscard]]
friend hint_inline
constexpr simd operator!(
simd a)
noexcept requires simd_mask_element<T> {
return ~a; }
5017 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator==(
simd a,
simd b)
noexcept {
return comparison(a.to_storage()==b.to_storage()); }
5019 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator!=(
simd a,
simd b)
noexcept {
return ~(a==b); }
5021 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator<(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return comparison(a.to_storage()<b.to_storage()); }
5023 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator>(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return comparison(a.to_storage()>b.to_storage()); }
5025 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator<=(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return comparison(a.to_storage()<=b.to_storage()); }
5027 [[nodiscard]]
friend hint_inline
constexpr mask_type
operator>=(
simd a,
simd b)
noexcept requires(!simd_mask_element<T>) {
return comparison(a.to_storage()>=b.to_storage()); }
5029 template<
class M>
requires(std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
5031 using FM=
typename storage_type::mask_type;
5032 return clean(
select(FM::from_bitset(mask.to_bitset()),a.to_storage(),b.to_storage()));
5035 [[nodiscard]]
friend hint_inline
constexpr simd fma(
simd a,
simd b,
simd c)
noexcept requires std::same_as<T,float> {
return clean(
fma(a.to_storage(),b.to_storage(),c.to_storage())); }
5037 [[nodiscard]]
friend hint_inline
constexpr simd sqrt(
simd a)
noexcept requires std::same_as<T,float> {
return clean(
sqrt(a.to_storage())); }
5043 template<std::
size_t K>
requires(K<32)
5046 template<std::
size_t K>
requires(K<32)
5049 template<
unsigned K>
requires(K<32) && simd_integer_element<T>
5050 [[nodiscard]] hint_inline
constexpr simd left() const noexcept {
return *this<<imm<K>; }
5052 template<
unsigned K>
requires(K<32) && simd_integer_element<T>
5064 template<simd_
integer_element U>
friend simd operator<<(
simd,U)
requires simd_integer_element<T> =
delete;
5066 template<simd_
integer_element U>
friend simd operator>>(
simd,U)
requires simd_integer_element<T> =
delete;
5068 template<simd_
integer_element U>
friend simd operator/(
simd,U)
requires simd_integer_element<T> =
delete;
5070 template<simd_
integer_element U>
friend simd operator/(U,
simd)
requires simd_integer_element<T> =
delete;
5072 template<simd_
integer_element U>
friend simd operator%(
simd,U)
requires simd_integer_element<T> =
delete;
5074 template<simd_
integer_element U>
friend simd operator%(U,
simd)
requires simd_integer_element<T> =
delete;
5076 hint_inline
constexpr simd &
operator+=(
simd b)
noexcept requires(!simd_mask_element<T>) {
return *
this=*
this+b; }
5078 hint_inline
constexpr simd &
operator-=(
simd b)
noexcept requires(!simd_mask_element<T>) {
return *
this=*
this-b; }
5080 hint_inline
constexpr simd &
operator*=(
simd b)
noexcept requires(!simd_mask_element<T>) {
return *
this=*
this*b; }
5082 hint_inline
constexpr simd &
operator/=(
simd b)
noexcept requires std::same_as<T,float> {
return *
this=*
this/b; }
5084 hint_inline
constexpr simd &
operator&=(
simd b)
noexcept requires(!std::same_as<T,float>) {
return *
this=*
this&b; }
5086 hint_inline
constexpr simd &
operator|=(
simd b)
noexcept requires(!std::same_as<T,float>) {
return *
this=*
this|b; }
5088 hint_inline
constexpr simd &
operator^=(
simd b)
noexcept requires(!std::same_as<T,float>) {
return *
this=*
this^b; }
5090 struct unchecked {};
5091 hint_inline
constexpr simd(unchecked,native_type x) noexcept : value(x) {}
5092 [[nodiscard]]
static hint_inline
constexpr simd clean(storage_type x)
noexcept {
return simd(unchecked{},__builtin_bit_cast(native_type,x.to_native())); }
5093 template<
class M> [[nodiscard]]
static hint_inline
constexpr mask_type comparison(M x)
noexcept {
5094 if constexpr(mask_type::compact)
return mask_type::from_native(x.to_native());
5095 else return mask_type::from_storage(x);
5100 template<simd_
integer_element T,std::
size_t N,::native::isa<> Arch>
5101 requires NATIVE_ARCH_REQUIRES(Arch) &&(N==2 || N==3) && (
sizeof(T)==4)
5102 [[nodiscard]] hint_inline
constexpr simd<T,N,Arch> bit_select(
simd<T,N,Arch> bits,
simd<T,N,Arch> a,
simd<T,N,Arch> b)
noexcept {
return (bits&a)|(~bits&b); }
5104 template<simd_
integer_element T,std::
size_t N,::native::isa<> Arch,
class M>
5105 requires NATIVE_ARCH_REQUIRES(Arch) &&(N==2 || N==3) && (
sizeof(T)==4) &&
5106 (std::same_as<M,typename simd<T,N,Arch>::mask> || std::same_as<M,simd<mask32,N,Arch>>)
5107 [[nodiscard]] hint_inline
constexpr simd<T,N,Arch> masked_add(M m,
simd<T,N,Arch> prior,
simd<T,N,Arch> a,
simd<T,N,Arch> b)
noexcept {
return select(m,a+b,prior); }
5109 template<simd_
integer_element T,std::
size_t N,::native::isa<> Arch,
class M>
5110 requires NATIVE_ARCH_REQUIRES(Arch) &&(N==2 || N==3) && (
sizeof(T)==4) &&
5111 (std::same_as<M,typename simd<T,N,Arch>::mask> || std::same_as<M,simd<mask32,N,Arch>>)
5112 [[nodiscard]] hint_inline
constexpr simd<T,N,Arch> masked_sub(M m,
simd<T,N,Arch> prior,
simd<T,N,Arch> a,
simd<T,N,Arch> b)
noexcept {
return select(m,a-b,prior); }
5114 template<simd_
integer_element T,std::
size_t N,::native::isa<> Arch,
class M>
5115 requires NATIVE_ARCH_REQUIRES(Arch) &&(N==2 || N==3) && (
sizeof(T)==4) &&
5116 (std::same_as<M,typename simd<T,N,Arch>::mask> || std::same_as<M,simd<mask32,N,Arch>>)
5117 [[nodiscard]] hint_inline
constexpr simd<T,N,Arch> masked_mul(M m,
simd<T,N,Arch> prior,
simd<T,N,Arch> a,
simd<T,N,Arch> b)
noexcept {
return select(m,a*b,prior); }
5122 namespace detail::NATIVE_BACKEND {
5123 enum class rounding_direction { down, up, zero };
5125 template<rounding_direction Direction,std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch)
5126 hint_inline
constexpr simd<float,N,Arch> round_integral(simd<float,N,Arch> x)
noexcept {
5127 using V = simd<float,N,Arch>;
5129 if constexpr(Direction==rounding_direction::down) return ::native::detail::float_constant::map(::native::detail::float_constant::floor,x);
5130 else if constexpr(Direction==rounding_direction::up) return ::native::detail::float_constant::map(::native::detail::float_constant::ceil,x);
5131 else return ::native::detail::float_constant::map(::native::detail::float_constant::trunc,x);
5133 if constexpr (N == 2 || N == 3) {
5134 return V::from_storage(round_integral<Direction>(x.to_storage()));
5137 constexpr int mode = (Direction == rounding_direction::down ? _MM_FROUND_TO_NEG_INF :
5138 Direction == rounding_direction::up ? _MM_FROUND_TO_POS_INF : _MM_FROUND_TO_ZERO) | _MM_FROUND_NO_EXC;
5139 if constexpr (N == 1)
5140 return V::from_native(_mm_cvtss_f32(_mm_round_ss(_mm_setzero_ps(),_mm_set_ss(x.to_native()),mode)));
5141 else if constexpr (N == 4)
return V::from_native(_mm_round_ps(x.to_native(),mode));
5142 else if constexpr (N == 8)
return V::from_native(_mm256_round_ps(x.to_native(),mode));
5143#if NATIVE_HAS_AVX512F
5144 else if constexpr (N == 16)
return V::from_native(_mm512_roundscale_ps(x.to_native(),mode));
5146#elif NATIVE_HAS_ARM_NEON
5147 if constexpr (N == 1) {
5148 auto a = vdup_n_f32(x.to_native());
5149 if constexpr (Direction == rounding_direction::down)
return V::from_native(vget_lane_f32(vrndm_f32(a),0));
5150 else if constexpr (Direction == rounding_direction::up)
return V::from_native(vget_lane_f32(vrndp_f32(a),0));
5151 else return V::from_native(vget_lane_f32(vrnd_f32(a),0));
5153 if constexpr (Direction == rounding_direction::down)
return V::from_native(__builtin_elementwise_floor(x.to_native()));
5154 else if constexpr (Direction == rounding_direction::up)
return V::from_native(vrndpq_f32(x.to_native()));
5155 else return V::from_native(vrndq_f32(x.to_native()));
5158 if constexpr (Direction == rounding_direction::down)
return V::from_native(std::floor(x.to_native()));
5159 else if constexpr (Direction == rounding_direction::up)
return V::from_native(std::ceil(x.to_native()));
5160 else return V::from_native(std::trunc(x.to_native()));
5174 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5176 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::down>(x);
5180 template<std::
size_t N,std::
size_t M, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5181 [[nodiscard]] hint_inline
constexpr std::array<simd<float,N,Arch>,M>
floor(std::array<
simd<float,N,Arch>,M>
const & input)
noexcept {
5182 auto const & [...x] = input;
5183 return {{
floor(x)...}};
5190 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5192 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::up>(x);
5196 template<std::
size_t N,std::
size_t M, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5197 [[nodiscard]] hint_inline
constexpr std::array<simd<float,N,Arch>,M>
ceil(std::array<
simd<float,N,Arch>,M>
const & input)
noexcept {
5198 auto const & [...x] = input;
5199 return {{
ceil(x)...}};
5206 template<std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5208 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::zero>(x);
5212 template<std::
size_t N,std::
size_t M, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5213 [[nodiscard]] hint_inline
constexpr std::array<simd<float,N,Arch>,M>
trunc(std::array<
simd<float,N,Arch>,M>
const & input)
noexcept {
5214 auto const & [...x] = input;
5215 return {{
trunc(x)...}};
5219namespace native::detail::NATIVE_BACKEND {
5220 template<std::
size_t N,
class M>
5221 hint_inline std::uint32_t compaction_mask_bits(M mask)
noexcept {
5223 if constexpr (N > 1 && !M::compact) {
5224 if constexpr (
sizeof(M) == 32)
5225 return std::uint32_t(_mm256_movemask_ps(__builtin_bit_cast(__m256,
mask.to_native())));
5226 else return std::uint32_t(_mm_movemask_ps(__builtin_bit_cast(__m128,
mask.to_native()))) & ((1u << N) - 1);
5228#elif NATIVE_HAS_ARM_NEON
5229 if constexpr (N > 1) {
5230 constexpr std::array<std::uint32_t,4> weights{1,2,4,8};
5231 return vaddvq_u32(vandq_u32(__builtin_bit_cast(uint32x4_t,
mask.to_native()),vld1q_u32(weights.data()))) & ((1u << N) - 1);
5234 return std::uint32_t(
mask.to_bitset()) & ((1u << N) - 1);
5239 template<
bool Expand, std::
size_t W
idth>
inline constexpr auto compaction_indices = [] {
5240 constexpr auto bytes = Width == 4 ? 16 : 8;
5241 std::array<std::array<std::uint8_t,bytes>,std::size_t(1) << Width> table{};
5242 for (std::size_t mask = 0;
mask != table.size(); ++
mask) {
5243 std::size_t packed = 0;
5244 for (std::size_t lane = 0; lane != Width; ++lane)
if (mask & (std::size_t(1) << lane)) {
5245 auto destination = Expand ? lane : packed;
5246 auto source = Expand ? packed : lane;
5247 if constexpr (Width == 4)
5248 for (std::size_t
byte = 0;
byte != 4; ++byte)
5249 table[mask][4 * destination +
byte] = std::uint8_t(4 * source +
byte);
5250 else table[
mask][destination] = std::uint8_t(source);
5257 template<
bool Expand,
class V>
5258 hint_inline V compact_register(std::uint32_t mask, V input, V prior)
noexcept {
5259 if constexpr (V::lanes == 1)
return mask ? input : prior;
5261#if NATIVE_HAS_AVX512F
5262 if constexpr (
sizeof(V) == 64) {
5263 auto x = __builtin_bit_cast(__m512i,input), merge = __builtin_bit_cast(__m512i,prior);
5264 if constexpr (Expand)
return __builtin_bit_cast(V,_mm512_mask_expand_epi32(merge, __mmask16(mask), x));
5265 else return __builtin_bit_cast(V,_mm512_mask_compress_epi32(merge, __mmask16(mask), x));
5267#if NATIVE_HAS_AVX512VL
5268 else if constexpr (
sizeof(V) == 32) {
5269 auto x = __builtin_bit_cast(__m256i,input), merge = __builtin_bit_cast(__m256i,prior);
5270 if constexpr (Expand)
return __builtin_bit_cast(V,_mm256_mask_expand_epi32(merge, __mmask8(mask), x));
5271 else return __builtin_bit_cast(V,_mm256_mask_compress_epi32(merge, __mmask8(mask), x));
5273 auto x = __builtin_bit_cast(__m128i,input), merge = __builtin_bit_cast(__m128i,prior);
5274 if constexpr (Expand)
return __builtin_bit_cast(V,_mm_mask_expand_epi32(merge, __mmask8(mask), x));
5275 else return __builtin_bit_cast(V,_mm_mask_compress_epi32(merge, __mmask8(mask), x));
5281#if (!NATIVE_HAS_AVX512F || !NATIVE_HAS_AVX512VL) && (NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON)
5285 if constexpr (
sizeof(V) == 32) {
5286 auto const & row = compaction_indices<Expand,8>[
mask];
5287 auto indices = _mm256_cvtepu8_epi32(_mm_loadl_epi64(
reinterpret_cast<__m128i
const *
>(row.data())));
5288 permuted = __builtin_bit_cast(V,_mm256_permutevar8x32_epi32(__builtin_bit_cast(__m256i,input), indices));
5290 auto const & row = compaction_indices<Expand,4>[
mask];
5291 auto indices = _mm_loadu_si128(
reinterpret_cast<__m128i
const *
>(row.data()));
5292 permuted = __builtin_bit_cast(V,_mm_shuffle_epi8(__builtin_bit_cast(__m128i,input), indices));
5295 auto const & row = compaction_indices<Expand,4>[
mask];
5296 permuted = __builtin_bit_cast(V,vqtbl1q_u8(__builtin_bit_cast(uint8x16_t,input), vld1q_u8(row.data())));
5301 if constexpr (
sizeof(V) == 32) {
5303 if constexpr (Expand) {
5304 auto bits = _mm256_setr_epi32(1,2,4,8,16,32,64,128);
5305 live = _mm256_cmpeq_epi32(_mm256_and_si256(_mm256_set1_epi32(
int(mask)),bits),bits);
5306 }
else live = _mm256_cmpgt_epi32(_mm256_set1_epi32(std::popcount(mask)),_mm256_setr_epi32(0,1,2,3,4,5,6,7));
5307 return __builtin_bit_cast(V,_mm256_blendv_epi8(__builtin_bit_cast(__m256i,prior),__builtin_bit_cast(__m256i,permuted),live));
5310 if constexpr (Expand) {
5311 auto bits = _mm_setr_epi32(1,2,4,8);
5312 live = _mm_cmpeq_epi32(_mm_and_si128(_mm_set1_epi32(
int(mask)),bits),bits);
5313 }
else live = _mm_cmpgt_epi32(_mm_set1_epi32(std::popcount(mask)),_mm_setr_epi32(0,1,2,3));
5314 return __builtin_bit_cast(V,_mm_blendv_epi8(__builtin_bit_cast(__m128i,prior),__builtin_bit_cast(__m128i,permuted),live));
5318 if constexpr (Expand) {
5319 constexpr std::array<std::uint32_t,4> weights{1,2,4,8};
5320 live = vtstq_u32(vdupq_n_u32(mask),vld1q_u32(weights.data()));
5322 constexpr std::array<std::uint32_t,4> lanes{0,1,2,3};
5323 live = vcltq_u32(vld1q_u32(lanes.data()),vdupq_n_u32(std::uint32_t(std::popcount(mask))));
5325 return __builtin_bit_cast(V,vbslq_u8(vreinterpretq_u8_u32(live),__builtin_bit_cast(uint8x16_t,permuted),__builtin_bit_cast(uint8x16_t,prior)));
5339 template<
class T, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) &&
5340 (std::same_as<T,float> || std::same_as<T,std::int32_t> || std::same_as<T,std::uint32_t>) &&
5341 ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5345 std::array<T,N> input{},output{}; value.store(input.data()); output.fill(fill);
5346 auto bits=
mask.to_bitset(); std::size_t count=0;
5347 for(std::size_t i=0;i<N;++i)
if((bits>>i)&1) output[count++]=input[i];
5351 auto bits = detail::NATIVE_BACKEND::compaction_mask_bits<N>(
mask);
5354 V prior = __builtin_bit_cast(V,U(std::bit_cast<std::uint32_t>(fill)));
5355 return {detail::NATIVE_BACKEND::compact_register<false>(bits, value, prior), std::size_t(std::popcount(bits))};
5361 template<
class T, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) &&
5362 (std::same_as<T,float> || std::same_as<T,std::int32_t> || std::same_as<T,std::uint32_t>) &&
5363 ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5368 std::array<T,N> input{},output{}; packed.store(input.data()); prior.store(output.data());
5369 auto bits=
mask.to_bitset(); std::size_t count=0;
5370 for(std::size_t i=0;i<N;++i)
if((bits>>i)&1) output[i]=input[count++];
5373 auto bits = detail::NATIVE_BACKEND::compaction_mask_bits<N>(
mask);
5374 auto result = detail::NATIVE_BACKEND::compact_register<true>(bits, packed, prior);
5383 template<
class T, std::
size_t N, ::native::isa<> Arch>
requires NATIVE_ARCH_REQUIRES(Arch) &&
5384 (std::same_as<T,float> || std::same_as<T,std::int32_t> || std::same_as<T,std::uint32_t>) &&
5385 ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5386 hint_inline
constexpr std::size_t
compress_store(T * destination, std::size_t capacity,
5389 std::array<T,N> input{}; value.store(input.data());
5390 auto bits=
mask.to_bitset(); std::size_t written=0;
5391 for(std::size_t i=0;i<N && written<capacity;++i)
5392 if((bits>>i)&1) destination[written++]=input[i];
5395 auto bits = detail::NATIVE_BACKEND::compaction_mask_bits<N>(
mask);
5396 auto selected = std::size_t(std::popcount(bits));
5397 auto written = capacity < selected ? capacity : selected;
5398 if (!written)
return 0;
5399#if NATIVE_HAS_AVX512F
5400 if constexpr (N > 1 && (
sizeof(value) == 64 || NATIVE_HAS_AVX512VL)) {
5403 auto prefix = (std::uint32_t(1) << written) - 1;
5404 if constexpr (
sizeof(value) == 64) {
5405 auto packed = _mm512_maskz_compress_epi32(__mmask16(bits),__builtin_bit_cast(__m512i,value));
5406 _mm512_mask_storeu_epi32(destination,__mmask16(prefix),packed);
5407 }
else if constexpr (
sizeof(value) == 32) {
5408 auto packed = _mm256_maskz_compress_epi32(__mmask8(bits),__builtin_bit_cast(__m256i,value));
5409 _mm256_mask_storeu_epi32(destination,__mmask8(prefix),packed);
5411 auto packed = _mm_maskz_compress_epi32(__mmask8(bits),__builtin_bit_cast(__m128i,value));
5412 _mm_mask_storeu_epi32(destination,__mmask8(prefix),packed);
5417 std::array<T,N> packed;
5419 std::memcpy(destination, packed.data(), written *
sizeof(T));
5424#if NATIVE_HAS_WASM_SIMD128 || defined(NATIVE_DOXYGEN)
5429 concept wasm_number =
5430 simd_integer_element<T> || std::same_as<T, float> || std::same_as<T, double>;
5432 using wasm_word = std::conditional_t<
5433 sizeof(T) == 1, std::uint8_t,
5434 std::conditional_t<
sizeof(T) == 2, std::uint16_t,
5435 std::conditional_t<
sizeof(T) == 4, std::uint32_t, std::uint64_t>>>;
5438 std::conditional_t<
sizeof(T) == 4, constexpr_float::binary32, constexpr_float::binary64>;
5441 constexpr auto wasm_lanes(V v)
noexcept {
5442 std::array<typename V::value_type, V::lanes> a{};
5447 template<
class V,
class F,
class... W>
5448 constexpr V wasm_map(F f, V v, W... w)
noexcept {
5449 auto inputs = std::tuple{wasm_lanes(v), wasm_lanes(w)...};
5450 std::array<typename V::value_type, V::lanes> r{};
5451 for (std::size_t i = 0; i < V::lanes; ++i) {
5452 r[i] = std::apply([&](
auto const &... a) {
return f(a[i]...); }, inputs);
5454 return V::load(r.data());
5457 template<
class T, std::
size_t N, isa<> A>
5458 requires ordinary_simd_element<T> && (A.has(wasm_feature::simd128)) &&
5459 (wasm_number<T> || simd_mask_element<T>) && (
sizeof(T) * N == 16)
5460 struct value_traits<simd<T, N, A>> {
5461 static constexpr isa<> value = A;
5462 static constexpr bool known =
true;
5463 static constexpr bool aggregate_default =
false;
5468 template<
class U, std::
size_t N, isa<> A>
5469 requires(A.has(wasm_feature::simd128)) && (
sizeof(U) * N == 16)
5472 using native_type = v128_t;
5473 using mask_type =
simd;
5477 static constexpr auto architecture = A;
5478 static constexpr std::size_t lanes = N;
5479 static constexpr bool compact =
false;
5482 native_type value_{};
5486 constexpr simd() noexcept = default;
5489 explicit hint_inline constexpr
simd(
bool x) noexcept
5493 [[nodiscard]] hint_inline
constexpr native_type
to_native() const noexcept {
5507 auto words = __builtin_bit_cast(std::array<U, N>, x);
5508 for (
auto & w : words) {
5509 w = w ? U(~U(0)) : U(0);
5513 if constexpr (
sizeof(U) == 1) {
5515 }
else if constexpr (
sizeof(U) == 2) {
5517 }
else if constexpr (
sizeof(U) == 4) {
5519 }
else if constexpr (
sizeof(U) == 8) {
5527 std::array<U, N> a{};
5528 for (std::size_t i = 0; i < N; ++i) {
5529 a[i] = ((
bits >> i) & 1) ? U(~U(0)) : U(0);
5535 [[nodiscard]] hint_inline
constexpr std::uint64_t
to_bitset() const noexcept {
5537 auto a = __builtin_bit_cast(std::array<U, N>, value_);
5538 std::uint64_t r = 0;
5539 for (std::size_t i = 0; i < N; ++i) {
5540 r |= std::uint64_t(a[i] != 0) << i;
5544 if constexpr (
sizeof(U) == 1) {
5545 return wasm_i8x16_bitmask(value_);
5546 }
else if constexpr (
sizeof(U) == 2) {
5547 return wasm_i16x8_bitmask(value_);
5548 }
else if constexpr (
sizeof(U) == 4) {
5549 return wasm_i32x4_bitmask(value_);
5550 }
else if constexpr (
sizeof(U) == 8) {
5551 return wasm_i64x2_bitmask(value_);
5557 [[nodiscard]] hint_inline
constexpr std::uint64_t
bits() const noexcept {
5562 [[nodiscard]]
static hint_inline
constexpr simd load(value_type
const * p)
noexcept {
5563 std::array<U, N> a{};
5564 for (std::size_t i = 0; i < N; ++i) {
5565 a[i] = p[i].to_bits();
5571 hint_inline
constexpr void store(value_type * p)
const noexcept {
5572 auto a = __builtin_bit_cast(std::array<U, N>, value_);
5573 for (std::size_t i = 0; i < N; ++i) {
5579 template<std::
size_t Align>
5580 [[nodiscard]]
static hint_inline
constexpr simd load_memory(value_type
const * p)
noexcept {
5585 template<std::
size_t Align>
5591 [[nodiscard]]
friend hint_inline
constexpr bool any(
simd v)
noexcept {
5593 return v.to_bitset() != 0;
5595 return wasm_v128_any_true(v.value_);
5600 [[nodiscard]]
friend hint_inline
constexpr bool all(
simd v)
noexcept {
5602 return v.to_bitset() == ((std::uint64_t{1} << N) - 1);
5604 if constexpr (
sizeof(U) == 1) {
5605 return wasm_i8x16_all_true(v.value_);
5606 }
else if constexpr (
sizeof(U) == 2) {
5607 return wasm_i16x8_all_true(v.value_);
5608 }
else if constexpr (
sizeof(U) == 4) {
5609 return wasm_i32x4_all_true(v.value_);
5611 return wasm_i64x2_all_true(v.value_);
5617 [[nodiscard]]
friend hint_inline
constexpr bool none(
simd v)
noexcept {
5624 return from_bitset(a.to_bitset() & b.to_bitset());
5632 return *
this = *
this & b;
5638 return from_bitset(a.to_bitset() | b.to_bitset());
5646 return *
this = *
this | b;
5652 return from_bitset(a.to_bitset() ^ b.to_bitset());
5660 return *
this = *
this ^ b;
5689 return (m & a) | (~m & b);
5694 template<detail::wasm_number T, std::
size_t N, isa<> A>
5695 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
5696 struct alignas(16) simd<T, N, A> {
5697 using value_type = T;
5698 using native_type = v128_t;
5699 using word_type = detail::wasm_word<T>;
5700 using mask_type = simd<mask_lane<word_type>, N, A>;
5701 using mask = mask_type;
5702 using vector_mask_type = mask_type;
5703 using bits_type = simd<word_type, N, A>;
5704 using register_type = simd;
5706 using rebind = simd<U, N, A>;
5707 static constexpr auto architecture = A;
5708 static constexpr std::size_t lanes = N;
5711 native_type value_{};
5715 constexpr simd() noexcept = default;
5718 hint_inline constexpr
simd(T x) noexcept
5719 requires
std::is_floating_point_v<T>
5722 std::array<T, N> a{};
5724 value_ = __builtin_bit_cast(native_type, a);
5726 if constexpr (std::same_as<T, float>) {
5727 value_ = wasm_f32x4_splat(x);
5729 value_ = wasm_f64x2_splat(x);
5735 template<simd_
integer_element U>
5736 requires simd_integer_element<T>
5737 hint_inline
constexpr simd(U input)
noexcept {
5738 auto x =
static_cast<T
>(input);
5740 std::array<T, N> a{};
5742 value_ = __builtin_bit_cast(native_type, a);
5744 if constexpr (std::same_as<T, std::int8_t>) {
5745 value_ = wasm_i8x16_splat(x);
5746 }
else if constexpr (std::same_as<T, std::uint8_t>) {
5747 value_ = wasm_u8x16_splat(x);
5748 }
else if constexpr (std::same_as<T, std::int16_t>) {
5749 value_ = wasm_i16x8_splat(x);
5750 }
else if constexpr (std::same_as<T, std::uint16_t>) {
5751 value_ = wasm_u16x8_splat(x);
5752 }
else if constexpr (std::same_as<T, std::int32_t>) {
5753 value_ = wasm_i32x4_splat(x);
5754 }
else if constexpr (std::same_as<T, std::uint32_t>) {
5755 value_ = wasm_u32x4_splat(x);
5756 }
else if constexpr (std::same_as<T, std::int64_t>) {
5757 value_ = wasm_i64x2_splat(x);
5758 }
else if constexpr (std::same_as<T, std::uint64_t>) {
5759 value_ = wasm_u64x2_splat(x);
5765 hint_inline
constexpr explicit simd(std::array<T, N>
const & a) noexcept
5769 template<
class... U>
5770 requires(
sizeof...(U) == N && ((std::same_as<U, T> && ...) ||
5771 (simd_integer_element<T> && (simd_integer_element<U> && ...))))
5772 hint_inline constexpr
simd(U... x) noexcept :
simd(
std::array<T, N>{
static_cast<T
>(x)...}) {}
5775 [[nodiscard]] hint_inline
constexpr native_type
to_native() const noexcept {
5792 [[nodiscard]]
static hint_inline
constexpr simd load(T
const * p)
noexcept {
5794 std::array<T, N> a{};
5795 for (std::size_t i = 0; i < N; ++i) {
5798 return from_native(__builtin_bit_cast(native_type, a));
5805 hint_inline
constexpr void store(T * p)
const noexcept {
5807 auto a = __builtin_bit_cast(std::array<T, N>, value_);
5808 for (std::size_t i = 0; i < N; ++i) {
5812 wasm_v128_store(p, value_);
5817 template<std::
size_t Align>
5822 return load(
static_cast<T
const *
>(__builtin_assume_aligned(p, Align)));
5827 template<std::
size_t Align>
5832 store(
static_cast<T *
>(__builtin_assume_aligned(p, Align)));
5838 T fill = {})
noexcept {
5839 std::array<T, N> a{};
5841 if consteval {
for(std::size_t i=0;i<n;++i) a[i]=p[i]; }
5844 if(n) std::memcpy(a.data(),
reinterpret_cast<unsigned char const *
>(p),n*
sizeof(T));
5845 }
else for(std::size_t i=0;i<n;++i) a[i]=p[i];
5847 return load(a.data());
5852 auto a = detail::wasm_lanes(*
this);
5853 if consteval {
for(std::size_t i=0;i<n;++i) p[i]=a[i]; }
5856 if(n) std::memcpy(
reinterpret_cast<unsigned char *
>(p),a.data(),n*
sizeof(T));
5857 }
else for(std::size_t i=0;i<n;++i) p[i]=a[i];
5862 [[nodiscard]]
static hint_inline
constexpr simd load_bits(word_type
const * p)
noexcept {
5867 hint_inline
constexpr void store_bits(word_type * p)
const noexcept {
5872 [[nodiscard]] hint_inline
constexpr bits_type
bits() const noexcept {
5873 return bits_type::from_native(value_);
5877 [[nodiscard]]
static hint_inline
constexpr simd from_bits(bits_type x)
noexcept {
5882 template<std::
size_t I>
5884 [[nodiscard]] hint_inline
constexpr T
get()
const noexcept {
5886 return __builtin_bit_cast(std::array<T, N>, value_)[I];
5888 if constexpr (std::same_as<T, float>) {
5889 return wasm_f32x4_extract_lane(value_, I);
5890 }
else if constexpr (std::same_as<T, double>) {
5891 return wasm_f64x2_extract_lane(value_, I);
5892 }
else if constexpr (std::same_as<T, std::int8_t>) {
5893 return wasm_i8x16_extract_lane(value_, I);
5894 }
else if constexpr (std::same_as<T, std::uint8_t>) {
5895 return wasm_u8x16_extract_lane(value_, I);
5896 }
else if constexpr (std::same_as<T, std::int16_t>) {
5897 return wasm_i16x8_extract_lane(value_, I);
5898 }
else if constexpr (std::same_as<T, std::uint16_t>) {
5899 return wasm_u16x8_extract_lane(value_, I);
5900 }
else if constexpr (std::same_as<T, std::int32_t>) {
5901 return wasm_i32x4_extract_lane(value_, I);
5902 }
else if constexpr (std::same_as<T, std::uint32_t>) {
5903 return wasm_u32x4_extract_lane(value_, I);
5904 }
else if constexpr (std::same_as<T, std::int64_t>) {
5905 return wasm_i64x2_extract_lane(value_, I);
5906 }
else if constexpr (std::same_as<T, std::uint64_t>) {
5907 return wasm_u64x2_extract_lane(value_, I);
5913 template<std::
size_t I>
5915 [[nodiscard]] hint_inline
constexpr simd replace(T x)
const noexcept {
5917 auto a = __builtin_bit_cast(std::array<T, N>, value_);
5919 return load(a.data());
5921 if constexpr (std::same_as<T, float>) {
5922 return from_native(wasm_f32x4_replace_lane(value_, I, x));
5923 }
else if constexpr (std::same_as<T, double>) {
5924 return from_native(wasm_f64x2_replace_lane(value_, I, x));
5925 }
else if constexpr (std::same_as<T, std::int8_t>) {
5926 return from_native(wasm_i8x16_replace_lane(value_, I, x));
5927 }
else if constexpr (std::same_as<T, std::uint8_t>) {
5928 return from_native(wasm_u8x16_replace_lane(value_, I, x));
5929 }
else if constexpr (std::same_as<T, std::int16_t>) {
5930 return from_native(wasm_i16x8_replace_lane(value_, I, x));
5931 }
else if constexpr (std::same_as<T, std::uint16_t>) {
5932 return from_native(wasm_u16x8_replace_lane(value_, I, x));
5933 }
else if constexpr (std::same_as<T, std::int32_t>) {
5934 return from_native(wasm_i32x4_replace_lane(value_, I, x));
5935 }
else if constexpr (std::same_as<T, std::uint32_t>) {
5936 return from_native(wasm_u32x4_replace_lane(value_, I, x));
5937 }
else if constexpr (std::same_as<T, std::int64_t>) {
5938 return from_native(wasm_i64x2_replace_lane(value_, I, x));
5939 }
else if constexpr (std::same_as<T, std::uint64_t>) {
5940 return from_native(wasm_u64x2_replace_lane(value_, I, x));
5948 return detail::wasm_map(
5950 if constexpr (std::is_floating_point_v<T>) {
5951 using format_type = detail::wasm_format<T>;
5952 return std::bit_cast<T>(detail::constexpr_float::add_bits<format_type>(
5953 std::bit_cast<word_type>(x), std::bit_cast<word_type>(y)));
5955 return std::bit_cast<T>(
5956 word_type(std::uint64_t(word_type(x)) + std::uint64_t(word_type(y))));
5961 if constexpr (std::same_as<T, float>) {
5962 return from_native(wasm_f32x4_add(a.value_, b.value_));
5963 }
else if constexpr (std::same_as<T, double>) {
5964 return from_native(wasm_f64x2_add(a.value_, b.value_));
5965 }
else if constexpr (std::same_as<T, std::int8_t>) {
5966 return from_native(wasm_i8x16_add(a.value_, b.value_));
5967 }
else if constexpr (std::same_as<T, std::uint8_t>) {
5968 return from_native(wasm_i8x16_add(a.value_, b.value_));
5969 }
else if constexpr (std::same_as<T, std::int16_t>) {
5970 return from_native(wasm_i16x8_add(a.value_, b.value_));
5971 }
else if constexpr (std::same_as<T, std::uint16_t>) {
5972 return from_native(wasm_i16x8_add(a.value_, b.value_));
5973 }
else if constexpr (std::same_as<T, std::int32_t>) {
5974 return from_native(wasm_i32x4_add(a.value_, b.value_));
5975 }
else if constexpr (std::same_as<T, std::uint32_t>) {
5976 return from_native(wasm_i32x4_add(a.value_, b.value_));
5977 }
else if constexpr (std::same_as<T, std::int64_t>) {
5978 return from_native(wasm_i64x2_add(a.value_, b.value_));
5979 }
else if constexpr (std::same_as<T, std::uint64_t>) {
5980 return from_native(wasm_i64x2_add(a.value_, b.value_));
5987 return *
this = *
this + b;
5993 return detail::wasm_map(
5995 if constexpr (std::is_floating_point_v<T>) {
5996 using format_type = detail::wasm_format<T>;
5997 return std::bit_cast<T>(detail::constexpr_float::sub_bits<format_type>(
5998 std::bit_cast<word_type>(x), std::bit_cast<word_type>(y)));
6000 return std::bit_cast<T>(
6001 word_type(std::uint64_t(word_type(x)) - std::uint64_t(word_type(y))));
6006 if constexpr (std::same_as<T, float>) {
6007 return from_native(wasm_f32x4_sub(a.value_, b.value_));
6008 }
else if constexpr (std::same_as<T, double>) {
6009 return from_native(wasm_f64x2_sub(a.value_, b.value_));
6010 }
else if constexpr (std::same_as<T, std::int8_t>) {
6011 return from_native(wasm_i8x16_sub(a.value_, b.value_));
6012 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6013 return from_native(wasm_i8x16_sub(a.value_, b.value_));
6014 }
else if constexpr (std::same_as<T, std::int16_t>) {
6015 return from_native(wasm_i16x8_sub(a.value_, b.value_));
6016 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6017 return from_native(wasm_i16x8_sub(a.value_, b.value_));
6018 }
else if constexpr (std::same_as<T, std::int32_t>) {
6019 return from_native(wasm_i32x4_sub(a.value_, b.value_));
6020 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6021 return from_native(wasm_i32x4_sub(a.value_, b.value_));
6022 }
else if constexpr (std::same_as<T, std::int64_t>) {
6023 return from_native(wasm_i64x2_sub(a.value_, b.value_));
6024 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6025 return from_native(wasm_i64x2_sub(a.value_, b.value_));
6032 return *
this = *
this - b;
6037 requires(
sizeof(T) > 1 || A.has(
polyfill))
6040 return detail::wasm_map(
6042 if constexpr (std::is_floating_point_v<T>) {
6043 using format_type = detail::wasm_format<T>;
6044 return std::bit_cast<T>(detail::constexpr_float::mul_bits<format_type>(
6045 std::bit_cast<word_type>(x), std::bit_cast<word_type>(y)));
6047 return std::bit_cast<T>(
6048 word_type(std::uint64_t(word_type(x)) * std::uint64_t(word_type(y))));
6053 if constexpr (
sizeof(T)==1) {
6054 auto low=wasm_i16x8_mul(wasm_u16x8_extend_low_u8x16(a.value_),wasm_u16x8_extend_low_u8x16(b.value_));
6055 auto high=wasm_i16x8_mul(wasm_u16x8_extend_high_u8x16(a.value_),wasm_u16x8_extend_high_u8x16(b.value_));
6056 return from_native(wasm_i8x16_shuffle(low,high,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30));
6057 }
else if constexpr (std::same_as<T, float>) {
6058 return from_native(wasm_f32x4_mul(a.value_, b.value_));
6059 }
else if constexpr (std::same_as<T, double>) {
6060 return from_native(wasm_f64x2_mul(a.value_, b.value_));
6061 }
else if constexpr (std::same_as<T, std::int16_t>) {
6062 return from_native(wasm_i16x8_mul(a.value_, b.value_));
6063 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6064 return from_native(wasm_i16x8_mul(a.value_, b.value_));
6065 }
else if constexpr (std::same_as<T, std::int32_t>) {
6066 return from_native(wasm_i32x4_mul(a.value_, b.value_));
6067 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6068 return from_native(wasm_i32x4_mul(a.value_, b.value_));
6069 }
else if constexpr (std::same_as<T, std::int64_t>) {
6070 return from_native(wasm_i64x2_mul(a.value_, b.value_));
6071 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6072 return from_native(wasm_i64x2_mul(a.value_, b.value_));
6079 requires(
sizeof(T) > 1 || A.has(
polyfill))
6081 return *
this = *
this * b;
6086 requires std::is_floating_point_v<T>
6089 return detail::wasm_map(
6091 using format_type = detail::wasm_format<T>;
6092 return std::bit_cast<T>(detail::constexpr_float::div_bits<format_type>(
6093 std::bit_cast<word_type>(x), std::bit_cast<word_type>(y)));
6097 if constexpr (std::same_as<T, float>) {
6098 return from_native(wasm_f32x4_div(a.value_, b.value_));
6099 }
else if constexpr (std::same_as<T, double>) {
6100 return from_native(wasm_f64x2_div(a.value_, b.value_));
6107 requires std::is_floating_point_v<T>
6109 return *
this = *
this / b;
6115 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6116 auto y = __builtin_bit_cast(std::array<word_type, N>, b.value_);
6117 for (std::size_t i = 0; i < N; ++i) {
6120 return from_native(__builtin_bit_cast(native_type, x));
6122 return from_native(wasm_v128_and(a.value_, b.value_));
6128 return *
this = *
this & b;
6134 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6135 auto y = __builtin_bit_cast(std::array<word_type, N>, b.value_);
6136 for (std::size_t i = 0; i < N; ++i) {
6139 return from_native(__builtin_bit_cast(native_type, x));
6141 return from_native(wasm_v128_or(a.value_, b.value_));
6147 return *
this = *
this | b;
6153 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6154 auto y = __builtin_bit_cast(std::array<word_type, N>, b.value_);
6155 for (std::size_t i = 0; i < N; ++i) {
6158 return from_native(__builtin_bit_cast(native_type, x));
6160 return from_native(wasm_v128_xor(a.value_, b.value_));
6166 return *
this = *
this ^ b;
6172 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6173 for (
auto & w : x) {
6176 return from_native(__builtin_bit_cast(native_type, x));
6185 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6186 for (
auto & w : x) {
6187 if constexpr (std::is_floating_point_v<T>) {
6188 w ^= word_type{1} << (
sizeof(T) * 8 - 1);
6190 w = word_type(0 - w);
6193 return from_native(__builtin_bit_cast(native_type, x));
6195 if constexpr (std::same_as<T, float>) {
6197 }
else if constexpr (std::same_as<T, double>) {
6199 }
else if constexpr (std::same_as<T, std::int8_t>) {
6201 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6203 }
else if constexpr (std::same_as<T, std::int16_t>) {
6205 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6207 }
else if constexpr (std::same_as<T, std::int32_t>) {
6209 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6211 }
else if constexpr (std::same_as<T, std::int64_t>) {
6213 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6222 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6223 std::uint64_t
bits = 0;
6224 for (std::size_t i = 0; i < N; ++i) {
6225 bits |= std::uint64_t(x[i] == y[i]) << i;
6229 if constexpr (std::same_as<T, float>) {
6230 return mask_type::unsafe_from_native(wasm_f32x4_eq(a.value_, b.value_));
6231 }
else if constexpr (std::same_as<T, double>) {
6232 return mask_type::unsafe_from_native(wasm_f64x2_eq(a.value_, b.value_));
6233 }
else if constexpr (std::same_as<T, std::int8_t>) {
6234 return mask_type::unsafe_from_native(wasm_i8x16_eq(a.value_, b.value_));
6235 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6236 return mask_type::unsafe_from_native(wasm_i8x16_eq(a.value_, b.value_));
6237 }
else if constexpr (std::same_as<T, std::int16_t>) {
6238 return mask_type::unsafe_from_native(wasm_i16x8_eq(a.value_, b.value_));
6239 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6240 return mask_type::unsafe_from_native(wasm_i16x8_eq(a.value_, b.value_));
6241 }
else if constexpr (std::same_as<T, std::int32_t>) {
6242 return mask_type::unsafe_from_native(wasm_i32x4_eq(a.value_, b.value_));
6243 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6244 return mask_type::unsafe_from_native(wasm_i32x4_eq(a.value_, b.value_));
6245 }
else if constexpr (std::same_as<T, std::int64_t>) {
6246 return mask_type::unsafe_from_native(wasm_i64x2_eq(a.value_, b.value_));
6247 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6248 return mask_type::unsafe_from_native(wasm_i64x2_eq(a.value_, b.value_));
6256 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6257 std::uint64_t
bits = 0;
6258 for (std::size_t i = 0; i < N; ++i) {
6259 bits |= std::uint64_t(x[i] != y[i]) << i;
6263 if constexpr (std::same_as<T, float>) {
6264 return mask_type::unsafe_from_native(wasm_f32x4_ne(a.value_, b.value_));
6265 }
else if constexpr (std::same_as<T, double>) {
6266 return mask_type::unsafe_from_native(wasm_f64x2_ne(a.value_, b.value_));
6267 }
else if constexpr (std::same_as<T, std::int8_t>) {
6268 return mask_type::unsafe_from_native(wasm_i8x16_ne(a.value_, b.value_));
6269 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6270 return mask_type::unsafe_from_native(wasm_i8x16_ne(a.value_, b.value_));
6271 }
else if constexpr (std::same_as<T, std::int16_t>) {
6272 return mask_type::unsafe_from_native(wasm_i16x8_ne(a.value_, b.value_));
6273 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6274 return mask_type::unsafe_from_native(wasm_i16x8_ne(a.value_, b.value_));
6275 }
else if constexpr (std::same_as<T, std::int32_t>) {
6276 return mask_type::unsafe_from_native(wasm_i32x4_ne(a.value_, b.value_));
6277 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6278 return mask_type::unsafe_from_native(wasm_i32x4_ne(a.value_, b.value_));
6279 }
else if constexpr (std::same_as<T, std::int64_t>) {
6280 return mask_type::unsafe_from_native(wasm_i64x2_ne(a.value_, b.value_));
6281 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6282 return mask_type::unsafe_from_native(wasm_i64x2_ne(a.value_, b.value_));
6290 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6291 std::uint64_t
bits = 0;
6292 for (std::size_t i = 0; i < N; ++i) {
6293 bits |= std::uint64_t(x[i] < y[i]) << i;
6297 if constexpr (std::same_as<T, float>) {
6298 return mask_type::unsafe_from_native(wasm_f32x4_lt(a.value_, b.value_));
6299 }
else if constexpr (std::same_as<T, double>) {
6300 return mask_type::unsafe_from_native(wasm_f64x2_lt(a.value_, b.value_));
6301 }
else if constexpr (std::same_as<T, std::int8_t>) {
6302 return mask_type::unsafe_from_native(wasm_i8x16_lt(a.value_, b.value_));
6303 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6304 return mask_type::unsafe_from_native(wasm_u8x16_lt(a.value_, b.value_));
6305 }
else if constexpr (std::same_as<T, std::int16_t>) {
6306 return mask_type::unsafe_from_native(wasm_i16x8_lt(a.value_, b.value_));
6307 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6308 return mask_type::unsafe_from_native(wasm_u16x8_lt(a.value_, b.value_));
6309 }
else if constexpr (std::same_as<T, std::int32_t>) {
6310 return mask_type::unsafe_from_native(wasm_i32x4_lt(a.value_, b.value_));
6311 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6312 return mask_type::unsafe_from_native(wasm_u32x4_lt(a.value_, b.value_));
6313 }
else if constexpr (std::same_as<T, std::int64_t>) {
6314 return mask_type::unsafe_from_native(wasm_i64x2_lt(a.value_, b.value_));
6315 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6316 return mask_type::unsafe_from_native(
6317 wasm_i64x2_lt(wasm_v128_xor(a.value_, wasm_i64x2_splat(INT64_MIN)),
6318 wasm_v128_xor(b.value_, wasm_i64x2_splat(INT64_MIN))));
6326 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6327 std::uint64_t
bits = 0;
6328 for (std::size_t i = 0; i < N; ++i) {
6329 bits |= std::uint64_t(x[i] <= y[i]) << i;
6333 if constexpr (std::same_as<T, float>) {
6334 return mask_type::unsafe_from_native(wasm_f32x4_le(a.value_, b.value_));
6335 }
else if constexpr (std::same_as<T, double>) {
6336 return mask_type::unsafe_from_native(wasm_f64x2_le(a.value_, b.value_));
6337 }
else if constexpr (std::same_as<T, std::int8_t>) {
6338 return mask_type::unsafe_from_native(wasm_i8x16_le(a.value_, b.value_));
6339 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6340 return mask_type::unsafe_from_native(wasm_u8x16_le(a.value_, b.value_));
6341 }
else if constexpr (std::same_as<T, std::int16_t>) {
6342 return mask_type::unsafe_from_native(wasm_i16x8_le(a.value_, b.value_));
6343 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6344 return mask_type::unsafe_from_native(wasm_u16x8_le(a.value_, b.value_));
6345 }
else if constexpr (std::same_as<T, std::int32_t>) {
6346 return mask_type::unsafe_from_native(wasm_i32x4_le(a.value_, b.value_));
6347 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6348 return mask_type::unsafe_from_native(wasm_u32x4_le(a.value_, b.value_));
6349 }
else if constexpr (std::same_as<T, std::int64_t>) {
6350 return mask_type::unsafe_from_native(wasm_i64x2_le(a.value_, b.value_));
6351 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6352 return mask_type::unsafe_from_native(
6353 wasm_i64x2_le(wasm_v128_xor(a.value_, wasm_i64x2_splat(INT64_MIN)),
6354 wasm_v128_xor(b.value_, wasm_i64x2_splat(INT64_MIN))));
6362 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6363 std::uint64_t
bits = 0;
6364 for (std::size_t i = 0; i < N; ++i) {
6365 bits |= std::uint64_t(x[i] > y[i]) << i;
6369 if constexpr (std::same_as<T, float>) {
6370 return mask_type::unsafe_from_native(wasm_f32x4_gt(a.value_, b.value_));
6371 }
else if constexpr (std::same_as<T, double>) {
6372 return mask_type::unsafe_from_native(wasm_f64x2_gt(a.value_, b.value_));
6373 }
else if constexpr (std::same_as<T, std::int8_t>) {
6374 return mask_type::unsafe_from_native(wasm_i8x16_gt(a.value_, b.value_));
6375 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6376 return mask_type::unsafe_from_native(wasm_u8x16_gt(a.value_, b.value_));
6377 }
else if constexpr (std::same_as<T, std::int16_t>) {
6378 return mask_type::unsafe_from_native(wasm_i16x8_gt(a.value_, b.value_));
6379 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6380 return mask_type::unsafe_from_native(wasm_u16x8_gt(a.value_, b.value_));
6381 }
else if constexpr (std::same_as<T, std::int32_t>) {
6382 return mask_type::unsafe_from_native(wasm_i32x4_gt(a.value_, b.value_));
6383 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6384 return mask_type::unsafe_from_native(wasm_u32x4_gt(a.value_, b.value_));
6385 }
else if constexpr (std::same_as<T, std::int64_t>) {
6386 return mask_type::unsafe_from_native(wasm_i64x2_gt(a.value_, b.value_));
6387 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6388 return mask_type::unsafe_from_native(
6389 wasm_i64x2_gt(wasm_v128_xor(a.value_, wasm_i64x2_splat(INT64_MIN)),
6390 wasm_v128_xor(b.value_, wasm_i64x2_splat(INT64_MIN))));
6398 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6399 std::uint64_t
bits = 0;
6400 for (std::size_t i = 0; i < N; ++i) {
6401 bits |= std::uint64_t(x[i] >= y[i]) << i;
6405 if constexpr (std::same_as<T, float>) {
6406 return mask_type::unsafe_from_native(wasm_f32x4_ge(a.value_, b.value_));
6407 }
else if constexpr (std::same_as<T, double>) {
6408 return mask_type::unsafe_from_native(wasm_f64x2_ge(a.value_, b.value_));
6409 }
else if constexpr (std::same_as<T, std::int8_t>) {
6410 return mask_type::unsafe_from_native(wasm_i8x16_ge(a.value_, b.value_));
6411 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6412 return mask_type::unsafe_from_native(wasm_u8x16_ge(a.value_, b.value_));
6413 }
else if constexpr (std::same_as<T, std::int16_t>) {
6414 return mask_type::unsafe_from_native(wasm_i16x8_ge(a.value_, b.value_));
6415 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6416 return mask_type::unsafe_from_native(wasm_u16x8_ge(a.value_, b.value_));
6417 }
else if constexpr (std::same_as<T, std::int32_t>) {
6418 return mask_type::unsafe_from_native(wasm_i32x4_ge(a.value_, b.value_));
6419 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6420 return mask_type::unsafe_from_native(wasm_u32x4_ge(a.value_, b.value_));
6421 }
else if constexpr (std::same_as<T, std::int64_t>) {
6422 return mask_type::unsafe_from_native(wasm_i64x2_ge(a.value_, b.value_));
6423 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6424 return mask_type::unsafe_from_native(
6425 wasm_i64x2_ge(wasm_v128_xor(a.value_, wasm_i64x2_splat(INT64_MIN)),
6426 wasm_v128_xor(b.value_, wasm_i64x2_splat(INT64_MIN))));
6432 [[nodiscard]] hint_inline
constexpr simd left(
unsigned count)
const noexcept
6433 requires simd_integer_element<T>
6435 count %=
sizeof(T) * 8;
6437 return detail::wasm_map(
6438 [&](T x) {
return std::bit_cast<T>(word_type(std::uint64_t(word_type(x)) << count)); },
6441 if constexpr (std::same_as<T, std::int8_t>) {
6442 return from_native(wasm_i8x16_shl(value_, count));
6443 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6444 return from_native(wasm_i8x16_shl(value_, count));
6445 }
else if constexpr (std::same_as<T, std::int16_t>) {
6446 return from_native(wasm_i16x8_shl(value_, count));
6447 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6448 return from_native(wasm_i16x8_shl(value_, count));
6449 }
else if constexpr (std::same_as<T, std::int32_t>) {
6450 return from_native(wasm_i32x4_shl(value_, count));
6451 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6452 return from_native(wasm_i32x4_shl(value_, count));
6453 }
else if constexpr (std::same_as<T, std::int64_t>) {
6454 return from_native(wasm_i64x2_shl(value_, count));
6455 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6456 return from_native(wasm_i64x2_shl(value_, count));
6462 template<std::
size_t K>
6463 [[nodiscard]] hint_inline
constexpr simd left() const noexcept
6464 requires simd_integer_element<T>
6471 requires simd_integer_element<T>
6477 template<std::
size_t K>
6479 requires simd_integer_element<T>
6485 [[nodiscard]] hint_inline
constexpr simd right(
unsigned count)
const noexcept
6486 requires simd_integer_element<T>
6488 count %=
sizeof(T) * 8;
6490 return detail::wasm_map([&](T x) {
return T(x >> count); }, *
this);
6492 if constexpr (std::same_as<T, std::int8_t>) {
6493 return from_native(wasm_i8x16_shr(value_, count));
6494 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6495 return from_native(wasm_u8x16_shr(value_, count));
6496 }
else if constexpr (std::same_as<T, std::int16_t>) {
6497 return from_native(wasm_i16x8_shr(value_, count));
6498 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6499 return from_native(wasm_u16x8_shr(value_, count));
6500 }
else if constexpr (std::same_as<T, std::int32_t>) {
6501 return from_native(wasm_i32x4_shr(value_, count));
6502 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6503 return from_native(wasm_u32x4_shr(value_, count));
6504 }
else if constexpr (std::same_as<T, std::int64_t>) {
6505 return from_native(wasm_i64x2_shr(value_, count));
6506 }
else if constexpr (std::same_as<T, std::uint64_t>) {
6507 return from_native(wasm_u64x2_shr(value_, count));
6513 template<std::
size_t K>
6514 [[nodiscard]] hint_inline
constexpr simd right() const noexcept
6515 requires simd_integer_element<T>
6522 requires simd_integer_element<T>
6528 template<std::
size_t K>
6530 requires simd_integer_element<T>
6537 template<detail::wasm_number T, std::
size_t N, isa<> A>
6538 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6542 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6543 auto bits = m.to_bitset();
6544 for (std::size_t i = 0; i < N; ++i) {
6545 if (!((bits >> i) & 1)) {
6552 wasm_v128_bitselect(a.to_native(), b.to_native(), m.to_native()));
6557 template<detail::wasm_number T, std::
size_t N, isa<> A>
6558 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6565 template<
class To,
class U, std::
size_t N, isa<> A>
6566 requires(A.has(wasm_feature::simd128)) && (
sizeof(U) * N == 16) && (
sizeof(To) ==
sizeof(U)) &&
6567 simd_integer_element<To>
6573 template<std::
size_t I, detail::wasm_number T, std::
size_t N, isa<> A>
6574 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16) && (I < N)
6580 template<std::size_t... I, detail::wasm_number T, std::size_t N, isa<> A>
6581 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16) && (
sizeof...(I) == N) &&
6582 ((I < 2 * N) && ...)
6585 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
6586 std::array<T, N> r{(I < N ? x[I] : y[I - N])...};
6589 using vector_type = T __attribute__((ext_vector_type(N)));
6591 v128_t, __builtin_shufflevector(__builtin_bit_cast(vector_type, a.to_native()),
6592 __builtin_bit_cast(vector_type, b.to_native()), I...)));
6598 requires(A.has(wasm_feature::simd128))
6599 [[nodiscard]] hint_inline
constexpr simd<std::uint8_t, 16, A>
6602 auto x = detail::wasm_lanes(v), i = detail::wasm_lanes(indices);
6603 for (
auto & n : i) {
6604 n = n < 16 ? x[n] : 0;
6609 wasm_i8x16_swizzle(v.to_native(), indices.to_native()));
6614 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6615 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6618 return detail::wasm_map(
6620 using format_type = detail::wasm_format<T>;
6621 using bits_type =
typename format_type::bits_type;
6622 auto w = std::bit_cast<bits_type>(x);
6623 return std::bit_cast<T>(detail::constexpr_float::sqrt_bits<format_type>(w));
6627 if constexpr (
sizeof(T) == 4) {
6636 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6637 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6640 return detail::wasm_map(
6642 using format_type = detail::wasm_format<T>;
6643 using bits_type =
typename format_type::bits_type;
6644 auto w = std::bit_cast<bits_type>(x);
6645 return std::bit_cast<T>(detail::constexpr_float::round_integral_bits<format_type>(
6646 w, detail::constexpr_float::rounding::downward));
6650 if constexpr (
sizeof(T) == 4) {
6659 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6660 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6663 return detail::wasm_map(
6665 using format_type = detail::wasm_format<T>;
6666 using bits_type =
typename format_type::bits_type;
6667 auto w = std::bit_cast<bits_type>(x);
6668 return std::bit_cast<T>(detail::constexpr_float::round_integral_bits<format_type>(
6669 w, detail::constexpr_float::rounding::upward));
6673 if constexpr (
sizeof(T) == 4) {
6682 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6683 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6686 return detail::wasm_map(
6688 using format_type = detail::wasm_format<T>;
6689 using bits_type =
typename format_type::bits_type;
6690 auto w = std::bit_cast<bits_type>(x);
6691 return std::bit_cast<T>(detail::constexpr_float::round_integral_bits<format_type>(
6692 w, detail::constexpr_float::rounding::toward_zero));
6696 if constexpr (
sizeof(T) == 4) {
6705 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6706 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6709 return detail::wasm_map(
6711 using format_type = detail::wasm_format<T>;
6712 using bits_type =
typename format_type::bits_type;
6713 auto w = std::bit_cast<bits_type>(x);
6714 return std::bit_cast<T>(detail::constexpr_float::round_integral_bits<format_type>(w));
6718 if constexpr (
sizeof(T) == 4) {
6727 template<std::
floating_po
int T, std::
size_t N, isa<> A>
6728 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6731 return detail::wasm_map(
6733 using format_type = detail::wasm_format<T>;
6734 using bits_type =
typename format_type::bits_type;
6735 auto w = std::bit_cast<bits_type>(x);
6736 return std::bit_cast<T>(bits_type(w & ~format_type::sign_mask));
6740 if constexpr (
sizeof(T) == 4) {
6752 constexpr T wasm_saturate(std::int64_t x)
noexcept {
6753 if (x < std::int64_t(std::numeric_limits<T>::min())) {
6754 return std::numeric_limits<T>::min();
6756 if (x > std::int64_t(std::numeric_limits<T>::max())) {
6757 return std::numeric_limits<T>::max();
6762 template<
class To,
class From>
6763 constexpr To wasm_trunc_sat(From x)
noexcept {
6767 if (x <=
double(std::numeric_limits<To>::min())) {
6768 return std::numeric_limits<To>::min();
6770 if (x >=
double(std::numeric_limits<To>::max())) {
6771 return std::numeric_limits<To>::max();
6778 template<simd_
integer_element T, std::
size_t N, isa<> A>
6779 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 2)
6783 return detail::wasm_map(
6784 [](T x, T y) {
return detail::wasm_saturate<T>(std::int64_t(x) + std::int64_t(y)); }, a, b);
6786 if constexpr (std::same_as<T, std::int8_t>) {
6787 return vector_type::from_native(wasm_i8x16_add_sat(a.to_native(), b.to_native()));
6788 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6789 return vector_type::from_native(wasm_u8x16_add_sat(a.to_native(), b.to_native()));
6790 }
else if constexpr (std::same_as<T, std::int16_t>) {
6791 return vector_type::from_native(wasm_i16x8_add_sat(a.to_native(), b.to_native()));
6792 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6793 return vector_type::from_native(wasm_u16x8_add_sat(a.to_native(), b.to_native()));
6799 template<simd_
integer_element T, std::
size_t N, isa<> A>
6800 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 2)
6804 return detail::wasm_map(
6805 [](T x, T y) {
return detail::wasm_saturate<T>(std::int64_t(x) - std::int64_t(y)); }, a, b);
6807 if constexpr (std::same_as<T, std::int8_t>) {
6808 return vector_type::from_native(wasm_i8x16_sub_sat(a.to_native(), b.to_native()));
6809 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6810 return vector_type::from_native(wasm_u8x16_sub_sat(a.to_native(), b.to_native()));
6811 }
else if constexpr (std::same_as<T, std::int16_t>) {
6812 return vector_type::from_native(wasm_i16x8_sub_sat(a.to_native(), b.to_native()));
6813 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6814 return vector_type::from_native(wasm_u16x8_sub_sat(a.to_native(), b.to_native()));
6820 template<simd_
integer_element T, std::
size_t N, isa<> A>
6821 requires(A.has(wasm_feature::simd128)) &&
6822 (std::is_unsigned_v<T> &&
sizeof(T) * N == 16 &&
sizeof(T) <= 2)
6825 return detail::wasm_map([](T x, T y) {
return T((
unsigned(x) + y + 1) / 2); }, a, b);
6827 if constexpr (
sizeof(T) == 1) {
6836 template<simd_
integer_element T, std::
size_t N, isa<> A>
6837 requires(A.has(wasm_feature::simd128)) && (std::is_signed_v<T> &&
sizeof(T) * N == 16)
6840 using unsigned_type = std::make_unsigned_t<T>;
6842 return detail::wasm_map(
6843 [](T x) {
return x < 0 ? std::bit_cast<T>(unsigned_type(0 - unsigned_type(x))) : x; }, a);
6845 if constexpr (
sizeof(T) == 1) {
6846 return vector_type::from_native(wasm_i8x16_abs(a.to_native()));
6847 }
else if constexpr (
sizeof(T) == 2) {
6848 return vector_type::from_native(wasm_i16x8_abs(a.to_native()));
6849 }
else if constexpr (
sizeof(T) == 4) {
6850 return vector_type::from_native(wasm_i32x4_abs(a.to_native()));
6851 }
else if constexpr (
sizeof(T) == 8) {
6852 return vector_type::from_native(wasm_i64x2_abs(a.to_native()));
6858 template<detail::wasm_number T, std::
size_t N, isa<> A>
6859 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6863 return detail::wasm_map(
6865 if constexpr (std::is_floating_point_v<T>) {
6866 using format_type = detail::wasm_format<T>;
6867 using bits_type =
typename format_type::bits_type;
6868 auto xx = std::bit_cast<bits_type>(x), yy = std::bit_cast<bits_type>(y);
6869 if (detail::constexpr_float::is_nan<format_type>(xx) ||
6870 detail::constexpr_float::is_nan<format_type>(yy)) {
6871 return std::bit_cast<T>(detail::constexpr_float::default_nan<format_type>({}));
6873 if (detail::constexpr_float::is_zero<format_type>(xx) &&
6874 detail::constexpr_float::is_zero<format_type>(yy)) {
6875 return std::bit_cast<T>(bits_type(xx | yy));
6878 return y < x ? y : x;
6882 if constexpr (std::same_as<T, float>) {
6883 return vector_type::from_native(wasm_f32x4_min(a.to_native(), b.to_native()));
6884 }
else if constexpr (std::same_as<T, double>) {
6885 return vector_type::from_native(wasm_f64x2_min(a.to_native(), b.to_native()));
6886 }
else if constexpr (std::same_as<T, std::int8_t>) {
6887 return vector_type::from_native(wasm_i8x16_min(a.to_native(), b.to_native()));
6888 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6889 return vector_type::from_native(wasm_u8x16_min(a.to_native(), b.to_native()));
6890 }
else if constexpr (std::same_as<T, std::int16_t>) {
6891 return vector_type::from_native(wasm_i16x8_min(a.to_native(), b.to_native()));
6892 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6893 return vector_type::from_native(wasm_u16x8_min(a.to_native(), b.to_native()));
6894 }
else if constexpr (std::same_as<T, std::int32_t>) {
6895 return vector_type::from_native(wasm_i32x4_min(a.to_native(), b.to_native()));
6896 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6897 return vector_type::from_native(wasm_u32x4_min(a.to_native(), b.to_native()));
6899 return select(a < b, a, b);
6905 template<detail::wasm_number T, std::
size_t N, isa<> A>
6906 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
6910 return detail::wasm_map(
6912 if constexpr (std::is_floating_point_v<T>) {
6913 using format_type = detail::wasm_format<T>;
6914 using bits_type =
typename format_type::bits_type;
6915 auto xx = std::bit_cast<bits_type>(x), yy = std::bit_cast<bits_type>(y);
6916 if (detail::constexpr_float::is_nan<format_type>(xx) ||
6917 detail::constexpr_float::is_nan<format_type>(yy)) {
6918 return std::bit_cast<T>(detail::constexpr_float::default_nan<format_type>({}));
6920 if (detail::constexpr_float::is_zero<format_type>(xx) &&
6921 detail::constexpr_float::is_zero<format_type>(yy)) {
6922 return std::bit_cast<T>(bits_type(xx & yy));
6925 return y > x ? y : x;
6929 if constexpr (std::same_as<T, float>) {
6930 return vector_type::from_native(wasm_f32x4_max(a.to_native(), b.to_native()));
6931 }
else if constexpr (std::same_as<T, double>) {
6932 return vector_type::from_native(wasm_f64x2_max(a.to_native(), b.to_native()));
6933 }
else if constexpr (std::same_as<T, std::int8_t>) {
6934 return vector_type::from_native(wasm_i8x16_max(a.to_native(), b.to_native()));
6935 }
else if constexpr (std::same_as<T, std::uint8_t>) {
6936 return vector_type::from_native(wasm_u8x16_max(a.to_native(), b.to_native()));
6937 }
else if constexpr (std::same_as<T, std::int16_t>) {
6938 return vector_type::from_native(wasm_i16x8_max(a.to_native(), b.to_native()));
6939 }
else if constexpr (std::same_as<T, std::uint16_t>) {
6940 return vector_type::from_native(wasm_u16x8_max(a.to_native(), b.to_native()));
6941 }
else if constexpr (std::same_as<T, std::int32_t>) {
6942 return vector_type::from_native(wasm_i32x4_max(a.to_native(), b.to_native()));
6943 }
else if constexpr (std::same_as<T, std::uint32_t>) {
6944 return vector_type::from_native(wasm_u32x4_max(a.to_native(), b.to_native()));
6946 return select(a > b, a, b);
6952 template<detail::wasm_number T, std::
size_t N, isa<> A>
6953 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 && std::is_floating_point_v<T>)
6957 return detail::wasm_map([](T x, T y) {
return y < x ? y : x; }, a, b);
6959 if constexpr (std::same_as<T, float>) {
6960 return vector_type::from_native(wasm_f32x4_pmin(a.to_native(), b.to_native()));
6961 }
else if constexpr (std::same_as<T, double>) {
6962 return vector_type::from_native(wasm_f64x2_pmin(a.to_native(), b.to_native()));
6968 template<detail::wasm_number T, std::
size_t N, isa<> A>
6969 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 && std::is_floating_point_v<T>)
6973 return detail::wasm_map([](T x, T y) {
return y > x ? y : x; }, a, b);
6975 if constexpr (std::same_as<T, float>) {
6976 return vector_type::from_native(wasm_f32x4_pmax(a.to_native(), b.to_native()));
6977 }
else if constexpr (std::same_as<T, double>) {
6978 return vector_type::from_native(wasm_f64x2_pmax(a.to_native(), b.to_native()));
6984 template<simd_
integer_element T, std::
size_t N, isa<> A>
6985 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 4)
6987 using wide_unsigned_type =
6988 std::conditional_t<
sizeof(T) == 1, std::uint16_t,
6989 std::conditional_t<
sizeof(T) == 2, std::uint32_t, std::uint64_t>>;
6991 std::conditional_t<std::is_signed_v<T>, std::make_signed_t<wide_unsigned_type>,
6992 wide_unsigned_type>;
6993 using vector_type =
simd<wide_type, N / 2, A>;
6995 auto x = detail::wasm_lanes(a);
6996 std::array<wide_type, N / 2> r{};
6997 for (std::size_t i = 0; i < N / 2; ++i) {
7000 return vector_type::load(r.data());
7002 if constexpr (std::same_as<T, std::int8_t>) {
7003 return vector_type::from_native(wasm_i16x8_extend_low_i8x16(a.to_native()));
7004 }
else if constexpr (std::same_as<T, std::uint8_t>) {
7005 return vector_type::from_native(wasm_u16x8_extend_low_u8x16(a.to_native()));
7006 }
else if constexpr (std::same_as<T, std::int16_t>) {
7007 return vector_type::from_native(wasm_i32x4_extend_low_i16x8(a.to_native()));
7008 }
else if constexpr (std::same_as<T, std::uint16_t>) {
7009 return vector_type::from_native(wasm_u32x4_extend_low_u16x8(a.to_native()));
7010 }
else if constexpr (std::same_as<T, std::int32_t>) {
7011 return vector_type::from_native(wasm_i64x2_extend_low_i32x4(a.to_native()));
7012 }
else if constexpr (std::same_as<T, std::uint32_t>) {
7013 return vector_type::from_native(wasm_u64x2_extend_low_u32x4(a.to_native()));
7019 template<simd_
integer_element T, std::
size_t N, isa<> A>
7020 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 4)
7026 template<simd_
integer_element T, std::
size_t N, isa<> A>
7027 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 4)
7029 using wide_unsigned_type =
7030 std::conditional_t<
sizeof(T) == 1, std::uint16_t,
7031 std::conditional_t<
sizeof(T) == 2, std::uint32_t, std::uint64_t>>;
7033 std::conditional_t<std::is_signed_v<T>, std::make_signed_t<wide_unsigned_type>,
7034 wide_unsigned_type>;
7035 using vector_type =
simd<wide_type, N / 2, A>;
7037 auto x = detail::wasm_lanes(a);
7038 std::array<wide_type, N / 2> r{};
7039 for (std::size_t i = 0; i < N / 2; ++i) {
7040 r[i] = x[i + N / 2];
7042 return vector_type::load(r.data());
7044 if constexpr (std::same_as<T, std::int8_t>) {
7045 return vector_type::from_native(wasm_i16x8_extend_high_i8x16(a.to_native()));
7046 }
else if constexpr (std::same_as<T, std::uint8_t>) {
7047 return vector_type::from_native(wasm_u16x8_extend_high_u8x16(a.to_native()));
7048 }
else if constexpr (std::same_as<T, std::int16_t>) {
7049 return vector_type::from_native(wasm_i32x4_extend_high_i16x8(a.to_native()));
7050 }
else if constexpr (std::same_as<T, std::uint16_t>) {
7051 return vector_type::from_native(wasm_u32x4_extend_high_u16x8(a.to_native()));
7052 }
else if constexpr (std::same_as<T, std::int32_t>) {
7053 return vector_type::from_native(wasm_i64x2_extend_high_i32x4(a.to_native()));
7054 }
else if constexpr (std::same_as<T, std::uint32_t>) {
7055 return vector_type::from_native(wasm_u64x2_extend_high_u32x4(a.to_native()));
7061 template<simd_
integer_element T, std::
size_t N, isa<> A>
7062 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 &&
sizeof(T) <= 4)
7068 template<simd_
integer_element To, simd_
integer_element From, std::
size_t N, isa<> A>
7069 requires(A.has(wasm_feature::simd128)) &&
7070 (
sizeof(From) * N == 16 &&
sizeof(From) == 2 *
sizeof(To) &&
sizeof(To) <= 2 &&
7071 std::is_signed_v<From>)
7076 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
7077 std::array<To, N * 2> r{};
7078 for (std::size_t i = 0; i < N; ++i) {
7079 r[i] = detail::wasm_saturate<To>(x[i]);
7080 r[i + N] = detail::wasm_saturate<To>(y[i]);
7082 return vector_type::load(r.data());
7084 if constexpr (std::same_as<To, std::int8_t>) {
7085 return vector_type::from_native(wasm_i8x16_narrow_i16x8(a.to_native(), b.to_native()));
7086 }
else if constexpr (std::same_as<To, std::uint8_t>) {
7087 return vector_type::from_native(wasm_u8x16_narrow_i16x8(a.to_native(), b.to_native()));
7088 }
else if constexpr (std::same_as<To, std::int16_t>) {
7089 return vector_type::from_native(wasm_i16x8_narrow_i32x4(a.to_native(), b.to_native()));
7090 }
else if constexpr (std::same_as<To, std::uint16_t>) {
7091 return vector_type::from_native(wasm_u16x8_narrow_i32x4(a.to_native(), b.to_native()));
7098 requires(A.has(wasm_feature::simd128))
7099 [[nodiscard]] hint_inline
constexpr simd<std::int16_t, 8, A>
7102 return detail::wasm_map(
7103 [](std::int16_t x, std::int16_t y) {
7104 return detail::wasm_saturate<std::int16_t>((std::int64_t(x) * y + 16384) >> 15);
7109 wasm_i16x8_q15mulr_sat(a.to_native(), b.to_native()));
7115 requires(A.has(wasm_feature::simd128))
7120 auto x = detail::wasm_lanes(a), y = detail::wasm_lanes(b);
7121 std::array<std::int32_t, 4> r{};
7122 for (std::size_t i = 0; i < 4; ++i) {
7123 r[i] = std::bit_cast<std::int32_t>(std::uint32_t(
7124 std::int64_t(x[2 * i]) * y[2 * i] + std::int64_t(x[2 * i + 1]) * y[2 * i + 1]));
7126 return vector_type::load(r.data());
7128 return vector_type::from_native(wasm_i32x4_dot_i16x8(a.to_native(), b.to_native()));
7134 template<simd_
integer_element To, std::
floating_po
int From, std::
size_t N, isa<> A>
7135 requires(A.has(wasm_feature::simd128)) && (
sizeof(From) * N == 16 &&
sizeof(To) == 4)
7139 auto x = detail::wasm_lanes(a);
7140 std::array<To, 4> r{};
7141 for (std::size_t i = 0; i < N; ++i) {
7142 r[i] = detail::wasm_trunc_sat<To>(x[i]);
7144 return vector_type::load(r.data());
7146 if constexpr (
sizeof(From) == 4 && std::is_signed_v<To>) {
7147 return vector_type::from_native(wasm_i32x4_trunc_sat_f32x4(a.to_native()));
7148 }
else if constexpr (
sizeof(From) == 4) {
7149 return vector_type::from_native(wasm_u32x4_trunc_sat_f32x4(a.to_native()));
7150 }
else if constexpr (std::is_signed_v<To>) {
7151 return vector_type::from_native(wasm_i32x4_trunc_sat_f64x2_zero(a.to_native()));
7153 return vector_type::from_native(wasm_u32x4_trunc_sat_f64x2_zero(a.to_native()));
7160 template<std::
floating_po
int To, detail::wasm_number From, std::
size_t N, isa<> A>
7161 requires(A.has(wasm_feature::simd128)) &&
7162 (
sizeof(From) * N == 16 && (
sizeof(From) == 4 || std::is_floating_point_v<From>) &&
7163 !std::same_as<To, From>)
7165 using vector_type =
simd<To, 16 /
sizeof(To), A>;
7167 auto x = detail::wasm_lanes(a);
7168 std::array<To, vector_type::lanes> r{};
7169 for (std::size_t i = 0; i < std::min(N, vector_type::lanes); ++i) {
7170 if constexpr (std::is_floating_point_v<From>) {
7171 using format_type = detail::wasm_format<From>;
7172 using result_format = detail::wasm_format<To>;
7174 std::bit_cast<To>(detail::constexpr_float::convert_bits<result_format, format_type>(
7175 std::bit_cast<typename format_type::bits_type>(x[i])));
7180 return vector_type::load(r.data());
7182 if constexpr (std::same_as<From, float>) {
7183 return vector_type::from_native(wasm_f64x2_promote_low_f32x4(a.to_native()));
7184 }
else if constexpr (std::same_as<From, double>) {
7185 return vector_type::from_native(wasm_f32x4_demote_f64x2_zero(a.to_native()));
7186 }
else if constexpr (
sizeof(To) == 4 && std::is_signed_v<From>) {
7187 return vector_type::from_native(wasm_f32x4_convert_i32x4(a.to_native()));
7188 }
else if constexpr (
sizeof(To) == 4) {
7189 return vector_type::from_native(wasm_f32x4_convert_u32x4(a.to_native()));
7190 }
else if constexpr (std::is_signed_v<From>) {
7191 return vector_type::from_native(wasm_f64x2_convert_low_i32x4(a.to_native()));
7193 return vector_type::from_native(wasm_f64x2_convert_low_u32x4(a.to_native()));
7200 requires detail::wasm_number<typename V::value_type> &&
7201 (V::architecture.has(wasm_feature::simd128)) &&
7202 (V::lanes *
sizeof(
typename V::value_type) == 16)
7203 [[nodiscard]] hint_inline
constexpr V
load_splat(
typename V::value_type
const * p)
noexcept {
7204 if constexpr(!V::architecture.has(
polyfill))
return V(*p);
7206 if consteval {
return V(*p); }
7208 typename V::value_type value;
7209 std::memcpy(&value,
reinterpret_cast<unsigned char const *
>(p),
sizeof(value));
return V(value);
7215 template<std::
size_t I, detail::wasm_number T, std::
size_t N, isa<> A>
7216 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 && I < N)
7218 if constexpr(!A.has(
polyfill))
return a.template replace<I>(*p);
7220 if consteval {
return a.template replace<I>(*p); }
7222 T value; std::memcpy(&value,
reinterpret_cast<unsigned char const *
>(p),
sizeof(T));
return a.template replace<I>(value);
7228 template<std::
size_t I, detail::wasm_number T, std::
size_t N, isa<> A>
7229 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16 && I < N)
7231 auto value=a.template get<I>();
7232 if constexpr(!A.has(
polyfill)) *p=value;
7234 if consteval { *p=value; }
7235 else { std::memcpy(
reinterpret_cast<unsigned char *
>(p),&value,
sizeof(T)); }
7241 requires(V::architecture.has(wasm_feature::simd128)) &&
7242 (V::lanes *
sizeof(
typename V::value_type) == 16 &&
sizeof(
typename V::value_type) >= 4)
7243 [[nodiscard]] hint_inline
constexpr V
load_zero(
typename V::value_type
const * p)
noexcept {
7245 std::array<typename V::value_type, V::lanes> a{};
7247 return V::load(a.data());
7249 if constexpr (
sizeof(
typename V::value_type) == 4) {
7250 return V::from_native(wasm_v128_load32_zero(p));
7252 return V::from_native(wasm_v128_load64_zero(p));
7258 template<simd_
integer_element To, simd_
integer_element From, isa<> A>
7259 requires(A.has(wasm_feature::simd128)) &&
7260 (
sizeof(To) == 2 *
sizeof(From) && std::is_signed_v<To> == std::is_signed_v<From>)
7261 [[nodiscard]] hint_inline
constexpr simd<To, 16 /
sizeof(To), A>
load_widened(From
const * p)
noexcept {
7262 using vector_type =
simd<To, 16 /
sizeof(To), A>;
7264 std::array<To, vector_type::lanes> a{};
7265 for (std::size_t i = 0; i < vector_type::lanes; ++i) {
7268 return vector_type::load(a.data());
7270 if constexpr (std::same_as<To, std::int16_t>) {
7271 return vector_type::from_native(wasm_i16x8_load8x8(p));
7272 }
else if constexpr (std::same_as<To, std::uint16_t>) {
7273 return vector_type::from_native(wasm_u16x8_load8x8(p));
7274 }
else if constexpr (std::same_as<To, std::int32_t>) {
7275 return vector_type::from_native(wasm_i32x4_load16x4(p));
7276 }
else if constexpr (std::same_as<To, std::uint32_t>) {
7277 return vector_type::from_native(wasm_u32x4_load16x4(p));
7278 }
else if constexpr (std::same_as<To, std::int64_t>) {
7279 return vector_type::from_native(wasm_i64x2_load32x2(p));
7280 }
else if constexpr (std::same_as<To, std::uint64_t>) {
7281 return vector_type::from_native(wasm_u64x2_load32x2(p));
7287 template<simd_
integer_element T, std::
size_t N, isa<> A>
7288 requires(A.has(wasm_feature::simd128)) &&
7289 (std::is_signed_v<T> &&
sizeof(T) * N == 16 &&
sizeof(T) <= 2)
7291 using wide_type = std::conditional_t<
sizeof(T) == 1, std::int16_t, std::int32_t>;
7292 using vector_type =
simd<wide_type, N / 2, A>;
7294 auto x = detail::wasm_lanes(a);
7295 std::array<wide_type, N / 2> r{};
7296 for (std::size_t i = 0; i < N / 2; ++i) {
7297 r[i] = wide_type(x[2 * i]) + x[2 * i + 1];
7299 return vector_type::load(r.data());
7301 if constexpr (
sizeof(T) == 1) {
7302 return vector_type::from_native(wasm_i16x8_extadd_pairwise_i8x16(a.to_native()));
7304 return vector_type::from_native(wasm_i32x4_extadd_pairwise_i16x8(a.to_native()));
7310 template<simd_
integer_element T, std::
size_t N, isa<> A>
7311 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
7314 auto x = detail::wasm_lanes(a);
7315 std::uint32_t r = 0;
7316 for (std::size_t i = 0; i < N; ++i) {
7317 r |= std::uint32_t(std::make_unsigned_t<T>(x[i]) >> (
sizeof(T) * 8 - 1)) << i;
7321 if constexpr (
sizeof(T) == 1) {
7322 return wasm_i8x16_bitmask(a.to_native());
7323 }
else if constexpr (
sizeof(T) == 2) {
7324 return wasm_i16x8_bitmask(a.to_native());
7325 }
else if constexpr (
sizeof(T) == 4) {
7326 return wasm_i32x4_bitmask(a.to_native());
7328 return wasm_i64x2_bitmask(a.to_native());
7334 template<simd_
integer_element T, std::
size_t N, isa<> A>
7335 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
7338 for (
auto x : detail::wasm_lanes(a)) {
7345 return wasm_v128_any_true(a.to_native());
7350 template<simd_
integer_element T, std::
size_t N, isa<> A>
7351 requires(A.has(wasm_feature::simd128)) && (
sizeof(T) * N == 16)
7354 for (
auto x : detail::wasm_lanes(a)) {
7361 if constexpr (
sizeof(T) == 1) {
7362 return wasm_i8x16_all_true(a.to_native());
7363 }
else if constexpr (
sizeof(T) == 2) {
7364 return wasm_i16x8_all_true(a.to_native());
7365 }
else if constexpr (
sizeof(T) == 4) {
7366 return wasm_i32x4_all_true(a.to_native());
7368 return wasm_i64x2_all_true(a.to_native());
constexpr simd< T, N, Arch > masked_add_zero(M m, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
constexpr simd< T, N, Arch > masked_mul(M m, simd< T, N, Arch > prior, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
constexpr simd< T, N, Arch > masked_mul_zero(M m, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
constexpr simd< U, N, Arch > mask_cast(simd< T, N, Arch > value) noexcept
constexpr simd< T, N, Arch > masked_add(M m, simd< T, N, Arch > prior, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
constexpr simd< T, N, Arch > masked_sub_zero(M m, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
constexpr simd< T, N, Arch > masked_sub(M m, simd< T, N, Arch > prior, simd< T, N, Arch > a, simd< T, N, Arch > b) noexcept
std::string_view const type
typename mask_traits< std::remove_cvref_t< T > >::type mask
constexpr simd< float, N, Arch > ceil(simd< float, N, Arch > x) noexcept
constexpr auto abs(simd< float, N, Arch > a) noexcept
constexpr simd< float, N, Arch > trunc(simd< float, N, Arch > x) noexcept
constexpr simd< float, N, Arch > floor(simd< float, N, Arch > x) noexcept
constexpr void store_simd(U *p, V value, simd_memory< A, Access >={}) noexcept(noexcept(value.template store_memory< A >(p)))
constexpr V load_simd(U const *p, simd_memory< A, Access >={}) noexcept(noexcept(V::template load_memory< A >(p)))
constexpr V load_simd_partial(U const *p, std::size_t count, typename V::value_type fill={}, simd_memory< A, Access >={}) noexcept(std::is_nothrow_default_constructible_v< U > &&std::is_nothrow_constructible_v< U, typename V::value_type & > &&std::is_nothrow_copy_assignable_v< U > &&noexcept(::native::load_simd< V >(p)))
constexpr void store_simd_partial(U *p, V value, std::size_t count, simd_memory< A, Access >={}) noexcept(std::is_nothrow_default_constructible_v< U > &&std::is_nothrow_copy_assignable_v< U > &&noexcept(::native::store_simd(p, value)))
constexpr auto operator!(wide< T, N > const &a)
constexpr auto operator~(wide< T, N > const &a)
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.
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 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 simd< T, N, A > sqrt(simd< T, N, A > v) noexcept
Compute correctly rounded square roots; negative finite lanes produce NaN.
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 all(simd< T, N, A > a) noexcept
Test whether every integer lane is nonzero.
constexpr std::size_t compress_store(T *p, std::size_t capacity, typename simd< T, N, A >::mask mask, simd< T, N, A > value) noexcept
Write only the selected prefix that fits capacity; zero writes permit null.
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< 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.
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 auto to_predicate(detail::polyfill_predicate< N, A > mask) noexcept
Preserve a logical compact predicate already in canonical form.
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< std::make_unsigned_t< T >, N, A > mask_bits(detail::polyfill_predicate< N, A > mask) noexcept
Expand each logical predicate bit into an unsigned zero/all-one lane.
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 > 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.
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 > round_even(simd< T, N, A > v) noexcept
Round floating lanes to nearest integers, choosing even at ties.
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 simd< T, N, A > broadcast(simd< T, N, A > value, imm_t< I >) noexcept
Broadcast one compile-time-selected logical lane.
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...
constexpr auto extend_high(simd< T, N, A > a) noexcept
Widen the upper half of integer lanes, preserving signedness.
constexpr simd< T, N, A > to_vector_mask(detail::polyfill_predicate< N, A > mask) noexcept
Expand a logical compact mask into canonical full-vector lanes.
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 > pmin(simd< T, N, A > a, simd< T, N, A > b) noexcept
Pseudo minimum/maximum selects the first operand for unordered or equal lanes.
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< bool, N, A > to_bool(detail::polyfill_predicate< N, A > mask) noexcept
Expand compact truth to zero-or-one Boolean data lanes.
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 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
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< float, N, A > masked_scaleb(M mask, simd< float, N, A > prior, simd< float, N, A > value, simd< float, N, A > exponent) noexcept
constexpr simd< T, N, A > bit_select(simd< U, N, A > mask, simd< T, N, A > a, simd< T, N, A > b) noexcept
Select individual representation bits without canonicalizing the bit mask.
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.
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::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.
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< float, N, A > scaleb(simd< float, N, A > value, simd< float, N, A > exponent) noexcept
Scale every logical lane, preferring each available native scaling register.
constexpr auto extend_low(simd< T, N, A > a) noexcept
Widen the lower half of integer lanes, preserving signedness.
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.
constexpr simd< float, N, A > masked_scaleb_zero(M mask, simd< float, N, A > value, simd< float, N, A > exponent) noexcept
Scale active lanes and write positive zero to every inactive lane.
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 float reduce_add(simd< float, N, A > value) noexcept
Sum binary32 logical lanes in increasing order, rounding after each addition.
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 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.
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 > 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 simd< T, N, A > expand(typename simd< T, N, A >::mask mask, simd< T, N, A > packed, simd< T, N, A > prior) noexcept
Expand a globally packed prefix into selected logical lanes, retaining prior elsewhere.
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 compaction_result< simd< T, N, A > > compress(typename simd< T, N, A >::mask mask, simd< T, N, A > value, T fill={}) noexcept
Pack selected logical lanes in increasing order, filling the suffix.
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.
Standard-library adaptations documented here for SIMD value types.
static constexpr mask_lane from_bits(U value) noexcept
Normalize nonzero bits to a true, all-one lane.
static constexpr predicate from_bitset(std::uint64_t bits) noexcept
Construct from the logical lane bitset.
constexpr predicate & operator&=(predicate b) noexcept
Apply the corresponding lane-wise AND operation in place and return *this.
constexpr predicate & operator^=(predicate b) noexcept
Apply the corresponding lane-wise XOR operation in place and return *this.
friend constexpr predicate operator==(predicate a, predicate b) noexcept
Return a mask whose lanes are true where a == b holds.
constexpr std::uint64_t to_bitset() const noexcept
Pack lane truth into low bits, with lane zero in bit zero.
friend constexpr bool none(predicate a) noexcept
Return true exactly when no logical lane is true.
constexpr predicate & operator|=(predicate b) noexcept
Apply the corresponding lane-wise OR operation in place and return *this.
friend constexpr predicate operator~(predicate a) noexcept
Invert each lane truth value, preserving the mask representation.
static constexpr predicate from_native(native_type value) noexcept
Import compact mask bits and clear bits above the lane count.
friend constexpr bool all(predicate a) noexcept
Return whether every logical lane is true.
friend constexpr predicate operator!(predicate a) noexcept
Return the lane-wise logical complement, retaining this mask type.
friend constexpr predicate operator&(predicate a, predicate b) noexcept
Bitwise AND of corresponding lane representations.
static constexpr predicate from_bitset(std::uint64_t value) noexcept
Import lane truth from the low logical-lane bits.
constexpr native_type to_native() const noexcept
Return the native storage representation.
constexpr predicate() noexcept=default
Initialize every logical lane to false.
friend constexpr predicate operator^(predicate a, predicate b) noexcept
Bitwise XOR of corresponding lane representations.
friend constexpr predicate select(predicate p, predicate a, predicate b) noexcept
Choose a in true lanes and b in false lanes; both values are already evaluated.
friend constexpr predicate operator!=(predicate a, predicate b) noexcept
Return a mask whose lanes are true where a != b holds.
static constexpr predicate unsafe_from_native(native_type value) noexcept
Import compact bits and clear bits above the lane count, just like from_native.
friend constexpr predicate operator|(predicate a, predicate b) noexcept
Bitwise OR of corresponding lane representations.
friend constexpr bool any(predicate a) noexcept
Return whether at least one logical lane is true.
friend constexpr simd operator>>(simd a, imm_t< K >) noexcept
Shift integer lanes right, extending signed lanes and reducing the count modulo their width.
friend constexpr simd operator<<(simd a, unsigned n) noexcept
Shift integer lanes left, reducing the count modulo the lane width.
constexpr simd replace(T x) const noexcept
Replace one compile-time-selected lane and preserve every other lane.
friend constexpr mask_type operator>=(simd a, simd b) noexcept
Compare lanes using their signedness; unordered floating lanes are false.
static constexpr simd load_bits(word_type const *p) noexcept
Load lane representations from corresponding unsigned words.
static constexpr simd load(T const *p) noexcept
Read exactly N elements with their natural alignment.
constexpr simd & operator/=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator|(simd a, simd b) noexcept
Unite representation bits.
constexpr void store_memory(T *p) const noexcept
Store all lanes; Align is the caller-provided pointer alignment.
constexpr simd(std::array< T, N > const &a) noexcept
Copy the array in lane order.
constexpr simd right() const noexcept
Shift every integral lane right by the immediate count.
constexpr bits_type bits() const noexcept
Return each lane as its unchanged unsigned representation.
constexpr simd left() const noexcept
Shift lanes left by K modulo the lane width.
constexpr T get() const noexcept
Extract the compile-time-selected lane as its scalar element type.
constexpr simd() noexcept=default
Initialize every lane to zero.
constexpr simd & operator^=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr mask_type operator<=(simd a, simd b) noexcept
Compare lanes using their signedness; unordered floating lanes are false.
constexpr native_type to_native() const noexcept
Return the implementation register without changing its bits.
friend constexpr simd operator&(simd a, simd b) noexcept
Intersect representation bits.
constexpr void store_partial(T *p, std::size_t n) const noexcept
Write only the first n lanes; require n <= N. Null is valid when n is zero.
static constexpr simd from_native(native_type x) noexcept
Adopt implementation storage; mask specializations normalize nonzero lanes.
constexpr native_type to_native() const noexcept
Bridge to the implementation register without numerical conversion.
constexpr simd left(unsigned count) const noexcept
WebAssembly shifts reduce the count modulo the lane width.
friend constexpr simd operator>>(simd a, unsigned n) noexcept
Shift integer lanes right, extending signed lanes and reducing the count modulo their width.
static constexpr simd load_partial(T const *p, std::size_t n, T fill={}) noexcept
Read n lanes and fill the rest; require n <= N. Null is valid when n is zero.
friend constexpr simd operator-(simd a) noexcept
Subtract or negate lanes; integer results wrap and floating signs are preserved.
friend constexpr mask_type operator>(simd a, simd b) noexcept
Compare lanes using their signedness; unordered floating lanes are false.
friend constexpr simd operator~(simd a) noexcept
Complement every representation bit (or lane truth for masks).
constexpr simd & operator+=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator*(simd a, simd b) noexcept
Multiply lanes; integer results wrap and floating results round to nearest-even.
friend constexpr simd operator<<(simd a, imm_t< K >) noexcept
Shift integer lanes left, reducing the count modulo the lane width.
static constexpr simd from_native(native_type value) noexcept
Adopt register bits unchanged; unused physical bytes are unspecified.
friend constexpr mask_type operator!=(simd a, simd b) noexcept
Compare lane inequality and return canonical mask lanes.
static constexpr simd load_memory(T const *p) noexcept
Load all lanes; Align is the caller-provided pointer alignment.
static constexpr simd unsafe_from_native(native_type x) noexcept
Adopt implementation bits; mask callers must supply canonical lanes.
constexpr simd right(unsigned count) const noexcept
WebAssembly shifts reduce the count modulo the lane width.
constexpr void store(T *p) const noexcept
Store exactly 16 unaligned bytes in lane order.
constexpr simd & operator-=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator+(simd a, simd b) noexcept
Add lanes; integer results wrap and floating results round to nearest-even.
friend constexpr simd operator-(simd a, simd b) noexcept
Subtract or negate lanes; integer results wrap and floating signs are preserved.
constexpr void store_bits(word_type *p) const noexcept
Store lane representations as corresponding unsigned words.
constexpr simd & operator|=(simd b) noexcept
Apply the corresponding lane operation and update this value.
constexpr simd right() const noexcept
Shift lanes right by K modulo the lane width.
constexpr simd(U input) noexcept
Broadcast an integer, retaining its low lane-width bits in every lane.
friend constexpr mask_type operator<(simd a, simd b) noexcept
Compare lanes using their signedness; unordered floating lanes are false.
constexpr bits_type bits() const noexcept
Reinterpret each lane as an unsigned word of the same width.
friend constexpr simd operator/(simd a, simd b) noexcept
Divide floating lanes with WebAssembly IEEE semantics.
friend constexpr mask_type operator==(simd a, simd b) noexcept
Compare lane equality and return canonical mask lanes.
constexpr simd & operator*=(simd b) noexcept
Apply the corresponding lane operation and update this value.
constexpr simd & operator&=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator^(simd a, simd b) noexcept
Exclusive-or representation bits.
static constexpr simd from_bits(bits_type x) noexcept
Adopt unsigned lane representations without numerical conversion.
constexpr simd left() const noexcept
Shift every integral lane left by the immediate count.
friend constexpr simd operator!(simd a) noexcept
Return the lane-wise logical complement, retaining this mask type.
constexpr simd & operator*=(simd b) noexcept
Apply the corresponding lane-wise multiply operation in place and return *this.
friend constexpr mask_type operator<(simd a, U b) noexcept
Return a mask whose lanes are true where a < b holds. Ordering follows the signedness of T....
friend constexpr mask_type operator>=(simd a, simd b) noexcept
Return a mask whose lanes are true where a >= b holds.
friend simd operator%(simd, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend simd operator%(U, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
constexpr simd & operator|=(simd b) noexcept
Apply the corresponding lane-wise OR operation in place and return *this.
friend constexpr simd operator^(U a, simd b) noexcept
Bitwise XOR of corresponding lane representations. Scalar operands are reduced to the lane width and ...
constexpr simd & operator^=(U b) noexcept
Apply the corresponding lane-wise XOR operation in place and return *this. Scalar operands are reduce...
friend constexpr simd operator+(simd a, U b) noexcept
Add corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width and bro...
friend constexpr mask_type operator<=(U a, simd b) noexcept
Return a mask whose lanes are true where a <= b holds. Ordering follows the signedness of T....
constexpr simd & operator+=(U b) noexcept
Apply the corresponding lane-wise add operation in place and return *this. Scalar operands are reduce...
friend constexpr mask_type operator>=(simd a, simd b) noexcept
Return a mask whose lanes are true where a >= b holds. Ordering follows the signedness of T.
friend constexpr simd operator+(simd a, simd b) noexcept
constexpr simd & operator/=(simd b) noexcept
Apply the corresponding lane-wise divide operation in place and return *this.
static constexpr simd loadu(T const *p) noexcept
Read all logical lanes without an extra alignment promise.
friend simd operator<<(simd, U)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
static constexpr simd unsafe_from_native(native_type x) noexcept
Adopt native bits and clear physical padding. If T is a mask element, every logical lane must already...
friend simd operator/(simd, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
static constexpr simd load_memory(T const *p) noexcept
Load logical lanes, assuming the template alignment in bytes.
friend simd operator%(simd, U)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend constexpr simd operator|(simd a, simd b) noexcept
Bitwise OR of corresponding lane representations.
friend simd operator>>(simd, U)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
friend constexpr simd operator|(simd a, U b) noexcept
Bitwise OR of corresponding lane representations. Scalar operands are reduced to the lane width and b...
friend constexpr simd round_even(simd a) noexcept
Round each logical lane to an integral value, ties to even, independently of the ambient rounding dir...
friend constexpr mask_type operator!=(simd a, U b) noexcept
Return a mask whose lanes are true where a != b holds. Ordering follows the signedness of T....
friend constexpr simd fma(simd a, simd b, simd c) noexcept
Compute a*b+c with one fused rounding per logical lane in the caller's floating-point environment.
static constexpr simd load_partial(T const *p, std::size_t n, T fill=T(0)) noexcept
Read exactly n logical lanes and fill the remainder; require n <= lanes. For n == 0,...
static constexpr simd unsafe_from_float32(native_type x) noexcept
Adopt raw float storage without numerical conversion or normalization.
friend constexpr simd operator*(U a, simd b) noexcept
Multiply corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width an...
constexpr simd & operator&=(simd b) noexcept
Apply the corresponding lane-wise AND operation in place and return *this.
friend constexpr simd operator&(U a, simd b) noexcept
Bitwise AND of corresponding lane representations. Scalar operands are reduced to the lane width and ...
constexpr simd left() const noexcept
Shift each lane left by compile-time K, discarding high bits; require K below the lane bit width.
friend simd operator/(simd, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend constexpr simd operator/(simd a, simd b) noexcept
constexpr std::uint64_t to_bitset() const noexcept
Pack each logical lane truth value into bit i; higher bits are zero.
constexpr native_type to_native() const noexcept
Return the native storage representation under its minimal register ABI.
constexpr simd & operator-=(U b) noexcept
Apply the corresponding lane-wise subtract operation in place and return *this. Scalar operands are r...
friend constexpr simd operator+(simd a) noexcept
Return the unchanged vector value.
static constexpr simd load_bits_partial(std::uint32_t const *p, std::size_t n, std::uint32_t fill=0) noexcept
Read n representation words and fill the remaining logical lanes; require n <= lanes....
constexpr simd & operator^=(simd b) noexcept
Apply the corresponding lane-wise XOR operation in place and return *this.
friend constexpr simd operator^(simd a, simd b) noexcept
Bitwise XOR of corresponding lane representations.
friend constexpr mask_type operator<=(simd a, U b) noexcept
Return a mask whose lanes are true where a <= b holds. Ordering follows the signedness of T....
constexpr simd(bool x) noexcept
Broadcast the supplied value to each logical lane. Unused physical lanes are zero.
friend constexpr mask_type operator<=(simd a, simd b) noexcept
Return a mask whose lanes are true where a <= b holds. Ordering follows the signedness of T.
friend constexpr simd normal_pow2(simd a) noexcept
Construct normal powers of two; require each input to be an integral exponent in [-126,...
static constexpr simd from_bitset(std::uint64_t bits) noexcept
Import lane i from bit i, clearing bits above the logical lane count.
static constexpr simd load_bits(std::uint32_t const *p) noexcept
Load exactly the logical count of binary32 words without normalizing their representations.
constexpr simd & operator&=(simd b) noexcept
Apply the corresponding lane-wise AND operation in place and return *this.
constexpr simd()=default
Default-construct the element customization; its initialization contract is retained.
friend constexpr simd operator&(simd a, U b) noexcept
Bitwise AND of corresponding lane representations. Scalar operands are reduced to the lane width and ...
friend simd operator/(U, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
static constexpr simd from_native(native_type x) noexcept
Adopt native storage without numerical conversion.
constexpr simd(std::array< T, N > const &values) noexcept
Copy one value per logical lane in array order.
constexpr simd & operator&=(U b) noexcept
Apply the corresponding lane-wise AND operation in place and return *this. Scalar operands are reduce...
friend constexpr mask_type operator>(simd a, U b) noexcept
Return a mask whose lanes are true where a > b holds. Ordering follows the signedness of T....
friend simd operator<<(simd, U)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
friend simd operator%(simd, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
constexpr simd(std::array< T, N > const &data) noexcept
Copy one value per logical lane in array order.
friend constexpr simd operator&(simd a, simd b) noexcept
Bitwise AND of corresponding lane representations.
constexpr simd & operator*=(U b) noexcept
Apply the corresponding lane-wise multiply operation in place and return *this. Scalar operands are r...
friend constexpr simd operator+(U a, simd b) noexcept
Add corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width and bro...
constexpr void store_bits(std::uint32_t *p) const noexcept
Store the exact binary32 words for every logical lane.
friend simd operator>>(simd, U)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
constexpr simd & operator+=(simd b) noexcept
Apply the corresponding lane-wise add operation in place and return *this.
friend simd operator%(simd, U)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend constexpr mask_type operator!=(U a, simd b) noexcept
Return a mask whose lanes are true where a != b holds. Ordering follows the signedness of T....
constexpr void store_partial(T *p, std::size_t n) const noexcept
Write exactly n logical lanes; require n <= lanes. For n == 0, p may be null.
friend simd operator>>(simd, simd)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
constexpr simd & operator=(simd const &)=default
Copy the stored value and return *this; no numerical conversion is performed.
constexpr simd left() const noexcept
Shift each lane left by compile-time K, discarding high bits; require K below the lane bit width.
friend constexpr mask_type operator<(simd a, simd b) noexcept
Return a mask whose lanes are true where a < b holds.
constexpr void storeu(T *p) const noexcept
Write all logical lanes without an extra alignment promise.
constexpr simd & operator-=(simd b) noexcept
Apply the corresponding lane-wise subtract operation in place and return *this.
friend simd operator<<(simd, simd)=delete
Reject runtime shift counts; use a compile-time imm<K> within the lane width.
friend constexpr simd operator-(simd a) noexcept
Negate each lane modulo 2^(sizeof(T)*8).
static constexpr simd from_float(float x) noexcept
Broadcast the float value without adding an FTZ or other normalization policy.
friend constexpr mask_type operator>(simd a, simd b) noexcept
Return a mask whose lanes are true where a > b holds. Ordering follows the signedness of T.
friend constexpr simd operator~(simd a) noexcept
Complement every bit in every lane.
friend constexpr simd operator~(simd a) noexcept
Complement every bit in every lane.
friend constexpr mask_type operator==(U a, simd b) noexcept
Return a mask whose lanes are true where a == b holds. Ordering follows the signedness of T....
static constexpr simd load(T const *p) noexcept
Read all logical lanes from an unaligned element pointer.
constexpr simd & operator*=(simd b) noexcept
Apply the corresponding lane-wise multiply operation in place and return *this.
friend constexpr mask_type operator>(U a, simd b) noexcept
Return a mask whose lanes are true where a > b holds. Ordering follows the signedness of T....
constexpr void store_memory(T *p) const noexcept
Store logical lanes, assuming the template alignment in bytes.
constexpr simd right() const noexcept
Shift each lane right by compile-time K; signed lanes extend their sign. Require K below the lane bit...
friend constexpr simd operator*(simd a, simd b) noexcept
friend constexpr simd operator^(simd a, U b) noexcept
Bitwise XOR of corresponding lane representations. Scalar operands are reduced to the lane width and ...
friend constexpr simd operator*(simd a, simd b) noexcept
Multiply corresponding lanes modulo 2^(sizeof(T)*8).
constexpr void store_bits_partial(std::uint32_t *p, std::size_t n) const noexcept
Write n exact representation words; require n <= lanes. A zero count permits null.
friend constexpr mask_type operator<=(simd a, simd b) noexcept
Return a mask whose lanes are true where a <= b holds.
friend constexpr simd operator|(simd a, simd b) noexcept
Bitwise OR of corresponding lane representations.
friend constexpr bool any(simd x) noexcept
Return true when at least one logical lane is true.
friend constexpr mask_type operator!=(simd a, simd b) noexcept
Return a mask whose lanes are true where a != b holds. Ordering follows the signedness of T.
constexpr simd() noexcept=default
Initialize the stored lane values to zero.
constexpr simd & operator|=(U b) noexcept
Apply the corresponding lane-wise OR operation in place and return *this. Scalar operands are reduced...
friend constexpr mask_type operator<(U a, simd b) noexcept
Return a mask whose lanes are true where a < b holds. Ordering follows the signedness of T....
constexpr bits_type bits() const noexcept
Return the exact binary32 lane representations in the unsigned vector.
friend constexpr simd operator|(U a, simd b) noexcept
Bitwise OR of corresponding lane representations. Scalar operands are reduced to the lane width and b...
constexpr simd(simd const &)=default
Copy the stored value without arithmetic or normalization.
constexpr simd & operator^=(simd b) noexcept
Apply the corresponding lane-wise XOR operation in place and return *this.
friend constexpr mask_type operator==(simd a, U b) noexcept
Return a mask whose lanes are true where a == b holds. Ordering follows the signedness of T....
friend constexpr simd sqrt(simd a) noexcept
Compute the native square root of each logical lane in the caller's floating-point environment.
friend simd operator%(U, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend constexpr simd operator-(simd a, U b) noexcept
Subtract corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width an...
constexpr simd & operator+=(simd b) noexcept
Apply the corresponding lane-wise add operation in place and return *this.
static constexpr simd from_bits(std::uint32_t x) noexcept
Reinterpret binary32 words as lane values without normalization; a scalar word is broadcast.
friend constexpr simd operator+(simd a, simd b) noexcept
Add corresponding lanes modulo 2^(sizeof(T)*8).
friend constexpr simd operator<<(simd a, simd counts) noexcept
Shift each unsigned 32-bit lane by its corresponding count, which must be less than 32.
constexpr simd & operator|=(simd b) noexcept
Apply the corresponding lane-wise OR operation in place and return *this.
friend constexpr simd operator-(simd a, simd b) noexcept
Subtract corresponding lanes modulo 2^(sizeof(T)*8).
friend constexpr mask_type operator>=(U a, simd b) noexcept
Return a mask whose lanes are true where a >= b holds. Ordering follows the signedness of T....
constexpr simd(T x) noexcept
Broadcast the supplied value to each logical lane. Unused physical lanes are zero.
friend constexpr simd operator-(simd a) noexcept
Negate every logical lane; floating-point lanes change sign.
static constexpr simd from_bits(bits_type x) noexcept
Reinterpret binary32 words as lane values without normalization; a scalar word is broadcast.
friend constexpr mask_type operator>(simd a, simd b) noexcept
Return a mask whose lanes are true where a > b holds.
friend constexpr mask_type operator<(simd a, simd b) noexcept
Return a mask whose lanes are true where a < b holds. Ordering follows the signedness of T.
friend simd operator/(simd, U)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
friend constexpr simd operator-(U a, simd b) noexcept
Subtract corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width an...
friend constexpr simd select(M mask, simd a, simd b) noexcept
Choose a in true mask lanes and b in false lanes; both values are already evaluated.
constexpr bits_type to_bits() const noexcept
Return the exact binary32 lane representations in the unsigned vector.
static constexpr simd from_native(native_type x) noexcept
Import native lanes, normalizing mask elements; other elements retain their bits. Clear physical padd...
constexpr simd(native_type x) noexcept
Adopt native lane storage and clear physical padding. Mask elements must already be canonical.
friend constexpr simd operator*(simd a, U b) noexcept
Multiply corresponding lanes modulo 2^(sizeof(T)*8). Scalar operands are reduced to the lane width an...
friend constexpr mask_type operator>=(simd a, U b) noexcept
Return a mask whose lanes are true where a >= b holds. Ordering follows the signedness of T....
friend constexpr mask_type operator==(simd a, simd b) noexcept
Return a mask whose lanes are true where a == b holds. Ordering follows the signedness of T.
constexpr storage_type to_storage() const noexcept
Return the corresponding four-lane storage vector without changing logical lane bits.
friend simd operator/(U, simd)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
static constexpr simd from_storage(storage_type x) noexcept
Copy the first N lanes from a four-lane storage value and clear physical padding.
static constexpr simd load_partial(T const *p, std::size_t n, T fill={}) noexcept
Read exactly n logical lanes and fill the remainder; require n <= lanes. For n == 0,...
friend constexpr simd operator^(simd a, simd b) noexcept
Bitwise XOR of corresponding lane representations.
friend constexpr simd operator-(simd a, simd b) noexcept
constexpr simd right() const noexcept
Shift each lane right by compile-time K; signed lanes extend their sign. Require K below the lane bit...
friend constexpr simd operator&(simd a, simd b) noexcept
Bitwise AND of corresponding lane representations.
friend constexpr bool none(simd x) noexcept
Return true exactly when no logical lane is true.
constexpr native_type to_native() const noexcept
Return the native storage representation, including physical padding when present.
friend constexpr bool all(simd x) noexcept
Return true exactly when every logical lane is true.
constexpr void store(T *p) const noexcept
Write all logical lanes to an unaligned element pointer.
friend simd operator/(simd, U)=delete
Integer division and remainder are not provided; do not fall back to native-register conversions.
constexpr simd & operator-=(simd b) noexcept
Apply the corresponding lane-wise subtract operation in place and return *this.
friend constexpr bool any(simd a) noexcept
Return whether at least one logical lane is true.
constexpr simd & operator|=(simd b) noexcept
Apply the corresponding lane-wise OR operation in place and return *this.
friend constexpr simd operator|(simd a, simd b) noexcept
Bitwise OR of corresponding lane representations.
friend constexpr simd operator!(simd a) noexcept
Return the lane-wise logical complement, retaining this mask type.
constexpr native_type to_native() const noexcept
Return the native storage representation.
static constexpr simd load_partial(bool const *p, std::size_t count, bool fill=false) noexcept
Read exactly n logical lanes and fill the remainder; require n <= lanes. For n == 0,...
friend constexpr bool none(simd a) noexcept
Return true exactly when no logical lane is true.
constexpr void store_partial(bool *p, std::size_t count) const noexcept
Write exactly n logical lanes; require n <= lanes. For n == 0, p may be null.
friend constexpr simd operator&(simd a, simd b) noexcept
Bitwise AND of corresponding lane representations.
static constexpr simd load_memory(bool const *p) noexcept
Load exactly the logical lanes; the template alignment is a caller promise, never permission to read ...
constexpr simd & operator&=(simd b) noexcept
Apply the corresponding lane-wise AND operation in place and return *this.
constexpr simd(std::array< bool, N > const &value) noexcept
Read all logical lanes from an unaligned element pointer.
friend constexpr simd select(M m, simd a, simd b) noexcept
Choose a in true lanes and b in false lanes; both values are already evaluated.
static constexpr simd unsafe_from_native(native_type value) noexcept
Adopt storage with the precondition that every Boolean byte is zero or one.
friend constexpr bool all(simd a) noexcept
Return whether every logical lane is true.
friend constexpr simd operator~(simd a) noexcept
Invert each lane truth value, preserving the mask representation.
friend constexpr mask_type operator!=(simd a, simd b) noexcept
Return a mask whose lanes are true where a != b holds.
constexpr simd() noexcept
Initialize every logical lane to false.
constexpr simd & operator^=(simd b) noexcept
Apply the corresponding lane-wise XOR operation in place and return *this.
static constexpr simd from_native(native_type value) noexcept
Import native byte lanes, converting each nonzero byte to Boolean one.
constexpr void store_memory(bool *p) const noexcept
Store exactly the logical lanes; the template alignment is a caller promise, never permission to writ...
friend constexpr mask_type operator==(simd a, simd b) noexcept
Return a mask whose lanes are true where a == b holds.
friend constexpr simd operator^(simd a, simd b) noexcept
Bitwise XOR of corresponding lane representations.
constexpr simd(bool value) noexcept
Broadcast the supplied truth value to every logical lane.
static constexpr simd load(bool const *p) noexcept
Read all logical lanes from an unaligned element pointer.
constexpr void store(bool *p) const noexcept
Write all logical lanes to an unaligned element pointer.
friend constexpr simd normal_pow2(simd n)
Construct normal powers of two; require integral exponents in [-126,127].
static constexpr simd load(float const *p)
Load every logical lane; no extra alignment is required.
friend constexpr mask_type operator>=(simd a, simd b) noexcept
Return a mask whose lanes are true where a >= b holds. NaN lanes yield false.
friend constexpr simd operator-(simd a)
Negate every logical lane; floating-point lanes change sign.
static constexpr simd loadu(float const *p)
Synonym for an unaligned full-vector load.
static constexpr simd from_bits(bits_type bits) noexcept
Reinterpret unsigned lane words as binary32, without normalization.
friend constexpr mask_type operator<(simd a, simd b)
Return a mask whose lanes are true where a < b holds. NaN lanes yield false.
constexpr void store_bits(std::uint32_t *p) const noexcept
Store exact binary32 representations as uint32_t words.
constexpr simd & operator+=(simd b) noexcept
Apply the corresponding lane-wise add operation in place and return *this.
static constexpr simd load_bits(std::uint32_t const *p) noexcept
Load exact binary32 representations from uint32_t words.
constexpr simd & operator*=(simd b) noexcept
Apply the corresponding lane-wise multiply operation in place and return *this.
static constexpr simd from_float(float x) noexcept
Broadcast one binary32 value to every lane.
friend constexpr mask_type operator==(simd a, simd b)
Return a mask whose lanes are true where a == b holds. NaN lanes yield false.
static constexpr simd load_memory(float const *p) noexcept
Load full lanes, assuming Alignment-byte pointer alignment.
friend constexpr simd sqrt(simd a)
Compute the native square root in every lane.
constexpr void storeu(float *p) const
Synonym for an unaligned full-vector store.
friend constexpr mask_type operator<=(simd a, simd b) noexcept
Return a mask whose lanes are true where a <= b holds. NaN lanes yield false.
constexpr bits_type to_bits() const noexcept
Synonym for bits(): preserve all binary32 representation bits.
friend constexpr simd operator+(simd a, simd b)
Add corresponding floating-point lanes using the caller's rounding and denormal environment.
constexpr simd & operator=(simd const &)=default
Copy the stored value and return *this; no numerical conversion is performed.
friend constexpr simd round_even(simd a)
Round to an integral value, ties to even, independent of ambient direction.
friend constexpr simd operator*(simd a, simd b)
Multiply corresponding floating-point lanes using the caller's rounding and denormal environment.
friend constexpr simd select(M m, simd a, simd b)
Choose a where the canonical mask is true, otherwise b; both operands are evaluated.
static constexpr simd from_bits(std::uint32_t bits) noexcept
Reinterpret unsigned lane words as binary32, without normalization.
friend constexpr simd operator/(simd a, simd b)
Divide corresponding floating-point lanes using the caller's rounding and denormal environment.
constexpr void store_bits_partial(std::uint32_t *p, std::size_t n) const noexcept
Store exactly n representation words; require n <= lanes.
constexpr simd(simd const &)=default
Copy the stored value without arithmetic or normalization.
static constexpr simd load_bits_partial(std::uint32_t const *p, std::size_t n, std::uint32_t fill=0) noexcept
Load n words and fill the remaining lanes; require n <= lanes.
constexpr simd(float x)
Broadcast x to all lanes.
constexpr simd & operator/=(simd b) noexcept
Apply the corresponding lane-wise divide operation in place and return *this.
constexpr bits_type bits() const noexcept
Project exact binary32 lane words into the unsigned vector.
friend constexpr mask_type operator!=(simd a, simd b) noexcept
Return a mask whose lanes are true where a != b holds. NaN lanes compare unequal.
static constexpr simd unsafe_from_float32(native_type x) noexcept
Adopt native raw float storage; this raw type adds no normalization.
constexpr void store_memory(float *p) const noexcept
Store full lanes, assuming Alignment-byte pointer alignment.
friend constexpr mask_type operator>(simd a, simd b)
Return a mask whose lanes are true where a > b holds. NaN lanes yield false.
static constexpr simd from_native(native_type x) noexcept
Adopt native register storage without changing its bits.
constexpr native_type to_native() const noexcept
Project native register storage without a numerical conversion.
constexpr simd(std::array< float, 4 > const &values) noexcept
Load one lane from each array element, in array order.
constexpr void store(float *p) const
Store every logical lane; no extra alignment is required.
simd()=default
Default initialization leaves storage unspecified; value initialization with braces zero-initializes ...
friend constexpr simd fma(simd a, simd b, simd c)
Compute a*b+c with one fused rounding per lane.
constexpr simd & operator-=(simd b) noexcept
Apply the corresponding lane-wise subtract operation in place and return *this.
constexpr simd(X... x) noexcept((noexcept(static_cast< float >(x)) &&...))
Convert one argument per lane; exceptions follow those named-lvalue conversions.
friend constexpr simd operator-(simd a, simd b)
Subtract corresponding floating-point lanes using the caller's rounding and denormal environment.
friend constexpr simd normal_pow2(simd n)
Construct normal powers of two; require integral exponents in [-126,127].
friend constexpr mask_type operator>=(simd a, simd b) noexcept
Return a mask whose lanes are true where a >= b holds. NaN lanes yield false.
friend constexpr simd operator-(simd a)
Negate every logical lane; floating-point lanes change sign.
simd()=default
Default initialization leaves storage unspecified; value initialization with braces zero-initializes ...
friend constexpr mask_type operator<(simd a, simd b)
Return a mask whose lanes are true where a < b holds. NaN lanes yield false.
static constexpr simd load_memory(float const *p) noexcept
Load full lanes, assuming Alignment-byte pointer alignment.
constexpr void store(float *p) const
Store every logical lane; no extra alignment is required.
static constexpr simd load_bits_partial(std::uint32_t const *p, std::size_t n, std::uint32_t fill=0) noexcept
Load n words and fill the remaining lanes; require n <= lanes.
constexpr simd & operator/=(simd b) noexcept
Apply the corresponding lane-wise divide operation in place and return *this.
constexpr void store_memory(float *p) const noexcept
Store full lanes, assuming Alignment-byte pointer alignment.
friend constexpr mask_type operator==(simd a, simd b)
Return a mask whose lanes are true where a == b holds. NaN lanes yield false.
constexpr native_type to_native() const noexcept
Project native register storage without a numerical conversion.
friend constexpr simd sqrt(simd a)
Compute the native square root in every lane.
friend constexpr mask_type operator<=(simd a, simd b) noexcept
Return a mask whose lanes are true where a <= b holds. NaN lanes yield false.
friend constexpr simd operator+(simd a, simd b)
Add corresponding floating-point lanes using the caller's rounding and denormal environment.
constexpr simd(float x)
Broadcast x to all lanes.
static constexpr simd load(float const *p)
Load every logical lane; no extra alignment is required.
static constexpr simd load_bits(std::uint32_t const *p) noexcept
Load exact binary32 representations from uint32_t words.
constexpr simd & operator-=(simd b) noexcept
Apply the corresponding lane-wise subtract operation in place and return *this.
friend constexpr simd round_even(simd a)
Round to an integral value, ties to even, independent of ambient direction.
static constexpr simd from_float(float x) noexcept
Broadcast one binary32 value to every lane.
friend constexpr simd operator*(simd a, simd b)
Multiply corresponding floating-point lanes using the caller's rounding and denormal environment.
static constexpr simd loadu(float const *p)
Synonym for an unaligned full-vector load.
friend constexpr simd select(M m, simd a, simd b)
Choose a where the canonical mask is true, otherwise b; both operands are evaluated.
friend constexpr simd operator/(simd a, simd b)
Divide corresponding floating-point lanes using the caller's rounding and denormal environment.
constexpr simd & operator*=(simd b) noexcept
Apply the corresponding lane-wise multiply operation in place and return *this.
constexpr simd & operator+=(simd b) noexcept
Apply the corresponding lane-wise add operation in place and return *this.
constexpr void storeu(float *p) const
Synonym for an unaligned full-vector store.
static constexpr simd unsafe_from_float32(native_type x) noexcept
Adopt native raw float storage; this raw type adds no normalization.
friend constexpr mask_type operator!=(simd a, simd b) noexcept
Return a mask whose lanes are true where a != b holds. NaN lanes compare unequal.
constexpr simd(simd const &)=default
Copy the stored value without arithmetic or normalization.
constexpr bits_type bits() const noexcept
Project exact binary32 lane words into the unsigned vector.
constexpr void store_bits_partial(std::uint32_t *p, std::size_t n) const noexcept
Store exactly n representation words; require n <= lanes.
constexpr simd(X... x) noexcept((noexcept(static_cast< float >(x)) &&...))
Convert one argument per lane; exceptions follow those named-lvalue conversions.
static constexpr simd from_native(native_type x) noexcept
Adopt native register storage without changing its bits.
constexpr bits_type to_bits() const noexcept
Synonym for bits(): preserve all binary32 representation bits.
friend constexpr mask_type operator>(simd a, simd b)
Return a mask whose lanes are true where a > b holds. NaN lanes yield false.
constexpr simd(std::array< float, 8 > const &values) noexcept
Load one lane from each array element, in array order.
static constexpr simd from_bits(bits_type bits) noexcept
Reinterpret unsigned lane words as binary32, without normalization.
constexpr simd & operator=(simd const &)=default
Copy the stored value and return *this; no numerical conversion is performed.
constexpr void store_bits(std::uint32_t *p) const noexcept
Store exact binary32 representations as uint32_t words.
static constexpr simd from_bits(std::uint32_t bits) noexcept
Reinterpret unsigned lane words as binary32, without normalization.
friend constexpr simd fma(simd a, simd b, simd c)
Compute a*b+c with one fused rounding per lane.
friend constexpr simd operator-(simd a, simd b)
Subtract corresponding floating-point lanes using the caller's rounding and denormal environment.
constexpr std::uint64_t to_bitset() const noexcept
Gather lane truth values into low scalar bits.
friend constexpr simd operator|(simd a, simd b) noexcept
Unite representation bits.
friend constexpr simd operator!(simd a) noexcept
Complement every mask lane.
friend constexpr bool any(simd v) noexcept
Test whether at least one lane is true.
constexpr simd & operator^=(simd b) noexcept
Apply the corresponding lane operation and update this value.
constexpr simd & operator&=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator!=(simd a, simd b) noexcept
Compare lane inequality and return canonical mask lanes.
friend constexpr bool all(simd v) noexcept
Test whether every lane is true.
static constexpr simd load_memory(value_type const *p) noexcept
Load all lanes; Align is the caller-provided pointer alignment.
constexpr native_type to_native() const noexcept
Return the implementation register without changing its bits.
static constexpr simd from_native(native_type x) noexcept
Adopt implementation storage; mask specializations normalize nonzero lanes.
friend constexpr simd operator&(simd a, simd b) noexcept
Intersect representation bits.
constexpr void store(value_type *p) const noexcept
Write exactly N canonical mask lane objects.
friend constexpr bool none(simd v) noexcept
Test whether no lane is true.
constexpr std::uint64_t bits() const noexcept
Return the compact lane truth bitset.
static constexpr simd unsafe_from_native(native_type x) noexcept
Adopt implementation bits; mask callers must supply canonical lanes.
constexpr simd() noexcept=default
Initialize every lane to zero.
friend constexpr simd operator==(simd a, simd b) noexcept
Compare lane equality and return canonical mask lanes.
constexpr simd & operator|=(simd b) noexcept
Apply the corresponding lane operation and update this value.
friend constexpr simd operator~(simd a) noexcept
Complement every representation bit (or lane truth for masks).
constexpr void store_memory(value_type *p) const noexcept
Store all lanes; Align is the caller-provided pointer alignment.
static constexpr simd load(value_type const *p) noexcept
Read exactly N canonical mask lane objects.
static constexpr simd from_bitset(std::uint64_t bits) noexcept
Expand low scalar bits to canonical mask lanes, ignoring excess bits.
friend constexpr simd select(simd m, simd a, simd b) noexcept
Choose mask lanes from a where m is true, otherwise from b.
friend constexpr simd operator^(simd a, simd b) noexcept
Exclusive-or representation bits.
Omitted architecture arguments use the native.simd provider's baseline.