blob: 56e3ffe245859ca62d477baa2656e19f54eedcb4 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// 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 <ndnboost/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 <ndnboost/integer_fwd.hpp> // self include
19
20#include <ndnboost/integer_traits.hpp> // for ndnboost::::ndnboost::integer_traits
21#include <ndnboost/limits.hpp> // for ::std::numeric_limits
22#include <ndnboost/cstdint.hpp> // for ndnboost::int64_t and BOOST_NO_INTEGRAL_INT64_T
23#include <ndnboost/static_assert.hpp>
24
25//
26// We simply cannot include this header on gcc without getting copious warnings of the kind:
27//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070028// ndnboost/integer.hpp:77:30: warning: use of C99 long long integer constant
Jeff Thompsona28eed82013-08-22 16:21:10 -070029//
30// And yet there is no other reasonable implementation, so we declare this a system header
31// to suppress these warnings.
32//
33#if defined(__GNUC__) && (__GNUC__ >= 4)
34#pragma GCC system_header
35#endif
36
37namespace ndnboost
38{
39
40 // Helper templates ------------------------------------------------------//
41
42 // fast integers from least integers
43 // int_fast_t<> works correctly for unsigned too, in spite of the name.
44 template< typename LeastInt >
45 struct int_fast_t
46 {
47 typedef LeastInt fast;
48 typedef fast type;
49 }; // imps may specialize
50
51 namespace detail{
52
53 // convert category to type
54 template< int Category > struct int_least_helper {}; // default is empty
55 template< int Category > struct uint_least_helper {}; // default is empty
56
57 // specializatons: 1=long, 2=int, 3=short, 4=signed char,
58 // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
59 // no specializations for 0 and 5: requests for a type > long are in error
60#ifdef BOOST_HAS_LONG_LONG
61 template<> struct int_least_helper<1> { typedef ndnboost::long_long_type least; };
62#elif defined(BOOST_HAS_MS_INT64)
63 template<> struct int_least_helper<1> { typedef __int64 least; };
64#endif
65 template<> struct int_least_helper<2> { typedef long least; };
66 template<> struct int_least_helper<3> { typedef int least; };
67 template<> struct int_least_helper<4> { typedef short least; };
68 template<> struct int_least_helper<5> { typedef signed char least; };
69#ifdef BOOST_HAS_LONG_LONG
70 template<> struct uint_least_helper<1> { typedef ndnboost::ulong_long_type least; };
71#elif defined(BOOST_HAS_MS_INT64)
72 template<> struct uint_least_helper<1> { typedef unsigned __int64 least; };
73#endif
74 template<> struct uint_least_helper<2> { typedef unsigned long least; };
75 template<> struct uint_least_helper<3> { typedef unsigned int least; };
76 template<> struct uint_least_helper<4> { typedef unsigned short least; };
77 template<> struct uint_least_helper<5> { typedef unsigned char least; };
78
79 template <int Bits>
80 struct exact_signed_base_helper{};
81 template <int Bits>
82 struct exact_unsigned_base_helper{};
83
84 template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
85 template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
86#if USHRT_MAX != UCHAR_MAX
87 template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
88 template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
89#endif
90#if UINT_MAX != USHRT_MAX
91 template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
92 template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
93#endif
94#if ULONG_MAX != UINT_MAX
95 template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
96 template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
97#endif
98#if defined(BOOST_HAS_LONG_LONG) &&\
99 ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
100 (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
101 (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
102 (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
103 template <> struct exact_signed_base_helper<sizeof(ndnboost::long_long_type)* CHAR_BIT> { typedef ndnboost::long_long_type exact; };
104 template <> struct exact_unsigned_base_helper<sizeof(ndnboost::ulong_long_type)* CHAR_BIT> { typedef ndnboost::ulong_long_type exact; };
105#endif
106
107
108 } // namespace detail
109
110 // integer templates specifying number of bits ---------------------------//
111
112 // signed
113 template< int Bits > // bits (including sign) required
114 struct int_t : public detail::exact_signed_base_helper<Bits>
115 {
116 BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(ndnboost::intmax_t) * CHAR_BIT),
117 "No suitable signed integer type with the requested number of bits is available.");
118 typedef typename detail::int_least_helper
119 <
120#ifdef BOOST_HAS_LONG_LONG
121 (Bits <= (int)(sizeof(ndnboost::long_long_type) * CHAR_BIT)) +
122#else
123 1 +
124#endif
125 (Bits-1 <= ::std::numeric_limits<long>::digits) +
126 (Bits-1 <= ::std::numeric_limits<int>::digits) +
127 (Bits-1 <= ::std::numeric_limits<short>::digits) +
128 (Bits-1 <= ::std::numeric_limits<signed char>::digits)
129 >::least least;
130 typedef typename int_fast_t<least>::type fast;
131 };
132
133 // unsigned
134 template< int Bits > // bits required
135 struct uint_t : public detail::exact_unsigned_base_helper<Bits>
136 {
137 BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(ndnboost::uintmax_t) * CHAR_BIT),
138 "No suitable unsigned integer type with the requested number of bits is available.");
139#if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
140 // It's really not clear why this workaround should be needed... shrug I guess! JM
141 BOOST_STATIC_CONSTANT(int, s =
142 6 +
143 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
144 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
145 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
146 (Bits <= ::std::numeric_limits<unsigned char>::digits));
147 typedef typename detail::int_least_helper< ::ndnboost::uint_t<Bits>::s>::least least;
148#else
149 typedef typename detail::uint_least_helper
150 <
151#ifdef BOOST_HAS_LONG_LONG
152 (Bits <= (int)(sizeof(ndnboost::long_long_type) * CHAR_BIT)) +
153#else
154 1 +
155#endif
156 (Bits <= ::std::numeric_limits<unsigned long>::digits) +
157 (Bits <= ::std::numeric_limits<unsigned int>::digits) +
158 (Bits <= ::std::numeric_limits<unsigned short>::digits) +
159 (Bits <= ::std::numeric_limits<unsigned char>::digits)
160 >::least least;
161#endif
162 typedef typename int_fast_t<least>::type fast;
163 // int_fast_t<> works correctly for unsigned too, in spite of the name.
164 };
165
166 // integer templates specifying extreme value ----------------------------//
167
168 // signed
169#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
170 template< ndnboost::long_long_type MaxValue > // maximum value to require support
171#else
172 template< long MaxValue > // maximum value to require support
173#endif
174 struct int_max_value_t
175 {
176 typedef typename detail::int_least_helper
177 <
178#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
179 (MaxValue <= ::ndnboost::integer_traits<ndnboost::long_long_type>::const_max) +
180#else
181 1 +
182#endif
183 (MaxValue <= ::ndnboost::integer_traits<long>::const_max) +
184 (MaxValue <= ::ndnboost::integer_traits<int>::const_max) +
185 (MaxValue <= ::ndnboost::integer_traits<short>::const_max) +
186 (MaxValue <= ::ndnboost::integer_traits<signed char>::const_max)
187 >::least least;
188 typedef typename int_fast_t<least>::type fast;
189 };
190
191#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
192 template< ndnboost::long_long_type MinValue > // minimum value to require support
193#else
194 template< long MinValue > // minimum value to require support
195#endif
196 struct int_min_value_t
197 {
198 typedef typename detail::int_least_helper
199 <
200#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
201 (MinValue >= ::ndnboost::integer_traits<ndnboost::long_long_type>::const_min) +
202#else
203 1 +
204#endif
205 (MinValue >= ::ndnboost::integer_traits<long>::const_min) +
206 (MinValue >= ::ndnboost::integer_traits<int>::const_min) +
207 (MinValue >= ::ndnboost::integer_traits<short>::const_min) +
208 (MinValue >= ::ndnboost::integer_traits<signed char>::const_min)
209 >::least least;
210 typedef typename int_fast_t<least>::type fast;
211 };
212
213 // unsigned
214#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
215 template< ndnboost::ulong_long_type MaxValue > // minimum value to require support
216#else
217 template< unsigned long MaxValue > // minimum value to require support
218#endif
219 struct uint_value_t
220 {
221#if (defined(__BORLANDC__) || defined(__CODEGEAR__))
222 // It's really not clear why this workaround should be needed... shrug I guess! JM
223#if defined(BOOST_NO_INTEGRAL_INT64_T)
224 BOOST_STATIC_CONSTANT(unsigned, which =
225 1 +
226 (MaxValue <= ::ndnboost::integer_traits<unsigned long>::const_max) +
227 (MaxValue <= ::ndnboost::integer_traits<unsigned int>::const_max) +
228 (MaxValue <= ::ndnboost::integer_traits<unsigned short>::const_max) +
229 (MaxValue <= ::ndnboost::integer_traits<unsigned char>::const_max));
230 typedef typename detail::int_least_helper< ::ndnboost::uint_value_t<MaxValue>::which>::least least;
231#else // BOOST_NO_INTEGRAL_INT64_T
232 BOOST_STATIC_CONSTANT(unsigned, which =
233 1 +
234 (MaxValue <= ::ndnboost::integer_traits<ndnboost::ulong_long_type>::const_max) +
235 (MaxValue <= ::ndnboost::integer_traits<unsigned long>::const_max) +
236 (MaxValue <= ::ndnboost::integer_traits<unsigned int>::const_max) +
237 (MaxValue <= ::ndnboost::integer_traits<unsigned short>::const_max) +
238 (MaxValue <= ::ndnboost::integer_traits<unsigned char>::const_max));
239 typedef typename detail::uint_least_helper< ::ndnboost::uint_value_t<MaxValue>::which>::least least;
240#endif // BOOST_NO_INTEGRAL_INT64_T
241#else
242 typedef typename detail::uint_least_helper
243 <
244#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
245 (MaxValue <= ::ndnboost::integer_traits<ndnboost::ulong_long_type>::const_max) +
246#else
247 1 +
248#endif
249 (MaxValue <= ::ndnboost::integer_traits<unsigned long>::const_max) +
250 (MaxValue <= ::ndnboost::integer_traits<unsigned int>::const_max) +
251 (MaxValue <= ::ndnboost::integer_traits<unsigned short>::const_max) +
252 (MaxValue <= ::ndnboost::integer_traits<unsigned char>::const_max)
253 >::least least;
254#endif
255 typedef typename int_fast_t<least>::type fast;
256 };
257
258
259} // namespace ndnboost
260
261#endif // BOOST_INTEGER_HPP