]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////// |
2 | // Copyright 2012 John Maddock. Distributed under the Boost | |
3 | // Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_ | |
5 | ||
6 | #ifndef BOOST_MP_CPP_INT_CORE_HPP | |
7 | #define BOOST_MP_CPP_INT_CORE_HPP | |
8 | ||
9 | #include <boost/integer.hpp> | |
10 | #include <boost/integer_traits.hpp> | |
11 | #include <boost/mpl/if.hpp> | |
12 | #include <boost/mpl/int.hpp> | |
13 | #include <boost/static_assert.hpp> | |
14 | #include <boost/assert.hpp> | |
15 | ||
16 | namespace boost{ namespace multiprecision{ | |
17 | ||
18 | namespace detail{ | |
19 | ||
20 | // | |
21 | // These traits calculate the largest type in the list | |
22 | // [unsigned] boost::long_long_type, long, int, which has the specified number | |
23 | // of bits. Note that intN_t and boost::int_t<N> find the first | |
24 | // member of the above list, not the last. We want the last in the | |
25 | // list to ensure that mixed arithmetic operations are as efficient | |
26 | // as possible. | |
27 | // | |
28 | template <unsigned N> | |
29 | struct largest_signed_type | |
30 | { | |
31 | typedef typename mpl::if_c< | |
32 | 1 + std::numeric_limits<boost::long_long_type>::digits == N, | |
33 | boost::long_long_type, | |
34 | typename mpl::if_c< | |
35 | 1 + std::numeric_limits<long>::digits == N, | |
36 | long, | |
37 | typename mpl::if_c< | |
38 | 1 + std::numeric_limits<int>::digits == N, | |
39 | int, | |
40 | typename boost::int_t<N>::exact | |
41 | >::type | |
42 | >::type | |
43 | >::type type; | |
44 | }; | |
45 | ||
46 | template <unsigned N> | |
47 | struct largest_unsigned_type | |
48 | { | |
49 | typedef typename mpl::if_c< | |
50 | std::numeric_limits<boost::ulong_long_type>::digits == N, | |
51 | boost::ulong_long_type, | |
52 | typename mpl::if_c< | |
53 | std::numeric_limits<unsigned long>::digits == N, | |
54 | unsigned long, | |
55 | typename mpl::if_c< | |
56 | std::numeric_limits<unsigned int>::digits == N, | |
57 | unsigned int, | |
58 | typename boost::uint_t<N>::exact | |
59 | >::type | |
60 | >::type | |
61 | >::type type; | |
62 | }; | |
63 | ||
64 | } // namespace detail | |
65 | ||
66 | #if defined(BOOST_HAS_INT128) | |
67 | ||
68 | typedef detail::largest_unsigned_type<64>::type limb_type; | |
69 | typedef detail::largest_signed_type<64>::type signed_limb_type; | |
70 | typedef boost::uint128_type double_limb_type; | |
71 | typedef boost::int128_type signed_double_limb_type; | |
72 | static const limb_type max_block_10 = 1000000000000000000uLL; | |
73 | static const limb_type digits_per_block_10 = 18; | |
74 | ||
75 | inline limb_type block_multiplier(unsigned count) | |
76 | { | |
77 | static const limb_type values[digits_per_block_10] | |
78 | = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000 }; | |
79 | BOOST_ASSERT(count < digits_per_block_10); | |
80 | return values[count]; | |
81 | } | |
82 | ||
83 | // Can't do formatted IO on an __int128 | |
84 | #define BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO | |
85 | ||
86 | // Need to specialise integer_traits for __int128 as it's not a normal native type: | |
87 | } // namespace multiprecision | |
88 | ||
89 | template<> | |
90 | class integer_traits<multiprecision::double_limb_type> | |
91 | : public std::numeric_limits<multiprecision::double_limb_type>, | |
92 | public detail::integer_traits_base<multiprecision::double_limb_type, 0, ~static_cast<multiprecision::double_limb_type>(0)> | |
93 | { }; | |
94 | template<> | |
95 | class integer_traits<multiprecision::signed_double_limb_type> | |
96 | : public std::numeric_limits<multiprecision::signed_double_limb_type>, | |
97 | public detail::integer_traits_base<multiprecision::signed_double_limb_type, static_cast<multiprecision::signed_double_limb_type>((static_cast<multiprecision::double_limb_type>(1) << 127)), static_cast<multiprecision::signed_double_limb_type>(((~static_cast<multiprecision::double_limb_type>(0)) >> 1))> | |
98 | { }; | |
99 | ||
100 | namespace multiprecision{ | |
101 | ||
102 | #else | |
103 | ||
104 | typedef detail::largest_unsigned_type<32>::type limb_type; | |
105 | typedef detail::largest_signed_type<32>::type signed_limb_type; | |
106 | typedef detail::largest_unsigned_type<64>::type double_limb_type; | |
107 | typedef detail::largest_signed_type<64>::type signed_double_limb_type; | |
108 | static const limb_type max_block_10 = 1000000000; | |
109 | static const limb_type digits_per_block_10 = 9; | |
110 | ||
111 | inline limb_type block_multiplier(unsigned count) | |
112 | { | |
113 | static const limb_type values[digits_per_block_10] | |
114 | = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; | |
115 | BOOST_ASSERT(count < digits_per_block_10); | |
116 | return values[count]; | |
117 | } | |
118 | ||
119 | #endif | |
120 | ||
121 | static const unsigned bits_per_limb = sizeof(limb_type) * CHAR_BIT; | |
122 | ||
123 | template <class T> | |
124 | inline void minmax(const T& a, const T& b, T& aa, T& bb) | |
125 | { | |
126 | if(a < b) | |
127 | { | |
128 | aa = a; | |
129 | bb = b; | |
130 | } | |
131 | else | |
132 | { | |
133 | aa = b; | |
134 | bb = a; | |
135 | } | |
136 | } | |
137 | ||
138 | enum cpp_integer_type | |
139 | { | |
140 | signed_magnitude = 1, | |
141 | unsigned_magnitude = 0, | |
142 | signed_packed = 3, | |
143 | unsigned_packed = 2 | |
144 | }; | |
145 | ||
146 | enum cpp_int_check_type | |
147 | { | |
148 | checked = 1, | |
149 | unchecked = 0 | |
150 | }; | |
151 | ||
152 | }} | |
153 | ||
154 | // | |
155 | // Figure out whether to support user-defined-literals or not: | |
156 | // | |
157 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_USER_DEFINED_LITERALS) \ | |
158 | && !defined(BOOST_NO_CXX11_CONSTEXPR) | |
159 | # define BOOST_MP_USER_DEFINED_LITERALS | |
160 | #endif | |
161 | ||
162 | #endif // BOOST_MP_CPP_INT_CORE_HPP | |
163 |