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 <__assert>
14#include <__config>
15#include <__memory/addressof.h>
16#include <__type_traits/integer_traits.h>
17#include <__utility/cmp.h>
18#include <limits>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29template <class _Tp>
30_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp __saturating_add(_Tp __x, _Tp __y) _NOEXCEPT {
31 static_assert(__is_signed_integer_v<_Tp> || __is_unsigned_integer_v<_Tp>);
32# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
33 return __builtin_elementwise_add_sat(__x, __y);
34# else
35 if (_Tp __sum; !__builtin_add_overflow(__x, __y, std::addressof(__sum)))
36 return __sum;
37 // Handle overflow
38 if _LIBCPP_CONSTEXPR (__is_unsigned_integer_v<_Tp>) {
39 return std::numeric_limits<_Tp>::max();
40 } else {
41 // Signed addition overflow
42 if (__x > 0)
43 // Overflows if (x > 0 && y > 0)
44 return std::numeric_limits<_Tp>::max();
45 else
46 // Overflows if (x < 0 && y < 0)
47 return std::numeric_limits<_Tp>::min();
48 }
49# endif
50}
51
52#if _LIBCPP_STD_VER >= 20
53
54template <__signed_or_unsigned_integer _Tp>
55_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_sub(_Tp __x, _Tp __y) noexcept {
56# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
57 return __builtin_elementwise_sub_sat(__x, __y);
58# else
59 if (_Tp __sub; !__builtin_sub_overflow(__x, __y, std::addressof(__sub)))
60 return __sub;
61 // Handle overflow
62 if constexpr (__unsigned_integer<_Tp>) {
63 // Overflows if (x < y)
64 return std::numeric_limits<_Tp>::min();
65 } else {
66 // Signed subtration overflow
67 if (__x >= 0)
68 // Overflows if (x >= 0 && y < 0)
69 return std::numeric_limits<_Tp>::max();
70 else
71 // Overflows if (x < 0 && y > 0)
72 return std::numeric_limits<_Tp>::min();
73 }
74# endif
75}
76
77template <__signed_or_unsigned_integer _Tp>
78_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_mul(_Tp __x, _Tp __y) noexcept {
79 if (_Tp __mul; !__builtin_mul_overflow(__x, __y, std::addressof(__mul)))
80 return __mul;
81 // Handle overflow
82 if constexpr (__unsigned_integer<_Tp>) {
83 return std::numeric_limits<_Tp>::max();
84 } else {
85 // Signed multiplication overflow
86 if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0))
87 return std::numeric_limits<_Tp>::max();
88 // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0)
89 return std::numeric_limits<_Tp>::min();
90 }
91}
92
93template <__signed_or_unsigned_integer _Tp>
94_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_div(_Tp __x, _Tp __y) noexcept {
95 _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
96 if constexpr (__unsigned_integer<_Tp>) {
97 return __x / __y;
98 } else {
99 // Handle signed division overflow
100 if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1})
101 return std::numeric_limits<_Tp>::max();
102 return __x / __y;
103 }
104}
105
106template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
107_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturating_cast(_Tp __x) noexcept {
108 // Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
109 // optimized out by the compiler.
110
111 // Handle overflow
112 if (std::cmp_less(__x, std::numeric_limits<_Rp>::min()))
113 return std::numeric_limits<_Rp>::min();
114 if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max()))
115 return std::numeric_limits<_Rp>::max();
116 // No overflow
117 return static_cast<_Rp>(__x);
118}
119
120#endif // _LIBCPP_STD_VER >= 20
121
122#if _LIBCPP_STD_VER >= 26
123
124template <__signed_or_unsigned_integer _Tp>
125[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_add(_Tp __x, _Tp __y) noexcept {
126 return std::__saturating_add(__x, __y);
127}
128
129template <__signed_or_unsigned_integer _Tp>
130[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_sub(_Tp __x, _Tp __y) noexcept {
131 return std::__saturating_sub(__x, __y);
132}
133
134template <__signed_or_unsigned_integer _Tp>
135[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_mul(_Tp __x, _Tp __y) noexcept {
136 return std::__saturating_mul(__x, __y);
137}
138
139template <__signed_or_unsigned_integer _Tp>
140[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_div(_Tp __x, _Tp __y) noexcept {
141 return std::__saturating_div(__x, __y);
142}
143
144template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
145[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturating_cast(_Tp __x) noexcept {
146 return std::__saturating_cast<_Rp>(__x);
147}
148
149#endif // _LIBCPP_STD_VER >= 26
150
151_LIBCPP_END_NAMESPACE_STD
152
153_LIBCPP_POP_MACROS
154
155#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
156