|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H |
| 11 | +#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H |
| 12 | + |
| 13 | +#include <__concepts/arithmetic.h> |
| 14 | +#include <__config> |
| 15 | +#include <__utility/cmp.h> |
| 16 | +#include <limits> |
| 17 | + |
| 18 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | +# pragma GCC system_header |
| 20 | +#endif |
| 21 | + |
| 22 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | + |
| 24 | +#if _LIBCPP_STD_VER >= 26 |
| 25 | + |
| 26 | +template <__libcpp_integer _Tp> |
| 27 | +_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept { |
| 28 | + if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum)) |
| 29 | + return __sum; |
| 30 | + // Handle overflow |
| 31 | + if constexpr (__libcpp_unsigned_integer<_Tp>) { |
| 32 | + return std::numeric_limits<_Tp>::max(); |
| 33 | + } else { |
| 34 | + // Signed addition overflow |
| 35 | + if (__x > 0) |
| 36 | + // Overflows if (x > 0 && y > 0) |
| 37 | + return std::numeric_limits<_Tp>::max(); |
| 38 | + else |
| 39 | + // Overflows if (x < 0 && y < 0) |
| 40 | + return std::numeric_limits<_Tp>::min(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +template <__libcpp_integer _Tp> |
| 45 | +_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept { |
| 46 | + if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub)) |
| 47 | + return __sub; |
| 48 | + // Handle overflow |
| 49 | + if constexpr (__libcpp_unsigned_integer<_Tp>) { |
| 50 | + // Overflows if (x < y) |
| 51 | + return std::numeric_limits<_Tp>::min(); |
| 52 | + } else { |
| 53 | + // Signed subtration overflow |
| 54 | + if (__x >= 0) |
| 55 | + // Overflows if (x >= 0 && y < 0) |
| 56 | + return std::numeric_limits<_Tp>::max(); |
| 57 | + else |
| 58 | + // Overflows if (x < 0 && y > 0) |
| 59 | + return std::numeric_limits<_Tp>::min(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +template <__libcpp_integer _Tp> |
| 64 | +_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept { |
| 65 | + if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul)) |
| 66 | + return __mul; |
| 67 | + // Handle overflow |
| 68 | + if constexpr (__libcpp_unsigned_integer<_Tp>) { |
| 69 | + return std::numeric_limits<_Tp>::max(); |
| 70 | + } else { |
| 71 | + // Signed multiplication overflow |
| 72 | + if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0)) |
| 73 | + return std::numeric_limits<_Tp>::max(); |
| 74 | + // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0) |
| 75 | + return std::numeric_limits<_Tp>::min(); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +template <__libcpp_integer _Tp> |
| 80 | +_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept { |
| 81 | + _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined"); |
| 82 | + if constexpr (__libcpp_unsigned_integer<_Tp>) { |
| 83 | + return __x / __y; |
| 84 | + } else { |
| 85 | + // Handle signed division overflow |
| 86 | + if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1}) |
| 87 | + return std::numeric_limits<_Tp>::max(); |
| 88 | + return __x / __y; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +template <__libcpp_integer _Rp, __libcpp_integer _Tp> |
| 93 | +_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept { |
| 94 | + // Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be |
| 95 | + // optimized out by the compiler. |
| 96 | + |
| 97 | + // Handle overflow |
| 98 | + if (std::cmp_less(__x, std::numeric_limits<_Rp>::min())) |
| 99 | + return std::numeric_limits<_Rp>::min(); |
| 100 | + if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max())) |
| 101 | + return std::numeric_limits<_Rp>::max(); |
| 102 | + // No overflow |
| 103 | + return static_cast<_Rp>(__x); |
| 104 | +} |
| 105 | + |
| 106 | +#endif // _LIBCPP_STD_VER >= 26 |
| 107 | + |
| 108 | +_LIBCPP_END_NAMESPACE_STD |
| 109 | + |
| 110 | +#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H |
0 commit comments