30#ifndef _GLIBCXX_SAT_ARITH_H
31#define _GLIBCXX_SAT_ARITH_H 1
34#pragma GCC system_header
39#ifdef __glibcxx_saturation_arithmetic
44namespace std _GLIBCXX_VISIBILITY(default)
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
49 template<
typename _Tp>
requires __is_signed_or_unsigned_integer<_Tp>::value
54 if (!__builtin_add_overflow(__x, __y, &__z))
56 if constexpr (is_unsigned_v<_Tp>)
57 return __gnu_cxx::__int_traits<_Tp>::__max;
59 return __gnu_cxx::__int_traits<_Tp>::__min;
61 return __gnu_cxx::__int_traits<_Tp>::__max;
65 template<
typename _Tp>
requires __is_signed_or_unsigned_integer<_Tp>::value
70 if (!__builtin_sub_overflow(__x, __y, &__z))
72 if constexpr (is_unsigned_v<_Tp>)
73 return __gnu_cxx::__int_traits<_Tp>::__min;
75 return __gnu_cxx::__int_traits<_Tp>::__min;
77 return __gnu_cxx::__int_traits<_Tp>::__max;
81 template<
typename _Tp>
requires __is_signed_or_unsigned_integer<_Tp>::value
86 if (!__builtin_mul_overflow(__x, __y, &__z))
88 if constexpr (is_unsigned_v<_Tp>)
89 return __gnu_cxx::__int_traits<_Tp>::__max;
90 else if ((__x < 0) != (__y < 0))
91 return __gnu_cxx::__int_traits<_Tp>::__min;
93 return __gnu_cxx::__int_traits<_Tp>::__max;
97 template<
typename _Tp>
requires __is_signed_or_unsigned_integer<_Tp>::value
101 __glibcxx_assert(__y != 0);
102 if constexpr (is_signed_v<_Tp>)
103 if (__x == __gnu_cxx::__int_traits<_Tp>::__min && __y == _Tp(-1))
104 return __gnu_cxx::__int_traits<_Tp>::__max;
109 template<
typename _Res,
typename _Tp>
110 requires __is_signed_or_unsigned_integer<_Res>::value
111 && __is_signed_or_unsigned_integer<_Tp>::value
115 constexpr int __digits_R = __gnu_cxx::__int_traits<_Res>::__digits;
116 constexpr int __digits_T = __gnu_cxx::__int_traits<_Tp>::__digits;
117 constexpr _Res __max_Res = __gnu_cxx::__int_traits<_Res>::__max;
119 if constexpr (is_signed_v<_Res> == is_signed_v<_Tp>)
121 if constexpr (__digits_R < __digits_T)
123 constexpr _Res __min_Res = __gnu_cxx::__int_traits<_Res>::__min;
125 if (__x <
static_cast<_Tp
>(__min_Res))
127 else if (__x >
static_cast<_Tp
>(__max_Res))
131 else if constexpr (is_signed_v<_Tp>)
136 return __gnu_cxx::__int_traits<_Res>::__max;
143 return static_cast<_Res
>(__x);
146_GLIBCXX_END_NAMESPACE_VERSION
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
ISO C++ entities toplevel namespace is std.
constexpr _Tp saturating_mul(_Tp __x, _Tp __y) noexcept
Multiply two integers, with saturation in case of overflow.
constexpr _Tp saturating_add(_Tp __x, _Tp __y) noexcept
Add two integers, with saturation in case of overflow.
constexpr _Tp saturating_div(_Tp __x, _Tp __y) noexcept
Divide one integer by another, with saturation in case of overflow.
constexpr _Res saturating_cast(_Tp __x) noexcept
Divide one integer by another, with saturation in case of overflow.
constexpr _Tp saturating_sub(_Tp __x, _Tp __y) noexcept
Subtract one integer from another, with saturation in case of overflow.