1// -*- C++ -*-
2
3// Copyright (C) 2007-2015 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#pragma GCC system_header
33
34#include <bits/cpp_type_traits.h>
35#include <ext/type_traits.h>
36
37namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
38{
39_GLIBCXX_BEGIN_NAMESPACE_VERSION
40
41 // Compile time constants for builtin types.
42 // Sadly std::numeric_limits member functions cannot be used for this.
43#define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
44#define __glibcxx_digits(_Tp) \
45 (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
46
47#define __glibcxx_min(_Tp) \
48 (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
49
50#define __glibcxx_max(_Tp) \
51 (__glibcxx_signed(_Tp) ? \
52 (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
53
54 template<typename _Value>
55 struct __numeric_traits_integer
56 {
57 // Only integers for initialization of member constant.
58 static const _Value __min = __glibcxx_min(_Value);
59 static const _Value __max = __glibcxx_max(_Value);
60
61 // NB: these two also available in std::numeric_limits as compile
62 // time constants, but <limits> is big and we avoid including it.
63 static const bool __is_signed = __glibcxx_signed(_Value);
64 static const int __digits = __glibcxx_digits(_Value);
65 };
66
67 template<typename _Value>
68 const _Value __numeric_traits_integer<_Value>::__min;
69
70 template<typename _Value>
71 const _Value __numeric_traits_integer<_Value>::__max;
72
73 template<typename _Value>
74 const bool __numeric_traits_integer<_Value>::__is_signed;
75
76 template<typename _Value>
77 const int __numeric_traits_integer<_Value>::__digits;
78
79#undef __glibcxx_signed
80#undef __glibcxx_digits
81#undef __glibcxx_min
82#undef __glibcxx_max
83
84#define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
85 (std::__are_same<_Tp, float>::__value ? _Fval \
86 : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
87
88#define __glibcxx_max_digits10(_Tp) \
89 (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
90 __LDBL_MANT_DIG__) * 643L / 2136)
91
92#define __glibcxx_digits10(_Tp) \
93 __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
94
95#define __glibcxx_max_exponent10(_Tp) \
96 __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
97 __LDBL_MAX_10_EXP__)
98
99 template<typename _Value>
100 struct __numeric_traits_floating
101 {
102 // Only floating point types. See N1822.
103 static const int __max_digits10 = __glibcxx_max_digits10(_Value);
104
105 // See above comment...
106 static const bool __is_signed = true;
107 static const int __digits10 = __glibcxx_digits10(_Value);
108 static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
109 };
110
111 template<typename _Value>
112 const int __numeric_traits_floating<_Value>::__max_digits10;
113
114 template<typename _Value>
115 const bool __numeric_traits_floating<_Value>::__is_signed;
116
117 template<typename _Value>
118 const int __numeric_traits_floating<_Value>::__digits10;
119
120 template<typename _Value>
121 const int __numeric_traits_floating<_Value>::__max_exponent10;
122
123 template<typename _Value>
124 struct __numeric_traits
125 : public __conditional_type<std::__is_integer<_Value>::__value,
126 __numeric_traits_integer<_Value>,
127 __numeric_traits_floating<_Value> >::__type
128 { };
129
130_GLIBCXX_END_NAMESPACE_VERSION
131} // namespace
132
133#undef __glibcxx_floating
134#undef __glibcxx_max_digits10
135#undef __glibcxx_digits10
136#undef __glibcxx_max_exponent10
137
138#endif
139