| 1 |
// { dg-do compile { target c++26 } } |
| 2 |
|
| 3 |
// C++26 Saturation arithmetic [numerics.sat] |
| 4 |
|
| 5 |
#include <numeric> |
| 6 |
#include <climits> |
| 7 |
|
| 8 |
template<typename T, typename U> |
| 9 |
concept can_div_sat |
| 10 |
= requires(T t, U u) { { std::div_sat(t, u) } -> std::same_as<T>; }; |
| 11 |
|
| 12 |
static_assert( can_div_sat<int, int> ); |
| 13 |
static_assert( not can_div_sat<int, short> ); |
| 14 |
static_assert( not can_div_sat<unsigned, int> ); |
| 15 |
static_assert( noexcept(std::div_sat(0, 1)) ); |
| 16 |
|
| 17 |
using std::div_sat; |
| 18 |
|
| 19 |
static_assert(std::div_sat(0, 1) == 0); |
| 20 |
static_assert(std::div_sat(0, -1) == 0); |
| 21 |
static_assert(std::div_sat(1, -1) == -1); |
| 22 |
static_assert(std::div_sat(10, -2) == -5); |
| 23 |
static_assert(std::div_sat(-10, -2) == 5); |
| 24 |
static_assert(std::div_sat(INT_MAX, 1) == INT_MAX); |
| 25 |
static_assert(std::div_sat(INT_MIN, 1) == INT_MIN); |
| 26 |
static_assert(std::div_sat(INT_MIN + 1, -1) == INT_MAX); |
| 27 |
static_assert(std::div_sat(0u, 1u) == 0u); |
| 28 |
static_assert(std::div_sat(UINT_MAX, 1u) == UINT_MAX); |
| 29 |
static_assert(std::div_sat(INT_MIN, -1) == INT_MAX); |
| 30 |
static_assert(std::div_sat((short)SHRT_MIN, (short)-1) == SHRT_MAX); |
| 31 |
static_assert(std::div_sat(LONG_MIN, -1L) == LONG_MAX); |
| 32 |
static_assert(std::div_sat(LLONG_MIN, -1LL) == LLONG_MAX); |
| 33 |
|
| 34 |
template<auto N> |
| 35 |
std::integral_constant<decltype(N), std::div_sat(N, N-N)> |
| 36 |
div_sat_by_zero(); |
| 37 |
|
| 38 |
template<auto N> |
| 39 |
concept can_div_sat_by_zero = requires { div_sat_by_zero<N>(); }; |
| 40 |
|
| 41 |
static_assert( not can_div_sat_by_zero<0> ); |
| 42 |
static_assert( not can_div_sat_by_zero<1> ); |
| 43 |
static_assert( not can_div_sat_by_zero<1u> ); |
| 44 |
static_assert( not can_div_sat_by_zero<-1L> ); |
| 45 |
static_assert( not can_div_sat_by_zero<short(99)> ); |