Skip to content

Commit 24c4b48

Browse files
authored
Merge 2023-11 LWG Motion 2
P0543R3 Saturation arithmetic
2 parents 7805874 + 8f1b3c5 commit 24c4b48

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

source/algorithms.tex

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9621,6 +9621,18 @@
96219621
constexpr T midpoint(T a, T b) noexcept;
96229622
template<class T>
96239623
constexpr T* midpoint(T* a, T* b);
9624+
9625+
// \ref{numeric.sat}, saturation arithmetic
9626+
template<class T>
9627+
constexpr T add_sat(T x, T y) noexcept; // freestanding
9628+
template<class T>
9629+
constexpr T sub_sat(T x, T y) noexcept; // freestanding
9630+
template<class T>
9631+
constexpr T mul_sat(T x, T y) noexcept; // freestanding
9632+
template<class T>
9633+
constexpr T div_sat(T x, T y) noexcept; // freestanding
9634+
template<class T, class U>
9635+
constexpr T saturate_cast(U x) noexcept; // freestanding
96249636
}
96259637
\end{codeblock}
96269638

@@ -10755,6 +10767,119 @@
1075510767
where the result of the division is truncated towards zero.
1075610768
\end{itemdescr}
1075710769

10770+
\rSec2[numeric.sat]{Saturation arithmetic}
10771+
10772+
\rSec3[numeric.sat.func]{Arithmetic functions}
10773+
10774+
\pnum
10775+
\begin{note}
10776+
In the following descriptions, an arithmetic operation
10777+
is performed as a mathematical operation with infinite range and then
10778+
it is determined whether the mathematical result fits into the result type.
10779+
\end{note}
10780+
10781+
\indexlibraryglobal{add_sat}%
10782+
\begin{itemdecl}
10783+
template<class T>
10784+
constexpr T add_sat(T x, T y) noexcept;
10785+
\end{itemdecl}
10786+
10787+
\begin{itemdescr}
10788+
\pnum
10789+
\constraints
10790+
\tcode{T} is a signed or unsigned integer type\iref{basic.fundamental}.
10791+
10792+
\pnum
10793+
\returns
10794+
If $\tcode{x} + \tcode{y}$ is representable as a value of type \tcode{T}, $\tcode{x} + \tcode{y}$;
10795+
otherwise, either the largest or smallest representable value of type \tcode{T},
10796+
whichever is closer to the value of $\tcode{x} + \tcode{y}$.
10797+
\end{itemdescr}
10798+
10799+
\indexlibraryglobal{sub_sat}%
10800+
\begin{itemdecl}
10801+
template<class T>
10802+
constexpr T sub_sat(T x, T y) noexcept;
10803+
\end{itemdecl}
10804+
10805+
\begin{itemdescr}
10806+
\pnum
10807+
\constraints
10808+
\tcode{T} is a signed or unsigned integer type\iref{basic.fundamental}.
10809+
10810+
\pnum
10811+
\returns
10812+
If $\tcode{x} - \tcode{y}$ is representable as a value of type \tcode{T}, $\tcode{x} - \tcode{y}$;
10813+
otherwise, either the largest or smallest representable value of type \tcode{T},
10814+
whichever is closer to the value of $\tcode{x} - \tcode{y}$.
10815+
\end{itemdescr}
10816+
10817+
\indexlibraryglobal{mul_sat}%
10818+
\begin{itemdecl}
10819+
template<class T>
10820+
constexpr T mul_sat(T x, T y) noexcept;
10821+
\end{itemdecl}
10822+
10823+
\begin{itemdescr}
10824+
\pnum
10825+
\constraints
10826+
\tcode{T} is a signed or unsigned integer type\iref{basic.fundamental}.
10827+
10828+
\pnum
10829+
\returns
10830+
If $\tcode{x} \times \tcode{y}$ is representable as a value of type \tcode{T}, $\tcode{x} \times \tcode{y}$;
10831+
otherwise, either the largest or smallest representable value of type \tcode{T},
10832+
whichever is closer to the value of $\tcode{x} \times \tcode{y}$.
10833+
\end{itemdescr}
10834+
10835+
\indexlibraryglobal{div_sat}%
10836+
\begin{itemdecl}
10837+
template<class T>
10838+
constexpr T div_sat(T x, T y) noexcept;
10839+
\end{itemdecl}
10840+
10841+
\begin{itemdescr}
10842+
\pnum
10843+
\constraints
10844+
\tcode{T} is a signed or unsigned integer type\iref{basic.fundamental}.
10845+
10846+
\pnum
10847+
\expects
10848+
\tcode{y != 0} is \tcode{true}.
10849+
10850+
\pnum
10851+
\returns
10852+
If \tcode{T} is a signed integer type
10853+
and \tcode{x == numeric_limits<T>::min() \&\& y == -1} is \tcode{true},
10854+
\tcode{numeric_limits<T>::max()}, otherwise, \tcode{x / y}.
10855+
10856+
\pnum
10857+
\remarks
10858+
A function call expression
10859+
that violates the precondition in the \Fundescx{Preconditions} element
10860+
is not a core constant expression\iref{expr.const}.
10861+
\end{itemdescr}
10862+
10863+
\rSec3[numeric.sat.cast]{Casting}
10864+
10865+
\indexlibraryglobal{saturate_cast}%
10866+
\begin{itemdecl}
10867+
template<class R, class T>
10868+
constexpr R saturate_cast(T x) noexcept;
10869+
\end{itemdecl}
10870+
10871+
\begin{itemdescr}
10872+
\pnum
10873+
\constraints
10874+
\tcode{R} and \tcode{T} are signed or unsigned integer types\iref{basic.fundamental}.
10875+
10876+
\pnum
10877+
\returns
10878+
If \tcode{x} is representable as a value of type \tcode{R}, \tcode{x};
10879+
otherwise, either the largest or smallest representable value of type \tcode{R},
10880+
whichever is closer to the value of \tcode{x}.
10881+
\end{itemdescr}
10882+
1075810883
\rSec1[specialized.algorithms]{Specialized \tcode{<memory>} algorithms}
1075910884

1076010885
\rSec2[specialized.algorithms.general]{General}

source/lib-intro.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@
14921492
\ref{c.strings} & Null-terminated sequence utilities & \tcode{<cstring>}, \tcode{<cwchar>} \\ \rowsep
14931493
\ref{iterators} & Iterators library & \tcode{<iterator>} \\ \rowsep
14941494
\ref{ranges} & Ranges library & \tcode{<ranges>} \\ \rowsep
1495+
\ref{algorithms} & Algorithms library & \tcode{<numeric>} \\ \rowsep
14951496
\ref{c.math} & Mathematical functions for floating-point types & \tcode{<cmath>} \\ \rowsep
14961497
\ref{atomics} & Atomics & \tcode{<atomic>} \\ \rowsep
14971498
\end{libsumtab}

source/support.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@
742742
// freestanding, also in \libheader{functional}, \libheader{type_traits}
743743
#define @\defnlibxname{cpp_lib_robust_nonmodifying_seq_ops}@ 201304L // also in \libheader{algorithm}
744744
#define @\defnlibxname{cpp_lib_sample}@ 201603L // also in \libheader{algorithm}
745+
#define @\defnlibxname{cpp_lib_saturation_arithmetic}@ 202311L // also in \libheader{numeric}
745746
#define @\defnlibxname{cpp_lib_scoped_lock}@ 201703L // also in \libheader{mutex}
746747
#define @\defnlibxname{cpp_lib_semaphore}@ 201907L // also in \libheader{semaphore}
747748
#define @\defnlibxname{cpp_lib_shared_mutex}@ 201505L // also in \libheader{shared_mutex}

0 commit comments

Comments
 (0)