From 03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Mon, 22 Jan 2024 06:57:45 +0200 Subject: [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 Co-authored-by: Mark de Wever --- libcxx/docs/FeatureTestMacroTable.rst | 2 +- libcxx/docs/ReleaseNotes/18.rst | 1 + libcxx/docs/Status/Cxx2cPapers.csv | 2 +- libcxx/include/CMakeLists.txt | 1 + libcxx/include/__numeric/saturation_arithmetic.h | 110 ++++++ libcxx/include/libcxx.imp | 1 + libcxx/include/module.modulemap.in | 1 + libcxx/include/numeric | 13 + libcxx/include/version | 2 +- libcxx/modules/std/numeric.inc | 10 + .../numeric.version.compile.pass.cpp | 16 +- .../version.version.compile.pass.cpp | 16 +- .../numeric.ops.sat/add_sat.compile.pass.cpp | 77 ++++ .../numeric.ops/numeric.ops.sat/add_sat.pass.cpp | 173 +++++++++ .../numeric.ops.sat/div_sat.assert.pass.cpp | 58 +++ .../numeric.ops.sat/div_sat.compile.pass.cpp | 108 ++++++ .../numeric.ops/numeric.ops.sat/div_sat.pass.cpp | 158 ++++++++ .../numeric.ops.sat/mul_sat.compile.pass.cpp | 77 ++++ .../numeric.ops/numeric.ops.sat/mul_sat.pass.cpp | 180 ++++++++++ .../numeric.ops.sat/saturate_cast.compile.pass.cpp | 79 ++++ .../numeric.ops.sat/saturate_cast.pass.cpp | 397 +++++++++++++++++++++ .../numeric.ops.sat/sub_sat.compile.pass.cpp | 77 ++++ .../numeric.ops/numeric.ops.sat/sub_sat.pass.cpp | 162 +++++++++ .../generate_feature_test_macro_components.py | 5 +- 24 files changed, 1697 insertions(+), 29 deletions(-) create mode 100644 libcxx/include/__numeric/saturation_arithmetic.h create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst index 82e51d4..d0d057e 100644 --- a/libcxx/docs/FeatureTestMacroTable.rst +++ b/libcxx/docs/FeatureTestMacroTable.rst @@ -432,7 +432,7 @@ Status --------------------------------------------------- ----------------- ``__cpp_lib_rcu`` *unimplemented* --------------------------------------------------- ----------------- - ``__cpp_lib_saturation_arithmetic`` *unimplemented* + ``__cpp_lib_saturation_arithmetic`` ``202311L`` --------------------------------------------------- ----------------- ``__cpp_lib_smart_ptr_owner_equality`` *unimplemented* --------------------------------------------------- ----------------- diff --git a/libcxx/docs/ReleaseNotes/18.rst b/libcxx/docs/ReleaseNotes/18.rst index 0b43d76..b868b37 100644 --- a/libcxx/docs/ReleaseNotes/18.rst +++ b/libcxx/docs/ReleaseNotes/18.rst @@ -75,6 +75,7 @@ Implemented Papers - P2909R4 - Fix formatting of code units as integers (Dude, where’s my ``char``?) - P2821R5 - ``span.at()`` - P0521R0 - Proposed Resolution for CA 14 (``shared_ptr`` ``use_count/unique``) +- P0543R3 - Saturation arithmetic - P1759R6 - Native handles and file streams - P2868R3 - Remove Deprecated ``std::allocator`` Typedef From C++26 - P2517R1 - Add a conditional ``noexcept`` specification to ``std::apply`` diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv index e49290b..c45aa3c 100644 --- a/libcxx/docs/Status/Cxx2cPapers.csv +++ b/libcxx/docs/Status/Cxx2cPapers.csv @@ -27,7 +27,7 @@ "`P2714R1 `__","LWG","Bind front and back to NTTP callables","Varna June 2023","","","" "`P2630R4 `__","LWG","``submdspan``","Varna June 2023","","","" "","","","","","","" -"`P0543R3 `__","LWG","Saturation arithmetic","Kona November 2023","","","" +"`P0543R3 `__","LWG","Saturation arithmetic","Kona November 2023","|Complete|","18.0","" "`P2407R5 `__","LWG","Freestanding Library: Partial Classes","Kona November 2023","","","" "`P2546R5 `__","LWG","Debugging Support","Kona November 2023","","","" "`P2905R2 `__","LWG","Runtime format strings","Kona November 2023","|Complete|","18.0","|format| |DR|" diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 9fdf978..ed721d4 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -569,6 +569,7 @@ set(files __numeric/pstl_reduce.h __numeric/pstl_transform_reduce.h __numeric/reduce.h + __numeric/saturation_arithmetic.h __numeric/transform_exclusive_scan.h __numeric/transform_inclusive_scan.h __numeric/transform_reduce.h diff --git a/libcxx/include/__numeric/saturation_arithmetic.h b/libcxx/include/__numeric/saturation_arithmetic.h new file mode 100644 index 0000000..50274c6 --- /dev/null +++ b/libcxx/include/__numeric/saturation_arithmetic.h @@ -0,0 +1,110 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H +#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H + +#include <__concepts/arithmetic.h> +#include <__config> +#include <__utility/cmp.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 26 + +template <__libcpp_integer _Tp> +_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept { + if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum)) + return __sum; + // Handle overflow + if constexpr (__libcpp_unsigned_integer<_Tp>) { + return std::numeric_limits<_Tp>::max(); + } else { + // Signed addition overflow + if (__x > 0) + // Overflows if (x > 0 && y > 0) + return std::numeric_limits<_Tp>::max(); + else + // Overflows if (x < 0 && y < 0) + return std::numeric_limits<_Tp>::min(); + } +} + +template <__libcpp_integer _Tp> +_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept { + if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub)) + return __sub; + // Handle overflow + if constexpr (__libcpp_unsigned_integer<_Tp>) { + // Overflows if (x < y) + return std::numeric_limits<_Tp>::min(); + } else { + // Signed subtration overflow + if (__x >= 0) + // Overflows if (x >= 0 && y < 0) + return std::numeric_limits<_Tp>::max(); + else + // Overflows if (x < 0 && y > 0) + return std::numeric_limits<_Tp>::min(); + } +} + +template <__libcpp_integer _Tp> +_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept { + if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul)) + return __mul; + // Handle overflow + if constexpr (__libcpp_unsigned_integer<_Tp>) { + return std::numeric_limits<_Tp>::max(); + } else { + // Signed multiplication overflow + if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0)) + return std::numeric_limits<_Tp>::max(); + // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0) + return std::numeric_limits<_Tp>::min(); + } +} + +template <__libcpp_integer _Tp> +_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept { + _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined"); + if constexpr (__libcpp_unsigned_integer<_Tp>) { + return __x / __y; + } else { + // Handle signed division overflow + if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1}) + return std::numeric_limits<_Tp>::max(); + return __x / __y; + } +} + +template <__libcpp_integer _Rp, __libcpp_integer _Tp> +_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_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. + + // Handle overflow + if (std::cmp_less(__x, std::numeric_limits<_Rp>::min())) + return std::numeric_limits<_Rp>::min(); + if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max())) + return std::numeric_limits<_Rp>::max(); + // No overflow + return static_cast<_Rp>(__x); +} + +#endif // _LIBCPP_STD_VER >= 26 + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp index 8616f96..45fa4a9 100644 --- a/libcxx/include/libcxx.imp +++ b/libcxx/include/libcxx.imp @@ -559,6 +559,7 @@ { include: [ "<__numeric/pstl_reduce.h>", "private", "", "public" ] }, { include: [ "<__numeric/pstl_transform_reduce.h>", "private", "", "public" ] }, { include: [ "<__numeric/reduce.h>", "private", "", "public" ] }, + { include: [ "<__numeric/saturation_arithmetic.h>", "private", "", "public" ] }, { include: [ "<__numeric/transform_exclusive_scan.h>", "private", "", "public" ] }, { include: [ "<__numeric/transform_inclusive_scan.h>", "private", "", "public" ] }, { include: [ "<__numeric/transform_reduce.h>", "private", "", "public" ] }, diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in index d10670d..194a74a 100644 --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -1580,6 +1580,7 @@ module std_private_numeric_pstl_transform_reduce [system] { export * } module std_private_numeric_reduce [system] { header "__numeric/reduce.h" } +module std_private_numeric_saturation_arithmetic [system] { header "__numeric/saturation_arithmetic.h" } module std_private_numeric_transform_exclusive_scan [system] { header "__numeric/transform_exclusive_scan.h" } module std_private_numeric_transform_inclusive_scan [system] { header "__numeric/transform_inclusive_scan.h" } module std_private_numeric_transform_reduce [system] { header "__numeric/transform_reduce.h" } diff --git a/libcxx/include/numeric b/libcxx/include/numeric index d09d0a8..0fe7115 100644 --- a/libcxx/include/numeric +++ b/libcxx/include/numeric @@ -140,6 +140,18 @@ template template constexpr T* midpoint(T* a, T* b); // C++20 +// [numeric.sat], saturation arithmetic +template +constexpr T add_sat(T x, T y) noexcept; // freestanding, Since C++26 +template +constexpr T sub_sat(T x, T y) noexcept; // freestanding, Since C++26 +template +constexpr T mul_sat(T x, T y) noexcept; // freestanding, Since C++26 +template +constexpr T div_sat(T x, T y) noexcept; // freestanding, Since C++26 +template +constexpr T saturate_cast(U x) noexcept; // freestanding, Since C++26 + } // std */ @@ -160,6 +172,7 @@ template #include <__numeric/pstl_reduce.h> #include <__numeric/pstl_transform_reduce.h> #include <__numeric/reduce.h> +#include <__numeric/saturation_arithmetic.h> #include <__numeric/transform_exclusive_scan.h> #include <__numeric/transform_inclusive_scan.h> #include <__numeric/transform_reduce.h> diff --git a/libcxx/include/version b/libcxx/include/version index 7b2f487..9e26da8 100644 --- a/libcxx/include/version +++ b/libcxx/include/version @@ -504,7 +504,7 @@ __cpp_lib_within_lifetime 202306L // # define __cpp_lib_out_ptr 202311L # define __cpp_lib_ratio 202306L // # define __cpp_lib_rcu 202306L -// # define __cpp_lib_saturation_arithmetic 202311L +# define __cpp_lib_saturation_arithmetic 202311L // # define __cpp_lib_smart_ptr_owner_equality 202306L # define __cpp_lib_span_at 202311L # define __cpp_lib_span_initializer_list 202311L diff --git a/libcxx/modules/std/numeric.inc b/libcxx/modules/std/numeric.inc index d2b7688..3bc7b23 100644 --- a/libcxx/modules/std/numeric.inc +++ b/libcxx/modules/std/numeric.inc @@ -54,4 +54,14 @@ export namespace std { // [numeric.ops.midpoint], midpoint using std::midpoint; + +#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; +#endif + } // namespace std diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp index b510eef..d132b7c 100644 --- a/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp +++ b/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp @@ -263,17 +263,11 @@ # endif # endif -# if !defined(_LIBCPP_VERSION) -# 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" -# endif -# else // _LIBCPP_VERSION -# ifdef __cpp_lib_saturation_arithmetic -# error "__cpp_lib_saturation_arithmetic should not be defined because it is unimplemented in libc++!" -# endif +# 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" # endif #endif // TEST_STD_VER > 23 diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp index fa18853..c319940 100644 --- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp +++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp @@ -7167,17 +7167,11 @@ # error "__cpp_lib_sample should have the value 201603L in c++26" # endif -# if !defined(_LIBCPP_VERSION) -# 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" -# endif -# else // _LIBCPP_VERSION -# ifdef __cpp_lib_saturation_arithmetic -# error "__cpp_lib_saturation_arithmetic should not be defined because it is unimplemented in libc++!" -# endif +# 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" # endif # ifndef __cpp_lib_scoped_lock 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 + +// + +// template +// constexpr T add_sat(T x, T y) noexcept; // freestanding + +#include +#include + +#include "test_macros.h" + +template +concept CanDo = requires(T x, U y) { + { std::add_sat(x, y) } -> std::same_as; +}; + +template +constexpr void test_constraint_success() { + static_assert(CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +template +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail(); + test_constraint_fail(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail(); +#endif + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); +} 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 + +// + +// template +// constexpr T add_sat(T x, T y) noexcept; // freestanding + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool test_signed() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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 +constexpr bool test_unsigned() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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(); + test_signed(); + test_signed(); + test_signed(); + test_signed(); +#ifndef TEST_HAS_NO_INT128 + test_signed<__int128_t>(); +#endif + // Unsigned + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); +#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 + +// + +// template +// constexpr T div_sat(T x, T y) noexcept; // freestanding + +#include +#include + +#include "check_assertion.h" +#include "test_macros.h" + +template +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(); + test_runtime_assertion(); + test_runtime_assertion(); + test_runtime_assertion(); + test_runtime_assertion(); +#ifndef TEST_HAS_NO_INT128 + test_runtime_assertion<__int128_t>(); +#endif + // Unsigned + test_runtime_assertion(); + test_runtime_assertion(); + test_runtime_assertion(); + test_runtime_assertion(); + test_runtime_assertion(); +#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 + +// + +// template +// constexpr T div_sat(T x, T y) noexcept; // freestanding + +#include +#include +#include + +#include "test_macros.h" + +// Constraints + +template +concept CanDo = requires(T x, U y) { + { std::div_sat(x, y) } -> std::same_as; +}; + +template +constexpr void test_constraint_success() { + static_assert(CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +template +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail(); + test_constraint_fail(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_fail(); +#endif + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); +} + +// A function call expression that violates the precondition in the Preconditions: element is not a core constant expression (7.7 [expr.const]). + +template +using QuotT = std::integral_constant; + +template +QuotT div_by_zero(); + +template +concept CanDivByZero = requires { div_by_zero(); }; + +static_assert(!CanDivByZero(0)>); +static_assert(!CanDivByZero(0)>); +static_assert(!CanDivByZero<0>); +static_assert(!CanDivByZero<0L>); +static_assert(!CanDivByZero<0LL>); +#ifndef TEST_HAS_NO_INT128 +static_assert(!CanDivByZero(0)>); +#endif +static_assert(!CanDivByZero(0)>); +static_assert(!CanDivByZero(0)>); +static_assert(!CanDivByZero<0U>); +static_assert(!CanDivByZero<0UL>); +static_assert(!CanDivByZero<0ULL>); +#ifndef TEST_HAS_NO_INT128 +static_assert(!CanDivByZero(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" + +// + +// template +// constexpr T div_sat(T x, T y) noexcept; // freestanding + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool test_signed() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] and `{}` scope since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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 +constexpr bool test_unsigned() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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(); + test_signed(); + test_signed(); + test_signed(); + test_signed(); +#ifndef TEST_HAS_NO_INT128 + test_signed<__int128_t>(); +#endif + // Unsigned + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); +#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 + +// + +// template +// constexpr T mul_sat(T x, T y) noexcept; // freestanding + +#include +#include + +#include "test_macros.h" + +template +concept CanDo = requires(T x, U y) { + { std::mul_sat(x, y) } -> std::same_as; +}; + +template +constexpr void test_constraint_success() { + static_assert(CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +template +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail(); + test_constraint_fail(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail(); +#endif + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); +} 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" + +// + +// template +// constexpr T mul_sat(T x, T y) noexcept; // freestanding + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool test_signed() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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 +constexpr bool test_unsigned() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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(); + test_signed(); + test_signed(); + test_signed(); + test_signed(); +#ifndef TEST_HAS_NO_INT128 + test_signed<__int128_t>(); +#endif + // Unsigned + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); +#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 + +// + +// template +// constexpr R saturate_cast(T x) noexcept; // freestanding + +#include +#include + +#include "test_macros.h" + +template +concept CanDo = requires(T x) { + { std::saturate_cast(x) } -> std::same_as; +}; + +template +constexpr void test_constraint_success() { + static_assert(CanDo); + static_assert(CanDo); + static_assert(CanDo); +} + +template +constexpr void test_constraint_fail() { + using I = int; + using R = T; + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success(); +#endif + + // Contraint failure + test_constraint_fail(); + test_constraint_fail(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_constraint_fail(); +#endif + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); +} 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 + +// + +// template +// constexpr R saturate_cast(T x) noexcept; // freestanding + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include + +// Smaller to larger +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); + +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); + +// Same type +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); + +// Larger to smaller +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); + +static_assert(noexcept(std::saturate_cast(std::numeric_limits::max()))); +static_assert(noexcept(std::saturate_cast(std::numeric_limits::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::min(); + constexpr auto sZero = SIntT{0}; + constexpr auto sBigMax = std::numeric_limits::max(); + + constexpr auto uZero = UIntT{0}; + constexpr auto uBigMax = std::numeric_limits::max(); + + // Constants to avoid casting in place + + constexpr auto O_C = static_cast(0); + constexpr auto O_UC = static_cast(0); + + constexpr auto O_S = static_cast(0); + constexpr auto O_US = static_cast(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 decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == SCHAR_MIN); + assert(std::saturate_cast( O_C) == O_C); + assert(std::saturate_cast(SCHAR_MAX) == SCHAR_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == O_C); + assert(std::saturate_cast(UCHAR_MAX) == SCHAR_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast(sBigMin) == SCHAR_MIN); // saturated + assert(std::saturate_cast( sZero) == O_C); + assert(std::saturate_cast(sBigMax) == SCHAR_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == O_C); + assert(std::saturate_cast(uBigMax) == SCHAR_MAX); // saturated + + // short + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == static_cast(SCHAR_MIN)); + assert(std::saturate_cast( O_C) == O_S); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == O_S); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SHRT_MAX); } + assert(std::saturate_cast( SHRT_MIN) == SHRT_MIN); + assert(std::saturate_cast( O_S) == O_S); + assert(std::saturate_cast( SHRT_MAX) == SHRT_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(USHRT_MAX); } + assert(std::saturate_cast( O_US) == O_S); + assert(std::saturate_cast(USHRT_MAX) == SHRT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == SHRT_MIN); // saturated + assert(std::saturate_cast( sZero) == O_S); + assert(std::saturate_cast( sBigMax) == SHRT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == O_S); + assert(std::saturate_cast( uBigMax) == SHRT_MAX); // saturated + + // int + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == static_cast(SCHAR_MIN)); + assert(std::saturate_cast( O_C) == 0); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(INT_MAX); } + assert(std::saturate_cast( INT_MIN) == INT_MIN); + assert(std::saturate_cast( 0) == 0); + assert(std::saturate_cast( INT_MAX) == INT_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UINT_MAX); } + assert(std::saturate_cast( 0) == 0); + assert(std::saturate_cast(UINT_MAX) == INT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == INT_MIN); // saturated + assert(std::saturate_cast( sZero) == 0); + assert(std::saturate_cast( sBigMax) == INT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0); + assert(std::saturate_cast( uBigMax) == INT_MAX); // saturated + + // long + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == static_cast(SCHAR_MIN)); + assert(std::saturate_cast( O_C) == 0L); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0L); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(LONG_MAX); } + assert(std::saturate_cast( LONG_MIN) == LONG_MIN); + assert(std::saturate_cast( 0L) == 0L); + assert(std::saturate_cast( LONG_MAX) == LONG_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(ULONG_MAX); } + assert(std::saturate_cast( 0UL) == 0L); + assert(std::saturate_cast(ULONG_MAX) == LONG_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == LONG_MIN); // saturated + assert(std::saturate_cast( sZero) == 0L); + assert(std::saturate_cast( sBigMax) == LONG_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0L); + assert(std::saturate_cast( uBigMax) == LONG_MAX); // saturated + + // long long + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == static_cast(SCHAR_MIN)); + assert(std::saturate_cast( 0LL) == 0LL); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0LL); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(LLONG_MIN); } + assert(std::saturate_cast(LLONG_MIN) == LLONG_MIN); + assert(std::saturate_cast( 0LL) == 0LL); + assert(std::saturate_cast(LLONG_MAX) == LLONG_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(ULLONG_MAX); } + assert(std::saturate_cast( 0ULL) == 0LL); + assert(std::saturate_cast(ULLONG_MAX) == LLONG_MAX); // saturated + +#ifndef TEST_HAS_NO_INT128 + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == LLONG_MIN); // (128-bit) saturated + assert(std::saturate_cast( sZero) == 0LL); + assert(std::saturate_cast( sBigMax) == LLONG_MAX); // (128-bit) saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0LL); + assert(std::saturate_cast( 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 decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == O_UC); + assert(std::saturate_cast( O_C) == O_UC); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == O_UC); + assert(std::saturate_cast(UCHAR_MAX) == UCHAR_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == O_UC); // saturated + assert(std::saturate_cast( sZero) == O_UC); + assert(std::saturate_cast( sBigMax) == UCHAR_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == O_UC); + assert(std::saturate_cast( uBigMax) == UCHAR_MAX); // saturated + + // unsigned short + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == O_US); + assert(std::saturate_cast( O_C) == O_US); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == O_US); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MIN); } + assert(std::saturate_cast( SHRT_MIN) == O_US); + assert(std::saturate_cast( O_S) == O_US); + assert(std::saturate_cast( SHRT_MAX) == static_cast(SHRT_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_US) == O_US); + assert(std::saturate_cast(USHRT_MAX) == USHRT_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == O_US); // saturated + assert(std::saturate_cast( sZero) == O_US); + assert(std::saturate_cast( sBigMax) == USHRT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == O_US); + assert(std::saturate_cast( uBigMax) == USHRT_MAX); // saturated + + // unsigned int + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == O_US); + assert(std::saturate_cast( O_UC) == 0U); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0U); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(INT_MAX); } + assert(std::saturate_cast( INT_MIN) == 0U); + assert(std::saturate_cast( 0) == 0U); + assert(std::saturate_cast( INT_MAX) == static_cast(INT_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UINT_MAX); } + assert(std::saturate_cast( 0U) == 0U); + assert(std::saturate_cast( UINT_MAX) == UINT_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == 0U); // saturated + assert(std::saturate_cast( sZero) == 0U); + assert(std::saturate_cast( sBigMax) == UINT_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0U); + assert(std::saturate_cast( uBigMax) == UINT_MAX); // saturated + + // unsigned long + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast(SCHAR_MIN) == 0UL); + assert(std::saturate_cast( O_C) == 0UL); + assert(std::saturate_cast(SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0UL); + assert(std::saturate_cast(UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(LONG_MAX); } + assert(std::saturate_cast( LONG_MIN) == 0UL); + assert(std::saturate_cast( 0L) == 0UL); + assert(std::saturate_cast( LONG_MAX) == static_cast(LONG_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(ULONG_MAX); } + assert(std::saturate_cast( 0UL) == 0UL); + assert(std::saturate_cast(ULONG_MAX) == ULONG_MAX); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == 0UL); // saturated + assert(std::saturate_cast( sZero) == 0UL); + assert(std::saturate_cast( sBigMax) == ULONG_MAX); // saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0UL); + assert(std::saturate_cast( uBigMax) == ULONG_MAX); // saturated + + // unsigned long long + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(SCHAR_MAX); } + assert(std::saturate_cast( SCHAR_MIN) == 0ULL); + assert(std::saturate_cast( O_C) == 0ULL); + assert(std::saturate_cast( SCHAR_MAX) == static_cast(SCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(UCHAR_MAX); } + assert(std::saturate_cast( O_UC) == 0ULL); + assert(std::saturate_cast( UCHAR_MAX) == static_cast(UCHAR_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(LLONG_MAX); } + assert(std::saturate_cast( LLONG_MIN) == 0ULL); + assert(std::saturate_cast( 0LL) == 0ULL); + assert(std::saturate_cast( LLONG_MAX) == static_cast(LLONG_MAX)); + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(ULLONG_MAX); } + assert(std::saturate_cast( 0ULL) == 0ULL); + assert(std::saturate_cast(ULLONG_MAX) == ULLONG_MAX); + +#ifndef TEST_HAS_NO_INT128 + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(sBigMax); } + assert(std::saturate_cast( sBigMin) == 0ULL); // (128-bit) saturated + assert(std::saturate_cast( sZero) == 0ULL); + assert(std::saturate_cast( sBigMax) == ULLONG_MAX); // (128-bit) saturated + + { [[maybe_unused]] std::same_as decltype(auto) _ = std::saturate_cast(uBigMax); } + assert(std::saturate_cast( uZero) == 0ULL); + assert(std::saturate_cast( 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 + +// + +// template +// constexpr T sub_sat(T x, T y) noexcept; // freestanding + +#include +#include + +#include "test_macros.h" + +template +concept CanDo = requires(T x, U y) { + { std::sub_sat(x, y) } -> std::same_as; +}; + +template +constexpr void test_constraint_success() { + static_assert(CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +template +constexpr void test_constraint_fail() { + using I = int; + static_assert(!CanDo); + static_assert(!CanDo); + static_assert(!CanDo); +} + +constexpr void test() { + // Contraint success - Signed + using SI = long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__int128_t, SI>(); +#endif + // Contraint success - Unsigned + using UI = unsigned long long int; + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); + test_constraint_success(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_success<__uint128_t, UI>(); +#endif + + // Contraint failure + test_constraint_fail(); + test_constraint_fail(); +#ifndef TEST_HAS_NO_INT128 + test_constraint_fail(); +#endif + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); + test_constraint_fail(); +} 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" + +// + +// template +// constexpr T sub_sat(T x, T y) noexcept; // freestanding + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool test_signed() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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 +constexpr bool test_unsigned() { + constexpr auto minVal = std::numeric_limits::min(); + constexpr auto maxVal = std::numeric_limits::max(); + + // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name" + [[maybe_unused]] std::same_as 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(); + test_signed(); + test_signed(); + test_signed(); + test_signed(); +#ifndef TEST_HAS_NO_INT128 + test_signed<__int128_t>(); +#endif + // Unsigned + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); + test_unsigned(); +#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/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py index 667dd75..065b706 100755 --- a/libcxx/utils/generate_feature_test_macro_components.py +++ b/libcxx/utils/generate_feature_test_macro_components.py @@ -1011,10 +1011,7 @@ feature_test_macros = [ { "name": "__cpp_lib_saturation_arithmetic", "values": {"c++26": 202311}, # P0543R3 Saturation arithmetic - "headers": [ - "numeric" # TODO verify this entry since the paper was underspecified. - ], - "unimplemented": True, + "headers": ["numeric"], }, { "name": "__cpp_lib_scoped_lock", -- cgit v1.1