1 // Minimal replacement for numeric_limits of integers. -*- C++ -*-
2
3 // Copyright (C) 2020 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file bits/int_limits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{limits}
28 */
29
30 #ifndef _GLIBCXX_INT_LIMITS_H
31 #define _GLIBCXX_INT_LIMITS_H 1
32
33 #pragma GCC system_header
34
35 #if __cplusplus >= 201103L
36 #include <bits/c++config.h>
37
_GLIBCXX_VISIBILITY(default)38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 namespace __detail
42 {
43 // This template is used for arbitrary signed and unsigned integer types
44 // (by headers <bit> and <charconv>) and for specific integer types
45 // (by <memory_resource> and <string_view>) but also for char (<charconv>).
46 // For simplicity's sake, all integral types except bool are supported.
47
48 // Lightweight alternative to numeric_limits<signed integer type>.
49 template<typename _Tp, bool = is_signed<_Tp>::value>
50 struct __int_limits
51 {
52 static_assert(is_integral<_Tp>::value, "unsupported specialization");
53 using _Up = typename make_unsigned<_Tp>::type;
54 static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__ - 1;
55 static constexpr _Tp min() noexcept { return _Tp(_Up(1) << digits); }
56 static constexpr _Tp max() noexcept { return _Tp(_Up(~_Up(0)) >> 1); }
57 };
58
59 // Lightweight alternative to numeric_limits<unsigned integer type>.
60 template<typename _Tp>
61 struct __int_limits<_Tp, false>
62 {
63 static_assert(is_integral<_Tp>::value, "unsupported specialization");
64 static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__;
65 static constexpr _Tp min() noexcept { return 0; }
66 static constexpr _Tp max() noexcept { return _Tp(-1); }
67 };
68
69 template<> struct __int_limits<bool>; // not defined
70 }
71 _GLIBCXX_END_NAMESPACE_VERSION
72 } // namespace
73 #endif // C++11
74 #endif // _GLIBCXX_INT_LIMITS_H
75