| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_ARITHMETIC_SHIFT_HPP |
| 4 |
#define MPT_BASE_ARITHMETIC_SHIFT_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
|
| 12 |
namespace mpt { |
| 13 |
inline namespace MPT_INLINE_NS { |
| 14 |
|
| 15 |
|
| 16 |
// mpt::rshift_signed |
| 17 |
// mpt::lshift_signed |
| 18 |
// Shift a signed integer value in a well-defined manner. |
| 19 |
// Does the same thing as MSVC would do. This is verified by the test suite. |
| 20 |
|
| 21 |
template <typename T> |
| 22 |
constexpr auto rshift_signed_portable(T x, int y) noexcept -> decltype(x >> y) { |
| 23 |
static_assert(std::numeric_limits<T>::is_integer); |
| 24 |
static_assert(std::numeric_limits<T>::is_signed); |
| 25 |
using result_type = decltype(x >> y); |
| 26 |
using unsigned_result_type = typename std::make_unsigned<result_type>::type; |
| 27 |
const unsigned_result_type roffset = static_cast<unsigned_result_type>(1) << ((sizeof(result_type) * 8) - 1); |
| 28 |
result_type rx = x; |
| 29 |
unsigned_result_type urx = static_cast<unsigned_result_type>(rx); |
| 30 |
urx += roffset; |
| 31 |
urx >>= y; |
| 32 |
urx -= roffset >> y; |
| 33 |
return static_cast<result_type>(urx); |
| 34 |
} |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
constexpr auto lshift_signed_portable(T x, int y) noexcept -> decltype(x << y) { |
| 38 |
static_assert(std::numeric_limits<T>::is_integer); |
| 39 |
static_assert(std::numeric_limits<T>::is_signed); |
| 40 |
using result_type = decltype(x << y); |
| 41 |
using unsigned_result_type = typename std::make_unsigned<result_type>::type; |
| 42 |
const unsigned_result_type roffset = static_cast<unsigned_result_type>(1) << ((sizeof(result_type) * 8) - 1); |
| 43 |
result_type rx = x; |
| 44 |
unsigned_result_type urx = static_cast<unsigned_result_type>(rx); |
| 45 |
urx += roffset; |
| 46 |
urx <<= y; |
| 47 |
urx -= roffset << y; |
| 48 |
return static_cast<result_type>(urx); |
| 49 |
} |
| 50 |
|
| 51 |
#if MPT_CXX_AT_LEAST(20) || MPT_COMPILER_SHIFT_SIGNED |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
constexpr auto rshift_signed_cxx20(T x, int y) noexcept -> decltype(x >> y) { |
| 55 |
static_assert(std::numeric_limits<T>::is_integer); |
| 56 |
static_assert(std::numeric_limits<T>::is_signed); |
| 57 |
#if MPT_COMPILER_GCC |
| 58 |
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826> |
| 59 |
#pragma GCC diagnostic push |
| 60 |
#pragma GCC diagnostic ignored "-Wshift-negative-value" |
| 61 |
#endif // MPT_COMPILER_GCC |
| 62 |
return x >> y; |
| 63 |
#if MPT_COMPILER_GCC |
| 64 |
#pragma GCC diagnostic pop |
| 65 |
#endif // MPT_COMPILER_GCC |
| 66 |
} |
| 67 |
|
| 68 |
template <typename T> |
| 69 |
constexpr auto lshift_signed_cxx20(T x, int y) noexcept -> decltype(x << y) { |
| 70 |
static_assert(std::numeric_limits<T>::is_integer); |
| 71 |
static_assert(std::numeric_limits<T>::is_signed); |
| 72 |
#if MPT_COMPILER_GCC |
| 73 |
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826> |
| 74 |
#pragma GCC diagnostic push |
| 75 |
#pragma GCC diagnostic ignored "-Wshift-negative-value" |
| 76 |
#endif // MPT_COMPILER_GCC |
| 77 |
return x << y; |
| 78 |
#if MPT_COMPILER_GCC |
| 79 |
#pragma GCC diagnostic pop |
| 80 |
#endif // MPT_COMPILER_GCC |
| 81 |
} |
| 82 |
|
| 83 |
template <typename T> |
| 84 |
constexpr auto rshift_signed(T x, int y) noexcept -> decltype(x >> y) { |
| 85 |
return mpt::rshift_signed_cxx20(x, y); |
| 86 |
} |
| 87 |
|
| 88 |
template <typename T> |
| 89 |
constexpr auto lshift_signed(T x, int y) noexcept -> decltype(x << y) { |
| 90 |
return mpt::lshift_signed_cxx20(x, y); |
| 91 |
} |
| 92 |
|
| 93 |
#else |
| 94 |
|
| 95 |
template <typename T> |
| 96 |
constexpr auto rshift_signed(T x, int y) noexcept -> decltype(x >> y) { |
| 97 |
return mpt::rshift_signed_portable(x, y); |
| 98 |
} |
| 99 |
|
| 100 |
template <typename T> |
| 101 |
constexpr auto lshift_signed(T x, int y) noexcept -> decltype(x << y) { |
| 102 |
return mpt::lshift_signed_portable(x, y); |
| 103 |
} |
| 104 |
|
| 105 |
#endif |
| 106 |
|
| 107 |
template <typename T> |
| 108 |
constexpr auto arithmetic_shift_right(T x, int y) noexcept -> decltype(x >> y) { |
| 109 |
return mpt::rshift_signed(x, y); |
| 110 |
} |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
constexpr auto arithmetic_shift_right(T x, int y) noexcept -> decltype(x << y) { |
| 114 |
return mpt::lshift_signed(x, y); |
| 115 |
} |
| 116 |
|
| 117 |
template <typename T> |
| 118 |
constexpr auto sar(T x, int y) noexcept -> decltype(x >> y) { |
| 119 |
return mpt::rshift_signed(x, y); |
| 120 |
} |
| 121 |
|
| 122 |
template <typename T> |
| 123 |
constexpr auto sal(T x, int y) noexcept -> decltype(x << y) { |
| 124 |
return mpt::lshift_signed(x, y); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
} // namespace MPT_INLINE_NS |
| 129 |
} // namespace mpt |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#endif // MPT_BASE_ARITHMETIC_SHIFT_HPP |