native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
simd_family.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3// Repeated for each disjoint backend family under its function target scope.
4// Storage, masks and operations use the selected compile-time ISA profile.
5#include "native/simd/common_body.h"
6
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;
19 return true;
20 }();
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);
24 return int(D);
25 }
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) {
33 sizeof(result<Self>);
34 { result<Self>::template load_memory<1>(p) } -> std::same_as<result<Self>>;
35 };
36 }
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);
44 };
45 }
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>);
50 else return false;
51 }();
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());
56 } else {
57 std::array<T,4> values{};
58 self.template store_memory<1>(values.data());
59 return __builtin_bit_cast(swizzle_native<T>,values);
60 }
61 }
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));
66 } else {
67 auto values=__builtin_bit_cast(std::array<T,4>,words);
68 return V::template load_memory<1>(values.data());
69 }
70 }
71 template<class Self>
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]]));
78 } else {
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);
83 }
84 }
85 template<class Self>
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);
92 }();
93 auto shuffled=__builtin_shufflevector(before,replacement,slot<0>(),slot<1>(),slot<2>(),slot<3>());
94 self=from_words<Self>(shuffled);
95 return rhs;
96 }
97 };
98}
99namespace native::detail {
100 // Properties are compiler accessors, not proxy objects: a read owns its lanes,
101 // and assignment materializes the complete right side before any scatter.
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>>;
105 // Pattern: [swizzle names](../../README.md#swizzle-names).
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); \
111 } \
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); \
116 } \
117 __declspec(property(get=get_##NAME, put=set_##NAME)) result<K> NAME;
118
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)
124
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)
130
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)
136
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)
142
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)
148
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)
154
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)
172
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
182 };
183}
184
185namespace native {
186 namespace detail::NATIVE_BACKEND {
187 // Register storage can exist without this backend's arithmetic operations.
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
191#endif
192#if NATIVE_HAS_AVX2
193 || N==8
194#endif
195#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
196 || N==16
197#endif
198 ;
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);
202 }();
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
208 N>1 && (B>=4
209#if NATIVE_HAS_AVX512BW
210 || B==1 || B==2
211#endif
212 ) && (B*N==64
213#if NATIVE_HAS_AVX512VL
214 || B*N==16 || B*N==32
215#endif
216 );
217#else
218 false;
219#endif
220 template <std::size_t B,std::size_t N> inline constexpr bool mask_shape = N==1 || (B*N==64 && mask_compact<B,N>)
221#if NATIVE_HAS_AVX2
222 || B*N==16 || B*N==32
223#endif
224#if NATIVE_HAS_ARM_NEON
225 || B*N==16
226#endif
227 ;
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;
231 }();
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>>>;
234
235 template<class U> struct mask_scalar_ops {
236 using native_type=U;
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); }
247 };
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>>>;
251#if NATIVE_HOST_X86
252 hint_target("sse2")
253#endif
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>);
257 }
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);
264 else
265#endif
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);
270#else
271 else return native_type(a&b);
272#endif
273#else
274 return native_type(a&b);
275#endif
276 }
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);
282 else
283#endif
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);
288#else
289 else return native_type(a|b);
290#endif
291#else
292 return native_type(a|b);
293#endif
294 }
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);
300 else
301#endif
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);
306#else
307 else return native_type(a^b);
308#endif
309#else
310 return native_type(a^b);
311#endif
312 }
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>);
315 // Partial mask8 values complement only their N logical lanes.
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);
320#endif
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);
325#else
326 else return native_type(~a);
327#endif
328#else
329 else return native_type(~a);
330#endif
331 }
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)); }
336 };
337 template <std::size_t B,std::size_t N> struct mask_vector_ops {
338#if NATIVE_HAS_AVX2
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 {
341 if consteval {
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);
345 }
346 if constexpr (B*N==16) return _mm_set1_epi32(a?-1:0);
347 else return _mm256_set1_epi32(a?-1:0);
348 }
349 static hint_inline constexpr hint_const native_type bit_and(native_type a,native_type b) noexcept {
350 if consteval {
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);
355 }
356 if constexpr (B*N==16) return _mm_and_si128(a,b);
357 else return _mm256_and_si256(a,b);
358 }
359 static hint_inline constexpr hint_const native_type bit_or(native_type a,native_type b) noexcept {
360 if consteval {
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);
365 }
366 if constexpr (B*N==16) return _mm_or_si128(a,b);
367 else return _mm256_or_si256(a,b);
368 }
369 static hint_inline constexpr hint_const native_type bit_xor(native_type a,native_type b) noexcept {
370 if consteval {
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);
375 }
376 if constexpr (B*N==16) return _mm_xor_si128(a,b);
377 else return _mm256_xor_si256(a,b);
378 }
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 {
381 if consteval {
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);
385 }
386 auto z=broadcast(false);
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));
392 } else {
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));
397 }
398 }
399 static hint_inline constexpr hint_const bool any(native_type a) noexcept {
400 if consteval {
401 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
402 for(auto lane:lanes) if(lane!=0) return true;
403 return false;
404 }
405 if constexpr (B*N==16) return _mm_testz_si128(a,a)==0;
406 else return _mm256_testz_si256(a,a)==0;
407 }
408 static hint_inline constexpr hint_const bool all(native_type a) noexcept {
409 if consteval {
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;
412 return true;
413 }
414 if constexpr (B*N==16) return _mm_movemask_epi8(a)==0xffff;
415 else return _mm256_movemask_epi8(a)==-1;
416 }
417 static hint_inline constexpr hint_const std::uint64_t bits(native_type a) noexcept {
418 if consteval {
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;
422 return result;
423 }
424 std::uint32_t bytes;
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;
429 return result;
430 }
431 static hint_inline constexpr hint_const native_type from_bits(std::uint64_t bits) noexcept {
432 if consteval {
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);
436 }
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()));
441 }
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 {
445 if consteval {
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);
449 }
450 return vdupq_n_u8(a?0xff:0);
451 }
452 static hint_inline constexpr hint_const native_type bit_and(native_type a,native_type b) noexcept {
453 if consteval {
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);
458 }
459 return vandq_u8(a,b);
460 }
461 static hint_inline constexpr hint_const native_type bit_or(native_type a,native_type b) noexcept {
462 if consteval {
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);
467 }
468 return vorrq_u8(a,b);
469 }
470 static hint_inline constexpr hint_const native_type bit_xor(native_type a,native_type b) noexcept {
471 if consteval {
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);
476 }
477 return veorq_u8(a,b);
478 }
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 {
481 if consteval {
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);
485 }
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))));
490 }
491 static hint_inline constexpr hint_const bool any(native_type a) noexcept {
492 if consteval {
493 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
494 for(auto lane:lanes) if(lane!=0) return true;
495 return false;
496 }
497 return vmaxvq_u8(a)!=0;
498 }
499 static hint_inline constexpr hint_const bool all(native_type a) noexcept {
500 if consteval {
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;
503 return true;
504 }
505 return vminvq_u8(a)==0xff;
506 }
507 static hint_inline constexpr hint_const std::uint64_t bits(native_type a) noexcept {
508 if consteval {
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;
512 return result;
513 }
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;
517 return result;
518 }
519 static hint_inline constexpr hint_const native_type from_bits(std::uint64_t bits) noexcept {
520 if consteval {
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);
524 }
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());
528 }
529#endif
530 };
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 {
535 if consteval {
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);
539 }
540 return _mm512_set1_epi32(a?-1:0);
541 }
542 static hint_inline constexpr hint_const native_type bit_and(native_type a,native_type b) noexcept {
543 if consteval {
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);
548 }
549 return _mm512_and_si512(a,b);
550 }
551 static hint_inline constexpr hint_const native_type bit_or(native_type a,native_type b) noexcept {
552 if consteval {
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);
557 }
558 return _mm512_or_si512(a,b);
559 }
560 static hint_inline constexpr hint_const native_type bit_xor(native_type a,native_type b) noexcept {
561 if consteval {
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);
566 }
567 return _mm512_xor_si512(a,b);
568 }
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 {
571 if consteval {
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;
575 return result;
576 }
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());
581 }
582 static hint_inline constexpr hint_const native_type from_bits(std::uint64_t a) noexcept {
583 if consteval {
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);
587 }
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);
592 }
593 static hint_inline constexpr hint_const native_type normalize(native_type a) noexcept {
594 if consteval {
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);
598 }
599 return from_bits(bits(a));
600 }
601 static hint_inline constexpr hint_const bool any(native_type a) noexcept {
602 if consteval {
603 auto lanes=__builtin_bit_cast(std::array<mask_word<B>,N>,a);
604 for(auto lane:lanes) if(lane!=0) return true;
605 return false;
606 }
607 return bits(a)!=0;
608 }
609 static hint_inline constexpr hint_const bool all(native_type a) noexcept {
610 if consteval {
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;
613 return true;
614 }
615 return bits(a)==mask_low_bits<N>;
616 }
617#endif
618 };
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
625 || N==32 || N==64
626#endif
627 );
628#else
629 false;
630#endif
631 }
632
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> {
639 static constexpr isa<> architecture=Arch;
640 template <class X> using rebind = simd<X,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); }
647
648 using value_type=mask_lane<U>;
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;
654 using predicate_type = predicate<N,Arch>;
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())) {}
668 // Safe native import interprets each whole U-sized lane as nonzero truth.
670 [[nodiscard]] static hint_inline hint_const constexpr simd from_native(native_type value) noexcept { return simd(raw{},ops::normalize(value)); }
671 // Caller promises zero/all-ones for EVERY lane. Arbitrary bitselect masks
672 // must use bit_select instead, never this canonical predicate domain.
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 {
683 if consteval {
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);
687 }
688 native_type value;std::memcpy(&value,static_cast<void const *>(p),sizeof(value));return unsafe_from_native(value);
689 }
691 hint_inline constexpr void store(value_type * p) const noexcept {
692 if consteval {
693 auto bits=to_bitset();
694 for(std::size_t i=0;i<N;++i) p[i]=value_type(((bits>>i)&1)!=0);
695 return;
696 }
697 std::memcpy(static_cast<void *>(p),&value_,sizeof(value_));
698 }
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; }
714 hint_inline constexpr simd & operator&=(simd b) noexcept { return *this=*this&b; }
716 hint_inline constexpr simd & operator|=(simd b) noexcept { return *this=*this|b; }
718 hint_inline constexpr simd & operator^=(simd b) noexcept { return *this=*this^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); }
726 [[nodiscard]] friend hint_inline hint_const constexpr simd select(simd predicate,simd a,simd b) noexcept { return (predicate&a)|(~predicate&b); }
727 private:
728 struct raw {};
729 hint_inline constexpr simd(raw,native_type value) noexcept : value_(value) {}
730 native_type value_;
731 };
732
736
737 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::predicate_shape<N>
738 struct predicate<N,Arch> {
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;
745 hint_inline constexpr predicate() noexcept = default;
747 explicit hint_inline constexpr predicate(bool value) noexcept : value_(ops::broadcast(value)) {}
749#if NATIVE_HOST_X86
750 hint_target("sse2")
751#endif
752 [[nodiscard]] static hint_inline hint_const constexpr predicate from_native(native_type value) noexcept { return predicate(raw{},ops::normalize(value)); }
754#if NATIVE_HOST_X86
755 hint_target("sse2")
756#endif
757 [[nodiscard]] static hint_inline hint_const constexpr predicate unsafe_from_native(native_type value) noexcept { return from_native(value); }
759#if NATIVE_HOST_X86
760 hint_target("sse2")
761#endif
762 [[nodiscard]] hint_inline hint_pure constexpr native_type to_native() const noexcept { return value_; }
764#if NATIVE_HOST_X86
765 hint_target("sse2")
766#endif
767 [[nodiscard]] static hint_inline hint_const constexpr predicate from_bitset(std::uint64_t value) noexcept { return from_native(native_type(value)); }
769#if NATIVE_HOST_X86
770 // Compact masks hold scalar bits even when their vector profile is stronger.
771 hint_target("sse2")
772#endif
773 [[nodiscard]] hint_inline hint_pure constexpr std::uint64_t to_bitset() const noexcept { return value_; }
775 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator~(predicate a) noexcept { return predicate(raw{},ops::bit_not(a.value_)); }
777 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator!(predicate a) noexcept { return ~a; }
779 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator&(predicate a,predicate b) noexcept { return predicate(raw{},ops::bit_and(a.value_,b.value_)); }
781 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator|(predicate a,predicate b) noexcept { return predicate(raw{},ops::bit_or(a.value_,b.value_)); }
783 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator^(predicate a,predicate b) noexcept { return predicate(raw{},ops::bit_xor(a.value_,b.value_)); }
785 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator==(predicate a,predicate b) noexcept { return ~(a^b); }
787 [[nodiscard]] friend hint_inline hint_const constexpr predicate operator!=(predicate a,predicate b) noexcept { return a^b; }
789 hint_inline constexpr predicate & operator&=(predicate b) noexcept { return *this=*this&b; }
791 hint_inline constexpr predicate & operator|=(predicate b) noexcept { return *this=*this|b; }
793 hint_inline constexpr predicate & operator^=(predicate b) noexcept { return *this=*this^b; }
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); }
801 [[nodiscard]] friend hint_inline hint_const constexpr predicate select(predicate p,predicate a,predicate b) noexcept { return (p&a)|(~p&b); }
802 private:
803 struct raw {};
804#if NATIVE_HOST_X86
805 hint_target("sse2")
806#endif
807 hint_inline constexpr predicate(raw,native_type value) noexcept : value_(value) {}
808 native_type value_=0;
809 };
810
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>>;
815 }
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());
827 }
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];
832 }
833 }
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> &&
837 requires { typename simd<U,N,Arch>::native_type; typename simd<T,N,Arch>::native_type; }
838 [[nodiscard]] hint_inline constexpr hint_const simd<U,N,Arch> mask_cast(simd<T,N,Arch> value) noexcept {
839 if constexpr(sizeof(U)==sizeof(T)) return simd<U,N,Arch>::unsafe_from_native(value.to_native());
840 else return simd<U,N,Arch>::from_bitset(value.to_bitset());
841 }
842
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; }
845 [[nodiscard]] hint_inline constexpr hint_const predicate<N,Arch> to_predicate(simd<T,N,Arch> value) noexcept {
846 if consteval { return predicate<N,Arch>::from_bitset(value.to_bitset()); }
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();
850 if constexpr(sizeof(T)==1) return predicate<N,Arch>::from_native(_mm_cmpneq_epi8_mask(x,z));
851 else if constexpr(sizeof(T)==2) return predicate<N,Arch>::from_native(_mm_cmpneq_epi16_mask(x,z));
852 else if constexpr(sizeof(T)==4) return predicate<N,Arch>::from_native(_mm_cmpneq_epi32_mask(x,z));
853 else return predicate<N,Arch>::from_native(_mm_cmpneq_epi64_mask(x,z));
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();
856 if constexpr(sizeof(T)==1) return predicate<N,Arch>::from_native(_mm256_cmpneq_epi8_mask(x,z));
857 else if constexpr(sizeof(T)==2) return predicate<N,Arch>::from_native(_mm256_cmpneq_epi16_mask(x,z));
858 else if constexpr(sizeof(T)==4) return predicate<N,Arch>::from_native(_mm256_cmpneq_epi32_mask(x,z));
859 else return predicate<N,Arch>::from_native(_mm256_cmpneq_epi64_mask(x,z));
860 } else
861#endif
862 return predicate<N,Arch>::from_bitset(value.to_bitset());
863 }
864
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; }
867 [[nodiscard]] hint_inline constexpr hint_const simd<T,N,Arch> to_vector_mask(predicate<N,Arch> value) noexcept {
868 if consteval { return simd<T,N,Arch>::from_bitset(value.to_bitset()); }
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();
872 if constexpr(sizeof(T)==1) return simd<T,N,Arch>::unsafe_from_native(_mm_maskz_set1_epi8(k,-1));
873 else if constexpr(sizeof(T)==2) return simd<T,N,Arch>::unsafe_from_native(_mm_maskz_set1_epi16(k,-1));
874 else if constexpr(sizeof(T)==4) return simd<T,N,Arch>::unsafe_from_native(_mm_maskz_set1_epi32(k,-1));
875 else return simd<T,N,Arch>::unsafe_from_native(_mm_maskz_set1_epi64(k,-1));
876 } else if constexpr(sizeof(T)*N==32 && ::NATIVE_BACKEND_NAMESPACE::mask_compact<sizeof(T),N>) {
877 auto k=value.to_native();
878 if constexpr(sizeof(T)==1) return simd<T,N,Arch>::unsafe_from_native(_mm256_maskz_set1_epi8(k,-1));
879 else if constexpr(sizeof(T)==2) return simd<T,N,Arch>::unsafe_from_native(_mm256_maskz_set1_epi16(k,-1));
880 else if constexpr(sizeof(T)==4) return simd<T,N,Arch>::unsafe_from_native(_mm256_maskz_set1_epi32(k,-1));
881 else return simd<T,N,Arch>::unsafe_from_native(_mm256_maskz_set1_epi64(k,-1));
882 } else
883#endif
884 return simd<T,N,Arch>::from_bitset(value.to_bitset());
885 }
886}
887
888namespace native {
889 namespace detail::NATIVE_BACKEND {
890 template<std::size_t N> [[nodiscard]] hint_inline hint_const constexpr auto bool_ones() noexcept {
891 if consteval {
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);
895 }
896 if constexpr(N==1) return std::uint8_t(1);
897#if NATIVE_HAS_AVX2
898 else if constexpr(N==16) return _mm_set1_epi8(1);
899 else if constexpr(N==32) return _mm256_set1_epi8(1);
900#endif
901#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512BW
902 else if constexpr(N==64) return _mm512_set1_epi8(1);
903#endif
904#if NATIVE_HAS_ARM_NEON
905 else return vdupq_n_u8(1);
906#endif
907 }
908 }
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;
915 template <class X> using rebind = simd<X,N,Arch>;
917 template <std::size_t A = 1>
918 [[nodiscard]] static hint_inline constexpr simd load_memory(bool const * p) noexcept { return load(p); }
920 template <std::size_t A = 1>
921 hint_inline constexpr void store_memory(bool * p) const noexcept { store(p); }
922
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;
927 using vector_mask_type=simd<mask8,N,Arch>;
928 using mask_type=::NATIVE_BACKEND_NAMESPACE::comparison_mask<bool,N,Arch>;
929 using mask = mask_type;
930 using predicate_type = predicate<N,Arch>;
931 static constexpr std::size_t lanes=N;
933 hint_inline constexpr simd() noexcept : value_(ops::broadcast(false)) {}
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>()));
944 }
945 // Caller promises EVERY byte is 0 or 1, never an all-ones mask byte.
947 [[nodiscard]] static hint_inline hint_const constexpr simd unsafe_from_native(native_type value) noexcept { return simd(raw{},value); }
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;
954 if consteval { return unsafe_from_native(__builtin_bit_cast(native_type,bytes)); }
955 native_type value;std::memcpy(&value,bytes.data(),sizeof(value));return unsafe_from_native(value);
956 }
957
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;
963 }
964 // The caller supplies 0 <= count <= N. Zero touches no pointer, even null.
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());
970 }
971
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];
975 }
976
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_)); }
987 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator!=(simd a,simd b) noexcept {
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()));
995#endif
996 else
997#endif
998 return vector_mask_type::from_native(x);
999 }
1000
1001 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator==(simd a,simd b) noexcept { return ~(a!=b); }
1003 hint_inline constexpr simd & operator&=(simd b) noexcept { return *this=*this&b; }
1005 hint_inline constexpr simd & operator|=(simd b) noexcept { return *this=*this|b; }
1007 hint_inline constexpr simd & operator^=(simd b) noexcept { return *this=*this^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>)
1016 [[nodiscard]] friend hint_inline hint_const constexpr simd select(M m,simd a,simd b) noexcept {
1017 if consteval {
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_)));
1020 }
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_));
1027#endif
1028 } else
1029#endif
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_)));
1031 }
1032 private:
1033 struct raw {};
1034 hint_inline constexpr simd(raw,native_type value) noexcept : value_(value) {}
1035 native_type value_;
1036 };
1037
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);
1049 }
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); }
1053 }
1055 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && requires { typename simd<bool,N,Arch>::native_type; }
1056 [[nodiscard]] hint_inline constexpr hint_const simd<mask8,N,Arch> to_vector_mask(simd<bool,N,Arch> value) noexcept {
1057 if constexpr(Arch.has(polyfill) && (requires { typename simd<bool,N,Arch>::chunk_type; } || requires { typename simd<mask8,N,Arch>::chunk_type; }))
1058 return simd<mask8,N,Arch>::from_bitset(value.to_bitset());
1059 else return simd<mask8,N,Arch>::from_native(value.to_native());
1060 }
1061
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; }
1063 [[nodiscard]] hint_inline constexpr hint_const simd<bool,N,Arch> to_bool(simd<T,N,Arch> value) noexcept {
1064 if constexpr(Arch.has(polyfill) && (requires { typename simd<T,N,Arch>::chunk_type; } || requires { typename simd<bool,N,Arch>::chunk_type; }))
1065 return simd<bool,N,Arch>::from_bitset(value.to_bitset());
1066 else {
1067 auto byte_mask=mask_cast<mask8>(value);
1068 return simd<bool,N,Arch>::unsafe_from_native(simd<bool,N,Arch>::ops::bit_and(byte_mask.to_native(),::NATIVE_BACKEND_NAMESPACE::bool_ones<N>()));
1069 }
1070 }
1071
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; }
1073 [[nodiscard]] hint_inline constexpr hint_const simd<bool,N,Arch> to_bool(predicate<N,Arch> value) noexcept { return to_bool(to_vector_mask<mask8>(value)); }
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; }
1076 [[nodiscard]] hint_inline constexpr hint_const predicate<N,Arch> to_predicate(simd<bool,N,Arch> value) noexcept {
1077 auto result=value!=simd<bool,N,Arch>(false);
1078 if constexpr(decltype(result)::compact) return result;
1079 else return to_predicate(result);
1080 }
1081}
1082
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);
1086 }
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));
1090 }
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)));
1097 }
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)));
1102 }
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)));
1107 }
1108
1109#if NATIVE_HAS_AVX2
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));
1120 }
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);
1131 }
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);
1142 }
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) {
1146 // Independent low-byte products inside each word, with cross-byte bits removed.
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);
1157#else
1158 // Low 64 bits: low32*low32 plus both cross products shifted by 32.
1159 auto cross =
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));
1162#endif
1163 }
1164 }
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)
1169 return a;
1170 else {
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);
1180 }
1181 }
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)
1186 return a;
1187 else {
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>)
1191 return low;
1192 else {
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))))));
1196 }
1197 } else if constexpr (sizeof(T) == 2) {
1198 if constexpr (std::is_signed_v<T>)
1199 return _mm_srai_epi16(a, S);
1200 else
1201 return _mm_srli_epi16(a, S);
1202 }
1203 if constexpr (sizeof(T) == 4) {
1204 if constexpr (std::is_signed_v<T>)
1205 return _mm_srai_epi32(a, S);
1206 else
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);
1211 else {
1212#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
1213 return _mm_srai_epi64(a, S);
1214#else
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)))));
1219#endif
1220 }
1221 }
1222 }
1223 }
1224#endif
1225
1226#if NATIVE_HAS_AVX2
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));
1237 }
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);
1248 }
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);
1259 }
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) {
1263 // Independent low-byte products inside each word, with cross-byte bits removed.
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);
1274#else
1275 // Low 64 bits: low32*low32 plus both cross products shifted by 32.
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));
1279#endif
1280 }
1281 }
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)
1286 return a;
1287 else {
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);
1297 }
1298 }
1299 [[nodiscard]] hint_inline hint_const __m256i integer_shift_left_variable(__m256i a,__m256i counts) noexcept {
1300 return _mm256_sllv_epi32(a,counts);
1301 }
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)
1306 return a;
1307 else {
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>)
1311 return low;
1312 else {
1313 auto negative = _mm256_cmpgt_epi8(_mm256_setzero_si256(), a);
1314 return _mm256_or_si256(
1315 low,
1316 _mm256_and_si256(negative, _mm256_set1_epi8(std::bit_cast<int8_t>(uint8_t(255u ^ (255u >> S))))));
1317 }
1318 } else if constexpr (sizeof(T) == 2) {
1319 if constexpr (std::is_signed_v<T>)
1320 return _mm256_srai_epi16(a, S);
1321 else
1322 return _mm256_srli_epi16(a, S);
1323 }
1324 if constexpr (sizeof(T) == 4) {
1325 if constexpr (std::is_signed_v<T>)
1326 return _mm256_srai_epi32(a, S);
1327 else
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);
1332 else {
1333#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512VL
1334 return _mm256_srai_epi64(a, S);
1335#else
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)))));
1340#endif
1341 }
1342 }
1343 }
1344 }
1345#endif
1346
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));
1355#endif
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));
1360 }
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);
1368#endif
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);
1373 }
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);
1381#endif
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);
1386 }
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) {
1391 // Independent low-byte products inside each word, with cross-byte bits removed.
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);
1397#endif
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);
1402 }
1403 }
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)
1408 return a;
1409 else {
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);
1416#endif
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);
1421 }
1422 }
1423 [[nodiscard]] hint_inline hint_const __m512i integer_shift_left_variable(__m512i a,__m512i counts) noexcept {
1424 return _mm512_sllv_epi32(a,counts);
1425 }
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)
1430 return a;
1431 else {
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>)
1436 return low;
1437 else {
1438 auto negative = _mm512_movm_epi8(_mm512_cmp_epi8_mask(a, _mm512_setzero_si512(), _MM_CMPINT_LT));
1439 return _mm512_or_si512(
1440 low,
1441 _mm512_and_si512(negative, _mm512_set1_epi8(std::bit_cast<int8_t>(uint8_t(255u ^ (255u >> S))))));
1442 }
1443 } else if constexpr (sizeof(T) == 2) {
1444 if constexpr (std::is_signed_v<T>)
1445 return _mm512_srai_epi16(a, S);
1446 else
1447 return _mm512_srli_epi16(a, S);
1448 }
1449#endif
1450 if constexpr (sizeof(T) == 4) {
1451 if constexpr (std::is_signed_v<T>)
1452 return _mm512_srai_epi32(a, S);
1453 else
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);
1458 else {
1459 return _mm512_srai_epi64(a, S);
1460 }
1461 }
1462 }
1463 }
1464#endif
1465#if NATIVE_HAS_AVX2
1466 [[nodiscard]] hint_inline hint_const __m128i integer_and(__m128i a, __m128i b) noexcept {
1467 return _mm_and_si128(a, b);
1468 }
1469 [[nodiscard]] hint_inline hint_const __m128i integer_or(__m128i a, __m128i b) noexcept {
1470 return _mm_or_si128(a, b);
1471 }
1472 [[nodiscard]] hint_inline hint_const __m128i integer_xor(__m128i a, __m128i b) noexcept {
1473 return _mm_xor_si128(a, b);
1474 }
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);
1482 else
1483 return _mm_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1484 } else
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);
1490 else
1491 return _mm_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1492 } else
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);
1498 else
1499 return _mm_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1500 } else
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);
1506 else
1507 return _mm_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1508 } else
1509 return _mm_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1510 }
1511 } else {
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);
1517 else {
1518 auto bias = _mm_set1_epi8(int8_t(-128));
1519 return _mm_cmpgt_epi8(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1520 }
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);
1526 else {
1527 auto bias = _mm_set1_epi16(int16_t(-32768));
1528 return _mm_cmpgt_epi16(_mm_xor_si128(a, bias), _mm_xor_si128(b, bias));
1529 }
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);
1535 else {
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));
1538 }
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);
1544 else {
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));
1547 }
1548 }
1549 }
1550 }
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);
1562 } else
1563 return _mm_or_si128(_mm_and_si128(m.to_native(), a), _mm_andnot_si128(m.to_native(), b));
1564 }
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);
1577 } else
1578 return integer_select<T>(m, integer_add<T>(a, b), prior);
1579 }
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);
1592 } else
1593 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1594 }
1595#endif
1596
1597#if NATIVE_HAS_AVX2
1598 [[nodiscard]] hint_inline hint_const __m256i integer_and(__m256i a, __m256i b) noexcept {
1599 return _mm256_and_si256(a, b);
1600 }
1601 [[nodiscard]] hint_inline hint_const __m256i integer_or(__m256i a, __m256i b) noexcept {
1602 return _mm256_or_si256(a, b);
1603 }
1604 [[nodiscard]] hint_inline hint_const __m256i integer_xor(__m256i a, __m256i b) noexcept {
1605 return _mm256_xor_si256(a, b);
1606 }
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);
1614 else
1615 return _mm256_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1616 } else
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);
1622 else
1623 return _mm256_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1624 } else
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);
1630 else
1631 return _mm256_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1632 } else
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);
1638 else
1639 return _mm256_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1640 } else
1641 return _mm256_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1642 }
1643 } else {
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);
1649 else {
1650 auto bias = _mm256_set1_epi8(int8_t(-128));
1651 return _mm256_cmpgt_epi8(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1652 }
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);
1658 else {
1659 auto bias = _mm256_set1_epi16(int16_t(-32768));
1660 return _mm256_cmpgt_epi16(_mm256_xor_si256(a, bias), _mm256_xor_si256(b, bias));
1661 }
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);
1667 else {
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));
1670 }
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);
1676 else {
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));
1679 }
1680 }
1681 }
1682 }
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);
1694 } else
1695 return _mm256_or_si256(_mm256_and_si256(m.to_native(), a), _mm256_andnot_si256(m.to_native(), b));
1696 }
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);
1709 } else
1710 return integer_select<T>(m, integer_add<T>(a, b), prior);
1711 }
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);
1724 } else
1725 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1726 }
1727#endif
1728
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);
1732 }
1733 [[nodiscard]] hint_inline hint_const __m512i integer_or(__m512i a, __m512i b) noexcept {
1734 return _mm512_or_si512(a, b);
1735 }
1736 [[nodiscard]] hint_inline hint_const __m512i integer_xor(__m512i a, __m512i b) noexcept {
1737 return _mm512_xor_si512(a, b);
1738 }
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);
1747 else
1748 return _mm512_cmp_epu8_mask(a, b, _MM_CMPINT_GT);
1749 } else
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);
1755 else
1756 return _mm512_cmp_epu16_mask(a, b, _MM_CMPINT_GT);
1757 } else
1758 return _mm512_cmp_epi16_mask(a, b, _MM_CMPINT_EQ);
1759 }
1760#endif
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);
1765 else
1766 return _mm512_cmp_epu32_mask(a, b, _MM_CMPINT_GT);
1767 } else
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);
1773 else
1774 return _mm512_cmp_epu64_mask(a, b, _MM_CMPINT_GT);
1775 } else
1776 return _mm512_cmp_epi64_mask(a, b, _MM_CMPINT_EQ);
1777 }
1778 } else {
1779 static_assert(sizeof(T) == 0, "512-bit integer comparisons require native predicates");
1780 }
1781 }
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);
1790#endif
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);
1795 } else
1796 return _mm512_or_si512(_mm512_and_si512(m.to_native(), a), _mm512_andnot_si512(m.to_native(), b));
1797 }
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);
1807#endif
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);
1812 } else
1813 return integer_select<T>(m, integer_add<T>(a, b), prior);
1814 }
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);
1824#endif
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);
1829 } else
1830 return integer_select<T>(m, integer_sub<T>(a, b), prior);
1831 }
1832#endif
1833
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);
1837 }
1838 [[nodiscard]] hint_inline hint_const uint8x16_t integer_or(uint8x16_t a, uint8x16_t b) noexcept {
1839 return vorrq_u8(a, b);
1840 }
1841 [[nodiscard]] hint_inline hint_const uint8x16_t integer_xor(uint8x16_t a, uint8x16_t b) noexcept {
1842 return veorq_u8(a, b);
1843 }
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)));
1854 }
1855 }
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)));
1866 }
1867 }
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);
1879 auto cross =
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)));
1882 }
1883 }
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)));
1894 }
1895 }
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)
1900 return a;
1901 else {
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));
1910 }
1911 }
1912 }
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)));
1915 }
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)
1920 return a;
1921 else {
1922 if constexpr (sizeof(T) == 1) {
1923 if constexpr (std::is_unsigned_v<T>)
1924 return vshrq_n_u8(a, S);
1925 else
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));
1930 else
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));
1935 else
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));
1940 else
1941 return vreinterpretq_u8_s64(vshrq_n_s64(vreinterpretq_s64_u8(a), S));
1942 }
1943 }
1944 }
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);
1952 else
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)));
1959 else
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)));
1966 else
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)));
1973 else
1974 return vreinterpretq_u8_u64(vcgtq_s64(vreinterpretq_s64_u8(a), vreinterpretq_s64_u8(b)));
1975 }
1976 }
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);
1980 }
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);
1985 }
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);
1990 }
1991#endif
1992
1993#if NATIVE_HAS_AVX2
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);
2004 } else
2005 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2006 }
2007#endif
2008#if NATIVE_HAS_AVX2
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);
2019 } else
2020 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2021 }
2022#endif
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);
2031#endif
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);
2036 } else
2037 return integer_select<T>(m, integer_mul<T>(a, b), prior);
2038 }
2039#endif
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);
2045 }
2046#endif
2047#if NATIVE_HAS_AVX2
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));
2050 }
2051#endif
2052#if NATIVE_HAS_AVX2
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));
2055 }
2056#endif
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));
2060 }
2061#endif
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); }
2064#endif
2065} // namespace NATIVE_BACKEND_NAMESPACE
2066
2067namespace native {
2068 namespace detail::NATIVE_BACKEND {
2069 template <std::size_t Bytes> struct integer_storage;
2070#if NATIVE_HAS_AVX2
2071 template <> struct integer_storage<16> {
2072 using type = __m128i;
2073 };
2074 template <> struct integer_storage<32> {
2075 using type = __m256i;
2076 };
2077#endif
2078#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2079 template <> struct integer_storage<64> {
2080 using type = __m512i;
2081 };
2082#endif
2083#if NATIVE_HAS_ARM_NEON
2084 template <> struct integer_storage<16> {
2085 using type = uint8x16_t;
2086 };
2087#endif
2088 template <class T, std::size_t N>
2089 inline constexpr bool integer_shape =
2090 simd_integer_element<T> && (N == 1
2091#if NATIVE_HAS_AVX2
2092 || sizeof(T) * N == 16 || sizeof(T) * N == 32
2093#endif
2094#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
2095 || (sizeof(T) * N == 64 && (sizeof(T) >= 4
2096#if NATIVE_HAS_AVX512BW
2097 || sizeof(T) <= 2
2098#endif
2099 ))
2100#endif
2101#if NATIVE_HAS_ARM_NEON
2102 || sizeof(T) * N == 16
2103#endif
2104 );
2105 } // namespace detail
2106
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");
2126
2127 }
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> {
2129 static constexpr isa<> architecture=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>{});
2135 }
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>{});
2140 }
2141
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;
2149 T value{};
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));
2165 }
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));
2169 }
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));
2173 }
2175 [[nodiscard]] friend hint_inline hint_const constexpr simd operator&(simd a, simd b) noexcept {
2176 return from_native(
2177 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) & ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2178 }
2180 [[nodiscard]] friend hint_inline hint_const constexpr simd operator|(simd a, simd b) noexcept {
2181 return from_native(
2182 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) | ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2183 }
2185 [[nodiscard]] friend hint_inline hint_const constexpr simd operator^(simd a, simd b) noexcept {
2186 return from_native(
2187 ::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(::NATIVE_BACKEND_NAMESPACE::integer_word(a.value) ^ ::NATIVE_BACKEND_NAMESPACE::integer_word(b.value)));
2188 }
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)));
2192 }
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);
2200 }
2202 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator>(simd a, simd b) noexcept {
2203 return mask_type(a.value > b.value);
2204 }
2206 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator!=(simd a, simd b) noexcept {
2207 return ~(a == b);
2208 }
2210 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator<(simd a, simd b) noexcept {
2211 return b > a;
2212 }
2214 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator<=(simd a, simd b) noexcept {
2215 return ~(a > b);
2216 }
2218 [[nodiscard]] friend hint_inline hint_const constexpr mask_type operator>=(simd a, simd b) noexcept {
2219 return ~(b > a);
2220 }
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));
2225 }
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;
2230 }
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));
2235 }
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;
2240 }
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));
2245 }
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;
2250 }
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));
2255 }
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;
2260 }
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));
2265 }
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;
2270 }
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));
2275 }
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;
2280 }
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));
2285 }
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;
2290 }
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));
2295 }
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;
2300 }
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));
2305 }
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;
2310 }
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));
2315 }
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;
2320 }
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));
2325 }
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;
2330 }
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));
2335 }
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;
2340 }
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));
2372 }
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)
2381 return *this;
2382 else {
2383 U result = U(W(u) >> K);
2384 if constexpr (std::is_signed_v<T>) {
2385 if (value < T(0))
2386 result = U(result | U(std::numeric_limits<U>::max() ^ (std::numeric_limits<U>::max() >> K)));
2387 }
2388 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(result));
2389 }
2390 }
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>();
2396 }
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>();
2402 }
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);
2406 }
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);
2410 }
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") {
2418 assert(n <= lanes);
2419 std::array<T, lanes> data;
2420 data.fill(fill);
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());
2424 }
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;
2555 };
2556
2557#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON || (NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ)
2558 // Qualified native helper names must exist even before constraints are checked.
2559 // A real integer specialization owns one intrinsic register. The storage trait
2560 // chooses representation only; every operation below has integer semantics.
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>)
2563 struct simd<T, N,Arch> : detail::swizzle_access<T,N,Arch> {
2564 static constexpr isa<> architecture=Arch;
2565 template <class U> using rebind = simd<U,N,Arch>;
2566 template <std::size_t A = 1>
2568 [[nodiscard]] static hint_inline constexpr simd load_memory(T const * p) noexcept {
2569 return ::NATIVE_BACKEND_NAMESPACE::load_simd<Arch,T,N>(p,simd_memory<A>{});
2570 }
2571 template <std::size_t A = 1>
2573 hint_inline constexpr void store_memory(T * p) const noexcept {
2574 ::NATIVE_BACKEND_NAMESPACE::store_simd(p,*this,simd_memory<A>{});
2575 }
2576
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;
2581 using predicate_type = predicate<N,Arch>;
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);
2590 if consteval {
2591 std::array<T,N> data;
2592 data.fill(x);
2593 value=__builtin_bit_cast(native_type,data);
2594 } else {
2595 if constexpr (sizeof(T) * N == 16)
2596 value = ::NATIVE_BACKEND_NAMESPACE::integer_broadcast_16(x);
2597#if NATIVE_HAS_AVX2
2598 else if constexpr (sizeof(T) * N == 32)
2599 value = ::NATIVE_BACKEND_NAMESPACE::integer_broadcast_32(x);
2600#endif
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);
2604#endif
2605 }
2606 }
2607#if NATIVE_HOST_X86
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) {}
2614#else
2616 hint_inline constexpr simd(native_type x) noexcept : value(x) {}
2617#endif
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)); }
2625 }
2626
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)); }
2630 }
2631#if NATIVE_HOST_X86
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")
2637 simd from_native(native_type x) noexcept requires(sizeof(native_type)==16) { return simd(x); }
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")
2646 simd from_native(native_type x) noexcept requires(sizeof(native_type)==32) { return simd(x); }
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")
2655 simd from_native(native_type x) noexcept requires(sizeof(native_type)==64) { return simd(x); }
2657 [[nodiscard]] hint_inline constexpr hint_const hint_target("avx512f")
2658 native_type to_native() const noexcept requires(sizeof(native_type)==64) { return value; }
2659#else
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; }
2666#endif
2668 [[nodiscard]] friend hint_inline constexpr hint_const simd operator+(simd a, simd b) noexcept {
2669 if consteval {
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)));
2674 }
2675 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_add<T>(a.value, b.value));
2676 }
2677
2678 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(simd a, simd b) noexcept {
2679 if consteval {
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)));
2684 }
2685 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_sub<T>(a.value, b.value));
2686 }
2687
2688 [[nodiscard]] friend hint_inline constexpr hint_const simd operator*(simd a, simd b) noexcept {
2689 if consteval {
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)));
2694 }
2695 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_mul<T>(a.value, b.value));
2696 }
2697
2698 [[nodiscard]] friend hint_inline constexpr hint_const simd operator&(simd a, simd b) noexcept {
2699 if consteval {
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)));
2704 }
2705 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_and(a.value, b.value));
2706 }
2707
2708 [[nodiscard]] friend hint_inline constexpr hint_const simd operator|(simd a, simd b) noexcept {
2709 if consteval {
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)));
2714 }
2715 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_or(a.value, b.value));
2716 }
2717
2718 [[nodiscard]] friend hint_inline constexpr hint_const simd operator^(simd a, simd b) noexcept {
2719 if consteval {
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)));
2724 }
2725 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_xor(a.value, b.value));
2726 }
2727
2728 [[nodiscard]] friend hint_inline constexpr hint_const simd operator~(simd a) noexcept {
2729 return a ^ simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(~std::make_unsigned_t<T>(0)));
2730 }
2731
2732 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(simd a) noexcept { return simd(T(0)) - a; }
2734 [[nodiscard]] friend hint_inline constexpr hint_const simd operator+(simd a) noexcept { return a; }
2736 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator==(simd a, simd b) noexcept {
2737 if consteval {
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);
2743 }
2744 return mask_type::unsafe_from_native(::NATIVE_BACKEND_NAMESPACE::integer_compare<T, false>(a.value, b.value));
2745 }
2746
2747 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator>(simd a, simd b) noexcept {
2748 if consteval {
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);
2754 }
2755 return mask_type::unsafe_from_native(::NATIVE_BACKEND_NAMESPACE::integer_compare<T, true>(a.value, b.value));
2756 }
2757
2758 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator!=(simd a, simd b) noexcept {
2759 return ~(a == b);
2760 }
2761
2762 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator<(simd a, simd b) noexcept {
2763 return b > a;
2764 }
2765
2766 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator<=(simd a, simd b) noexcept {
2767 return ~(a > b);
2768 }
2769
2770 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator>=(simd a, simd b) noexcept {
2771 return ~(b > a);
2772 }
2773
2774 template <simd_integer_element U>
2775 [[nodiscard]] friend hint_inline constexpr hint_const simd operator+(simd a, U b) noexcept {
2776 return a + simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2777 }
2778
2779 template <simd_integer_element U>
2780 [[nodiscard]] friend hint_inline constexpr hint_const simd operator+(U a, simd b) noexcept {
2781 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) + b;
2782 }
2783
2784 template <simd_integer_element U>
2785 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(simd a, U b) noexcept {
2786 return a - simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2787 }
2788
2789 template <simd_integer_element U>
2790 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(U a, simd b) noexcept {
2791 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) - b;
2792 }
2793
2794 template <simd_integer_element U>
2795 [[nodiscard]] friend hint_inline constexpr hint_const simd operator*(simd a, U b) noexcept {
2796 return a * simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2797 }
2798
2799 template <simd_integer_element U>
2800 [[nodiscard]] friend hint_inline constexpr hint_const simd operator*(U a, simd b) noexcept {
2801 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) * b;
2802 }
2803
2804 template <simd_integer_element U>
2805 [[nodiscard]] friend hint_inline constexpr hint_const simd operator&(simd a, U b) noexcept {
2806 return a & simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2807 }
2808
2809 template <simd_integer_element U>
2810 [[nodiscard]] friend hint_inline constexpr hint_const simd operator&(U a, simd b) noexcept {
2811 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) & b;
2812 }
2813
2814 template <simd_integer_element U>
2815 [[nodiscard]] friend hint_inline constexpr hint_const simd operator|(simd a, U b) noexcept {
2816 return a | simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2817 }
2818
2819 template <simd_integer_element U>
2820 [[nodiscard]] friend hint_inline constexpr hint_const simd operator|(U a, simd b) noexcept {
2821 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) | b;
2822 }
2823
2824 template <simd_integer_element U>
2825 [[nodiscard]] friend hint_inline constexpr hint_const simd operator^(simd a, U b) noexcept {
2826 return a ^ simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(b));
2827 }
2828
2829 template <simd_integer_element U>
2830 [[nodiscard]] friend hint_inline constexpr hint_const simd operator^(U a, simd b) noexcept {
2831 return simd(::NATIVE_BACKEND_NAMESPACE::integer_wrap<T>(a)) ^ b;
2832 }
2833
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));
2837 }
2838
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;
2842 }
2843
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));
2847 }
2848
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;
2852 }
2853
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));
2857 }
2858
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;
2862 }
2863
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));
2867 }
2868
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;
2872 }
2873
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));
2877 }
2878
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;
2882 }
2883
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));
2887 }
2888
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;
2892 }
2893
2894 hint_inline constexpr simd &operator+=(simd b) noexcept { return *this = *this + b; }
2896 template <simd_integer_element U> hint_inline constexpr simd &operator+=(U b) noexcept { return *this = *this + b; }
2898 hint_inline constexpr simd &operator-=(simd b) noexcept { return *this = *this - b; }
2900 template <simd_integer_element U> hint_inline constexpr simd &operator-=(U b) noexcept { return *this = *this - b; }
2902 hint_inline constexpr simd &operator*=(simd b) noexcept { return *this = *this * b; }
2904 template <simd_integer_element U> hint_inline constexpr simd &operator*=(U b) noexcept { return *this = *this * b; }
2906 hint_inline constexpr simd &operator&=(simd b) noexcept { return *this = *this & b; }
2908 template <simd_integer_element U> hint_inline constexpr simd &operator&=(U b) noexcept { return *this = *this & b; }
2910 hint_inline constexpr simd &operator|=(simd b) noexcept { return *this = *this | b; }
2912 template <simd_integer_element U> hint_inline constexpr simd &operator|=(U b) noexcept { return *this = *this | b; }
2914 hint_inline constexpr simd &operator^=(simd 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 {
2921 if consteval {
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));
2925 }
2926 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_left<T, K>(value));
2927 }
2928
2929 template <unsigned K>
2930 requires(K < sizeof(T) * 8)
2931 [[nodiscard]] hint_inline constexpr hint_const simd right() const noexcept {
2932 if consteval {
2933 using E=T;
2934 using V=E __attribute__((ext_vector_type(N)));
2935 return from_native(__builtin_bit_cast(native_type,__builtin_bit_cast(V,value) >> K));
2936 }
2937 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_right<T, K>(value));
2938 }
2939
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 {
2943 return a.template left<K>();
2944 }
2945
2946 [[nodiscard]] friend hint_inline constexpr hint_const simd operator<<(simd a,simd counts) noexcept
2947 requires std::same_as<T,std::uint32_t> && requires(native_type x) {
2948 ::NATIVE_BACKEND_NAMESPACE::integer_shift_left_variable(x,x);
2949 }
2950 {
2951 if consteval {
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);
2956 }
2957 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_shift_left_variable(a.value,counts.value));
2958 }
2959
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 {
2963 return a.template right<K>();
2964 }
2965
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);
2968 }
2969
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);
2972 }
2973
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") {
2980 assert(n <= lanes);
2981 if consteval {
2982 std::array<T,N> data; data.fill(fill);
2983 for(std::size_t i=0;i<n;++i) data[i]=p[i];
2984 return simd(data);
2985 }
2986 if (n == lanes) return load(p);
2987 if (!n) return simd(fill);
2988 // Native 32/64-bit lane tails must not touch masked-off addresses. This
2989 // also accepts byte-unaligned sources; scalar tail loads use memcpy.
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));
2997 }
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));
3003 } else {
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));
3007 }
3008#endif
3009 }
3010#endif
3011#if NATIVE_HAS_AVX2
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);
3018 } else {
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);
3021 }
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);
3026 } else {
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);
3029 }
3030 }
3031 return from_native(::NATIVE_BACKEND_NAMESPACE::integer_bit_select(active, loaded, simd(fill).value));
3032 }
3033#endif
3034#if NATIVE_HAS_ARM_NEON
3035 if constexpr (sizeof(T) == 4) {
3036 auto result = vreinterpretq_u32_u8(simd(fill).value);
3037 std::uint32_t word;
3038 auto bytes = reinterpret_cast<unsigned char const *>(p);
3039 switch (n) {
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);
3043 }
3044 return from_native(vreinterpretq_u8_u32(result));
3045 } else if constexpr (sizeof(T) == 8) {
3046 std::uint64_t word;
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)));
3049 }
3050#endif
3051 std::array<T, lanes> data;
3052 data.fill(fill);
3053 if (n)
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());
3056 }
3057
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") {
3059 if consteval {
3060 assert(n<=N);
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];
3063 return;
3064 } ::NATIVE_BACKEND_NAMESPACE::store_simd_partial(p, *this, n); }
3065
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>>)
3072 friend void operator+(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3080 friend void operator-(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3088 friend void operator*(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3096 friend void operator/(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3104 friend void operator%(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3112 friend void operator&(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3120 friend void operator|(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3128 friend void operator^(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3136 friend void operator==(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3144 friend void operator!=(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3152 friend void operator<(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3160 friend void operator>(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3168 friend void operator<=(simd, ::native::simd<U, M,Arch>) = delete;
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>>)
3176 friend void operator>=(simd, ::native::simd<U, M,Arch>) = delete;
3178 friend simd operator/(simd, simd) = delete;
3180 friend simd operator%(simd, simd) = delete;
3182 template <simd_integer_element U> friend simd operator/(simd, U) = delete;
3184 template <simd_integer_element U> friend simd operator/(U, simd) = delete;
3186 template <simd_integer_element U> friend simd operator%(simd, U) = delete;
3188 template <simd_integer_element U> friend simd operator%(U, simd) = delete;
3190 template <simd_integer_element U> friend simd operator<<(simd, U) = delete;
3192 template <simd_integer_element U> friend simd operator>>(simd, U) = delete;
3193 };
3194
3195#endif
3196
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>
3200 [[nodiscard]] hint_inline hint_pure constexpr simd<T, N,Arch> load_simd(T const *p, simd_memory<A, Access>) noexcept {
3201 using V = simd<T, N,Arch>;
3202 if consteval {
3203 if constexpr(N==1) return V(*p);
3204 else {
3205 std::array<T,N> data;
3206 for(std::size_t i=0;i<N;++i) data[i]=p[i];
3207 return V(data);
3208 }
3209 }
3210 typename V::native_type value;
3211 // Erase the element type at the byte-copy boundary: p need not satisfy
3212 // alignof(T). Only an explicit alignment promise may select an aligned
3213 // move. Streaming currently takes this ordinary fallback.
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);
3220#endif
3221 std::memcpy(&value, static_cast<void const *>(p), sizeof(value));
3222 return V::from_native(value);
3223 }
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 {
3227 if consteval {
3228 if constexpr(N==1) *p=v.value;
3229 else {
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];
3232 }
3233 return;
3234 }
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);
3241#endif
3242 std::memcpy(static_cast<void *>(p), &v.value, sizeof(v.value));
3243 }
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") {
3248 assert(count <= N);
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());
3253 }
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") {
3258 assert(count <= N);
3259 std::array<T, N> data;
3260 store_simd(data.data(), v);
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)); }
3263 }
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());
3268 }
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());
3273 }
3274
3275 }
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>>;
3280 }
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>)
3285 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> select(M m, simd<T, N,Arch> a, simd<T, N,Arch> b) noexcept {
3286 if consteval {
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];
3291 return simd<T,N,Arch>(first);
3292 }
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)
3296 else
3297 return simd<T, N,Arch>::from_native(::NATIVE_BACKEND_NAMESPACE::integer_select<T>(m, a.value, b.value));
3298#endif
3299 }
3300
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>
3304 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> bit_select(simd<T, N,Arch> bits, simd<T, N,Arch> a,
3305 simd<T, N,Arch> b) noexcept {
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)
3309 else return simd<T,N,Arch>::from_native(::NATIVE_BACKEND_NAMESPACE::integer_bit_select(bits.value,a.value,b.value));
3310#endif
3311 }
3312 // Compatibility: numeric masks select individual bits, never lane truth values.
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>
3317 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> select(simd<T, N,Arch> bits, simd<T, N,Arch> a,
3318 simd<T, N,Arch> b) noexcept {
3319 return bit_select(bits, a, b);
3320 }
3321
3323 template <simd_mask_element M, std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch)
3324 [[nodiscard]] hint_inline constexpr hint_const auto mask_bits(simd<M, N,Arch> m) noexcept {
3325 using U = typename M::storage_type;
3326 if constexpr(Arch.has(polyfill) && (requires { typename simd<M,N,Arch>::chunk_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{};
3330 return simd<U,N,Arch>::load(words.data());
3331 } else return simd<U, N,Arch>::from_native(m.to_native());
3332 }
3333
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))
3337 [[nodiscard]] hint_inline constexpr hint_const simd<std::make_unsigned_t<T>, N,Arch> mask_bits(simd<M, N,Arch> m) noexcept {
3338 return mask_bits(m);
3339 }
3340
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); }
3344 [[nodiscard]] hint_inline constexpr hint_const simd<std::make_unsigned_t<T>, N,Arch> mask_bits(predicate<N,Arch> m) noexcept {
3345 return mask_bits(to_vector_mask<::NATIVE_BACKEND_NAMESPACE::mask_lane_for<T>>(m));
3346 }
3347
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>)
3351 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_add(M m, simd<T, N,Arch> prior, simd<T, N,Arch> a,
3352 simd<T, N,Arch> b) noexcept {
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)
3357 else
3358 return simd<T, N,Arch>::from_native(::NATIVE_BACKEND_NAMESPACE::integer_masked_add<T>(m, prior.value, a.value, b.value));
3359#endif
3360 }
3361
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>)
3365 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_add_zero(M m, simd<T, N,Arch> a, simd<T, N,Arch> b) noexcept {
3366 return masked_add(m, simd<T, N,Arch>(T(0)), a, b);
3367 }
3368
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>)
3372 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_sub(M m, simd<T, N,Arch> prior, simd<T, N,Arch> a,
3373 simd<T, N,Arch> b) noexcept {
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)
3378 else
3379 return simd<T, N,Arch>::from_native(::NATIVE_BACKEND_NAMESPACE::integer_masked_sub<T>(m, prior.value, a.value, b.value));
3380#endif
3381 }
3382
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>)
3386 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_sub_zero(M m, simd<T, N,Arch> a, simd<T, N,Arch> b) noexcept {
3387 return masked_sub(m, simd<T, N,Arch>(T(0)), a, b);
3388 }
3389
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>)
3393 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_mul(M m, simd<T, N,Arch> prior, simd<T, N,Arch> a,
3394 simd<T, N,Arch> b) noexcept {
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)
3399 else
3400 return simd<T, N,Arch>::from_native(::NATIVE_BACKEND_NAMESPACE::integer_masked_mul<T>(m, prior.value, a.value, b.value));
3401#endif
3402 }
3403
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>)
3407 [[nodiscard]] hint_inline constexpr hint_const simd<T, N,Arch> masked_mul_zero(M m, simd<T, N,Arch> a, simd<T, N,Arch> b) noexcept {
3408 return masked_mul(m, simd<T, N,Arch>(T(0)), a, b);
3409 }
3410
3411 // Reject invalid immediate widths before implicit native conversion can select
3412 // a builtin scalar shift. The immediate tag retains its public size conversion.
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;
3421}
3422
3423namespace NATIVE_BACKEND_NAMESPACE::native {
3424 // Compatibility names point toward the public native class template.
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>;
3428#endif
3429#if NATIVE_HAS_AVX2
3430 using uint32x8=::native::simd<::native::uint32_t,8,NATIVE_DEFAULT_ARCH>;
3431#endif
3432#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
3433 using uint32x16=::native::simd<::native::uint32_t,16,NATIVE_DEFAULT_ARCH>;
3434#endif
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>(); }
3443 using ::native::mask_bits;
3444 using ::native::bit_select;
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;
3454#else
3455 using unsigned_type=uint32x1;
3456#endif
3457 static constexpr std::size_t lanes=unsigned_type::lanes;
3458 };
3459}
3460
3461namespace native {
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;
3465 }
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>;
3469 }
3470 namespace detail::NATIVE_BACKEND {
3471
3472 using ::native::detail::register_memory;
3473 }
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> {
3475 static constexpr isa<> architecture=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>;
3481 float value{};
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);
3494 }
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);
3499 }
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);
3504 }
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);
3509 }
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);
3514 }
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);
3519 }
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);
3524 }
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);
3529 }
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;
3535 }
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));
3540 }
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));
3545 }
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);
3553 }
3554 // Internal precondition: integral n in [-126,127].
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));
3559 }
3560
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);
3565 }
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);
3570 }
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); }
3621 };
3622#if NATIVE_HAS_AVX2
3623}
3624
3625namespace native {
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;
3630 template <class T> using rebind = simd<T,4,Arch>;
3631 using vector_mask_type=simd<mask32,4,Arch>;
3632 using mask_type=std::conditional_t<bool(NATIVE_HAS_AVX512VL),predicate<4,Arch>,simd<mask32,4,Arch>>;
3633 using mask = mask_type;
3634 using predicate_type = predicate<4,Arch>;
3635 __m128 value;
3637 hint_inline simd() = default;
3639 hint_inline constexpr simd(simd const &) = default;
3641 hint_reinitializes hint_inline constexpr simd & operator=(simd const &) = default;
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); }
3646 }
3647
3648 hint_inline hint_target("sse") constexpr simd(__m128 x) : value(x) {}
3650 [[nodiscard]] static hint_inline constexpr hint_pure simd load(hint_noescape float const * p) { return load_memory<1>(p); }
3652 hint_inline constexpr void store(hint_noescape float * p) const { store_memory<1>(p); }
3654 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator+(simd a, simd b) {
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));
3657 }
3658
3659 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator-(simd a, simd b) {
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));
3662 }
3663
3664 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator*(simd a, simd b) {
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));
3667 }
3668
3669 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator/(simd a, simd b) {
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));
3672 }
3673
3674 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(simd a) {
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)));
3677 }
3678
3679 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator<(simd a, simd b) {
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));
3683 else
3684 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_LT_OQ)));
3685 }
3686
3687 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator>(simd a, simd b) {
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));
3691 else
3692 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_GT_OQ)));
3693 }
3694
3695 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator==(simd a, simd b) {
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));
3699 else
3700 return mask_type::unsafe_from_native(_mm_castps_si128(_mm_cmp_ps(a.value,b.value,_CMP_EQ_OQ)));
3701 }
3702
3703 template<class M> requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
3704 [[nodiscard]] friend hint_inline constexpr hint_const simd select(M m,simd a,simd b) {
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));
3707 else
3708 return simd(_mm_blendv_ps(b.value,a.value,_mm_castsi128_ps(m.to_native()))); }
3709
3710 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd fma(simd a, simd b, simd c) {
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));
3713 }
3714
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));
3718 }
3719
3720 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_const simd round_even(simd a) {
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));
3723 }
3724
3725 [[nodiscard]] friend hint_inline constexpr hint_const simd normal_pow2(simd n) {
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)));
3728 }
3729
3730 template <std::size_t Alignment = 1>
3732 [[nodiscard]] static hint_inline constexpr hint_pure simd load_memory(float const * p) noexcept {
3733 if consteval {
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));
3737 }
3738 if constexpr(Alignment>=16) return simd(_mm_load_ps(p));
3739 else return simd(_mm_loadu_ps(p));
3740 }
3741 template <std::size_t Alignment = 1>
3743 hint_inline constexpr void store_memory(float * p) const noexcept {
3744 if consteval {
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];
3747 return;
3748 }
3749 if constexpr(Alignment>=16) _mm_store_ps(p,value);
3750 else _mm_storeu_ps(p,value);
3751 }
3752 using value_type = float;
3753 using register_type = simd;
3754 using native_type = __m128;
3755 using bits_type = simd<uint32_t,4,Arch>;
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)); }
3767 [[nodiscard]] static hint_inline constexpr hint_const simd from_bits(std::uint32_t bits) noexcept { return from_bits(bits_type(bits)); }
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); }
3773 [[nodiscard]] static hint_inline constexpr hint_const hint_target("sse") simd unsafe_from_float32(native_type x) noexcept { return simd(x); }
3775 [[nodiscard]] static hint_inline constexpr hint_pure simd loadu(hint_noescape float const * p) { return load_memory<1>(p); }
3777 hint_inline constexpr void storeu(hint_noescape float * p) const { store_memory<1>(p); }
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)...} {}
3792#else
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())) {}
3796#endif
3798 hint_inline constexpr simd & operator+=(simd b) noexcept { return *this=*this+b; }
3800 hint_inline constexpr simd & operator-=(simd b) noexcept { return *this=*this-b; }
3802 hint_inline constexpr simd & operator*=(simd b) noexcept { return *this=*this*b; }
3804 hint_inline constexpr simd & operator/=(simd b) noexcept { return *this=*this/b; }
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); }
3811 };
3812
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;
3817 template <class T> using rebind = simd<T,8,Arch>;
3818 using vector_mask_type=simd<mask32,8,Arch>;
3819 using mask_type=std::conditional_t<bool(NATIVE_HAS_AVX512VL),predicate<8,Arch>,simd<mask32,8,Arch>>;
3820 using mask = mask_type;
3821 using predicate_type = predicate<8,Arch>;
3822 __m256 value;
3824 hint_inline simd() = default;
3826 hint_inline constexpr simd(simd const &) = default;
3828 hint_reinitializes hint_inline constexpr simd & operator=(simd const &) = default;
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); }
3833 }
3834
3835 hint_inline hint_target("avx") constexpr simd(__m256 x) : value(x) {}
3837 [[nodiscard]] static hint_inline constexpr hint_pure simd load(hint_noescape float const * p) { return load_memory<1>(p); }
3839 hint_inline constexpr void store(hint_noescape float * p) const { store_memory<1>(p); }
3841 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator+(simd a, simd b) {
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));
3844 }
3845
3846 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator-(simd a, simd b) {
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));
3849 }
3850
3851 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator*(simd a, simd b) {
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));
3854 }
3855
3856 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd operator/(simd a, simd b) {
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));
3859 }
3860
3861 [[nodiscard]] friend hint_inline constexpr hint_const simd operator-(simd a) {
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)));
3864 }
3865
3866 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator<(simd a, simd b) {
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));
3870 else
3871 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_LT_OQ)));
3872 }
3873
3874 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator>(simd a, simd b) {
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));
3878 else
3879 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_GT_OQ)));
3880 }
3881
3882 [[nodiscard]] friend hint_inline constexpr hint_const mask_type operator==(simd a, simd b) {
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));
3886 else
3887 return mask_type::unsafe_from_native(_mm256_castps_si256(_mm256_cmp_ps(a.value,b.value,_CMP_EQ_OQ)));
3888 }
3889
3890 template<class M> requires (std::same_as<M,mask_type> || std::same_as<M,vector_mask_type>)
3891 [[nodiscard]] friend hint_inline constexpr hint_const simd select(M m,simd a,simd b) {
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));
3894 else
3895 return simd(_mm256_blendv_ps(b.value,a.value,_mm256_castsi256_ps(m.to_native()))); }
3896
3897 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_pure simd fma(simd a, simd b, simd c) {
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));
3900 }
3901
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));
3905 }
3906
3907 hint_artificial [[nodiscard]] friend hint_inline constexpr hint_const simd round_even(simd a) {
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));
3910 }
3911
3912 [[nodiscard]] friend hint_inline constexpr hint_const simd normal_pow2(simd n) {
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)));
3915 }
3916
3917 template <std::size_t Alignment = 1>
3919 [[nodiscard]] static hint_inline constexpr hint_pure simd load_memory(float const * p) noexcept {
3920 if consteval {
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));
3924 }
3925 if constexpr(Alignment>=32) return simd(_mm256_load_ps(p));
3926 else return simd(_mm256_loadu_ps(p));
3927 }
3928 template <std::size_t Alignment = 1>
3930 hint_inline constexpr void store_memory(float * p) const noexcept {
3931 if consteval {
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];
3934 return;
3935 }
3936 if constexpr(Alignment>=32) _mm256_store_ps(p,value);
3937 else _mm256_storeu_ps(p,value);
3938 }
3939 using value_type = float;
3940 using register_type = simd;
3941 using native_type = __m256;
3942 using bits_type = simd<uint32_t,8,Arch>;
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)); }
3954 [[nodiscard]] static hint_inline constexpr hint_const simd from_bits(std::uint32_t bits) noexcept { return from_bits(bits_type(bits)); }
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); }
3960 [[nodiscard]] static hint_inline constexpr hint_const hint_target("avx") simd unsafe_from_float32(native_type x) noexcept { return simd(x); }
3962 [[nodiscard]] static hint_inline constexpr hint_pure simd loadu(hint_noescape float const * p) { return load_memory<1>(p); }
3964 hint_inline constexpr void storeu(hint_noescape float * p) const { store_memory<1>(p); }
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)...} {}
3979#else
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())) {}
3983#endif
3985 hint_inline constexpr simd & operator+=(simd b) noexcept { return *this=*this+b; }
3987 hint_inline constexpr simd & operator-=(simd b) noexcept { return *this=*this-b; }
3989 hint_inline constexpr simd & operator*=(simd b) noexcept { return *this=*this*b; }
3991 hint_inline constexpr simd & operator/=(simd b) noexcept { return *this=*this/b; }
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); }
3998 };
3999
4000}
4001
4002namespace native {
4003#endif
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> {
4006 static constexpr isa<> architecture=Arch;
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>;
4012 __m512 value;
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); }
4023 }
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));
4034 }
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));
4039 }
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));
4044 }
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));
4049 }
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)));
4054 }
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));
4059 }
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));
4064 }
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));
4069 }
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));
4081 }
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));
4086 }
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));
4091 }
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)));
4096 }
4097
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);
4102 }
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);
4107 }
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)...} {}
4148#else
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())) {}
4152#endif
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); }
4167 };
4168#endif
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> {
4171 static constexpr isa<> architecture=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>;
4177 float32x4_t value;
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); }
4188 }
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));
4199 }
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));
4204 }
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));
4209 }
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));
4214 }
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));
4219 }
4220 // Clang's ACLE comparisons become scalar constrained fcmp under
4221 // -frounding-math. Keep the vector instruction and its FPCR/FPSR effects;
4222 // the memory clobber orders environment accesses without a CPU fence.
4224 [[nodiscard]] friend hint_inline constexpr mask_type operator<(simd a, simd b) {
4225 return b>a;
4226 }
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);
4232 uint32x4_t bits;
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)));
4235 }
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);
4241 uint32x4_t bits;
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)));
4244 }
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));
4250 }
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));
4255 }
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));
4260 }
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));
4265 }
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)));
4270 }
4271
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);
4276 }
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);
4281 }
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)...} {}
4322#else
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())) {}
4326#endif
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 {
4341 if consteval {
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);
4344 },a,b);
4345 }
4346 auto x=::native::detail::arm_register_order(a.value);
4347 auto y=::native::detail::arm_register_order(b.value);
4348 uint32x4_t bits;
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)));
4351 }
4352 };
4353#endif
4354 namespace detail::NATIVE_BACKEND {
4355 // Scalar VSCALEFSS and 512-bit VSCALEFPS require AVX512F; packed
4356 // 128/256-bit forms require AVX512VL. Short vectors mask their padding.
4357 // Keep names available for module exports without admitting software APIs.
4358 template<std::size_t N> inline constexpr bool native_scaleb_shape =
4359#if NATIVE_HAS_AVX512F
4360 N==1 || N==16
4361#if NATIVE_HAS_AVX512VL
4362 || N==2 || N==3 || N==4 || N==8
4363#endif
4364 ;
4365#else
4366 false;
4367#endif
4368 }
4369 // Full VSCALEFPS value semantics: floor(exponent), including nonfinite
4370 // operands. This raw operation follows the caller's FP environment.
4371 // Merge preserves inactive bits; floating-point exception flags are unspecified.
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>)
4382 [[nodiscard]] hint_inline constexpr hint_pure simd<float,N,Arch> masked_scaleb(M mask,
4383 simd<float,N,Arch> prior,simd<float,N,Arch> value,simd<float,N,Arch> exponent) noexcept {
4384 if consteval {
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]);
4389 return simd<float,N,Arch>::load_bits(result.data());
4390 }
4391#if NATIVE_HAS_AVX512F
4392 if constexpr(N==2 || N==3) {
4393 using V=simd<float,N,Arch>;
4394 // The compact mask excludes padding, whose prior bits are already zero.
4395 // A representation copy avoids re-normalizing the short-vector storage.
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())));
4399 } else {
4400 auto native_mask=[&] {if constexpr(M::compact) return mask.to_native();else return to_predicate(mask).to_native();};
4401 if constexpr(N==1) return simd<float,N,Arch>(_mm_cvtss_f32(_mm_mask_scalef_ss(
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));
4407#endif
4408 }
4409#endif
4410 }
4411
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>)
4416 [[nodiscard]] hint_inline constexpr hint_pure simd<float,N,Arch> masked_scaleb_zero(M mask,
4417 simd<float,N,Arch> value,simd<float,N,Arch> exponent) noexcept {
4418 if consteval { return masked_scaleb(mask,simd<float,N,Arch>(0.f),value,exponent); }
4419#if NATIVE_HAS_AVX512F
4420 auto native_mask=[&] {if constexpr(M::compact) return mask.to_native();else return to_predicate(mask).to_native();};
4421 if constexpr(N==1) return simd<float,N,Arch>(_mm_cvtss_f32(_mm_maskz_scalef_ss(
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));
4429#endif
4430#endif
4431 }
4432
4435 template <std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::native_scaleb_shape<N>
4436 [[nodiscard]] hint_inline constexpr hint_pure simd<float,N,Arch> scaleb(simd<float,N,Arch> value,
4437 simd<float,N,Arch> exponent) noexcept {
4438 return masked_scaleb_zero(typename simd<float,N,Arch>::mask_type(true),value,exponent);
4439 }
4440
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>>
4442 [[nodiscard]] hint_inline constexpr auto operator+(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a+simd<float,N,Arch>(b); }
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>>
4445 [[nodiscard]] hint_inline constexpr auto operator+(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)+b; }
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>>
4448 [[nodiscard]] hint_inline constexpr auto operator-(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a-simd<float,N,Arch>(b); }
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>>
4451 [[nodiscard]] hint_inline constexpr auto operator-(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)-b; }
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>>
4454 [[nodiscard]] hint_inline constexpr auto operator*(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a*simd<float,N,Arch>(b); }
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>>
4457 [[nodiscard]] hint_inline constexpr auto operator*(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)*b; }
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>>
4460 [[nodiscard]] hint_inline constexpr auto operator/(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a/simd<float,N,Arch>(b); }
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>>
4463 [[nodiscard]] hint_inline constexpr auto operator/(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)/b; }
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>>
4466 [[nodiscard]] hint_inline constexpr auto operator<(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a<simd<float,N,Arch>(b); }
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>>
4469 [[nodiscard]] hint_inline constexpr auto operator<(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)<b; }
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>>
4472 [[nodiscard]] hint_inline constexpr auto operator>(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a>simd<float,N,Arch>(b); }
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>>
4475 [[nodiscard]] hint_inline constexpr auto operator>(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)>b; }
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>>
4478 [[nodiscard]] hint_inline constexpr auto operator<=(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a<=simd<float,N,Arch>(b); }
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>>
4481 [[nodiscard]] hint_inline constexpr auto operator<=(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)<=b; }
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>>
4484 [[nodiscard]] hint_inline constexpr auto operator>=(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a>=simd<float,N,Arch>(b); }
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>>
4487 [[nodiscard]] hint_inline constexpr auto operator>=(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)>=b; }
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>>
4490 [[nodiscard]] hint_inline constexpr auto operator==(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a==simd<float,N,Arch>(b); }
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>>
4493 [[nodiscard]] hint_inline constexpr auto operator==(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)==b; }
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>>
4496 [[nodiscard]] hint_inline constexpr auto operator!=(simd<float,N,Arch> a, U b) noexcept(noexcept(simd<float,N,Arch>(b))) { return a!=simd<float,N,Arch>(b); }
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>>
4499 [[nodiscard]] hint_inline constexpr auto operator!=(U a, simd<float,N,Arch> b) noexcept(noexcept(simd<float,N,Arch>(a))) { return simd<float,N,Arch>(a)!=b; }
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>
4512 [[nodiscard]] hint_inline constexpr hint_const auto abs(simd<float,N,Arch> a) noexcept {
4513 return simd<float,N,Arch>::from_bits(a.bits() & typename simd<float,N,Arch>::bits_type(0x7fffffffu));
4514 }
4515
4516#if NATIVE_HOST_NEON
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>;
4526 if consteval {
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());
4533 } else {
4534 if constexpr (N == 1) return I(vcvts_s32_f32(x.value));
4535 else if constexpr (N == 2 || N == 3) {
4536 // Short float padding is zero, and conversion preserves that invariant.
4537 I result;
4538 result.value = __builtin_bit_cast(typename I::native_type,
4539 fcvtzs(x.to_storage()).to_native());
4540 return result;
4541 }
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)));
4545#endif
4546 }
4547 }
4548
4550 template<::native::isa<> Arch = NATIVE_BASELINE, 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;
4554 }
4555
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>;
4564 if consteval {
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());
4571 } else {
4572 if constexpr (N == 1) return I(vcvts_u32_f32(x.value));
4573 else if constexpr (N == 2 || N == 3) {
4574 I result;
4575 result.value = __builtin_bit_cast(typename I::native_type,
4576 fcvtzu(x.to_storage()).to_native());
4577 return result;
4578 }
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)));
4582#endif
4583 }
4584 }
4585
4587 template<::native::isa<> Arch = NATIVE_BASELINE, 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;
4591 }
4592#endif
4593
4594 // Truncating float-to-integer conversion requires a representable result.
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>
4599 [[nodiscard]] hint_inline constexpr simd<To,N,Arch> convert(simd<float,N,Arch> x) noexcept {
4600 if consteval {
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]);
4603 return simd<To,N,Arch>::load(b.data());
4604 }
4605 if constexpr(N==2 || N==3) return simd<To,N,Arch>::from_storage(convert<To>(x.to_storage()));
4606 else if constexpr (N==1) return simd<To,N,Arch>(static_cast<std::int32_t>(x.value));
4607#if NATIVE_HAS_AVX2
4608 else if constexpr (N==4) return simd<To,N,Arch>::from_native(_mm_cvttps_epi32(x.value));
4609 else if constexpr (N==8) return simd<To,N,Arch>::from_native(_mm256_cvttps_epi32(x.value));
4610#endif
4611#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4612 else if constexpr (N==16) return simd<To,N,Arch>::from_native(_mm512_cvttps_epi32(x.value));
4613#endif
4614#if NATIVE_HAS_ARM_NEON
4615 else if constexpr (N==4) return simd<To,N,Arch>::from_native(vreinterpretq_u8_s32(vcvtq_s32_f32(x.value)));
4616#endif
4617 }
4618
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>
4622 [[nodiscard]] hint_inline constexpr simd<To,N,Arch> convert(simd<std::int32_t,N,Arch> x) noexcept {
4623 if consteval {
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]);
4626 return simd<To,N,Arch>::load(b.data());
4627 }
4628 if constexpr(N==2 || N==3) return simd<To,N,Arch>::from_storage(convert<To>(x.to_storage()));
4629 else if constexpr (N==1) return simd<To,N,Arch>(static_cast<float>(x.value));
4630#if NATIVE_HAS_AVX2
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));
4633#endif
4634#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4635 else if constexpr (N==16) return simd<To,N,Arch>(_mm512_cvtepi32_ps(x.value));
4636#endif
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)));
4639#endif
4640 }
4641
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 {
4644 if consteval {
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));
4648 }
4649 if constexpr(V::lanes==1) return V(*p);
4650#if NATIVE_HAS_AVX2
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)); }
4653#endif
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)); }
4656#endif
4657#if NATIVE_HAS_ARM_NEON
4658 else if constexpr(V::lanes==4)return V(vld1q_f32(p));
4659#endif
4660 }
4661 template <class V,std::size_t Alignment> hint_artificial hint_inline constexpr void simd_store_native(hint_noescape float * p,V value) noexcept {
4662 if consteval {
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];
4665 return;
4666 }
4667 if constexpr(V::lanes==1)*p=value.value;
4668#if NATIVE_HAS_AVX2
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);}
4671#endif
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);}
4674#endif
4675#if NATIVE_HAS_ARM_NEON
4676 else if constexpr(V::lanes==4)vst1q_f32(p,value.value);
4677#endif
4678 }
4679 }
4680 namespace detail::NATIVE_BACKEND {
4681 // Alignment is a caller promise. Access is a hint; streaming currently falls
4682 // back to ordinary moves on every backend and needs no completion fence.
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);
4690 }
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);
4699 }
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());
4708 }
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];
4715 }
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()); }
4720 }
4721
4722}
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>;
4728#endif
4729#if NATIVE_HAS_AVX2
4730 using fp32x8 = ::native::simd<float,8,NATIVE_DEFAULT_ARCH>;
4731#endif
4732#if NATIVE_HAS_AVX512F && NATIVE_HAS_AVX512DQ
4733 using fp32x16 = ::native::simd<float,16,NATIVE_DEFAULT_ARCH>;
4734#endif
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);
4737 // Ordered comparison: second operand wins on equality or unordered, including
4738 // signed zero and NaNs, identically on each architecture.
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; // Increasing lane order, FP32.
4744 return sum;
4745 }
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());
4750 }
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;
4760 // Prior Mac wide-kernel tuning; remeasure when changing the kernel.
4761 static constexpr std::size_t preferred_registers = 15;
4762#else
4763 using float_type = fp32x1;
4764 static constexpr std::size_t preferred_registers = 1;
4765#endif
4766 // K is derived from the selected register, never a second ISA decision.
4767 static constexpr std::size_t lanes = float_type::lanes;
4768 };
4769}
4770
4771#if NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON
4772#if defined(__x86_64__) || defined(_M_X64)
4773#elif defined(__aarch64__) || defined(_M_ARM64)
4774#endif
4775
4776namespace native {
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();
4784 else return value;
4785 }
4786 }
4787
4788 // Logical short vectors use one physical four-lane register. Floating padding
4789 // stays zero; division supplies one in the unused denominator lanes.
4795 template<::NATIVE_BACKEND_NAMESPACE::short_element T,std::size_t N,::native::isa<> Arch>
4796 requires NATIVE_ARCH_REQUIRES(Arch) &&(N==2 || N==3) && requires { typename simd<T,4,Arch>::native_type; }
4797 // Make the register alignment explicit: MSVC's packed standard-library
4798 // aggregates can otherwise cap an ext_vector_type member's implicit alignment.
4799 struct alignas(typename simd<T,4,Arch>::native_type) simd<T,N,Arch> : detail::swizzle_access<T,N,Arch> {
4800 using value_type=T;
4801 static constexpr isa<> architecture=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;
4817 native_type value;
4818
4820 hint_inline constexpr simd() = default;
4822 hint_inline constexpr simd(simd const &) = default;
4824 hint_inline constexpr simd & operator=(simd const &) = 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} {}
4828
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)) {}
4833
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))...} {}
4837
4838 hint_inline constexpr simd(std::array<T,N> const & values) noexcept : simd(load(values.data())) {}
4839
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; }
4845 [[nodiscard]] static hint_inline constexpr simd from_native(native_type x) noexcept {
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);
4848 }
4849
4850 [[nodiscard]] static hint_inline constexpr simd unsafe_from_native(native_type x) noexcept { 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));
4855 }
4856
4857 [[nodiscard]] static hint_inline constexpr simd from_storage(storage_type x) noexcept {
4858 return simd(__builtin_bit_cast(native_type,x.to_native()));
4859 }
4860 // The caller supplies exactly N logical lanes; alignment never grants a
4861 // readable fourth lane. Three-lane x86 transfers use native masked memory.
4863 template<std::size_t Alignment=1>
4864 [[nodiscard]] static hint_inline constexpr simd load_memory(T const * p) noexcept {
4865 if consteval {
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);
4869 }
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)));
4876 } else {
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)));
4880 }
4881 }
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));
4888 } else {
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));
4893 }
4894 }
4895#endif
4896 }
4897
4898 template<std::size_t Alignment=1>
4899 hint_inline constexpr void store_memory(T * p) const noexcept {
4900 if consteval {
4901 for (std::size_t i=0;i<N;++i) {
4902 if constexpr(simd_mask_element<T>) p[i]=T::from_bits(value[i]);
4903 else p[i]=value[i];
4904 }
4905 return;
4906 }
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));
4913 } else {
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));
4917 }
4918 }
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);
4924 } else {
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);
4928 }
4929 }
4930#endif
4931 }
4932
4933 [[nodiscard]] static hint_inline constexpr simd load(T const * p) noexcept { return load_memory(p); }
4935 [[nodiscard]] static hint_inline constexpr simd loadu(T const * p) noexcept { return load_memory(p); }
4937 hint_inline constexpr void store(T * p) const noexcept { store_memory(p); }
4939 hint_inline constexpr void storeu(T * p) const noexcept { store_memory(p); }
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());
4946 }
4947
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)); }
4952 }
4953
4954 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept requires std::same_as<T,float> {
4955 return bits_type::from_storage(to_storage().bits());
4956 }
4957
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()));
4962 }
4963
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); }
4968 [[nodiscard]] static hint_inline constexpr simd unsafe_from_float32(native_type 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); }
4978 [[nodiscard]] static hint_inline constexpr simd from_bitset(std::uint64_t bits) noexcept requires simd_mask_element<T> { return from_storage(storage_type::from_bitset(bits&lane_mask)); }
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); }
4987
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()); }
4999 [[nodiscard]] friend hint_inline constexpr simd operator/(simd a,simd b) noexcept requires std::same_as<T,float> {
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);
5003 }
5004
5005 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a) noexcept requires(!simd_mask_element<T>) { return from_storage(-a.to_storage()); }
5007 [[nodiscard]] friend hint_inline constexpr simd operator&(simd a,simd b) noexcept requires(!std::same_as<T,float>) { return from_storage(a.to_storage()&b.to_storage()); }
5009 [[nodiscard]] friend hint_inline constexpr simd operator|(simd a,simd b) noexcept requires(!std::same_as<T,float>) { return from_storage(a.to_storage()|b.to_storage()); }
5011 [[nodiscard]] friend hint_inline constexpr simd operator^(simd a,simd b) noexcept requires(!std::same_as<T,float>) { return from_storage(a.to_storage()^b.to_storage()); }
5013 [[nodiscard]] friend hint_inline constexpr simd operator~(simd a) noexcept requires(!std::same_as<T,float>) { return from_storage(~a.to_storage()); }
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>)
5030 [[nodiscard]] friend hint_inline constexpr simd select(M mask,simd a,simd b) noexcept {
5031 using FM=typename storage_type::mask_type;
5032 return clean(select(FM::from_bitset(mask.to_bitset()),a.to_storage(),b.to_storage()));
5033 }
5034
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())); }
5039 [[nodiscard]] friend hint_inline constexpr simd round_even(simd a) noexcept requires std::same_as<T,float> { return clean(round_even(a.to_storage())); }
5041 [[nodiscard]] friend hint_inline constexpr simd normal_pow2(simd a) noexcept requires std::same_as<T,float> { return from_storage(normal_pow2(a.to_storage())); }
5043 template<std::size_t K> requires(K<32)
5044 [[nodiscard]] friend hint_inline constexpr simd operator<<(simd a,imm_t<K>) noexcept requires simd_integer_element<T> { return from_storage(a.to_storage()<<imm<K>); }
5046 template<std::size_t K> requires(K<32)
5047 [[nodiscard]] friend hint_inline constexpr simd operator>>(simd a,imm_t<K>) noexcept requires simd_integer_element<T> { return from_storage(a.to_storage()>>imm<K>); }
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>
5053 [[nodiscard]] hint_inline constexpr simd right() const noexcept { return *this>>imm<K>; }
5054 // Match native integer registers: division and run-time shifts are absent.
5056 friend simd operator<<(simd,simd) requires simd_integer_element<T> = delete;
5058 friend simd operator>>(simd,simd) requires simd_integer_element<T> = delete;
5060 friend simd operator/(simd,simd) requires simd_integer_element<T> = delete;
5062 friend simd operator%(simd,simd) requires simd_integer_element<T> = delete;
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; }
5089 private:
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);
5096 }
5097 };
5098
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); }
5118}
5119
5120#endif
5121namespace native {
5122 namespace detail::NATIVE_BACKEND {
5123 enum class rounding_direction { down, up, zero };
5124
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>;
5128 if consteval {
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);
5132 }
5133 if constexpr (N == 2 || N == 3) {
5134 return V::from_storage(round_integral<Direction>(x.to_storage()));
5135 } else {
5136#if NATIVE_HAS_AVX2
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));
5145#endif
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));
5152 } else {
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()));
5156 }
5157#else
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()));
5161#endif
5162 }
5163 }
5164 }
5165
5166 // Directions are encoded in the instruction, never taken from the ambient
5167 // rounding mode. As for other raw arithmetic, DAZ/FZ input handling follows
5168 // the configured floating-point environment.
5174 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5175 [[nodiscard]] hint_inline constexpr simd<float,N,Arch> floor(simd<float,N,Arch> x) noexcept {
5176 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::down>(x);
5177 }
5178
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)...}};
5184 }
5185
5190 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5191 [[nodiscard]] hint_inline constexpr simd<float,N,Arch> ceil(simd<float,N,Arch> x) noexcept {
5192 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::up>(x);
5193 }
5194
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)...}};
5200 }
5201
5206 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) && ::NATIVE_BACKEND_NAMESPACE::float_shape<N>
5207 [[nodiscard]] hint_inline constexpr simd<float,N,Arch> trunc(simd<float,N,Arch> x) noexcept {
5208 return detail::NATIVE_BACKEND::round_integral<detail::NATIVE_BACKEND::rounding_direction::zero>(x);
5209 }
5210
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)...}};
5216 }
5217}
5218
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 {
5222#if NATIVE_HAS_AVX2
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);
5227 } else
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);
5232 } else
5233#endif
5234 return std::uint32_t(mask.to_bitset()) & ((1u << N) - 1);
5235 }
5236
5237 // Four-lane shuffles use byte indices on both SSSE3 and NEON. The eight-lane
5238 // AVX2 permutation expands eight byte indices to dwords (2 KiB per table).
5239 template<bool Expand, std::size_t Width> 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);
5251 ++packed;
5252 }
5253 }
5254 return table;
5255 }();
5256
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;
5260 else {
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));
5266 }
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));
5272 } else {
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));
5276 }
5277#else
5278 else
5279#endif
5280#endif
5281#if (!NATIVE_HAS_AVX512F || !NATIVE_HAS_AVX512VL) && (NATIVE_HAS_AVX2 || NATIVE_HAS_ARM_NEON)
5282 {
5283 V permuted;
5284#if NATIVE_HAS_AVX2
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));
5289 } else {
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));
5293 }
5294#else
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())));
5297#endif
5298 // Build the destination predicate in registers instead of round-tripping
5299 // a packed mask through the generic byte-oriented mask representation.
5300#if NATIVE_HAS_AVX2
5301 if constexpr (sizeof(V) == 32) {
5302 __m256i live;
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));
5308 } else {
5309 __m128i 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));
5315 }
5316#else
5317 uint32x4_t 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()));
5321 } else {
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))));
5324 }
5325 return __builtin_bit_cast(V,vbslq_u8(vreinterpretq_u8_u32(live),__builtin_bit_cast(uint8x16_t,permuted),__builtin_bit_cast(uint8x16_t,prior)));
5326#endif
5327 }
5328#endif
5329 }
5330 }
5331}
5332
5333namespace native {
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>
5342 [[nodiscard]] hint_inline constexpr compaction_result<simd<T,N,Arch>> compress(
5343 typename simd<T,N,Arch>::mask mask, simd<T,N,Arch> value, T fill = T{}) noexcept {
5344 if consteval {
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];
5348 return {simd<T,N,Arch>::load(output.data()),count};
5349 }
5350 using V = simd<T,N,Arch>;
5351 auto bits = detail::NATIVE_BACKEND::compaction_mask_bits<N>(mask);
5352 // Construct fill through integer object representation, without FP arithmetic.
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))};
5356 }
5357
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>
5364 [[nodiscard]] hint_inline constexpr simd<T,N,Arch> expand(
5365 typename simd<T,N,Arch>::mask mask, simd<T,N,Arch> packed,
5366 simd<T,N,Arch> prior) noexcept {
5367 if consteval {
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++];
5371 return simd<T,N,Arch>::load(output.data());
5372 }
5373 auto bits = detail::NATIVE_BACKEND::compaction_mask_bits<N>(mask);
5374 auto result = detail::NATIVE_BACKEND::compact_register<true>(bits, packed, prior);
5375 if constexpr (N == 2 || N == 3) return simd<T,N,Arch>::from_storage(result.to_storage());
5376 else return result;
5377 }
5378
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,
5387 typename simd<T,N,Arch>::mask mask, simd<T,N,Arch> value) noexcept {
5388 if consteval {
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];
5393 return written;
5394 }
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)) {
5401 // Compress in registers, then store only the bounded contiguous prefix.
5402 // This also avoids trimming the source predicate when capacity is small.
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);
5410 } else {
5411 auto packed = _mm_maskz_compress_epi32(__mmask8(bits),__builtin_bit_cast(__m128i,value));
5412 _mm_mask_storeu_epi32(destination,__mmask8(prefix),packed);
5413 }
5414 return written;
5415 }
5416#endif
5417 std::array<T,N> packed;
5418 compress(mask, value).value.store(packed.data());
5419 std::memcpy(destination, packed.data(), written * sizeof(T));
5420 return written;
5421 }
5422}
5423
5424#if NATIVE_HAS_WASM_SIMD128 || defined(NATIVE_DOXYGEN)
5425// Included only in the SIMD128 backend target scope.
5426namespace native {
5427 namespace detail {
5428 template<class T>
5429 concept wasm_number =
5430 simd_integer_element<T> || std::same_as<T, float> || std::same_as<T, double>;
5431 template<class T>
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>>>;
5436 template<class T>
5437 using wasm_format =
5438 std::conditional_t<sizeof(T) == 4, constexpr_float::binary32, constexpr_float::binary64>;
5439
5440 template<class V>
5441 constexpr auto wasm_lanes(V v) noexcept {
5442 std::array<typename V::value_type, V::lanes> a{};
5443 v.store(a.data());
5444 return a;
5445 }
5446
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);
5453 }
5454 return V::load(r.data());
5455 }
5456
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;
5464 };
5465 } // namespace detail
5466
5468 template<class U, std::size_t N, isa<> A>
5469 requires(A.has(wasm_feature::simd128)) && (sizeof(U) * N == 16)
5470 struct alignas(16) simd<mask_lane<U>, N, A> {
5471 using value_type = mask_lane<U>;
5472 using native_type = v128_t;
5473 using mask_type = simd;
5474 using mask = simd;
5475 template<class T>
5476 using rebind = simd<T, N, A>;
5477 static constexpr auto architecture = A;
5478 static constexpr std::size_t lanes = N;
5479 static constexpr bool compact = false;
5480
5481 private:
5482 native_type value_{};
5483
5484 public:
5486 constexpr simd() noexcept = default;
5487
5489 explicit hint_inline constexpr simd(bool x) noexcept
5490 : simd(from_bitset(x ? ~std::uint64_t{} : 0)) {}
5491
5493 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept {
5494 return value_;
5495 }
5496
5498 [[nodiscard]] static hint_inline constexpr simd unsafe_from_native(native_type x) noexcept {
5499 simd r;
5500 r.value_ = x;
5501 return r;
5502 }
5503
5505 [[nodiscard]] static hint_inline constexpr simd from_native(native_type x) noexcept {
5506 if consteval {
5507 auto words = __builtin_bit_cast(std::array<U, N>, x);
5508 for (auto & w : words) {
5509 w = w ? U(~U(0)) : U(0);
5510 }
5511 return unsafe_from_native(__builtin_bit_cast(native_type, words));
5512 } else {
5513 if constexpr (sizeof(U) == 1) {
5514 return unsafe_from_native(wasm_i8x16_ne(x, wasm_i32x4_splat(0)));
5515 } else if constexpr (sizeof(U) == 2) {
5516 return unsafe_from_native(wasm_i16x8_ne(x, wasm_i32x4_splat(0)));
5517 } else if constexpr (sizeof(U) == 4) {
5518 return unsafe_from_native(wasm_i32x4_ne(x, wasm_i32x4_splat(0)));
5519 } else if constexpr (sizeof(U) == 8) {
5520 return unsafe_from_native(wasm_i64x2_ne(x, wasm_i32x4_splat(0)));
5521 }
5522 }
5523 }
5524
5526 [[nodiscard]] static hint_inline constexpr simd from_bitset(std::uint64_t bits) noexcept {
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);
5530 }
5531 return unsafe_from_native(__builtin_bit_cast(native_type, a));
5532 }
5533
5535 [[nodiscard]] hint_inline constexpr std::uint64_t to_bitset() const noexcept {
5536 if consteval {
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;
5541 }
5542 return r;
5543 } else {
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_);
5552 }
5553 }
5554 }
5555
5557 [[nodiscard]] hint_inline constexpr std::uint64_t bits() const noexcept {
5558 return to_bitset();
5559 }
5560
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();
5566 }
5567 return unsafe_from_native(__builtin_bit_cast(native_type, a));
5568 }
5569
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) {
5574 p[i] = value_type::from_bits(a[i]);
5575 }
5576 }
5577
5579 template<std::size_t Align>
5580 [[nodiscard]] static hint_inline constexpr simd load_memory(value_type const * p) noexcept {
5581 return load(p);
5582 }
5583
5585 template<std::size_t Align>
5586 hint_inline constexpr void store_memory(value_type * p) const noexcept {
5587 store(p);
5588 }
5589
5591 [[nodiscard]] friend hint_inline constexpr bool any(simd v) noexcept {
5592 if consteval {
5593 return v.to_bitset() != 0;
5594 } else {
5595 return wasm_v128_any_true(v.value_);
5596 }
5597 }
5598
5600 [[nodiscard]] friend hint_inline constexpr bool all(simd v) noexcept {
5601 if consteval {
5602 return v.to_bitset() == ((std::uint64_t{1} << N) - 1);
5603 } else {
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_);
5610 } else {
5611 return wasm_i64x2_all_true(v.value_);
5612 }
5613 }
5614 }
5615
5617 [[nodiscard]] friend hint_inline constexpr bool none(simd v) noexcept {
5618 return !any(v);
5619 }
5620
5622 [[nodiscard]] friend hint_inline constexpr simd operator&(simd a, simd b) noexcept {
5623 if consteval {
5624 return from_bitset(a.to_bitset() & b.to_bitset());
5625 } else {
5626 return unsafe_from_native(wasm_v128_and(a.value_, b.value_));
5627 }
5628 }
5629
5631 hint_inline constexpr simd & operator&=(simd b) noexcept {
5632 return *this = *this & b;
5633 }
5634
5636 [[nodiscard]] friend hint_inline constexpr simd operator|(simd a, simd b) noexcept {
5637 if consteval {
5638 return from_bitset(a.to_bitset() | b.to_bitset());
5639 } else {
5640 return unsafe_from_native(wasm_v128_or(a.value_, b.value_));
5641 }
5642 }
5643
5645 hint_inline constexpr simd & operator|=(simd b) noexcept {
5646 return *this = *this | b;
5647 }
5648
5650 [[nodiscard]] friend hint_inline constexpr simd operator^(simd a, simd b) noexcept {
5651 if consteval {
5652 return from_bitset(a.to_bitset() ^ b.to_bitset());
5653 } else {
5654 return unsafe_from_native(wasm_v128_xor(a.value_, b.value_));
5655 }
5656 }
5657
5659 hint_inline constexpr simd & operator^=(simd b) noexcept {
5660 return *this = *this ^ b;
5661 }
5662
5664 [[nodiscard]] friend hint_inline constexpr simd operator~(simd a) noexcept {
5665 if consteval {
5666 return from_bitset(~a.to_bitset());
5667 } else {
5668 return unsafe_from_native(wasm_v128_not(a.value_));
5669 }
5670 }
5671
5673 [[nodiscard]] friend hint_inline constexpr simd operator!(simd a) noexcept {
5674 return ~a;
5675 }
5676
5678 [[nodiscard]] friend hint_inline constexpr simd operator==(simd a, simd b) noexcept {
5679 return ~(a ^ b);
5680 }
5681
5683 [[nodiscard]] friend hint_inline constexpr simd operator!=(simd a, simd b) noexcept {
5684 return a ^ b;
5685 }
5686
5688 [[nodiscard]] friend hint_inline constexpr simd select(simd m, simd a, simd b) noexcept {
5689 return (m & a) | (~m & b);
5690 }
5691 };
5692
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;
5705 template<class U>
5706 using rebind = simd<U, N, A>;
5707 static constexpr auto architecture = A;
5708 static constexpr std::size_t lanes = N;
5709
5710 private:
5711 native_type value_{};
5712
5713 public:
5715 constexpr simd() noexcept = default;
5716
5718 hint_inline constexpr simd(T x) noexcept
5719 requires std::is_floating_point_v<T>
5720 {
5721 if consteval {
5722 std::array<T, N> a{};
5723 a.fill(x);
5724 value_ = __builtin_bit_cast(native_type, a);
5725 } else {
5726 if constexpr (std::same_as<T, float>) {
5727 value_ = wasm_f32x4_splat(x);
5728 } else {
5729 value_ = wasm_f64x2_splat(x);
5730 }
5731 }
5732 }
5733
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);
5739 if consteval {
5740 std::array<T, N> a{};
5741 a.fill(x);
5742 value_ = __builtin_bit_cast(native_type, a);
5743 } else {
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);
5760 }
5761 }
5762 }
5763
5765 hint_inline constexpr explicit simd(std::array<T, N> const & a) noexcept
5766 : simd(load(a.data())) {}
5767
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)...}) {}
5773
5775 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept {
5776 return value_;
5777 }
5778
5780 [[nodiscard]] static hint_inline constexpr simd from_native(native_type x) noexcept {
5781 simd r;
5782 r.value_ = x;
5783 return r;
5784 }
5785
5787 [[nodiscard]] static hint_inline constexpr simd unsafe_from_native(native_type x) noexcept {
5788 return from_native(x);
5789 }
5790
5792 [[nodiscard]] static hint_inline constexpr simd load(T const * p) noexcept {
5793 if consteval {
5794 std::array<T, N> a{};
5795 for (std::size_t i = 0; i < N; ++i) {
5796 a[i] = p[i];
5797 }
5798 return from_native(__builtin_bit_cast(native_type, a));
5799 } else {
5800 return from_native(wasm_v128_load(p));
5801 }
5802 }
5803
5805 hint_inline constexpr void store(T * p) const noexcept {
5806 if consteval {
5807 auto a = __builtin_bit_cast(std::array<T, N>, value_);
5808 for (std::size_t i = 0; i < N; ++i) {
5809 p[i] = a[i];
5810 }
5811 } else {
5812 wasm_v128_store(p, value_);
5813 }
5814 }
5815
5817 template<std::size_t Align>
5818 [[nodiscard]] static hint_inline constexpr simd load_memory(T const * p) noexcept {
5819 if consteval {
5820 return load(p);
5821 } else {
5822 return load(static_cast<T const *>(__builtin_assume_aligned(p, Align)));
5823 }
5824 }
5825
5827 template<std::size_t Align>
5828 hint_inline constexpr void store_memory(T * p) const noexcept {
5829 if consteval {
5830 store(p);
5831 } else {
5832 store(static_cast<T *>(__builtin_assume_aligned(p, Align)));
5833 }
5834 }
5835
5837 [[nodiscard]] static hint_inline constexpr simd load_partial(T const * p, std::size_t n,
5838 T fill = {}) noexcept {
5839 std::array<T, N> a{};
5840 a.fill(fill);
5841 if consteval { for(std::size_t i=0;i<n;++i) a[i]=p[i]; }
5842 else {
5843 if constexpr(A.has(polyfill)) {
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];
5846 }
5847 return load(a.data());
5848 }
5849
5851 hint_inline constexpr void store_partial(T * p, std::size_t n) const noexcept {
5852 auto a = detail::wasm_lanes(*this);
5853 if consteval { for(std::size_t i=0;i<n;++i) p[i]=a[i]; }
5854 else {
5855 if constexpr(A.has(polyfill)) {
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];
5858 }
5859 }
5860
5862 [[nodiscard]] static hint_inline constexpr simd load_bits(word_type const * p) noexcept {
5863 return from_native(bits_type::load(p).to_native());
5864 }
5865
5867 hint_inline constexpr void store_bits(word_type * p) const noexcept {
5868 bits().store(p);
5869 }
5870
5872 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept {
5873 return bits_type::from_native(value_);
5874 }
5875
5877 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type x) noexcept {
5878 return from_native(x.to_native());
5879 }
5880
5882 template<std::size_t I>
5883 requires(I < N)
5884 [[nodiscard]] hint_inline constexpr T get() const noexcept {
5885 if consteval {
5886 return __builtin_bit_cast(std::array<T, N>, value_)[I];
5887 } else {
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);
5908 }
5909 }
5910 }
5911
5913 template<std::size_t I>
5914 requires(I < N)
5915 [[nodiscard]] hint_inline constexpr simd replace(T x) const noexcept {
5916 if consteval {
5917 auto a = __builtin_bit_cast(std::array<T, N>, value_);
5918 a[I] = x;
5919 return load(a.data());
5920 } else {
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));
5941 }
5942 }
5943 }
5944
5946 [[nodiscard]] friend hint_inline constexpr simd operator+(simd a, simd b) noexcept {
5947 if consteval {
5948 return detail::wasm_map(
5949 [](T x, T y) {
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)));
5954 } else {
5955 return std::bit_cast<T>(
5956 word_type(std::uint64_t(word_type(x)) + std::uint64_t(word_type(y))));
5957 }
5958 },
5959 a, b);
5960 } else {
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_));
5981 }
5982 }
5983 }
5984
5986 hint_inline constexpr simd & operator+=(simd b) noexcept {
5987 return *this = *this + b;
5988 }
5989
5991 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a, simd b) noexcept {
5992 if consteval {
5993 return detail::wasm_map(
5994 [](T x, T y) {
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)));
5999 } else {
6000 return std::bit_cast<T>(
6001 word_type(std::uint64_t(word_type(x)) - std::uint64_t(word_type(y))));
6002 }
6003 },
6004 a, b);
6005 } else {
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_));
6026 }
6027 }
6028 }
6029
6031 hint_inline constexpr simd & operator-=(simd b) noexcept {
6032 return *this = *this - b;
6033 }
6034
6036 [[nodiscard]] friend hint_inline constexpr simd operator*(simd a, simd b) noexcept
6037 requires(sizeof(T) > 1 || A.has(polyfill))
6038 {
6039 if consteval {
6040 return detail::wasm_map(
6041 [](T x, T y) {
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)));
6046 } else {
6047 return std::bit_cast<T>(
6048 word_type(std::uint64_t(word_type(x)) * std::uint64_t(word_type(y))));
6049 }
6050 },
6051 a, b);
6052 } else {
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_));
6073 }
6074 }
6075 }
6076
6078 hint_inline constexpr simd & operator*=(simd b) noexcept
6079 requires(sizeof(T) > 1 || A.has(polyfill))
6080 {
6081 return *this = *this * b;
6082 }
6083
6085 [[nodiscard]] friend hint_inline constexpr simd operator/(simd a, simd b) noexcept
6086 requires std::is_floating_point_v<T>
6087 {
6088 if consteval {
6089 return detail::wasm_map(
6090 [](T x, T y) {
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)));
6094 },
6095 a, b);
6096 } else {
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_));
6101 }
6102 }
6103 }
6104
6106 hint_inline constexpr simd & operator/=(simd b) noexcept
6107 requires std::is_floating_point_v<T>
6108 {
6109 return *this = *this / b;
6110 }
6111
6113 [[nodiscard]] friend hint_inline constexpr simd operator&(simd a, simd b) noexcept {
6114 if consteval {
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) {
6118 x[i] &= y[i];
6119 }
6120 return from_native(__builtin_bit_cast(native_type, x));
6121 } else {
6122 return from_native(wasm_v128_and(a.value_, b.value_));
6123 }
6124 }
6125
6127 hint_inline constexpr simd & operator&=(simd b) noexcept {
6128 return *this = *this & b;
6129 }
6130
6132 [[nodiscard]] friend hint_inline constexpr simd operator|(simd a, simd b) noexcept {
6133 if consteval {
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) {
6137 x[i] |= y[i];
6138 }
6139 return from_native(__builtin_bit_cast(native_type, x));
6140 } else {
6141 return from_native(wasm_v128_or(a.value_, b.value_));
6142 }
6143 }
6144
6146 hint_inline constexpr simd & operator|=(simd b) noexcept {
6147 return *this = *this | b;
6148 }
6149
6151 [[nodiscard]] friend hint_inline constexpr simd operator^(simd a, simd b) noexcept {
6152 if consteval {
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) {
6156 x[i] ^= y[i];
6157 }
6158 return from_native(__builtin_bit_cast(native_type, x));
6159 } else {
6160 return from_native(wasm_v128_xor(a.value_, b.value_));
6161 }
6162 }
6163
6165 hint_inline constexpr simd & operator^=(simd b) noexcept {
6166 return *this = *this ^ b;
6167 }
6168
6170 [[nodiscard]] friend hint_inline constexpr simd operator~(simd a) noexcept {
6171 if consteval {
6172 auto x = __builtin_bit_cast(std::array<word_type, N>, a.value_);
6173 for (auto & w : x) {
6174 w = word_type(~w);
6175 }
6176 return from_native(__builtin_bit_cast(native_type, x));
6177 } else {
6178 return from_native(wasm_v128_not(a.value_));
6179 }
6180 }
6181
6183 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a) noexcept {
6184 if consteval {
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);
6189 } else {
6190 w = word_type(0 - w);
6191 }
6192 }
6193 return from_native(__builtin_bit_cast(native_type, x));
6194 } else {
6195 if constexpr (std::same_as<T, float>) {
6196 return from_native(wasm_f32x4_neg(a.value_));
6197 } else if constexpr (std::same_as<T, double>) {
6198 return from_native(wasm_f64x2_neg(a.value_));
6199 } else if constexpr (std::same_as<T, std::int8_t>) {
6200 return from_native(wasm_i8x16_neg(a.value_));
6201 } else if constexpr (std::same_as<T, std::uint8_t>) {
6202 return from_native(wasm_i8x16_neg(a.value_));
6203 } else if constexpr (std::same_as<T, std::int16_t>) {
6204 return from_native(wasm_i16x8_neg(a.value_));
6205 } else if constexpr (std::same_as<T, std::uint16_t>) {
6206 return from_native(wasm_i16x8_neg(a.value_));
6207 } else if constexpr (std::same_as<T, std::int32_t>) {
6208 return from_native(wasm_i32x4_neg(a.value_));
6209 } else if constexpr (std::same_as<T, std::uint32_t>) {
6210 return from_native(wasm_i32x4_neg(a.value_));
6211 } else if constexpr (std::same_as<T, std::int64_t>) {
6212 return from_native(wasm_i64x2_neg(a.value_));
6213 } else if constexpr (std::same_as<T, std::uint64_t>) {
6214 return from_native(wasm_i64x2_neg(a.value_));
6215 }
6216 }
6217 }
6218
6220 [[nodiscard]] friend hint_inline constexpr mask_type operator==(simd a, simd b) noexcept {
6221 if consteval {
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;
6226 }
6228 } else {
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_));
6249 }
6250 }
6251 }
6252
6254 [[nodiscard]] friend hint_inline constexpr mask_type operator!=(simd a, simd b) noexcept {
6255 if consteval {
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;
6260 }
6262 } else {
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_));
6283 }
6284 }
6285 }
6286
6288 [[nodiscard]] friend hint_inline constexpr mask_type operator<(simd a, simd b) noexcept {
6289 if consteval {
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;
6294 }
6296 } else {
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))));
6319 }
6320 }
6321 }
6322
6324 [[nodiscard]] friend hint_inline constexpr mask_type operator<=(simd a, simd b) noexcept {
6325 if consteval {
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;
6330 }
6332 } else {
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))));
6355 }
6356 }
6357 }
6358
6360 [[nodiscard]] friend hint_inline constexpr mask_type operator>(simd a, simd b) noexcept {
6361 if consteval {
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;
6366 }
6368 } else {
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))));
6391 }
6392 }
6393 }
6394
6396 [[nodiscard]] friend hint_inline constexpr mask_type operator>=(simd a, simd b) noexcept {
6397 if consteval {
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;
6402 }
6404 } else {
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))));
6427 }
6428 }
6429 }
6430
6432 [[nodiscard]] hint_inline constexpr simd left(unsigned count) const noexcept
6433 requires simd_integer_element<T>
6434 {
6435 count %= sizeof(T) * 8;
6436 if consteval {
6437 return detail::wasm_map(
6438 [&](T x) { return std::bit_cast<T>(word_type(std::uint64_t(word_type(x)) << count)); },
6439 *this);
6440 } else {
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));
6457 }
6458 }
6459 }
6460
6462 template<std::size_t K>
6463 [[nodiscard]] hint_inline constexpr simd left() const noexcept
6464 requires simd_integer_element<T>
6465 {
6466 return left(K);
6467 }
6468
6470 [[nodiscard]] friend hint_inline constexpr simd operator<<(simd a, unsigned n) noexcept
6471 requires simd_integer_element<T>
6472 {
6473 return a.left(n);
6474 }
6475
6477 template<std::size_t K>
6478 [[nodiscard]] friend hint_inline constexpr simd operator<<(simd a, imm_t<K>) noexcept
6479 requires simd_integer_element<T>
6480 {
6481 return a.left(K);
6482 }
6483
6485 [[nodiscard]] hint_inline constexpr simd right(unsigned count) const noexcept
6486 requires simd_integer_element<T>
6487 {
6488 count %= sizeof(T) * 8;
6489 if consteval {
6490 return detail::wasm_map([&](T x) { return T(x >> count); }, *this);
6491 } else {
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));
6508 }
6509 }
6510 }
6511
6513 template<std::size_t K>
6514 [[nodiscard]] hint_inline constexpr simd right() const noexcept
6515 requires simd_integer_element<T>
6516 {
6517 return right(K);
6518 }
6519
6521 [[nodiscard]] friend hint_inline constexpr simd operator>>(simd a, unsigned n) noexcept
6522 requires simd_integer_element<T>
6523 {
6524 return a.right(n);
6525 }
6526
6528 template<std::size_t K>
6529 [[nodiscard]] friend hint_inline constexpr simd operator>>(simd a, imm_t<K>) noexcept
6530 requires simd_integer_element<T>
6531 {
6532 return a.right(K);
6533 }
6534 };
6535
6537 template<detail::wasm_number T, std::size_t N, isa<> A>
6538 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6539 [[nodiscard]] hint_inline constexpr simd<T, N, A> select(typename simd<T, N, A>::mask_type m, simd<T, N, A> a,
6540 simd<T, N, A> b) noexcept {
6541 if consteval {
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)) {
6546 x[i] = y[i];
6547 }
6548 }
6549 return simd<T, N, A>::load(x.data());
6550 } else {
6552 wasm_v128_bitselect(a.to_native(), b.to_native(), m.to_native()));
6553 }
6554 }
6555
6557 template<detail::wasm_number T, std::size_t N, isa<> A>
6558 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6559 [[nodiscard]] hint_inline constexpr simd<T, N, A> bit_select(simd<detail::wasm_word<T>, N, A> m,
6560 simd<T, N, A> a, simd<T, N, A> b) noexcept {
6561 return simd<T, N, A>::from_bits((m & a.bits()) | (~m & b.bits()));
6562 }
6563
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>
6568 [[nodiscard]] hint_inline constexpr simd<To, N, A> mask_bits(simd<mask_lane<U>, N, A> m) noexcept {
6569 return simd<To, N, A>::from_native(m.to_native());
6570 }
6571
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)
6575 [[nodiscard]] hint_inline constexpr simd<T, N, A> broadcast(simd<T, N, A> v, imm_t<I>) noexcept {
6576 return simd<T, N, A>(v.template get<I>());
6577 }
6578
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) && ...)
6583 [[nodiscard]] hint_inline constexpr simd<T, N, A> shuffle(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6584 if consteval {
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])...};
6587 return simd<T, N, A>::load(r.data());
6588 } else {
6589 using vector_type = T __attribute__((ext_vector_type(N)));
6590 return simd<T, N, A>::from_native(__builtin_bit_cast(
6591 v128_t, __builtin_shufflevector(__builtin_bit_cast(vector_type, a.to_native()),
6592 __builtin_bit_cast(vector_type, b.to_native()), I...)));
6593 }
6594 }
6595
6597 template<isa<> A>
6598 requires(A.has(wasm_feature::simd128))
6599 [[nodiscard]] hint_inline constexpr simd<std::uint8_t, 16, A>
6601 if consteval {
6602 auto x = detail::wasm_lanes(v), i = detail::wasm_lanes(indices);
6603 for (auto & n : i) {
6604 n = n < 16 ? x[n] : 0;
6605 }
6606 return simd<std::uint8_t, 16, A>::load(i.data());
6607 } else {
6609 wasm_i8x16_swizzle(v.to_native(), indices.to_native()));
6610 }
6611 }
6612
6614 template<std::floating_point T, std::size_t N, isa<> A>
6615 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6616 [[nodiscard]] hint_inline constexpr simd<T, N, A> sqrt(simd<T, N, A> v) noexcept {
6617 if consteval {
6618 return detail::wasm_map(
6619 [](T x) {
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));
6624 },
6625 v);
6626 } else {
6627 if constexpr (sizeof(T) == 4) {
6628 return simd<T, N, A>::from_native(wasm_f32x4_sqrt(v.to_native()));
6629 } else {
6630 return simd<T, N, A>::from_native(wasm_f64x2_sqrt(v.to_native()));
6631 }
6632 }
6633 }
6634
6636 template<std::floating_point T, std::size_t N, isa<> A>
6637 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6638 [[nodiscard]] hint_inline constexpr simd<T, N, A> floor(simd<T, N, A> v) noexcept {
6639 if consteval {
6640 return detail::wasm_map(
6641 [](T x) {
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));
6647 },
6648 v);
6649 } else {
6650 if constexpr (sizeof(T) == 4) {
6651 return simd<T, N, A>::from_native(wasm_f32x4_floor(v.to_native()));
6652 } else {
6653 return simd<T, N, A>::from_native(wasm_f64x2_floor(v.to_native()));
6654 }
6655 }
6656 }
6657
6659 template<std::floating_point T, std::size_t N, isa<> A>
6660 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6661 [[nodiscard]] hint_inline constexpr simd<T, N, A> ceil(simd<T, N, A> v) noexcept {
6662 if consteval {
6663 return detail::wasm_map(
6664 [](T x) {
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));
6670 },
6671 v);
6672 } else {
6673 if constexpr (sizeof(T) == 4) {
6674 return simd<T, N, A>::from_native(wasm_f32x4_ceil(v.to_native()));
6675 } else {
6676 return simd<T, N, A>::from_native(wasm_f64x2_ceil(v.to_native()));
6677 }
6678 }
6679 }
6680
6682 template<std::floating_point T, std::size_t N, isa<> A>
6683 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6684 [[nodiscard]] hint_inline constexpr simd<T, N, A> trunc(simd<T, N, A> v) noexcept {
6685 if consteval {
6686 return detail::wasm_map(
6687 [](T x) {
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));
6693 },
6694 v);
6695 } else {
6696 if constexpr (sizeof(T) == 4) {
6697 return simd<T, N, A>::from_native(wasm_f32x4_trunc(v.to_native()));
6698 } else {
6699 return simd<T, N, A>::from_native(wasm_f64x2_trunc(v.to_native()));
6700 }
6701 }
6702 }
6703
6705 template<std::floating_point T, std::size_t N, isa<> A>
6706 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6707 [[nodiscard]] hint_inline constexpr simd<T, N, A> round_even(simd<T, N, A> v) noexcept {
6708 if consteval {
6709 return detail::wasm_map(
6710 [](T x) {
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));
6715 },
6716 v);
6717 } else {
6718 if constexpr (sizeof(T) == 4) {
6719 return simd<T, N, A>::from_native(wasm_f32x4_nearest(v.to_native()));
6720 } else {
6721 return simd<T, N, A>::from_native(wasm_f64x2_nearest(v.to_native()));
6722 }
6723 }
6724 }
6725
6727 template<std::floating_point T, std::size_t N, isa<> A>
6728 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6729 [[nodiscard]] hint_inline constexpr simd<T, N, A> abs(simd<T, N, A> v) noexcept {
6730 if consteval {
6731 return detail::wasm_map(
6732 [](T x) {
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));
6737 },
6738 v);
6739 } else {
6740 if constexpr (sizeof(T) == 4) {
6741 return simd<T, N, A>::from_native(wasm_f32x4_abs(v.to_native()));
6742 } else {
6743 return simd<T, N, A>::from_native(wasm_f64x2_abs(v.to_native()));
6744 }
6745 }
6746 }
6747} // namespace native
6748// SIMD128 instruction families, included in the SIMD128 target scope.
6749namespace native {
6750 namespace detail {
6751 template<class T>
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();
6755 }
6756 if (x > std::int64_t(std::numeric_limits<T>::max())) {
6757 return std::numeric_limits<T>::max();
6758 }
6759 return T(x);
6760 }
6761
6762 template<class To, class From>
6763 constexpr To wasm_trunc_sat(From x) noexcept {
6764 if (x != x) {
6765 return 0;
6766 }
6767 if (x <= double(std::numeric_limits<To>::min())) {
6768 return std::numeric_limits<To>::min();
6769 }
6770 if (x >= double(std::numeric_limits<To>::max())) {
6771 return std::numeric_limits<To>::max();
6772 }
6773 return To(x);
6774 }
6775 } // namespace detail
6776
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)
6780 [[nodiscard]] hint_inline constexpr simd<T, N, A> add_sat(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6781 using vector_type = simd<T, N, A>;
6782 if consteval {
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);
6785 } else {
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()));
6794 }
6795 }
6796 }
6797
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)
6801 [[nodiscard]] hint_inline constexpr simd<T, N, A> sub_sat(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6802 using vector_type = simd<T, N, A>;
6803 if consteval {
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);
6806 } else {
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()));
6815 }
6816 }
6817 }
6818
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)
6823 [[nodiscard]] hint_inline constexpr simd<T, N, A> average_round(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6824 if consteval {
6825 return detail::wasm_map([](T x, T y) { return T((unsigned(x) + y + 1) / 2); }, a, b);
6826 } else {
6827 if constexpr (sizeof(T) == 1) {
6828 return simd<T, N, A>::from_native(wasm_u8x16_avgr(a.to_native(), b.to_native()));
6829 } else {
6830 return simd<T, N, A>::from_native(wasm_u16x8_avgr(a.to_native(), b.to_native()));
6831 }
6832 }
6833 }
6834
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)
6838 [[nodiscard]] hint_inline constexpr simd<T, N, A> abs(simd<T, N, A> a) noexcept {
6839 using vector_type = simd<T, N, A>;
6840 using unsigned_type = std::make_unsigned_t<T>;
6841 if consteval {
6842 return detail::wasm_map(
6843 [](T x) { return x < 0 ? std::bit_cast<T>(unsigned_type(0 - unsigned_type(x))) : x; }, a);
6844 } else {
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()));
6853 }
6854 }
6855 }
6856
6858 template<detail::wasm_number T, std::size_t N, isa<> A>
6859 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6860 [[nodiscard]] hint_inline constexpr simd<T, N, A> min(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6861 using vector_type = simd<T, N, A>;
6862 if consteval {
6863 return detail::wasm_map(
6864 [](T x, T y) {
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>({}));
6872 }
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));
6876 }
6877 }
6878 return y < x ? y : x;
6879 },
6880 a, b);
6881 } else {
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()));
6898 } else {
6899 return select(a < b, a, b);
6900 }
6901 }
6902 }
6903
6905 template<detail::wasm_number T, std::size_t N, isa<> A>
6906 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
6907 [[nodiscard]] hint_inline constexpr simd<T, N, A> max(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6908 using vector_type = simd<T, N, A>;
6909 if consteval {
6910 return detail::wasm_map(
6911 [](T x, T y) {
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>({}));
6919 }
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));
6923 }
6924 }
6925 return y > x ? y : x;
6926 },
6927 a, b);
6928 } else {
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()));
6945 } else {
6946 return select(a > b, a, b);
6947 }
6948 }
6949 }
6950
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>)
6954 [[nodiscard]] hint_inline constexpr simd<T, N, A> pmin(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6955 using vector_type = simd<T, N, A>;
6956 if consteval {
6957 return detail::wasm_map([](T x, T y) { return y < x ? y : x; }, a, b);
6958 } else {
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()));
6963 }
6964 }
6965 }
6966
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>)
6970 [[nodiscard]] hint_inline constexpr simd<T, N, A> pmax(simd<T, N, A> a, simd<T, N, A> b) noexcept {
6971 using vector_type = simd<T, N, A>;
6972 if consteval {
6973 return detail::wasm_map([](T x, T y) { return y > x ? y : x; }, a, b);
6974 } else {
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()));
6979 }
6980 }
6981 }
6982
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)
6986 [[nodiscard]] hint_inline constexpr auto extend_low(simd<T, N, A> a) noexcept {
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>>;
6990 using wide_type =
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>;
6994 if consteval {
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) {
6998 r[i] = x[i];
6999 }
7000 return vector_type::load(r.data());
7001 } else {
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()));
7014 }
7015 }
7016 }
7017
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)
7021 [[nodiscard]] hint_inline constexpr auto multiply_widened_low(simd<T, N, A> a, simd<T, N, A> b) noexcept {
7022 return extend_low(a) * extend_low(b);
7023 }
7024
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)
7028 [[nodiscard]] hint_inline constexpr auto extend_high(simd<T, N, A> a) noexcept {
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>>;
7032 using wide_type =
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>;
7036 if consteval {
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];
7041 }
7042 return vector_type::load(r.data());
7043 } else {
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()));
7056 }
7057 }
7058 }
7059
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)
7063 [[nodiscard]] hint_inline constexpr auto multiply_widened_high(simd<T, N, A> a, simd<T, N, A> b) noexcept {
7064 return extend_high(a) * extend_high(b);
7065 }
7066
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>)
7072 [[nodiscard]] hint_inline constexpr simd<To, N * 2, A> narrow_sat(simd<From, N, A> a,
7073 simd<From, N, A> b) noexcept {
7074 using vector_type = simd<To, N * 2, A>;
7075 if consteval {
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]);
7081 }
7082 return vector_type::load(r.data());
7083 } else {
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()));
7092 }
7093 }
7094 }
7095
7097 template<isa<> A>
7098 requires(A.has(wasm_feature::simd128))
7099 [[nodiscard]] hint_inline constexpr simd<std::int16_t, 8, A>
7101 if consteval {
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);
7105 },
7106 a, b);
7107 } else {
7109 wasm_i16x8_q15mulr_sat(a.to_native(), b.to_native()));
7110 }
7111 }
7112
7114 template<isa<> A>
7115 requires(A.has(wasm_feature::simd128))
7116 [[nodiscard]] hint_inline constexpr simd<std::int32_t, 4, A> dot(simd<std::int16_t, 8, A> a,
7117 simd<std::int16_t, 8, A> b) noexcept {
7118 using vector_type = simd<std::int32_t, 4, A>;
7119 if consteval {
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]));
7125 }
7126 return vector_type::load(r.data());
7127 } else {
7128 return vector_type::from_native(wasm_i32x4_dot_i16x8(a.to_native(), b.to_native()));
7129 }
7130 }
7131
7134 template<simd_integer_element To, std::floating_point From, std::size_t N, isa<> A>
7135 requires(A.has(wasm_feature::simd128)) && (sizeof(From) * N == 16 && sizeof(To) == 4)
7136 [[nodiscard]] hint_inline constexpr simd<To, 4, A> trunc_sat(simd<From, N, A> a) noexcept {
7137 using vector_type = simd<To, 4, A>;
7138 if consteval {
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]);
7143 }
7144 return vector_type::load(r.data());
7145 } else {
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()));
7152 } else {
7153 return vector_type::from_native(wasm_u32x4_trunc_sat_f64x2_zero(a.to_native()));
7154 }
7155 }
7156 }
7157
7160 template<std::floating_point 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>)
7164 [[nodiscard]] hint_inline constexpr simd<To, 16 / sizeof(To), A> convert(simd<From, N, A> a) noexcept {
7165 using vector_type = simd<To, 16 / sizeof(To), A>;
7166 if consteval {
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>;
7173 r[i] =
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])));
7176 } else {
7177 r[i] = To(x[i]);
7178 }
7179 }
7180 return vector_type::load(r.data());
7181 } else {
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()));
7192 } else {
7193 return vector_type::from_native(wasm_f64x2_convert_low_u32x4(a.to_native()));
7194 }
7195 }
7196 }
7197
7199 template<class V>
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);
7205 else {
7206 if consteval { return V(*p); }
7207 else {
7208 typename V::value_type value;
7209 std::memcpy(&value,reinterpret_cast<unsigned char const *>(p),sizeof(value)); return V(value);
7210 }
7211 }
7212 }
7213
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)
7217 [[nodiscard]] hint_inline constexpr simd<T, N, A> load_lane(T const * p, simd<T, N, A> a) noexcept {
7218 if constexpr(!A.has(polyfill)) return a.template replace<I>(*p);
7219 else {
7220 if consteval { return a.template replace<I>(*p); }
7221 else {
7222 T value; std::memcpy(&value,reinterpret_cast<unsigned char const *>(p),sizeof(T)); return a.template replace<I>(value);
7223 }
7224 }
7225 }
7226
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)
7230 hint_inline constexpr void store_lane(T * p, simd<T, N, A> a) noexcept {
7231 auto value=a.template get<I>();
7232 if constexpr(!A.has(polyfill)) *p=value;
7233 else {
7234 if consteval { *p=value; }
7235 else { std::memcpy(reinterpret_cast<unsigned char *>(p),&value,sizeof(T)); }
7236 }
7237 }
7238
7240 template<class V>
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 {
7244 if consteval {
7245 std::array<typename V::value_type, V::lanes> a{};
7246 a[0] = *p;
7247 return V::load(a.data());
7248 } else {
7249 if constexpr (sizeof(typename V::value_type) == 4) {
7250 return V::from_native(wasm_v128_load32_zero(p));
7251 } else {
7252 return V::from_native(wasm_v128_load64_zero(p));
7253 }
7254 }
7255 }
7256
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>;
7263 if consteval {
7264 std::array<To, vector_type::lanes> a{};
7265 for (std::size_t i = 0; i < vector_type::lanes; ++i) {
7266 a[i] = p[i];
7267 }
7268 return vector_type::load(a.data());
7269 } else {
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));
7282 }
7283 }
7284 }
7285
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)
7290 [[nodiscard]] hint_inline constexpr auto pairwise_add_widened(simd<T, N, A> a) noexcept {
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>;
7293 if consteval {
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];
7298 }
7299 return vector_type::load(r.data());
7300 } else {
7301 if constexpr (sizeof(T) == 1) {
7302 return vector_type::from_native(wasm_i16x8_extadd_pairwise_i8x16(a.to_native()));
7303 } else {
7304 return vector_type::from_native(wasm_i32x4_extadd_pairwise_i16x8(a.to_native()));
7305 }
7306 }
7307 }
7308
7310 template<simd_integer_element T, std::size_t N, isa<> A>
7311 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
7312 [[nodiscard]] hint_inline constexpr std::uint32_t bitmask(simd<T, N, A> a) noexcept {
7313 if consteval {
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;
7318 }
7319 return r;
7320 } else {
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());
7327 } else {
7328 return wasm_i64x2_bitmask(a.to_native());
7329 }
7330 }
7331 }
7332
7334 template<simd_integer_element T, std::size_t N, isa<> A>
7335 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
7336 [[nodiscard]] hint_inline constexpr bool any(simd<T, N, A> a) noexcept {
7337 if consteval {
7338 for (auto x : detail::wasm_lanes(a)) {
7339 if (x) {
7340 return true;
7341 }
7342 }
7343 return false;
7344 } else {
7345 return wasm_v128_any_true(a.to_native());
7346 }
7347 }
7348
7350 template<simd_integer_element T, std::size_t N, isa<> A>
7351 requires(A.has(wasm_feature::simd128)) && (sizeof(T) * N == 16)
7352 [[nodiscard]] hint_inline constexpr bool all(simd<T, N, A> a) noexcept {
7353 if consteval {
7354 for (auto x : detail::wasm_lanes(a)) {
7355 if (!x) {
7356 return false;
7357 }
7358 }
7359 return true;
7360 } else {
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());
7367 } else {
7368 return wasm_i64x2_all_true(a.to_native());
7369 }
7370 }
7371 }
7372
7373} // namespace native
7374#endif
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
Definition mask_traits.h:22
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)))
Definition common_body.h:58
simd_access
Definition common.h:282
constexpr V load_simd(U const *p, simd_memory< A, Access >={}) noexcept(noexcept(V::template load_memory< A >(p)))
Definition common_body.h:40
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)))
Definition common_body.h:96
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 imm_t< K > imm
Definition common.h:82
constexpr auto operator!(wide< T, N > const &a)
Definition wide.h:613
constexpr auto operator~(wide< T, N > const &a)
Definition wide.h:606
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.
Definition config.h:24
void operator/=(simd< T, N, A > &, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
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...
Definition isa.h:43
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.
Definition common.h:96
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.