[libc++] Simplify <limits> 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 <ldionne.2@gmail.com>
This commit is contained in:
Nikolas Klauser
2025-06-21 09:58:45 +02:00
committed by GitHub
parent 2050d2e181
commit 685af55fe0
3 changed files with 5 additions and 17 deletions

View File

@@ -10,7 +10,6 @@
#ifndef _LIBCPP___NUMERIC_GCD_LCM_H #ifndef _LIBCPP___NUMERIC_GCD_LCM_H
#define _LIBCPP___NUMERIC_GCD_LCM_H #define _LIBCPP___NUMERIC_GCD_LCM_H
#include <__algorithm/min.h>
#include <__assert> #include <__assert>
#include <__bit/countr.h> #include <__bit/countr.h>
#include <__config> #include <__config>
@@ -20,6 +19,7 @@
#include <__type_traits/is_same.h> #include <__type_traits/is_same.h>
#include <__type_traits/is_signed.h> #include <__type_traits/is_signed.h>
#include <__type_traits/make_unsigned.h> #include <__type_traits/make_unsigned.h>
#include <__type_traits/remove_cv.h>
#include <limits> #include <limits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

View File

@@ -233,6 +233,7 @@ template <class T, class Allocator, class Predicate>
# include <__type_traits/is_pointer.h> # include <__type_traits/is_pointer.h>
# include <__type_traits/is_same.h> # include <__type_traits/is_same.h>
# include <__type_traits/is_swappable.h> # include <__type_traits/is_swappable.h>
# include <__type_traits/remove_cv.h>
# include <__type_traits/type_identity.h> # include <__type_traits/type_identity.h>
# include <__utility/forward.h> # include <__utility/forward.h>
# include <__utility/move.h> # include <__utility/move.h>

View File

@@ -108,7 +108,6 @@ template<> class numeric_limits<cv long double>;
# include <__config> # include <__config>
# include <__type_traits/is_arithmetic.h> # include <__type_traits/is_arithmetic.h>
# include <__type_traits/is_signed.h> # include <__type_traits/is_signed.h>
# include <__type_traits/remove_cv.h>
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header # pragma GCC system_header
@@ -178,16 +177,6 @@ protected:
static _LIBCPP_CONSTEXPR const float_round_style round_style = round_toward_zero; static _LIBCPP_CONSTEXPR const float_round_style round_style = round_toward_zero;
}; };
template <class _Tp, int __digits, bool _IsSigned>
struct __libcpp_compute_min {
static _LIBCPP_CONSTEXPR const _Tp value = _Tp(_Tp(1) << __digits);
};
template <class _Tp, int __digits>
struct __libcpp_compute_min<_Tp, __digits, false> {
static _LIBCPP_CONSTEXPR const _Tp value = _Tp(0);
};
template <class _Tp> template <class _Tp>
class __libcpp_numeric_limits<_Tp, true> { class __libcpp_numeric_limits<_Tp, true> {
protected: protected:
@@ -199,7 +188,7 @@ protected:
static _LIBCPP_CONSTEXPR const int digits = static_cast<int>(sizeof(type) * __CHAR_BIT__ - is_signed); static _LIBCPP_CONSTEXPR const int digits = static_cast<int>(sizeof(type) * __CHAR_BIT__ - is_signed);
static _LIBCPP_CONSTEXPR const int digits10 = digits * 3 / 10; static _LIBCPP_CONSTEXPR const int digits10 = digits * 3 / 10;
static _LIBCPP_CONSTEXPR const int max_digits10 = 0; static _LIBCPP_CONSTEXPR const int max_digits10 = 0;
static _LIBCPP_CONSTEXPR const type __min = __libcpp_compute_min<type, digits, is_signed>::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); 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 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 max() _NOEXCEPT { return __max; }
@@ -250,10 +239,8 @@ protected:
static _LIBCPP_CONSTEXPR const int digits = 1; static _LIBCPP_CONSTEXPR const int digits = 1;
static _LIBCPP_CONSTEXPR const int digits10 = 0; static _LIBCPP_CONSTEXPR const int digits10 = 0;
static _LIBCPP_CONSTEXPR const int max_digits10 = 0; static _LIBCPP_CONSTEXPR const int max_digits10 = 0;
static _LIBCPP_CONSTEXPR const type __min = false; [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return false; }
static _LIBCPP_CONSTEXPR const type __max = true; [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return 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 lowest() _NOEXCEPT { return min(); } [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type lowest() _NOEXCEPT { return min(); }
static _LIBCPP_CONSTEXPR const bool is_integer = true; static _LIBCPP_CONSTEXPR const bool is_integer = true;