diff options
author | Hristo Hristov <hristo.goshev.hristov@gmail.com> | 2024-01-22 06:57:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-22 06:57:45 +0200 |
commit | 03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4 (patch) | |
tree | e4b1ffdf5ce979e79ab1f002651289afdf048a7f /libcxx/test/std/numerics/numeric.ops | |
parent | 85337df9e36a10941faa14472b1a4ea0607bfced (diff) | |
download | llvm-03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4.zip llvm-03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4.tar.gz llvm-03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4.tar.bz2 |
[libc++][numeric] P0543R3: Saturation arithmetic (#77967)
Implements: https://wg21.link/P0543R3
- https://eel.is/c++draft/numeric.sat
Additional references:
- Division: https://eel.is/c++draft/expr.mul#4
- Arithmetic conversions: https://eel.is/c++draft/expr.arith.conv#1
- Clang builtins:
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions
Depends on: https://github.com/llvm/llvm-project/pull/78086
---------
Co-authored-by: Zingam <zingam@outlook.com>
Co-authored-by: Mark de Wever <zar-rpg@xs4all.nl>
Diffstat (limited to 'libcxx/test/std/numerics/numeric.ops')
11 files changed, 1546 insertions, 0 deletions
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp new file mode 100644 index 0000000..38b6d62 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <numeric> + +// template<class T> +// constexpr T add_sat(T x, T y) noexcept; // freestanding + +#include <concepts> +#include <numeric> + +#include "test_macros.h" + +template <typename T, typename U> +concept CanDo = requires(T x, U y) { + { std::add_sat(x, y) } -> std::same_as<T>; +}; + +template <typename T, typename U> +constexpr void test_constraint_success() { + static_assert(CanDo<T, T>); + static_assert(!CanDo<U, T>); + static_assert(!CanDo<T, U>); +} + +template <typename T> +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo<T, T>); + static_assert(!CanDo<I, T>); + static_assert(!CanDo<T, I>); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<int, SI>(); + test_constraint_success<long int, SI>(); + test_constraint_success<long long int, int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success<unsigned char, UI>(); + test_constraint_success<unsigned short int, UI>(); + test_constraint_success<unsigned int, UI>(); + test_constraint_success<unsigned long int, UI>(); + test_constraint_success<unsigned long long int, unsigned int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail<bool>(); + test_constraint_fail<char>(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail<wchar_t>(); +#endif + test_constraint_fail<char8_t>(); + test_constraint_fail<char16_t>(); + test_constraint_fail<char32_t>(); + test_constraint_fail<float>(); + test_constraint_fail<double>(); + test_constraint_fail<long double>(); +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp new file mode 100644 index 0000000..036bf53 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp @@ -0,0 +1,173 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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; +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp new file mode 100644 index 0000000..b1ef834 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// REQUIRES: has-unix-headers +// REQUIRES: libcpp-hardening-mode={{extensive|debug}} +// XFAIL: availability-verbose_abort-missing + +// <numeric> + +// template<class T> +// constexpr T div_sat(T x, T y) noexcept; // freestanding + +#include <cassert> +#include <numeric> + +#include "check_assertion.h" +#include "test_macros.h" + +template <typename IntegerT> +void test_runtime_assertion() { + TEST_LIBCPP_ASSERT_FAILURE((void)std::div_sat(IntegerT{27}, IntegerT{0}), "Division by 0 is undefined"); +} + +bool test() { + // Signed + test_runtime_assertion<signed char>(); + test_runtime_assertion<short int>(); + test_runtime_assertion<int>(); + test_runtime_assertion<long int>(); + test_runtime_assertion<long long int>(); +#ifndef TEST_HAS_NO_INT128 + test_runtime_assertion<__int128_t>(); +#endif + // Unsigned + test_runtime_assertion<unsigned char>(); + test_runtime_assertion<unsigned short int>(); + test_runtime_assertion<unsigned int>(); + test_runtime_assertion<unsigned long int>(); + test_runtime_assertion<unsigned long long int>(); +#ifndef TEST_HAS_NO_INT128 + test_runtime_assertion<__uint128_t>(); +#endif + + return true; +} + +int main(int, char**) { + test(); + + return 0; +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp new file mode 100644 index 0000000..32d77f71 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <numeric> + +// template<class T> +// constexpr T div_sat(T x, T y) noexcept; // freestanding + +#include <concepts> +#include <numeric> +#include <type_traits> + +#include "test_macros.h" + +// Constraints + +template <typename T, typename U> +concept CanDo = requires(T x, U y) { + { std::div_sat(x, y) } -> std::same_as<T>; +}; + +template <typename T, typename U> +constexpr void test_constraint_success() { + static_assert(CanDo<T, T>); + static_assert(!CanDo<U, T>); + static_assert(!CanDo<T, U>); +} + +template <typename T> +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo<T, T>); + static_assert(!CanDo<I, T>); + static_assert(!CanDo<T, I>); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<int, SI>(); + test_constraint_success<long int, SI>(); + test_constraint_success<long long int, int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success<unsigned char, UI>(); + test_constraint_success<unsigned short int, UI>(); + test_constraint_success<unsigned int, UI>(); + test_constraint_success<unsigned long int, UI>(); + test_constraint_success<unsigned long long int, unsigned int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail<bool>(); + test_constraint_fail<char>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_fail<wchar_t>(); +#endif + test_constraint_fail<char8_t>(); + test_constraint_fail<char16_t>(); + test_constraint_fail<char32_t>(); + test_constraint_fail<float>(); + test_constraint_fail<double>(); + test_constraint_fail<long double>(); +} + +// 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)>; + +template <auto N> +QuotT<N> div_by_zero(); + +template <auto N> +concept CanDivByZero = requires { div_by_zero<N>(); }; + +static_assert(!CanDivByZero<static_cast<signed char>(0)>); +static_assert(!CanDivByZero<static_cast<short int>(0)>); +static_assert(!CanDivByZero<0>); +static_assert(!CanDivByZero<0L>); +static_assert(!CanDivByZero<0LL>); +#ifndef TEST_HAS_NO_INT128 +static_assert(!CanDivByZero<static_cast<__int128_t>(0)>); +#endif +static_assert(!CanDivByZero<static_cast<unsigned char>(0)>); +static_assert(!CanDivByZero<static_cast<unsigned short int>(0)>); +static_assert(!CanDivByZero<0U>); +static_assert(!CanDivByZero<0UL>); +static_assert(!CanDivByZero<0ULL>); +#ifndef TEST_HAS_NO_INT128 +static_assert(!CanDivByZero<static_cast<__uint128_t>(0)>); +#endif diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp new file mode 100644 index 0000000..b1cace7 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp @@ -0,0 +1,158 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 +// The test uses "Placeholder variables with no name" + +// <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(); + + // TODO(LLVM-20) remove [[maybe_unused]] and `{}` scope since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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; +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp new file mode 100644 index 0000000..f1762d2 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <numeric> + +// template<class T> +// constexpr T mul_sat(T x, T y) noexcept; // freestanding + +#include <concepts> +#include <numeric> + +#include "test_macros.h" + +template <typename T, typename U> +concept CanDo = requires(T x, U y) { + { std::mul_sat(x, y) } -> std::same_as<T>; +}; + +template <typename T, typename U> +constexpr void test_constraint_success() { + static_assert(CanDo<T, T>); + static_assert(!CanDo<U, T>); + static_assert(!CanDo<T, U>); +} + +template <typename T> +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo<T, T>); + static_assert(!CanDo<I, T>); + static_assert(!CanDo<T, I>); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<int, SI>(); + test_constraint_success<long int, SI>(); + test_constraint_success<long long int, int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success<unsigned char, UI>(); + test_constraint_success<unsigned short int, UI>(); + test_constraint_success<unsigned int, UI>(); + test_constraint_success<unsigned long int, UI>(); + test_constraint_success<unsigned long long int, unsigned int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail<bool>(); + test_constraint_fail<char>(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail<wchar_t>(); +#endif + test_constraint_fail<char8_t>(); + test_constraint_fail<char16_t>(); + test_constraint_fail<char32_t>(); + test_constraint_fail<float>(); + test_constraint_fail<double>(); + test_constraint_fail<long double>(); +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp new file mode 100644 index 0000000..2c8eec5 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp @@ -0,0 +1,180 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 +// The test uses "Placeholder variables with no name" + +// <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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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; +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp new file mode 100644 index 0000000..9b035c1 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <numeric> + +// template<class R, class T> +// constexpr R saturate_cast(T x) noexcept; // freestanding + +#include <concepts> +#include <numeric> + +#include "test_macros.h" + +template <typename R, typename T> +concept CanDo = requires(T x) { + { std::saturate_cast<R>(x) } -> std::same_as<R>; +}; + +template <typename R, typename T> +constexpr void test_constraint_success() { + static_assert(CanDo<R, T>); + static_assert(CanDo<T, T>); + static_assert(CanDo<T, R>); +} + +template <typename T> +constexpr void test_constraint_fail() { + using I = int; + using R = T; + static_assert(!CanDo<R, T>); + static_assert(!CanDo<T, R>); + static_assert(!CanDo<I, T>); + static_assert(!CanDo<T, I>); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success<SI, signed char>(); + test_constraint_success<SI, short int>(); + test_constraint_success<SI, signed char>(); + test_constraint_success<SI, short int>(); + test_constraint_success<SI, int>(); + test_constraint_success<SI, long int>(); + test_constraint_success<int, long long int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success<UI, unsigned char>(); + test_constraint_success<UI, unsigned short int>(); + test_constraint_success<UI, unsigned int>(); + test_constraint_success<UI, unsigned long int>(); + test_constraint_success<unsigned int, unsigned long long int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<UI, __uint128_t>(); +#endif + + // Contraint failure + test_constraint_fail<bool>(); + test_constraint_fail<char>(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail<wchar_t>(); +#endif + test_constraint_fail<char8_t>(); + test_constraint_fail<char16_t>(); + test_constraint_fail<char32_t>(); + test_constraint_fail<float>(); + test_constraint_fail<double>(); + test_constraint_fail<long double>(); +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp new file mode 100644 index 0000000..c06a9ed --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp @@ -0,0 +1,397 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <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" +#include <print> + +// 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 + + // TODO(LLVM-20) remove [[maybe_unused]] and `{}` scope since all supported compilers support "Placeholder variables with no name", + // here and below... + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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); + + { [[maybe_unused]] 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) == ULONG_MAX); // saturated + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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 + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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 + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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)); + + { [[maybe_unused]] 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; +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp new file mode 100644 index 0000000..9234819 --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 + +// <numeric> + +// template<class T> +// constexpr T sub_sat(T x, T y) noexcept; // freestanding + +#include <concepts> +#include <numeric> + +#include "test_macros.h" + +template <typename T, typename U> +concept CanDo = requires(T x, U y) { + { std::sub_sat(x, y) } -> std::same_as<T>; +}; + +template <typename T, typename U> +constexpr void test_constraint_success() { + static_assert(CanDo<T, T>); + static_assert(!CanDo<U, T>); + static_assert(!CanDo<T, U>); +} + +template <typename T> +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo<T, T>); + static_assert(!CanDo<I, T>); + static_assert(!CanDo<T, I>); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<signed char, SI>(); + test_constraint_success<short int, SI>(); + test_constraint_success<int, SI>(); + test_constraint_success<long int, SI>(); + test_constraint_success<long long int, int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success<unsigned char, UI>(); + test_constraint_success<unsigned short int, UI>(); + test_constraint_success<unsigned int, UI>(); + test_constraint_success<unsigned long int, UI>(); + test_constraint_success<unsigned long long int, unsigned int>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail<bool>(); + test_constraint_fail<char>(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_fail<wchar_t>(); +#endif + test_constraint_fail<char8_t>(); + test_constraint_fail<char16_t>(); + test_constraint_fail<char32_t>(); + test_constraint_fail<float>(); + test_constraint_fail<double>(); + test_constraint_fail<long double>(); +} diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp new file mode 100644 index 0000000..d7bdf2c --- /dev/null +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp @@ -0,0 +1,162 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 +// The test uses "Placeholder variables with no name" + +// <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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] 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; +} |