Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
invoke.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::forward, std::integer_sequence
14
15namespace _XVEC_NAMESPACE::simd
16{
17
18namespace detail
19{
23template<typename _Tp>
24inline constexpr auto arg_native_size = _Tp::never_instantiated;
25
26template<typename _Tp, typename _Abi>
28
29template<std::size_t _Bytes, typename _Abi>
31
33template <simd_size_type Size, simd_size_type... Sizes>
34inline constexpr bool are_all_sizes_equal = (... && (Size == Sizes));
35
36} // End namespace detail
37
47
60template<simd_size_type _SuggestedN = 0, typename _Fn, vec_or_mask_type _Arg0, vec_or_mask_type... _Args>
61constexpr auto chunked_invoke(_Fn fn, const _Arg0& arg0, const _Args&... args)
62{
63 using namespace detail;
64
65 // Check that all sizes of vec or mask arguments are equal element-wise
66 static_assert(are_all_sizes_equal<_Arg0::size(), _Args::size()...>, "chunked_invoke expects all vec or mask arguments to have the same size");
67
68 // Check that block size is either set by the user or all native sizes are equal
69 static_assert(_SuggestedN != 0 || are_all_sizes_equal<arg_native_size<_Arg0>, arg_native_size<_Args>...>, "chunked_invoke expects all vec or mask arguments to have the same native size. Use the first template parameter to override this");
70
71 // Get the size of each piece which will be processed.
72 constexpr auto _B = _SuggestedN == 0 ? arg_native_size<_Arg0> : _SuggestedN;
73
74 // Check the function appears to be callable, and whether it takes an index or not.
75 constexpr auto _TS = std::min<simd_size_type>(_B, _Arg0::size());
76 using ArgTypesWithoutIndex = std::tuple<resize_t<_TS, _Arg0>, resize_t<_TS, _Args>...>;
77 using ArgTypesWithIndex = std::tuple<resize_t<_TS, _Arg0>, resize_t<_TS, _Args>..., simd_size_type>;
78 constexpr bool callableWithoutIndex = requires { fn(extract<0, _TS>(arg0), extract<0, _TS>(args)...); };
79 constexpr bool callableWithIndex = requires { fn(extract<0, _TS>(arg0), extract<0, _TS>(args)..., simd_size_type(0)); };
80 static_assert(callableWithIndex || callableWithoutIndex, "chunked_invoke can't call the function");
81 using ArgTypes = std::conditional_t<callableWithIndex, ArgTypesWithIndex, ArgTypesWithoutIndex>;
82
83 // Query the return type and check that it appears valid. The result must
84 // either be void, or a vec or mask type that can be joined to the results of the
85 // other pieces.
86 using ResultType = decltype(std::apply(fn, ArgTypes()));
87 constexpr bool isVoidFn = std::same_as<void, ResultType>;
88 static_assert(isVoidFn || vec_or_mask_type<ResultType>, "chunked_invoke expects the function to return a vec or mask result, or none at all");
89
90 // This utility lambda calls the function with a specific region of each vec or mask argument.
91 auto callFn = [&]<simd_size_type _Begin>(size_constant<_Begin>) {
92 constexpr int _End = std::min<int>(_Begin + _B, _Arg0::size());
93 if constexpr (callableWithoutIndex)
94 return fn(extract<_Begin, _End>(arg0), extract<_Begin, _End>(args)...);
95 else
96 return fn(extract<_Begin, _End>(arg0), extract<_Begin, _End>(args)..., _Begin);
97 };
98
99 // Call each sub-block as many times as needed.
100 constexpr int numBlocks = (_Arg0::size() + (_B - 1)) / _B;
101 return [=]<simd_size_type... _Idx>(std::integer_sequence<simd_size_type, _Idx...>)
102 {
103 // Note that the blocks must be executed in order of index.
104 if constexpr (isVoidFn)
105 (callFn(size_constant<_Idx * _B>{}), ...);
106 else
107 {
108 // Brace-init guarantees order.
109 auto results = std::tuple { callFn(size_constant<_Idx * _B>{})... };
110
111 // Concatenate the results together
112 return std::apply([](auto&&... r) { return cat(std::forward<decltype(r)>(r)...); }, results);
113 }
114 }(std::make_integer_sequence<simd_size_type, numBlocks>{});
115
116}
117
119
120} // namespace _XVEC_NAMESPACE::simd
Definition core.hpp:63
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 chunked_invoke(_Fn fn, const _Arg0 &arg0, const _Args &... args)
Invoke a callable on corresponding vector or mask chunks.
Definition invoke.hpp:61
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
Detect a scoped enum. This will be provided in C++23 onwards.
Definition compat.hpp:27
constexpr auto arg_native_size
Definition invoke.hpp:24
constexpr bool are_all_sizes_equal
Checks that all sizes are equal.
Definition invoke.hpp:34
constexpr simd_size_type max_native_size
The maximum native size of a register containing the given types.
Definition utilities.hpp:151
constexpr int maxBytesInVec
Definition target_config.hpp:148