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; };
61 template<> struct int_least_helper<2> { typedef long least; };
62 template<> struct int_least_helper<3> { typedef int least; };
63 template<> struct int_least_helper<4> { typedef short least; };
64 template<> struct int_least_helper<5> { typedef signed char least; };
65 #ifdef BOOST_HAS_LONG_LONG
66 template<> struct int_least_helper<6> { typedef boost::ulong_long_type least; };
68 template<> struct int_least_helper<7> { typedef unsigned long least; };
69 template<> struct int_least_helper<8> { typedef unsigned int least; };
70 template<> struct int_least_helper<9> { typedef unsigned short least; };
71 template<> struct int_least_helper<10> { typedef unsigned char least; };
74 struct exact_signed_base_helper{};
76 struct exact_unsigned_base_helper{};
78 template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
79 template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
80 #if USHRT_MAX != UCHAR_MAX
81 template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
82 template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
84 #if UINT_MAX != USHRT_MAX
85 template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
86 template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
88 #if ULONG_MAX != UINT_MAX
89 template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
90 template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
92 #if defined(BOOST_HAS_LONG_LONG) &&\
93 ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
94 (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
95 (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
96 (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
97 template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
98 template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
102 } // namespace detail
104 // integer templates specifying number of bits ---------------------------//
107 template< int Bits > // bits (including sign) required
108 struct int_t : public detail::exact_signed_base_helper<Bits>
110 typedef typename detail::int_least_helper
112 #ifdef BOOST_HAS_LONG_LONG
113 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
117 (Bits-1 <= ::std::numeric_limits<long>::digits) +
118 (Bits-1 <= ::std::numeric_limits<int>::digits) +
119 (Bits-1 <= ::std::numeric_limits<short>::digits) +
120 (Bits-1 <= ::std::numeric_limits<signed char>::digits)
122 typedef typename int_fast_t<least>::type fast;
126 template< int Bits > // bits required
127 struct uint_t : public detail::exact_unsigned_base_helper<Bits>
129 #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
130 // It's really not clear why this workaround should be needed... shrug I guess! JM
131 BOOST_STATIC_CONSTANT(int, s =
133 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
134 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
135 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
136 (Bits <= ::std::numeric_limits<unsigned char>::digits));
137 typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
139 typedef typename detail::int_least_helper
142 #ifdef BOOST_HAS_LONG_LONG
143 (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
147 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
148 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
149 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
150 (Bits <= ::std::numeric_limits<unsigned char>::digits)
153 typedef typename int_fast_t<least>::type fast;
154 // int_fast_t<> works correctly for unsigned too, in spite of the name.
157 // integer templates specifying extreme value ----------------------------//
160 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
161 template< boost::long_long_type MaxValue > // maximum value to require support
163 template< long MaxValue > // maximum value to require support
165 struct int_max_value_t
167 typedef typename detail::int_least_helper
169 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
170 (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
174 (MaxValue <= ::boost::integer_traits<long>::const_max) +
175 (MaxValue <= ::boost::integer_traits<int>::const_max) +
176 (MaxValue <= ::boost::integer_traits<short>::const_max) +
177 (MaxValue <= ::boost::integer_traits<signed char>::const_max)
179 typedef typename int_fast_t<least>::type fast;
182 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
183 template< boost::long_long_type MinValue > // minimum value to require support
185 template< long MinValue > // minimum value to require support
187 struct int_min_value_t
189 typedef typename detail::int_least_helper
191 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
192 (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
196 (MinValue >= ::boost::integer_traits<long>::const_min) +
197 (MinValue >= ::boost::integer_traits<int>::const_min) +
198 (MinValue >= ::boost::integer_traits<short>::const_min) +
199 (MinValue >= ::boost::integer_traits<signed char>::const_min)
201 typedef typename int_fast_t<least>::type fast;
205 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
206 template< boost::ulong_long_type MaxValue > // minimum value to require support
208 template< unsigned long MaxValue > // minimum value to require support
212 #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
213 // It's really not clear why this workaround should be needed... shrug I guess! JM
214 #if defined(BOOST_NO_INTEGRAL_INT64_T)
215 BOOST_STATIC_CONSTANT(unsigned, which =
217 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
218 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
219 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
220 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
221 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
222 #else // BOOST_NO_INTEGRAL_INT64_T
223 BOOST_STATIC_CONSTANT(unsigned, which =
225 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
226 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
227 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
228 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
229 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
230 typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
231 #endif // BOOST_NO_INTEGRAL_INT64_T
233 typedef typename detail::int_least_helper
236 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
237 (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
241 (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
242 (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
243 (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
244 (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
247 typedef typename int_fast_t<least>::type fast;
253 #endif // BOOST_INTEGER_HPP