Skip to content

Commit 782cb4b

Browse files
committed
[libc++][numeric] P4052R0: Renaming saturation arithmetic functions
Also renames the internal version for consistency. - cplusplus/draft#8877 - https://isocpp.org/files/papers/P4052R0.html
1 parent acc609b commit 782cb4b

21 files changed

Lines changed: 510 additions & 509 deletions

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ Status
508508
---------------------------------------------------------- -----------------
509509
``__cpp_lib_reference_wrapper`` ``202403L``
510510
---------------------------------------------------------- -----------------
511-
``__cpp_lib_saturation_arithmetic`` ``202311L``
511+
``__cpp_lib_saturation_arithmetic`` ``202603L``
512512
---------------------------------------------------------- -----------------
513513
``__cpp_lib_senders`` *unimplemented*
514514
---------------------------------------------------------- -----------------

libcxx/docs/ReleaseNotes/23.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Implemented Papers
3939
------------------
4040

4141
- P2440R1: ``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right`` (`Github <https://llvm.org/PR105184>`__)
42+
- P4052R0: Renaming saturation arithmetic functions
4243

4344
Improvements and New Features
4445
-----------------------------

libcxx/include/__numeric/saturation_arithmetic.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
#if _LIBCPP_STD_VER >= 20
3030

3131
template <__signed_or_unsigned_integer _Tp>
32-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
32+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_add(_Tp __x, _Tp __y) noexcept {
3333
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
3434
return __builtin_elementwise_add_sat(__x, __y);
3535
# else
@@ -51,7 +51,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
5151
}
5252

5353
template <__signed_or_unsigned_integer _Tp>
54-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
54+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_sub(_Tp __x, _Tp __y) noexcept {
5555
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
5656
return __builtin_elementwise_sub_sat(__x, __y);
5757
# else
@@ -74,7 +74,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
7474
}
7575

7676
template <__signed_or_unsigned_integer _Tp>
77-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
77+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_mul(_Tp __x, _Tp __y) noexcept {
7878
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, std::addressof(__mul)))
7979
return __mul;
8080
// Handle overflow
@@ -90,7 +90,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
9090
}
9191

9292
template <__signed_or_unsigned_integer _Tp>
93-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {
93+
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_div(_Tp __x, _Tp __y) noexcept {
9494
_LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
9595
if constexpr (__unsigned_integer<_Tp>) {
9696
return __x / __y;
@@ -103,7 +103,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {
103103
}
104104

105105
template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
106-
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
106+
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturating_cast(_Tp __x) noexcept {
107107
// Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
108108
// optimized out by the compiler.
109109

@@ -121,28 +121,28 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
121121
#if _LIBCPP_STD_VER >= 26
122122

123123
template <__signed_or_unsigned_integer _Tp>
124-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
125-
return std::__add_sat(__x, __y);
124+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_add(_Tp __x, _Tp __y) noexcept {
125+
return std::__saturating_add(__x, __y);
126126
}
127127

128128
template <__signed_or_unsigned_integer _Tp>
129-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
130-
return std::__sub_sat(__x, __y);
129+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_sub(_Tp __x, _Tp __y) noexcept {
130+
return std::__saturating_sub(__x, __y);
131131
}
132132

133133
template <__signed_or_unsigned_integer _Tp>
134-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
135-
return std::__mul_sat(__x, __y);
134+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_mul(_Tp __x, _Tp __y) noexcept {
135+
return std::__saturating_mul(__x, __y);
136136
}
137137

138138
template <__signed_or_unsigned_integer _Tp>
139-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
140-
return std::__div_sat(__x, __y);
139+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_div(_Tp __x, _Tp __y) noexcept {
140+
return std::__saturating_div(__x, __y);
141141
}
142142

143143
template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
144-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
145-
return std::__saturate_cast<_Rp>(__x);
144+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturating_cast(_Tp __x) noexcept {
145+
return std::__saturating_cast<_Rp>(__x);
146146
}
147147

148148
#endif // _LIBCPP_STD_VER >= 26

libcxx/include/numeric

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ template<class T>
142142
143143
// [numeric.sat], saturation arithmetic
144144
template<class T>
145-
constexpr T add_sat(T x, T y) noexcept; // freestanding, Since C++26
145+
constexpr T saturating_add(T x, T y) noexcept; // freestanding, Since C++26
146146
template<class T>
147-
constexpr T sub_sat(T x, T y) noexcept; // freestanding, Since C++26
147+
constexpr T saturating_sub(T x, T y) noexcept; // freestanding, Since C++26
148148
template<class T>
149-
constexpr T mul_sat(T x, T y) noexcept; // freestanding, Since C++26
149+
constexpr T saturating_mul(T x, T y) noexcept; // freestanding, Since C++26
150150
template<class T>
151-
constexpr T div_sat(T x, T y) noexcept; // freestanding, Since C++26
151+
constexpr T saturating_div(T x, T y) noexcept; // freestanding, Since C++26
152152
template<class T, class U>
153-
constexpr T saturate_cast(U x) noexcept; // freestanding, Since C++26
153+
constexpr T saturating_cast(U x) noexcept; // freestanding, Since C++26
154154
155155
} // std
156156

libcxx/include/version

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ __cpp_lib_remove_cvref 201711L <type_traits>
227227
__cpp_lib_result_of_sfinae 201210L <functional> <type_traits>
228228
__cpp_lib_robust_nonmodifying_seq_ops 201304L <algorithm>
229229
__cpp_lib_sample 201603L <algorithm>
230-
__cpp_lib_saturation_arithmetic 202311L <numeric>
230+
__cpp_lib_saturation_arithmetic 202603L <numeric>
231231
__cpp_lib_scoped_lock 201703L <mutex>
232232
__cpp_lib_semaphore 201907L <semaphore>
233233
__cpp_lib_senders 202406L <execution>
@@ -612,7 +612,7 @@ __cpp_lib_void_t 201411L <type_traits>
612612
# define __cpp_lib_ratio 202306L
613613
// # define __cpp_lib_rcu 202306L
614614
# define __cpp_lib_reference_wrapper 202403L
615-
# define __cpp_lib_saturation_arithmetic 202311L
615+
# define __cpp_lib_saturation_arithmetic 202603L
616616
// # define __cpp_lib_senders 202406L
617617
// # define __cpp_lib_smart_ptr_owner_equality 202306L
618618
# define __cpp_lib_span_at 202311L

libcxx/modules/std/numeric.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export namespace std {
6161

6262
#if _LIBCPP_STD_VER >= 26
6363
// [numeric.sat], saturation arithmetic
64-
using std::add_sat;
65-
using std::div_sat;
66-
using std::mul_sat;
67-
using std::saturate_cast;
68-
using std::sub_sat;
64+
using std::saturating_add;
65+
using std::saturating_cast;
66+
using std::saturating_div;
67+
using std::saturating_mul;
68+
using std::saturating_sub;
6969
#endif
7070

7171
} // namespace std

libcxx/test/libcxx/numerics/nodiscard.verify.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ void test() {
2525
// clang-format off
2626
#if TEST_STD_VER >= 26
2727
// [numeric.sat]
28-
std::add_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29-
std::sub_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30-
std::mul_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31-
std::div_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
32-
std::saturate_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
std::saturating_add(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
std::saturating_sub(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
std::saturating_mul(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
std::saturating_div(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
std::saturating_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3333
#endif // TEST_STD_VER >= 26
3434
// clang-format on
3535

libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@
245245
# ifndef __cpp_lib_saturation_arithmetic
246246
# error "__cpp_lib_saturation_arithmetic should be defined in c++26"
247247
# endif
248-
# if __cpp_lib_saturation_arithmetic != 202311L
249-
# error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
248+
# if __cpp_lib_saturation_arithmetic != 202603L
249+
# error "__cpp_lib_saturation_arithmetic should have the value 202603L in c++26"
250250
# endif
251251

252252
#endif // TEST_STD_VER > 23

libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7782,8 +7782,8 @@
77827782
# ifndef __cpp_lib_saturation_arithmetic
77837783
# error "__cpp_lib_saturation_arithmetic should be defined in c++26"
77847784
# endif
7785-
# if __cpp_lib_saturation_arithmetic != 202311L
7786-
# error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
7785+
# if __cpp_lib_saturation_arithmetic != 202603L
7786+
# error "__cpp_lib_saturation_arithmetic should have the value 202603L in c++26"
77877787
# endif
77887788

77897789
# if !defined(_LIBCPP_VERSION) || _LIBCPP_HAS_THREADS

libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// <numeric>
1212

1313
// template<class T>
14-
// constexpr T add_sat(T x, T y) noexcept; // freestanding
14+
// constexpr T saturating_add(T x, T y) noexcept; // freestanding
1515

1616
#include <concepts>
1717
#include <numeric>
@@ -20,7 +20,7 @@
2020

2121
template <typename T, typename U>
2222
concept CanDo = requires(T x, U y) {
23-
{ std::add_sat(x, y) } -> std::same_as<T>;
23+
{ std::saturating_add(x, y) } -> std::same_as<T>;
2424
};
2525

2626
template <typename T, typename U>

0 commit comments

Comments
 (0)