Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
named_permute.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <xvec/detail/config.hpp>
12
13#include <utility> // std::index_sequence, std::make_index_sequence
14
15namespace _XVEC_NAMESPACE::simd {
16
17namespace detail {
18
21 template<int _Np, vec_or_mask_type _Sp>
22 constexpr auto permute_take(generic_tag, const _Sp& in) {
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 ; });
25 }
26
29 template<int _Np, vec_or_mask_type _Sp>
30 constexpr auto permute_drop(generic_tag, const _Sp& in) noexcept {
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; });
33 }
34
37 // :TODO: Or concat several?
38 template<int _Np, vec_or_mask_type _Sp>
39 constexpr auto permute_repeat_all(generic_tag, const _Sp& in) noexcept {
40 static_assert(_Np >= 1, "Expected at least one repeat");
41 return permute<_Sp::size * _Np>(in, [](simd_size_type i) -> simd_size_type { return i % _Sp::size; });
42 }
43
46 template<int _Np, vec_or_mask_type _Sp>
47 constexpr auto permute_repeat_each(generic_tag, const _Sp& in) noexcept {
48 static_assert(_Np >= 1, "Expected at least one repeat");
49 return permute<_Sp::size * _Np>(in, [](simd_size_type i) -> simd_size_type { return i / _Np; });
50 }
51
54 template<int _Np, vec_or_mask_type _Sp>
55 constexpr auto permute_stride(generic_tag, const _Sp& in)noexcept {
56 constexpr int NewSize = (_Sp::size + (_Np - 1)) / _Np;
57 return permute<NewSize>(in, [](simd_size_type i) -> simd_size_type { return i * _Np ; });
58 }
59
62 template<int _Np, vec_or_mask_type _Sp>
63 constexpr auto permute_grow(generic_tag, const _Sp& in) noexcept {
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; });
66 }
67
68 // Grow with filled initialise
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));
75 }
76
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));
82 }
83
85 template<vec_or_mask_type _Sp>
86 constexpr auto permute_reverse(generic_tag, const _Sp& in) {
87 return permute(in, [](simd_size_type i) -> simd_size_type { return (_Sp::size - 1) - i; });
88 }
89
92 template<int _Middle, vec_or_mask_type _Sp>
93 constexpr auto permute_rotate(generic_tag, const _Sp& in) noexcept {
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; });
96 }
97
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; });
102 }
103
108 template<vec_or_mask_type... _Sps>
109 constexpr auto permute_zip(generic_tag, const _Sps& ...simds) noexcept {
110 constexpr auto ROWS = sizeof...(simds);
111 constexpr auto COLS = std::min({_Sps::size()...});
112 auto t = cat(permute_take<COLS>(target, simds)...);
113 return permute<ROWS * COLS>(t, [](auto idx) { return (idx / ROWS) + (idx % ROWS) * COLS; });
114 }
115
120 template<int _Np, vec_or_mask_type _Sp>
121 constexpr auto permute_unzip(generic_tag, const _Sp& in) noexcept {
122 static_assert(_Sp::size >= _Np, "Must have at least one element per output");
123 return [=]<std::size_t... _Idx>(std::index_sequence<_Idx...>) {
124 return std::make_tuple(stride<_Np>(permute_drop<_Idx>(target, in))...);
125 } (std::make_index_sequence<_Np>());
126 }
127
130 template<vec_type _Vp>
131 constexpr _Vp set_element(generic_tag, const _Vp& v, int index, typename _Vp::value_type new_value)
132 {
133 using _C = detail::container_for_type<typename _Vp::value_type>;
134
135 auto t = vec_as_container(v).to_builtin(); // Index using the raw compiler support.
136 t[index] = std::bit_cast<_C>(new_value);
137
138 return std::bit_cast<_Vp>(rebind_t<_C, _Vp>(t)); // Reconstruct back to original type.
139 }
140
141 template<mask_type _Mp>
142 constexpr _Mp
143 set_element(generic_tag, const _Mp& m, int index, bool new_value) {
144 auto t = m.to_builtin();
145 t[index] = new_value;
146 return _Mp::from_builtin(t);
147 }
148
149 template<mask_type _Mp>
150 constexpr _Mp
151 set_element(compact_mask_tag, const _Mp& m, int index, bool new_value) {
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);
155 }
156
157
158 template<simd_size_type _Begin, simd_size_type _End, vec_or_mask_type _Vp>
159 constexpr auto extract(generic_tag, const _Vp& v) {
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; });
163 }
164
165 template<simd_size_type _Begin, simd_size_type _End, mask_type _Mp>
166 constexpr auto extract(compact_mask_tag, const _Mp& m)
167 {
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");
170
171 // Move the desired part to the lower bits, and then fit it to the desired
172 // output size.
173 return take<_End - _Begin>(_Mp::from_builtin(m.to_register() >> _Begin));
174 }
175
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>)
178 constexpr _Container
179 insert(generic_tag, const _Container& container, const _Child& child) noexcept
180 {
181 static_assert((_Child::size() + _Begin) <= _Container::size(), "Child cannot overlap container end");
182
183 // Resize the child to be the same size as the container. Permute only works if they are the same size.
184 const auto resizedChild = permute<_Container::size()>(child, detail::perm_uninitResize);
185
186 // Create a sequence which is a copy of the parent, but with a sub-sequence referencing the child.
187 auto inserter = [](int i) -> int {
188 if ((i >= _Begin) && i < (_Begin + _Child::size()))
189 return (i - _Begin) + _Container::size(); // Choose from the child.
190 else
191 return i; // Choose from the parent
192 };
193
194 return permute_pair<_Container::size()>(container, resizedChild, inserter);
195 }
196
197 template<simd_size_type _Begin, mask_type _Container, mask_type _Child>
198 requires (vec_or_mask_same_element_type<_Container, _Child>)
199 constexpr _Container
200 insert(compact_mask_tag, const _Container& container, const _Child& child) noexcept
201 {
202 static_assert((_Child::size() + _Begin) <= _Container::size(), "Child cannot overlap container end");
203
204 if constexpr (_Container::size() == _Child::size())
205 return child; // Some cases of cat need this. :TODO: Fix cat instead.
206 else
207 {
208 using _C = typename _Container::builtin_type;
209 // Create a block of bits that is positioned at the insertion position.
210 constexpr auto block = ((_C(1) << _Child::size()) - 1) << _Begin;
211
212 // Zero out the insertion position.
213 const auto removeBlock = container.to_builtin() & ~block;
214
215 // Move the desired bits to the insertion position and limit to just those bits.
216 const auto insertBlock = (_C(child.to_builtin()) << _Begin) & block;
217
218 return _Container::from_builtin(insertBlock | removeBlock);
219 }
220 }
221
222}
223
234
237template<int _Np> constexpr auto take(const vec_or_mask_type auto& in) { return detail::permute_take<_Np>(target, in); }
238
240template<int _Np> constexpr auto drop(const vec_or_mask_type auto& in) { return detail::permute_drop<_Np>(target, in); }
241
243template<int _Np> constexpr auto grow(const vec_or_mask_type auto& in) { return detail::permute_grow<_Np>(target, in); }
244
246template<int _Np, vec_or_mask_type _Sp> constexpr auto grow(const _Sp& in, const typename _Sp::value_type& fill)
247{
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; });
251 else
252 // :TODO: Make select/mask_from_count constexpr?
253 return detail::permute_grow<_Np>(target, in, fill);
254}
255
257template<int _Np> constexpr auto repeat_all(const vec_or_mask_type auto& in) { return detail::permute_repeat_all<_Np>(target, in); }
258
260template<int _Np> constexpr auto repeat_each(const vec_or_mask_type auto& in) { return detail::permute_repeat_each<_Np>(target, in); }
261
263constexpr auto reverse(const vec_or_mask_type auto& in) { return detail::permute_reverse(target, in); }
264
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 ; });
270 else
271 return detail::permute_stride<_Np>(target, in); }
272
276template<int _Middle> constexpr auto rotate(const vec_or_mask_type auto& in) { return detail::permute_rotate<_Middle>(target, in); }
277
283template<vec_or_mask_type... _Sps> constexpr auto zip(const _Sps& ...simds) { return detail::permute_zip(target, simds...); }
284
288template<int N> constexpr auto unzip(const vec_or_mask_type auto& in) { return detail::permute_unzip<N>(target, in); }
289
291template<int _Rows, int _Cols> constexpr auto transpose(const vec_or_mask_type auto& in) { return detail::permute_transpose<_Rows, _Cols>(target, in); }
292
294template<vec_or_mask_type _V> constexpr auto set_element(const _V& v, int index, typename _V::value_type element)
295{
296 if (std::is_constant_evaluated())
297 return _V([=](auto i) { return i == index ? element : v[i]; });
298 else
299 return detail::set_element(target, v, index, element);
300}
301
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); }
305
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); }
320
322
323} // namespace _XVEC_NAMESPACE::simd
Definition core.hpp:63
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