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 99 // -*- C++ -*- 100 101 // Copyright (C) 2007 Free Software Foundation, Inc. 102 // 103 // This file is part of the GNU ISO C++ Library. This library is free 104 // software; you can redistribute it and/or modify it under the terms 105 // of the GNU General Public License as published by the Free Software 106 // Foundation; either version 2, or (at your option) any later 107 // version. 108 109 // This library is distributed in the hope that it will be useful, but 110 // WITHOUT ANY WARRANTY; without even the implied warranty of 111 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 112 // General Public License for more details. 113 114 // You should have received a copy of the GNU General Public License along 115 // with this library; see the file COPYING. If not, write to the Free 116 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 117 // USA. 118 119 // As a special exception, you may use this file as part of a free 120 // software library without restriction. Specifically, if other files 121 // instantiate templates or use macros or inline functions from this 122 // file, or you compile this file and link it with other files to 123 // produce an executable, this file does not by itself cause the 124 // resulting executable to be covered by the GNU General Public 125 // License. This exception does not however invalidate any other 126 // reasons why the executable file might be covered by the GNU General 127 // Public License. 128 129 /** @file ext/numeric_traits.h 130 * This file is a GNU extension to the Standard C++ Library. 131 */ 132 133 #ifndef _EXT_NUMERIC_TRAITS 134 #define _EXT_NUMERIC_TRAITS 1 135 136 #pragma GCC system_header 137 138 #include <limits> 139 #include <bits/cpp_type_traits.h> 140 #include <ext/type_traits.h> 141 142 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 143 144 // Compile time constants for builtin types. 145 // Sadly std::numeric_limits member functions cannot be used for this. 146 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0) 147 #define __glibcxx_digits(_Tp) \ 148 (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp)) 149 150 #define __glibcxx_min(_Tp) \ 151 (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0) 152 153 #define __glibcxx_max(_Tp) \ 154 (__glibcxx_signed(_Tp) ? \ 155 (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0) 156 157 template<typename _Value> 158 struct __numeric_traits_integer 159 { 160 // Only integers for initialization of member constant. 161 static const _Value __min = __glibcxx_min(_Value); 162 static const _Value __max = __glibcxx_max(_Value); 163 }; 164 165 template<typename _Value> 166 const _Value __numeric_traits_integer<_Value>::__min; 167 168 template<typename _Value> 169 const _Value __numeric_traits_integer<_Value>::__max; 170 171 template<typename _Value> 172 struct __numeric_traits_floating 173 { 174 // Only floating point types. See N1822. 175 static const int __max_digits10 = 176 2 + std::numeric_limits<_Value>::digits * 3010/10000; 177 }; 178 179 template<typename _Value> 180 const int __numeric_traits_floating<_Value>::__max_digits10; 181 182 template<typename _Value> 183 struct __numeric_traits 184 : public __conditional_type<std::__is_integer<_Value>::__value, 185 __numeric_traits_integer<_Value>, 186 __numeric_traits_floating<_Value> >::__type 187 { }; 188 189 _GLIBCXX_END_NAMESPACE 190 191 #undef __glibcxx_signed 192 #undef __glibcxx_min 193 #undef __glibcxx_max 194 #undef __glibcxx_digits 195 196 #endif 197