native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
polyfill_storage.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4namespace native::detail {
5 template<class T,std::size_t N,isa<> A,class Self> struct polyfill_storage_operations {};
9 template<class T,std::size_t N,isa<> A,class Self>
10 requires(A.has(polyfill) && polyfill_element_traits<T>::supported)
11 struct polyfill_storage_operations<T,N,A,Self> {
12 using emulated_type=simd<T,N,isa<>(polyfill)>;
13 using word_type=std::conditional_t<sizeof(T)==1,std::uint8_t,
14 std::conditional_t<sizeof(T)==2,std::uint16_t,
15 std::conditional_t<sizeof(T)==4,std::uint32_t,std::uint64_t>>>;
16 using vector_mask_type=simd<mask_lane<word_type>,N,A>;
17 static constexpr emulated_type project(Self value) noexcept {
18 std::array<T,N> lanes{}; value.store(lanes.data());
19 return emulated_type::load(lanes.data());
20 }
21 static constexpr Self restore(emulated_type value) noexcept {
22 std::array<T,N> lanes{}; value.store(lanes.data());
23 return Self::load(lanes.data());
24 }
25#define NATIVE_POLYFILL_STORAGE_BINARY(OP) \
26 \
27 friend constexpr Self operator OP(Self a,Self b) noexcept \
28 requires requires(emulated_type x) { { x OP x } -> std::same_as<emulated_type>; } { \
29 return restore(project(a) OP project(b)); \
30 } \
31 \
32 constexpr Self & operator OP##=(Self b) noexcept \
33 requires requires(emulated_type x) { { x OP x } -> std::same_as<emulated_type>; } { \
34 auto & self=static_cast<Self &>(*this); return self=self OP b; \
35 }
36 NATIVE_POLYFILL_STORAGE_BINARY(+)
37 NATIVE_POLYFILL_STORAGE_BINARY(-)
38 NATIVE_POLYFILL_STORAGE_BINARY(*)
39 NATIVE_POLYFILL_STORAGE_BINARY(/)
40 NATIVE_POLYFILL_STORAGE_BINARY(%)
41 NATIVE_POLYFILL_STORAGE_BINARY(&)
42 NATIVE_POLYFILL_STORAGE_BINARY(|)
43 NATIVE_POLYFILL_STORAGE_BINARY(^)
44#undef NATIVE_POLYFILL_STORAGE_BINARY
45 template<class U> static constexpr bool scalar_operand=
46 simd_integer_element<T>?simd_integer_element<U>:
47 (polyfill_element_traits<T>::arithmetic && std::convertible_to<U,T>);
48#define NATIVE_POLYFILL_STORAGE_SCALAR(OP) \
49 \
50 template<class U> requires(scalar_operand<U> && requires(emulated_type x,U y) { x OP emulated_type(y); }) \
51 friend constexpr Self operator OP(Self a,U b) noexcept { return restore(project(a) OP emulated_type(b)); } \
52 \
53 template<class U> requires(scalar_operand<U> && requires(emulated_type x,U y) { emulated_type(y) OP x; }) \
54 friend constexpr Self operator OP(U a,Self b) noexcept { return restore(emulated_type(a) OP project(b)); }
55 NATIVE_POLYFILL_STORAGE_SCALAR(+)
56 NATIVE_POLYFILL_STORAGE_SCALAR(-)
57 NATIVE_POLYFILL_STORAGE_SCALAR(*)
58 NATIVE_POLYFILL_STORAGE_SCALAR(/)
59 NATIVE_POLYFILL_STORAGE_SCALAR(%)
60 NATIVE_POLYFILL_STORAGE_SCALAR(&)
61 NATIVE_POLYFILL_STORAGE_SCALAR(|)
62 NATIVE_POLYFILL_STORAGE_SCALAR(^)
63#undef NATIVE_POLYFILL_STORAGE_SCALAR
64#define NATIVE_POLYFILL_STORAGE_COMPARE(OP) \
65 \
66 friend constexpr auto operator OP(Self a,Self b) noexcept \
67 requires requires(emulated_type x) { (x OP x).to_bitset(); } { \
68 return Self::mask_type::from_bitset((project(a) OP project(b)).to_bitset()); \
69 }
70 NATIVE_POLYFILL_STORAGE_COMPARE(==)
71 NATIVE_POLYFILL_STORAGE_COMPARE(!=)
72 NATIVE_POLYFILL_STORAGE_COMPARE(<)
73 NATIVE_POLYFILL_STORAGE_COMPARE(<=)
74 NATIVE_POLYFILL_STORAGE_COMPARE(>)
75 NATIVE_POLYFILL_STORAGE_COMPARE(>=)
76#undef NATIVE_POLYFILL_STORAGE_COMPARE
77#define NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(OP) \
78 \
79 template<class U> requires(scalar_operand<U> && requires(emulated_type x,U y) { (x OP emulated_type(y)).to_bitset(); }) \
80 friend constexpr auto operator OP(Self a,U b) noexcept { return Self::mask_type::from_bitset((project(a) OP emulated_type(b)).to_bitset()); } \
81 \
82 template<class U> requires(scalar_operand<U> && requires(emulated_type x,U y) { (emulated_type(y) OP x).to_bitset(); }) \
83 friend constexpr auto operator OP(U a,Self b) noexcept { return Self::mask_type::from_bitset((emulated_type(a) OP project(b)).to_bitset()); }
84 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(==)
85 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(!=)
86 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(<)
87 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(<=)
88 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(>)
89 NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE(>=)
90#undef NATIVE_POLYFILL_STORAGE_SCALAR_COMPARE
91
93 friend constexpr Self operator-(Self value) noexcept
94 requires requires(emulated_type x) { { -x } -> std::same_as<emulated_type>; } { return restore(-project(value)); }
96 friend constexpr Self operator~(Self value) noexcept
97 requires requires(emulated_type x) { { ~x } -> std::same_as<emulated_type>; } { return restore(~project(value)); }
99 template<class M> requires(std::same_as<M,predicate<N,A>> || std::same_as<M,polyfill_predicate<N,A>> || std::same_as<M,vector_mask_type>)
100 friend constexpr Self select(M mask,Self a,Self b) noexcept {
101 return restore(select(emulated_type::mask_type::from_bitset(mask.to_bitset()),project(a),project(b)));
102 }
103#define NATIVE_POLYFILL_STORAGE_UNARY(NAME) \
104 \
105 friend constexpr Self NAME(Self value) noexcept \
106 requires requires(emulated_type x) { { NAME(x) } -> std::same_as<emulated_type>; } { return restore(NAME(project(value))); }
107 NATIVE_POLYFILL_STORAGE_UNARY(sqrt)
108 NATIVE_POLYFILL_STORAGE_UNARY(abs)
109 NATIVE_POLYFILL_STORAGE_UNARY(floor)
110 NATIVE_POLYFILL_STORAGE_UNARY(ceil)
111 NATIVE_POLYFILL_STORAGE_UNARY(trunc)
112 NATIVE_POLYFILL_STORAGE_UNARY(round_even)
113 NATIVE_POLYFILL_STORAGE_UNARY(popcount)
114#undef NATIVE_POLYFILL_STORAGE_UNARY
116 friend constexpr Self fma(Self a,Self b,Self c) noexcept
117 requires(polyfill_element_traits<T>::arithmetic) { return restore(fma(project(a),project(b),project(c))); }
119 friend constexpr Self min(Self a,Self b) noexcept requires(polyfill_element_traits<T>::arithmetic) { return select(a<b,a,b); }
121 friend constexpr Self max(Self a,Self b) noexcept requires(polyfill_element_traits<T>::arithmetic) { return select(a>b,a,b); }
123 template<std::size_t I> requires(I<N)
124 constexpr T get() const noexcept { return project(static_cast<Self const &>(*this)).template get<I>(); }
126 template<std::size_t I> requires(I<N)
127 constexpr Self set(T value) const noexcept { return restore(project(static_cast<Self const &>(*this)).template set<I>(value)); }
129 template<std::size_t I> requires(I<N)
130 constexpr Self replace(T value) const noexcept { return this->template set<I>(value); }
132 friend constexpr Self normal_pow2(Self n) noexcept requires(std::same_as<T,float>) { return restore(normal_pow2(project(n))); }
134 template<std::size_t K> requires(simd_integer_element<T> && K<sizeof(T)*8)
135 friend constexpr Self operator<<(Self value,imm_t<K>) noexcept { return value.template left<K>(); }
137 template<std::size_t K> requires(simd_integer_element<T> && K<sizeof(T)*8)
138 friend constexpr Self operator>>(Self value,imm_t<K>) noexcept { return value.template right<K>(); }
140 friend constexpr Self operator<<(Self value,Self counts) noexcept requires(std::same_as<T,std::uint32_t>) {
141 return restore(project(value)<<project(counts));
142 }
144 template<std::size_t K>
145 constexpr Self left() const noexcept
146 requires requires(emulated_type x) { x.template left<K>(); } {
147 return restore(project(static_cast<Self const &>(*this)).template left<K>());
148 }
150 template<std::size_t K>
151 constexpr Self right() const noexcept
152 requires requires(emulated_type x) { x.template right<K>(); } {
153 return restore(project(static_cast<Self const &>(*this)).template right<K>());
154 }
155 };
156}
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22
constexpr simd< float, N, Arch > ceil(simd< float, N, Arch > x) noexcept
constexpr auto abs(simd< float, N, Arch > a) noexcept
constexpr simd< float, N, Arch > trunc(simd< float, N, Arch > x) noexcept
constexpr simd< float, N, Arch > floor(simd< float, N, Arch > x) noexcept
constexpr auto operator~(wide< T, N > const &a)
Definition wide.h:606
void operator-(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
constexpr simd< T, N, A > sqrt(simd< T, N, A > v) noexcept
Compute correctly rounded square roots; negative finite lanes produce NaN.
constexpr simd< T, N, A > round_even(simd< T, N, A > v) noexcept
Round floating lanes to nearest integers, choosing even at ties.
constexpr simd< fp16, 32, Arch > fma(simd< fp16, 32, Arch > a, simd< fp16, 32, Arch > b, simd< fp16, 32, Arch > c) noexcept
constexpr simd< T, N, A > max(simd< T, N, A > a, simd< T, N, A > b) noexcept
Minimum/maximum; floating NaNs propagate and signed zeros follow WebAssembly rules.
void operator<<(simd< T, N, A >, simd< U, M, B >)=delete
Reject mixed architectures and mismatched short-vector widths before native conversions can participa...
constexpr simd< T, N, A > select(M mask, simd< T, N, A > a, simd< T, N, A > b) noexcept
Bridge a matching compact mask to native half's existing vector-mask selection.
constexpr simd< T, N, Arch > popcount(simd< T, N, Arch > value) noexcept
constexpr polyfill_t polyfill
Compose this permission with an ISA using A | polyfill. Used alone, it selects scalar emulation in th...
Definition isa.h:43
constexpr simd< T, N, A > min(simd< T, N, A > a, simd< T, N, A > b) noexcept
Minimum/maximum; floating NaNs propagate and signed zeros follow WebAssembly rules.