libstdc++: Define C++26 saturation arithmetic functions (P0543R3)
[official-gcc.git] / libstdc++-v3 / testsuite / 26_numerics / saturation / mul.cc
blob1b5538c9af3b917ba273ff8d637494f0ec8e013a
1 // { dg-do compile { target c++26 } }
3 // C++26 Saturation arithmetic [numerics.sat]
5 #include <numeric>
6 #include <climits>
8 template<typename T, typename U>
9 concept can_mul_sat
10 = requires(T t, U u) { { std::mul_sat(t, u) } -> std::same_as<T>; };
12 static_assert( can_mul_sat<int, int> );
13 static_assert( not can_mul_sat<int, short> );
14 static_assert( not can_mul_sat<unsigned, int> );
15 static_assert( noexcept(std::mul_sat(0, 0)) );
17 using std::mul_sat;
19 static_assert(mul_sat(1, 1) == 1);
20 static_assert(mul_sat(10, 11) == 110);
21 static_assert(mul_sat(INT_MAX / 2, 3) == INT_MAX);
22 static_assert(mul_sat(INT_MAX / 2, -3) == INT_MIN);
23 static_assert(mul_sat(INT_MAX / -2, 3) == INT_MIN);
24 static_assert(mul_sat(INT_MIN / 2, -3) == INT_MAX);
25 static_assert(mul_sat(INT_MIN, -1) == INT_MAX);
26 static_assert(mul_sat(INT_MAX, -1) == INT_MIN + 1);
27 static_assert(mul_sat(INT_MAX, INT_MAX) == INT_MAX);
28 static_assert(mul_sat(INT_MAX, -INT_MAX) == INT_MIN);
29 static_assert(mul_sat(UINT_MAX, UINT_MAX) == UINT_MAX);
30 static_assert(mul_sat(UINT_MAX, 0u) == 0);
31 static_assert(mul_sat(0u, UINT_MAX) == 0);
32 static_assert(mul_sat((short)SHRT_MAX, (short)2) == SHRT_MAX);
33 static_assert(mul_sat((short)SHRT_MAX, (short)SHRT_MIN) == SHRT_MIN);
34 static_assert(mul_sat<long long>(SHRT_MAX, 2) == 2L * SHRT_MAX);