Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
compat.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 <cassert>
12#include <concepts>
13#include <limits>
14#include <type_traits>
15#include <utility>
16
17#include <xvec/detail/config.hpp>
18
20#if defined(__cpp_lib_is_scoped_enum) && (__cpp_lib_is_scoped_enum >= 202011L)
21// C++23 library feature-test macro for the std::is_scoped_enum trait
22namespace _XVEC_NAMESPACE::simd::detail {
23 template<class E>
24 concept is_scoped_enum = std::is_scoped_enum_v<E>;
25}
26#else
27namespace _XVEC_NAMESPACE::simd::detail {
28 template<class E>
30 std::is_enum_v<E> &&
31 (!std::is_convertible_v<E, std::underlying_type_t<E>>);
32}
33#endif
34
35namespace _XVEC_NAMESPACE::simd::detail {
36
37template<class T>
39 std::integral<T> &&
40 !std::same_as<std::remove_cv_t<T>, bool> &&
41 !std::same_as<std::remove_cv_t<T>, char> &&
42 !std::same_as<std::remove_cv_t<T>, wchar_t> &&
43 !std::same_as<std::remove_cv_t<T>, char8_t> &&
44 !std::same_as<std::remove_cv_t<T>, char16_t> &&
45 !std::same_as<std::remove_cv_t<T>, char32_t>;
46
49template<saturation_integer To, saturation_integer From>
50[[nodiscard]] constexpr To saturating_cast(From value) noexcept
51{
52 if (std::in_range<To>(value))
53 return static_cast<To>(value);
54
55 // A negative out-of-range value lies below the destination range. This also
56 // covers conversion to an unsigned destination, whose minimum is zero.
57 if constexpr (std::signed_integral<From>) {
58 if (value < 0)
59 return std::numeric_limits<To>::min();
60 }
61
62 // Every remaining out-of-range value lies above the destination range.
63 return std::numeric_limits<To>::max();
64}
65
67template<saturation_integer T>
68[[nodiscard]] constexpr T saturating_add(T lhs, T rhs) noexcept
69{
70 T result{};
71
72 if (!__builtin_add_overflow(lhs, rhs, &result))
73 return result;
74
75 constexpr T lowest = std::numeric_limits<T>::min();
76 constexpr T highest = std::numeric_limits<T>::max();
77
78 if constexpr (std::unsigned_integral<T>) {
79 // Unsigned addition can overflow only above the upper endpoint.
80 return highest;
81 } else {
82 // Signed addition can overflow only when both operands have the same
83 // sign. Their shared sign therefore determines the saturation direction.
84 return rhs < 0 ? lowest : highest;
85 }
86}
87
89template<saturation_integer T>
90[[nodiscard]] constexpr T saturating_sub(T lhs, T rhs) noexcept
91{
92 T result{};
93
94 if (!__builtin_sub_overflow(lhs, rhs, &result))
95 return result;
96
97 constexpr T lowest = std::numeric_limits<T>::min();
98 constexpr T highest = std::numeric_limits<T>::max();
99
100 if constexpr (std::unsigned_integral<T>) {
101 // Unsigned subtraction is unrepresentable only when the mathematical
102 // result falls below zero.
103 return lowest;
104 } else {
105 // Subtracting a negative value can overflow only toward the upper
106 // endpoint. Subtracting a positive value can overflow only toward the
107 // lower endpoint.
108 return rhs < 0 ? highest : lowest;
109 }
110}
111
113template<saturation_integer T>
114[[nodiscard]] constexpr T saturating_mul(T lhs, T rhs) noexcept
115{
116 T result{};
117
118 if (!__builtin_mul_overflow(lhs, rhs, &result))
119 return result;
120
121 constexpr T lowest = std::numeric_limits<T>::min();
122 constexpr T highest = std::numeric_limits<T>::max();
123
124 if constexpr (std::unsigned_integral<T>) {
125 // An unsigned mathematical product cannot be negative, so overflow is
126 // always above the upper endpoint.
127 return highest;
128 } else {
129 // Operands with equal signs have a positive mathematical product and
130 // therefore overflow toward the upper endpoint. Different signs produce
131 // a negative product and overflow toward the lower endpoint.
132 return (lhs < 0) == (rhs < 0) ? highest : lowest;
133 }
134}
135
137template<saturation_integer T>
138[[nodiscard]] constexpr T saturating_div(T lhs, T rhs) noexcept
139{
140 if constexpr (std::signed_integral<T>) {
141 // lowest / -1 is the only integral division with a nonzero divisor whose
142 // mathematical result is not representable in the operand type.
143 if (lhs == std::numeric_limits<T>::min() && rhs == T{-1})
144 return std::numeric_limits<T>::max();
145 }
146
147 return lhs / rhs;
148}
149
150// Transparent binary function objects for bitwise shift operations. These are
151// used as the default shift operators for simd types.
152template<typename T = void>
154 constexpr T operator()(const T& lhs, const T& rhs) const {
155 return lhs << rhs;
156 }
157};
158
159template<typename T = void>
161 constexpr T operator()(const T& lhs, const T& rhs) const {
162 return lhs >> rhs;
163 }
164};
165
166// Transparent specializations
167template<>
168struct bit_lshift<void> {
169 template<typename T, typename U>
170 constexpr auto operator()(T&& lhs, U&& rhs) const
171 -> decltype(std::forward<T>(lhs) << std::forward<U>(rhs)) {
172 return std::forward<T>(lhs) << std::forward<U>(rhs);
173 }
174
175 using is_transparent = void;
176};
177
178template<>
179struct bit_rshift<void> {
180 template<typename T, typename U>
181 constexpr auto operator()(T&& lhs, U&& rhs) const
182 -> decltype(std::forward<T>(lhs) >> std::forward<U>(rhs)) {
183 return std::forward<T>(lhs) >> std::forward<U>(rhs);
184 }
185
186 using is_transparent = void;
187};
188
189template<typename _Tp>
191 std::same_as<std::remove_cvref_t<_Tp>, unsigned char> ||
192 std::same_as<std::remove_cvref_t<_Tp>, unsigned short> ||
193 std::same_as<std::remove_cvref_t<_Tp>, unsigned int> ||
194 std::same_as<std::remove_cvref_t<_Tp>, unsigned long> ||
195 std::same_as<std::remove_cvref_t<_Tp>, unsigned long long>;
196
202template <funnel_shift_source T, std::integral S>
203[[nodiscard]] constexpr T funnel_shift_right(T high, T low, S s) noexcept {
204 constexpr auto N = std::numeric_limits<T>::digits;
205 assert(s >= 0 && static_cast<unsigned>(s) < static_cast<unsigned>(N));
206 const auto r = static_cast<unsigned>(s);
207 if (r == 0) return low;
208 return static_cast<T>((low >> r) | (high << (N - r)));
209}
210
216template <funnel_shift_source T, std::integral S>
217[[nodiscard]] constexpr T funnel_shift_left(T high, T low, S s) noexcept {
218 constexpr auto N = std::numeric_limits<T>::digits;
219 assert(s >= 0 && static_cast<unsigned>(s) < static_cast<unsigned>(N));
220 const auto r = static_cast<unsigned>(s);
221 if (r == 0) return high;
222 return static_cast<T>((high << r) | (low >> (N - r)));
223}
224
225} // namespace _XVEC_NAMESPACE::simd::detail
226
227#if defined(__FLT16_MIN__) && !defined(__STDCPP_FLOAT16_T__)
228// The compiler has _Float16 as an extension, but the standard library has
229// not yet provided std::numeric_limits<_Float16>, so supply our own.
230
231namespace std {
232template <>
233struct numeric_limits<_Float16> {
234 static constexpr bool is_specialized = true;
235
236 static constexpr int digits = 11;
237 static constexpr int digits10 = 3;
238 static constexpr int max_digits10 = 5;
239 static constexpr bool is_signed = true;
240 static constexpr bool is_integer = false;
241 static constexpr bool is_exact = false;
242 static constexpr int radix = 2;
243 static constexpr int min_exponent = -13;
244 static constexpr int min_exponent10 = -4;
245 static constexpr int max_exponent = 16;
246 static constexpr int max_exponent10 = 4;
247
248 static constexpr bool has_infinity = true;
249 static constexpr bool has_quiet_NaN = true;
250 static constexpr bool has_signaling_NaN = true;
251
252 static constexpr bool is_iec559 = true;
253 static constexpr bool is_bounded = true;
254 static constexpr bool is_modulo = false;
255
256 static constexpr bool traps = false;
257 static constexpr bool tinyness_before = false;
258 static constexpr float_round_style round_style = round_to_nearest;
259
260 static constexpr _Float16 min() noexcept { return static_cast<_Float16>(__FLT16_MIN__); }
261 static constexpr _Float16 max() noexcept { return static_cast<_Float16>(__FLT16_MAX__); }
262 static constexpr _Float16 lowest() noexcept { return static_cast<_Float16>(-__FLT16_MAX__); }
263 static constexpr _Float16 epsilon() noexcept { return static_cast<_Float16>(__FLT16_EPSILON__); }
264 static constexpr _Float16 round_error() noexcept { return static_cast<_Float16>(0.5f); }
265 static constexpr _Float16 infinity() noexcept { return static_cast<_Float16>(__builtin_huge_valf()); }
266 static constexpr _Float16 quiet_NaN() noexcept { return static_cast<_Float16>(__builtin_nanf("")); }
267 static constexpr _Float16 signaling_NaN() noexcept { return static_cast<_Float16>(__builtin_nansf("")); }
268 static constexpr _Float16 denorm_min() noexcept { return static_cast<_Float16>(__FLT16_DENORM_MIN__); }
269};
270
271} // namespace std
272#endif // defined(__FLT16_MIN__) && !defined(__STDCPP_FLOAT16_T__)
Definition compat.hpp:29
constexpr T funnel_shift_right(T high, T low, S s) noexcept
Definition compat.hpp:203
constexpr T saturating_sub(T lhs, T rhs) noexcept
Subtract two integers, clamping an unrepresentable mathematical result.
Definition compat.hpp:90
constexpr T saturating_div(T lhs, T rhs) noexcept
Divide two integers, clamping an unrepresentable mathematical result.
Definition compat.hpp:138
constexpr T saturating_add(T lhs, T rhs) noexcept
Add two integers, clamping an unrepresentable mathematical result.
Definition compat.hpp:68
constexpr T funnel_shift_left(T high, T low, S s) noexcept
Definition compat.hpp:217
constexpr To saturating_cast(From value) noexcept
Definition compat.hpp:50
constexpr T saturating_mul(T lhs, T rhs) noexcept
Multiply two integers, clamping an unrepresentable mathematical result.
Definition compat.hpp:114
Definition compat.hpp:153
Definition compat.hpp:160