libstdc++
ext/numeric_traits.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2007-2026 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
10 
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // 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 ext/numeric_traits.h
26  * This file is a GNU extension to the Standard C++ Library.
27  */
28 
29 #ifndef _EXT_NUMERIC_TRAITS
30 #define _EXT_NUMERIC_TRAITS 1
31 
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35 
36 #include <bits/cpp_type_traits.h>
37 #include <ext/type_traits.h>
38 
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  // Compile time constants for builtin types.
44  // In C++98 std::numeric_limits member functions are not constant expressions
45  // (that changed in C++11 with the addition of 'constexpr').
46  // Even for C++11, this header is smaller than <limits> and can be used
47  // when only is_signed, digits, min, or max values are needed for integers,
48  // or is_signed, digits10, max_digits10, or max_exponent10 for floats.
49 
50  // Unlike __is_integer (and std::is_integral) this trait is true for
51  // non-standard built-in integer types such as __int20.
52  template<typename _Tp>
53  struct __is_integer_nonstrict
54  : public std::__is_integer<_Tp>
55  {
56  using std::__is_integer<_Tp>::__value;
57 
58  // The number of bits in the value representation.
59  enum { __width = __value ? sizeof(_Tp) * __CHAR_BIT__ : 0 };
60  };
61 
62  template<typename _Value>
63  struct __numeric_traits_integer
64  {
65 #if __cplusplus >= 201103L
66  static_assert(__is_integer_nonstrict<_Value>::__value,
67  "invalid specialization");
68 #endif
69 
70  // NB: these two are also available in std::numeric_limits as compile
71  // time constants, but <limits> is big and we can avoid including it.
72  static const bool __is_signed = (_Value)(-1) < 0;
73  static const int __digits
74  = __is_integer_nonstrict<_Value>::__width - __is_signed;
75 
76  // The initializers must be constants so that __max and __min are too.
77  static const _Value __max = __is_signed
78  ? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
79  : ~(_Value)0;
80  static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
81  };
82 
83  template<typename _Value>
84  const _Value __numeric_traits_integer<_Value>::__min;
85 
86  template<typename _Value>
87  const _Value __numeric_traits_integer<_Value>::__max;
88 
89  template<typename _Value>
90  const bool __numeric_traits_integer<_Value>::__is_signed;
91 
92  template<typename _Value>
93  const int __numeric_traits_integer<_Value>::__digits;
94 
95  // Enable __numeric_traits_integer for types where the __is_integer_nonstrict
96  // primary template doesn't give the right answer.
97 #define _GLIBCXX_INT_N_TRAITS(T, WIDTH) \
98  __extension__ \
99  template<> struct __is_integer_nonstrict<T> \
100  { \
101  enum { __value = 1 }; \
102  typedef std::__true_type __type; \
103  enum { __width = WIDTH }; \
104  }; \
105  __extension__ \
106  template<> struct __is_integer_nonstrict<unsigned T> \
107  { \
108  enum { __value = 1 }; \
109  typedef std::__true_type __type; \
110  enum { __width = WIDTH }; \
111  };
112 
113  // We need to specify the width for some __intNN types because they
114  // have padding bits, e.g. the object representation of __int20 has 32 bits,
115  // but its width (number of bits in the value representation) is only 20.
116 #if defined __GLIBCXX_TYPE_INT_N_0 && __GLIBCXX_BITSIZE_INT_N_0 % __CHAR_BIT__
117  _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_0, __GLIBCXX_BITSIZE_INT_N_0)
118 #endif
119 #if defined __GLIBCXX_TYPE_INT_N_1 && __GLIBCXX_BITSIZE_INT_N_1 % __CHAR_BIT__
120  _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_1, __GLIBCXX_BITSIZE_INT_N_1)
121 #endif
122 #if defined __GLIBCXX_TYPE_INT_N_2 && __GLIBCXX_BITSIZE_INT_N_2 % __CHAR_BIT__
123  _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_2, __GLIBCXX_BITSIZE_INT_N_2)
124 #endif
125 #if defined __GLIBCXX_TYPE_INT_N_3 && __GLIBCXX_BITSIZE_INT_N_3 % __CHAR_BIT__
126  _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_3, __GLIBCXX_BITSIZE_INT_N_3)
127 #endif
128 
129 #undef _GLIBCXX_INT_N_TRAITS
130 
131 #if __cplusplus >= 201103L
132  /// Convenience alias for __numeric_traits<integer-type>.
133  template<typename _Tp>
134  using __int_traits = __numeric_traits_integer<_Tp>;
135 #endif
136 
137 #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
138  (std::__are_same<_Tp, float>::__value ? _Fval \
139  : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
140 
141 #define __glibcxx_max_digits10(_Tp) \
142  (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
143  __LDBL_MANT_DIG__) * 643L / 2136)
144 
145 #define __glibcxx_digits10(_Tp) \
146  __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
147 
148 #define __glibcxx_max_exponent10(_Tp) \
149  __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
150  __LDBL_MAX_10_EXP__)
151 
152  // N.B. this only supports float, double and long double (no __float128 etc.)
153  template<typename _Value>
154  struct __numeric_traits_floating
155  {
156  // Only floating point types. See N1822.
157  static const int __max_digits10 = __glibcxx_max_digits10(_Value);
158 
159  // See above comment...
160  static const bool __is_signed = true;
161  static const int __digits10 = __glibcxx_digits10(_Value);
162  static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
163  };
164 
165  template<typename _Value>
166  const int __numeric_traits_floating<_Value>::__max_digits10;
167 
168  template<typename _Value>
169  const bool __numeric_traits_floating<_Value>::__is_signed;
170 
171  template<typename _Value>
172  const int __numeric_traits_floating<_Value>::__digits10;
173 
174  template<typename _Value>
175  const int __numeric_traits_floating<_Value>::__max_exponent10;
176 
177 #undef __glibcxx_floating
178 #undef __glibcxx_max_digits10
179 #undef __glibcxx_digits10
180 #undef __glibcxx_max_exponent10
181 
182  template<typename _Value>
183  struct __numeric_traits
184  : public __numeric_traits_integer<_Value>
185  { };
186 
187  template<>
188  struct __numeric_traits<float>
189  : public __numeric_traits_floating<float>
190  { };
191 
192  template<>
193  struct __numeric_traits<double>
194  : public __numeric_traits_floating<double>
195  { };
196 
197  template<>
198  struct __numeric_traits<long double>
199  : public __numeric_traits_floating<long double>
200  { };
201 
202 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
203 # if defined __LONG_DOUBLE_IEEE128__
204  // long double is __ieee128, define traits for __ibm128
205  template<>
206  struct __numeric_traits_floating<__ibm128>
207  {
208  static const int __max_digits10 = 33;
209  static const bool __is_signed = true;
210  static const int __digits10 = 31;
211  static const int __max_exponent10 = 308;
212  };
213  template<>
214  struct __numeric_traits<__ibm128>
215  : public __numeric_traits_floating<__ibm128>
216  { };
217 # elif defined __LONG_DOUBLE_IBM128__
218  // long double is __ibm128, define traits for __ieee128
219  template<>
220  struct __numeric_traits_floating<__ieee128>
221  {
222  static const int __max_digits10 = 36;
223  static const bool __is_signed = true;
224  static const int __digits10 = 33;
225  static const int __max_exponent10 = 4932;
226  };
227  template<>
228  struct __numeric_traits<__ieee128>
229  : public __numeric_traits_floating<__ieee128>
230  { };
231 # endif
232 #endif
233 
234 _GLIBCXX_END_NAMESPACE_VERSION
235 } // namespace
236 
237 #endif
GNU extensions for public use.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.