Skip to content

Commit 685af55

Browse files
philnik777ldionne
andauthored
[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>
1 parent 2050d2e commit 685af55

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

libcxx/include/__numeric/gcd_lcm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef _LIBCPP___NUMERIC_GCD_LCM_H
1111
#define _LIBCPP___NUMERIC_GCD_LCM_H
1212

13-
#include <__algorithm/min.h>
1413
#include <__assert>
1514
#include <__bit/countr.h>
1615
#include <__config>
@@ -20,6 +19,7 @@
2019
#include <__type_traits/is_same.h>
2120
#include <__type_traits/is_signed.h>
2221
#include <__type_traits/make_unsigned.h>
22+
#include <__type_traits/remove_cv.h>
2323
#include <limits>
2424

2525
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/forward_list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ template <class T, class Allocator, class Predicate>
233233
# include <__type_traits/is_pointer.h>
234234
# include <__type_traits/is_same.h>
235235
# include <__type_traits/is_swappable.h>
236+
# include <__type_traits/remove_cv.h>
236237
# include <__type_traits/type_identity.h>
237238
# include <__utility/forward.h>
238239
# include <__utility/move.h>

libcxx/include/limits

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ template<> class numeric_limits<cv long double>;
108108
# include <__config>
109109
# include <__type_traits/is_arithmetic.h>
110110
# include <__type_traits/is_signed.h>
111-
# include <__type_traits/remove_cv.h>
112111

113112
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
114113
# pragma GCC system_header
@@ -178,16 +177,6 @@ protected:
178177
static _LIBCPP_CONSTEXPR const float_round_style round_style = round_toward_zero;
179178
};
180179

181-
template <class _Tp, int __digits, bool _IsSigned>
182-
struct __libcpp_compute_min {
183-
static _LIBCPP_CONSTEXPR const _Tp value = _Tp(_Tp(1) << __digits);
184-
};
185-
186-
template <class _Tp, int __digits>
187-
struct __libcpp_compute_min<_Tp, __digits, false> {
188-
static _LIBCPP_CONSTEXPR const _Tp value = _Tp(0);
189-
};
190-
191180
template <class _Tp>
192181
class __libcpp_numeric_limits<_Tp, true> {
193182
protected:
@@ -199,7 +188,7 @@ protected:
199188
static _LIBCPP_CONSTEXPR const int digits = static_cast<int>(sizeof(type) * __CHAR_BIT__ - is_signed);
200189
static _LIBCPP_CONSTEXPR const int digits10 = digits * 3 / 10;
201190
static _LIBCPP_CONSTEXPR const int max_digits10 = 0;
202-
static _LIBCPP_CONSTEXPR const type __min = __libcpp_compute_min<type, digits, is_signed>::value;
191+
static _LIBCPP_CONSTEXPR const type __min = is_signed ? _Tp(_Tp(1) << digits) : 0;
203192
static _LIBCPP_CONSTEXPR const type __max = is_signed ? type(type(~0) ^ __min) : type(~0);
204193
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return __min; }
205194
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return __max; }
@@ -250,10 +239,8 @@ protected:
250239
static _LIBCPP_CONSTEXPR const int digits = 1;
251240
static _LIBCPP_CONSTEXPR const int digits10 = 0;
252241
static _LIBCPP_CONSTEXPR const int max_digits10 = 0;
253-
static _LIBCPP_CONSTEXPR const type __min = false;
254-
static _LIBCPP_CONSTEXPR const type __max = true;
255-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return __min; }
256-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return __max; }
242+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type min() _NOEXCEPT { return false; }
243+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type max() _NOEXCEPT { return true; }
257244
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR type lowest() _NOEXCEPT { return min(); }
258245

259246
static _LIBCPP_CONSTEXPR const bool is_integer = true;

0 commit comments

Comments
 (0)