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