1 | // boost integer.hpp header file -------------------------------------------// |
---|
2 | |
---|
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) |
---|
6 | |
---|
7 | // See http://www.boost.org/libs/integer for documentation. |
---|
8 | |
---|
9 | // Revision History |
---|
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 |
---|
14 | |
---|
15 | #ifndef BOOST_INTEGER_HPP |
---|
16 | #define BOOST_INTEGER_HPP |
---|
17 | |
---|
18 | #include <boost/integer_fwd.hpp> // self include |
---|
19 | |
---|
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 |
---|
23 | |
---|
24 | // |
---|
25 | // We simply cannot include this header on gcc without getting copious warnings of the kind: |
---|
26 | // |
---|
27 | // boost/integer.hpp:77:30: warning: use of C99 long long integer constant |
---|
28 | // |
---|
29 | // And yet there is no other reasonable implementation, so we declare this a system header |
---|
30 | // to suppress these warnings. |
---|
31 | // |
---|
32 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
---|
33 | #pragma GCC system_header |
---|
34 | #endif |
---|
35 | |
---|
36 | namespace boost |
---|
37 | { |
---|
38 | |
---|
39 | // Helper templates ------------------------------------------------------// |
---|
40 | |
---|
41 | // fast integers from least integers |
---|
42 | // int_fast_t<> works correctly for unsigned too, in spite of the name. |
---|
43 | template< typename LeastInt > |
---|
44 | struct int_fast_t |
---|
45 | { |
---|
46 | typedef LeastInt fast; |
---|
47 | typedef fast type; |
---|
48 | }; // imps may specialize |
---|
49 | |
---|
50 | namespace detail{ |
---|
51 | |
---|
52 | // convert category to type |
---|
53 | template< int Category > struct int_least_helper {}; // default is empty |
---|
54 | |
---|
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; }; |
---|
62 | #endif |
---|
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; }; |
---|
71 | #endif |
---|
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; }; |
---|
76 | |
---|
77 | template <int Bits> |
---|
78 | struct exact_signed_base_helper{}; |
---|
79 | template <int Bits> |
---|
80 | struct exact_unsigned_base_helper{}; |
---|
81 | |
---|
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; }; |
---|
87 | #endif |
---|
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; }; |
---|
91 | #endif |
---|
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; }; |
---|
95 | #endif |
---|
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; }; |
---|
103 | #endif |
---|
104 | |
---|
105 | |
---|
106 | } // namespace detail |
---|
107 | |
---|
108 | // integer templates specifying number of bits ---------------------------// |
---|
109 | |
---|
110 | // signed |
---|
111 | template< int Bits > // bits (including sign) required |
---|
112 | struct int_t : public detail::exact_signed_base_helper<Bits> |
---|
113 | { |
---|
114 | typedef typename detail::int_least_helper |
---|
115 | < |
---|
116 | #ifdef BOOST_HAS_LONG_LONG |
---|
117 | (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) + |
---|
118 | #else |
---|
119 | 1 + |
---|
120 | #endif |
---|
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) |
---|
125 | >::least least; |
---|
126 | typedef typename int_fast_t<least>::type fast; |
---|
127 | }; |
---|
128 | |
---|
129 | // unsigned |
---|
130 | template< int Bits > // bits required |
---|
131 | struct uint_t : public detail::exact_unsigned_base_helper<Bits> |
---|
132 | { |
---|
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 = |
---|
136 | 6 + |
---|
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; |
---|
142 | #else |
---|
143 | typedef typename detail::int_least_helper |
---|
144 | < |
---|
145 | 5 + |
---|
146 | #ifdef BOOST_HAS_LONG_LONG |
---|
147 | (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) + |
---|
148 | #else |
---|
149 | 1 + |
---|
150 | #endif |
---|
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) |
---|
155 | >::least least; |
---|
156 | #endif |
---|
157 | typedef typename int_fast_t<least>::type fast; |
---|
158 | // int_fast_t<> works correctly for unsigned too, in spite of the name. |
---|
159 | }; |
---|
160 | |
---|
161 | // integer templates specifying extreme value ----------------------------// |
---|
162 | |
---|
163 | // signed |
---|
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 |
---|
166 | #else |
---|
167 | template< long MaxValue > // maximum value to require support |
---|
168 | #endif |
---|
169 | struct int_max_value_t |
---|
170 | { |
---|
171 | typedef typename detail::int_least_helper |
---|
172 | < |
---|
173 | #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) |
---|
174 | (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) + |
---|
175 | #else |
---|
176 | 1 + |
---|
177 | #endif |
---|
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) |
---|
182 | >::least least; |
---|
183 | typedef typename int_fast_t<least>::type fast; |
---|
184 | }; |
---|
185 | |
---|
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 |
---|
188 | #else |
---|
189 | template< long MinValue > // minimum value to require support |
---|
190 | #endif |
---|
191 | struct int_min_value_t |
---|
192 | { |
---|
193 | typedef typename detail::int_least_helper |
---|
194 | < |
---|
195 | #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) |
---|
196 | (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) + |
---|
197 | #else |
---|
198 | 1 + |
---|
199 | #endif |
---|
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) |
---|
204 | >::least least; |
---|
205 | typedef typename int_fast_t<least>::type fast; |
---|
206 | }; |
---|
207 | |
---|
208 | // unsigned |
---|
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 |
---|
211 | #else |
---|
212 | template< unsigned long MaxValue > // minimum value to require support |
---|
213 | #endif |
---|
214 | struct uint_value_t |
---|
215 | { |
---|
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 = |
---|
220 | 6 + |
---|
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 = |
---|
228 | 5 + |
---|
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 |
---|
236 | #else |
---|
237 | typedef typename detail::int_least_helper |
---|
238 | < |
---|
239 | 5 + |
---|
240 | #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) |
---|
241 | (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) + |
---|
242 | #else |
---|
243 | 1 + |
---|
244 | #endif |
---|
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) |
---|
249 | >::least least; |
---|
250 | #endif |
---|
251 | typedef typename int_fast_t<least>::type fast; |
---|
252 | }; |
---|
253 | |
---|
254 | |
---|
255 | } // namespace boost |
---|
256 | |
---|
257 | #endif // BOOST_INTEGER_HPP |
---|