Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
utilities.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 <bit>
14#include <complex>
15#include <cstdint>
16#include <cstddef>
17#include <stdexcept>
18#include <span>
19#include <string>
20#include <tuple>
21#include <type_traits>
22#include <utility>
23
24#include <xvec/detail/compat.hpp>
25
26namespace _XVEC_NAMESPACE::simd {
27
29using simd_size_type = int;
30
32template <simd_size_type _Np> using size_constant = std::integral_constant<simd_size_type, _Np>;
33
34namespace detail
35{
36
38#if defined(__llvm__)
39inline constexpr bool isLlvm = true;
40#else
41inline constexpr bool isLlvm = false;
42#endif
43
47// :TODO: Provide a default which asserts if no suitable case can be found.
48template<class... Ts> struct target_overloads : Ts... { using Ts::operator()...; };
49template<class... Ts> target_overloads(Ts...) -> target_overloads<Ts...>;
50
52template <typename _Tp>
53concept complex_number = std::is_same_v<std::remove_cvref_t<_Tp>, std::complex<typename _Tp::value_type>>;
54
57template<class T, class... Us>
58inline constexpr bool any_same_v = (std::same_as<std::remove_cvref_t<T>, Us> || ...);
59
61template<typename _Tp>
63 unsigned char, unsigned short, unsigned int,
64 unsigned long, unsigned long long>;
65
71template<typename _Tp> struct element_type { using value_type = _Tp; };
72template<complex_number _Tp> struct element_type<_Tp> { using value_type = typename _Tp::value_type; };
73template<typename _Tp> using element_type_t = typename element_type<_Tp>::value_type;
75
79template<typename _Tp> struct complex_element_type_query { struct value_type {}; }; // Dummy value type.
80template<complex_number _Tp> struct complex_element_type_query<_Tp> { using value_type = typename _Tp::value_type; };
81template<typename _Tp> using complex_element_type = typename complex_element_type_query<_Tp>::value_type;
83
87#if defined(__FLT16_MIN__)
88template<typename _T> inline constexpr bool is_fp16_v = std::same_as<std::remove_cvref_t<_T>, _Float16>;
89#else
90template<typename _T> inline constexpr bool is_fp16_v = false;
91#endif
92
93template <typename _T> struct is_fp16 : std::bool_constant<is_fp16_v<_T>> {};
94
102template <typename>
103inline constexpr bool dependent_false = false;
104
117template<typename _Parent, typename _Type>
118using dependent_type = typename std::conditional<true, _Type, _Parent>::type;
119
122// :TODO: :COMPILER: The 128-bit type is required for std::complex<double>, but it isn't very efficient. Need to spend
123// some time refining the code it generates to avoid the worst inefficiencies.
124using containers_for_num_bytes = std::tuple<void, std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t, __uint128_t>;
125
126template<std::size_t _Bytes>
127using container_for_num_bytes = typename std::tuple_element_t<std::bit_width(std::bit_ceil(_Bytes)), containers_for_num_bytes>;
128
129template<typename _Tp> using container_for_type = container_for_num_bytes<sizeof(_Tp)>;
130
132template<class _Tp>
134 : std::disjunction<std::is_floating_point<_Tp>, is_fp16<_Tp>> {};
135
136// Convenience variable template + cvref stripping for our definition of floating point.
137template<class T>
138inline constexpr bool is_floating_point_v = is_floating_point<std::remove_cvref_t<T>>::value;
139
140// Our own version of arithmetic to deal with FP16 properly.
141template<class T>
142inline constexpr bool is_arithmetic_like_v = std::is_integral_v<std::remove_cvref_t<T>> || is_floating_point_v<T>;
143
146template<typename _Tp> struct unsigned_to_signed { using value_type = std::make_signed_t<_Tp>; };
147template<> struct unsigned_to_signed<unsigned __int128> { using value_type = signed __int128; };
148template<typename _Tp> using make_signed_t = typename unsigned_to_signed<_Tp>::value_type;
149
151template <typename _Tp> inline constexpr simd_size_type max_native_size = detail::maxBytesInVec / sizeof(_Tp);
152
155template <typename _Tp> inline constexpr simd_size_type max_fixed_size = 128;
156
163template<typename _Tp> struct element_storage { using value_type = container_for_type<_Tp>; };
164template<complex_number _Tp> struct element_storage<_Tp> { using value_type = typename _Tp::value_type; };
165template<typename _Tp> requires (std::is_enum_v<_Tp>) struct element_storage<_Tp> { using value_type = std::underlying_type_t<_Tp>; };
166template<typename _Tp> requires (std::is_arithmetic_v<_Tp> || is_floating_point_v<_Tp>) struct element_storage<_Tp> { using value_type = _Tp; };
167template<typename _Tp> using element_storage_t = typename element_storage<_Tp>::value_type;
169
171template<typename _Tp>
172constexpr auto get_span_static_extent(const _Tp& in) {
173 if constexpr (requires { std::span{in}; })
174 // Span is useful for extracting static extents from other things, like C-arrays or std::array.
175 return std::integral_constant<std::size_t, decltype(std::span{in})::extent>{};
176 else
177 return std::integral_constant<std::size_t, std::dynamic_extent>{};
178}
179
180template<class _Span> inline constexpr std::size_t span_extent_v = decltype(get_span_static_extent(std::declval<_Span>()))::value;
181
184template<typename _Span>
185constexpr auto get_span_element_type_helper(const _Span& in) {
186 if constexpr (requires { std::span{in}; })
187 // Span is useful for extracting static extents from other things, like C-arrays or std::array.
188 return typename decltype(std::span{in})::value_type();
189 else
190 return nullptr;
191}
192
193template<class _Span> using span_element_type = decltype(get_span_element_type_helper(std::declval<_Span>()));
194
199template<typename _Vec, std::size_t _Extent, typename _Mask = bool>
200constexpr void checkStaticMemoryBounds([[maybe_unused]] const std::string& op_name,
201 [[maybe_unused]] std::span<typename _Vec::value_type, _Extent> mem_span,
202 [[maybe_unused]] _Mask m = _Mask())
203{
204 constexpr bool opIsMasked = !std::same_as<_Mask, bool>;
205
206 // There is a precondition that the span is big enough to do the memory
207 // operation. However, if the span is a known size this can be explicitly
208 // checked at compile time. Note that this check is only performed when no
209 // mask is in use because the programmer may creating a mask to limit the
210 // operation to valid memory. Without knowing what the mask is we can't tell
211 // whether the precondition has been met or not. :TODO: If the mask is
212 // constexpr, check that too.
213 static_assert(opIsMasked || _Vec::size <= _Extent,
214 "Memory span is not big enough for simd load/store operation");
215
216#if defined(XVEC_ALWAYS_RANGE_CHECK_MEMORY)
217 auto opSize = _Vec::size;
218
219 if constexpr (opIsMasked) {
220 // A mask has been provided. The caller may be using the mask to limit the
221 // range. Check that no bits outside the valid range are set.
222 auto outOfRangeBits = !_Mask::__mask_from_count(mem_span.size());
223 if (any_of(outOfRangeBits & m))
224 {
225 std::string msg =
226 "xvec masked range error for " + op_name + ". Range size:" + std::to_string(mem_span.size()) +
227 " Mask:" + m.to_bitset().to_string();
228 throw std::out_of_range(msg);
229 }
230 }
231 else if (opSize > mem_span.size()) {
232 std::string msg =
233 "xvec range error for " + op_name + ". Range size:" + std::to_string(mem_span.size()) +
234 " simd size:" + std::to_string(opSize);
235 throw std::out_of_range(msg);
236 }
237#endif
238}
239
244template<typename _Vec>
245constexpr void checkStaticMemoryBounds([[maybe_unused]] const std::string& op_name,
246 [[maybe_unused]] const _Vec& indexes,
247 [[maybe_unused]] std::size_t range_size,
248 [[maybe_unused]] typename _Vec::mask_type m = typename _Vec::mask_type(true))
249{
250#if defined(XVEC_ALWAYS_RANGE_CHECK_MEMORY)
251 auto biggestIndex = reduce_max(indexes, m);
252 if (std::size_t(biggestIndex) >= range_size)
253 {
254 std::string msg =
255 "xvec range error for " + op_name + ". Range size:" + std::to_string(range_size) +
256 " maxIndex:" + std::to_string(biggestIndex);
257 throw std::out_of_range(msg);
258 }
259#endif
260}
261
262// Create a constant_wrapper-like variable which can be used to create
263// compile-time constants. Eventually this will be superseded by C++26's own cw handling.
264template <int X> inline constexpr std::integral_constant<int, X> cw{};
265
266} // End of namespace detail
267
268} // namespace _XVEC_NAMESPACE::simd
269
270/* Figure out whether to define the output operators */
271#if defined(_IOSTREAM_) || defined(_CPP_IOSTREAM) || \
272 defined(_GLIBCXX_IOSTREAM) || defined(_LIBCPP_IOSTREAM)
273#define _XVEC_DEFINE_OUTPUT_OPERATORS
274#endif
Detect whether a given type is std::complex.
Definition utilities.hpp:53
Detect a fundamental unsigned integer type.
Definition utilities.hpp:62
constexpr bool any_of(const mask_type auto &mask) noexcept
Return true if any mask element is true.
Definition mask.hpp:471
constexpr _V::value_type reduce_max(const _V &v) noexcept
Return the maximum vector element.
Definition reduce.hpp:187
constexpr bool any_same_v
Definition utilities.hpp:58
constexpr bool is_fp16_v
Definition utilities.hpp:90
std::tuple< void, std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t, __uint128_t > containers_for_num_bytes
Definition utilities.hpp:124
constexpr bool dependent_false
Definition utilities.hpp:103
typename std::conditional< true, _Type, _Parent >::type dependent_type
Definition utilities.hpp:118
constexpr auto get_span_static_extent(const _Tp &in)
Query the extent of a given type.
Definition utilities.hpp:172
constexpr auto get_span_element_type_helper(const _Span &in)
Definition utilities.hpp:185
constexpr simd_size_type max_native_size
The maximum native size of a register containing the given types.
Definition utilities.hpp:151
constexpr bool isLlvm
Compile time flag to indicate whether the LLVM compiler is being used.
Definition utilities.hpp:41
constexpr simd_size_type max_fixed_size
Definition utilities.hpp:155
constexpr int maxBytesInVec
Definition target_config.hpp:148
Definition utilities.hpp:163
Definition utilities.hpp:71
Temporary floating-point test. Used to handle half/_Float16 until the compiler catches up.
Definition utilities.hpp:134
Definition utilities.hpp:93
Allow overloaded lambdas to be used for target-specific code. Described at https://www....
Definition utilities.hpp:48
Definition utilities.hpp:146