xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/ext/numeric_traits.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2007-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the terms
7*38fd1498Szrj // of the GNU General Public License as published by the Free Software
8*38fd1498Szrj // Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj // version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful, but
12*38fd1498Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*38fd1498Szrj // General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file ext/numeric_traits.h
26*38fd1498Szrj  *  This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _EXT_NUMERIC_TRAITS
30*38fd1498Szrj #define _EXT_NUMERIC_TRAITS 1
31*38fd1498Szrj 
32*38fd1498Szrj #pragma GCC system_header
33*38fd1498Szrj 
34*38fd1498Szrj #include <bits/cpp_type_traits.h>
35*38fd1498Szrj #include <ext/type_traits.h>
36*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)37*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
38*38fd1498Szrj {
39*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
40*38fd1498Szrj 
41*38fd1498Szrj   // Compile time constants for builtin types.
42*38fd1498Szrj   // Sadly std::numeric_limits member functions cannot be used for this.
43*38fd1498Szrj #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
44*38fd1498Szrj #define __glibcxx_digits(_Tp) \
45*38fd1498Szrj   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
46*38fd1498Szrj 
47*38fd1498Szrj #define __glibcxx_min(_Tp) \
48*38fd1498Szrj   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
49*38fd1498Szrj 
50*38fd1498Szrj #define __glibcxx_max(_Tp) \
51*38fd1498Szrj   (__glibcxx_signed(_Tp) ? \
52*38fd1498Szrj    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
53*38fd1498Szrj 
54*38fd1498Szrj   template<typename _Value>
55*38fd1498Szrj     struct __numeric_traits_integer
56*38fd1498Szrj     {
57*38fd1498Szrj       // Only integers for initialization of member constant.
58*38fd1498Szrj       static const _Value __min = __glibcxx_min(_Value);
59*38fd1498Szrj       static const _Value __max = __glibcxx_max(_Value);
60*38fd1498Szrj 
61*38fd1498Szrj       // NB: these two also available in std::numeric_limits as compile
62*38fd1498Szrj       // time constants, but <limits> is big and we avoid including it.
63*38fd1498Szrj       static const bool __is_signed = __glibcxx_signed(_Value);
64*38fd1498Szrj       static const int __digits = __glibcxx_digits(_Value);
65*38fd1498Szrj     };
66*38fd1498Szrj 
67*38fd1498Szrj   template<typename _Value>
68*38fd1498Szrj     const _Value __numeric_traits_integer<_Value>::__min;
69*38fd1498Szrj 
70*38fd1498Szrj   template<typename _Value>
71*38fd1498Szrj     const _Value __numeric_traits_integer<_Value>::__max;
72*38fd1498Szrj 
73*38fd1498Szrj   template<typename _Value>
74*38fd1498Szrj     const bool __numeric_traits_integer<_Value>::__is_signed;
75*38fd1498Szrj 
76*38fd1498Szrj   template<typename _Value>
77*38fd1498Szrj     const int __numeric_traits_integer<_Value>::__digits;
78*38fd1498Szrj 
79*38fd1498Szrj #undef __glibcxx_signed
80*38fd1498Szrj #undef __glibcxx_digits
81*38fd1498Szrj #undef __glibcxx_min
82*38fd1498Szrj #undef __glibcxx_max
83*38fd1498Szrj 
84*38fd1498Szrj #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
85*38fd1498Szrj   (std::__are_same<_Tp, float>::__value ? _Fval \
86*38fd1498Szrj    : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
87*38fd1498Szrj 
88*38fd1498Szrj #define __glibcxx_max_digits10(_Tp) \
89*38fd1498Szrj   (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
90*38fd1498Szrj 			  __LDBL_MANT_DIG__) * 643L / 2136)
91*38fd1498Szrj 
92*38fd1498Szrj #define __glibcxx_digits10(_Tp) \
93*38fd1498Szrj   __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
94*38fd1498Szrj 
95*38fd1498Szrj #define __glibcxx_max_exponent10(_Tp) \
96*38fd1498Szrj   __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
97*38fd1498Szrj 		     __LDBL_MAX_10_EXP__)
98*38fd1498Szrj 
99*38fd1498Szrj   template<typename _Value>
100*38fd1498Szrj     struct __numeric_traits_floating
101*38fd1498Szrj     {
102*38fd1498Szrj       // Only floating point types. See N1822.
103*38fd1498Szrj       static const int __max_digits10 = __glibcxx_max_digits10(_Value);
104*38fd1498Szrj 
105*38fd1498Szrj       // See above comment...
106*38fd1498Szrj       static const bool __is_signed = true;
107*38fd1498Szrj       static const int __digits10 = __glibcxx_digits10(_Value);
108*38fd1498Szrj       static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
109*38fd1498Szrj     };
110*38fd1498Szrj 
111*38fd1498Szrj   template<typename _Value>
112*38fd1498Szrj     const int __numeric_traits_floating<_Value>::__max_digits10;
113*38fd1498Szrj 
114*38fd1498Szrj   template<typename _Value>
115*38fd1498Szrj     const bool __numeric_traits_floating<_Value>::__is_signed;
116*38fd1498Szrj 
117*38fd1498Szrj   template<typename _Value>
118*38fd1498Szrj     const int __numeric_traits_floating<_Value>::__digits10;
119*38fd1498Szrj 
120*38fd1498Szrj   template<typename _Value>
121*38fd1498Szrj     const int __numeric_traits_floating<_Value>::__max_exponent10;
122*38fd1498Szrj 
123*38fd1498Szrj   template<typename _Value>
124*38fd1498Szrj     struct __numeric_traits
125*38fd1498Szrj     : public __conditional_type<std::__is_integer<_Value>::__value,
126*38fd1498Szrj 				__numeric_traits_integer<_Value>,
127*38fd1498Szrj 				__numeric_traits_floating<_Value> >::__type
128*38fd1498Szrj     { };
129*38fd1498Szrj 
130*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
131*38fd1498Szrj } // namespace
132*38fd1498Szrj 
133*38fd1498Szrj #undef __glibcxx_floating
134*38fd1498Szrj #undef __glibcxx_max_digits10
135*38fd1498Szrj #undef __glibcxx_digits10
136*38fd1498Szrj #undef __glibcxx_max_exponent10
137*38fd1498Szrj 
138*38fd1498Szrj #endif
139