diff --git a/libcxx/docs/Cxx2aStatusPaperStatus.csv b/libcxx/docs/Cxx2aStatusPaperStatus.csv --- a/libcxx/docs/Cxx2aStatusPaperStatus.csv +++ b/libcxx/docs/Cxx2aStatusPaperStatus.csv @@ -164,7 +164,7 @@ "`P1961 `__","LWG","Harmonizing the definitions of total order for pointers","Belfast","* *","" "`P1965 `__","LWG","Blanket Wording for Specifying ""Hidden Friends""","Belfast","* *","" "","","","","","" -"`P0586 `__","LWG","Safe integral comparisons","Prague","* *","" +"`P0586 `__","LWG","Safe integral comparisons","Prague","|Complete|","13.0" "`P0593 `__","CWG","Implicit creation of objects for low-level object manipulation","Prague","* *","" "`P1115 `__","LWG","Improving the Return Value of Erase-Like Algorithms II: Free erase/erase if","Prague","|Complete|","11.0" "`P1243 `__","LWG","Rangify New Algorithms","Prague","* *","" diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst --- a/libcxx/docs/FeatureTestMacroTable.rst +++ b/libcxx/docs/FeatureTestMacroTable.rst @@ -240,7 +240,7 @@ ------------------------------------------------- ----------------- ``__cpp_lib_int_pow2`` ``202002L`` ------------------------------------------------- ----------------- - ``__cpp_lib_integer_comparison_functions`` *unimplemented* + ``__cpp_lib_integer_comparison_functions`` ``202002L`` ------------------------------------------------- ----------------- ``__cpp_lib_interpolate`` ``201902L`` ------------------------------------------------- ----------------- diff --git a/libcxx/include/utility b/libcxx/include/utility --- a/libcxx/include/utility +++ b/libcxx/include/utility @@ -58,6 +58,14 @@ template typename add_rvalue_reference::type declval() noexcept; +template constexpr bool cmp_equal(T t, U u) noexcept; // C++20 +template constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20 +template constexpr bool cmp_less(T t, U u) noexcept; // C++20 +template constexpr bool cmp_greater(T t, U u) noexcept; // C++20 +template constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20 +template constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20 +template constexpr bool in_range(T t) noexcept; // C++20 + template struct pair { @@ -207,6 +215,7 @@ #include #include #include +#include #include #include <__debug> @@ -214,6 +223,9 @@ #pragma GCC system_header #endif +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + _LIBCPP_BEGIN_NAMESPACE_STD namespace rel_ops @@ -284,6 +296,82 @@ void as_const(const _Tp&&) = delete; #endif +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) +template +struct _IsSameAsAny : _Or<_IsSame<_Tp, _Up>...> {}; + +template +concept __is_safe_integral_cmp = is_integral_v<_Tp> && + !_IsSameAsAny<_Tp, bool, char, +#ifndef _LIBCPP_NO_HAS_CHAR8_T + char8_t, +#endif +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS + char16_t, char32_t, +#endif + wchar_t>::value; + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_equal(_Tp __t, _Up __u) noexcept +{ + if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) + return __t == __u; + else if constexpr (is_signed_v<_Tp>) + return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u; + else + return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_not_equal(_Tp __t, _Up __u) noexcept +{ + return !_VSTD::cmp_equal(__t, __u); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_less(_Tp __t, _Up __u) noexcept +{ + if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) + return __t < __u; + else if constexpr (is_signed_v<_Tp>) + return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u; + else + return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_greater(_Tp __t, _Up __u) noexcept +{ + return _VSTD::cmp_less(__u, __t); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_less_equal(_Tp __t, _Up __u) noexcept +{ + return !_VSTD::cmp_greater(__t, __u); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool cmp_greater_equal(_Tp __t, _Up __u) noexcept +{ + return !_VSTD::cmp_less(__t, __u); +} + +template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up> +_LIBCPP_INLINE_VISIBILITY constexpr +bool in_range(_Up __u) noexcept +{ + return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) && + _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min()); +} +#endif + struct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { explicit piecewise_construct_t() = default; }; #if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY) extern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct;// = piecewise_construct_t(); @@ -1647,4 +1735,6 @@ _LIBCPP_END_NAMESPACE_STD +_LIBCPP_POP_MACROS + #endif // _LIBCPP_UTILITY diff --git a/libcxx/include/version b/libcxx/include/version --- a/libcxx/include/version +++ b/libcxx/include/version @@ -322,7 +322,9 @@ // # define __cpp_lib_format 201907L # define __cpp_lib_generic_unordered_lookup 201811L # define __cpp_lib_int_pow2 202002L -// # define __cpp_lib_integer_comparison_functions 202002L +# if !defined(_LIBCPP_HAS_NO_CONCEPTS) +# define __cpp_lib_integer_comparison_functions 202002L +# endif # define __cpp_lib_interpolate 201902L # if !defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED) # define __cpp_lib_is_constant_evaluated 201811L diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/utility.version.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/utility.version.pass.cpp --- a/libcxx/test/std/language.support/support.limits/support.limits.general/utility.version.pass.cpp +++ b/libcxx/test/std/language.support/support.limits/support.limits.general/utility.version.pass.cpp @@ -184,16 +184,16 @@ # error "__cpp_lib_exchange_function should have the value 201304L in c++20" # endif -# if !defined(_LIBCPP_VERSION) +# if defined(__cpp_concepts) && __cpp_concepts >= 201907L # ifndef __cpp_lib_integer_comparison_functions # error "__cpp_lib_integer_comparison_functions should be defined in c++20" # endif # if __cpp_lib_integer_comparison_functions != 202002L # error "__cpp_lib_integer_comparison_functions should have the value 202002L in c++20" # endif -# else // _LIBCPP_VERSION +# else # ifdef __cpp_lib_integer_comparison_functions -# error "__cpp_lib_integer_comparison_functions should not be defined because it is unimplemented in libc++!" +# error "__cpp_lib_integer_comparison_functions should not be defined when defined(__cpp_concepts) && __cpp_concepts >= 201907L is not defined!" # endif # endif @@ -251,16 +251,16 @@ # error "__cpp_lib_exchange_function should have the value 201304L in c++2b" # endif -# if !defined(_LIBCPP_VERSION) +# if defined(__cpp_concepts) && __cpp_concepts >= 201907L # ifndef __cpp_lib_integer_comparison_functions # error "__cpp_lib_integer_comparison_functions should be defined in c++2b" # endif # if __cpp_lib_integer_comparison_functions != 202002L # error "__cpp_lib_integer_comparison_functions should have the value 202002L in c++2b" # endif -# else // _LIBCPP_VERSION +# else # ifdef __cpp_lib_integer_comparison_functions -# error "__cpp_lib_integer_comparison_functions should not be defined because it is unimplemented in libc++!" +# error "__cpp_lib_integer_comparison_functions should not be defined when defined(__cpp_concepts) && __cpp_concepts >= 201907L is not defined!" # endif # endif diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp --- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp +++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp @@ -2653,16 +2653,16 @@ # error "__cpp_lib_int_pow2 should have the value 202002L in c++20" # endif -# if !defined(_LIBCPP_VERSION) +# if defined(__cpp_concepts) && __cpp_concepts >= 201907L # ifndef __cpp_lib_integer_comparison_functions # error "__cpp_lib_integer_comparison_functions should be defined in c++20" # endif # if __cpp_lib_integer_comparison_functions != 202002L # error "__cpp_lib_integer_comparison_functions should have the value 202002L in c++20" # endif -# else // _LIBCPP_VERSION +# else # ifdef __cpp_lib_integer_comparison_functions -# error "__cpp_lib_integer_comparison_functions should not be defined because it is unimplemented in libc++!" +# error "__cpp_lib_integer_comparison_functions should not be defined when defined(__cpp_concepts) && __cpp_concepts >= 201907L is not defined!" # endif # endif @@ -3834,16 +3834,16 @@ # error "__cpp_lib_int_pow2 should have the value 202002L in c++2b" # endif -# if !defined(_LIBCPP_VERSION) +# if defined(__cpp_concepts) && __cpp_concepts >= 201907L # ifndef __cpp_lib_integer_comparison_functions # error "__cpp_lib_integer_comparison_functions should be defined in c++2b" # endif # if __cpp_lib_integer_comparison_functions != 202002L # error "__cpp_lib_integer_comparison_functions should have the value 202002L in c++2b" # endif -# else // _LIBCPP_VERSION +# else # ifdef __cpp_lib_integer_comparison_functions -# error "__cpp_lib_integer_comparison_functions should not be defined because it is unimplemented in libc++!" +# error "__cpp_lib_integer_comparison_functions should not be defined when defined(__cpp_concepts) && __cpp_concepts >= 201907L is not defined!" # endif # endif diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_equal/cmp_equal.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_equal/cmp_equal.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_equal/cmp_equal.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// template +// constexpr bool cmp_equal(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_equal1() { + constexpr Tuple tup; + assert(std::cmp_equal(T(0), T(0))); + assert(std::cmp_equal(T(10), T(10))); + assert(std::cmp_equal(tup.min, tup.min)); + assert(std::cmp_equal(tup.max, tup.max)); + assert(!std::cmp_equal(T(0), T(1))); + assert(!std::cmp_equal(T(1), T(0))); + assert(!std::cmp_equal(T(5), T(10))); + assert(!std::cmp_equal(T(10), T(5))); + assert(!std::cmp_equal(tup.min, tup.max)); + assert(!std::cmp_equal(tup.max, tup.min)); + assert(!std::cmp_equal(1, tup.max)); + assert(!std::cmp_equal(tup.max, 1)); + assert(!std::cmp_equal(1, tup.min)); + assert(!std::cmp_equal(tup.min, 1)); + assert(std::cmp_equal(T(-5), T(-5))); + assert(!std::cmp_equal(-2, tup.min)); + assert(!std::cmp_equal(tup.min, -2)); + assert(!std::cmp_equal(-2, tup.max)); + assert(!std::cmp_equal(tup.max, -2)); +} + +template +constexpr void test_cmp_equal2() { + constexpr Tuple ttup; + constexpr Tuple utup; + assert(std::cmp_equal(T(0), U(0))); + assert(std::cmp_equal(T(10), U(10))); + assert(!std::cmp_equal(T(0), U(1))); + assert(!std::cmp_equal(T(1), U(0))); + assert(!std::cmp_equal(T(5), U(10))); + assert(!std::cmp_equal(T(10), U(5))); + assert(!std::cmp_equal(ttup.min, utup.max)); + assert(!std::cmp_equal(utup.min, ttup.max)); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_equal1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_equal2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_equal(0, 0)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater/cmp_greater.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater/cmp_greater.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater/cmp_greater.pass.cpp @@ -0,0 +1,98 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// constexpr bool cmp_greater(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_greater1() { + constexpr Tuple tup; + assert(!std::cmp_greater(T(0), T(1))); + assert(!std::cmp_greater(T(1), T(2))); + assert(!std::cmp_greater(tup.min, tup.max)); + assert(!std::cmp_greater(tup.min, tup.mid)); + assert(!std::cmp_greater(tup.mid, tup.max)); + assert(std::cmp_greater(T(1), T(0))); + assert(std::cmp_greater(T(10), T(5))); + assert(std::cmp_greater(tup.max, tup.min)); + assert(std::cmp_greater(tup.mid, tup.min)); + assert(!std::cmp_greater(tup.mid, tup.mid)); + assert(!std::cmp_greater(tup.min, tup.min)); + assert(!std::cmp_greater(tup.max, tup.max)); + assert(std::cmp_greater(tup.max, 1)); + assert(std::cmp_greater(1, tup.min)); + assert(!std::cmp_greater(T(-1), T(-1))); + assert(std::cmp_greater(-2, tup.min) == std::is_signed_v); + assert(!std::cmp_greater(tup.min, -2) == std::is_signed_v); + assert(!std::cmp_greater(-2, tup.max)); + assert(std::cmp_greater(tup.max, -2)); +} + +template +constexpr void test_cmp_greater2() { + assert(!std::cmp_greater(T(0), U(1))); + assert(std::cmp_greater(T(1), U(0))); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_greater1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_greater2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_greater(1, 0)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater_equal/cmp_greater_equal.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater_equal/cmp_greater_equal.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_greater_equal/cmp_greater_equal.pass.cpp @@ -0,0 +1,100 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_greater_equal1() { + constexpr Tuple tup; + assert(!std::cmp_greater_equal(T(0), T(1))); + assert(!std::cmp_greater_equal(T(1), T(2))); + assert(!std::cmp_greater_equal(tup.min, tup.max)); + assert(!std::cmp_greater_equal(tup.min, tup.mid)); + assert(!std::cmp_greater_equal(tup.mid, tup.max)); + assert(std::cmp_greater_equal(T(1), T(0))); + assert(std::cmp_greater_equal(T(10), T(5))); + assert(std::cmp_greater_equal(tup.max, tup.min)); + assert(std::cmp_greater_equal(tup.mid, tup.min)); + assert(std::cmp_greater_equal(tup.mid, tup.mid)); + assert(std::cmp_greater_equal(tup.min, tup.min)); + assert(std::cmp_greater_equal(tup.max, tup.max)); + assert(std::cmp_greater_equal(tup.max, 1)); + assert(std::cmp_greater_equal(1, tup.min)); + assert(std::cmp_greater_equal(T(-1), T(-1))); + assert(std::cmp_greater_equal(-2, tup.min) == std::is_signed_v); + assert(std::cmp_greater_equal(tup.min, -2) == std::is_unsigned_v); + assert(!std::cmp_greater_equal(-2, tup.max)); + assert(std::cmp_greater_equal(tup.max, -2)); +} + +template +constexpr void test_cmp_greater_equal2() { + assert(!std::cmp_greater_equal(T(0), U(1))); + assert(std::cmp_greater_equal(T(1), U(0))); + assert(std::cmp_greater_equal(T(0), U(0))); + assert(std::cmp_greater_equal(T(1), U(1))); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_greater_equal1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_greater_equal2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_greater_equal(1, 0)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less/cmp_less.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less/cmp_less.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less/cmp_less.pass.cpp @@ -0,0 +1,99 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// template +// constexpr bool cmp_less(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_less1() { + constexpr Tuple tup; + assert(std::cmp_less(T(0), T(1))); + assert(std::cmp_less(T(1), T(2))); + assert(std::cmp_less(tup.min, tup.max)); + assert(std::cmp_less(tup.min, tup.mid)); + assert(std::cmp_less(tup.mid, tup.max)); + assert(!std::cmp_less(T(1), T(0))); + assert(!std::cmp_less(T(10), T(5))); + assert(!std::cmp_less(tup.max, tup.min)); + assert(!std::cmp_less(tup.mid, tup.min)); + assert(!std::cmp_less(tup.mid, tup.mid)); + assert(!std::cmp_less(tup.min, tup.min)); + assert(!std::cmp_less(tup.max, tup.max)); + assert(!std::cmp_less(tup.max, 1)); + assert(!std::cmp_less(1, tup.min)); + assert(!std::cmp_less(T(-1), T(-1))); + assert(!std::cmp_less(-2, tup.min) == std::is_signed_v); + assert(std::cmp_less(tup.min, -2) == std::is_signed_v); + assert(std::cmp_less(-2, tup.max)); + assert(!std::cmp_less(tup.max, -2)); +} + +template +constexpr void test_cmp_less2() { + assert(std::cmp_less(T(0), U(1))); + assert(!std::cmp_less(T(1), U(0))); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_less1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_less2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_less(0, 1)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less_equal/cmp_less_equal.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less_equal/cmp_less_equal.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_less_equal/cmp_less_equal.pass.cpp @@ -0,0 +1,99 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_less_equal1() { + constexpr Tuple tup; + assert(std::cmp_less_equal(T(0), T(0))); + assert(std::cmp_less_equal(T(0), T(1))); + assert(std::cmp_less_equal(tup.min, tup.max)); + assert(std::cmp_less_equal(tup.min, tup.mid)); + assert(std::cmp_less_equal(tup.mid, tup.max)); + assert(std::cmp_less_equal(tup.max, tup.max)); + assert(std::cmp_less_equal(tup.mid, tup.mid)); + assert(std::cmp_less_equal(tup.min, tup.min)); + assert(!std::cmp_less_equal(T(1), T(0))); + assert(!std::cmp_less_equal(T(10), T(5))); + assert(!std::cmp_less_equal(tup.max, tup.min)); + assert(!std::cmp_less_equal(tup.mid, tup.min)); + assert(!std::cmp_less_equal(tup.max, 1)); + assert(!std::cmp_less_equal(1, tup.min)); + assert(std::cmp_less_equal(T(-1), T(-1))); + assert(!std::cmp_less_equal(-2, tup.min) == std::is_signed_v); + assert(std::cmp_less_equal(tup.min, -2) == std::is_signed_v); + assert(std::cmp_less_equal(-2, tup.max)); + assert(!std::cmp_less_equal(tup.max, -2)); +} + +template +constexpr void test_cmp_less_equal2() { + assert(std::cmp_less_equal(T(0), U(1))); + assert(std::cmp_less_equal(T(0), U(0))); + assert(!std::cmp_less_equal(T(1), U(0))); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_less_equal1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_less_equal2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_less_equal(0, 1)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_not_equal/cmp_not_equal.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_not_equal/cmp_not_equal.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.cmp_not_equal/cmp_not_equal.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// template +// constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_cmp_not_equal1() { + constexpr Tuple tup; + assert(!std::cmp_not_equal(T(0), T(0))); + assert(!std::cmp_not_equal(T(10), T(10))); + assert(!std::cmp_not_equal(tup.min, tup.min)); + assert(!std::cmp_not_equal(tup.max, tup.max)); + assert(std::cmp_not_equal(T(0), T(1))); + assert(std::cmp_not_equal(T(1), T(0))); + assert(std::cmp_not_equal(T(5), T(10))); + assert(std::cmp_not_equal(T(10), T(5))); + assert(std::cmp_not_equal(tup.min, tup.max)); + assert(std::cmp_not_equal(tup.max, tup.min)); + assert(std::cmp_not_equal(1, tup.max)); + assert(std::cmp_not_equal(tup.max, 1)); + assert(std::cmp_not_equal(1, tup.min)); + assert(std::cmp_not_equal(tup.min, 1)); + assert(std::cmp_not_equal(-2, tup.min)); + assert(std::cmp_not_equal(tup.min, -2)); + assert(std::cmp_not_equal(-2, tup.max)); + assert(std::cmp_not_equal(tup.max, -2)); +} + +template +constexpr void test_cmp_not_equal2() { + constexpr Tuple ttup; + constexpr Tuple utup; + assert(!std::cmp_not_equal(T(0), U(0))); + assert(!std::cmp_not_equal(T(10), U(10))); + assert(std::cmp_not_equal(T(0), U(1))); + assert(std::cmp_not_equal(T(1), U(0))); + assert(std::cmp_not_equal(T(5), U(10))); + assert(std::cmp_not_equal(T(10), U(5))); + assert(std::cmp_not_equal(ttup.min, utup.max)); + assert(std::cmp_not_equal(utup.min, ttup.max)); +} + +template +constexpr void test1(const std::tuple&) { + (test_cmp_not_equal1() , ...); +} + +template +constexpr void test2_impl(const std::tuple&) { + (test_cmp_not_equal2() , ...); +} + +template +constexpr void test2(const std::tuple&, const UTuple& utuple) { + (test2_impl(utuple) , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test2(types, types); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::cmp_not_equal(0, 0)); + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.fail.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.fail.cpp @@ -0,0 +1,147 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// template +// constexpr bool cmp_equal(T t, U u) noexcept; // C++20 + +// template +// constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20 + +// template +// constexpr bool cmp_less(T t, U u) noexcept; // C++20 + +// template +// constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20 + +// template +// constexpr bool cmp_greater(T t, U u) noexcept; // C++20 + +// template +// constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20 + +// template +// constexpr bool in_range(T t) noexcept; // C++20 + +#include + +#include "test_macros.h" + +struct NonEmptyT { + int val; + NonEmptyT() : val(0) {} + NonEmptyT(int val) : val(val) {} + operator int&() { return val; } + operator int() const { return val; } +}; + +enum ColorT { red, green, blue }; + +struct EmptyT {}; + +template +constexpr void test() { + std::cmp_equal(T(), T()); // expected-error11{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(T(), int()); // expected-error11{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(int(), T()); // expected-error11{{no matching function for call to 'cmp_equal'}} + std::cmp_not_equal(T(), T()); // expected-error11{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(T(), int()); // expected-error11{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(int(), T()); // expected-error11{{no matching function for call to 'cmp_not_equal'}} + std::cmp_less(T(), T()); // expected-error11{{no matching function for call to 'cmp_less'}} + std::cmp_less(T(), int()); // expected-error11{{no matching function for call to 'cmp_less'}} + std::cmp_less(int(), T()); // expected-error11{{no matching function for call to 'cmp_less'}} + std::cmp_less_equal(T(), T()); // expected-error11{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(T(), int()); // expected-error11{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(int(), T()); // expected-error11{{no matching function for call to 'cmp_less_equal'}} + std::cmp_greater(T(), T()); // expected-error11{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(T(), int()); // expected-error11{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(int(), T()); // expected-error11{{no matching function for call to 'cmp_greater'}} + std::cmp_greater_equal(T(), T()); // expected-error11{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(T(), int()); // expected-error11{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(int(), T()); // expected-error11{{no matching function for call to 'cmp_greater_equal'}} + std::in_range(int()); // expected-error11{{no matching function for call to 'in_range'}} + std::in_range(T()); // expected-error11{{no matching function for call to 'in_range'}} +} +#ifndef _LIBCPP_NO_HAS_CHAR8_T +template +constexpr void test_char8t() { + std::cmp_equal(T(), T()); // expected-error1{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(T(), int()); // expected-error1{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(int(), T()); // expected-error1{{no matching function for call to 'cmp_equal'}} + std::cmp_not_equal(T(), T()); // expected-error1{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(T(), int()); // expected-error1{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(int(), T()); // expected-error1{{no matching function for call to 'cmp_not_equal'}} + std::cmp_less(T(), T()); // expected-error1{{no matching function for call to 'cmp_less'}} + std::cmp_less(T(), int()); // expected-error1{{no matching function for call to 'cmp_less'}} + std::cmp_less(int(), T()); // expected-error1{{no matching function for call to 'cmp_less'}} + std::cmp_less_equal(T(), T()); // expected-error1{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(T(), int()); // expected-error1{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(int(), T()); // expected-error1{{no matching function for call to 'cmp_less_equal'}} + std::cmp_greater(T(), T()); // expected-error1{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(T(), int()); // expected-error1{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(int(), T()); // expected-error1{{no matching function for call to 'cmp_greater'}} + std::cmp_greater_equal(T(), T()); // expected-error1{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(T(), int()); // expected-error1{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(int(), T()); // expected-error1{{no matching function for call to 'cmp_greater_equal'}} + std::in_range(int()); // expected-error1{{no matching function for call to 'in_range'}} + std::in_range(T()); // expected-error1{{no matching function for call to 'in_range'}} +} +#endif // _LIBCPP_NO_HAS_CHAR8_T + +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS +template +constexpr void test_uchars() { + std::cmp_equal(T(), T()); // expected-error2{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(T(), int()); // expected-error2{{no matching function for call to 'cmp_equal'}} + std::cmp_equal(int(), T()); // expected-error2{{no matching function for call to 'cmp_equal'}} + std::cmp_not_equal(T(), T()); // expected-error2{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(T(), int()); // expected-error2{{no matching function for call to 'cmp_not_equal'}} + std::cmp_not_equal(int(), T()); // expected-error2{{no matching function for call to 'cmp_not_equal'}} + std::cmp_less(T(), T()); // expected-error2{{no matching function for call to 'cmp_less'}} + std::cmp_less(T(), int()); // expected-error2{{no matching function for call to 'cmp_less'}} + std::cmp_less(int(), T()); // expected-error2{{no matching function for call to 'cmp_less'}} + std::cmp_less_equal(T(), T()); // expected-error2{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(T(), int()); // expected-error2{{no matching function for call to 'cmp_less_equal'}} + std::cmp_less_equal(int(), T()); // expected-error2{{no matching function for call to 'cmp_less_equal'}} + std::cmp_greater(T(), T()); // expected-error2{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(T(), int()); // expected-error2{{no matching function for call to 'cmp_greater'}} + std::cmp_greater(int(), T()); // expected-error2{{no matching function for call to 'cmp_greater'}} + std::cmp_greater_equal(T(), T()); // expected-error2{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(T(), int()); // expected-error2{{no matching function for call to 'cmp_greater_equal'}} + std::cmp_greater_equal(int(), T()); // expected-error2{{no matching function for call to 'cmp_greater_equal'}} + std::in_range(int()); // expected-error2{{no matching function for call to 'in_range'}} + std::in_range(T()); // expected-error2{{no matching function for call to 'in_range'}} +} +#endif // _LIBCPP_HAS_NO_UNICODE_CHARS + +int main() { + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); +#ifndef _LIBCPP_NO_HAS_CHAR8_T + test_char8t(); +#endif // !_LIBCPP_NO_HAS_CHAR8_T + +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS + test_uchars(); + test_uchars(); +#endif // !_LIBCPP_HAS_NO_UNICODE_CHARS + return 0; +} diff --git a/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.in_range/in_range.pass.cpp b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.in_range/in_range.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/utility.intcmp/intcmp.in_range/in_range.pass.cpp @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// UNSUPPORTED: libcpp-no-concepts + +// + +// template +// constexpr bool in_range(T t) noexcept; // C++20 + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Tuple { + T min; + T max; + T mid; + constexpr Tuple() { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + mid = std::midpoint(min, max); + } +}; + +template +constexpr void test_in_range1() { + constexpr Tuple tup; + assert(std::in_range(tup.min)); + assert(std::in_range(tup.min + 1)); + assert(std::in_range(tup.max)); + assert(std::in_range(tup.max - 1)); + assert(std::in_range(tup.mid)); + assert(std::in_range(tup.mid - 1)); + assert(std::in_range(tup.mid + 1)); +} + +constexpr void test_in_range() { + constexpr Tuple utup8; + constexpr Tuple stup8; + assert(!std::in_range(utup8.max)); + assert(std::in_range(utup8.max)); + assert(!std::in_range(stup8.min)); + assert(std::in_range(utup8.mid)); + assert(!std::in_range(stup8.mid)); + assert(!std::in_range(-1)); +} + +template +constexpr void test1(const std::tuple&) { + (test_in_range1() , ...); +} + +constexpr bool test() { + std::tuple< +#ifndef _LIBCPP_HAS_NO_INT128 + __int128_t, __uint128_t, +#endif + unsigned long long, long long, unsigned long, long, unsigned int, int, + unsigned short, short, unsigned char, signed char> types; + test1(types); + test_in_range(); + return true; +} + +int main() { + ASSERT_NOEXCEPT(std::in_range(-1)); + 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 --- a/libcxx/utils/generate_feature_test_macro_components.py +++ b/libcxx/utils/generate_feature_test_macro_components.py @@ -340,7 +340,8 @@ "name": "__cpp_lib_integer_comparison_functions", "values": { "c++20": 202002 }, "headers": ["utility"], - "unimplemented": True, + "test_suite_guard": "defined(__cpp_concepts) && __cpp_concepts >= 201907L", + "libcxx_guard": "!defined(_LIBCPP_HAS_NO_CONCEPTS)", }, { "name": "__cpp_lib_integer_sequence", "values": { "c++14": 201304 },