diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2020-10-06 00:05:11 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-10-06 00:05:11 +0100 |
commit | 9af65c2b9047168f14e623d55f87beda33ba1503 (patch) | |
tree | bed8b420d0dd9cbb74392afae4b379de69c79d4e | |
parent | 66a032079309069fec085fff2a014ac217ce5781 (diff) | |
download | gcc-9af65c2b9047168f14e623d55f87beda33ba1503.zip gcc-9af65c2b9047168f14e623d55f87beda33ba1503.tar.gz gcc-9af65c2b9047168f14e623d55f87beda33ba1503.tar.bz2 |
libstdc++: Reduce uses of std::numeric_limits
This avoids unnecessary instantiations of std::numeric_limits or
inclusion of <limits> 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<int> instead of
std::numeric_limits<int>.
* include/bits/uniform_int_dist.h: Use __int_traits<T>::__max
instead of std::numeric_limits<T>::max().
* include/bits/hashtable_policy.h: Use size_t(-1) instead of
std::numeric_limits<size_t>::max().
* include/std/regex: Include <ext/numeric_traits.h>.
* include/std/string_view: Use typedef for __int_traits<int>.
* src/c++11/hashtable_c++0x.cc: Use size_t(-1) instead of
std::numeric_limits<size_t>::max().
* testsuite/std/ranges/iota/96042.cc: Include <limits>.
* testsuite/std/ranges/iota/difference_type.cc: Likewise.
* testsuite/std/ranges/subrange/96042.cc: Likewise.
-rw-r--r-- | libstdc++-v3/include/bits/hashtable_policy.h | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/regex.h | 9 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/uniform_int_dist.h | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/std/regex | 1 | ||||
-rw-r--r-- | libstdc++-v3/include/std/string_view | 9 | ||||
-rw-r--r-- | libstdc++-v3/src/c++11/hashtable_c++0x.cc | 2 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/std/ranges/iota/96042.cc | 1 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc | 1 | ||||
-rw-r--r-- | 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 0109ef8..31ff4f1 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 <tuple> // for std::tuple, std::forward_as_tuple -#include <limits> // for std::numeric_limits #include <bits/stl_algobase.h> // for std::min, std::is_permutation. +#include <ext/numeric_traits.h> // 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<size_t>::digits - __lz - 1)) << 1; + return (size_t(1) << (__int_traits<size_t>::__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<size_t>::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 31ebcc1..15e4289 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<int>; const difference_type __diff = _M_len - __s._M_len; - if (__diff > std::numeric_limits<int>::max()) - return std::numeric_limits<int>::max(); - if (__diff < std::numeric_limits<int>::min()) - return std::numeric_limits<int>::min(); + if (__diff > __limits::__max) + return __limits::__max; + if (__diff < __limits::__min) + return __limits::__min; return static_cast<int>(__diff); } diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h index e3d7934..6e1e3d5 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 <type_traits> -#include <limits> +#include <ext/numeric_traits.h> #if __cplusplus > 201703L # include <concepts> #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 b4b8ea7..43ee1ae 100644 --- a/libstdc++-v3/include/std/regex +++ b/libstdc++-v3/include/std/regex @@ -53,6 +53,7 @@ #include <cstring> #include <ext/aligned_buffer.h> +#include <ext/numeric_traits.h> #include <bits/std_function.h> #include <bits/regex_constants.h> #include <bits/regex_error.h> diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index 32c51b2..656d06b 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<int>; const difference_type __diff = __n1 - __n2; - if (__diff > __gnu_cxx::__int_traits<int>::__max) - return __gnu_cxx::__int_traits<int>::__max; - if (__diff < __gnu_cxx::__int_traits<int>::__min) - return __gnu_cxx::__int_traits<int>::__min; + if (__diff > __limits::__max) + return __limits::__max; + if (__diff < __limits::__min) + return __limits::__min; return static_cast<int>(__diff); } diff --git a/libstdc++-v3/src/c++11/hashtable_c++0x.cc b/libstdc++-v3/src/c++11/hashtable_c++0x.cc index de8e2c7..62762f3 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<size_t>::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 911663b..b2374ff 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 <ranges> +#include <limits> 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 6eb1a16..c6a06a2 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 <ranges> +#include <limits> #include <testsuite_hooks.h> void diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc b/libstdc++-v3/testsuite/std/ranges/subrange/96042.cc index 5826203..8cf9ba8 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 <ranges> +#include <limits> constexpr bool test01() |