From 9af65c2b9047168f14e623d55f87beda33ba1503 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 6 Oct 2020 00:05:11 +0100 Subject: [PATCH] libstdc++: Reduce uses of std::numeric_limits This avoids unnecessary instantiations of std::numeric_limits or inclusion of when a more lightweight alternative would work. Some uses can be replaced with __gnu_cxx::__int_traits and some can just use size_t(-1) directly where SIZE_MAX is needed. libstdc++-v3/ChangeLog: * include/bits/regex.h: Use __int_traits instead of std::numeric_limits. * include/bits/uniform_int_dist.h: Use __int_traits::__max instead of std::numeric_limits::max(). * include/bits/hashtable_policy.h: Use size_t(-1) instead of std::numeric_limits::max(). * include/std/regex: Include . * include/std/string_view: Use typedef for __int_traits. * src/c++11/hashtable_c++0x.cc: Use size_t(-1) instead of std::numeric_limits::max(). * testsuite/std/ranges/iota/96042.cc: Include . * testsuite/std/ranges/iota/difference_type.cc: Likewise. * testsuite/std/ranges/subrange/96042.cc: Likewise. --- libstdc++-v3/include/bits/hashtable_policy.h | 7 ++++--- libstdc++-v3/include/bits/regex.h | 9 +++++---- libstdc++-v3/include/bits/uniform_int_dist.h | 7 ++++--- libstdc++-v3/include/std/regex | 1 + libstdc++-v3/include/std/string_view | 9 +++++---- libstdc++-v3/src/c++11/hashtable_c++0x.cc | 2 +- libstdc++-v3/testsuite/std/ranges/iota/96042.cc | 1 + .../testsuite/std/ranges/iota/difference_type.cc | 1 + libstdc++-v3/testsuite/std/ranges/subrange/96042.cc | 1 + 9 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index 0109ef86a7b..31ff4f16579 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -32,8 +32,8 @@ #define _HASHTABLE_POLICY_H 1 #include // for std::tuple, std::forward_as_tuple -#include // for std::numeric_limits #include // for std::min, std::is_permutation. +#include // for __gnu_cxx::__int_traits namespace std _GLIBCXX_VISIBILITY(default) { @@ -506,6 +506,7 @@ namespace __detail inline std::size_t __clp2(std::size_t __n) noexcept { + using __gnu_cxx::__int_traits; // Equivalent to return __n ? std::bit_ceil(__n) : 0; if (__n < 2) return __n; @@ -513,7 +514,7 @@ namespace __detail ? __builtin_clzll(__n - 1ull) : __builtin_clzl(__n - 1ul); // Doing two shifts avoids undefined behaviour when __lz == 0. - return (size_t(1) << (numeric_limits::digits - __lz - 1)) << 1; + return (size_t(1) << (__int_traits::__digits - __lz - 1)) << 1; } /// Rehash policy providing power of 2 bucket numbers. Avoids modulo @@ -556,7 +557,7 @@ namespace __detail // Set next resize to the max value so that we never try to rehash again // as we already reach the biggest possible bucket number. // Note that it might result in max_load_factor not being respected. - _M_next_resize = numeric_limits::max(); + _M_next_resize = size_t(-1); else _M_next_resize = __builtin_floorl(__res * (long double)_M_max_load_factor); diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index 31ebcc1eb86..15e4289bf95 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -973,11 +973,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 if (const size_t __n = std::min(_M_len, __s._M_len)) if (int __ret = traits_type::compare(_M_data, __s._M_data, __n)) return __ret; + using __limits = __gnu_cxx::__int_traits; const difference_type __diff = _M_len - __s._M_len; - if (__diff > std::numeric_limits::max()) - return std::numeric_limits::max(); - if (__diff < std::numeric_limits::min()) - return std::numeric_limits::min(); + if (__diff > __limits::__max) + return __limits::__max; + if (__diff < __limits::__min) + return __limits::__min; return static_cast(__diff); } diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h index e3d7934e997..6e1e3d5fc5f 100644 --- a/libstdc++-v3/include/bits/uniform_int_dist.h +++ b/libstdc++-v3/include/bits/uniform_int_dist.h @@ -32,7 +32,7 @@ #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H #include -#include +#include #if __cplusplus > 201703L # include #endif @@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION explicit param_type(_IntType __a, - _IntType __b = numeric_limits<_IntType>::max()) + _IntType __b = __gnu_cxx::__int_traits<_IntType>::__max) : _M_a(__a), _M_b(__b) { __glibcxx_assert(_M_a <= _M_b); @@ -126,7 +126,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ explicit uniform_int_distribution(_IntType __a, - _IntType __b = numeric_limits<_IntType>::max()) + _IntType __b + = __gnu_cxx::__int_traits<_IntType>::__max) : _M_param(__a, __b) { } diff --git a/libstdc++-v3/include/std/regex b/libstdc++-v3/include/std/regex index b4b8ea7daca..43ee1aee616 100644 --- a/libstdc++-v3/include/std/regex +++ b/libstdc++-v3/include/std/regex @@ -53,6 +53,7 @@ #include #include +#include #include #include #include diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index 32c51b2bbd4..656d06bf90e 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -459,11 +459,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static constexpr int _S_compare(size_type __n1, size_type __n2) noexcept { + using __limits = __gnu_cxx::__int_traits; const difference_type __diff = __n1 - __n2; - if (__diff > __gnu_cxx::__int_traits::__max) - return __gnu_cxx::__int_traits::__max; - if (__diff < __gnu_cxx::__int_traits::__min) - return __gnu_cxx::__int_traits::__min; + if (__diff > __limits::__max) + return __limits::__max; + if (__diff < __limits::__min) + return __limits::__min; return static_cast(__diff); } diff --git a/libstdc++-v3/src/c++11/hashtable_c++0x.cc b/libstdc++-v3/src/c++11/hashtable_c++0x.cc index de8e2c7cb91..62762f34caf 100644 --- a/libstdc++-v3/src/c++11/hashtable_c++0x.cc +++ b/libstdc++-v3/src/c++11/hashtable_c++0x.cc @@ -78,7 +78,7 @@ namespace __detail // Set next resize to the max value so that we never try to rehash again // as we already reach the biggest possible bucket number. // Note that it might result in max_load_factor not being respected. - _M_next_resize = numeric_limits::max(); + _M_next_resize = size_t(-1); else _M_next_resize = __builtin_floorl(*__next_bkt * (long double)_M_max_load_factor); diff --git a/libstdc++-v3/testsuite/std/ranges/iota/96042.cc b/libstdc++-v3/testsuite/std/ranges/iota/96042.cc index 911663bc413..b2374ff9397 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/96042.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/96042.cc @@ -19,6 +19,7 @@ // { dg-do compile { target c++2a } } #include +#include void test01() diff --git a/libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc b/libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc index 6eb1a1676f7..c6a06a27d66 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc @@ -19,6 +19,7 @@ // { dg-do run { target c++2a } } #include +#include #include void diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc b/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc index 5826203f03c..8cf9ba8ad76 100644 --- a/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc +++ b/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc @@ -19,6 +19,7 @@ // { dg-do compile { target c++2a } } #include +#include constexpr bool test01() -- 2.30.2