11#include <xvec/detail/config.hpp>
15namespace _XVEC_NAMESPACE::simd {
21 template<
int _Np, vec_or_mask_type _Sp>
23 static_assert(_Np <= _Sp::size,
"Too short to take N elements");
24 return permute<_Np>(in, [](simd_size_type i) -> simd_size_type {
return i ; });
29 template<
int _Np, vec_or_mask_type _Sp>
31 static_assert(_Sp::size >= _Np,
"Too short to drop N elements");
32 return permute<_Sp::size - _Np>(in, [](simd_size_type i) -> simd_size_type {
return i + _Np; });
38 template<
int _Np, vec_or_mask_type _Sp>
40 static_assert(_Np >= 1,
"Expected at least one repeat");
46 template<
int _Np, vec_or_mask_type _Sp>
48 static_assert(_Np >= 1,
"Expected at least one repeat");
54 template<
int _Np, vec_or_mask_type _Sp>
56 constexpr int NewSize = (_Sp::size + (_Np - 1)) / _Np;
57 return permute<NewSize>(in, [](simd_size_type i) -> simd_size_type {
return i * _Np ; });
62 template<
int _Np, vec_or_mask_type _Sp>
64 static_assert(_Np >= _Sp::size,
"Can't grow to a size smaller than the input");
65 return permute<_Np>(in, [](simd_size_type i) -> simd_size_type {
return i < _Sp::size ? i : zero_element; });
69 template<
int _Np, vec_type _Vp>
70 constexpr auto permute_grow(
generic_tag,
const _Vp& in,
typename _Vp::value_type fill)
noexcept {
71 static_assert(_Np >= _Vp::size,
"Can't grow to a size smaller than the input");
72 using _NewType = resize_t<_Np, _Vp>;
73 using _M =
typename _NewType::mask_type;
74 return select(_M::__mask_from_count(_Vp::size), permute_grow<_Np>(target, in), _NewType(fill));
77 template<
int _Np, mask_type _Mp>
78 constexpr auto permute_grow(generic_tag,
const _Mp& in,
bool fill)
noexcept {
79 static_assert(_Np >= _Mp::size,
"Can't grow to a size smaller than the input");
80 using _NewMask = resize_t<_Np, _Mp>;
81 return select(_NewMask::__mask_from_count(_Mp::size), permute_grow<_Np>(target, in), _NewMask(fill));
85 template<vec_or_mask_type _Sp>
87 return permute(in, [](simd_size_type i) -> simd_size_type {
return (_Sp::size - 1) - i; });
92 template<
int _M
iddle, vec_or_mask_type _Sp>
94 static_assert(_Middle >= 0 && _Middle < _Sp::size,
"Expected middle to refer to valid index [0..size)");
95 return permute(in, [](simd_size_type i) -> simd_size_type {
return (i + _Middle) % _Sp::size; });
98 template<
int ROWS,
int COLS, vec_or_mask_type _Sp>
99 constexpr auto permute_transpose(
generic_tag,
const _Sp& in)
noexcept {
100 static_assert(ROWS * COLS == _Sp::size,
"Input data is not a matrix of size ROWS * COLS");
101 return permute(in, [](
auto idx) {
return (idx / ROWS) + (idx % ROWS) * COLS; });
108 template<vec_or_mask_type... _Sps>
110 constexpr auto ROWS =
sizeof...(simds);
111 constexpr auto COLS = std::min({_Sps::size()...});
120 template<
int _Np, vec_or_mask_type _Sp>
122 static_assert(_Sp::size >= _Np,
"Must have at least one element per output");
123 return [=]<std::size_t... _Idx>(std::index_sequence<_Idx...>) {
125 } (std::make_index_sequence<_Np>());
130 template<vec_type _Vp>
133 using _C = detail::container_for_type<typename _Vp::value_type>;
136 t[index] = std::bit_cast<_C>(new_value);
138 return std::bit_cast<_Vp>(rebind_t<_C, _Vp>(t));
141 template<mask_type _Mp>
144 auto t = m.to_builtin();
145 t[index] = new_value;
146 return _Mp::from_builtin(t);
149 template<mask_type _Mp>
152 auto maskBit =
typename _Mp::builtin_type(1) << index;
153 auto newBit = new_value << index;
154 return _Mp::from_builtin((m.to_builtin() & ~maskBit) | newBit);
158 template<simd_
size_type _Begin, simd_
size_type _End, vec_or_mask_type _Vp>
160 static_assert(_Begin < _End,
"Start of requested range must be less than end");
161 static_assert(_End <= _Vp::size(),
"Requested range can't overlap end of container");
162 return permute<_End - _Begin>(v, [](
int i) ->
int {
return i + _Begin; });
165 template<simd_
size_type _Begin, simd_
size_type _End, mask_type _Mp>
166 constexpr auto extract(compact_mask_tag,
const _Mp& m)
168 static_assert(_Begin < _End,
"Start of requested range must be less than end");
169 static_assert(_End <= _Mp::size(),
"Requested range can't overlap end of container");
173 return take<_End - _Begin>(_Mp::from_builtin(m.to_register() >> _Begin));
176 template<simd_
size_type _Begin, vec_or_mask_type _Container, vec_or_mask_type _Child>
177 requires (vec_or_mask_same_element_type<_Container, _Child>)
179 insert(generic_tag,
const _Container& container,
const _Child& child)
noexcept
181 static_assert((_Child::size() + _Begin) <= _Container::size(),
"Child cannot overlap container end");
184 const auto resizedChild = permute<_Container::size()>(child, detail::perm_uninitResize);
187 auto inserter = [](
int i) ->
int {
188 if ((i >= _Begin) && i < (_Begin + _Child::size()))
189 return (i - _Begin) + _Container::size();
194 return permute_pair<_Container::size()>(container, resizedChild, inserter);
197 template<simd_
size_type _Begin, mask_type _Container, mask_type _Child>
198 requires (vec_or_mask_same_element_type<_Container, _Child>)
200 insert(compact_mask_tag,
const _Container& container,
const _Child& child)
noexcept
202 static_assert((_Child::size() + _Begin) <= _Container::size(),
"Child cannot overlap container end");
204 if constexpr (_Container::size() == _Child::size())
208 using _C =
typename _Container::builtin_type;
210 constexpr auto block = ((_C(1) << _Child::size()) - 1) << _Begin;
213 const auto removeBlock = container.to_builtin() & ~block;
216 const auto insertBlock = (_C(child.to_builtin()) << _Begin) & block;
218 return _Container::from_builtin(insertBlock | removeBlock);
246template<
int _Np, vec_or_mask_type _Sp>
constexpr auto grow(
const _Sp& in,
const typename _Sp::value_type& fill)
248 using _NewType = resize_t<_Np, _Sp>;
249 if (std::is_constant_evaluated())
250 return _NewType([=](
auto i) {
return i < _Sp::size ? in[i] : fill; });
267template<
int _Np, vec_or_mask_type _V>
constexpr auto stride(
const _V& in) {
268 if (std::is_constant_evaluated())
269 return permute<(_V::size + (_Np - 1)) / _Np>(in, [](simd_size_type i) -> simd_size_type {
return i * _Np ; });
291template<
int _Rows,
int _Cols>
constexpr auto transpose(
const vec_or_mask_type auto& in) {
return detail::permute_transpose<_Rows, _Cols>(target, in); }
294template<vec_or_mask_type _V>
constexpr auto set_element(
const _V& v,
int index,
typename _V::value_type element)
296 if (std::is_constant_evaluated())
297 return _V([=](
auto i) {
return i == index ? element : v[i]; });
303template<
int _Begin,
int _End, vec_or_mask_type _Vp>
304constexpr resize_t<_End - _Begin, _Vp>
extract(
const _Vp& v) {
return detail::extract<_Begin, _End>(target, v); }
316template<simd_
size_type _Begin, vec_or_mask_type _Parent, vec_or_mask_type _Child>
317requires (vec_or_mask_same_element_type<_Parent, _Child>)
318constexpr _Parent
insert(
const _Parent& parent,
const _Child& child)
noexcept
319 {
return detail::insert<_Begin>(target, parent, child); }
constexpr auto select(const _M &m, const _T &if_true, const _U &if_false) noexcept -> decltype(simd_select_impl(m, if_true, if_false))
Select between corresponding elements according to a mask.
Definition std_algorithm.hpp:88
constexpr auto cat(const basic_mask< _Bytes, _Abis > &... masks) noexcept
Concatenate several smaller mask values with the same element size into a single large mask value.
Definition split_concat.hpp:144
constexpr auto vec_as_container(const _Vp &v)
Generate a variable containing the iota sequence (i.e., 0, 1, 2, ... size-1) for the given simd::vec ...
Definition generic_impl.hpp:116
constexpr auto permute(const _Vp &v, detail::index_generator_function auto &&fn)
Permute the elements in a vec value using a generator function to compute the indexing.
Definition permute.hpp:94
constexpr auto grow(const vec_or_mask_type auto &in)
Grow the vec or mask input to be N elements.
Definition named_permute.hpp:243
constexpr auto zip(const _Sps &...simds)
Definition named_permute.hpp:283
constexpr auto take(const vec_or_mask_type auto &in)
Definition named_permute.hpp:237
constexpr auto transpose(const vec_or_mask_type auto &in)
Treat the vec or mask as a matrix of size [ROWS, COLS] and transpose the data into a matrix of size[C...
Definition named_permute.hpp:291
constexpr auto reverse(const vec_or_mask_type auto &in)
Reverse the order of the elements in the input vec or mask.
Definition named_permute.hpp:263
constexpr auto drop(const vec_or_mask_type auto &in)
Discard the first _Np elements of the input, returning the remainder.
Definition named_permute.hpp:240
constexpr auto stride(const _V &in)
Definition named_permute.hpp:267
constexpr auto repeat_all(const vec_or_mask_type auto &in)
Repeat the entire input value N times. The input [a b c] repeated 3 times would be [a b c a b c a b c...
Definition named_permute.hpp:257
constexpr auto repeat_each(const vec_or_mask_type auto &in)
Repeat each value in the vec or mask N times. The input [a b c] repeated 2 times would be [a a b b c ...
Definition named_permute.hpp:260
constexpr auto rotate(const vec_or_mask_type auto &in)
Definition named_permute.hpp:276
constexpr auto set_element(const _V &v, int index, typename _V::value_type element)
Insert a scalar element into the given index position of the supplied vec_or_mask_type object.
Definition named_permute.hpp:294
constexpr auto unzip(const vec_or_mask_type auto &in)
Definition named_permute.hpp:288
constexpr _Parent insert(const _Parent &parent, const _Child &child) noexcept
Definition named_permute.hpp:318
constexpr resize_t< _End - _Begin, _Vp > extract(const _Vp &v)
Extract the subset denoted by the range [begin..end) from the supplied vec or mask.
Definition named_permute.hpp:304
constexpr auto permute_grow(generic_tag, const _Sp &in) noexcept
A named permute which increases the number of elements in the vec or mask, placing a copy of the give...
Definition named_permute.hpp:63
constexpr auto permute_repeat_each(generic_tag, const _Sp &in) noexcept
A named permute which creates N copies of each element.
Definition named_permute.hpp:47
constexpr auto permute_zip(generic_tag, const _Sps &...simds) noexcept
Definition named_permute.hpp:109
constexpr basic_vec< _Tp, _AbiIp > permute(VENDOR tag, const basic_vec< _Tp, _AbiTp > &v, const basic_vec< _Ip, _AbiIp > &indexes)
Definition permute.hpp:276
constexpr auto permute_drop(generic_tag, const _Sp &in) noexcept
A named permute which discards the first _Np values of a vec or mask, returning the remainder.
Definition named_permute.hpp:30
constexpr auto permute_reverse(generic_tag, const _Sp &in)
Reverse the order in which the elements appear in the given input vec or mask.
Definition named_permute.hpp:86
constexpr _Vp set_element(generic_tag, const _Vp &v, int index, typename _Vp::value_type new_value)
Definition named_permute.hpp:131
constexpr auto permute_unzip(generic_tag, const _Sp &in) noexcept
Unzip breaks a vec or mask down into N smaller objects, where each vec or mask is the values at the s...
Definition named_permute.hpp:121
constexpr auto permute_rotate(generic_tag, const _Sp &in) noexcept
A named permute which rotates the input so that the element at the MIDDLE index becomes the first ele...
Definition named_permute.hpp:93
constexpr auto permute_take(generic_tag, const _Sp &in)
A named permute which keeps the first _Np values of a simd and discards the remainder.
Definition named_permute.hpp:22
constexpr auto permute_repeat_all(generic_tag, const _Sp &in) noexcept
A named permute which creates N copies of the input concatenated together.
Definition named_permute.hpp:39
constexpr auto permute_stride(generic_tag, const _Sp &in) noexcept
A named permute which extracts every N'th value, discarding values which do not have indexes which ar...
Definition named_permute.hpp:55
Definition target_config.hpp:161
Definition target_config.hpp:23