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