libstdc++
numeric_traits.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/numeric_traits.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _EXT_NUMERIC_TRAITS
00030 #define _EXT_NUMERIC_TRAITS 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <bits/cpp_type_traits.h>
00035 #include <ext/type_traits.h>
00036 
00037 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041   // Compile time constants for builtin types.
00042   // Sadly std::numeric_limits member functions cannot be used for this.
00043 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
00044 #define __glibcxx_digits(_Tp) \
00045   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
00046 
00047 #define __glibcxx_min(_Tp) \
00048   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
00049 
00050 #define __glibcxx_max(_Tp) \
00051   (__glibcxx_signed(_Tp) ? \
00052    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
00053 
00054   template<typename _Value>
00055     struct __numeric_traits_integer
00056     {
00057       // Only integers for initialization of member constant.
00058       static const _Value __min = __glibcxx_min(_Value);
00059       static const _Value __max = __glibcxx_max(_Value);
00060 
00061       // NB: these two also available in std::numeric_limits as compile
00062       // time constants, but <limits> is big and we avoid including it.
00063       static const bool __is_signed = __glibcxx_signed(_Value);
00064       static const int __digits = __glibcxx_digits(_Value);      
00065     };
00066 
00067   template<typename _Value>
00068     const _Value __numeric_traits_integer<_Value>::__min;
00069 
00070   template<typename _Value>
00071     const _Value __numeric_traits_integer<_Value>::__max;
00072 
00073   template<typename _Value>
00074     const bool __numeric_traits_integer<_Value>::__is_signed;
00075 
00076   template<typename _Value>
00077     const int __numeric_traits_integer<_Value>::__digits;
00078 
00079 #undef __glibcxx_signed
00080 #undef __glibcxx_digits
00081 #undef __glibcxx_min
00082 #undef __glibcxx_max
00083 
00084 #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
00085   (std::__are_same<_Tp, float>::__value ? _Fval \
00086    : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
00087 
00088 #define __glibcxx_max_digits10(_Tp) \
00089   (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
00090               __LDBL_MANT_DIG__) * 643L / 2136)
00091 
00092 #define __glibcxx_digits10(_Tp) \
00093   __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
00094 
00095 #define __glibcxx_max_exponent10(_Tp) \
00096   __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
00097              __LDBL_MAX_10_EXP__)
00098 
00099   template<typename _Value>
00100     struct __numeric_traits_floating
00101     {
00102       // Only floating point types. See N1822. 
00103       static const int __max_digits10 = __glibcxx_max_digits10(_Value);
00104 
00105       // See above comment...
00106       static const bool __is_signed = true;
00107       static const int __digits10 = __glibcxx_digits10(_Value);
00108       static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
00109     };
00110 
00111   template<typename _Value>
00112     const int __numeric_traits_floating<_Value>::__max_digits10;
00113 
00114   template<typename _Value>
00115     const bool __numeric_traits_floating<_Value>::__is_signed;
00116 
00117   template<typename _Value>
00118     const int __numeric_traits_floating<_Value>::__digits10;
00119 
00120   template<typename _Value>
00121     const int __numeric_traits_floating<_Value>::__max_exponent10;
00122 
00123   template<typename _Value>
00124     struct __numeric_traits
00125     : public __conditional_type<std::__is_integer<_Value>::__value,
00126                 __numeric_traits_integer<_Value>,
00127                 __numeric_traits_floating<_Value> >::__type
00128     { };
00129 
00130 _GLIBCXX_END_NAMESPACE_VERSION
00131 } // namespace
00132 
00133 #undef __glibcxx_floating
00134 #undef __glibcxx_max_digits10
00135 #undef __glibcxx_digits10
00136 #undef __glibcxx_max_exponent10
00137 
00138 #endif