1 // boost integer.hpp header file -------------------------------------------//
3 // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/integer for documentation.
10 // 22 Sep 01 Added value-based integer templates. (Daryle Walker)
11 // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
12 // 30 Jul 00 Add typename syntax fix (Jens Maurer)
13 // 28 Aug 99 Initial version
15 #ifndef BOOST_INTEGER_HPP
16 #define BOOST_INTEGER_HPP
18 #include <boost/integer_fwd.hpp> // self include
20 #include <boost/integer_traits.hpp> // for boost::::boost::integer_traits
21 #include <boost/limits.hpp> // for ::std::numeric_limits
22 #include <boost/cstdint.hpp> // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T
25 // We simply cannot include this header on gcc without getting copious warnings of the kind:
27 // boost/integer.hpp:77:30: warning: use of C99 long long integer constant
29 // And yet there is no other reasonable implementation, so we declare this a system header
30 // to suppress these warnings.
32 #if defined(__GNUC__) && (__GNUC__ >= 4)
33 #pragma GCC system_header
39 // Helper templates ------------------------------------------------------//
41 // fast integers from least integers
42 // int_fast_t<> works correctly for unsigned too, in spite of the name.
43 template< typename LeastInt >
46 typedef LeastInt fast;
48 }; // imps may specialize
52 // convert category to type
53 template< int Category > struct int_least_helper {}; // default is empty
55 // specializatons: 1=long, 2=int, 3=short, 4=signed char,
56 // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
57 // no specializations for 0 and 5: requests for a type > long are in error
58 #ifdef BOOST_HAS_LONG_LONG
59 template<> struct int_least_helper<1> { typedef boost::long_long_type least; };
60 #elif defined(BOOST_HAS_MS_INT64)
61 template<> struct int_least_helper<1> { typedef __int64 least; };
63 template<> struct int_least_helper<2> { typedef long least; };
64 template<> struct int_least_helper<3> { typedef int least; };
65 template<> struct int_least_helper<4> { typedef short least; };
66 template<> struct int_least_helper<5> { typedef signed char least; };
67 #ifdef BOOST_HAS_LONG_LONG
68 template<> struct int_least_helper<6> { typedef boost::ulong_long_type least; };
69 #elif defined(BOOST_HAS_MS_INT64)
70 template<> struct int_least_helper<6> { typedef unsigned __int64 least; };
72 template<> struct int_least_helper<7> { typedef unsigned long least; };
73 template<> struct int_least_helper<8> { typedef unsigned int least; };
74 template<> struct int_least_helper<9> { typedef unsigned short least; };
75 template<> struct int_least_helper<10> { typedef unsigned char least; };
78 struct exact_signed_base_helper{};
80 struct exact_unsigned_base_helper{};
82 template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
83 template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
84 #if USHRT_MAX != UCHAR_MAX
85 template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
86 template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
88 #if UINT_MAX != USHRT_MAX
89 template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
90 template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
92 #if ULONG_MAX != UINT_MAX
93 template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
94 template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
96 #if defined(BOOST_HAS_LONG_LONG) &&\
97 ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
98 (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
99 (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
100 (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
101 template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
102 template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
106 } // namespace detail
108 // integer templates specifying number of bits ---------------------------//
111 template< int Bits > // bits (including sign) required
112 struct int_t : public detail::exact_signed_base_helper<Bits>
114 typedef typename detail::int_least_helper
116 #ifdef BOOST_HAS_LONG_LONG
117 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
121 (Bits-1 <= ::std::numeric_limits<long>::digits) +
122 (Bits-1 <= ::std::numeric_limits<int>::digits) +
123 (Bits-1 <= ::std::numeric_limits<short>::digits) +
124 (Bits-1 <= ::std::numeric_limits<signed char>::digits)
126 typedef typename int_fast_t<least>::type fast;
130 template< int Bits > // bits required
131 struct uint_t : public detail::exact_unsigned_base_helper<Bits>
133 #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
134 // It's really not clear why this workaround should be needed... shrug I guess! JM
135 BOOST_STATIC_CONSTANT(int, s =
137 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
138 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
139 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
140 (Bits <= ::std::numeric_limits<unsigned char>::digits));
141 typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
143 typedef typename detail::int_least_helper
146 #ifdef BOOST_HAS_LONG_LONG
147 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
151 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
152 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
153 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
154 (Bits <= ::std::numeric_limits<unsigned char>::digits)
157 typedef typename int_fast_t<least>::type fast;
158 // int_fast_t<> works correctly for unsigned too, in spite of the name.
161 // integer templates specifying extreme value ----------------------------//
164 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
165 template< boost::long_long_type MaxValue > // maximum value to require support
167 template< long MaxValue > // maximum value to require support
169 struct int_max_value_t
171 typedef typename detail::int_least_helper
173 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
174 (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
178 (MaxValue <= ::boost::integer_traits<long>::const_max) +
179 (MaxValue <= ::boost::integer_traits<int>::const_max) +
180 (MaxValue <= ::boost::integer_traits<short>::const_max) +
181 (MaxValue <= ::boost::integer_traits<signed char>::const_max)
183 typedef typename int_fast_t<least>::type fast;
186 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
187 template< boost::long_long_type MinValue > // minimum value to require support
189 template< long MinValue > // minimum value to require support
191 struct int_min_value_t
193 typedef typename detail::int_least_helper
195 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
196 (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
200 (MinValue >= ::boost::integer_traits<long>::const_min) +
201 (MinValue >= ::boost::integer_traits<int>::const_min) +
202 (MinValue >= ::boost::integer_traits<short>::const_min) +
203 (MinValue >= ::boost::integer_traits<signed char>::const_min)
205 typedef typename int_fast_t<least>::type fast;
209 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
210 template< boost::ulong_long_type MaxValue > // minimum value to require support
212 template< unsigned long MaxValue > // minimum value to require support
216 #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
217 // It's really not clear why this workaround should be needed... shrug I guess! JM
218 #if defined(BOOST_NO_INTEGRAL_INT64_T)
219 BOOST_STATIC_CONSTANT(unsigned, which =
221 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
222 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
223 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
224 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
225 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
226 #else // BOOST_NO_INTEGRAL_INT64_T
227 BOOST_STATIC_CONSTANT(unsigned, which =
229 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
230 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
231 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
232 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
233 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
234 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
235 #endif // BOOST_NO_INTEGRAL_INT64_T
237 typedef typename detail::int_least_helper
240 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
241 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
245 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
246 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
247 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
248 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
251 typedef typename int_fast_t<least>::type fast;
257 #endif // BOOST_INTEGER_HPP