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