Skip to content

Commit

Permalink
libstdc++: Reduce uses of std::numeric_limits
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwakely committed Oct 5, 2020
1 parent 66a0320 commit 9af65c2
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 15 deletions.
7 changes: 4 additions & 3 deletions libstdc++-v3/include/bits/hashtable_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -506,14 +506,15 @@ 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;
const unsigned __lz = sizeof(size_t) > sizeof(long)
? __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
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions libstdc++-v3/include/bits/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions libstdc++-v3/include/bits/uniform_int_dist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{ }

Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/include/std/regex
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
9 changes: 5 additions & 4 deletions libstdc++-v3/include/std/string_view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion libstdc++-v3/src/c++11/hashtable_c++0x.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/testsuite/std/ranges/iota/96042.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// { dg-do compile { target c++2a } }

#include <ranges>
#include <limits>

void
test01()
Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/testsuite/std/ranges/iota/difference_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// { dg-do run { target c++2a } }

#include <ranges>
#include <limits>
#include <testsuite_hooks.h>

void
Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/testsuite/std/ranges/subrange/96042.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// { dg-do compile { target c++2a } }

#include <ranges>
#include <limits>

constexpr bool
test01()
Expand Down

0 comments on commit 9af65c2

Please sign in to comment.