native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
polyfill_body.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 under its native register target scope.
4// Pattern: [target-scoped includes](../../README.md#target-scoped-includes).
5namespace native {
6#if NATIVE_HOST_WASM
8 template<std::size_t N,isa<> A> requires NATIVE_ARCH_REQUIRES(A) && (A.has(polyfill) && !(A<=scalar) &&
9 N>0 && N<=64 && !::NATIVE_BACKEND_NAMESPACE::predicate_shape<N>)
10 struct predicate<N,A> : detail::polyfill_mask<predicate<N,A>,N,A> {
11 using base=detail::polyfill_mask<predicate,N,A>;
12 using base::base;
13 constexpr predicate() noexcept=default;
14 };
15#endif
16
23 template<class T,std::size_t N,isa<> A> requires NATIVE_ARCH_REQUIRES(A) && detail::polyfill_shape<T,N,A>
24 struct simd<T,N,A> : detail::swizzle_access<T,N,A> {
25 using value_type=T;
26 using unsigned_register_tag=void;
27 static constexpr isa<> architecture=A;
28 static constexpr std::size_t lanes=N;
30 static constexpr std::size_t register_lanes=detail::polyfill_register_lanes<T,A>();
32 static constexpr std::size_t register_count=(N+register_lanes-1)/register_lanes;
33 using register_type=simd;
34 using chunk_type=typename detail::polyfill_chunk<T,register_lanes,detail::hardware_isa<A>>::type;
36 using native_type=std::array<typename chunk_type::native_type,register_count>;
37 using mask=detail::polyfill_predicate<N,A>;
38 using mask_type=mask;
39 using predicate_type=mask;
40 using word_type=std::conditional_t<sizeof(T)==1,std::uint8_t,
41 std::conditional_t<sizeof(T)==2,std::uint16_t,
42 std::conditional_t<sizeof(T)==4,std::uint32_t,std::uint64_t>>>;
43 using bits_type=simd<word_type,N,A>;
44 using vector_mask_type=simd<mask_lane<word_type>,N,A>;
45 template<class U> using rebind=simd<U,N,A>;
46 private:
47 alignas(typename chunk_type::native_type) native_type value_{};
48 static constexpr bool half_representation=detail::polyfill_element_traits<T>::kind==detail::polyfill_element_kind::binary16 ||
49 detail::polyfill_element_traits<T>::kind==detail::polyfill_element_kind::bfloat16;
50#if NATIVE_HOST_WASM
51 static constexpr bool permitted_native_byte=A.has(wasm_feature::simd128) && sizeof(T)==1 && register_lanes==16;
52#else
53 static constexpr bool permitted_native_byte=false;
54#endif
55 template<class F,class... V>
56 static constexpr simd map(F operation,V const &... values) noexcept {
57 simd result;
58 for(std::size_t i=0;i<register_count;++i)
59 result.value_[i]=operation(chunk_type::from_native(values.value_[i])...).to_native();
60 return result;
61 }
62 template<class F,class... V>
63 static constexpr simd map_permitted_native(F operation,V const &... values) noexcept {
64 using C=simd<T,register_lanes,A>;
65 simd result;
66 for(std::size_t i=0;i<register_count;++i)
67 result.value_[i]=operation(C::from_native(values.value_[i])...).to_native();
68 return result;
69 }
70 using scalar_chunk_type=typename detail::polyfill_chunk<T,1,detail::hardware_isa<A>>::type;
71 template<class F,class... V>
72 static constexpr simd map_scalar(F operation,V const &... values) noexcept {
73 auto input=std::tuple{[&] { std::array<T,N> lanes{}; values.store(lanes.data()); return lanes; }()...};
74 std::array<T,N> output{};
75 for(std::size_t i=0;i<N;++i) std::apply([&](auto const &... lanes) {
76 operation(scalar_chunk_type::load(lanes.data()+i)...).store(output.data()+i);
77 },input);
78 return load(output.data());
79 }
80 template<class F>
81 static constexpr mask compare(F operation,simd const & a,simd const & b) noexcept {
82 std::uint64_t bits=0;
83 for(std::size_t i=0;i<register_count;++i)
84 bits|=operation(chunk_type::from_native(a.value_[i]),
85 chunk_type::from_native(b.value_[i])).to_bitset()<<(i*register_lanes);
86 return mask::from_bitset(bits);
87 }
88 public:
90 constexpr simd() noexcept=default;
92 constexpr simd(T value) noexcept requires(!simd_integer_element<T>) {
93 if consteval {} else {
94 if constexpr(half_representation) {
95 std::array<word_type,register_count*register_lanes> words{};
96 words.fill(std::bit_cast<word_type>(value));
97 std::memcpy(value_.data(),words.data(),sizeof(words)); return;
98 }
99 }
100 for(auto & part:value_) part=chunk_type(value).to_native();
101 }
102
103 template<simd_integer_element U>
104 constexpr simd(U value) noexcept requires(simd_integer_element<T>) {
105 for(auto & part:value_) part=chunk_type(value).to_native();
106 }
107
108 constexpr simd(std::array<T,N> const & values) noexcept : simd(load(values.data())) {}
110 template<class... U> requires(sizeof...(U)==N && (std::convertible_to<U,T> && ...) &&
111 (!simd_integer_element<T> || (simd_integer_element<U> && ...)))
112 constexpr simd(U... values) noexcept : simd(std::array<T,N>{T(values)...}) {}
115 static constexpr simd from_native(native_type value) noexcept {
116 simd result;
117 if constexpr(std::same_as<T,bool> || simd_mask_element<T>) {
118 for(std::size_t i=0;i<register_count;++i)
119 result.value_[i]=chunk_type::from_native(value[i]).to_native();
120 } else result.value_=value;
121 return result;
122 }
123
124 static constexpr simd unsafe_from_native(native_type value) noexcept { return from_native(value); }
126 constexpr simd(native_type value) noexcept
127 requires(!std::same_as<native_type,std::array<T,N>>) : simd(from_native(value)) {}
128
129 static constexpr simd from_float(float value) noexcept requires(std::same_as<T,float>) { return simd(value); }
131 static constexpr simd unsafe_from_float32(native_type value) noexcept
132 requires(std::same_as<T,float>) { return from_native(value); }
133
134 constexpr operator native_type() const noexcept { return to_native(); }
136 constexpr native_type to_native() const noexcept { return value_; }
138 static constexpr simd load(T const * p) noexcept { return load_partial(p,N); }
140 static constexpr simd loadu(T const * p) noexcept { return load(p); }
142 constexpr void store(T * p) const noexcept { store_partial(p,N); }
144 constexpr void storeu(T * p) const noexcept { store(p); }
146 template<std::size_t Alignment=1>
147 static constexpr simd load_memory(T const * p) noexcept { return load(p); }
149 template<std::size_t Alignment=1>
150 constexpr void store_memory(T * p) const noexcept { store(p); }
152 static constexpr simd load_partial(T const * p,std::size_t count,T fill={}) noexcept
153 hint_diagnose_if(count>N,"partial SIMD count exceeds the lane count") {
154 simd result;
155 if consteval {} else {
156 if constexpr(half_representation) {
157 // Keep half/BF16 transport in integer representations. Copying a
158 // typed __bf16 temporary can introduce a compiler conversion helper.
159 std::array<word_type,register_count*register_lanes> words{};
160 words.fill(std::bit_cast<word_type>(fill));
161 static_assert(sizeof(words)==sizeof(native_type));
162 if(count) std::memcpy(words.data(),reinterpret_cast<unsigned char const *>(p),count*sizeof(T));
163 std::memcpy(result.value_.data(),words.data(),sizeof(words));
164 return result;
165 }
166 }
167 for(std::size_t i=0;i<register_count;++i) {
168 std::array<T,register_lanes> part{};
169 part.fill(fill);
170 auto begin=i*register_lanes;
171 auto active=begin<count?std::min(register_lanes,count-begin):0;
172 if consteval {
173 for(std::size_t j=0;j<active;++j) part[j]=p[begin+j];
174 } else {
175 if(active) std::memcpy(part.data(),reinterpret_cast<unsigned char const *>(p)+begin*sizeof(T),active*sizeof(T));
176 }
177 result.value_[i]=chunk_type::template load_memory<1>(part.data()).to_native();
178 }
179 return result;
180 }
181
182 constexpr void store_partial(T * p,std::size_t count) const noexcept
183 hint_diagnose_if(count>N,"partial SIMD count exceeds the lane count") {
184 if consteval {} else {
185 if constexpr(half_representation) {
186 if(count) std::memcpy(reinterpret_cast<unsigned char *>(p),value_.data(),count*sizeof(T));
187 return;
188 }
189 }
190 for(std::size_t i=0;i<register_count && i*register_lanes<count;++i) {
191 std::array<T,register_lanes> part{};
192 chunk_type::from_native(value_[i]).template store_memory<1>(part.data());
193 auto begin=i*register_lanes;
194 auto active=std::min(register_lanes,count-begin);
195 if consteval {
196 for(std::size_t j=0;j<active;++j) p[begin+j]=part[j];
197 } else {
198 std::memcpy(reinterpret_cast<unsigned char *>(p)+begin*sizeof(T),part.data(),active*sizeof(T));
199 }
200 }
201 }
202
203 constexpr bits_type bits() const noexcept requires(!std::same_as<T,bool> && !simd_mask_element<T>) {
204 if consteval {} else {
205 if constexpr(half_representation) {
206 std::array<word_type,N> words{}; std::memcpy(words.data(),value_.data(),sizeof(words));
207 return bits_type::template load_memory<1>(words.data());
208 }
209 }
210 std::array<T,N> values{}; store(values.data());
211 std::array<word_type,N> words{};
212 for(std::size_t i=0;i<N;++i) words[i]=std::bit_cast<word_type>(values[i]);
213 return bits_type::template load_memory<1>(words.data());
214 }
215
216 constexpr bits_type to_bits() const noexcept requires(!std::same_as<T,bool> && !simd_mask_element<T>) { return bits(); }
218 static constexpr simd from_bits(bits_type bits) noexcept requires(!std::same_as<T,bool> && !simd_mask_element<T>) {
219 std::array<word_type,N> words{}; bits.template store_memory<1>(words.data());
220 return load_bits(words.data());
221 }
222
223 static constexpr simd load_bits(word_type const * p) noexcept requires(!std::same_as<T,bool> && !simd_mask_element<T>) {
224 if consteval {} else {
225 if constexpr(half_representation) {
226 simd result; std::memcpy(result.value_.data(),reinterpret_cast<unsigned char const *>(p),N*sizeof(T)); return result;
227 }
228 }
229 std::array<word_type,N> words{};
230 if consteval {
231 for(std::size_t i=0;i<N;++i) words[i]=p[i];
232 } else {
233 std::memcpy(words.data(),reinterpret_cast<unsigned char const *>(p),N*sizeof(word_type));
234 }
235 auto values=std::bit_cast<std::array<T,N>>(words);
236 return load(values.data());
237 }
238
239 constexpr void store_bits(word_type * p) const noexcept requires(!std::same_as<T,bool> && !simd_mask_element<T>) {
240 bits().template store_memory<1>(p);
241 }
242
243 static constexpr simd load_bits_partial(word_type const * p,std::size_t count,word_type fill=0) noexcept
244 requires(!std::same_as<T,bool> && !simd_mask_element<T>)
245 hint_diagnose_if(count>N,"partial SIMD count exceeds the lane count") {
246 std::array<word_type,N> words{}; words.fill(fill);
247 if consteval {
248 for(std::size_t i=0;i<count;++i) words[i]=p[i];
249 } else {
250 if(count) std::memcpy(words.data(),reinterpret_cast<unsigned char const *>(p),count*sizeof(word_type));
251 }
252 return load_bits(words.data());
253 }
254
255 constexpr void store_bits_partial(word_type * p,std::size_t count) const noexcept
256 requires(!std::same_as<T,bool> && !simd_mask_element<T>)
257 hint_diagnose_if(count>N,"partial SIMD count exceeds the lane count") {
258 std::array<word_type,N> words{}; store_bits(words.data());
259 if consteval {
260 for(std::size_t i=0;i<count;++i) p[i]=words[i];
261 } else {
262 if(count) std::memcpy(reinterpret_cast<unsigned char *>(p),words.data(),count*sizeof(word_type));
263 }
264 }
265
266 static constexpr simd from_bitset(std::uint64_t bits) noexcept requires(std::same_as<T,bool> || simd_mask_element<T>) {
267 if constexpr(std::same_as<T,bool>) {
268 std::array<bool,N> values{};
269 for(std::size_t i=0;i<N;++i) values[i]=((bits>>i)&1)!=0;
270 return load(values.data());
271 } else {
272 simd result;
273 for(std::size_t i=0;i<register_count;++i)
274 result.value_[i]=chunk_type::from_bitset(bits>>(i*register_lanes)).to_native();
275 return result;
276 }
277 }
278
279 constexpr std::uint64_t to_bitset() const noexcept requires(std::same_as<T,bool> || simd_mask_element<T>) {
280 std::uint64_t bits=0;
281 if constexpr(std::same_as<T,bool>) {
282 std::array<bool,N> values{}; store(values.data());
283 for(std::size_t i=0;i<N;++i) bits|=std::uint64_t(values[i])<<i;
284 } else {
285 for(std::size_t i=0;i<register_count;++i)
286 bits|=chunk_type::from_native(value_[i]).to_bitset()<<(i*register_lanes);
287 }
288 if constexpr(N<64) bits&=(std::uint64_t{1}<<N)-1;
289 return bits;
290 }
291#define NATIVE_POLYFILL_BINARY(OP) \
292 \
293 friend constexpr simd operator OP(simd a,simd b) noexcept \
294 requires(requires(chunk_type x) { { x OP x } -> std::same_as<chunk_type>; } || \
295 requires(scalar_chunk_type x) { { x OP x } -> std::same_as<scalar_chunk_type>; }) { \
296 if constexpr(requires(chunk_type x) { { x OP x } -> std::same_as<chunk_type>; }) \
297 return map([](auto x,auto y) { return x OP y; },a,b); \
298 else if constexpr(permitted_native_byte) \
299 return map_permitted_native([](auto x,auto y) { return x OP y; },a,b); \
300 else return map_scalar([](auto x,auto y) { return x OP y; },a,b); \
301 } \
302 \
303 constexpr simd & operator OP##=(simd b) noexcept \
304 requires requires(simd x) { { x OP x } -> std::same_as<simd>; } { return *this=*this OP b; }
305 NATIVE_POLYFILL_BINARY(+)
306 NATIVE_POLYFILL_BINARY(-)
307 NATIVE_POLYFILL_BINARY(*)
308 NATIVE_POLYFILL_BINARY(/)
309 NATIVE_POLYFILL_BINARY(%)
310 NATIVE_POLYFILL_BINARY(&)
311 NATIVE_POLYFILL_BINARY(|)
312 NATIVE_POLYFILL_BINARY(^)
313#undef NATIVE_POLYFILL_BINARY
314#define NATIVE_POLYFILL_COMPARE(OP) \
315 \
316 friend constexpr mask operator OP(simd a,simd b) noexcept \
317 requires requires(chunk_type x) { (x OP x).to_bitset(); } { \
318 return compare([](auto x,auto y) { return x OP y; },a,b); \
319 }
320 NATIVE_POLYFILL_COMPARE(==)
321 NATIVE_POLYFILL_COMPARE(<)
322#undef NATIVE_POLYFILL_COMPARE
324 friend constexpr mask operator!=(simd a,simd b) noexcept
325 requires requires(chunk_type x) { (x==x).to_bitset(); } { return ~(a==b); }
326
327 friend constexpr mask operator>(simd a,simd b) noexcept
328 requires requires(chunk_type x) { (x<x).to_bitset(); } { return b<a; }
329
330 friend constexpr mask operator<=(simd a,simd b) noexcept
331 requires requires(chunk_type x) { (x<x).to_bitset(); (x==x).to_bitset(); } { return (a<b)|(a==b); }
332
333 friend constexpr mask operator>=(simd a,simd b) noexcept
334 requires requires(chunk_type x) { (x<x).to_bitset(); (x==x).to_bitset(); } { return b<=a; }
335
336 friend constexpr simd operator-(simd value) noexcept
337 requires requires(chunk_type x) { { -x } -> std::same_as<chunk_type>; } {
338 return map([](auto x) { return -x; },value);
339 }
340
341 friend constexpr simd operator~(simd value) noexcept
342 requires requires(chunk_type x) { { ~x } -> std::same_as<chunk_type>; } {
343 return map([](auto x) { return ~x; },value);
344 }
345
346 friend constexpr simd operator!(simd value) noexcept
347 requires(std::same_as<T,bool> || simd_mask_element<T>) { return from_bitset(~value.to_bitset()); }
348
349 friend constexpr bool any(simd value) noexcept
350 requires(std::same_as<T,bool> || simd_mask_element<T>) { return value.to_bitset()!=0; }
351
352 friend constexpr bool none(simd value) noexcept
353 requires(std::same_as<T,bool> || simd_mask_element<T>) { return !any(value); }
354
355 friend constexpr bool all(simd value) noexcept
356 requires(std::same_as<T,bool> || simd_mask_element<T>) {
357 if constexpr(N==64) return value.to_bitset()==~std::uint64_t{};
358 else return value.to_bitset()==(std::uint64_t{1}<<N)-1;
359 }
360
361 template<class M> requires(std::same_as<M,mask_type> || std::same_as<M,predicate<N,A>> || std::same_as<M,vector_mask_type>)
362 friend constexpr simd select(M mask,simd a,simd b) noexcept {
363 if constexpr(half_representation) {
364 std::array<word_type,N> first{},second{}; a.store_bits(first.data()); b.store_bits(second.data());
365 auto bits=mask.to_bitset();
366 for(std::size_t i=0;i<N;++i) if(!((bits>>i)&1)) first[i]=second[i];
367 return load_bits(first.data());
368 }
369 std::array<T,N> first{},second{}; a.store(first.data()); b.store(second.data());
370 auto bits=mask.to_bitset();
371 for(std::size_t i=0;i<N;++i) if(!((bits>>i)&1)) first[i]=second[i];
372 return load(first.data());
373 }
374
375 template<std::size_t I> requires(I<N)
376 constexpr T get() const noexcept {
377 if constexpr(half_representation) { std::array<word_type,N> words{}; store_bits(words.data()); return T::from_bits(words[I]); }
378 else { std::array<T,N> lanes{}; store(lanes.data()); return lanes[I]; }
379 }
380
381 template<std::size_t I> requires(I<N)
382 constexpr simd set(T value) const noexcept {
383 if constexpr(half_representation) {
384 std::array<word_type,N> words{}; store_bits(words.data()); words[I]=value.to_bits(); return load_bits(words.data());
385 } else { std::array<T,N> lanes{}; store(lanes.data()); lanes[I]=value; return load(lanes.data()); }
386 }
387
388 template<std::size_t I> requires(I<N)
389 constexpr simd replace(T value) const noexcept { return this->template set<I>(value); }
391 friend constexpr simd normal_pow2(simd n) noexcept requires(std::same_as<T,float>) {
392 if constexpr(requires(chunk_type x) { normal_pow2(x); }) return map([](auto x) { return normal_pow2(x); },n);
393 else return map_scalar([](auto x) { return normal_pow2(x); },n);
394 }
395
396 template<std::size_t K> requires(simd_integer_element<T> && ((NATIVE_HOST_WASM!=0) || K<sizeof(T)*8))
397 friend constexpr simd operator<<(simd value,imm_t<K>) noexcept { return value.template left<K>(); }
399 template<std::size_t K> requires(simd_integer_element<T> && ((NATIVE_HOST_WASM!=0) || K<sizeof(T)*8))
400 friend constexpr simd operator>>(simd value,imm_t<K>) noexcept { return value.template right<K>(); }
402 friend constexpr simd operator<<(simd value,simd counts) noexcept requires(std::same_as<T,std::uint32_t>) {
403 if constexpr(requires(chunk_type x) { { x<<x } -> std::same_as<chunk_type>; })
404 return map([](auto x,auto n) {
405 auto valid=n<chunk_type(32u);
406 return select(valid,x<<select(valid,n,chunk_type(0u)),chunk_type(0u));
407 },value,counts);
408 else {
409 std::array<T,N> lanes{},shifts{}; value.store(lanes.data()); counts.store(shifts.data());
410 for(std::size_t i=0;i<N;++i) lanes[i]=shifts[i]<32?lanes[i]<<shifts[i]:0;
411 return load(lanes.data());
412 }
413 }
414
415 template<std::size_t K>
416 constexpr simd right() const noexcept requires(simd_integer_element<T> &&
417 ((NATIVE_HOST_WASM!=0) || requires(chunk_type x) { x.template right<K>(); })) {
418 if constexpr(requires(chunk_type x) { x.template right<K>(); }) return map([](auto x) { return x.template right<K>(); },*this);
419 else {
420 std::array<T,N> lanes{}; store(lanes.data());
421 for(auto & lane:lanes) lane=T(lane>>(K%(sizeof(T)*8)));
422 return load(lanes.data());
423 }
424 }
425
426 template<std::size_t K>
427 constexpr simd left() const noexcept requires(simd_integer_element<T> &&
428 ((NATIVE_HOST_WASM!=0) || requires(chunk_type x) { x.template left<K>(); })) {
429 if constexpr(requires(chunk_type x) { x.template left<K>(); }) return map([](auto x) { return x.template left<K>(); },*this);
430 else {
431 std::array<T,N> lanes{}; store(lanes.data()); using U=std::make_unsigned_t<T>;
432 for(auto & lane:lanes) lane=std::bit_cast<T>(U(std::uint64_t(U(lane))<<(K%(sizeof(T)*8))));
433 return load(lanes.data());
434 }
435 }
436
437 friend constexpr simd operator>>(simd value,int count) noexcept
438 requires(simd_integer_element<T> && ((NATIVE_HOST_WASM!=0) || requires(chunk_type x) { { x>>count } -> std::same_as<chunk_type>; })) {
439 if constexpr(requires(chunk_type x) { { x>>count } -> std::same_as<chunk_type>; }) return map([=](auto x) { return x>>count; },value);
440 else {
441 std::array<T,N> lanes{}; value.store(lanes.data());
442 for(auto & lane:lanes) lane=T(lane>>(unsigned(count)%(sizeof(T)*8)));
443 return load(lanes.data());
444 }
445 }
446
447 friend constexpr simd operator<<(simd value,int count) noexcept
448 requires(simd_integer_element<T> && ((NATIVE_HOST_WASM!=0) || requires(chunk_type x) { { x<<count } -> std::same_as<chunk_type>; })) {
449 if constexpr(requires(chunk_type x) { { x<<count } -> std::same_as<chunk_type>; }) return map([=](auto x) { return x<<count; },value);
450 else {
451 std::array<T,N> lanes{}; value.store(lanes.data()); using U=std::make_unsigned_t<T>;
452 for(auto & lane:lanes) lane=std::bit_cast<T>(U(std::uint64_t(U(lane))<<(unsigned(count)%(sizeof(T)*8))));
453 return load(lanes.data());
454 }
455 }
456
457 template<class C> requires requires(simd value,C count) { { value<<count } -> std::same_as<simd>; }
458 constexpr simd & operator<<=(C count) noexcept { return *this=*this<<count; }
460 template<class C> requires requires(simd value,C count) { { value>>count } -> std::same_as<simd>; }
461 constexpr simd & operator>>=(C count) noexcept { return *this=*this>>count; }
463 friend constexpr simd fma(simd a,simd b,simd c) noexcept
464 requires(detail::polyfill_element_traits<T>::arithmetic) {
465 if constexpr(requires(chunk_type x) { fma(x,x,x); }) return map([](auto x,auto y,auto z) { return fma(x,y,z); },a,b,c);
466 else return map_scalar([](auto x,auto y,auto z) { return fma(x,y,z); },a,b,c);
467 }
468
469 friend constexpr simd sqrt(simd value) noexcept
470 requires(detail::polyfill_element_traits<T>::arithmetic) {
471 if constexpr(requires(chunk_type x) { sqrt(x); }) return map([](auto x) { return sqrt(x); },value);
472 else return map_scalar([](auto x) { return sqrt(x); },value);
473 }
474
475 friend constexpr simd popcount(simd value) noexcept
476 requires(simd_integer_element<T> && std::is_unsigned_v<T>) {
477 return map([](auto x) { return popcount(x); },value);
478 }
479
480 friend constexpr simd abs(simd value) noexcept
481 requires(detail::polyfill_element_traits<T>::arithmetic) {
482 if constexpr(requires(chunk_type x) { abs(x); }) return map([](auto x) { return abs(x); },value);
483 else return map_scalar([](auto x) { return abs(x); },value);
484 }
485
486 friend constexpr simd floor(simd value) noexcept
487 requires(detail::polyfill_element_traits<T>::arithmetic) {
488 if constexpr(requires(chunk_type x) { floor(x); }) return map([](auto x) { return floor(x); },value);
489 else return map_scalar([](auto x) { return floor(x); },value);
490 }
491
492 friend constexpr simd ceil(simd value) noexcept
493 requires(detail::polyfill_element_traits<T>::arithmetic) {
494 if constexpr(requires(chunk_type x) { ceil(x); }) return map([](auto x) { return ceil(x); },value);
495 else return map_scalar([](auto x) { return ceil(x); },value);
496 }
497
498 friend constexpr simd trunc(simd value) noexcept
499 requires(detail::polyfill_element_traits<T>::arithmetic) {
500 if constexpr(requires(chunk_type x) { trunc(x); }) return map([](auto x) { return trunc(x); },value);
501 else return map_scalar([](auto x) { return trunc(x); },value);
502 }
503
504 friend constexpr simd min(simd a,simd b) noexcept requires(detail::polyfill_element_traits<T>::arithmetic &&
505 !((NATIVE_HOST_WASM!=0) && std::floating_point<T>)) { return select(a<b,a,b); }
506
507 friend constexpr simd max(simd a,simd b) noexcept requires(detail::polyfill_element_traits<T>::arithmetic &&
508 !((NATIVE_HOST_WASM!=0) && std::floating_point<T>)) { return select(a>b,a,b); }
509
510 friend constexpr simd round_even(simd value) noexcept
511 requires(detail::polyfill_element_traits<T>::arithmetic) {
512 if constexpr(requires(chunk_type x) { round_even(x); }) return map([](auto x) { return round_even(x); },value);
513 else return map_scalar([](auto x) { return round_even(x); },value);
514 }
515 };
516}
517
518namespace native {
521 template<simd_integer_element To,simd_integer_element From,std::size_t N,isa<> A>
522 requires NATIVE_ARCH_REQUIRES(A) && (A.has(polyfill) && sizeof(From)*N%sizeof(To)==0 &&
523 requires { sizeof(simd<From,N,A>); sizeof(simd<To,sizeof(From)*N/sizeof(To),A>); } &&
524 (detail::polyfill_shape<From,N,A> || detail::polyfill_shape<To,sizeof(From)*N/sizeof(To),A> ||
525 sizeof(typename simd<From,N,A>::native_type)!=sizeof(typename simd<To,sizeof(From)*N/sizeof(To),A>::native_type)))
526 constexpr auto reinterpret_bits(simd<From,N,A> value) noexcept {
527 std::array<From,N> input{}; value.store(input.data());
528 auto output=std::bit_cast<std::array<To,sizeof(From)*N/sizeof(To)>>(input);
529 return simd<To,sizeof(From)*N/sizeof(To),A>::template load_memory<1>(output.data());
530 }
531
533 template<simd_integer_element T,std::size_t N,isa<> A>
534 requires NATIVE_ARCH_REQUIRES(A) && (detail::polyfill_operation_shape<T,N,A> && std::is_unsigned_v<T> && sizeof(T)<=4 && N%2==0)
535 constexpr auto pairwise_add_widened(simd<T,N,A> value) noexcept {
536 using U=std::conditional_t<sizeof(T)==1,std::uint16_t,
537 std::conditional_t<sizeof(T)==2,std::uint32_t,std::uint64_t>>;
538 std::array<T,N> input{}; value.store(input.data());
539 std::array<U,N/2> output{};
540 for(std::size_t i=0;i<N/2;++i) output[i]=U(input[2*i])+input[2*i+1];
541 return simd<U,N/2,A>::template load_memory<1>(output.data());
542 }
543
545 template<simd_integer_element To,simd_integer_element From,std::size_t N,isa<> A>
546 requires NATIVE_ARCH_REQUIRES(A) && (A.has(polyfill) && std::is_unsigned_v<To> && std::is_unsigned_v<From> &&
547 sizeof(From)==2*sizeof(To) &&
548 (detail::polyfill_operation_shape<From,N,A> || detail::polyfill_operation_shape<To,2*N,A>) &&
549 // F/DQ already narrows dwords into storage-only word registers without BW.
550 !(NATIVE_HAS_AVX512F != 0 && N*sizeof(From)==64 && sizeof(From)>=4 &&
551 !detail::polyfill_shape<From,N,A> && !detail::polyfill_shape<To,2*N,A>) &&
552 requires { sizeof(simd<To,2*N,A>); })
554 std::array<From,N> first{},second{}; a.store(first.data()); b.store(second.data());
555 std::array<To,2*N> output{};
556 for(std::size_t i=0;i<N;++i) {
557 output[i]=static_cast<To>(first[i]);
558 output[N+i]=static_cast<To>(second[i]);
559 }
560 return simd<To,2*N,A>::template load_memory<1>(output.data());
561 }
562}
std::string_view const type
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
architecture
Instruction-set families; an ISA value belongs to exactly one family.
Definition config.h:24
constexpr simd< To, 2 *N, Arch > narrow_concat(simd< From, N, Arch > a, simd< From, N, Arch > b) noexcept
constexpr auto pairwise_add_widened(simd< T, N, Arch > value) noexcept
constexpr auto reinterpret_bits(simd< From, N, Arch > value) noexcept -> simd< To, sizeof(From) *N/sizeof(To), Arch >
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
Standard-library adaptations documented here for SIMD value types.
static constexpr simd load_bits_partial(word_type const *p, std::size_t count, word_type fill=0) noexcept
Read count representation words, filling remaining lanes; zero permits null.
friend constexpr simd operator>>(simd value, int count) noexcept
Shift every integral lane right by a runtime count.
constexpr simd(U value) noexcept
Reduce an integral scalar to the lane width and broadcast it.
static constexpr simd load(T const *p) noexcept
Read exactly N elements with their natural alignment.
constexpr void store_bits(word_type *p) const noexcept
Write exact lane representations as unsigned words.
friend constexpr simd trunc(simd value) noexcept
Round floating-point lanes toward zero.
static constexpr simd load_bits(word_type const *p) noexcept
Read N unsigned lane representations without numerical conversion.
friend constexpr simd floor(simd value) noexcept
Round floating-point lanes toward negative infinity.
friend constexpr mask operator<=(simd a, simd b) noexcept
Compare corresponding lanes for ordered less-than or equality.
friend constexpr bool any(simd value) noexcept
Test whether any logical mask lane is true.
static constexpr simd unsafe_from_float32(native_type value) noexcept
Adopt raw binary32 register storage without numerical conversion.
friend constexpr simd operator~(simd value) noexcept
Complement every logical lane representation.
constexpr simd set(T value) const noexcept
Replace one logical lane, retaining every other lane's representation.
constexpr void store_memory(T *p) const noexcept
Store logical lanes; Alignment is the caller's byte-alignment promise.
static constexpr simd load_bits(word_type const *p) noexcept
Read exact lane representations from unsigned words.
constexpr void store_bits_partial(word_type *p, std::size_t count) const noexcept
Store count exact lane words; zero permits null and touches nothing.
constexpr bits_type bits() const noexcept
Return exact unsigned lane representations without normalization.
friend constexpr simd round_even(simd value) noexcept
Round to nearest integral values, choosing even at ties.
static constexpr std::size_t register_lanes
Number of logical lanes in each underlying native register.
friend constexpr simd fma(simd a, simd b, simd c) noexcept
Compute a*b+c with each register's fused lane semantics.
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 T get() const noexcept
Read one logical lane selected at compile time.
friend constexpr simd popcount(simd value) noexcept
Count the set bits of each unsigned lane using the native chunk operation.
constexpr bits_type to_bits() const noexcept
Return exact unsigned lane representations.
friend constexpr simd max(simd a, simd b) noexcept
Select the larger lane using the underlying floating-point comparison.
constexpr simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
static constexpr std::size_t register_count
Number of underlying registers, including a possible partial tail.
friend constexpr simd sqrt(simd value) noexcept
Compute square roots with each register's floating-point semantics.
friend constexpr simd abs(simd value) noexcept
Clear the sign bit of each floating-point lane.
constexpr simd & operator>>=(C count) noexcept
Apply the corresponding supported shift in place.
friend constexpr simd min(simd a, simd b) noexcept
Select the smaller lane using the underlying floating-point comparison.
constexpr native_type to_native() const noexcept
Return the underlying register representations in logical order.
constexpr void store_partial(T *p, std::size_t n) const noexcept
Write the first n <= N elements; a null pointer is valid for n == 0.
constexpr simd(native_type value) noexcept
Adopt the underlying register array without numerical conversion.
static constexpr simd unsafe_from_native(native_type value) noexcept
Adopt canonical representations; callers supply canonical mask/Boolean lanes.
friend constexpr bool all(simd value) noexcept
Test whether every logical mask lane is true.
constexpr simd & operator<<=(C count) noexcept
Apply the corresponding supported shift in place.
friend constexpr bool none(simd value) noexcept
Test whether every logical mask lane is false.
static constexpr simd from_bitset(std::uint64_t bits) noexcept
Construct canonical mask lanes from a logical bitset.
constexpr native_type to_native() const noexcept
Bridge to the implementation register without numerical conversion.
constexpr std::uint64_t to_bitset() const noexcept
Return logical mask lanes, excluding tail padding.
static constexpr simd loadu(T const *p) noexcept
Read all logical lanes without an alignment promise.
friend constexpr simd normal_pow2(simd n) noexcept
Encode normal powers of two for integral exponents in [-126,127].
constexpr simd(std::array< T, N > const &values) noexcept
Copy logical lanes in array order.
friend constexpr mask operator>(simd a, simd b) noexcept
Compare corresponding lanes with the reverse ordered less-than relation.
static constexpr simd from_native(native_type value) noexcept
friend constexpr simd operator-(simd value) noexcept
Negate every logical lane with the underlying register's semantics.
constexpr void store_partial(T *p, std::size_t count) const noexcept
Write exactly count lanes. Zero count permits null and touches nothing.
friend constexpr mask operator!=(simd a, simd b) noexcept
Compare corresponding lanes for inequality; unordered floats are unequal.
static constexpr simd from_native(native_type value) noexcept
Adopt register bits unchanged; unused physical bytes are unspecified.
friend constexpr simd ceil(simd value) noexcept
Round floating-point lanes toward positive infinity.
static constexpr simd load_partial(T const *p, std::size_t count, T fill={}) noexcept
Read exactly count lanes and fill the remainder. Zero count permits null.
static constexpr simd load_memory(T const *p) noexcept
Load logical lanes; Alignment is the caller's byte-alignment promise.
friend constexpr simd operator<<(simd value, simd counts) noexcept
Shift each unsigned 32-bit lane by its corresponding count; counts >=32 yield zero.
constexpr void store(T *p) const noexcept
Write all logical lanes to an unaligned element pointer.
friend constexpr simd select(M mask, simd a, simd b) noexcept
Choose each logical lane from a or b without reading padding as a result.
friend constexpr simd operator<<(simd value, int count) noexcept
Shift every integral lane left by a runtime count.
friend constexpr mask operator>=(simd a, simd b) noexcept
Compare corresponding lanes for ordered greater-than or equality.
static constexpr simd load_partial(T const *p, std::size_t n, T fill=T{}) noexcept
Read n <= N elements and fill the remainder; a null pointer is valid for n == 0.
friend constexpr simd operator!(simd value) noexcept
Complement every logical mask lane.
constexpr void store_bits(word_type *p) const noexcept
Write N unsigned lane representations without numerical conversion.
constexpr simd replace(T value) const noexcept
Synonym for replacing one compile-time-selected logical lane.
constexpr void storeu(T *p) const noexcept
Write all logical lanes without an alignment promise.
static constexpr simd from_float(float value) noexcept
Broadcast float lanes without imposing an element normalization policy.
static constexpr simd from_bits(bits_type bits) noexcept
Adopt unsigned lane representations without conversion.
constexpr simd left() const noexcept
Shift every integral lane left by the immediate count.
Omitted architecture arguments use the native.simd provider's baseline.