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.
This commit is contained in:
Jonathan Wakely 2020-10-06 00:05:11 +01:00
parent 66a0320793
commit 9af65c2b90
9 changed files with 23 additions and 15 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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)
{ }

View File

@ -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>

View File

@ -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);
}

View File

@ -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);

View File

@ -19,6 +19,7 @@
// { dg-do compile { target c++2a } }
#include <ranges>
#include <limits>
void
test01()

View File

@ -19,6 +19,7 @@
// { dg-do run { target c++2a } }
#include <ranges>
#include <limits>
#include <testsuite_hooks.h>
void

View File

@ -19,6 +19,7 @@
// { dg-do compile { target c++2a } }
#include <ranges>
#include <limits>
constexpr bool
test01()