native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.simd.ccm
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3module;
4#include "native/isa_import.h"
5#include <native/simd.h>
6#include <native/integer.h>
7#include <native/packing.h>
8#include <native/simd/math/bits.h>
9#include <native/targets.h>
10export module native.simd;
11export import native.numerics;
12export import native.wide;
13export import native.static_string;
14export import native.types;
15export import native.memory;
16// Defaults use the owning module provider's baseline.
17extern "C++" namespace native {
19 template<class T, std::size_t N, isa<> Arch = NATIVE_BASELINE> struct simd;
21// Keep the GMF partial specializations reachable to module consumers.
22export namespace native { using ::native::mask_traits; }
23#include "native/simd/exports.h"
24export namespace native::detail {
25 template<> struct polyfill_element_traits<fp16> {
26 static constexpr bool supported=true;
27 static constexpr bool arithmetic=true;
28 static constexpr auto kind=polyfill_element_kind::binary16;
29 using format=constexpr_float::binary16;
30 };
31 template<> struct polyfill_element_traits<bf16> {
32 static constexpr bool supported=true;
33 static constexpr bool arithmetic=false;
34 static constexpr auto kind=polyfill_element_kind::bfloat16;
35 using format=constexpr_float::binary_format<8,7>;
36 };
38#if NATIVE_HOST_X86
39#define NATIVE_ARCH_REQUIRES(A) (::native::avx512_bf16 <= A)
40#pragma clang attribute push(__attribute__((target("avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512bf16"))), apply_to=function)
41export namespace native {
42 namespace detail {
43 template<std::size_t N,::native::isa<> A> requires NATIVE_ARCH_REQUIRES(A) &&(N==8 || N==16 || N==32)
44 struct value_traits<simd<bf16,N,A>> {
45 static constexpr isa<> value=avx512_bf16;
46 static constexpr bool known=true;
47 static constexpr bool aggregate_default=false;
48 };
49 }
57 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) &&(N == 8 || N == 16 || N == 32)
62 static constexpr isa<> architecture=Arch;
66 using native_type = std::conditional_t<N == 8,__m128bh,std::conditional_t<N == 16,__m256bh,__m512bh>>;
72 using mask_type = mask;
79 template<class T> using rebind = simd<T,N,architecture>;
81 static constexpr std::size_t lanes = N;
82#include "native/simd/polyfill_lane_access.h"
83 private:
84 native_type value_;
85 public:
87 simd() noexcept = default;
89 hint_inline constexpr explicit simd(bf16 value) noexcept
90 : value_(__builtin_bit_cast(native_type,bits_type(value.to_bits()).to_native())) {}
91
92 hint_inline constexpr explicit simd(std::array<bf16,lanes> const & values) noexcept : simd(load(values.data())) {}
94 template<class... T> requires(sizeof...(T) == lanes && (std::same_as<T,bf16> && ...))
95 hint_inline constexpr simd(T... values) noexcept : simd(std::array<bf16,lanes>{values...}) {}
97 hint_inline constexpr simd(native_type value) noexcept : value_(value) {}
99 [[nodiscard]] hint_inline constexpr operator native_type() const noexcept { return value_; }
100 // Native interoperability must not add elementwise BF16 arithmetic.
101#include "native/simd/bf16_reject_operators.h"
103 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept { return value_; }
105 [[nodiscard]] static hint_inline constexpr simd from_native(native_type value) noexcept {
106 simd result; result.value_ = value; return result;
107 }
108
109 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept {
110 return bits_type::from_native(__builtin_bit_cast(typename bits_type::native_type,value_));
111 }
112
113 [[nodiscard]] hint_inline constexpr bits_type to_bits() const noexcept { return bits(); }
115 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type value) noexcept {
116 return from_native(__builtin_bit_cast(native_type,value.to_native()));
117 }
118
120 [[nodiscard]] static hint_inline constexpr simd load_bits(std::uint16_t const * p) noexcept {
121 return from_bits(bits_type::load(p));
122 }
123
125 hint_inline constexpr void store_bits(std::uint16_t * p) const noexcept { bits().store(p); }
130 template<std::size_t Alignment = 1>
131 [[nodiscard]] static hint_inline constexpr simd load_memory(bf16 const * p) noexcept {
132 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
133 if consteval {
134 std::array<std::uint16_t,lanes> words{};
135 for (std::size_t i=0;i<lanes;++i) words[i]=p[i].to_bits();
136 return load_bits(words.data());
137 }
138 native_type value; std::memcpy(&value, reinterpret_cast<unsigned char const *>(p), sizeof(value)); return from_native(value);
139 }
140
144 template<std::size_t Alignment = 1>
145 hint_inline constexpr void store_memory(bf16 * p) const noexcept {
146 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
147 if consteval {
148 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
149 for (std::size_t i=0;i<lanes;++i) p[i]=bf16::from_bits(words[i]);
150 return;
151 }
152 std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, sizeof(value_));
153 }
154
155 [[nodiscard]] static hint_inline constexpr simd load(bf16 const * p) noexcept { return load_memory(p); }
157 hint_inline constexpr void store(bf16 * p) const noexcept { store_memory(p); }
159 [[nodiscard]] static hint_inline constexpr simd loadu(bf16 const * p) noexcept { return load(p); }
161 hint_inline constexpr void storeu(bf16 * p) const noexcept { store(p); }
166 [[nodiscard]] static hint_inline constexpr simd load_partial(bf16 const * p, std::size_t n,
167 bf16 fill = bf16::from_bits(0)) noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
168 assert(n <= lanes);
169 std::array<bf16,lanes> values; values.fill(fill);
170 if consteval {
171 for (std::size_t i=0;i<n;++i) values[i]=p[i];
172 } else { if (n) std::memcpy(values.data(), reinterpret_cast<unsigned char const *>(p), n * sizeof(bf16)); }
173 return load(values.data());
174 }
175
178 hint_inline constexpr void store_partial(bf16 * p, std::size_t n) const noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
179 assert(n <= lanes);
180 if consteval {
181 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
182 for (std::size_t i=0;i<n;++i) p[i]=bf16::from_bits(words[i]);
183 } else { if (n) std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, n * sizeof(bf16)); }
184 }
185 };
186
193 template<std::size_t N, ::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) &&(N == 8 || N == 16 || N == 32)
194 [[nodiscard]] hint_inline constexpr simd<float,N/2,Arch> dot2(
196 simd<float,N/2,Arch> accumulator) noexcept {
197 if consteval { return detail::half_constant::dot2<false>(a,b,accumulator); }
198 return simd<float,N/2,Arch>::from_native(
199 detail::avx512_bf16_backend::dot2_native(a.to_native(), b.to_native(), accumulator.to_native()));
200 }
201}
202
203#pragma clang attribute pop
204#undef NATIVE_ARCH_REQUIRES
205#define NATIVE_ARCH_REQUIRES(A) (::native::avx512_fp16 <= A)
206#pragma clang attribute push(__attribute__((target("avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512fp16"))), apply_to=function)
207export namespace native {
208 namespace detail {
209 template<::native::isa<> A> requires NATIVE_ARCH_REQUIRES(A)
210 struct value_traits<simd<fp16,32,A>> {
211 static constexpr isa<> value=avx512_fp16;
212 static constexpr bool known=true;
213 static constexpr bool aggregate_default=false;
214 };
215 }
228 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) struct simd<fp16,32,Arch> {
232 static constexpr isa<> architecture=Arch;
236 using native_type = __m512h;
249 template<class T> using rebind = simd<T,32,architecture>;
251 static constexpr std::size_t lanes = 32;
252#include "native/simd/polyfill_lane_access.h"
253 private:
254 native_type value_;
255 public:
257 simd() noexcept = default;
259 hint_inline constexpr explicit simd(fp16 value) noexcept
260 : value_(__builtin_bit_cast(native_type,bits_type(value.to_bits()).to_native())) {}
261
262 hint_inline constexpr explicit simd(std::array<fp16,lanes> const & values) noexcept : simd(load(values.data())) {}
264 template<class... T> requires(sizeof...(T) == lanes && (std::same_as<T,fp16> && ...))
265 hint_inline constexpr simd(T... values) noexcept : simd(std::array<fp16,lanes>{values...}) {}
267 hint_inline constexpr simd(native_type value) noexcept : value_(value) {}
269 [[nodiscard]] hint_inline constexpr operator native_type() const noexcept { return value_; }
271 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept { return value_; }
273 [[nodiscard]] static hint_inline constexpr simd from_native(native_type value) noexcept {
274 simd result; result.value_ = value; return result;
275 }
276
277 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept {
278 return bits_type::from_native(__builtin_bit_cast(typename bits_type::native_type,value_));
279 }
280
281 [[nodiscard]] hint_inline constexpr bits_type to_bits() const noexcept { return bits(); }
283 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type value) noexcept {
284 return from_native(__builtin_bit_cast(native_type,value.to_native()));
285 }
286
288 [[nodiscard]] static hint_inline constexpr simd load_bits(std::uint16_t const * p) noexcept {
289 return from_bits(bits_type::load(p));
290 }
291
293 hint_inline constexpr void store_bits(std::uint16_t * p) const noexcept { bits().store(p); }
298 template<std::size_t Alignment = 1>
299 [[nodiscard]] static hint_inline constexpr simd load_memory(fp16 const * p) noexcept {
300 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
301 if consteval {
302 std::array<std::uint16_t,lanes> words{};
303 for (std::size_t i=0;i<lanes;++i) words[i]=p[i].to_bits();
304 return load_bits(words.data());
305 }
306 native_type value; std::memcpy(&value, reinterpret_cast<unsigned char const *>(p), sizeof(value)); return from_native(value);
307 }
308
312 template<std::size_t Alignment = 1>
313 hint_inline constexpr void store_memory(fp16 * p) const noexcept {
314 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
315 if consteval {
316 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
317 for (std::size_t i=0;i<lanes;++i) p[i]=fp16::from_bits(words[i]);
318 return;
319 }
320 std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, sizeof(value_));
321 }
322
323 [[nodiscard]] static hint_inline constexpr simd load(fp16 const * p) noexcept { return load_memory(p); }
325 hint_inline constexpr void store(fp16 * p) const noexcept { store_memory(p); }
327 [[nodiscard]] static hint_inline constexpr simd loadu(fp16 const * p) noexcept { return load(p); }
329 hint_inline constexpr void storeu(fp16 * p) const noexcept { store(p); }
334 [[nodiscard]] static hint_inline constexpr simd load_partial(fp16 const * p, std::size_t n,
335 fp16 fill = fp16::from_bits(0)) noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
336 assert(n <= lanes);
337 std::array<fp16,lanes> values; values.fill(fill);
338 if consteval {
339 for (std::size_t i=0;i<n;++i) values[i]=p[i];
340 } else { if (n) std::memcpy(values.data(), reinterpret_cast<unsigned char const *>(p), n * sizeof(fp16)); }
341 return load(values.data());
342 }
343
346 hint_inline constexpr void store_partial(fp16 * p, std::size_t n) const noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
347 assert(n <= lanes);
348 if consteval {
349 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
350 for (std::size_t i=0;i<n;++i) p[i]=fp16::from_bits(words[i]);
351 } else { if (n) std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, n * sizeof(fp16)); }
352 }
353
354 [[nodiscard]] friend hint_inline constexpr simd operator+(simd a,simd b) noexcept {
355 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::add,false>(a,b); }
356 return from_native(detail::avx512_fp16_backend::add_half(a.value_,b.value_));
357 }
358
359 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a,simd b) noexcept {
360 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::subtract,false>(a,b); }
361 return from_native(detail::avx512_fp16_backend::sub_half(a.value_,b.value_));
362 }
363
364 [[nodiscard]] friend hint_inline constexpr simd operator*(simd a,simd b) noexcept {
365 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::multiply,false>(a,b); }
366 return from_native(detail::avx512_fp16_backend::mul_half(a.value_,b.value_));
367 }
368
370 [[nodiscard]] friend hint_inline constexpr simd operator/(simd a,simd b) noexcept {
371 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::divide,false>(a,b); }
372 return from_native(detail::avx512_fp16_backend::div_half(a.value_,b.value_));
373 }
374
377 [[nodiscard]] friend hint_inline constexpr simd sqrt(simd a) noexcept {
378 if consteval { return detail::half_constant::square_root<false>(a); }
379 return from_native(detail::avx512_fp16_backend::sqrt_half(a.value_));
380 }
381
382 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a) noexcept {
383 if consteval { return detail::half_constant::unary(a,[](auto x) { return std::uint16_t(x^0x8000); }); }
384 return from_native(detail::avx512_fp16_backend::neg_half(a.value_));
385 }
386
388 [[nodiscard]] friend hint_inline constexpr mask operator==(simd a,simd b) noexcept {
389 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::equal_bits<detail::constexpr_float::binary16>(x,y); }); }
390 return mask::from_native(detail::avx512_fp16_backend::eq_half(a.value_,b.value_));
391 }
392
393 [[nodiscard]] friend hint_inline constexpr mask operator!=(simd a,simd b) noexcept { return ~(a==b); }
395 [[nodiscard]] friend hint_inline constexpr mask operator<(simd a,simd b) noexcept {
396 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::less_bits<detail::constexpr_float::binary16>(x,y); }); }
397 return mask::from_native(detail::avx512_fp16_backend::lt_half(a.value_,b.value_));
398 }
399
400 [[nodiscard]] friend hint_inline constexpr mask operator<=(simd a,simd b) noexcept {
401 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::less_bits<detail::constexpr_float::binary16>(x,y) || detail::constexpr_float::equal_bits<detail::constexpr_float::binary16>(x,y); }); }
402 return mask::from_native(detail::avx512_fp16_backend::le_half(a.value_,b.value_));
403 }
404
405 [[nodiscard]] friend hint_inline constexpr mask operator>(simd a,simd b) noexcept { return b<a; }
407 [[nodiscard]] friend hint_inline constexpr mask operator>=(simd a,simd b) noexcept { return b<=a; }
410 [[nodiscard]] friend hint_inline constexpr simd select(mask m,simd a,simd b) noexcept {
411 if consteval { return detail::half_constant::select(m,a,b); }
412 return from_native(detail::avx512_fp16_backend::select_half(m.to_native(),a.value_,b.value_));
413 }
414 };
415
419 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch)
420 [[nodiscard]] hint_inline constexpr simd<fp16,32,Arch> fma(
422 if consteval { return detail::half_constant::fused<false>(a,b,c); }
424 detail::avx512_fp16_backend::fma_half(a.to_native(),b.to_native(),c.to_native()));
425 }
426}
427
428#pragma clang attribute pop
429#undef NATIVE_ARCH_REQUIRES
430#endif
431#if NATIVE_HOST_NEON || defined(NATIVE_DOXYGEN)
432#define NATIVE_ARCH_REQUIRES(A) (::native::neon_bf16 <= A)
433#pragma clang attribute push(__attribute__((target("neon,bf16"))), apply_to=function)
434export namespace native {
435 namespace detail {
436 template<::native::isa<> A> requires NATIVE_ARCH_REQUIRES(A)
437 struct value_traits<simd<bf16,8,A>> {
438 static constexpr isa<> value=neon_bf16;
439 static constexpr bool known=true;
440 static constexpr bool aggregate_default=false;
441 };
442 }
450 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) struct simd<bf16,8,Arch> {
454 static constexpr isa<> architecture=Arch;
458 using native_type = bfloat16x8_t;
471 template<class T> using rebind = simd<T,8,architecture>;
473 static constexpr std::size_t lanes = 8;
474#include "native/simd/polyfill_lane_access.h"
475 private:
476 native_type value_;
477 public:
479 simd() noexcept = default;
481 hint_inline constexpr explicit simd(bf16 value) noexcept
482 : value_(__builtin_bit_cast(native_type,bits_type(value.to_bits()).to_native())) {}
483
484 hint_inline constexpr explicit simd(std::array<bf16,lanes> const & values) noexcept : simd(load(values.data())) {}
486 template<class... T> requires(sizeof...(T) == lanes && (std::same_as<T,bf16> && ...))
487 hint_inline constexpr simd(T... values) noexcept : simd(std::array<bf16,lanes>{values...}) {}
489 hint_inline constexpr simd(native_type value) noexcept : value_(value) {}
491 [[nodiscard]] hint_inline constexpr operator native_type() const noexcept { return value_; }
492 // Native interoperability must not add elementwise BF16 arithmetic.
493#include "native/simd/bf16_reject_operators.h"
495 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept { return value_; }
497 [[nodiscard]] static hint_inline constexpr simd from_native(native_type value) noexcept {
498 simd result; result.value_ = value; return result;
499 }
500
501 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept {
502 return bits_type::from_native(__builtin_bit_cast(typename bits_type::native_type,value_));
503 }
504
505 [[nodiscard]] hint_inline constexpr bits_type to_bits() const noexcept { return bits(); }
507 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type value) noexcept {
508 return from_native(__builtin_bit_cast(native_type,value.to_native()));
509 }
510
512 [[nodiscard]] static hint_inline constexpr simd load_bits(std::uint16_t const * p) noexcept {
513 return from_bits(bits_type::load(p));
514 }
515
517 hint_inline constexpr void store_bits(std::uint16_t * p) const noexcept { bits().store(p); }
522 template<std::size_t Alignment = 1>
523 [[nodiscard]] static hint_inline constexpr simd load_memory(bf16 const * p) noexcept {
524 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
525 if consteval {
526 std::array<std::uint16_t,lanes> words{};
527 for (std::size_t i=0;i<lanes;++i) words[i]=p[i].to_bits();
528 return load_bits(words.data());
529 }
530 native_type value; std::memcpy(&value, reinterpret_cast<unsigned char const *>(p), sizeof(value)); return from_native(value);
531 }
532
536 template<std::size_t Alignment = 1>
537 hint_inline constexpr void store_memory(bf16 * p) const noexcept {
538 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
539 if consteval {
540 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
541 for (std::size_t i=0;i<lanes;++i) p[i]=bf16::from_bits(words[i]);
542 return;
543 }
544 std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, sizeof(value_));
545 }
546
547 [[nodiscard]] static hint_inline constexpr simd load(bf16 const * p) noexcept { return load_memory(p); }
549 hint_inline constexpr void store(bf16 * p) const noexcept { store_memory(p); }
551 [[nodiscard]] static hint_inline constexpr simd loadu(bf16 const * p) noexcept { return load(p); }
553 hint_inline constexpr void storeu(bf16 * p) const noexcept { store(p); }
558 [[nodiscard]] static hint_inline constexpr simd load_partial(bf16 const * p, std::size_t n,
559 bf16 fill = bf16::from_bits(0)) noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
560 assert(n <= lanes);
561 std::array<bf16,lanes> values; values.fill(fill);
562 if consteval {
563 for (std::size_t i=0;i<n;++i) values[i]=p[i];
564 } else { if (n) std::memcpy(values.data(), reinterpret_cast<unsigned char const *>(p), n * sizeof(bf16)); }
565 return load(values.data());
566 }
567
570 hint_inline constexpr void store_partial(bf16 * p, std::size_t n) const noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
571 assert(n <= lanes);
572 if consteval {
573 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
574 for (std::size_t i=0;i<n;++i) p[i]=bf16::from_bits(words[i]);
575 } else { if (n) std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, n * sizeof(bf16)); }
576 }
577 };
578
590 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch)
591 [[nodiscard]] hint_inline constexpr simd<float,4,Arch> dot2(
593 simd<float,4,Arch> accumulator) noexcept {
594 if consteval { return detail::half_constant::dot2<true>(a,b,accumulator); }
596 detail::neon_bf16_backend::dot2_native(a.to_native(), b.to_native(), accumulator.to_native()));
597 }
598}
599
600#pragma clang attribute pop
601#undef NATIVE_ARCH_REQUIRES
602#define NATIVE_ARCH_REQUIRES(A) (::native::neon_fp16 <= A)
603#pragma clang attribute push(__attribute__((target("neon,fullfp16"))), apply_to=function)
604export namespace native {
605 namespace detail {
606 template<::native::isa<> A> requires NATIVE_ARCH_REQUIRES(A)
607 struct value_traits<simd<fp16,8,A>> {
608 static constexpr isa<> value=neon_fp16;
609 static constexpr bool known=true;
610 static constexpr bool aggregate_default=false;
611 };
612 }
625 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch) struct simd<fp16,8,Arch> {
629 static constexpr isa<> architecture=Arch;
633 using native_type = float16x8_t;
646 template<class T> using rebind = simd<T,8,architecture>;
648 static constexpr std::size_t lanes = 8;
649#include "native/simd/polyfill_lane_access.h"
650 private:
651 native_type value_;
652 public:
654 simd() noexcept = default;
656 hint_inline constexpr explicit simd(fp16 value) noexcept
657 : value_(__builtin_bit_cast(native_type,bits_type(value.to_bits()).to_native())) {}
658
659 hint_inline constexpr explicit simd(std::array<fp16,lanes> const & values) noexcept : simd(load(values.data())) {}
661 template<class... T> requires(sizeof...(T) == lanes && (std::same_as<T,fp16> && ...))
662 hint_inline constexpr simd(T... values) noexcept : simd(std::array<fp16,lanes>{values...}) {}
664 hint_inline constexpr simd(native_type value) noexcept : value_(value) {}
666 [[nodiscard]] hint_inline constexpr operator native_type() const noexcept { return value_; }
668 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept { return value_; }
670 [[nodiscard]] static hint_inline constexpr simd from_native(native_type value) noexcept {
671 simd result; result.value_ = value; return result;
672 }
673
674 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept {
675 return bits_type::from_native(__builtin_bit_cast(typename bits_type::native_type,value_));
676 }
677
678 [[nodiscard]] hint_inline constexpr bits_type to_bits() const noexcept { return bits(); }
680 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type value) noexcept {
681 return from_native(__builtin_bit_cast(native_type,value.to_native()));
682 }
683
685 [[nodiscard]] static hint_inline constexpr simd load_bits(std::uint16_t const * p) noexcept {
686 return from_bits(bits_type::load(p));
687 }
688
690 hint_inline constexpr void store_bits(std::uint16_t * p) const noexcept { bits().store(p); }
695 template<std::size_t Alignment = 1>
696 [[nodiscard]] static hint_inline constexpr simd load_memory(fp16 const * p) noexcept {
697 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
698 if consteval {
699 std::array<std::uint16_t,lanes> words{};
700 for (std::size_t i=0;i<lanes;++i) words[i]=p[i].to_bits();
701 return load_bits(words.data());
702 }
703 native_type value; std::memcpy(&value, reinterpret_cast<unsigned char const *>(p), sizeof(value)); return from_native(value);
704 }
705
709 template<std::size_t Alignment = 1>
710 hint_inline constexpr void store_memory(fp16 * p) const noexcept {
711 static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0);
712 if consteval {
713 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
714 for (std::size_t i=0;i<lanes;++i) p[i]=fp16::from_bits(words[i]);
715 return;
716 }
717 std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, sizeof(value_));
718 }
719
720 [[nodiscard]] static hint_inline constexpr simd load(fp16 const * p) noexcept { return load_memory(p); }
722 hint_inline constexpr void store(fp16 * p) const noexcept { store_memory(p); }
724 [[nodiscard]] static hint_inline constexpr simd loadu(fp16 const * p) noexcept { return load(p); }
726 hint_inline constexpr void storeu(fp16 * p) const noexcept { store(p); }
731 [[nodiscard]] static hint_inline constexpr simd load_partial(fp16 const * p, std::size_t n,
732 fp16 fill = fp16::from_bits(0)) noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
733 assert(n <= lanes);
734 std::array<fp16,lanes> values; values.fill(fill);
735 if consteval {
736 for (std::size_t i=0;i<n;++i) values[i]=p[i];
737 } else { if (n) std::memcpy(values.data(), reinterpret_cast<unsigned char const *>(p), n * sizeof(fp16)); }
738 return load(values.data());
739 }
740
743 hint_inline constexpr void store_partial(fp16 * p, std::size_t n) const noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
744 assert(n <= lanes);
745 if consteval {
746 std::array<std::uint16_t,lanes> words{}; store_bits(words.data());
747 for (std::size_t i=0;i<n;++i) p[i]=fp16::from_bits(words[i]);
748 } else { if (n) std::memcpy(reinterpret_cast<unsigned char *>(p), &value_, n * sizeof(fp16)); }
749 }
750
751 [[nodiscard]] friend hint_inline constexpr simd operator+(simd a,simd b) noexcept {
752 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::add,true>(a,b); }
753 return from_native(detail::neon_fp16_backend::add_half(a.value_,b.value_));
754 }
755
756 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a,simd b) noexcept {
757 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::subtract,true>(a,b); }
758 return from_native(detail::neon_fp16_backend::sub_half(a.value_,b.value_));
759 }
760
761 [[nodiscard]] friend hint_inline constexpr simd operator*(simd a,simd b) noexcept {
762 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::multiply,true>(a,b); }
763 return from_native(detail::neon_fp16_backend::mul_half(a.value_,b.value_));
764 }
765
767 [[nodiscard]] friend hint_inline constexpr simd operator/(simd a,simd b) noexcept {
768 if consteval { return detail::half_constant::arithmetic<detail::half_constant::operation::divide,true>(a,b); }
769 return from_native(detail::neon_fp16_backend::div_half(a.value_,b.value_));
770 }
771
775 [[nodiscard]] friend hint_inline constexpr simd sqrt(simd a) noexcept {
776 if consteval { return detail::half_constant::square_root<true>(a); }
777 return from_native(detail::neon_fp16_backend::sqrt_half(a.value_));
778 }
779
780 [[nodiscard]] friend hint_inline constexpr simd operator-(simd a) noexcept {
781 if consteval { return detail::half_constant::unary(a,[](auto x) { return std::uint16_t(x^0x8000); }); }
782 return from_native(detail::neon_fp16_backend::neg_half(a.value_));
783 }
784
786 [[nodiscard]] friend hint_inline constexpr mask operator==(simd a,simd b) noexcept {
787 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::equal_bits<detail::constexpr_float::binary16>(x,y); }); }
788 return mask::unsafe_from_native(detail::neon_fp16_backend::eq_half(a.value_,b.value_));
789 }
790
791 [[nodiscard]] friend hint_inline constexpr mask operator!=(simd a,simd b) noexcept { return ~(a==b); }
793 [[nodiscard]] friend hint_inline constexpr mask operator<(simd a,simd b) noexcept {
794 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::less_bits<detail::constexpr_float::binary16>(x,y); }); }
795 return mask::unsafe_from_native(detail::neon_fp16_backend::lt_half(a.value_,b.value_));
796 }
797
798 [[nodiscard]] friend hint_inline constexpr mask operator<=(simd a,simd b) noexcept {
799 if consteval { return detail::half_constant::compare(a,b,[](auto x,auto y) { return detail::constexpr_float::less_bits<detail::constexpr_float::binary16>(x,y) || detail::constexpr_float::equal_bits<detail::constexpr_float::binary16>(x,y); }); }
800 return mask::unsafe_from_native(detail::neon_fp16_backend::le_half(a.value_,b.value_));
801 }
802
803 [[nodiscard]] friend hint_inline constexpr mask operator>(simd a,simd b) noexcept { return b<a; }
805 [[nodiscard]] friend hint_inline constexpr mask operator>=(simd a,simd b) noexcept { return b<=a; }
808 [[nodiscard]] friend hint_inline constexpr simd select(mask m,simd a,simd b) noexcept {
809 if consteval { return detail::half_constant::select(m,a,b); }
810 return from_native(detail::neon_fp16_backend::select_half(m.to_native(),a.value_,b.value_));
811 }
812 };
813
817 template<::native::isa<> Arch> requires NATIVE_ARCH_REQUIRES(Arch)
818 [[nodiscard]] hint_inline constexpr simd<fp16,8,Arch> fma(
820 if consteval { return detail::half_constant::fused<true>(a,b,c); }
822 detail::neon_fp16_backend::fma_half(a.to_native(),b.to_native(),c.to_native()));
823 }
824}
825
826#pragma clang attribute pop
827#undef NATIVE_ARCH_REQUIRES
828#endif
829// Register shapes needed by instruction interfaces outside the arithmetic kernels.
830#if !NATIVE_HOST_WASM
831export namespace native {
832 namespace detail {
833 template<class T> concept instruction_element = simd_integer_element<T> ||
834 std::same_as<T,float> || std::same_as<T,double> ||
835 std::same_as<T,fp16> || std::same_as<T,bf16>;
836
837 template<class T, std::size_t N, isa<> A>
838 inline constexpr bool instruction_storage_shape = [] {
839 if constexpr(!instruction_element<T> || N<2) return false;
840 else if constexpr(N>64/sizeof(T)) return false;
841 else return instruction_only_shape<T,N,A>;
842 }();
843
844 template<std::size_t N,isa<> A> inline constexpr bool instruction_predicate_shape =
845 N>0 && N<=64 &&
846#if NATIVE_HOST_X86
847 A.has(x86_feature::sse2) && !((kernel_base<=A) &&
848 (N==1 || N==2 || N==3 || N==4 || N==8 || N==16 ||
849 ((N==32 || N==64) && A.has(x86_feature::avx512bw))));
850#elif NATIVE_HOST_NEON
851 (neon<=A);
852#else
853 false;
854#endif
855
856 template<class T,std::size_t N,isa<> A>
857 requires ordinary_simd_element<T> && instruction_storage_shape<T,N,A>
858 struct value_traits<simd<T,N,A>> {
859 static constexpr isa<> value=A;
860 static constexpr bool known=true;
861 static constexpr bool aggregate_default=false;
862 };
863 template<std::size_t N,isa<> A> requires instruction_predicate_shape<N,A>
864 struct value_traits<predicate<N,A>> {
865 static constexpr isa<> value=A;
866 static constexpr bool known=true;
867 static constexpr bool aggregate_default=false;
868 };
869
870 template<class T, std::size_t N> struct instruction_register {
871#if NATIVE_HOST_NEON
872 using type=std::conditional_t<std::same_as<T,double>,float64x2_t,
873 std::conditional_t<(sizeof(T)*N<=8),uint8x8_t,uint8x16_t>>;
874#elif NATIVE_HOST_X86
875 using type=std::conditional_t<std::same_as<T,float>,
876 std::conditional_t<(sizeof(T)*N<=16),__m128,std::conditional_t<(sizeof(T)*N==32),__m256,__m512>>,
877 std::conditional_t<std::same_as<T,double>,
878 std::conditional_t<(sizeof(T)*N<=16),__m128d,std::conditional_t<(sizeof(T)*N==32),__m256d,__m512d>>,
879 std::conditional_t<(sizeof(T)*N<=16),__m128i,std::conditional_t<(sizeof(T)*N==32),__m256i,__m512i>>>>;
880#endif
881 };
882 }
883
885 template<std::size_t N, isa<> A> requires detail::instruction_predicate_shape<N,A>
886 struct predicate<N,A> {
887 static constexpr isa<> architecture=A;
888 static constexpr std::size_t lanes=N;
889 using native_type=std::conditional_t<(N<=8),std::uint8_t,
890 std::conditional_t<(N<=16),std::uint16_t,std::conditional_t<(N<=32),std::uint32_t,std::uint64_t>>>;
891 using mask=predicate;
892 using mask_type=predicate;
893 static constexpr bool compact=true;
894 private:
895 native_type value_{};
896 static constexpr std::uint64_t active=[] { if constexpr(N==64) return ~std::uint64_t{}; else return (std::uint64_t{1}<<N)-1; }();
897 public:
899 hint_inline constexpr predicate() noexcept=default;
901 explicit hint_inline constexpr predicate(bool value) noexcept : value_(value?native_type(active):0) {}
903 static hint_inline constexpr predicate from_bits(std::uint64_t bits) noexcept { predicate p; p.value_=native_type(bits&active); return p; }
905 static hint_inline constexpr predicate from_bitset(std::uint64_t bits) noexcept { return from_bits(bits); }
907 static hint_inline constexpr predicate from_native(native_type bits) noexcept { return from_bits(bits); }
909 hint_inline constexpr native_type to_native() const noexcept { return value_; }
911 hint_inline constexpr std::uint64_t bits() const noexcept { return value_; }
913 hint_inline constexpr std::uint64_t to_bitset() const noexcept { return value_; }
915 friend hint_inline constexpr bool any(predicate p) noexcept { return p.value_!=0; }
917 friend hint_inline constexpr bool all(predicate p) noexcept { return p.value_==active; }
919 friend hint_inline constexpr bool none(predicate p) noexcept { return p.value_==0; }
921 friend hint_inline constexpr predicate operator~(predicate p) noexcept { return from_bits(~p.value_); }
923 friend hint_inline constexpr predicate operator!(predicate p) noexcept { return ~p; }
925 friend hint_inline constexpr predicate operator&(predicate a,predicate b) noexcept { return from_bits(a.value_&b.value_); }
927 friend hint_inline constexpr predicate operator|(predicate a,predicate b) noexcept { return from_bits(a.value_|b.value_); }
929 friend hint_inline constexpr predicate operator^(predicate a,predicate b) noexcept { return from_bits(a.value_^b.value_); }
931 friend hint_inline constexpr predicate operator==(predicate a,predicate b) noexcept { return ~(a^b); }
933 friend hint_inline constexpr predicate operator!=(predicate a,predicate b) noexcept { return a^b; }
935 hint_inline constexpr predicate & operator&=(predicate b) noexcept { return *this=*this&b; }
937 hint_inline constexpr predicate & operator|=(predicate b) noexcept { return *this=*this|b; }
939 hint_inline constexpr predicate & operator^=(predicate b) noexcept { return *this=*this^b; }
941 friend hint_inline constexpr predicate select(predicate p,predicate a,predicate b) noexcept { return (p&a)|(~p&b); }
942 };
943
947 template<class T, std::size_t N, isa<> A> requires detail::instruction_storage_shape<T,N,A>
948 struct alignas(typename detail::instruction_register<T,N>::type) simd<T,N,A>
949 : detail::polyfill_storage_operations<T,N,A,simd<T,N,A>> {
950 using value_type=T;
951 using register_type=simd;
952 using native_type=typename detail::instruction_register<T,N>::type;
953 static constexpr isa<> architecture=A;
954 static constexpr std::size_t lanes=N;
955 using word_type=std::conditional_t<sizeof(T)==1,std::uint8_t,std::conditional_t<sizeof(T)==2,std::uint16_t,std::conditional_t<sizeof(T)==4,std::uint32_t,std::uint64_t>>>;
956 using mask=predicate<N,A>;
957 using mask_type=mask;
958 using predicate_type=mask;
959 using bits_type=simd<std::conditional_t<sizeof(T)==1,std::uint8_t,
960 std::conditional_t<sizeof(T)==2,std::uint16_t,
961 std::conditional_t<sizeof(T)==4,std::uint32_t,std::uint64_t>>>,N,A>;
962 template<class U> using rebind=simd<U,N,A>;
963 private:
964 native_type value_;
965 public:
966 // Clang may accept a storage record mixed with a builtin vector even
967 // without a native conversion operator. Reject half arithmetic explicitly.
968#define NATIVE_INSTRUCTION_HALF_REJECT_BINARY(OP) \
969 \
970 friend void operator OP(simd,simd) \
971 requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) = delete; \
972 \
973 template<class U> requires(((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) && \
974 !std::same_as<std::remove_cvref_t<U>,simd>) \
975 friend void operator OP(simd,U) = delete; \
976 \
977 template<class U> requires(((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) && \
978 !std::same_as<std::remove_cvref_t<U>,simd>) \
979 friend void operator OP(U,simd) = delete;
980 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(+)
981 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(-)
982 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(*)
983 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(/)
984 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(%)
985 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(&)
986 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(|)
987 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(^)
988 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(<<)
989 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(>>)
990 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(==)
991 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(!=)
992 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(<)
993 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(<=)
994 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(>)
995 NATIVE_INSTRUCTION_HALF_REJECT_BINARY(>=)
996#undef NATIVE_INSTRUCTION_HALF_REJECT_BINARY
997#define NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(OP) \
998 \
999 template<class U> requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) \
1000 void operator OP(U) = delete;
1001 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(+=)
1002 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(-=)
1003 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(*=)
1004 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(/=)
1005 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(%=)
1006 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(&=)
1007 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(|=)
1008 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(^=)
1009 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(<<=)
1010 NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN(>>=)
1011#undef NATIVE_INSTRUCTION_HALF_REJECT_ASSIGN
1012#define NATIVE_INSTRUCTION_POLYFILL_ASSIGN(OP) \
1013 \
1014 template<class U> requires(A.has(polyfill) && requires(simd x,U y) { { x OP y } -> std::same_as<simd>; }) \
1015 constexpr simd & operator OP##=(U value) noexcept { return *this=*this OP value; }
1016 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(+)
1017 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(-)
1018 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(*)
1019 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(/)
1020 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(%)
1021 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(&)
1022 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(|)
1023 NATIVE_INSTRUCTION_POLYFILL_ASSIGN(^)
1024#undef NATIVE_INSTRUCTION_POLYFILL_ASSIGN
1026 friend void operator+(simd) requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) = delete;
1028 friend void operator-(simd) requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) = delete;
1030 friend void operator~(simd) requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) = delete;
1032 friend void operator!(simd) requires((std::same_as<T,fp16> && !A.has(polyfill)) || std::same_as<T,bf16>) = delete;
1034 constexpr simd() noexcept=default;
1036 hint_inline constexpr explicit simd(T value) noexcept {
1037 std::array<T,N> values;
1038 values.fill(value);
1039 *this=load(values.data());
1040 }
1041
1042 hint_inline constexpr explicit simd(std::array<T,N> const & values) noexcept : simd(load(values.data())) {}
1044 template<class... U> requires(sizeof...(U)==N && (std::same_as<U,T> && ...))
1045 hint_inline constexpr simd(U... values) noexcept : simd(std::array<T,N>{values...}) {}
1046#if NATIVE_HOST_X86
1047 // Native vector arguments and returns must carry their register ABI even
1048 // when an always-inline caller has already enabled that target.
1050 [[nodiscard]] hint_inline constexpr hint_target("sse2")
1051 native_type to_native() const noexcept requires(sizeof(native_type)==16) { return value_; }
1053 [[nodiscard]] static hint_inline constexpr hint_target("sse2")
1054 simd from_native(native_type value) noexcept requires(sizeof(native_type)==16) {
1055 simd result; result.value_=value; return result;
1056 }
1057
1058 [[nodiscard]] static hint_inline constexpr hint_target("sse2")
1059 simd unsafe_from_native(native_type value) noexcept requires(sizeof(native_type)==16) {
1060 simd result; result.value_=value; return result;
1061 }
1062
1063 [[nodiscard]] hint_inline constexpr hint_target("avx")
1064 native_type to_native() const noexcept requires(sizeof(native_type)==32) { return value_; }
1066 [[nodiscard]] static hint_inline constexpr hint_target("avx")
1067 simd from_native(native_type value) noexcept requires(sizeof(native_type)==32) {
1068 simd result; result.value_=value; return result;
1069 }
1070
1071 [[nodiscard]] static hint_inline constexpr hint_target("avx")
1072 simd unsafe_from_native(native_type value) noexcept requires(sizeof(native_type)==32) {
1073 simd result; result.value_=value; return result;
1074 }
1075
1076 [[nodiscard]] hint_inline constexpr hint_target("avx512f")
1077 native_type to_native() const noexcept requires(sizeof(native_type)==64) { return value_; }
1079 [[nodiscard]] static hint_inline constexpr hint_target("avx512f")
1080 simd from_native(native_type value) noexcept requires(sizeof(native_type)==64) {
1081 simd result; result.value_=value; return result;
1082 }
1083
1084 [[nodiscard]] static hint_inline constexpr hint_target("avx512f")
1085 simd unsafe_from_native(native_type value) noexcept requires(sizeof(native_type)==64) {
1086 simd result; result.value_=value; return result;
1087 }
1088#else
1090 [[nodiscard]] hint_inline constexpr native_type to_native() const noexcept { return value_; }
1092 [[nodiscard]] static hint_inline constexpr simd from_native(native_type value) noexcept {
1093 simd result; result.value_=value; return result;
1094 }
1095#endif
1097 template<std::size_t Alignment=1>
1098 [[nodiscard]] static hint_inline constexpr simd load_memory(T const * p) noexcept {
1099 static_assert(Alignment>0 && (Alignment&(Alignment-1))==0);
1100 simd result{};
1101 if consteval {
1102 std::array<T,sizeof(native_type)/sizeof(T)> values{};
1103 for(std::size_t i=0;i<N;++i) values[i]=p[i];
1104 result.value_=std::bit_cast<native_type>(values);
1105 } else { std::memcpy(&result.value_,reinterpret_cast<unsigned char const *>(p),sizeof(T)*N); }
1106 return result;
1107 }
1108
1109 template<std::size_t Alignment=1>
1110 hint_inline constexpr void store_memory(T * p) const noexcept {
1111 static_assert(Alignment>0 && (Alignment&(Alignment-1))==0);
1112 if consteval {
1113 auto values=std::bit_cast<std::array<T,sizeof(native_type)/sizeof(T)>>(value_);
1114 for(std::size_t i=0;i<N;++i) p[i]=values[i];
1115 } else { std::memcpy(reinterpret_cast<unsigned char *>(p),&value_,sizeof(T)*N); }
1116 }
1117
1118 [[nodiscard]] static hint_inline constexpr simd load(T const * p) noexcept { return load_memory(p); }
1120 [[nodiscard]] static hint_inline constexpr simd loadu(T const * p) noexcept { return load(p); }
1122 hint_inline constexpr void store(T * p) const noexcept { store_memory(p); }
1124 hint_inline constexpr void storeu(T * p) const noexcept { store(p); }
1126 [[nodiscard]] static hint_inline constexpr simd load_partial(T const * p,std::size_t n,T fill=T{}) noexcept hint_diagnose_if(n > simd::lanes,"partial SIMD count exceeds the lane count") {
1127 assert(n<=N);
1128 std::array<T,N> values; values.fill(fill);
1129 if consteval {
1130 for(std::size_t i=0;i<n;++i) values[i]=p[i];
1131 } else { if(n) std::memcpy(values.data(),reinterpret_cast<unsigned char const *>(p),n*sizeof(T)); }
1132 return load(values.data());
1133 }
1134
1135 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") {
1136 assert(n<=N);
1137 if consteval {
1138 if(n) {
1139 auto values=std::bit_cast<std::array<T,sizeof(native_type)/sizeof(T)>>(value_);
1140 for(std::size_t i=0;i<n;++i) p[i]=values[i];
1141 }
1142 } else { if(n) std::memcpy(reinterpret_cast<unsigned char *>(p),&value_,n*sizeof(T)); }
1143 }
1144
1145 [[nodiscard]] hint_inline constexpr bits_type bits() const noexcept requires(std::same_as<T,fp16> || std::same_as<T,bf16> || A.has(polyfill)) {
1146 return bits_type::from_native(std::bit_cast<typename bits_type::native_type>(value_));
1147 }
1148
1149 [[nodiscard]] hint_inline constexpr bits_type to_bits() const noexcept requires(std::same_as<T,fp16> || std::same_as<T,bf16> || A.has(polyfill)) { return bits(); }
1151 [[nodiscard]] static hint_inline constexpr simd from_bits(bits_type words) noexcept requires(std::same_as<T,fp16> || std::same_as<T,bf16> || A.has(polyfill)) {
1152 return from_native(std::bit_cast<native_type>(words.to_native()));
1153 }
1154
1155 [[nodiscard]] static hint_inline constexpr simd load_bits(word_type const * p) noexcept requires(std::same_as<T,fp16> || std::same_as<T,bf16> || A.has(polyfill)) {
1156 if consteval { return from_bits(bits_type::load(p)); }
1157 else { simd result{}; std::memcpy(&result.value_,reinterpret_cast<unsigned char const *>(p),sizeof(T)*N); return result; }
1158 }
1159
1160 hint_inline constexpr void store_bits(word_type * p) const noexcept requires(std::same_as<T,fp16> || std::same_as<T,bf16> || A.has(polyfill)) {
1161 if consteval { bits().store(p); }
1162 else { std::memcpy(reinterpret_cast<unsigned char *>(p),&value_,sizeof(T)*N); }
1163 }
1164 };
1165}
1166
1167#endif
1168
1169#if NATIVE_HOST_NEON || NATIVE_HOST_X86
1170export namespace native {
1175 template<std::size_t N,isa<> A> requires(N>0 && N<=64 && N%2==0 && A.has(polyfill) &&
1176#if NATIVE_HOST_NEON
1177 !A.has(arm_feature::neon_bf16) &&
1178#else
1179 !A.has(x86_feature::avx512bf16) &&
1180#endif
1181 requires { sizeof(simd<bf16,N,A>); sizeof(simd<float,N/2,A>); })
1182 [[nodiscard]] hint_inline constexpr simd<float,N/2,A> dot2(
1183 simd<bf16,N,A> a,simd<bf16,N,A> b,simd<float,N/2,A> accumulator) noexcept {
1184 return detail::half_constant::dot2<NATIVE_HOST_NEON!=0>(a,b,accumulator);
1185 }
1186}
1187#if NATIVE_HOST_NEON
1188#pragma clang attribute push(__attribute__((target("neon,bf16"))), apply_to=function)
1189#else
1190#pragma clang attribute push(__attribute__((target("avx512bf16"))), apply_to=function)
1191#endif
1192export namespace native {
1196 template<std::size_t N,isa<> A> requires(N>0 && N<=64 && N%2==0 && A.has(polyfill) &&
1197#if NATIVE_HOST_NEON
1198 A.has(arm_feature::neon_bf16) && !(N==8 && neon_bf16<=A) &&
1199#else
1200 A.has(x86_feature::avx512bf16) && !((N==8 || N==16 || N==32) && avx512_bf16<=A) &&
1201#endif
1202 requires { sizeof(simd<bf16,N,A>); sizeof(simd<float,N/2,A>); })
1203 [[nodiscard]] hint_inline constexpr simd<float,N/2,A> dot2(
1204 simd<bf16,N,A> a,simd<bf16,N,A> b,simd<float,N/2,A> accumulator) noexcept {
1205 return detail::polyfill_dot2_native(a,b,accumulator);
1206 }
1207}
1208#pragma clang attribute pop
1209#endif
Scalar half storage, numeric limits and legacy scalar predicates.
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr simd< float, N/2, Arch > dot2(simd< bf16, N, Arch > a, simd< bf16, N, Arch > b, simd< float, N/2, Arch > accumulator) noexcept
constexpr simd< fp16, 32, Arch > fma(simd< fp16, 32, Arch > a, simd< fp16, 32, Arch > b, simd< fp16, 32, Arch > c) 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
Standard-library adaptations documented here for SIMD value types.
static constexpr bf16 from_bits(uint16_t data) noexcept
Adopt the exact sixteen representation bits without normalization.
static constexpr fp16 from_bits(uint16_t data) noexcept
Adopt the exact sixteen representation bits without normalization.
constexpr std::uint64_t to_bitset() const noexcept
Return the logical lane bitset.
static constexpr predicate from_bits(std::uint64_t bits) noexcept
Read one bit per lane and clear bits above the logical lane count.
friend constexpr bool all(predicate p) noexcept
Test whether every logical lane is set.
static constexpr predicate from_bitset(std::uint64_t bits) noexcept
Construct from the logical lane bitset.
friend constexpr predicate operator~(predicate p) noexcept
Complement logical lanes, leaving padding clear.
constexpr std::uint64_t bits() const noexcept
Return one bit per logical lane.
friend constexpr predicate operator==(predicate a, predicate b) noexcept
Mark lanes whose truth values agree.
friend constexpr bool none(predicate p) noexcept
Test whether every logical lane is clear.
constexpr native_type to_native() const noexcept
Return the compact implementation representation.
constexpr predicate & operator^=(predicate b) noexcept
Toggle lanes present in another mask.
friend constexpr predicate operator!(predicate p) noexcept
Complement each logical lane.
static constexpr predicate from_native(native_type bits) noexcept
Adopt the compact representation, clearing unused bits.
friend constexpr predicate operator&(predicate a, predicate b) noexcept
Intersect the logical lane masks.
friend constexpr bool any(predicate p) noexcept
Test whether any logical lane is set.
friend constexpr predicate operator^(predicate a, predicate b) noexcept
Toggle lanes present in exactly one operand.
friend constexpr predicate select(predicate p, predicate a, predicate b) noexcept
Choose each mask lane from a or b according to p.
friend constexpr predicate operator!=(predicate a, predicate b) noexcept
Mark lanes whose truth values differ.
constexpr predicate & operator&=(predicate b) noexcept
Intersect with another mask in place.
friend constexpr predicate operator|(predicate a, predicate b) noexcept
Unite the logical lane masks.
constexpr predicate() noexcept=default
Construct an empty mask.
constexpr predicate & operator|=(predicate b) noexcept
Unite with another mask in place.
friend void operator+(simd)=delete
Reject unary arithmetic on half instruction storage.
static constexpr simd load(T const *p) noexcept
Read exactly N elements with their natural alignment.
static constexpr simd load_bits(word_type const *p) noexcept
Read N unsigned lane representations without numerical conversion.
constexpr void store_memory(T *p) const noexcept
Write exactly N objects; Alignment is a caller promise.
constexpr bits_type to_bits() const noexcept
Synonym for bits; no floating-point conversion occurs.
friend void operator-(simd)=delete
Reject unary arithmetic on half instruction storage.
constexpr bits_type bits() const noexcept
Return each lane as its unchanged unsigned representation.
friend void operator!(simd)=delete
Half storage does not define a predicate conversion.
constexpr simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
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 native_type to_native() const noexcept
Bridge to the implementation register without numerical conversion.
static constexpr simd unsafe_from_native(native_type value) noexcept
Synonym for from_native; these storage-only shapes do not normalize padding.
static constexpr simd loadu(T const *p) noexcept
Synonym for load; register alignment is unnecessary.
constexpr simd(std::array< T, N > const &values) noexcept
Copy the array elements in lane order.
friend void operator~(simd)=delete
Use bits() for explicit bitwise operations on half representations.
static constexpr simd from_bits(bits_type words) noexcept
Interpret unsigned words as lane representations without conversion.
static constexpr simd from_native(native_type value) noexcept
Adopt register bits unchanged; unused physical bytes are unspecified.
static constexpr simd load_memory(T const *p) noexcept
Read exactly N objects, without requiring register-width alignment.
constexpr void store(T *p) const noexcept
Write exactly N elements in lane order.
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.
constexpr void store_bits(word_type *p) const noexcept
Write N unsigned lane representations without numerical conversion.
constexpr void storeu(T *p) const noexcept
Synonym for store; register alignment is unnecessary.
constexpr void store_partial(bf16 *p, std::size_t n) const noexcept
simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
simd< std::uint16_t, 8, architecture > bits_type
Unsigned 16-bit lanes in the same profile and lane order.
static constexpr simd from_bits(bits_type value) noexcept
Interpret each unsigned lane as a BF16 representation without changing its bits.
static constexpr simd load_bits(std::uint16_t const *p) noexcept
static constexpr std::size_t lanes
Number of logical BF16 lanes, with no padding lanes.
constexpr bits_type to_bits() const noexcept
Synonym for bits(); this is a representation bridge, not a numeric conversion.
constexpr void store(bf16 *p) const noexcept
Store 8 BF16 objects with the default alignment contract of store_memory().
constexpr native_type to_native() const noexcept
Return all lane bits as a native register, without conversion or lane reordering.
constexpr void store_memory(bf16 *p) const noexcept
static constexpr simd loadu(bf16 const *p) noexcept
Synonym for load(); no register-width alignment is required.
simd< T, 8, architecture > rebind
constexpr bits_type bits() const noexcept
Return the 16-bit representation of each lane in an unsigned vector.
static constexpr simd load_memory(bf16 const *p) noexcept
simd< mask16, 8, architecture > mask
Full-register predicate with zero or all-one 16-bit lanes.
constexpr simd(native_type value) noexcept
Adopt a native register without conversion or representation changes.
constexpr void store_bits(std::uint16_t *p) const noexcept
static constexpr isa architecture
The distinct compile-time NEON_BF16 instruction profile.
mask predicate_type
Explicit predicate spelling for the same full-register mask type.
bf16 value_type
Scalar storage element; each lane retains all 16 representation bits.
static constexpr simd load(bf16 const *p) noexcept
Load 8 BF16 objects with the default alignment contract of load_memory().
constexpr void storeu(bf16 *p) const noexcept
Synonym for store(); no register-width alignment is required.
static constexpr simd from_native(native_type value) noexcept
Copy a native BF16 register into this vector, preserving every representation bit.
bfloat16x8_t native_type
Native 128-bit BF16 register representation; native bridges copy bits.
constexpr simd(std::array< bf16, lanes > const &values) noexcept
Copy array element i into lane i without conversion or representation changes.
simd register_type
This one-register vector type, for generic register-based algorithms.
mask mask_type
Generic mask spelling for the full-register lane mask.
simd< mask16, 8, architecture > vector_mask_type
Full-register mask shape with zero or all-one 16-bit lanes.
static constexpr simd load_partial(bf16 const *p, std::size_t n, bf16 fill=bf16::from_bits(0)) noexcept
static constexpr std::size_t lanes
Number of logical BF16 lanes, with no padding lanes.
static constexpr simd load_memory(bf16 const *p) noexcept
static constexpr simd loadu(bf16 const *p) noexcept
Synonym for load(); no register-width alignment is required.
constexpr void store_memory(bf16 *p) const noexcept
static constexpr simd load_bits(std::uint16_t const *p) noexcept
constexpr simd(native_type value) noexcept
Adopt a native register without conversion or representation changes.
constexpr void store_bits(std::uint16_t *p) const noexcept
std::conditional_t< N==8, __m128bh, std::conditional_t< N==16, __m256bh, __m512bh > > native_type
Native register-width BF16 register representation; native bridges copy bits.
constexpr simd(std::array< bf16, lanes > const &values) noexcept
Copy array element i into lane i without conversion or representation changes.
constexpr void store_partial(bf16 *p, std::size_t n) const noexcept
simd< mask16, N, architecture > vector_mask_type
Full-register mask shape with zero or all-one 16-bit lanes.
bf16 value_type
Scalar storage element; each lane retains all 16 representation bits.
static constexpr isa architecture
The distinct compile-time AVX512_BF16 instruction profile.
constexpr bits_type to_bits() const noexcept
Synonym for bits(); this is a representation bridge, not a numeric conversion.
constexpr void storeu(bf16 *p) const noexcept
Synonym for store(); no register-width alignment is required.
static constexpr simd load(bf16 const *p) noexcept
Load N BF16 objects with the default alignment contract of load_memory().
static constexpr simd load_partial(bf16 const *p, std::size_t n, bf16 fill=bf16::from_bits(0)) noexcept
constexpr bits_type bits() const noexcept
Return the 16-bit representation of each lane in an unsigned vector.
constexpr void store(bf16 *p) const noexcept
Store N BF16 objects with the default alignment contract of store_memory().
simd< std::uint16_t, N, architecture > bits_type
Unsigned 16-bit lanes in the same profile and lane order.
mask mask_type
Generic mask spelling for the compact lane predicate.
static constexpr simd from_bits(bits_type value) noexcept
Interpret each unsigned lane as a BF16 representation without changing its bits.
predicate< N, architecture > mask
Compact predicate with bit i selecting BF16 lane i.
mask predicate_type
Explicit predicate spelling for the same compact mask type.
simd< T, N, architecture > rebind
constexpr native_type to_native() const noexcept
Return all lane bits as a native register, without conversion or lane reordering.
simd register_type
This one-register vector type, for generic register-based algorithms.
simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
static constexpr simd from_native(native_type value) noexcept
Copy a native BF16 register into this vector, preserving every representation bit.
constexpr bits_type bits() const noexcept
Return the 16-bit representation of each lane in an unsigned vector.
constexpr void store_partial(fp16 *p, std::size_t n) const noexcept
constexpr void store_bits(std::uint16_t *p) const noexcept
friend constexpr simd sqrt(simd a) noexcept
simd< T, 32, architecture > rebind
friend constexpr mask operator>=(simd a, simd b) noexcept
Ordered lane greater-or-equal, with native less-or-equal operands reversed.
static constexpr simd load_bits(std::uint16_t const *p) noexcept
simd register_type
This one-register vector type, for generic register-based algorithms.
constexpr void store(fp16 *p) const noexcept
Store 32 FP16 objects with the default alignment contract of store_memory().
constexpr bits_type to_bits() const noexcept
Synonym for bits(); this is a representation bridge, not a numeric conversion.
friend constexpr simd operator/(simd a, simd b) noexcept
simd< std::uint16_t, 32, architecture > bits_type
Unsigned 16-bit lanes in the same profile and lane order.
static constexpr simd from_native(native_type value) noexcept
Copy a native FP16 register into this vector, preserving every representation bit.
friend constexpr mask operator==(simd a, simd b) noexcept
constexpr simd(native_type value) noexcept
Adopt a native register without conversion or representation changes.
friend constexpr simd select(mask m, simd a, simd b) noexcept
static constexpr simd load_memory(fp16 const *p) noexcept
static constexpr simd from_bits(bits_type value) noexcept
Interpret each unsigned lane as a FP16 representation without changing its bits.
predicate< 32, architecture > mask
Compact predicate with bit i selecting FP16 lane i.
fp16 value_type
Scalar storage element; each lane retains all 16 representation bits.
static constexpr isa architecture
The distinct compile-time AVX512_FP16 instruction profile.
friend constexpr mask operator!=(simd a, simd b) noexcept
Lane inequality, true for unordered NaN operands; complements native equality.
static constexpr simd load_partial(fp16 const *p, std::size_t n, fp16 fill=fp16::from_bits(0)) noexcept
static constexpr simd load(fp16 const *p) noexcept
Load 32 FP16 objects with the default alignment contract of load_memory().
friend constexpr mask operator<=(simd a, simd b) noexcept
Ordered lane less-or-equal. NaNs compare false; native MXCSR exception semantics apply.
constexpr void store_memory(fp16 *p) const noexcept
static constexpr simd loadu(fp16 const *p) noexcept
Synonym for load(); no register-width alignment is required.
friend constexpr simd operator-(simd a) noexcept
Toggle every sign bit, preserving all payload bits without arithmetic exceptions.
constexpr simd(std::array< fp16, lanes > const &values) noexcept
Copy array element i into lane i without conversion or representation changes.
constexpr void storeu(fp16 *p) const noexcept
Synonym for store(); no register-width alignment is required.
friend constexpr simd operator*(simd a, simd b) noexcept
Multiply corresponding half lanes, rounding directly under the caller's MXCSR rounding control.
simd< mask16, 32, architecture > vector_mask_type
Full-register mask shape with zero or all-one 16-bit lanes.
simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
constexpr native_type to_native() const noexcept
Return all lane bits as a native register, without conversion or lane reordering.
mask mask_type
Generic mask spelling for the compact lane predicate.
friend constexpr simd operator+(simd a, simd b) noexcept
Add corresponding half lanes, rounding directly under the caller's MXCSR rounding control.
mask predicate_type
Explicit predicate spelling for the same compact predicate type.
friend constexpr simd operator-(simd a, simd b) noexcept
Subtract corresponding half lanes, rounding directly under the caller's MXCSR rounding control.
friend constexpr mask operator<(simd a, simd b) noexcept
Ordered lane less-than. NaNs compare false; native MXCSR exception semantics apply.
__m512h native_type
Native 512-bit FP16 register representation; native bridges copy bits.
static constexpr std::size_t lanes
Number of logical FP16 lanes, with no padding lanes.
friend constexpr mask operator>(simd a, simd b) noexcept
Ordered lane greater-than, with the native less-than operands reversed.
static constexpr std::size_t lanes
Number of logical FP16 lanes, with no padding lanes.
mask mask_type
Generic mask spelling for the full-register lane mask.
friend constexpr simd sqrt(simd a) noexcept
friend constexpr mask operator>=(simd a, simd b) noexcept
Ordered lane greater-or-equal, with native less-or-equal operands reversed.
simd< T, 8, architecture > rebind
static constexpr simd loadu(fp16 const *p) noexcept
Synonym for load(); no register-width alignment is required.
static constexpr simd load_memory(fp16 const *p) noexcept
static constexpr simd from_bits(bits_type value) noexcept
Interpret each unsigned lane as a FP16 representation without changing its bits.
constexpr void store(fp16 *p) const noexcept
Store 8 FP16 objects with the default alignment contract of store_memory().
friend constexpr simd operator/(simd a, simd b) noexcept
friend constexpr mask operator==(simd a, simd b) noexcept
simd< mask16, 8, architecture > mask
Full-register predicate with zero or all-one bits in each 16-bit lane.
friend constexpr simd select(mask m, simd a, simd b) noexcept
simd() noexcept=default
Default initialization leaves storage unspecified; braces zero it.
float16x8_t native_type
Native 128-bit FP16 register representation; native bridges copy bits.
friend constexpr mask operator!=(simd a, simd b) noexcept
Lane inequality, true for unordered NaN operands; complements native equality.
friend constexpr mask operator<=(simd a, simd b) noexcept
Ordered lane less-or-equal. NaNs compare false; native FPCR/FPSR semantics apply.
mask predicate_type
Explicit predicate spelling for the same full-register mask type.
static constexpr simd from_native(native_type value) noexcept
Copy a native FP16 register into this vector, preserving every representation bit.
friend constexpr simd operator-(simd a) noexcept
Apply native FNEG to each half lane; no scalar half-to-float conversion occurs.
static constexpr simd load_partial(fp16 const *p, std::size_t n, fp16 fill=fp16::from_bits(0)) noexcept
static constexpr simd load(fp16 const *p) noexcept
Load 8 FP16 objects with the default alignment contract of load_memory().
constexpr native_type to_native() const noexcept
Return all lane bits as a native register, without conversion or lane reordering.
fp16 value_type
Scalar storage element; each lane retains all 16 representation bits.
friend constexpr simd operator*(simd a, simd b) noexcept
Multiply corresponding half lanes, rounding directly under the caller's FPCR.
constexpr bits_type bits() const noexcept
Return the 16-bit representation of each lane in an unsigned vector.
constexpr void store_memory(fp16 *p) const noexcept
constexpr void store_bits(std::uint16_t *p) const noexcept
friend constexpr simd operator+(simd a, simd b) noexcept
Add corresponding half lanes, rounding directly under the caller's FPCR.
constexpr void store_partial(fp16 *p, std::size_t n) const noexcept
simd< mask16, 8, architecture > vector_mask_type
Full-register mask shape with zero or all-one 16-bit lanes.
friend constexpr simd operator-(simd a, simd b) noexcept
Subtract corresponding half lanes, rounding directly under the caller's FPCR.
constexpr bits_type to_bits() const noexcept
Synonym for bits(); this is a representation bridge, not a numeric conversion.
constexpr simd(std::array< fp16, lanes > const &values) noexcept
Copy array element i into lane i without conversion or representation changes.
static constexpr simd load_bits(std::uint16_t const *p) noexcept
simd< std::uint16_t, 8, architecture > bits_type
Unsigned 16-bit lanes in the same profile and lane order.
friend constexpr mask operator<(simd a, simd b) noexcept
Ordered lane less-than. NaNs compare false; native FPCR/FPSR semantics apply.
constexpr simd(native_type value) noexcept
Adopt a native register without conversion or representation changes.
static constexpr isa architecture
The distinct compile-time NEON_FP16 instruction profile.
constexpr void storeu(fp16 *p) const noexcept
Synonym for store(); no register-width alignment is required.
simd register_type
This one-register vector type, for generic register-based algorithms.
friend constexpr mask operator>(simd a, simd b) noexcept
Ordered lane greater-than, with the native less-than operands reversed.
Omitted architecture arguments use the native.simd provider's baseline.