]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/numeric_traits.h
type_traits.h (__numeric_traits): Move...
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / numeric_traits.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 /** @file ext/numeric_traits.h
32 * This file is a GNU extension to the Standard C++ Library.
33 */
34
35 #ifndef _EXT_NUMERIC_TRAITS
36 #define _EXT_NUMERIC_TRAITS 1
37
38 #pragma GCC system_header
39
40 #include <limits>
41 #include <bits/cpp_type_traits.h>
42 #include <ext/type_traits.h>
43
44 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45
46 // Compile time constants for builtin types.
47 // Sadly std::numeric_limits member functions cannot be used for this.
48 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
49 #define __glibcxx_digits(_Tp) \
50 (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
51
52 #define __glibcxx_min(_Tp) \
53 (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
54
55 #define __glibcxx_max(_Tp) \
56 (__glibcxx_signed(_Tp) ? \
57 (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
58
59 template<typename _Value>
60 struct __numeric_traits_integer
61 {
62 // Only integers for initialization of member constant.
63 static const _Value __min = __glibcxx_min(_Value);
64 static const _Value __max = __glibcxx_max(_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 struct __numeric_traits_floating
75 {
76 // Only floating point types. See N1822.
77 static const int __max_digits10 =
78 2 + std::numeric_limits<_Value>::digits * 3010/10000;
79 };
80
81 template<typename _Value>
82 const int __numeric_traits_floating<_Value>::__max_digits10;
83
84 template<typename _Value>
85 struct __numeric_traits
86 : public __conditional_type<std::__is_integer<_Value>::__value,
87 __numeric_traits_integer<_Value>,
88 __numeric_traits_floating<_Value> >::__type
89 { };
90
91 _GLIBCXX_END_NAMESPACE
92
93 #undef __glibcxx_signed
94 #undef __glibcxx_min
95 #undef __glibcxx_max
96 #undef __glibcxx_digits
97
98 #endif