xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/ext/numeric_traits.h (revision b4ec0e72446ae98ef6c5803d0441b02ac1f6fdee)
1*404b540aSrobert // -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2007 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the terms
7*404b540aSrobert // of the GNU General Public License as published by the Free Software
8*404b540aSrobert // Foundation; either version 2, or (at your option) any later
9*404b540aSrobert // version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful, but
12*404b540aSrobert // WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*404b540aSrobert // General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free
22*404b540aSrobert // software library without restriction.  Specifically, if other files
23*404b540aSrobert // instantiate templates or use macros or inline functions from this
24*404b540aSrobert // file, or you compile this file and link it with other files to
25*404b540aSrobert // produce an executable, this file does not by itself cause the
26*404b540aSrobert // resulting executable to be covered by the GNU General Public
27*404b540aSrobert // License.  This exception does not however invalidate any other
28*404b540aSrobert // reasons why the executable file might be covered by the GNU General
29*404b540aSrobert // Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file ext/numeric_traits.h
32*404b540aSrobert  *  This file is a GNU extension to the Standard C++ Library.
33*404b540aSrobert  */
34*404b540aSrobert 
35*404b540aSrobert #ifndef _EXT_NUMERIC_TRAITS
36*404b540aSrobert #define _EXT_NUMERIC_TRAITS 1
37*404b540aSrobert 
38*404b540aSrobert #pragma GCC system_header
39*404b540aSrobert 
40*404b540aSrobert #include <limits>
41*404b540aSrobert #include <bits/cpp_type_traits.h>
42*404b540aSrobert #include <ext/type_traits.h>
43*404b540aSrobert 
44*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45*404b540aSrobert 
46*404b540aSrobert   // Compile time constants for builtin types.
47*404b540aSrobert   // Sadly std::numeric_limits member functions cannot be used for this.
48*404b540aSrobert #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
49*404b540aSrobert #define __glibcxx_digits(_Tp) \
50*404b540aSrobert   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
51*404b540aSrobert 
52*404b540aSrobert #define __glibcxx_min(_Tp) \
53*404b540aSrobert   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
54*404b540aSrobert 
55*404b540aSrobert #define __glibcxx_max(_Tp) \
56*404b540aSrobert   (__glibcxx_signed(_Tp) ? \
57*404b540aSrobert    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
58*404b540aSrobert 
59*404b540aSrobert   template<typename _Value>
60*404b540aSrobert     struct __numeric_traits_integer
61*404b540aSrobert     {
62*404b540aSrobert       // Only integers for initialization of member constant.
63*404b540aSrobert       static const _Value __min = __glibcxx_min(_Value);
64*404b540aSrobert       static const _Value __max = __glibcxx_max(_Value);
65*404b540aSrobert     };
66*404b540aSrobert 
67*404b540aSrobert   template<typename _Value>
68*404b540aSrobert     const _Value __numeric_traits_integer<_Value>::__min;
69*404b540aSrobert 
70*404b540aSrobert   template<typename _Value>
71*404b540aSrobert     const _Value __numeric_traits_integer<_Value>::__max;
72*404b540aSrobert 
73*404b540aSrobert   template<typename _Value>
74*404b540aSrobert     struct __numeric_traits_floating
75*404b540aSrobert     {
76*404b540aSrobert       // Only floating point types. See N1822.
77*404b540aSrobert       static const int __max_digits10 =
78*404b540aSrobert 	2 + std::numeric_limits<_Value>::digits * 3010/10000;
79*404b540aSrobert     };
80*404b540aSrobert 
81*404b540aSrobert   template<typename _Value>
82*404b540aSrobert     const int __numeric_traits_floating<_Value>::__max_digits10;
83*404b540aSrobert 
84*404b540aSrobert   template<typename _Value>
85*404b540aSrobert     struct __numeric_traits
86*404b540aSrobert     : public __conditional_type<std::__is_integer<_Value>::__value,
87*404b540aSrobert 				__numeric_traits_integer<_Value>,
88*404b540aSrobert 				__numeric_traits_floating<_Value> >::__type
89*404b540aSrobert     { };
90*404b540aSrobert 
91*404b540aSrobert _GLIBCXX_END_NAMESPACE
92*404b540aSrobert 
93*404b540aSrobert #undef __glibcxx_signed
94*404b540aSrobert #undef __glibcxx_min
95*404b540aSrobert #undef __glibcxx_max
96*404b540aSrobert #undef __glibcxx_digits
97*404b540aSrobert 
98*404b540aSrobert #endif
99