From 685af55fe004b0d904c3de1c28fdebbeee15d0a4 Mon Sep 17 00:00:00 2001 From: Nikolas Klauser Date: Sat, 21 Jun 2025 09:58:45 +0200 Subject: [PATCH] [libc++] Simplify a bit (#140021) This does a few small things: - inline `__libcpp_compute_min`, since we can don't have to put the arithmetic behind a constraint. Simple arithmetic also tends to be faster to compile than instantiating a type. - Remove an unused include (and add missing includes elsewhere) - Remove `__min` and `__max` from the `bool` specialization Co-authored-by: Louis Dionne --- libcxx/include/__numeric/gcd_lcm.h | 2 +- libcxx/include/forward_list | 1 + libcxx/include/limits | 19 +++---------------- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/libcxx/include/__numeric/gcd_lcm.h b/libcxx/include/__numeric/gcd_lcm.h index ce58f8698f72..95df54dc066d 100644 --- a/libcxx/include/__numeric/gcd_lcm.h +++ b/libcxx/include/__numeric/gcd_lcm.h @@ -10,7 +10,6 @@ #ifndef _LIBCPP___NUMERIC_GCD_LCM_H #define _LIBCPP___NUMERIC_GCD_LCM_H -#include <__algorithm/min.h> #include <__assert> #include <__bit/countr.h> #include <__config> @@ -20,6 +19,7 @@ #include <__type_traits/is_same.h> #include <__type_traits/is_signed.h> #include <__type_traits/make_unsigned.h> +#include <__type_traits/remove_cv.h> #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list index bad0c11b7c7e..6daa7fbbc03c 100644 --- a/libcxx/include/forward_list +++ b/libcxx/include/forward_list @@ -233,6 +233,7 @@ template # include <__type_traits/is_pointer.h> # include <__type_traits/is_same.h> # include <__type_traits/is_swappable.h> +# include <__type_traits/remove_cv.h> # include <__type_traits/type_identity.h> # include <__utility/forward.h> # include <__utility/move.h> diff --git a/libcxx/include/limits b/libcxx/include/limits index f5d16523763b..1205e6a0c278 100644 --- a/libcxx/include/limits +++ b/libcxx/include/limits @@ -108,7 +108,6 @@ template<> class numeric_limits; # include <__config> # include <__type_traits/is_arithmetic.h> # include <__type_traits/is_signed.h> -# include <__type_traits/remove_cv.h> # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -178,16 +177,6 @@ protected: static _LIBCPP_CONSTEXPR const float_round_style round_style = round_toward_zero; }; -template -struct __libcpp_compute_min { - static _LIBCPP_CONSTEXPR const _Tp value = _Tp(_Tp(1) << __digits); -}; - -template -struct __libcpp_compute_min<_Tp, __digits, false> { - static _LIBCPP_CONSTEXPR const _Tp value = _Tp(0); -}; - template class __libcpp_numeric_limits<_Tp, true> { protected: @@ -199,7 +188,7 @@ protected: static _LIBCPP_CONSTEXPR const int digits = static_cast(sizeof(type) * __CHAR_BIT__ - is_signed); static _LIBCPP_CONSTEXPR const int digits10 = digits * 3 / 10; static _LIBCPP_CONSTEXPR const int max_digits10 = 0; - static _LIBCPP_CONSTEXPR const type __min = __libcpp_compute_min::value; + static _LIBCPP_CONSTEXPR const type __min = is_signed ? _Tp(_Tp(1) << digits) : 0; static _LIBCPP_CONSTEXPR const type __max = is_signed ? type(type(~0) ^ __min) : type(~0); [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return __min; } [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return __max; } @@ -250,10 +239,8 @@ protected: static _LIBCPP_CONSTEXPR const int digits = 1; static _LIBCPP_CONSTEXPR const int digits10 = 0; static _LIBCPP_CONSTEXPR const int max_digits10 = 0; - static _LIBCPP_CONSTEXPR const type __min = false; - static _LIBCPP_CONSTEXPR const type __max = true; - [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return __min; } - [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return __max; } + [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return false; } + [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return true; } [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type lowest() _NOEXCEPT { return min(); } static _LIBCPP_CONSTEXPR const bool is_integer = true;