
This was approved for C++26 last week at the WG21 meeting in Kona. libstdc++-v3/ChangeLog: * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/bits/version.def (saturation_arithmetic): Define. * include/bits/version.h: Regenerate. * include/std/numeric: Include new header. * include/bits/sat_arith.h: New file. * testsuite/26_numerics/saturation/add.cc: New test. * testsuite/26_numerics/saturation/cast.cc: New test. * testsuite/26_numerics/saturation/div.cc: New test. * testsuite/26_numerics/saturation/mul.cc: New test. * testsuite/26_numerics/saturation/sub.cc: New test. * testsuite/26_numerics/saturation/version.cc: New test.
45 lines
1.5 KiB
C++
45 lines
1.5 KiB
C++
// { dg-do compile { target c++26 } }
|
|
|
|
// C++26 Saturation arithmetic [numerics.sat]
|
|
|
|
#include <numeric>
|
|
#include <climits>
|
|
|
|
template<typename T, typename U>
|
|
concept can_div_sat
|
|
= requires(T t, U u) { { std::div_sat(t, u) } -> std::same_as<T>; };
|
|
|
|
static_assert( can_div_sat<int, int> );
|
|
static_assert( not can_div_sat<int, short> );
|
|
static_assert( not can_div_sat<unsigned, int> );
|
|
static_assert( noexcept(std::div_sat(0, 1)) );
|
|
|
|
using std::div_sat;
|
|
|
|
static_assert(std::div_sat(0, 1) == 0);
|
|
static_assert(std::div_sat(0, -1) == 0);
|
|
static_assert(std::div_sat(1, -1) == -1);
|
|
static_assert(std::div_sat(10, -2) == -5);
|
|
static_assert(std::div_sat(-10, -2) == 5);
|
|
static_assert(std::div_sat(INT_MAX, 1) == INT_MAX);
|
|
static_assert(std::div_sat(INT_MIN, 1) == INT_MIN);
|
|
static_assert(std::div_sat(INT_MIN + 1, -1) == INT_MAX);
|
|
static_assert(std::div_sat(0u, 1u) == 0u);
|
|
static_assert(std::div_sat(UINT_MAX, 1u) == UINT_MAX);
|
|
static_assert(std::div_sat(INT_MIN, -1) == INT_MAX);
|
|
static_assert(std::div_sat((short)SHRT_MIN, (short)-1) == SHRT_MAX);
|
|
static_assert(std::div_sat(LONG_MIN, -1L) == LONG_MAX);
|
|
static_assert(std::div_sat(LLONG_MIN, -1LL) == LLONG_MAX);
|
|
|
|
template<auto N>
|
|
std::integral_constant<decltype(N), std::div_sat(N, N-N)>
|
|
div_sat_by_zero();
|
|
|
|
template<auto N>
|
|
concept can_div_sat_by_zero = requires { div_sat_by_zero<N>(); };
|
|
|
|
static_assert( not can_div_sat_by_zero<0> );
|
|
static_assert( not can_div_sat_by_zero<1> );
|
|
static_assert( not can_div_sat_by_zero<1u> );
|
|
static_assert( not can_div_sat_by_zero<-1L> );
|
|
static_assert( not can_div_sat_by_zero<short(99)> );
|