c03b53da91
Many uses of std::numeric_limits in C++17 and C++20 features only really need the min(), max() and digits constants for integral types. By adding __detail::__int_limits we can avoid including the whole <limits> header. The <limits> header isn't especially large, but avoiding it still gives small savings in compilation time and memory usage for the compiler. There are also C++11 features that could benefit from this change (e.g. <bits/hashtable_policy.h> and <bits/uniform_int_dist.h>) but I won't change those until stage 1. The implementation of __int_limits assumes two's complement integers, which is true for all targets supported by GCC. PR libstdc++/92546 (partial) * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/bits/int_limits.h: New header. * include/bits/parse_numbers.h (__select_int::_Select_int): Replace numeric_limits with __detail::__int_limits. * include/std/bit (__rotl, __rotr, __countl_zero, __countl_one) (__countr_zero, __countr_one, __popcount, __ceil2, __floor2, __log2p1): Likewise. * include/std/charconv (__to_chars_8, __from_chars_binary) (__from_chars_alpha_to_num, from_chars): Likewise. * include/std/memory_resource (polymorphic_allocator::allocate) (polymorphic_allocator::allocate_object): Likewise. * include/std/string_view (basic_string_view::_S_compare): Likewise. * include/std/utility (in_range): Likewise. * testsuite/20_util/integer_comparisons/in_range_neg.cc: Adjust for extra error about incomplete type __int_limits<bool>. * testsuite/26_numerics/bit/bit.count/countl_one.cc: Include <limits>. * testsuite/26_numerics/bit/bit.count/countl_zero.cc: Likewise. * testsuite/26_numerics/bit/bit.count/countr_one.cc: Likewise. * testsuite/26_numerics/bit/bit.count/countr_zero.cc: Likewise. * testsuite/26_numerics/bit/bit.count/popcount.cc: Likewise. * testsuite/26_numerics/bit/bit.pow.two/ceil2_neg.cc: Likewise. * testsuite/26_numerics/bit/bit.pow.two/ceil2.cc: Likewise. * testsuite/26_numerics/bit/bit.pow.two/floor2.cc: Likewise. * testsuite/26_numerics/bit/bit.pow.two/ispow2.cc: Likewise. * testsuite/26_numerics/bit/bit.pow.two/log2p1.cc: Likewise. * testsuite/26_numerics/bit/bit.rotate/rotl.cc: Likewise. * testsuite/26_numerics/bit/bit.rotate/rotr.cc: Likewise.
75 lines
2.8 KiB
C++
75 lines
2.8 KiB
C++
// Minimal replacement for numeric_limits of integers. -*- C++ -*-
|
|
|
|
// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
//
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
// software; you can redistribute it and/or modify it under the
|
|
// terms of the GNU General Public License as published by the
|
|
// Free Software Foundation; either version 3, or (at your option)
|
|
// any later version.
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
/** @file bits/int_limits.h
|
|
* This is an internal header file, included by other library headers.
|
|
* Do not attempt to use it directly. @headername{limits}
|
|
*/
|
|
|
|
#ifndef _GLIBCXX_INT_LIMITS_H
|
|
#define _GLIBCXX_INT_LIMITS_H 1
|
|
|
|
#pragma GCC system_header
|
|
|
|
#if __cplusplus >= 201103L
|
|
#include <bits/c++config.h>
|
|
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
|
{
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
namespace __detail
|
|
{
|
|
// This template is used for arbitrary signed and unsigned integer types
|
|
// (by headers <bit> and <charconv>) and for specific integer types
|
|
// (by <memory_resource> and <string_view>) but also for char (<charconv>).
|
|
// For simplicity's sake, all integral types except bool are supported.
|
|
|
|
// Lightweight alternative to numeric_limits<signed integer type>.
|
|
template<typename _Tp, bool = is_signed<_Tp>::value>
|
|
struct __int_limits
|
|
{
|
|
static_assert(is_integral<_Tp>::value, "unsupported specialization");
|
|
using _Up = typename make_unsigned<_Tp>::type;
|
|
static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__ - 1;
|
|
static constexpr _Tp min() noexcept { return _Tp(_Up(1) << digits); }
|
|
static constexpr _Tp max() noexcept { return _Tp(_Up(~_Up(0)) >> 1); }
|
|
};
|
|
|
|
// Lightweight alternative to numeric_limits<unsigned integer type>.
|
|
template<typename _Tp>
|
|
struct __int_limits<_Tp, false>
|
|
{
|
|
static_assert(is_integral<_Tp>::value, "unsupported specialization");
|
|
static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__;
|
|
static constexpr _Tp min() noexcept { return 0; }
|
|
static constexpr _Tp max() noexcept { return _Tp(-1); }
|
|
};
|
|
|
|
template<> struct __int_limits<bool>; // not defined
|
|
}
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
|
} // namespace
|
|
#endif // C++11
|
|
#endif // _GLIBCXX_INT_LIMITS_H
|