Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
split_concat.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 <algorithm> // std::min
14#include <cstddef>
15#include <tuple>
16#include <type_traits>
17
18#include <utility> // std::forward
19
20namespace _XVEC_NAMESPACE::simd
21{
22
23namespace detail
24{
25
30template<typename _Tuple> constexpr auto get_array_from_tuple(_Tuple&& tuple) {
31 constexpr auto get_array = [](auto &&...x) { return std::array{std::forward<decltype(x)>(x)...}; };
32 return std::apply(get_array, std::forward<_Tuple>(tuple));
33}
34
43template<simd_size_type _Begin, simd_size_type _Np, vec_or_mask_type _Sp>
44constexpr auto splitNHelper(const _Sp& v)
45{
46 constexpr simd_size_type remaining = _Sp::size - _Begin;
47 constexpr simd_size_type numInBlock = std::min(remaining, _Np);
48
49 const auto headTuple = std::make_tuple(extract<_Begin, _Begin + numInBlock>(v));
50
51 if constexpr (remaining <= _Np)
52 return headTuple;
53 else
54 {
55 const auto tailTuple = splitNHelper<_Begin + numInBlock, _Np>(v);
56 return std::tuple_cat(headTuple, tailTuple);
57 }
58}
59
69template<simd_size_type _Begin, typename _Result, typename _Sp>
70constexpr _Result concatHelper(const _Result& container, const _Sp& head, const vec_or_mask_type auto&... tail)
71{
72 _Result r;
73 // Insert, permute, etc. don't work fully in constexpr contexts because they rely on compiler intrinsics
74 // or implementation-specific details that are unavailable during constant evaluation. Therefore, we use
75 // a fallback approach here to ensure compatibility with constexpr.
76 if (std::is_constant_evaluated())
77 r = _Result([=](auto i) { if (i >= _Begin && i < (_Begin + _Sp::size())) return head[i - _Begin]; else return container[i]; });
78 else
79 r = insert<_Begin>(container, head);
80
81 if constexpr (sizeof...(tail) == 0)
82 return r;
83 else
84 return concatHelper<_Begin + _Sp::size>(r, tail...);
85}
86
87}
88
96
105template<simd_size_type _Np, vec_or_mask_type _S>
106constexpr auto
107chunk(const _S& container) noexcept {
108 auto r = detail::splitNHelper<0, _Np>(container);
109
110 // Exact multiplies return as a uniform array. Non-exact multiplies have to allow the last
111 // element to be a different size, so return as a tuple.
112 if constexpr (_S::size % _Np == 0)
114 else
115 return r;
116}
117
125template<typename _V, typename _Abi>
126constexpr auto
128
134template<mask_type _M, typename _Abi>
135constexpr auto chunk(const basic_mask<simd_mask_element_size_v<_M>, _Abi>& m) noexcept { return chunk<_M::size>(m); }
136
143template<std::size_t _Bytes, typename... _Abis>
144constexpr auto cat(const basic_mask<_Bytes, _Abis>&... masks) noexcept
145{
146 // Determine how big to make the container to store the concatenated vec or masks, and then insert each
147 // piece in turn.
148 constexpr auto totalNumElements = (... + basic_mask<_Bytes, _Abis>::size());
150 return ResultMask(detail::concatHelper<0>(ResultMask(), masks...));
151}
152
162template<typename _Tp, typename... _Abis>
163constexpr auto cat(const basic_vec<_Tp, _Abis>&... vecs) noexcept
164{
165 // Determine how big to make the container to store the concatenated simds, and then insert each
166 // piece in turn.
167 constexpr simd_size_type _resultSize = (... + basic_vec<_Tp, _Abis>::size());
169}
170
172
173} // namespace _XVEC_NAMESPACE::simd
A mask containing boolean elements.
Definition mask.hpp:121
Vector of elements sharing an element type and ABI.
Definition simd:161
static constexpr auto size
Return the number of elements contained in the vec.
Definition simd:187
Definition core.hpp:63
constexpr auto chunk(const _S &container) noexcept
Definition split_concat.hpp:107
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
static constexpr auto size
Return the number of elements contained in the mask.
Definition mask.hpp:148
basic_vec< _Tp, simd_fixed_size_abi< _Np > > vec
Definition core.hpp:379
constexpr _Result concatHelper(const _Result &container, const _Sp &head, const vec_or_mask_type auto &... tail)
Definition split_concat.hpp:70
constexpr auto splitNHelper(const _Sp &v)
Definition split_concat.hpp:44
constexpr auto get_array_from_tuple(_Tuple &&tuple)
Definition split_concat.hpp:30