[libc++][numeric] P4052R0: Renaming saturation arithmetic functions (#189574)
Implements P4052R0. Also renames: - the internal names for consistency. - test files (no changes to the contents but the function names). Fixes: #189589 --------- Co-authored-by: A. Jiang <de34@live.cn>
This commit is contained in:
parent
be62f270fd
commit
48ad929c0f
27 changed files with 1109 additions and 1108 deletions
|
|
@ -508,7 +508,7 @@ Status
|
|||
---------------------------------------------------------- -----------------
|
||||
``__cpp_lib_reference_wrapper`` ``202403L``
|
||||
---------------------------------------------------------- -----------------
|
||||
``__cpp_lib_saturation_arithmetic`` ``202311L``
|
||||
``__cpp_lib_saturation_arithmetic`` ``202603L``
|
||||
---------------------------------------------------------- -----------------
|
||||
``__cpp_lib_senders`` *unimplemented*
|
||||
---------------------------------------------------------- -----------------
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ Implemented Papers
|
|||
- P2440R1: ``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right`` (`Github <https://llvm.org/PR105184>`__)
|
||||
- P3936R1: Safer ``atomic_ref::address`` (`Github <https://llvm.org/PR189594>`__)
|
||||
- P3953R3: Rename ``std::runtime_format`` (`Github <https://llvm.org/PR189624>`__)
|
||||
- P4052R0: Renaming saturation arithmetic functions (`Github <https://llvm.org/PR189589>`__)
|
||||
|
||||
Improvements and New Features
|
||||
-----------------------------
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@
|
|||
"`P3980R1 <https://wg21.link/P3980R1>`__","Task's Allocator Use","2026-03 (Croydon)","","","`#189621 <https://github.com/llvm/llvm-project/issues/189621>`__",""
|
||||
"`P4156R0 <https://wg21.link/P4156R0>`__","Rename meta::has_ellipsis_parameter to meta::is_vararg_function","2026-03 (Croydon)","","","`#189622 <https://github.com/llvm/llvm-project/issues/189622>`__",""
|
||||
"`P3953R3 <https://wg21.link/P3953R3>`__","Rename ``std::runtime_format``","2026-03 (Croydon)","|Complete|","23","`#189624 <https://github.com/llvm/llvm-project/issues/189624>`__",""
|
||||
"`P4052R0 <https://wg21.link/P4052R0>`__","Renaming saturation arithmetic functions","2026-03 (Croydon)","","","`#189589 <https://github.com/llvm/llvm-project/issues/189589>`__",""
|
||||
"`P4052R0 <https://wg21.link/P4052R0>`__","Renaming saturation arithmetic functions","2026-03 (Croydon)","|Complete|","23","`#189589 <https://github.com/llvm/llvm-project/issues/189589>`__",""
|
||||
"`P3941R4 <https://wg21.link/P3941R4>`__","Scheduler Affinity","2026-03 (Croydon)","","","`#189627 <https://github.com/llvm/llvm-project/issues/189627>`__",""
|
||||
"`P3856R8 <https://wg21.link/P3856R8>`__","New reflection metafunction - is_structural_type (US NB comment 49)","2026-03 (Croydon)","","","`#189625 <https://github.com/llvm/llvm-project/issues/189625>`__",""
|
||||
"`P3927R2 <https://wg21.link/P3927R2>`__","``task_scheduler`` support for parallel ``bulk`` execution","2026-03 (Croydon)","","","`#189629 <https://github.com/llvm/llvm-project/issues/189629>`__",""
|
||||
|
|
|
|||
|
|
|
@ -29,7 +29,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
|
|||
#if _LIBCPP_STD_VER >= 20
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_add(_Tp __x, _Tp __y) noexcept {
|
||||
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
|
||||
return __builtin_elementwise_add_sat(__x, __y);
|
||||
# else
|
||||
|
|
@ -51,7 +51,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
|
|||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_sub(_Tp __x, _Tp __y) noexcept {
|
||||
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
|
||||
return __builtin_elementwise_sub_sat(__x, __y);
|
||||
# else
|
||||
|
|
@ -74,7 +74,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
|
|||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_mul(_Tp __x, _Tp __y) noexcept {
|
||||
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, std::addressof(__mul)))
|
||||
return __mul;
|
||||
// Handle overflow
|
||||
|
|
@ -90,7 +90,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
|
|||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __saturating_div(_Tp __x, _Tp __y) noexcept {
|
||||
_LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
|
||||
if constexpr (__unsigned_integer<_Tp>) {
|
||||
return __x / __y;
|
||||
|
|
@ -103,7 +103,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {
|
|||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturating_cast(_Tp __x) noexcept {
|
||||
// Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
|
||||
// optimized out by the compiler.
|
||||
|
||||
|
|
@ -121,28 +121,28 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
|
|||
#if _LIBCPP_STD_VER >= 26
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__add_sat(__x, __y);
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_add(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__saturating_add(__x, __y);
|
||||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__sub_sat(__x, __y);
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_sub(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__saturating_sub(__x, __y);
|
||||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__mul_sat(__x, __y);
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_mul(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__saturating_mul(__x, __y);
|
||||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Tp>
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__div_sat(__x, __y);
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp saturating_div(_Tp __x, _Tp __y) noexcept {
|
||||
return std::__saturating_div(__x, __y);
|
||||
}
|
||||
|
||||
template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
|
||||
return std::__saturate_cast<_Rp>(__x);
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturating_cast(_Tp __x) noexcept {
|
||||
return std::__saturating_cast<_Rp>(__x);
|
||||
}
|
||||
|
||||
#endif // _LIBCPP_STD_VER >= 26
|
||||
|
|
|
|||
|
|
@ -142,15 +142,15 @@ template<class T>
|
|||
|
||||
// [numeric.sat], saturation arithmetic
|
||||
template<class T>
|
||||
constexpr T add_sat(T x, T y) noexcept; // freestanding, Since C++26
|
||||
constexpr T saturating_add(T x, T y) noexcept; // freestanding, Since C++26
|
||||
template<class T>
|
||||
constexpr T sub_sat(T x, T y) noexcept; // freestanding, Since C++26
|
||||
constexpr T saturating_sub(T x, T y) noexcept; // freestanding, Since C++26
|
||||
template<class T>
|
||||
constexpr T mul_sat(T x, T y) noexcept; // freestanding, Since C++26
|
||||
constexpr T saturating_mul(T x, T y) noexcept; // freestanding, Since C++26
|
||||
template<class T>
|
||||
constexpr T div_sat(T x, T y) noexcept; // freestanding, Since C++26
|
||||
constexpr T saturating_div(T x, T y) noexcept; // freestanding, Since C++26
|
||||
template<class T, class U>
|
||||
constexpr T saturate_cast(U x) noexcept; // freestanding, Since C++26
|
||||
constexpr T saturating_cast(U x) noexcept; // freestanding, Since C++26
|
||||
|
||||
} // std
|
||||
|
||||
|
|
|
|||
|
|
@ -227,7 +227,7 @@ __cpp_lib_remove_cvref 201711L <type_traits>
|
|||
__cpp_lib_result_of_sfinae 201210L <functional> <type_traits>
|
||||
__cpp_lib_robust_nonmodifying_seq_ops 201304L <algorithm>
|
||||
__cpp_lib_sample 201603L <algorithm>
|
||||
__cpp_lib_saturation_arithmetic 202311L <numeric>
|
||||
__cpp_lib_saturation_arithmetic 202603L <numeric>
|
||||
__cpp_lib_scoped_lock 201703L <mutex>
|
||||
__cpp_lib_semaphore 201907L <semaphore>
|
||||
__cpp_lib_senders 202406L <execution>
|
||||
|
|
@ -612,7 +612,7 @@ __cpp_lib_void_t 201411L <type_traits>
|
|||
# define __cpp_lib_ratio 202306L
|
||||
// # define __cpp_lib_rcu 202306L
|
||||
# define __cpp_lib_reference_wrapper 202403L
|
||||
# define __cpp_lib_saturation_arithmetic 202311L
|
||||
# define __cpp_lib_saturation_arithmetic 202603L
|
||||
// # define __cpp_lib_senders 202406L
|
||||
// # define __cpp_lib_smart_ptr_owner_equality 202306L
|
||||
# define __cpp_lib_span_at 202311L
|
||||
|
|
|
|||
|
|
@ -61,11 +61,11 @@ export namespace std {
|
|||
|
||||
#if _LIBCPP_STD_VER >= 26
|
||||
// [numeric.sat], saturation arithmetic
|
||||
using std::add_sat;
|
||||
using std::div_sat;
|
||||
using std::mul_sat;
|
||||
using std::saturate_cast;
|
||||
using std::sub_sat;
|
||||
using std::saturating_add;
|
||||
using std::saturating_cast;
|
||||
using std::saturating_div;
|
||||
using std::saturating_mul;
|
||||
using std::saturating_sub;
|
||||
#endif
|
||||
|
||||
} // namespace std
|
||||
|
|
|
|||
|
|
@ -25,11 +25,11 @@ void test() {
|
|||
// clang-format off
|
||||
#if TEST_STD_VER >= 26
|
||||
// [numeric.sat]
|
||||
std::add_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::sub_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::mul_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::div_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturate_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturating_add(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturating_sub(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturating_mul(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturating_div(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::saturating_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
#endif // TEST_STD_VER >= 26
|
||||
// clang-format on
|
||||
|
||||
|
|
|
|||
|
|
@ -245,8 +245,8 @@
|
|||
# ifndef __cpp_lib_saturation_arithmetic
|
||||
# error "__cpp_lib_saturation_arithmetic should be defined in c++26"
|
||||
# endif
|
||||
# if __cpp_lib_saturation_arithmetic != 202311L
|
||||
# error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
|
||||
# if __cpp_lib_saturation_arithmetic != 202603L
|
||||
# error "__cpp_lib_saturation_arithmetic should have the value 202603L in c++26"
|
||||
# endif
|
||||
|
||||
#endif // TEST_STD_VER > 23
|
||||
|
|
|
|||
|
|
@ -7782,8 +7782,8 @@
|
|||
# ifndef __cpp_lib_saturation_arithmetic
|
||||
# error "__cpp_lib_saturation_arithmetic should be defined in c++26"
|
||||
# endif
|
||||
# if __cpp_lib_saturation_arithmetic != 202311L
|
||||
# error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
|
||||
# if __cpp_lib_saturation_arithmetic != 202603L
|
||||
# error "__cpp_lib_saturation_arithmetic should have the value 202603L in c++26"
|
||||
# endif
|
||||
|
||||
# if !defined(_LIBCPP_VERSION) || _LIBCPP_HAS_THREADS
|
||||
|
|
|
|||
|
|
@ -1,171 +0,0 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T add_sat(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::add_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::add_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::add_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{-2});
|
||||
assert(std::add_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
|
||||
assert(std::add_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::add_sat(IntegerT{-1}, minVal) == minVal); // saturated
|
||||
assert(std::add_sat(IntegerT{-1}, maxVal) == IntegerT{-1} + maxVal);
|
||||
|
||||
assert(std::add_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::add_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::add_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::add_sat(IntegerT{ 0}, minVal) == minVal);
|
||||
assert(std::add_sat(IntegerT{ 0}, maxVal) == maxVal);
|
||||
|
||||
assert(std::add_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::add_sat(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 1});
|
||||
assert(std::add_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 2});
|
||||
assert(std::add_sat(IntegerT{ 1}, minVal) == IntegerT{ 1} + minVal);
|
||||
assert(std::add_sat(IntegerT{ 1}, maxVal) == maxVal); // saturated
|
||||
|
||||
assert(std::add_sat( minVal, IntegerT{-1}) == minVal); // saturated
|
||||
assert(std::add_sat( minVal, IntegerT{ 0}) == minVal);
|
||||
assert(std::add_sat( minVal, IntegerT{ 1}) == minVal + IntegerT{ 1});
|
||||
assert(std::add_sat( minVal, minVal) == minVal); // saturated
|
||||
assert(std::add_sat( minVal, maxVal) == IntegerT{-1});
|
||||
|
||||
assert(std::add_sat( maxVal, IntegerT{-1}) == maxVal + IntegerT{-1});
|
||||
assert(std::add_sat( maxVal, IntegerT{ 0}) == maxVal);
|
||||
assert(std::add_sat( maxVal, IntegerT{ 1}) == maxVal); // saturated
|
||||
assert(std::add_sat( maxVal, minVal) == IntegerT{-1});
|
||||
assert(std::add_sat( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::add_sat(IntegerT{-27}, IntegerT{28})== IntegerT{ 1});
|
||||
assert(std::add_sat(IntegerT{ 27}, IntegerT{28})== IntegerT{55});
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::add_sat(x, y) == maxVal);
|
||||
}
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-28};
|
||||
assert(std::add_sat(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::add_sat(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::add_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::add_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Litmit values (0, 1, min, max)
|
||||
|
||||
assert(std::add_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::add_sat(IntegerT{0}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::add_sat(IntegerT{0}, minVal) == IntegerT{0});
|
||||
assert(std::add_sat(IntegerT{0}, maxVal) == maxVal);
|
||||
assert(std::add_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1});
|
||||
assert(std::add_sat(IntegerT{1}, IntegerT{1}) == IntegerT{2});
|
||||
assert(std::add_sat(IntegerT{1}, minVal) == IntegerT{1});
|
||||
assert(std::add_sat(IntegerT{1}, maxVal) == maxVal); // saturated
|
||||
assert(std::add_sat( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::add_sat( minVal, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::add_sat( minVal, minVal) == minVal);
|
||||
assert(std::add_sat( minVal, maxVal) == maxVal);
|
||||
assert(std::add_sat( maxVal, IntegerT{0}) == maxVal);
|
||||
assert(std::add_sat( maxVal, IntegerT{1}) == maxVal); // saturated
|
||||
assert(std::add_sat( maxVal, minVal) == maxVal);
|
||||
assert(std::add_sat( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::add_sat(IntegerT{27}, IntegerT{28}) == IntegerT{55});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::add_sat( x, y) == maxVal); // saturated
|
||||
assert(std::add_sat( x, maxVal) == maxVal); // saturated
|
||||
assert(std::add_sat(maxVal, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,155 +0,0 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T div_sat(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::div_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::div_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::div_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::div_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::div_sat(IntegerT{-1}, minVal) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{-1}, maxVal) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 0}, minVal) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 0}, maxVal) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::div_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::div_sat(IntegerT{ 1}, minVal) == IntegerT{ 0});
|
||||
assert(std::div_sat(IntegerT{ 1}, maxVal) == IntegerT{ 0});
|
||||
assert(std::div_sat( minVal, IntegerT{ 1}) == minVal);
|
||||
assert(std::div_sat( minVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::div_sat( minVal, minVal) == IntegerT{ 1});
|
||||
assert(std::div_sat( minVal, maxVal) == (minVal / maxVal));
|
||||
assert(std::div_sat( maxVal, IntegerT{-1}) == -maxVal);
|
||||
assert(std::div_sat( maxVal, IntegerT{ 1}) == maxVal);
|
||||
assert(std::div_sat( maxVal, minVal) == IntegerT{ 0});
|
||||
assert(std::div_sat( maxVal, maxVal) == IntegerT{ 1});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::div_sat(IntegerT{27}, IntegerT{28}) == IntegerT{0});
|
||||
assert(std::div_sat(IntegerT{28}, IntegerT{27}) == IntegerT{1});
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-28};
|
||||
constexpr IntegerT biggerVal = minVal / IntegerT{2} + IntegerT{-27};
|
||||
assert(std::div_sat(lesserVal, biggerVal) == IntegerT{1});
|
||||
assert(std::div_sat(biggerVal, lesserVal) == IntegerT{0});
|
||||
}
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::div_sat(lesserVal, biggerVal) == IntegerT{-1});
|
||||
assert(std::div_sat(biggerVal, lesserVal) == IntegerT{-1});
|
||||
}
|
||||
{
|
||||
constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::div_sat(lesserVal, biggerVal) == IntegerT{0});
|
||||
assert(std::div_sat(biggerVal, lesserVal) == IntegerT{1});
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::div_sat(minVal, maxVal);
|
||||
static_assert(noexcept(std::div_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// No limit values (0, 1, min, max)
|
||||
|
||||
assert(std::div_sat(IntegerT{0}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::div_sat(IntegerT{0}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::div_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::div_sat(IntegerT{1}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::div_sat( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::div_sat( minVal, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::div_sat( maxVal, IntegerT{1}) == maxVal);
|
||||
assert(std::div_sat( maxVal, maxVal) == IntegerT{1});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::div_sat(IntegerT{27}, IntegerT{28}) == IntegerT{0});
|
||||
assert(std::div_sat(IntegerT{28}, IntegerT{27}) == IntegerT{1});
|
||||
{
|
||||
constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::div_sat(lesserVal, biggerVal) == IntegerT{0});
|
||||
assert(std::div_sat(biggerVal, lesserVal) == IntegerT{1});
|
||||
}
|
||||
|
||||
// Unsigned integer division never overflows
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,177 +0,0 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T mul_sat(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::mul_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::mul_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::mul_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::mul_sat(IntegerT{-1}, minVal) == maxVal); // saturated
|
||||
assert(std::mul_sat(IntegerT{-1}, maxVal) == -maxVal);
|
||||
|
||||
assert(std::mul_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{ 0}, minVal) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{ 0}, maxVal) == IntegerT{ 0});
|
||||
|
||||
assert(std::mul_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::mul_sat(IntegerT{ 1}, minVal) == minVal);
|
||||
assert(std::mul_sat(IntegerT{ 1}, maxVal) == maxVal);
|
||||
|
||||
assert(std::mul_sat( minVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::mul_sat( minVal, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::mul_sat( minVal, IntegerT{ 1}) == minVal);
|
||||
assert(std::mul_sat( minVal, minVal) == maxVal); // saturated
|
||||
assert(std::mul_sat( minVal, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::mul_sat( maxVal, IntegerT{-1}) == -maxVal);
|
||||
assert(std::mul_sat( maxVal, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::mul_sat( maxVal, IntegerT{ 1}) == maxVal); // saturated
|
||||
assert(std::mul_sat( maxVal, minVal) == minVal); // saturated
|
||||
assert(std::mul_sat( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::mul_sat(IntegerT{27}, IntegerT{ 2}) == IntegerT{54});
|
||||
assert(std::mul_sat(IntegerT{ 2}, IntegerT{28}) == IntegerT{56});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::mul_sat(x, y) == maxVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::mul_sat(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::mul_sat(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::mul_sat(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::mul_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// No saturation (0, 1)
|
||||
|
||||
assert(std::mul_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::mul_sat(IntegerT{0}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::mul_sat(IntegerT{0}, minVal) == IntegerT{0});
|
||||
assert(std::mul_sat(IntegerT{0}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::mul_sat(IntegerT{1}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::mul_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::mul_sat(IntegerT{1}, minVal) == minVal);
|
||||
assert(std::mul_sat(IntegerT{1}, maxVal) == maxVal);
|
||||
|
||||
assert(std::mul_sat( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::mul_sat( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::mul_sat( minVal, maxVal) == minVal);
|
||||
assert(std::mul_sat( minVal, maxVal) == minVal);
|
||||
|
||||
assert(std::mul_sat( maxVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::mul_sat( maxVal, IntegerT{1}) == maxVal);
|
||||
assert(std::mul_sat( maxVal, minVal) == IntegerT{0});
|
||||
assert(std::mul_sat( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::mul_sat(IntegerT{28}, IntegerT{2}) == IntegerT{56});
|
||||
|
||||
// Saturation (no limit values
|
||||
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::mul_sat(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,394 +0,0 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class R, class T>
|
||||
// constexpr R saturate_cast(T x) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
// Smaller to larger
|
||||
static_assert(noexcept(std::saturate_cast<signed int>(std::numeric_limits<signed char>::max())));
|
||||
static_assert(noexcept(std::saturate_cast<signed int>(std::numeric_limits<unsigned char>::max())));
|
||||
|
||||
static_assert(noexcept(std::saturate_cast<unsigned int>(std::numeric_limits<signed char>::max())));
|
||||
static_assert(noexcept(std::saturate_cast<unsigned int>(std::numeric_limits<unsigned char>::max())));
|
||||
|
||||
// Same type
|
||||
static_assert(noexcept(std::saturate_cast<signed long int>(std::numeric_limits<signed long int>::max())));
|
||||
static_assert(noexcept(std::saturate_cast<unsigned long int>(std::numeric_limits<unsigned long int>::max())));
|
||||
|
||||
// Larger to smaller
|
||||
static_assert(noexcept(std::saturate_cast<signed char>(std::numeric_limits<signed int>::max())));
|
||||
static_assert(noexcept(std::saturate_cast<signed char>(std::numeric_limits<unsigned int>::max())));
|
||||
|
||||
static_assert(noexcept(std::saturate_cast<unsigned char>(std::numeric_limits<signed int>::max())));
|
||||
static_assert(noexcept(std::saturate_cast<unsigned char>(std::numeric_limits<unsigned int>::max())));
|
||||
|
||||
// Tests
|
||||
|
||||
constexpr bool test() {
|
||||
// clang-format off
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
using SIntT = __int128_t;
|
||||
using UIntT = __uint128_t;
|
||||
#else
|
||||
using SIntT = long long int;
|
||||
using UIntT = unsigned long long int;
|
||||
#endif
|
||||
|
||||
// Constants the values of which depend on the context (platform)
|
||||
|
||||
constexpr auto sBigMin = std::numeric_limits<SIntT>::min();
|
||||
constexpr auto sZero = SIntT{0};
|
||||
constexpr auto sBigMax = std::numeric_limits<SIntT>::max();
|
||||
|
||||
constexpr auto uZero = UIntT{0};
|
||||
constexpr auto uBigMax = std::numeric_limits<UIntT>::max();
|
||||
|
||||
// Constants to avoid casting in place
|
||||
|
||||
constexpr auto O_C = static_cast<signed char>(0);
|
||||
constexpr auto O_UC = static_cast<unsigned char>(0);
|
||||
|
||||
constexpr auto O_S = static_cast<signed short int>(0);
|
||||
constexpr auto O_US = static_cast<unsigned short int>(0);
|
||||
|
||||
// signed char
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<signed char>(SCHAR_MIN) == SCHAR_MIN);
|
||||
assert(std::saturate_cast<signed char>( O_C) == O_C);
|
||||
assert(std::saturate_cast<signed char>(SCHAR_MAX) == SCHAR_MAX);
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<signed char>( O_UC) == O_C);
|
||||
assert(std::saturate_cast<signed char>(UCHAR_MAX) == SCHAR_MAX);
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(sBigMax);
|
||||
assert(std::saturate_cast<signed char>(sBigMin) == SCHAR_MIN); // saturated
|
||||
assert(std::saturate_cast<signed char>( sZero) == O_C);
|
||||
assert(std::saturate_cast<signed char>(sBigMax) == SCHAR_MAX); // saturated
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(uBigMax);
|
||||
assert(std::saturate_cast<signed char>( uZero) == O_C);
|
||||
assert(std::saturate_cast<signed char>(uBigMax) == SCHAR_MAX); // saturated
|
||||
|
||||
// short
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<signed short int>(SCHAR_MIN) == static_cast<signed short int>(SCHAR_MIN));
|
||||
assert(std::saturate_cast<signed short int>( O_C) == O_S);
|
||||
assert(std::saturate_cast<signed short int>(SCHAR_MAX) == static_cast<signed short int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<signed short int>( O_UC) == O_S);
|
||||
assert(std::saturate_cast<signed short int>(UCHAR_MAX) == static_cast<signed short int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(SHRT_MAX);
|
||||
assert(std::saturate_cast<signed short int>( SHRT_MIN) == SHRT_MIN);
|
||||
assert(std::saturate_cast<signed short int>( O_S) == O_S);
|
||||
assert(std::saturate_cast<signed short int>( SHRT_MAX) == SHRT_MAX);
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(USHRT_MAX);
|
||||
assert(std::saturate_cast<signed short int>( O_US) == O_S);
|
||||
assert(std::saturate_cast<signed short int>(USHRT_MAX) == SHRT_MAX); // saturated
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(sBigMax);
|
||||
assert(std::saturate_cast<signed short int>( sBigMin) == SHRT_MIN); // saturated
|
||||
assert(std::saturate_cast<signed short int>( sZero) == O_S);
|
||||
assert(std::saturate_cast<signed short int>( sBigMax) == SHRT_MAX); // saturated
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(uBigMax);
|
||||
assert(std::saturate_cast<signed short int>( uZero) == O_S);
|
||||
assert(std::saturate_cast<signed short int>( uBigMax) == SHRT_MAX); // saturated
|
||||
|
||||
// int
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<signed int>(SCHAR_MIN) == static_cast<signed int>(SCHAR_MIN));
|
||||
assert(std::saturate_cast<signed int>( O_C) == 0);
|
||||
assert(std::saturate_cast<signed int>(SCHAR_MAX) == static_cast<signed int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<signed int>( O_UC) == 0);
|
||||
assert(std::saturate_cast<signed int>(UCHAR_MAX) == static_cast<signed int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(INT_MAX);
|
||||
assert(std::saturate_cast<signed int>( INT_MIN) == INT_MIN);
|
||||
assert(std::saturate_cast<signed int>( 0) == 0);
|
||||
assert(std::saturate_cast<signed int>( INT_MAX) == INT_MAX);
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(UINT_MAX);
|
||||
assert(std::saturate_cast<signed int>( 0) == 0);
|
||||
assert(std::saturate_cast<signed int>(UINT_MAX) == INT_MAX); // saturated
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(sBigMax);
|
||||
assert(std::saturate_cast<signed int>( sBigMin) == INT_MIN); // saturated
|
||||
assert(std::saturate_cast<signed int>( sZero) == 0);
|
||||
assert(std::saturate_cast<signed int>( sBigMax) == INT_MAX); // saturated
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(uBigMax);
|
||||
assert(std::saturate_cast<signed int>( uZero) == 0);
|
||||
assert(std::saturate_cast<signed int>( uBigMax) == INT_MAX); // saturated
|
||||
|
||||
// long
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<signed long int>(SCHAR_MIN) == static_cast<signed long int>(SCHAR_MIN));
|
||||
assert(std::saturate_cast<signed long int>( O_C) == 0L);
|
||||
assert(std::saturate_cast<signed long int>(SCHAR_MAX) == static_cast<signed long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<signed long int>( O_UC) == 0L);
|
||||
assert(std::saturate_cast<signed long int>(UCHAR_MAX) == static_cast<signed long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(LONG_MAX);
|
||||
assert(std::saturate_cast<signed long int>( LONG_MIN) == LONG_MIN);
|
||||
assert(std::saturate_cast<signed long int>( 0L) == 0L);
|
||||
assert(std::saturate_cast<signed long int>( LONG_MAX) == LONG_MAX);
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(ULONG_MAX);
|
||||
assert(std::saturate_cast<signed long int>( 0UL) == 0L);
|
||||
assert(std::saturate_cast<signed long int>(ULONG_MAX) == LONG_MAX); // saturated
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(sBigMax);
|
||||
assert(std::saturate_cast<signed long int>( sBigMin) == LONG_MIN); // saturated
|
||||
assert(std::saturate_cast<signed long int>( sZero) == 0L);
|
||||
assert(std::saturate_cast<signed long int>( sBigMax) == LONG_MAX); // saturated
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(uBigMax);
|
||||
assert(std::saturate_cast<signed long int>( uZero) == 0L);
|
||||
assert(std::saturate_cast<signed long int>( uBigMax) == LONG_MAX); // saturated
|
||||
|
||||
// long long
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<signed long long int>(SCHAR_MIN) == static_cast<signed long long int>(SCHAR_MIN));
|
||||
assert(std::saturate_cast<signed long long int>( 0LL) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>(SCHAR_MAX) == static_cast<signed long long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<signed long long int>( O_UC) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>(UCHAR_MAX) == static_cast<signed long long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(LLONG_MIN);
|
||||
assert(std::saturate_cast<signed long long int>(LLONG_MIN) == LLONG_MIN);
|
||||
assert(std::saturate_cast<signed long long int>( 0LL) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>(LLONG_MAX) == LLONG_MAX);
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(ULLONG_MAX);
|
||||
assert(std::saturate_cast<signed long long int>( 0ULL) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>(ULLONG_MAX) == LLONG_MAX); // saturated
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(sBigMax);
|
||||
assert(std::saturate_cast<signed long long int>( sBigMin) == LLONG_MIN); // (128-bit) saturated
|
||||
assert(std::saturate_cast<signed long long int>( sZero) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>( sBigMax) == LLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(uBigMax);
|
||||
assert(std::saturate_cast<signed long long int>( uZero) == 0LL);
|
||||
assert(std::saturate_cast<signed long long int>( uBigMax) == LLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<__int128_t>(SCHAR_MIN) == static_cast<__int128_t>(SCHAR_MIN));
|
||||
assert(std::saturate_cast<__int128_t>( O_C) == sZero);
|
||||
assert(std::saturate_cast<__int128_t>(SCHAR_MAX) == static_cast<__int128_t>(SCHAR_MAX));
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<__int128_t>( O_UC) == sZero);
|
||||
assert(std::saturate_cast<__int128_t>(UCHAR_MAX) == static_cast<__int128_t>(UCHAR_MAX));
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(sBigMax);
|
||||
assert(std::saturate_cast<__int128_t>( sBigMin) == sBigMin);
|
||||
assert(std::saturate_cast<__int128_t>( sZero) == sZero);
|
||||
assert(std::saturate_cast<__int128_t>( sBigMax) == sBigMax);
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(uBigMax);
|
||||
assert(std::saturate_cast<__int128_t>( uZero) == sZero);
|
||||
assert(std::saturate_cast<__int128_t>( uBigMax) == sBigMax); // saturated
|
||||
#endif
|
||||
|
||||
// unsigned char
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned char>(SCHAR_MIN) == O_UC);
|
||||
assert(std::saturate_cast<unsigned char>( O_C) == O_UC);
|
||||
assert(std::saturate_cast<unsigned char>(SCHAR_MAX) == static_cast<unsigned char>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned char>( O_UC) == O_UC);
|
||||
assert(std::saturate_cast<unsigned char>(UCHAR_MAX) == UCHAR_MAX);
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(sBigMax);
|
||||
assert(std::saturate_cast<unsigned char>( sBigMin) == O_UC); // saturated
|
||||
assert(std::saturate_cast<unsigned char>( sZero) == O_UC);
|
||||
assert(std::saturate_cast<unsigned char>( sBigMax) == UCHAR_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(uBigMax);
|
||||
assert(std::saturate_cast<unsigned char>( uZero) == O_UC);
|
||||
assert(std::saturate_cast<unsigned char>( uBigMax) == UCHAR_MAX); // saturated
|
||||
|
||||
// unsigned short
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned short int>(SCHAR_MIN) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>( O_C) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>(SCHAR_MAX) == static_cast<unsigned short int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned short int>( O_UC) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>(UCHAR_MAX) == static_cast<unsigned short int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(SCHAR_MIN);
|
||||
assert(std::saturate_cast<unsigned short int>( SHRT_MIN) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>( O_S) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>( SHRT_MAX) == static_cast<unsigned short int>(SHRT_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned short int>( O_US) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>(USHRT_MAX) == USHRT_MAX);
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(sBigMax);
|
||||
assert(std::saturate_cast<unsigned short int>( sBigMin) == O_US); // saturated
|
||||
assert(std::saturate_cast<unsigned short int>( sZero) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>( sBigMax) == USHRT_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(uBigMax);
|
||||
assert(std::saturate_cast<unsigned short int>( uZero) == O_US);
|
||||
assert(std::saturate_cast<unsigned short int>( uBigMax) == USHRT_MAX); // saturated
|
||||
|
||||
// unsigned int
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned int>(SCHAR_MIN) == O_US);
|
||||
assert(std::saturate_cast<unsigned int>( O_UC) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>(SCHAR_MAX) == static_cast<unsigned int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned int>( O_UC) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>(UCHAR_MAX) == static_cast<unsigned int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(INT_MAX);
|
||||
assert(std::saturate_cast<unsigned int>( INT_MIN) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>( 0) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>( INT_MAX) == static_cast<unsigned int>(INT_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(UINT_MAX);
|
||||
assert(std::saturate_cast<unsigned int>( 0U) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>( UINT_MAX) == UINT_MAX);
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(sBigMax);
|
||||
assert(std::saturate_cast<unsigned int>( sBigMin) == 0U); // saturated
|
||||
assert(std::saturate_cast<unsigned int>( sZero) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>( sBigMax) == UINT_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(uBigMax);
|
||||
assert(std::saturate_cast<unsigned int>( uZero) == 0U);
|
||||
assert(std::saturate_cast<unsigned int>( uBigMax) == UINT_MAX); // saturated
|
||||
|
||||
// unsigned long
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned long int>(SCHAR_MIN) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>( O_C) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>(SCHAR_MAX) == static_cast<unsigned long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned long int>( O_UC) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>(UCHAR_MAX) == static_cast<unsigned long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(LONG_MAX);
|
||||
assert(std::saturate_cast<unsigned long int>( LONG_MIN) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>( 0L) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>( LONG_MAX) == static_cast<unsigned long int>(LONG_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(ULONG_MAX);
|
||||
assert(std::saturate_cast<unsigned long int>( 0UL) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>(ULONG_MAX) == ULONG_MAX);
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(sBigMax);
|
||||
assert(std::saturate_cast<unsigned long int>( sBigMin) == 0UL); // saturated
|
||||
assert(std::saturate_cast<unsigned long int>( sZero) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>( sBigMax) == (sizeof(UIntT) > sizeof(unsigned long int) ? ULONG_MAX : LONG_MAX)); // saturated depending on underlying types
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(uBigMax);
|
||||
assert(std::saturate_cast<unsigned long int>( uZero) == 0UL);
|
||||
assert(std::saturate_cast<unsigned long int>( uBigMax) == ULONG_MAX); // saturated
|
||||
|
||||
// unsigned long long
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(SCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned long long int>( SCHAR_MIN) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( O_C) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( SCHAR_MAX) == static_cast<unsigned long long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<unsigned long long int>( O_UC) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( UCHAR_MAX) == static_cast<unsigned long long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(LLONG_MAX);
|
||||
assert(std::saturate_cast<unsigned long long int>( LLONG_MIN) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( 0LL) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( LLONG_MAX) == static_cast<unsigned long long int>(LLONG_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(ULLONG_MAX);
|
||||
assert(std::saturate_cast<unsigned long long int>( 0ULL) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>(ULLONG_MAX) == ULLONG_MAX);
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(sBigMax);
|
||||
assert(std::saturate_cast<unsigned long long int>( sBigMin) == 0ULL); // (128-bit) saturated
|
||||
assert(std::saturate_cast<unsigned long long int>( sZero) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( sBigMax) == ULLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(uBigMax);
|
||||
assert(std::saturate_cast<unsigned long long int>( uZero) == 0ULL);
|
||||
assert(std::saturate_cast<unsigned long long int>( uBigMax) == ULLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(SCHAR_MIN);
|
||||
assert(std::saturate_cast<__uint128_t>(SCHAR_MIN) == uZero);
|
||||
assert(std::saturate_cast<__uint128_t>( O_C) == uZero);
|
||||
assert(std::saturate_cast<__uint128_t>(SCHAR_MAX) == static_cast<__uint128_t>(SCHAR_MAX));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(UCHAR_MAX);
|
||||
assert(std::saturate_cast<__uint128_t>( O_UC) == uZero);
|
||||
assert(std::saturate_cast<__uint128_t>(UCHAR_MAX) == static_cast<__uint128_t>(UCHAR_MAX));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(sBigMax);
|
||||
assert(std::saturate_cast<__uint128_t>( sBigMin) == uZero); // saturated
|
||||
assert(std::saturate_cast<__uint128_t>( sZero) == uZero);
|
||||
assert(std::saturate_cast<__uint128_t>( sBigMax) == static_cast<__uint128_t>(sBigMax));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(uBigMax);
|
||||
assert(std::saturate_cast<__uint128_t>( uZero) == uZero);
|
||||
assert(std::saturate_cast<__uint128_t>( uBigMax) == uBigMax);
|
||||
#endif
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T add_sat(T x, T y) noexcept; // freestanding
|
||||
// constexpr T saturating_add(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <concepts>
|
||||
#include <numeric>
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
template <typename T, typename U>
|
||||
concept CanDo = requires(T x, U y) {
|
||||
{ std::add_sat(x, y) } -> std::same_as<T>;
|
||||
{ std::saturating_add(x, y) } -> std::same_as<T>;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
|
|
@ -0,0 +1,171 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T saturating_add(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_add(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_add(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::saturating_add(IntegerT{-1}, IntegerT{-1}) == IntegerT{-2});
|
||||
assert(std::saturating_add(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
|
||||
assert(std::saturating_add(IntegerT{-1}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::saturating_add(IntegerT{-1}, minVal) == minVal); // saturated
|
||||
assert(std::saturating_add(IntegerT{-1}, maxVal) == IntegerT{-1} + maxVal);
|
||||
|
||||
assert(std::saturating_add(IntegerT{ 0}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::saturating_add(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_add(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::saturating_add(IntegerT{ 0}, minVal) == minVal);
|
||||
assert(std::saturating_add(IntegerT{ 0}, maxVal) == maxVal);
|
||||
|
||||
assert(std::saturating_add(IntegerT{ 1}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::saturating_add(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 1});
|
||||
assert(std::saturating_add(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 2});
|
||||
assert(std::saturating_add(IntegerT{ 1}, minVal) == IntegerT{ 1} + minVal);
|
||||
assert(std::saturating_add(IntegerT{ 1}, maxVal) == maxVal); // saturated
|
||||
|
||||
assert(std::saturating_add( minVal, IntegerT{-1}) == minVal); // saturated
|
||||
assert(std::saturating_add( minVal, IntegerT{ 0}) == minVal);
|
||||
assert(std::saturating_add( minVal, IntegerT{ 1}) == minVal + IntegerT{ 1});
|
||||
assert(std::saturating_add( minVal, minVal) == minVal); // saturated
|
||||
assert(std::saturating_add( minVal, maxVal) == IntegerT{-1});
|
||||
|
||||
assert(std::saturating_add( maxVal, IntegerT{-1}) == maxVal + IntegerT{-1});
|
||||
assert(std::saturating_add( maxVal, IntegerT{ 0}) == maxVal);
|
||||
assert(std::saturating_add( maxVal, IntegerT{ 1}) == maxVal); // saturated
|
||||
assert(std::saturating_add( maxVal, minVal) == IntegerT{-1});
|
||||
assert(std::saturating_add( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_add(IntegerT{-27}, IntegerT{28})== IntegerT{ 1});
|
||||
assert(std::saturating_add(IntegerT{ 27}, IntegerT{28})== IntegerT{55});
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_add(x, y) == maxVal);
|
||||
}
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-28};
|
||||
assert(std::saturating_add(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_add(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_add(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_add(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Litmit values (0, 1, min, max)
|
||||
|
||||
assert(std::saturating_add(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_add(IntegerT{0}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::saturating_add(IntegerT{0}, minVal) == IntegerT{0});
|
||||
assert(std::saturating_add(IntegerT{0}, maxVal) == maxVal);
|
||||
assert(std::saturating_add(IntegerT{1}, IntegerT{0}) == IntegerT{1});
|
||||
assert(std::saturating_add(IntegerT{1}, IntegerT{1}) == IntegerT{2});
|
||||
assert(std::saturating_add(IntegerT{1}, minVal) == IntegerT{1});
|
||||
assert(std::saturating_add(IntegerT{1}, maxVal) == maxVal); // saturated
|
||||
assert(std::saturating_add( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_add( minVal, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::saturating_add( minVal, minVal) == minVal);
|
||||
assert(std::saturating_add( minVal, maxVal) == maxVal);
|
||||
assert(std::saturating_add( maxVal, IntegerT{0}) == maxVal);
|
||||
assert(std::saturating_add( maxVal, IntegerT{1}) == maxVal); // saturated
|
||||
assert(std::saturating_add( maxVal, minVal) == maxVal);
|
||||
assert(std::saturating_add( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_add(IntegerT{27}, IntegerT{28}) == IntegerT{55});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_add( x, y) == maxVal); // saturated
|
||||
assert(std::saturating_add( x, maxVal) == maxVal); // saturated
|
||||
assert(std::saturating_add(maxVal, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class R, class T>
|
||||
// constexpr R saturate_cast(T x) noexcept; // freestanding
|
||||
// constexpr R saturating_cast(T x) noexcept; // freestanding
|
||||
|
||||
#include <concepts>
|
||||
#include <numeric>
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
template <typename R, typename T>
|
||||
concept CanDo = requires(T x) {
|
||||
{ std::saturate_cast<R>(x) } -> std::same_as<R>;
|
||||
{ std::saturating_cast<R>(x) } -> std::same_as<R>;
|
||||
};
|
||||
|
||||
template <typename R, typename T>
|
||||
|
|
@ -0,0 +1,394 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class R, class T>
|
||||
// constexpr R saturating_cast(T x) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
// Smaller to larger
|
||||
static_assert(noexcept(std::saturating_cast<signed int>(std::numeric_limits<signed char>::max())));
|
||||
static_assert(noexcept(std::saturating_cast<signed int>(std::numeric_limits<unsigned char>::max())));
|
||||
|
||||
static_assert(noexcept(std::saturating_cast<unsigned int>(std::numeric_limits<signed char>::max())));
|
||||
static_assert(noexcept(std::saturating_cast<unsigned int>(std::numeric_limits<unsigned char>::max())));
|
||||
|
||||
// Same type
|
||||
static_assert(noexcept(std::saturating_cast<signed long int>(std::numeric_limits<signed long int>::max())));
|
||||
static_assert(noexcept(std::saturating_cast<unsigned long int>(std::numeric_limits<unsigned long int>::max())));
|
||||
|
||||
// Larger to smaller
|
||||
static_assert(noexcept(std::saturating_cast<signed char>(std::numeric_limits<signed int>::max())));
|
||||
static_assert(noexcept(std::saturating_cast<signed char>(std::numeric_limits<unsigned int>::max())));
|
||||
|
||||
static_assert(noexcept(std::saturating_cast<unsigned char>(std::numeric_limits<signed int>::max())));
|
||||
static_assert(noexcept(std::saturating_cast<unsigned char>(std::numeric_limits<unsigned int>::max())));
|
||||
|
||||
// Tests
|
||||
|
||||
constexpr bool test() {
|
||||
// clang-format off
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
using SIntT = __int128_t;
|
||||
using UIntT = __uint128_t;
|
||||
#else
|
||||
using SIntT = long long int;
|
||||
using UIntT = unsigned long long int;
|
||||
#endif
|
||||
|
||||
// Constants the values of which depend on the context (platform)
|
||||
|
||||
constexpr auto sBigMin = std::numeric_limits<SIntT>::min();
|
||||
constexpr auto sZero = SIntT{0};
|
||||
constexpr auto sBigMax = std::numeric_limits<SIntT>::max();
|
||||
|
||||
constexpr auto uZero = UIntT{0};
|
||||
constexpr auto uBigMax = std::numeric_limits<UIntT>::max();
|
||||
|
||||
// Constants to avoid casting in place
|
||||
|
||||
constexpr auto O_C = static_cast<signed char>(0);
|
||||
constexpr auto O_UC = static_cast<unsigned char>(0);
|
||||
|
||||
constexpr auto O_S = static_cast<signed short int>(0);
|
||||
constexpr auto O_US = static_cast<unsigned short int>(0);
|
||||
|
||||
// signed char
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturating_cast<signed char>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<signed char>(SCHAR_MIN) == SCHAR_MIN);
|
||||
assert(std::saturating_cast<signed char>( O_C) == O_C);
|
||||
assert(std::saturating_cast<signed char>(SCHAR_MAX) == SCHAR_MAX);
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturating_cast<signed char>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<signed char>( O_UC) == O_C);
|
||||
assert(std::saturating_cast<signed char>(UCHAR_MAX) == SCHAR_MAX);
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturating_cast<signed char>(sBigMax);
|
||||
assert(std::saturating_cast<signed char>(sBigMin) == SCHAR_MIN); // saturated
|
||||
assert(std::saturating_cast<signed char>( sZero) == O_C);
|
||||
assert(std::saturating_cast<signed char>(sBigMax) == SCHAR_MAX); // saturated
|
||||
|
||||
std::same_as<signed char> decltype(auto) _ = std::saturating_cast<signed char>(uBigMax);
|
||||
assert(std::saturating_cast<signed char>( uZero) == O_C);
|
||||
assert(std::saturating_cast<signed char>(uBigMax) == SCHAR_MAX); // saturated
|
||||
|
||||
// short
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<signed short int>(SCHAR_MIN) == static_cast<signed short int>(SCHAR_MIN));
|
||||
assert(std::saturating_cast<signed short int>( O_C) == O_S);
|
||||
assert(std::saturating_cast<signed short int>(SCHAR_MAX) == static_cast<signed short int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<signed short int>( O_UC) == O_S);
|
||||
assert(std::saturating_cast<signed short int>(UCHAR_MAX) == static_cast<signed short int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(SHRT_MAX);
|
||||
assert(std::saturating_cast<signed short int>( SHRT_MIN) == SHRT_MIN);
|
||||
assert(std::saturating_cast<signed short int>( O_S) == O_S);
|
||||
assert(std::saturating_cast<signed short int>( SHRT_MAX) == SHRT_MAX);
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(USHRT_MAX);
|
||||
assert(std::saturating_cast<signed short int>( O_US) == O_S);
|
||||
assert(std::saturating_cast<signed short int>(USHRT_MAX) == SHRT_MAX); // saturated
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(sBigMax);
|
||||
assert(std::saturating_cast<signed short int>( sBigMin) == SHRT_MIN); // saturated
|
||||
assert(std::saturating_cast<signed short int>( sZero) == O_S);
|
||||
assert(std::saturating_cast<signed short int>( sBigMax) == SHRT_MAX); // saturated
|
||||
|
||||
std::same_as<signed short int> decltype(auto) _ = std::saturating_cast<signed short int>(uBigMax);
|
||||
assert(std::saturating_cast<signed short int>( uZero) == O_S);
|
||||
assert(std::saturating_cast<signed short int>( uBigMax) == SHRT_MAX); // saturated
|
||||
|
||||
// int
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<signed int>(SCHAR_MIN) == static_cast<signed int>(SCHAR_MIN));
|
||||
assert(std::saturating_cast<signed int>( O_C) == 0);
|
||||
assert(std::saturating_cast<signed int>(SCHAR_MAX) == static_cast<signed int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<signed int>( O_UC) == 0);
|
||||
assert(std::saturating_cast<signed int>(UCHAR_MAX) == static_cast<signed int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(INT_MAX);
|
||||
assert(std::saturating_cast<signed int>( INT_MIN) == INT_MIN);
|
||||
assert(std::saturating_cast<signed int>( 0) == 0);
|
||||
assert(std::saturating_cast<signed int>( INT_MAX) == INT_MAX);
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(UINT_MAX);
|
||||
assert(std::saturating_cast<signed int>( 0) == 0);
|
||||
assert(std::saturating_cast<signed int>(UINT_MAX) == INT_MAX); // saturated
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(sBigMax);
|
||||
assert(std::saturating_cast<signed int>( sBigMin) == INT_MIN); // saturated
|
||||
assert(std::saturating_cast<signed int>( sZero) == 0);
|
||||
assert(std::saturating_cast<signed int>( sBigMax) == INT_MAX); // saturated
|
||||
|
||||
std::same_as<signed int> decltype(auto) _ = std::saturating_cast<signed int>(uBigMax);
|
||||
assert(std::saturating_cast<signed int>( uZero) == 0);
|
||||
assert(std::saturating_cast<signed int>( uBigMax) == INT_MAX); // saturated
|
||||
|
||||
// long
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<signed long int>(SCHAR_MIN) == static_cast<signed long int>(SCHAR_MIN));
|
||||
assert(std::saturating_cast<signed long int>( O_C) == 0L);
|
||||
assert(std::saturating_cast<signed long int>(SCHAR_MAX) == static_cast<signed long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<signed long int>( O_UC) == 0L);
|
||||
assert(std::saturating_cast<signed long int>(UCHAR_MAX) == static_cast<signed long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(LONG_MAX);
|
||||
assert(std::saturating_cast<signed long int>( LONG_MIN) == LONG_MIN);
|
||||
assert(std::saturating_cast<signed long int>( 0L) == 0L);
|
||||
assert(std::saturating_cast<signed long int>( LONG_MAX) == LONG_MAX);
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(ULONG_MAX);
|
||||
assert(std::saturating_cast<signed long int>( 0UL) == 0L);
|
||||
assert(std::saturating_cast<signed long int>(ULONG_MAX) == LONG_MAX); // saturated
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(sBigMax);
|
||||
assert(std::saturating_cast<signed long int>( sBigMin) == LONG_MIN); // saturated
|
||||
assert(std::saturating_cast<signed long int>( sZero) == 0L);
|
||||
assert(std::saturating_cast<signed long int>( sBigMax) == LONG_MAX); // saturated
|
||||
|
||||
std::same_as<signed long int> decltype(auto) _ = std::saturating_cast<signed long int>(uBigMax);
|
||||
assert(std::saturating_cast<signed long int>( uZero) == 0L);
|
||||
assert(std::saturating_cast<signed long int>( uBigMax) == LONG_MAX); // saturated
|
||||
|
||||
// long long
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<signed long long int>(SCHAR_MIN) == static_cast<signed long long int>(SCHAR_MIN));
|
||||
assert(std::saturating_cast<signed long long int>( 0LL) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>(SCHAR_MAX) == static_cast<signed long long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<signed long long int>( O_UC) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>(UCHAR_MAX) == static_cast<signed long long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(LLONG_MIN);
|
||||
assert(std::saturating_cast<signed long long int>(LLONG_MIN) == LLONG_MIN);
|
||||
assert(std::saturating_cast<signed long long int>( 0LL) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>(LLONG_MAX) == LLONG_MAX);
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(ULLONG_MAX);
|
||||
assert(std::saturating_cast<signed long long int>( 0ULL) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>(ULLONG_MAX) == LLONG_MAX); // saturated
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(sBigMax);
|
||||
assert(std::saturating_cast<signed long long int>( sBigMin) == LLONG_MIN); // (128-bit) saturated
|
||||
assert(std::saturating_cast<signed long long int>( sZero) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>( sBigMax) == LLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<signed long long int> decltype(auto) _ = std::saturating_cast<signed long long int>(uBigMax);
|
||||
assert(std::saturating_cast<signed long long int>( uZero) == 0LL);
|
||||
assert(std::saturating_cast<signed long long int>( uBigMax) == LLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturating_cast<__int128_t>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<__int128_t>(SCHAR_MIN) == static_cast<__int128_t>(SCHAR_MIN));
|
||||
assert(std::saturating_cast<__int128_t>( O_C) == sZero);
|
||||
assert(std::saturating_cast<__int128_t>(SCHAR_MAX) == static_cast<__int128_t>(SCHAR_MAX));
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturating_cast<__int128_t>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<__int128_t>( O_UC) == sZero);
|
||||
assert(std::saturating_cast<__int128_t>(UCHAR_MAX) == static_cast<__int128_t>(UCHAR_MAX));
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturating_cast<__int128_t>(sBigMax);
|
||||
assert(std::saturating_cast<__int128_t>( sBigMin) == sBigMin);
|
||||
assert(std::saturating_cast<__int128_t>( sZero) == sZero);
|
||||
assert(std::saturating_cast<__int128_t>( sBigMax) == sBigMax);
|
||||
|
||||
std::same_as<__int128_t> decltype(auto) _ = std::saturating_cast<__int128_t>(uBigMax);
|
||||
assert(std::saturating_cast<__int128_t>( uZero) == sZero);
|
||||
assert(std::saturating_cast<__int128_t>( uBigMax) == sBigMax); // saturated
|
||||
#endif
|
||||
|
||||
// unsigned char
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturating_cast<unsigned char>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned char>(SCHAR_MIN) == O_UC);
|
||||
assert(std::saturating_cast<unsigned char>( O_C) == O_UC);
|
||||
assert(std::saturating_cast<unsigned char>(SCHAR_MAX) == static_cast<unsigned char>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturating_cast<unsigned char>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned char>( O_UC) == O_UC);
|
||||
assert(std::saturating_cast<unsigned char>(UCHAR_MAX) == UCHAR_MAX);
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturating_cast<unsigned char>(sBigMax);
|
||||
assert(std::saturating_cast<unsigned char>( sBigMin) == O_UC); // saturated
|
||||
assert(std::saturating_cast<unsigned char>( sZero) == O_UC);
|
||||
assert(std::saturating_cast<unsigned char>( sBigMax) == UCHAR_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned char> decltype(auto) _ = std::saturating_cast<unsigned char>(uBigMax);
|
||||
assert(std::saturating_cast<unsigned char>( uZero) == O_UC);
|
||||
assert(std::saturating_cast<unsigned char>( uBigMax) == UCHAR_MAX); // saturated
|
||||
|
||||
// unsigned short
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned short int>(SCHAR_MIN) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>( O_C) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>(SCHAR_MAX) == static_cast<unsigned short int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned short int>( O_UC) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>(UCHAR_MAX) == static_cast<unsigned short int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(SCHAR_MIN);
|
||||
assert(std::saturating_cast<unsigned short int>( SHRT_MIN) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>( O_S) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>( SHRT_MAX) == static_cast<unsigned short int>(SHRT_MAX));
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned short int>( O_US) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>(USHRT_MAX) == USHRT_MAX);
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(sBigMax);
|
||||
assert(std::saturating_cast<unsigned short int>( sBigMin) == O_US); // saturated
|
||||
assert(std::saturating_cast<unsigned short int>( sZero) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>( sBigMax) == USHRT_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned short int> decltype(auto) _ = std::saturating_cast<unsigned short int>(uBigMax);
|
||||
assert(std::saturating_cast<unsigned short int>( uZero) == O_US);
|
||||
assert(std::saturating_cast<unsigned short int>( uBigMax) == USHRT_MAX); // saturated
|
||||
|
||||
// unsigned int
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned int>(SCHAR_MIN) == O_US);
|
||||
assert(std::saturating_cast<unsigned int>( O_UC) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>(SCHAR_MAX) == static_cast<unsigned int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned int>( O_UC) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>(UCHAR_MAX) == static_cast<unsigned int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(INT_MAX);
|
||||
assert(std::saturating_cast<unsigned int>( INT_MIN) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>( 0) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>( INT_MAX) == static_cast<unsigned int>(INT_MAX));
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(UINT_MAX);
|
||||
assert(std::saturating_cast<unsigned int>( 0U) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>( UINT_MAX) == UINT_MAX);
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(sBigMax);
|
||||
assert(std::saturating_cast<unsigned int>( sBigMin) == 0U); // saturated
|
||||
assert(std::saturating_cast<unsigned int>( sZero) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>( sBigMax) == UINT_MAX); // saturated
|
||||
|
||||
std::same_as<unsigned int> decltype(auto) _ = std::saturating_cast<unsigned int>(uBigMax);
|
||||
assert(std::saturating_cast<unsigned int>( uZero) == 0U);
|
||||
assert(std::saturating_cast<unsigned int>( uBigMax) == UINT_MAX); // saturated
|
||||
|
||||
// unsigned long
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned long int>(SCHAR_MIN) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>( O_C) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>(SCHAR_MAX) == static_cast<unsigned long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned long int>( O_UC) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>(UCHAR_MAX) == static_cast<unsigned long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(LONG_MAX);
|
||||
assert(std::saturating_cast<unsigned long int>( LONG_MIN) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>( 0L) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>( LONG_MAX) == static_cast<unsigned long int>(LONG_MAX));
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(ULONG_MAX);
|
||||
assert(std::saturating_cast<unsigned long int>( 0UL) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>(ULONG_MAX) == ULONG_MAX);
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(sBigMax);
|
||||
assert(std::saturating_cast<unsigned long int>( sBigMin) == 0UL); // saturated
|
||||
assert(std::saturating_cast<unsigned long int>( sZero) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>( sBigMax) == (sizeof(UIntT) > sizeof(unsigned long int) ? ULONG_MAX : LONG_MAX)); // saturated depending on underlying types
|
||||
|
||||
std::same_as<unsigned long int> decltype(auto) _ = std::saturating_cast<unsigned long int>(uBigMax);
|
||||
assert(std::saturating_cast<unsigned long int>( uZero) == 0UL);
|
||||
assert(std::saturating_cast<unsigned long int>( uBigMax) == ULONG_MAX); // saturated
|
||||
|
||||
// unsigned long long
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(SCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned long long int>( SCHAR_MIN) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( O_C) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( SCHAR_MAX) == static_cast<unsigned long long int>(SCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<unsigned long long int>( O_UC) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( UCHAR_MAX) == static_cast<unsigned long long int>(UCHAR_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(LLONG_MAX);
|
||||
assert(std::saturating_cast<unsigned long long int>( LLONG_MIN) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( 0LL) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( LLONG_MAX) == static_cast<unsigned long long int>(LLONG_MAX));
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(ULLONG_MAX);
|
||||
assert(std::saturating_cast<unsigned long long int>( 0ULL) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>(ULLONG_MAX) == ULLONG_MAX);
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(sBigMax);
|
||||
assert(std::saturating_cast<unsigned long long int>( sBigMin) == 0ULL); // (128-bit) saturated
|
||||
assert(std::saturating_cast<unsigned long long int>( sZero) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( sBigMax) == ULLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<unsigned long long int> decltype(auto) _ = std::saturating_cast<unsigned long long int>(uBigMax);
|
||||
assert(std::saturating_cast<unsigned long long int>( uZero) == 0ULL);
|
||||
assert(std::saturating_cast<unsigned long long int>( uBigMax) == ULLONG_MAX); // (128-bit) saturated
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturating_cast<__uint128_t>(SCHAR_MIN);
|
||||
assert(std::saturating_cast<__uint128_t>(SCHAR_MIN) == uZero);
|
||||
assert(std::saturating_cast<__uint128_t>( O_C) == uZero);
|
||||
assert(std::saturating_cast<__uint128_t>(SCHAR_MAX) == static_cast<__uint128_t>(SCHAR_MAX));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturating_cast<__uint128_t>(UCHAR_MAX);
|
||||
assert(std::saturating_cast<__uint128_t>( O_UC) == uZero);
|
||||
assert(std::saturating_cast<__uint128_t>(UCHAR_MAX) == static_cast<__uint128_t>(UCHAR_MAX));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturating_cast<__uint128_t>(sBigMax);
|
||||
assert(std::saturating_cast<__uint128_t>( sBigMin) == uZero); // saturated
|
||||
assert(std::saturating_cast<__uint128_t>( sZero) == uZero);
|
||||
assert(std::saturating_cast<__uint128_t>( sBigMax) == static_cast<__uint128_t>(sBigMax));
|
||||
|
||||
std::same_as<__uint128_t> decltype(auto) _ = std::saturating_cast<__uint128_t>(uBigMax);
|
||||
assert(std::saturating_cast<__uint128_t>( uZero) == uZero);
|
||||
assert(std::saturating_cast<__uint128_t>( uBigMax) == uBigMax);
|
||||
#endif
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T div_sat(T x, T y) noexcept; // freestanding
|
||||
// constexpr T saturating_div(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <numeric>
|
||||
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
template <typename IntegerT>
|
||||
void test_runtime_assertion() {
|
||||
TEST_LIBCPP_ASSERT_FAILURE((void)std::div_sat(IntegerT{27}, IntegerT{0}), "Division by 0 is undefined");
|
||||
TEST_LIBCPP_ASSERT_FAILURE((void)std::saturating_div(IntegerT{27}, IntegerT{0}), "Division by 0 is undefined");
|
||||
}
|
||||
|
||||
bool test() {
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T div_sat(T x, T y) noexcept; // freestanding
|
||||
// constexpr T saturating_div(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <concepts>
|
||||
#include <numeric>
|
||||
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
template <typename T, typename U>
|
||||
concept CanDo = requires(T x, U y) {
|
||||
{ std::div_sat(x, y) } -> std::same_as<T>;
|
||||
{ std::saturating_div(x, y) } -> std::same_as<T>;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
|
|
@ -82,7 +82,7 @@ constexpr void test() {
|
|||
// A function call expression that violates the precondition in the Preconditions: element is not a core constant expression (7.7 [expr.const]).
|
||||
|
||||
template <auto N>
|
||||
using QuotT = std::integral_constant<decltype(N), std::div_sat(N, N)>;
|
||||
using QuotT = std::integral_constant<decltype(N), std::saturating_div(N, N)>;
|
||||
|
||||
template <auto N>
|
||||
QuotT<N> div_by_zero();
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T saturating_div(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_div(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_div(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::saturating_div(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::saturating_div(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::saturating_div(IntegerT{-1}, minVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{-1}, maxVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 0}, minVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 0}, maxVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::saturating_div(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::saturating_div(IntegerT{ 1}, minVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div(IntegerT{ 1}, maxVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div( minVal, IntegerT{ 1}) == minVal);
|
||||
assert(std::saturating_div( minVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::saturating_div( minVal, minVal) == IntegerT{ 1});
|
||||
assert(std::saturating_div( minVal, maxVal) == (minVal / maxVal));
|
||||
assert(std::saturating_div( maxVal, IntegerT{-1}) == -maxVal);
|
||||
assert(std::saturating_div( maxVal, IntegerT{ 1}) == maxVal);
|
||||
assert(std::saturating_div( maxVal, minVal) == IntegerT{ 0});
|
||||
assert(std::saturating_div( maxVal, maxVal) == IntegerT{ 1});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_div(IntegerT{27}, IntegerT{28}) == IntegerT{0});
|
||||
assert(std::saturating_div(IntegerT{28}, IntegerT{27}) == IntegerT{1});
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-28};
|
||||
constexpr IntegerT biggerVal = minVal / IntegerT{2} + IntegerT{-27};
|
||||
assert(std::saturating_div(lesserVal, biggerVal) == IntegerT{1});
|
||||
assert(std::saturating_div(biggerVal, lesserVal) == IntegerT{0});
|
||||
}
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_div(lesserVal, biggerVal) == IntegerT{-1});
|
||||
assert(std::saturating_div(biggerVal, lesserVal) == IntegerT{-1});
|
||||
}
|
||||
{
|
||||
constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_div(lesserVal, biggerVal) == IntegerT{0});
|
||||
assert(std::saturating_div(biggerVal, lesserVal) == IntegerT{1});
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_div(minVal, maxVal);
|
||||
static_assert(noexcept(std::saturating_div(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// No limit values (0, 1, min, max)
|
||||
|
||||
assert(std::saturating_div(IntegerT{0}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::saturating_div(IntegerT{0}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::saturating_div(IntegerT{1}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::saturating_div(IntegerT{1}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::saturating_div( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::saturating_div( minVal, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::saturating_div( maxVal, IntegerT{1}) == maxVal);
|
||||
assert(std::saturating_div( maxVal, maxVal) == IntegerT{1});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_div(IntegerT{27}, IntegerT{28}) == IntegerT{0});
|
||||
assert(std::saturating_div(IntegerT{28}, IntegerT{27}) == IntegerT{1});
|
||||
{
|
||||
constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_div(lesserVal, biggerVal) == IntegerT{0});
|
||||
assert(std::saturating_div(biggerVal, lesserVal) == IntegerT{1});
|
||||
}
|
||||
|
||||
// Unsigned integer division never overflows
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T mul_sat(T x, T y) noexcept; // freestanding
|
||||
// constexpr T saturating_mul(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <concepts>
|
||||
#include <numeric>
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
template <typename T, typename U>
|
||||
concept CanDo = requires(T x, U y) {
|
||||
{ std::mul_sat(x, y) } -> std::same_as<T>;
|
||||
{ std::saturating_mul(x, y) } -> std::same_as<T>;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
|
|
@ -0,0 +1,177 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T saturating_mul(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_mul(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_mul(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::saturating_mul(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::saturating_mul(IntegerT{-1}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::saturating_mul(IntegerT{-1}, minVal) == maxVal); // saturated
|
||||
assert(std::saturating_mul(IntegerT{-1}, maxVal) == -maxVal);
|
||||
|
||||
assert(std::saturating_mul(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{ 0}, minVal) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{ 0}, maxVal) == IntegerT{ 0});
|
||||
|
||||
assert(std::saturating_mul(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
|
||||
assert(std::saturating_mul(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
|
||||
assert(std::saturating_mul(IntegerT{ 1}, minVal) == minVal);
|
||||
assert(std::saturating_mul(IntegerT{ 1}, maxVal) == maxVal);
|
||||
|
||||
assert(std::saturating_mul( minVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::saturating_mul( minVal, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul( minVal, IntegerT{ 1}) == minVal);
|
||||
assert(std::saturating_mul( minVal, minVal) == maxVal); // saturated
|
||||
assert(std::saturating_mul( minVal, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::saturating_mul( maxVal, IntegerT{-1}) == -maxVal);
|
||||
assert(std::saturating_mul( maxVal, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_mul( maxVal, IntegerT{ 1}) == maxVal); // saturated
|
||||
assert(std::saturating_mul( maxVal, minVal) == minVal); // saturated
|
||||
assert(std::saturating_mul( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_mul(IntegerT{27}, IntegerT{ 2}) == IntegerT{54});
|
||||
assert(std::saturating_mul(IntegerT{ 2}, IntegerT{28}) == IntegerT{56});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_mul(x, y) == maxVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_mul(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_mul(x, y) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_mul(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_mul(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_mul(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// No saturation (0, 1)
|
||||
|
||||
assert(std::saturating_mul(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_mul(IntegerT{0}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::saturating_mul(IntegerT{0}, minVal) == IntegerT{0});
|
||||
assert(std::saturating_mul(IntegerT{0}, maxVal) == IntegerT{0});
|
||||
|
||||
assert(std::saturating_mul(IntegerT{1}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_mul(IntegerT{1}, IntegerT{1}) == IntegerT{1});
|
||||
assert(std::saturating_mul(IntegerT{1}, minVal) == minVal);
|
||||
assert(std::saturating_mul(IntegerT{1}, maxVal) == maxVal);
|
||||
|
||||
assert(std::saturating_mul( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_mul( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::saturating_mul( minVal, maxVal) == minVal);
|
||||
assert(std::saturating_mul( minVal, maxVal) == minVal);
|
||||
|
||||
assert(std::saturating_mul( maxVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_mul( maxVal, IntegerT{1}) == maxVal);
|
||||
assert(std::saturating_mul( maxVal, minVal) == IntegerT{0});
|
||||
assert(std::saturating_mul( maxVal, maxVal) == maxVal); // saturated
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_mul(IntegerT{28}, IntegerT{2}) == IntegerT{56});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_mul(x, y) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T sub_sat(T x, T y) noexcept; // freestanding
|
||||
// constexpr T saturating_sub(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <concepts>
|
||||
#include <numeric>
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
template <typename T, typename U>
|
||||
concept CanDo = requires(T x, U y) {
|
||||
{ std::sub_sat(x, y) } -> std::same_as<T>;
|
||||
{ std::saturating_sub(x, y) } -> std::same_as<T>;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T saturating_sub(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_sub(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_sub(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::saturating_sub(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::saturating_sub(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
|
||||
assert(std::saturating_sub(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-2});
|
||||
assert(std::saturating_sub(IntegerT{-1}, minVal) == IntegerT{-1} - minVal);
|
||||
assert(std::saturating_sub(IntegerT{-1}, maxVal) == IntegerT{-1} - maxVal);
|
||||
|
||||
assert(std::saturating_sub(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::saturating_sub(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::saturating_sub(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::saturating_sub(IntegerT{ 0}, minVal) == maxVal); // saturated
|
||||
assert(std::saturating_sub(IntegerT{ 0}, maxVal) == -maxVal);
|
||||
|
||||
assert(std::saturating_sub( minVal, IntegerT{-1}) == minVal - IntegerT{-1});
|
||||
assert(std::saturating_sub( minVal, IntegerT{ 0}) == minVal);
|
||||
assert(std::saturating_sub( minVal, IntegerT{ 1}) == minVal); // saturated
|
||||
assert(std::saturating_sub( minVal, minVal) == IntegerT{0});
|
||||
assert(std::saturating_sub( minVal, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::saturating_sub( maxVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::saturating_sub( maxVal, IntegerT{ 0}) == maxVal);
|
||||
assert(std::saturating_sub( maxVal, IntegerT{ 1}) == maxVal - IntegerT{ 1});
|
||||
assert(std::saturating_sub( maxVal, minVal) == maxVal); // saturated
|
||||
assert(std::saturating_sub( maxVal, maxVal) == IntegerT{0});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::saturating_sub(IntegerT{ 27}, IntegerT{-28}) == 55);
|
||||
assert(std::saturating_sub(IntegerT{ 27}, IntegerT{ 28}) == -1);
|
||||
assert(std::saturating_sub(IntegerT{-27}, IntegerT{ 28}) == -55);
|
||||
assert(std::saturating_sub(IntegerT{-27}, IntegerT{-28}) == 1);
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_sub(lesserVal, biggerVal) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
assert(std::saturating_sub(biggerVal, lesserVal) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::saturating_sub(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::saturating_sub(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (0, 1, min, max)
|
||||
|
||||
assert(std::saturating_sub(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_sub(IntegerT{0}, IntegerT{1}) == minVal); // saturated
|
||||
assert(std::saturating_sub(IntegerT{0}, minVal) == minVal);
|
||||
assert(std::saturating_sub(IntegerT{0}, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::saturating_sub(IntegerT{1}, IntegerT{0}) == IntegerT{1});
|
||||
assert(std::saturating_sub(IntegerT{1}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::saturating_sub(IntegerT{1}, minVal) == IntegerT{1});
|
||||
assert(std::saturating_sub(IntegerT{1}, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::saturating_sub( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::saturating_sub( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::saturating_sub( minVal, maxVal) == minVal);
|
||||
assert(std::saturating_sub( minVal, maxVal) == minVal);
|
||||
|
||||
assert(std::saturating_sub( maxVal, IntegerT{0}) == maxVal);
|
||||
assert(std::saturating_sub( maxVal, IntegerT{1}) == maxVal - IntegerT{1});
|
||||
assert(std::saturating_sub( maxVal, minVal) == maxVal);
|
||||
assert(std::saturating_sub( maxVal, maxVal) == IntegerT{0});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::saturating_sub(lesserVal, biggerVal) == minVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,159 +0,0 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++26
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template<class T>
|
||||
// constexpr T sub_sat(T x, T y) noexcept; // freestanding
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_signed() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::sub_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (-1, 0, 1, min, max)
|
||||
|
||||
assert(std::sub_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 0});
|
||||
assert(std::sub_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
|
||||
assert(std::sub_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-2});
|
||||
assert(std::sub_sat(IntegerT{-1}, minVal) == IntegerT{-1} - minVal);
|
||||
assert(std::sub_sat(IntegerT{-1}, maxVal) == IntegerT{-1} - maxVal);
|
||||
|
||||
assert(std::sub_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 1});
|
||||
assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
|
||||
assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{-1});
|
||||
assert(std::sub_sat(IntegerT{ 0}, minVal) == maxVal); // saturated
|
||||
assert(std::sub_sat(IntegerT{ 0}, maxVal) == -maxVal);
|
||||
|
||||
assert(std::sub_sat( minVal, IntegerT{-1}) == minVal - IntegerT{-1});
|
||||
assert(std::sub_sat( minVal, IntegerT{ 0}) == minVal);
|
||||
assert(std::sub_sat( minVal, IntegerT{ 1}) == minVal); // saturated
|
||||
assert(std::sub_sat( minVal, minVal) == IntegerT{0});
|
||||
assert(std::sub_sat( minVal, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::sub_sat( maxVal, IntegerT{-1}) == maxVal); // saturated
|
||||
assert(std::sub_sat( maxVal, IntegerT{ 0}) == maxVal);
|
||||
assert(std::sub_sat( maxVal, IntegerT{ 1}) == maxVal - IntegerT{ 1});
|
||||
assert(std::sub_sat( maxVal, minVal) == maxVal); // saturated
|
||||
assert(std::sub_sat( maxVal, maxVal) == IntegerT{0});
|
||||
|
||||
// No saturation (no limit values)
|
||||
|
||||
assert(std::sub_sat(IntegerT{ 27}, IntegerT{-28}) == 55);
|
||||
assert(std::sub_sat(IntegerT{ 27}, IntegerT{ 28}) == -1);
|
||||
assert(std::sub_sat(IntegerT{-27}, IntegerT{ 28}) == -55);
|
||||
assert(std::sub_sat(IntegerT{-27}, IntegerT{-28}) == 1);
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated
|
||||
}
|
||||
{
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
assert(std::sub_sat(biggerVal, lesserVal) == maxVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename IntegerT>
|
||||
constexpr bool test_unsigned() {
|
||||
constexpr auto minVal = std::numeric_limits<IntegerT>::min();
|
||||
constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
|
||||
|
||||
std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);
|
||||
|
||||
static_assert(noexcept(std::sub_sat(minVal, maxVal)));
|
||||
|
||||
// clang-format off
|
||||
|
||||
// Limit values (0, 1, min, max)
|
||||
|
||||
assert(std::sub_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::sub_sat(IntegerT{0}, IntegerT{1}) == minVal); // saturated
|
||||
assert(std::sub_sat(IntegerT{0}, minVal) == minVal);
|
||||
assert(std::sub_sat(IntegerT{0}, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::sub_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1});
|
||||
assert(std::sub_sat(IntegerT{1}, IntegerT{1}) == IntegerT{0});
|
||||
assert(std::sub_sat(IntegerT{1}, minVal) == IntegerT{1});
|
||||
assert(std::sub_sat(IntegerT{1}, maxVal) == minVal); // saturated
|
||||
|
||||
assert(std::sub_sat( minVal, IntegerT{0}) == IntegerT{0});
|
||||
assert(std::sub_sat( minVal, IntegerT{1}) == minVal);
|
||||
assert(std::sub_sat( minVal, maxVal) == minVal);
|
||||
assert(std::sub_sat( minVal, maxVal) == minVal);
|
||||
|
||||
assert(std::sub_sat( maxVal, IntegerT{0}) == maxVal);
|
||||
assert(std::sub_sat( maxVal, IntegerT{1}) == maxVal - IntegerT{1});
|
||||
assert(std::sub_sat( maxVal, minVal) == maxVal);
|
||||
assert(std::sub_sat( maxVal, maxVal) == IntegerT{0});
|
||||
|
||||
// Saturation (no limit values)
|
||||
|
||||
{
|
||||
constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
|
||||
constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
|
||||
assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
// Signed
|
||||
test_signed<signed char>();
|
||||
test_signed<short int>();
|
||||
test_signed<int>();
|
||||
test_signed<long int>();
|
||||
test_signed<long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_signed<__int128_t>();
|
||||
#endif
|
||||
// Unsigned
|
||||
test_unsigned<unsigned char>();
|
||||
test_unsigned<unsigned short int>();
|
||||
test_unsigned<unsigned int>();
|
||||
test_unsigned<unsigned long int>();
|
||||
test_unsigned<unsigned long long int>();
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
test_unsigned<__uint128_t>();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1222,7 +1222,7 @@ feature_test_macros = [
|
|||
},
|
||||
{
|
||||
"name": "__cpp_lib_saturation_arithmetic",
|
||||
"values": {"c++26": 202311}, # P0543R3 Saturation arithmetic
|
||||
"values": {"c++26": 202603}, # P0543R3 Saturation arithmetic
|
||||
"headers": ["numeric"],
|
||||
},
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue