30 #ifndef _GLIBCXX_SAT_ARITH_H 31 #define _GLIBCXX_SAT_ARITH_H 1 33 #pragma GCC system_header 37 #ifdef __glibcxx_saturation_arithmetic // C++ >= 26 42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<
typename _Tp> requires __is_standard_integer<_Tp>::value
49 add_sat(_Tp __x, _Tp __y) noexcept
52 if (!__builtin_add_overflow(__x, __y, &__z))
54 if constexpr (is_unsigned_v<_Tp>)
63 template<
typename _Tp> requires __is_standard_integer<_Tp>::value
65 sub_sat(_Tp __x, _Tp __y) noexcept
68 if (!__builtin_sub_overflow(__x, __y, &__z))
70 if constexpr (is_unsigned_v<_Tp>)
79 template<
typename _Tp> requires __is_standard_integer<_Tp>::value
81 mul_sat(_Tp __x, _Tp __y) noexcept
84 if (!__builtin_mul_overflow(__x, __y, &__z))
86 if constexpr (is_unsigned_v<_Tp>)
88 else if (__x < 0 != __y < 0)
95 template<
typename _Tp> requires __is_standard_integer<_Tp>::value
97 div_sat(_Tp __x, _Tp __y) noexcept
99 __glibcxx_assert(__y != 0);
100 if constexpr (is_signed_v<_Tp>)
107 template<
typename _Res,
typename _Tp>
108 requires __is_standard_integer<_Res>::value
109 && __is_standard_integer<_Tp>::value
111 saturate_cast(_Tp __x) noexcept
117 if constexpr (is_signed_v<_Res> == is_signed_v<_Tp>)
119 if constexpr (__digits_R < __digits_T)
123 if (__x < static_cast<_Tp>(__min_Res))
125 else if (__x > static_cast<_Tp>(__max_Res))
129 else if constexpr (is_signed_v<_Tp>)
133 else if (make_unsigned_t<_Tp>(__x) > __max_Res)
138 if (__x > make_unsigned_t<_Res>(__max_Res))
141 return static_cast<_Res
>(__x);
144 _GLIBCXX_END_NAMESPACE_VERSION
147 #endif // __glibcxx_saturation_arithmetic ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.