1 // boost integer.hpp header file -------------------------------------------//
\r
3 // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost
\r
4 // Software License, Version 1.0. (See accompanying file
\r
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 // See http://www.boost.org/libs/integer for documentation.
\r
10 // 22 Sep 01 Added value-based integer templates. (Daryle Walker)
\r
11 // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
\r
12 // 30 Jul 00 Add typename syntax fix (Jens Maurer)
\r
13 // 28 Aug 99 Initial version
\r
15 #ifndef BOOST_INTEGER_HPP
\r
16 #define BOOST_INTEGER_HPP
\r
18 #include <boost/integer_fwd.hpp> // self include
\r
20 #include <boost/integer_traits.hpp> // for boost::::boost::integer_traits
\r
21 #include <boost/limits.hpp> // for ::std::numeric_limits
\r
22 #include <boost/cstdint.hpp> // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T
\r
25 // We simply cannot include this header on gcc without getting copious warnings of the kind:
\r
27 // boost/integer.hpp:77:30: warning: use of C99 long long integer constant
\r
29 // And yet there is no other reasonable implementation, so we declare this a system header
\r
30 // to suppress these warnings.
\r
32 #if defined(__GNUC__) && (__GNUC__ >= 4)
\r
33 #pragma GCC system_header
\r
39 // Helper templates ------------------------------------------------------//
\r
41 // fast integers from least integers
\r
42 // int_fast_t<> works correctly for unsigned too, in spite of the name.
\r
43 template< typename LeastInt >
\r
46 typedef LeastInt fast;
\r
48 }; // imps may specialize
\r
52 // convert category to type
\r
53 template< int Category > struct int_least_helper {}; // default is empty
\r
55 // specializatons: 1=long, 2=int, 3=short, 4=signed char,
\r
56 // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
\r
57 // no specializations for 0 and 5: requests for a type > long are in error
\r
58 #ifdef BOOST_HAS_LONG_LONG
\r
59 template<> struct int_least_helper<1> { typedef boost::long_long_type least; };
\r
61 template<> struct int_least_helper<2> { typedef long least; };
\r
62 template<> struct int_least_helper<3> { typedef int least; };
\r
63 template<> struct int_least_helper<4> { typedef short least; };
\r
64 template<> struct int_least_helper<5> { typedef signed char least; };
\r
65 #ifdef BOOST_HAS_LONG_LONG
\r
66 template<> struct int_least_helper<6> { typedef boost::ulong_long_type least; };
\r
68 template<> struct int_least_helper<7> { typedef unsigned long least; };
\r
69 template<> struct int_least_helper<8> { typedef unsigned int least; };
\r
70 template<> struct int_least_helper<9> { typedef unsigned short least; };
\r
71 template<> struct int_least_helper<10> { typedef unsigned char least; };
\r
74 struct exact_signed_base_helper{};
\r
76 struct exact_unsigned_base_helper{};
\r
78 template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
\r
79 template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
\r
80 #if USHRT_MAX != UCHAR_MAX
\r
81 template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
\r
82 template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
\r
84 #if UINT_MAX != USHRT_MAX
\r
85 template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
\r
86 template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
\r
88 #if ULONG_MAX != UINT_MAX
\r
89 template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
\r
90 template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
\r
92 #if defined(BOOST_HAS_LONG_LONG) &&\
\r
93 ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
\r
94 (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
\r
95 (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
\r
96 (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
\r
97 template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
\r
98 template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
\r
102 } // namespace detail
\r
104 // integer templates specifying number of bits ---------------------------//
\r
107 template< int Bits > // bits (including sign) required
\r
108 struct int_t : public detail::exact_signed_base_helper<Bits>
\r
110 typedef typename detail::int_least_helper
\r
112 #ifdef BOOST_HAS_LONG_LONG
\r
113 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
\r
117 (Bits-1 <= ::std::numeric_limits<long>::digits) +
\r
118 (Bits-1 <= ::std::numeric_limits<int>::digits) +
\r
119 (Bits-1 <= ::std::numeric_limits<short>::digits) +
\r
120 (Bits-1 <= ::std::numeric_limits<signed char>::digits)
\r
122 typedef typename int_fast_t<least>::type fast;
\r
126 template< int Bits > // bits required
\r
127 struct uint_t : public detail::exact_unsigned_base_helper<Bits>
\r
129 #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
\r
130 // It's really not clear why this workaround should be needed... shrug I guess! JM
\r
131 BOOST_STATIC_CONSTANT(int, s =
\r
133 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
\r
134 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
\r
135 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
\r
136 (Bits <= ::std::numeric_limits<unsigned char>::digits));
\r
137 typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
\r
139 typedef typename detail::int_least_helper
\r
142 #ifdef BOOST_HAS_LONG_LONG
\r
143 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
\r
147 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
\r
148 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
\r
149 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
\r
150 (Bits <= ::std::numeric_limits<unsigned char>::digits)
\r
153 typedef typename int_fast_t<least>::type fast;
\r
154 // int_fast_t<> works correctly for unsigned too, in spite of the name.
\r
157 // integer templates specifying extreme value ----------------------------//
\r
160 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
161 template< boost::long_long_type MaxValue > // maximum value to require support
\r
163 template< long MaxValue > // maximum value to require support
\r
165 struct int_max_value_t
\r
167 typedef typename detail::int_least_helper
\r
169 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
170 (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
\r
174 (MaxValue <= ::boost::integer_traits<long>::const_max) +
\r
175 (MaxValue <= ::boost::integer_traits<int>::const_max) +
\r
176 (MaxValue <= ::boost::integer_traits<short>::const_max) +
\r
177 (MaxValue <= ::boost::integer_traits<signed char>::const_max)
\r
179 typedef typename int_fast_t<least>::type fast;
\r
182 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
183 template< boost::long_long_type MinValue > // minimum value to require support
\r
185 template< long MinValue > // minimum value to require support
\r
187 struct int_min_value_t
\r
189 typedef typename detail::int_least_helper
\r
191 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
192 (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
\r
196 (MinValue >= ::boost::integer_traits<long>::const_min) +
\r
197 (MinValue >= ::boost::integer_traits<int>::const_min) +
\r
198 (MinValue >= ::boost::integer_traits<short>::const_min) +
\r
199 (MinValue >= ::boost::integer_traits<signed char>::const_min)
\r
201 typedef typename int_fast_t<least>::type fast;
\r
205 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
206 template< boost::ulong_long_type MaxValue > // minimum value to require support
\r
208 template< unsigned long MaxValue > // minimum value to require support
\r
210 struct uint_value_t
\r
212 #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
\r
213 // It's really not clear why this workaround should be needed... shrug I guess! JM
\r
214 #if defined(BOOST_NO_INTEGRAL_INT64_T)
\r
215 BOOST_STATIC_CONSTANT(unsigned, which =
\r
217 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
\r
218 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
\r
219 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
\r
220 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
\r
221 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
\r
222 #else // BOOST_NO_INTEGRAL_INT64_T
\r
223 BOOST_STATIC_CONSTANT(unsigned, which =
\r
225 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
\r
226 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
\r
227 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
\r
228 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
\r
229 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
\r
230 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
\r
231 #endif // BOOST_NO_INTEGRAL_INT64_T
\r
233 typedef typename detail::int_least_helper
\r
236 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
\r
237 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
\r
241 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
\r
242 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
\r
243 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
\r
244 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
\r
247 typedef typename int_fast_t<least>::type fast;
\r
251 } // namespace boost
\r
253 #endif // BOOST_INTEGER_HPP
\r