Skip to content

Commit 77f45c0

Browse files
committed
[libc++] Makes saturation functions privately available.
These functions are useful in the implementation of the time zone database. So expose them with private names. The functions could be exposed before C++ 20, but since libc++ is mostly C++ 17 complete it seems less useful to allow earlier.
1 parent 4e9decf commit 77f45c0

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

libcxx/include/__numeric/saturation_arithmetic.h

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ _LIBCPP_PUSH_MACROS
2525

2626
_LIBCPP_BEGIN_NAMESPACE_STD
2727

28-
#if _LIBCPP_STD_VER >= 26
28+
#if _LIBCPP_STD_VER >= 20
2929

3030
template <__libcpp_integer _Tp>
31-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
31+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
3232
if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum))
3333
return __sum;
3434
// Handle overflow
@@ -46,7 +46,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
4646
}
4747

4848
template <__libcpp_integer _Tp>
49-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
49+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
5050
if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub))
5151
return __sub;
5252
// Handle overflow
@@ -65,7 +65,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
6565
}
6666

6767
template <__libcpp_integer _Tp>
68-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
68+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
6969
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul))
7070
return __mul;
7171
// Handle overflow
@@ -81,7 +81,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
8181
}
8282

8383
template <__libcpp_integer _Tp>
84-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
84+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {
8585
_LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
8686
if constexpr (__libcpp_unsigned_integer<_Tp>) {
8787
return __x / __y;
@@ -94,7 +94,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
9494
}
9595

9696
template <__libcpp_integer _Rp, __libcpp_integer _Tp>
97-
_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
97+
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
9898
// Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
9999
// optimized out by the compiler.
100100

@@ -107,6 +107,35 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
107107
return static_cast<_Rp>(__x);
108108
}
109109

110+
#endif // _LIBCPP_STD_VER >= 20
111+
112+
#if _LIBCPP_STD_VER >= 26
113+
114+
template <__libcpp_integer _Tp>
115+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
116+
return std::add_sat(__x, __y);
117+
}
118+
119+
template <__libcpp_integer _Tp>
120+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
121+
return std::sub_sat(__x, __y);
122+
}
123+
124+
template <__libcpp_integer _Tp>
125+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
126+
return std::mul_sat(__x, __y);
127+
}
128+
129+
template <__libcpp_integer _Tp>
130+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
131+
return std::div_sat(__x, __y);
132+
}
133+
134+
template <__libcpp_integer _Rp, __libcpp_integer _Tp>
135+
_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
136+
return std::saturate_cast<_Rp>(__x);
137+
}
138+
110139
#endif // _LIBCPP_STD_VER >= 26
111140

112141
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)