1 //////////////////3/////////////////////////////////////////////
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 https://www.boost.org/LICENSE_1_0.txt
5
6 #ifndef BOOST_MP_CPP_INT_HPP
7 #define BOOST_MP_CPP_INT_HPP
8
9 #include <iostream>
10 #include <iomanip>
11 #include <type_traits>
12 #include <cstdint>
13 #include <boost/multiprecision/detail/endian.hpp>
14 #include <boost/multiprecision/number.hpp>
15 #include <boost/multiprecision/detail/integer_ops.hpp>
16 #include <boost/multiprecision/detail/rebind.hpp>
17 #include <boost/core/empty_value.hpp>
18 #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
19 #include <boost/multiprecision/rational_adaptor.hpp>
20 #include <boost/multiprecision/traits/is_byte_container.hpp>
21 #include <boost/integer/static_min_max.hpp>
22 #include <boost/multiprecision/cpp_int/checked.hpp>
23 #include <boost/multiprecision/detail/constexpr.hpp>
24 #include <boost/multiprecision/cpp_int/value_pack.hpp>
25
26 namespace boost {
27 namespace multiprecision {
28 namespace backends {
29
30 #ifdef BOOST_MSVC
31 #pragma warning(push)
32 #pragma warning(disable : 4307) // integral constant overflow (oveflow is in a branch not taken when it would overflow)
33 #pragma warning(disable : 4127) // conditional expression is constant
34 #pragma warning(disable : 4702) // Unreachable code (reachability depends on template params)
35 #endif
36
37 template <unsigned MinBits = 0, unsigned MaxBits = 0, boost::multiprecision::cpp_integer_type SignType = signed_magnitude, cpp_int_check_type Checked = unchecked, class Allocator = typename std::conditional<MinBits && (MinBits == MaxBits), void, std::allocator<limb_type> >::type>
38 struct cpp_int_backend;
39
40 } // namespace backends
41
42 namespace detail {
43
44 template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
45 struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::false_type
46 {};
47
48 } // namespace detail
49
50 namespace backends {
51
52 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
53 struct cpp_int_base;
54 //
55 // Traits class determines the maximum and minimum precision values:
56 //
57 template <class T>
58 struct max_precision;
59
60 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
61 struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
62 {
63 static constexpr const unsigned value = std::is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value
64 : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : UINT_MAX);
65 };
66
67 template <class T>
68 struct min_precision;
69
70 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
71 struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
72 {
73 static constexpr const unsigned value = (std::is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value : MinBits);
74 };
75 //
76 // Traits class determines whether the number of bits precision requested could fit in a native type,
77 // we call this a "trivial" cpp_int:
78 //
79 template <class T>
80 struct is_trivial_cpp_int
81 {
82 static constexpr const bool value = false;
83 };
84
85 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
86 struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
87 {
88 using self = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
89 static constexpr const bool value = std::is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
90 };
91
92 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
93 struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
94 {
95 static constexpr const bool value = true;
96 };
97
98 } // namespace backends
99 //
100 // Traits class to determine whether a cpp_int_backend is signed or not:
101 //
102 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
103 struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
104 : public std::integral_constant<bool, (SignType == unsigned_magnitude) || (SignType == unsigned_packed)>
105 {};
106
107 namespace backends {
108 //
109 // Traits class determines whether T should be implicitly convertible to U, or
110 // whether the constructor should be made explicit. The latter happens if we
111 // are losing the sign, or have fewer digits precision in the target type:
112 //
113 template <class T, class U>
114 struct is_implicit_cpp_int_conversion;
115
116 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
117 struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
118 {
119 using t1 = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> ;
120 using t2 = cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>;
121 static constexpr const bool value =
122 (is_signed_number<t2>::value || !is_signed_number<t1>::value) && (max_precision<t1>::value <= max_precision<t2>::value);
123 };
124
125 //
126 // Traits class to determine whether operations on a cpp_int may throw:
127 //
128 template <class T>
129 struct is_non_throwing_cpp_int : public std::integral_constant<bool, false>
130 {};
131 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType>
132 struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public std::integral_constant<bool, true>
133 {};
134
135 //
136 // Traits class, determines whether the cpp_int is fixed precision or not:
137 //
138 template <class T>
139 struct is_fixed_precision;
140 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
141 struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
142 : public std::integral_constant<bool, max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != UINT_MAX>
143 {};
144
145 namespace detail {
146
verify_new_size(unsigned new_size,unsigned min_size,const std::integral_constant<int,checked> &)147 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(unsigned new_size, unsigned min_size, const std::integral_constant<int, checked>&)
148 {
149 if (new_size < min_size)
150 BOOST_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
151 }
verify_new_size(unsigned,unsigned,const std::integral_constant<int,unchecked> &)152 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(unsigned /*new_size*/, unsigned /*min_size*/, const std::integral_constant<int, unchecked>&) {}
153
154 template <class U>
verify_limb_mask(bool b,U limb,U mask,const std::integral_constant<int,checked> &)155 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool b, U limb, U mask, const std::integral_constant<int, checked>&)
156 {
157 // When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error:
158 if (b && (limb & ~mask))
159 BOOST_THROW_EXCEPTION(std::overflow_error("Overflow in cpp_int arithmetic: there is insufficient precision in the target type to hold all of the bits of the result."));
160 }
161 template <class U>
verify_limb_mask(bool,U,U,const std::integral_constant<int,unchecked> &)162 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const std::integral_constant<int, unchecked>&) {}
163
164 } // namespace detail
165
166 //
167 // Now define the various data layouts that are possible as partial specializations of the base class,
168 // starting with the default arbitrary precision signed integer type:
169 //
170 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
171 struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>
172 : private boost::empty_value<typename detail::rebind<limb_type, Allocator>::type>
173 {
174 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, bool trivial2>
175 friend struct cpp_int_base;
176
177 using allocator_type = typename detail::rebind<limb_type, Allocator>::type;
178 using limb_pointer = typename std::allocator_traits<allocator_type>::pointer ;
179 using const_limb_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
180 using checked_type = std::integral_constant<int, Checked>;
181
182 //
183 // Interface invariants:
184 //
185 static_assert(!std::is_void<Allocator>::value, "Allocator must not be void here");
186
187 using base_type = boost::empty_value<allocator_type>;
188
189 private:
190 struct limb_data
191 {
192 unsigned capacity;
193 limb_pointer data;
194 };
195
196 public:
197 static constexpr unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
198 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
199 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
200 static constexpr unsigned internal_limb_count =
201 MinBits
202 ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
203 : (sizeof(limb_data) / sizeof(limb_type)) > 1 ? (sizeof(limb_data) / sizeof(limb_type)) : 2;
204 private:
205 union data_type
206 {
207 limb_data ld;
208 limb_type la[internal_limb_count];
209 limb_type first;
210 double_limb_type double_first;
211
data_type()212 constexpr data_type() noexcept : first(0) {}
data_type(limb_type i)213 constexpr data_type(limb_type i) noexcept : first(i) {}
data_type(signed_limb_type i)214 constexpr data_type(signed_limb_type i) noexcept : first(i < 0 ? static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
215 #if BOOST_MP_ENDIAN_LITTLE_BYTE
data_type(double_limb_type i)216 constexpr data_type(double_limb_type i) noexcept : double_first(i)
217 {}
data_type(signed_double_limb_type i)218 constexpr data_type(signed_double_limb_type i) noexcept : double_first(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
219 #endif
220 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !(defined(BOOST_MSVC) && (BOOST_MSVC < 1900))
data_type(limb_type * limbs,unsigned len)221 constexpr data_type(limb_type* limbs, unsigned len) noexcept : ld{ len, limbs }
222 {}
223 #else
data_type(limb_type * limbs,unsigned len)224 constexpr data_type(limb_type* limbs, unsigned len) noexcept
225 {
226 ld.capacity = len;
227 ld.data = limbs;
228 }
229 #endif
230 };
231
232 data_type m_data;
233 unsigned m_limbs;
234 bool m_sign, m_internal, m_alias;
235
236 public:
237 //
238 // Direct construction:
239 //
cpp_int_baseboost::multiprecision::backends::cpp_int_base240 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
241 : m_data(i),
242 m_limbs(1),
243 m_sign(false),
244 m_internal(true),
245 m_alias(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base246 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
247 : m_data(i),
248 m_limbs(1),
249 m_sign(i < 0),
250 m_internal(true),
251 m_alias(false) {}
252 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base253 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
254 : m_data(i),
255 m_limbs(i > max_limb_value ? 2 : 1),
256 m_sign(false),
257 m_internal(true),
258 m_alias(false)
259 {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base260 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
261 : m_data(i),
262 m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > static_cast<double_limb_type>(max_limb_value) ? 2 : 1) : (i > max_limb_value ? 2 : 1)),
263 m_sign(i < 0),
264 m_internal(true),
265 m_alias(false) {}
266 #endif
267 //
268 // Aliasing constructor aliases data:
269 //
270 struct scoped_shared_storage : private boost::empty_value<allocator_type>
271 {
272 private:
273 limb_type* data;
274 unsigned capacity;
275 unsigned allocated;
276 bool is_alias;
allocatorboost::multiprecision::backends::cpp_int_base::scoped_shared_storage277 allocator_type& allocator() noexcept { return boost::empty_value<allocator_type>::get(); }
278
279 public:
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage280 scoped_shared_storage(const allocator_type& a, unsigned len)
281 : boost::empty_value<allocator_type>(boost::empty_init_t(), a), capacity(len), allocated(0), is_alias(false)
282 {
283 data = allocator().allocate(len);
284 }
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage285 scoped_shared_storage(const cpp_int_base& i, unsigned len)
286 : boost::empty_value<allocator_type>(boost::empty_init_t(), i.allocator()), capacity(len), allocated(0), is_alias(false)
287 {
288 data = allocator().allocate(len);
289 }
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage290 scoped_shared_storage(limb_type* limbs, unsigned n) : data(limbs), capacity(n), allocated(0), is_alias(true) {}
~scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage291 ~scoped_shared_storage()
292 {
293 if(!is_alias)
294 allocator().deallocate(data, capacity);
295 }
allocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage296 limb_type* allocate(unsigned n) noexcept
297 {
298 limb_type* result = data + allocated;
299 allocated += n;
300 BOOST_ASSERT(allocated <= capacity);
301 return result;
302 }
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage303 void deallocate(unsigned n)
304 {
305 BOOST_ASSERT(n <= allocated);
306 allocated -= n;
307 }
308 };
cpp_int_baseboost::multiprecision::backends::cpp_int_base309 explicit constexpr cpp_int_base(limb_type* data, unsigned offset, unsigned len) noexcept
310 : m_data(data + offset, len),
311 m_limbs(len),
312 m_sign(false),
313 m_internal(false),
314 m_alias(true) {}
315 // This next constructor is for constructing const objects from const limb_type*'s only.
316 // Unfortunately we appear to have no way to assert that within the language, and the const_cast
317 // is a side effect of that :(
cpp_int_baseboost::multiprecision::backends::cpp_int_base318 explicit constexpr cpp_int_base(const limb_type* data, unsigned offset, unsigned len) noexcept
319 : m_data(const_cast<limb_type*>(data) + offset, len),
320 m_limbs(len),
321 m_sign(false),
322 m_internal(false),
323 m_alias(true) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base324 explicit cpp_int_base(scoped_shared_storage& data, unsigned len) noexcept
325 : m_data(data.allocate(len), len),
326 m_limbs(len),
327 m_sign(false),
328 m_internal(false),
329 m_alias(true) {}
330 //
331 // Helper functions for getting at our internal data, and manipulating storage:
332 //
allocatorboost::multiprecision::backends::cpp_int_base333 BOOST_MP_FORCEINLINE allocator_type& allocator() noexcept { return base_type::get(); }
allocatorboost::multiprecision::backends::cpp_int_base334 BOOST_MP_FORCEINLINE const allocator_type& allocator() const noexcept { return base_type::get(); }
sizeboost::multiprecision::backends::cpp_int_base335 BOOST_MP_FORCEINLINE unsigned size() const noexcept { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base336 BOOST_MP_FORCEINLINE limb_pointer limbs() noexcept { return m_internal ? m_data.la : m_data.ld.data; }
limbsboost::multiprecision::backends::cpp_int_base337 BOOST_MP_FORCEINLINE const_limb_pointer limbs() const noexcept { return m_internal ? m_data.la : m_data.ld.data; }
capacityboost::multiprecision::backends::cpp_int_base338 BOOST_MP_FORCEINLINE unsigned capacity() const noexcept { return m_internal ? internal_limb_count : m_data.ld.capacity; }
signboost::multiprecision::backends::cpp_int_base339 BOOST_MP_FORCEINLINE bool sign() const noexcept { return m_sign; }
signboost::multiprecision::backends::cpp_int_base340 void sign(bool b) noexcept
341 {
342 m_sign = b;
343 // Check for zero value:
344 if (m_sign && (m_limbs == 1))
345 {
346 if (limbs()[0] == 0)
347 m_sign = false;
348 }
349 }
resizeboost::multiprecision::backends::cpp_int_base350 void resize(unsigned new_size, unsigned min_size)
351 {
352 constexpr const unsigned max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
353 // We never resize beyond MaxSize:
354 if (new_size > max_limbs)
355 new_size = max_limbs;
356 detail::verify_new_size(new_size, min_size, checked_type());
357 // See if we have enough capacity already:
358 unsigned cap = capacity();
359 if (new_size > cap)
360 {
361 // We must not be an alias, memory allocation here defeats the whole point of aliasing:
362 BOOST_ASSERT(!m_alias);
363 // Allocate a new buffer and copy everything over:
364 cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
365 limb_pointer pl = allocator().allocate(cap);
366 std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
367 if (!m_internal && !m_alias)
368 allocator().deallocate(limbs(), capacity());
369 else
370 m_internal = false;
371 m_limbs = new_size;
372 m_data.ld.capacity = cap;
373 m_data.ld.data = pl;
374 }
375 else
376 {
377 m_limbs = new_size;
378 }
379 }
normalizeboost::multiprecision::backends::cpp_int_base380 BOOST_MP_FORCEINLINE void normalize() noexcept
381 {
382 limb_pointer p = limbs();
383 while ((m_limbs - 1) && !p[m_limbs - 1])
384 --m_limbs;
385 }
cpp_int_baseboost::multiprecision::backends::cpp_int_base386 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(), m_limbs(1), m_sign(false), m_internal(true), m_alias(false){}
cpp_int_baseboost::multiprecision::backends::cpp_int_base387 BOOST_MP_FORCEINLINE cpp_int_base(const cpp_int_base& o) : base_type(o), m_limbs(o.m_alias ? o.m_limbs : 0), m_sign(o.m_sign), m_internal(o.m_alias ? false : true), m_alias(o.m_alias)
388 {
389 if (m_alias)
390 {
391 m_data.ld = o.m_data.ld;
392 }
393 else
394 {
395 resize(o.size(), o.size());
396 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
397 }
398 }
399 // rvalue copy:
cpp_int_baseboost::multiprecision::backends::cpp_int_base400 cpp_int_base(cpp_int_base&& o)
401 : base_type(static_cast<base_type&&>(o)), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal), m_alias(o.m_alias)
402 {
403 if (m_internal)
404 {
405 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
406 }
407 else
408 {
409 m_data.ld = o.m_data.ld;
410 o.m_limbs = 0;
411 o.m_internal = true;
412 }
413 }
operator =boost::multiprecision::backends::cpp_int_base414 cpp_int_base& operator=(cpp_int_base&& o) noexcept
415 {
416 if (!m_internal && !m_alias)
417 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
418 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
419 m_limbs = o.m_limbs;
420 m_sign = o.m_sign;
421 m_internal = o.m_internal;
422 m_alias = o.m_alias;
423 if (m_internal)
424 {
425 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
426 }
427 else
428 {
429 m_data.ld = o.m_data.ld;
430 o.m_limbs = 0;
431 o.m_internal = true;
432 }
433 return *this;
434 }
435 template <unsigned MinBits2, unsigned MaxBits2, cpp_int_check_type Checked2>
operator =boost::multiprecision::backends::cpp_int_base436 cpp_int_base& operator=(cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>&& o) noexcept
437 {
438 if(o.m_internal)
439 {
440 m_sign = o.m_sign;
441 this->resize(o.size(), o.size());
442 std::memcpy(this->limbs(), o.limbs(), o.size() * sizeof(*(o.limbs())));
443 return *this;
444 }
445 if (!m_internal && !m_alias)
446 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
447 *static_cast<base_type*>(this) = static_cast<typename cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>::base_type&&>(o);
448 m_limbs = o.m_limbs;
449 m_sign = o.m_sign;
450 m_internal = o.m_internal;
451 m_alias = o.m_alias;
452 m_data.ld.capacity = o.m_data.ld.capacity;
453 m_data.ld.data = o.limbs();
454 o.m_limbs = 0;
455 o.m_internal = true;
456 return *this;
457 }
~cpp_int_baseboost::multiprecision::backends::cpp_int_base458 BOOST_MP_FORCEINLINE ~cpp_int_base() noexcept
459 {
460 if (!m_internal && !m_alias)
461 allocator().deallocate(limbs(), capacity());
462 }
assignboost::multiprecision::backends::cpp_int_base463 void assign(const cpp_int_base& o)
464 {
465 if (this != &o)
466 {
467 static_cast<base_type&>(*this) = static_cast<const base_type&>(o);
468 m_limbs = 0;
469 resize(o.size(), o.size());
470 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
471 m_sign = o.m_sign;
472 }
473 }
negateboost::multiprecision::backends::cpp_int_base474 BOOST_MP_FORCEINLINE void negate() noexcept
475 {
476 m_sign = !m_sign;
477 // Check for zero value:
478 if (m_sign && (m_limbs == 1))
479 {
480 if (limbs()[0] == 0)
481 m_sign = false;
482 }
483 }
isnegboost::multiprecision::backends::cpp_int_base484 BOOST_MP_FORCEINLINE bool isneg() const noexcept
485 {
486 return m_sign;
487 }
do_swapboost::multiprecision::backends::cpp_int_base488 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) noexcept
489 {
490 std::swap(m_data, o.m_data);
491 std::swap(m_sign, o.m_sign);
492 std::swap(m_internal, o.m_internal);
493 std::swap(m_limbs, o.m_limbs);
494 std::swap(m_alias, o.m_alias);
495 }
496
497 protected:
498 template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base499 void check_in_range(const A&) noexcept {}
500 };
501
502 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
503 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
504 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
505 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
506 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
507 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
508 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
509 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
510
511 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
512 struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false>
513 : private boost::empty_value<typename detail::rebind<limb_type, Allocator>::type>
514 {
515 //
516 // There is currently no support for unsigned arbitrary precision arithmetic, largely
517 // because it's not clear what subtraction should do:
518 //
519 static_assert(((sizeof(Allocator) == 0) && !std::is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
520 };
521 //
522 // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
523 //
524 template <unsigned MinBits, cpp_int_check_type Checked>
525 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
526 {
527 using limb_pointer = limb_type* ;
528 using const_limb_pointer = const limb_type* ;
529 using checked_type = std::integral_constant<int, Checked>;
530
531 struct scoped_shared_storage
532 {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage533 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage534 BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
535 };
536
537 //
538 // Interface invariants:
539 //
540 static_assert(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
541
542 public:
543 static constexpr unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
544 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
545 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
546 static constexpr unsigned internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
547 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
548 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
549
550 private:
551 union data_type
552 {
553 limb_type m_data[internal_limb_count];
554 limb_type m_first_limb;
555 double_limb_type m_double_first_limb;
556
data_type()557 constexpr data_type()
558 : m_data{0}
559 {}
data_type(limb_type i)560 constexpr data_type(limb_type i)
561 : m_data{i}
562 {}
563 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
data_type(limb_type i,limb_type j)564 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
565 {}
566 #endif
data_type(double_limb_type i)567 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
568 {
569 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
570 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
571 {
572 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
573 *this = t;
574 }
575 #endif
576 }
577 template <limb_type... VALUES>
data_type(literals::detail::value_pack<VALUES...>)578 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
579 {}
580 } m_wrapper;
581 std::uint16_t m_limbs;
582 bool m_sign;
583
584 public:
585 //
586 // Direct construction:
587 //
cpp_int_baseboost::multiprecision::backends::cpp_int_base588 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
589 : m_wrapper(i),
590 m_limbs(1),
591 m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base592 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
593 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)),
594 m_limbs(1),
595 m_sign(i < 0) {}
596 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base597 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
598 : m_wrapper(i),
599 m_limbs(i > max_limb_value ? 2 : 1),
600 m_sign(false)
601 {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base602 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
603 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
604 m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1)),
605 m_sign(i < 0) {}
606 #endif
607 template <limb_type... VALUES>
cpp_int_baseboost::multiprecision::backends::cpp_int_base608 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
609 : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false)
610 {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base611 constexpr cpp_int_base(literals::detail::value_pack<> i)
612 : m_wrapper(i), m_limbs(1), m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base613 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
614 : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base615 explicit constexpr cpp_int_base(scoped_shared_storage&, unsigned) noexcept : m_wrapper(), m_limbs(0), m_sign(false)
616 {}
617 //
618 // These are deprecated in C++20 unless we make them explicit:
619 //
620 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
621 //
622 // Helper functions for getting at our internal data, and manipulating storage:
623 //
sizeboost::multiprecision::backends::cpp_int_base624 BOOST_MP_FORCEINLINE constexpr unsigned size() const noexcept { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base625 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
limbsboost::multiprecision::backends::cpp_int_base626 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
signboost::multiprecision::backends::cpp_int_base627 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return m_sign; }
signboost::multiprecision::backends::cpp_int_base628 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
629 {
630 m_sign = b;
631 // Check for zero value:
632 if (m_sign && (m_limbs == 1))
633 {
634 if (limbs()[0] == 0)
635 m_sign = false;
636 }
637 }
resizeboost::multiprecision::backends::cpp_int_base638 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned new_size, unsigned min_size) noexcept((Checked == unchecked))
639 {
640 m_limbs = static_cast<std::uint16_t>((std::min)(new_size, internal_limb_count));
641 detail::verify_new_size(m_limbs, min_size, checked_type());
642 }
normalizeboost::multiprecision::backends::cpp_int_base643 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
644 {
645 limb_pointer p = limbs();
646 detail::verify_limb_mask(m_limbs == internal_limb_count, p[m_limbs - 1], upper_limb_mask, checked_type());
647 p[internal_limb_count - 1] &= upper_limb_mask;
648 while ((m_limbs - 1) && !p[m_limbs - 1])
649 --m_limbs;
650 if ((m_limbs == 1) && (!*p))
651 m_sign = false; // zero is always unsigned
652 }
653
cpp_int_baseboost::multiprecision::backends::cpp_int_base654 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
655 // Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
cpp_int_baseboost::multiprecision::backends::cpp_int_base656 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
657 : m_wrapper(o.m_wrapper),
658 m_limbs(o.m_limbs),
659 m_sign(o.m_sign) {}
660 // Defaulted functions:
661 //~cpp_int_base() noexcept {}
662
assignboost::multiprecision::backends::cpp_int_base663 void BOOST_MP_CXX14_CONSTEXPR assign(const cpp_int_base& o) noexcept
664 {
665 if (this != &o)
666 {
667 m_limbs = o.m_limbs;
668 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
669 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
670 {
671 for (unsigned i = 0; i < m_limbs; ++i)
672 limbs()[i] = o.limbs()[i];
673 }
674 else
675 #endif
676 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
677 m_sign = o.m_sign;
678 }
679 }
negateboost::multiprecision::backends::cpp_int_base680 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
681 {
682 m_sign = !m_sign;
683 // Check for zero value:
684 if (m_sign && (m_limbs == 1))
685 {
686 if (limbs()[0] == 0)
687 m_sign = false;
688 }
689 }
isnegboost::multiprecision::backends::cpp_int_base690 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
691 {
692 return m_sign;
693 }
do_swapboost::multiprecision::backends::cpp_int_base694 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
695 {
696 for (unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
697 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
698 std_constexpr::swap(m_sign, o.m_sign);
699 std_constexpr::swap(m_limbs, o.m_limbs);
700 }
701
702 protected:
703 template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base704 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
705 };
706
707 template <unsigned MinBits, cpp_int_check_type Checked>
708 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
709 template <unsigned MinBits, cpp_int_check_type Checked>
710 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
711 template <unsigned MinBits, cpp_int_check_type Checked>
712 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
713 template <unsigned MinBits, cpp_int_check_type Checked>
714 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
715 //
716 // Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
717 //
718 template <unsigned MinBits, cpp_int_check_type Checked>
719 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
720 {
721 using limb_pointer = limb_type* ;
722 using const_limb_pointer = const limb_type* ;
723 using checked_type = std::integral_constant<int, Checked>;
724
725 struct scoped_shared_storage
726 {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage727 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage728 BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
729 };
730 //
731 // Interface invariants:
732 //
733 static_assert(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
734
735 public:
736 static constexpr unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
737 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
738 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
739 static constexpr unsigned internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
740 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
741 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
742
743 private:
744 union data_type
745 {
746 limb_type m_data[internal_limb_count];
747 limb_type m_first_limb;
748 double_limb_type m_double_first_limb;
749
data_type()750 constexpr data_type()
751 : m_data{0}
752 {}
data_type(limb_type i)753 constexpr data_type(limb_type i)
754 : m_data{i}
755 {}
756 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
data_type(limb_type i,limb_type j)757 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
758 {}
759 #endif
data_type(double_limb_type i)760 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
761 {
762 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
763 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
764 {
765 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
766 *this = t;
767 }
768 #endif
769 }
770 template <limb_type... VALUES>
data_type(literals::detail::value_pack<VALUES...>)771 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
772 {}
773 } m_wrapper;
774 limb_type m_limbs;
775
776 public:
777 //
778 // Direct construction:
779 //
cpp_int_baseboost::multiprecision::backends::cpp_int_base780 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
781 : m_wrapper(i),
782 m_limbs(1) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base783 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_limb_type i) noexcept((Checked == unchecked))
784 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)), m_limbs(1)
785 {
786 if (i < 0)
787 negate();
788 }
789 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base790 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
791 : m_wrapper(i),
792 m_limbs(i > max_limb_value ? 2 : 1)
793 {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base794 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_double_limb_type i) noexcept((Checked == unchecked))
795 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
796 m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1))
797 {
798 if (i < 0)
799 negate();
800 }
801 #endif
802 template <limb_type... VALUES>
cpp_int_baseboost::multiprecision::backends::cpp_int_base803 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
804 : m_wrapper(i), m_limbs(sizeof...(VALUES))
805 {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base806 constexpr cpp_int_base(literals::detail::value_pack<>)
807 : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base808 explicit constexpr cpp_int_base(scoped_shared_storage&, unsigned) noexcept : m_wrapper(), m_limbs(1)
809 {}
810 //
811 // Helper functions for getting at our internal data, and manipulating storage:
812 //
sizeboost::multiprecision::backends::cpp_int_base813 BOOST_MP_FORCEINLINE constexpr unsigned size() const noexcept { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base814 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
limbsboost::multiprecision::backends::cpp_int_base815 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
signboost::multiprecision::backends::cpp_int_base816 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
signboost::multiprecision::backends::cpp_int_base817 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
818 {
819 if (b)
820 negate();
821 }
resizeboost::multiprecision::backends::cpp_int_base822 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned new_size, unsigned min_size) noexcept((Checked == unchecked))
823 {
824 m_limbs = (std::min)(new_size, internal_limb_count);
825 detail::verify_new_size(m_limbs, min_size, checked_type());
826 }
normalizeboost::multiprecision::backends::cpp_int_base827 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
828 {
829 limb_pointer p = limbs();
830 detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
831 p[internal_limb_count - 1] &= upper_limb_mask;
832 while ((m_limbs - 1) && !p[m_limbs - 1])
833 --m_limbs;
834 }
835
cpp_int_baseboost::multiprecision::backends::cpp_int_base836 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept
837 : m_wrapper(limb_type(0u)),
838 m_limbs(1) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base839 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
840 : m_wrapper(o.m_wrapper),
841 m_limbs(o.m_limbs) {}
842 // Defaulted functions:
843 //~cpp_int_base() noexcept {}
844 //
845 // These are deprecated in C++20 unless we make them explicit:
846 //
847 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
848
assignboost::multiprecision::backends::cpp_int_base849 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
850 {
851 if (this != &o)
852 {
853 m_limbs = o.m_limbs;
854 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
855 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
856 {
857 for (unsigned i = 0; i < m_limbs; ++i)
858 limbs()[i] = o.limbs()[i];
859 }
860 else
861 #endif
862 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
863 }
864 }
865
866 private:
check_negateboost::multiprecision::backends::cpp_int_base867 void check_negate(const std::integral_constant<int, checked>&)
868 {
869 BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
870 }
check_negateboost::multiprecision::backends::cpp_int_base871 BOOST_MP_CXX14_CONSTEXPR void check_negate(const std::integral_constant<int, unchecked>&) {}
872
873 public:
negateboost::multiprecision::backends::cpp_int_base874 BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
875 {
876 // Not so much a negate as a complement - this gets called when subtraction
877 // would result in a "negative" number:
878 if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
879 return; // negating zero is always zero, and always OK.
880 check_negate(checked_type());
881 unsigned i = m_limbs;
882 for (; i < internal_limb_count; ++i)
883 m_wrapper.m_data[i] = 0;
884 m_limbs = internal_limb_count;
885 for (i = 0; i < internal_limb_count; ++i)
886 m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
887 normalize();
888 eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
889 }
isnegboost::multiprecision::backends::cpp_int_base890 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
891 {
892 return false;
893 }
do_swapboost::multiprecision::backends::cpp_int_base894 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
895 {
896 for (unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
897 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
898 std_constexpr::swap(m_limbs, o.m_limbs);
899 }
900
901 protected:
902 template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base903 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
904 };
905
906 template <unsigned MinBits, cpp_int_check_type Checked>
907 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
908 template <unsigned MinBits, cpp_int_check_type Checked>
909 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
910 template <unsigned MinBits, cpp_int_check_type Checked>
911 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
912 template <unsigned MinBits, cpp_int_check_type Checked>
913 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
914 //
915 // Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
916 // because some platforms have native integer types longer than boost::long_long_type, "really boost::long_long_type" anyone??
917 //
918 template <unsigned N, bool s>
919 struct trivial_limb_type_imp
920 {
921 using type = double_limb_type;
922 };
923
924 template <unsigned N>
925 struct trivial_limb_type_imp<N, true>
926 {
927 using type = typename boost::uint_t<N>::least;
928 };
929
930 template <unsigned N>
931 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(boost::long_long_type) * CHAR_BIT>
932 {};
933 //
934 // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
935 //
936 template <unsigned MinBits, cpp_int_check_type Checked>
937 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
938 {
939 using local_limb_type = typename trivial_limb_type<MinBits>::type;
940 using limb_pointer = local_limb_type* ;
941 using const_limb_pointer = const local_limb_type* ;
942 using checked_type = std::integral_constant<int, Checked> ;
943
944 struct scoped_shared_storage
945 {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage946 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage947 BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
948 };
949
950 protected:
951 static constexpr unsigned limb_bits = sizeof(local_limb_type) * CHAR_BIT;
952 static constexpr local_limb_type limb_mask = (MinBits < limb_bits) ? local_limb_type((local_limb_type(~local_limb_type(0))) >> (limb_bits - MinBits)) : local_limb_type(~local_limb_type(0));
953
954 private:
955 local_limb_type m_data;
956 bool m_sign;
957
958 //
959 // Interface invariants:
960 //
961 static_assert(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
962
963 protected:
964 template <class T>
965 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!boost::multiprecision::detail::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits)))>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base966 check_in_range(T val, const std::integral_constant<int, checked>&)
967 {
968 using common_type = typename std::common_type<typename boost::multiprecision::detail::make_unsigned<T>::type, local_limb_type>::type;
969
970 if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
971 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
972 }
973 template <class T>
974 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(boost::multiprecision::detail::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits)))>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base975 check_in_range(T val, const std::integral_constant<int, checked>&)
976 {
977 using std::abs;
978 using common_type = typename std::common_type<T, local_limb_type>::type;
979
980 if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
981 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
982 }
983 template <class T, int C>
check_in_rangeboost::multiprecision::backends::cpp_int_base984 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&) noexcept {}
985
986 template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base987 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type())))
988 {
989 check_in_range(val, checked_type());
990 }
991
992 public:
993 //
994 // Direct construction:
995 //
996 template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base997 BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
998 : m_data(i < 0 ? static_cast<local_limb_type>(static_cast<typename boost::multiprecision::detail::make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0) {}
999 template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1000 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1001 : m_data(i < 0 ? (static_cast<local_limb_type>(static_cast<typename boost::multiprecision::detail::make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask)) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0)
1002 {
1003 check_in_range(i);
1004 }
1005 template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1006 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = 0) noexcept
1007 : m_data(static_cast<local_limb_type>(i) & limb_mask),
1008 m_sign(false) {}
1009 template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1010 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1011 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
1012 #if !(defined(__clang__) && defined(__MINGW32__))
1013 template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1014 BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = 0) noexcept
1015 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask),
1016 m_sign(i < 0) {}
1017 template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1018 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == checked)>::type const* = 0)
1019 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1020 #else
1021 //
1022 // conversion from float to __int128 is broken on clang/mingw,
1023 // see: https://bugs.llvm.org/show_bug.cgi?id=48940
1024 // Since no floating point type has more than 64 bits of
1025 // precision, we can simply cast to an intermediate type to
1026 // solve the issue:
1027 //
1028 template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1029 BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = 0) noexcept
1030 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(std::fabs(i))) & limb_mask),
1031 m_sign(i < 0) {}
1032 template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1033 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == checked)>::type const* = 0)
1034 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(std::fabs(i))) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1035 #endif
1036
cpp_int_baseboost::multiprecision::backends::cpp_int_base1037 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1038 : m_data(static_cast<local_limb_type>(0u)),
1039 m_sign(false)
1040 {}
1041 template <limb_type a>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1042 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1043 : m_data(static_cast<local_limb_type>(a)),
1044 m_sign(false) {}
1045 template <limb_type a, limb_type b>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1046 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1047 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
1048 m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1049 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) noexcept
1050 : m_data(a.m_data),
1051 m_sign(a.m_data ? !a.m_sign : false) {}
1052 //
1053 // These are deprecated in C++20 unless we make them explicit:
1054 //
1055 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1056
cpp_int_baseboost::multiprecision::backends::cpp_int_base1057 explicit constexpr cpp_int_base(scoped_shared_storage&, unsigned) noexcept : m_data(0), m_sign(false)
1058 {}
1059 //
1060 // Helper functions for getting at our internal data, and manipulating storage:
1061 //
sizeboost::multiprecision::backends::cpp_int_base1062 BOOST_MP_FORCEINLINE constexpr unsigned size() const noexcept { return 1; }
limbsboost::multiprecision::backends::cpp_int_base1063 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
limbsboost::multiprecision::backends::cpp_int_base1064 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
signboost::multiprecision::backends::cpp_int_base1065 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool sign() const noexcept { return m_sign; }
signboost::multiprecision::backends::cpp_int_base1066 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
1067 {
1068 m_sign = b;
1069 // Check for zero value:
1070 if (m_sign && !m_data)
1071 {
1072 m_sign = false;
1073 }
1074 }
resizeboost::multiprecision::backends::cpp_int_base1075 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned /* new_size */, unsigned min_size)
1076 {
1077 detail::verify_new_size(2, min_size, checked_type());
1078 }
normalizeboost::multiprecision::backends::cpp_int_base1079 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1080 {
1081 if (!m_data)
1082 m_sign = false; // zero is always unsigned
1083 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1084 m_data &= limb_mask;
1085 }
1086
cpp_int_baseboost::multiprecision::backends::cpp_int_base1087 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0), m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1088 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1089 : m_data(o.m_data),
1090 m_sign(o.m_sign) {}
1091 //~cpp_int_base() noexcept {}
assignboost::multiprecision::backends::cpp_int_base1092 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1093 {
1094 m_data = o.m_data;
1095 m_sign = o.m_sign;
1096 }
negateboost::multiprecision::backends::cpp_int_base1097 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
1098 {
1099 m_sign = !m_sign;
1100 // Check for zero value:
1101 if (m_data == 0)
1102 {
1103 m_sign = false;
1104 }
1105 }
isnegboost::multiprecision::backends::cpp_int_base1106 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
1107 {
1108 return m_sign;
1109 }
do_swapboost::multiprecision::backends::cpp_int_base1110 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1111 {
1112 std_constexpr::swap(m_sign, o.m_sign);
1113 std_constexpr::swap(m_data, o.m_data);
1114 }
1115 };
1116 //
1117 // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
1118 //
1119 template <unsigned MinBits, cpp_int_check_type Checked>
1120 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
1121 {
1122 using local_limb_type = typename trivial_limb_type<MinBits>::type;
1123 using limb_pointer = local_limb_type* ;
1124 using const_limb_pointer = const local_limb_type* ;
1125
1126 struct scoped_shared_storage
1127 {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage1128 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage1129 BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
1130 };
1131
1132 private:
1133 static constexpr unsigned limb_bits = sizeof(local_limb_type) * CHAR_BIT;
1134 static constexpr local_limb_type limb_mask = limb_bits != MinBits ? static_cast<local_limb_type>(static_cast<local_limb_type>(~local_limb_type(0)) >> (limb_bits - MinBits))
1135 : static_cast<local_limb_type>(~local_limb_type(0));
1136
1137 local_limb_type m_data;
1138
1139 using checked_type = std::integral_constant<int, Checked>;
1140
1141 //
1142 // Interface invariants:
1143 //
1144 static_assert(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
1145
1146 protected:
1147 template <class T>
1148 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base1149 check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, false>&)
1150 {
1151 using common_type = typename std::common_type<T, local_limb_type>::type;
1152
1153 if (static_cast<common_type>(val) > limb_mask)
1154 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1155 }
1156 template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base1157 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, true>&)
1158 {
1159 using common_type = typename std::common_type<T, local_limb_type>::type;
1160
1161 if (static_cast<common_type>(val) > static_cast<common_type>(limb_mask))
1162 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1163 if (val < 0)
1164 BOOST_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
1165 }
1166 template <class T, int C, bool B>
check_in_rangeboost::multiprecision::backends::cpp_int_base1167 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&, const std::integral_constant<bool, B>&) noexcept {}
1168
1169 template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base1170 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type(), boost::multiprecision::detail::is_signed<T>())))
1171 {
1172 check_in_range(val, checked_type(), boost::multiprecision::detail::is_signed<T>());
1173 }
1174
1175 public:
1176 //
1177 // Direct construction:
1178 //
1179 #ifdef __MSVC_RUNTIME_CHECKS
1180 template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1181 BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = 0) noexcept
1182 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
1183 {}
1184 template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1185 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1186 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i & limb_mask) : static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1187 template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1188 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = 0) noexcept
1189 : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
1190 template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1191 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1192 : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1193 #else
1194 template <class SI>
1195 BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = 0) noexcept
1196 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
1197 {}
1198 template <class SI>
1199 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1200 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
1201 template <class UI>
1202 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = 0) noexcept
1203 : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
1204 template <class UI>
1205 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = 0) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1206 : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
1207 #endif
1208 template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1209 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value >::type const* = 0) noexcept((Checked == unchecked))
1210 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask)
1211 {
1212 check_in_range(i);
1213 if (i < 0)
1214 negate();
1215 }
cpp_int_baseboost::multiprecision::backends::cpp_int_base1216 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1217 : m_data(static_cast<local_limb_type>(0u))
1218 {}
1219 template <limb_type a>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1220 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1221 : m_data(static_cast<local_limb_type>(a)) {}
1222 template <limb_type a, limb_type b>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1223 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1224 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
1225 //
1226 // These are deprecated in C++20 unless we make them explicit:
1227 //
1228 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1229
cpp_int_baseboost::multiprecision::backends::cpp_int_base1230 explicit constexpr cpp_int_base(scoped_shared_storage&, unsigned) noexcept : m_data(0)
1231 {}
1232 //
1233 // Helper functions for getting at our internal data, and manipulating storage:
1234 //
sizeboost::multiprecision::backends::cpp_int_base1235 BOOST_MP_FORCEINLINE constexpr unsigned size() const noexcept { return 1; }
limbsboost::multiprecision::backends::cpp_int_base1236 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
limbsboost::multiprecision::backends::cpp_int_base1237 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
signboost::multiprecision::backends::cpp_int_base1238 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
signboost::multiprecision::backends::cpp_int_base1239 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
1240 {
1241 if (b)
1242 negate();
1243 }
resizeboost::multiprecision::backends::cpp_int_base1244 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, unsigned min_size)
1245 {
1246 detail::verify_new_size(2, min_size, checked_type());
1247 }
normalizeboost::multiprecision::backends::cpp_int_base1248 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1249 {
1250 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1251 m_data &= limb_mask;
1252 }
1253
cpp_int_baseboost::multiprecision::backends::cpp_int_base1254 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1255 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1256 : m_data(o.m_data) {}
1257 //~cpp_int_base() noexcept {}
assignboost::multiprecision::backends::cpp_int_base1258 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1259 {
1260 m_data = o.m_data;
1261 }
negateboost::multiprecision::backends::cpp_int_base1262 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
1263 {
1264 BOOST_IF_CONSTEXPR(Checked == checked)
1265 {
1266 BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1267 }
1268 m_data = ~m_data;
1269 ++m_data;
1270 }
isnegboost::multiprecision::backends::cpp_int_base1271 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
1272 {
1273 return false;
1274 }
do_swapboost::multiprecision::backends::cpp_int_base1275 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1276 {
1277 std_constexpr::swap(m_data, o.m_data);
1278 }
1279 };
1280 //
1281 // Traits class, lets us know whether type T can be directly converted to the base type,
1282 // used to enable/disable constructors etc:
1283 //
1284 template <class Arg, class Base>
1285 struct is_allowed_cpp_int_base_conversion : public std::conditional<
1286 std::is_same<Arg, limb_type>::value || std::is_same<Arg, signed_limb_type>::value
1287 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
1288 || std::is_same<Arg, double_limb_type>::value || std::is_same<Arg, signed_double_limb_type>::value
1289 #endif
1290 || literals::detail::is_value_pack<Arg>::value || (is_trivial_cpp_int<Base>::value && boost::multiprecision::detail::is_arithmetic<Arg>::value),
1291 std::integral_constant<bool, true>,
1292 std::integral_constant<bool, false>>::type
1293 {};
1294 //
1295 // Now the actual backend, normalising parameters passed to the base class:
1296 //
1297 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1298 struct cpp_int_backend
1299 : public cpp_int_base<
1300 min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1301 max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1302 SignType,
1303 Checked,
1304 Allocator,
1305 is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1306 {
1307 using self_type = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
1308 using base_type = cpp_int_base<
1309 min_precision<self_type>::value,
1310 max_precision<self_type>::value,
1311 SignType,
1312 Checked,
1313 Allocator,
1314 is_trivial_cpp_int<self_type>::value>;
1315 using trivial_tag = std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>;
1316
1317 public:
1318 using signed_types = typename std::conditional<
1319 is_trivial_cpp_int<self_type>::value,
1320 std::tuple<
1321 signed char, short, int, long,
1322 boost::long_long_type, signed_double_limb_type>,
1323 std::tuple<signed_limb_type, signed_double_limb_type> >::type;
1324 using unsigned_types = typename std::conditional<
1325 is_trivial_cpp_int<self_type>::value,
1326 std::tuple<unsigned char, unsigned short, unsigned,
1327 unsigned long, boost::ulong_long_type, double_limb_type>,
1328 std::tuple<limb_type, double_limb_type> >::type;
1329 using float_types = typename std::conditional<
1330 is_trivial_cpp_int<self_type>::value,
1331 std::tuple<float, double, long double>,
1332 std::tuple<long double> >::type;
1333 using checked_type = std::integral_constant<int, Checked> ;
1334
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1335 BOOST_MP_FORCEINLINE constexpr cpp_int_backend() noexcept {}
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1336 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(const cpp_int_backend& o) noexcept(std::is_void<Allocator>::value) : base_type(o) {}
1337 // rvalue copy:
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1338 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(cpp_int_backend&& o) noexcept
1339 : base_type(static_cast<base_type&&>(o))
1340 {}
1341 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1342 BOOST_MP_FORCEINLINE BOOST_CXX14_CONSTEXPR cpp_int_backend(cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&& o, typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>, self_type>::value>::type* = 0) noexcept
1343 {
1344 *this = static_cast<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&&>(o);
1345 }
1346 //
1347 // Direct construction from arithmetic type:
1348 //
1349 template <class Arg>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1350 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(Arg i, typename std::enable_if<is_allowed_cpp_int_base_conversion<Arg, base_type>::value>::type const* = 0) noexcept(noexcept(base_type(std::declval<Arg>())))
1351 : base_type(i) {}
1352 //
1353 // Aliasing constructor: the result will alias the memory referenced, unless
1354 // we have fixed precision and storage, in which case we copy the memory:
1355 //
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1356 explicit constexpr cpp_int_backend(limb_type* data, unsigned offset, unsigned len) noexcept
1357 : base_type(data, offset, len) {}
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1358 explicit cpp_int_backend(const limb_type* data, unsigned offset, unsigned len) noexcept
1359 : base_type(data, offset, len) { this->normalize(); }
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1360 explicit constexpr cpp_int_backend(typename base_type::scoped_shared_storage& data, unsigned len) noexcept
1361 : base_type(data, len) {}
1362
1363 private:
1364 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1365 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::integral_constant<bool, true> const&, std::integral_constant<bool, true> const&)
1366 {
1367 // Assigning trivial type to trivial type:
1368 this->check_in_range(*other.limbs());
1369 *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1370 this->sign(other.sign());
1371 this->normalize();
1372 }
1373 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1374 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::integral_constant<bool, true> const&, std::integral_constant<bool, false> const&)
1375 {
1376 // non-trivial to trivial narrowing conversion:
1377 double_limb_type v = *other.limbs();
1378 if (other.size() > 1)
1379 {
1380 v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1381 BOOST_IF_CONSTEXPR(Checked == checked)
1382 {
1383 if (other.size() > 2)
1384 {
1385 BOOST_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1386 }
1387 }
1388 }
1389 *this = v;
1390 this->sign(other.sign());
1391 this->normalize();
1392 }
1393 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1394 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::integral_constant<bool, false> const&, std::integral_constant<bool, true> const&)
1395 {
1396 // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards:
1397 *this = static_cast<
1398 typename boost::multiprecision::detail::canonical<
1399 typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1400 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
1401 this->sign(other.sign());
1402 }
1403 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1404 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::integral_constant<bool, false> const&, std::integral_constant<bool, false> const&)
1405 {
1406 // regular non-trivial to non-trivial assign:
1407 this->resize(other.size(), other.size());
1408
1409 #if !defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
1410 unsigned count = (std::min)(other.size(), this->size());
1411 for (unsigned i = 0; i < count; ++i)
1412 this->limbs()[i] = other.limbs()[i];
1413 #else
1414 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1415 if (BOOST_MP_IS_CONST_EVALUATED(other.size()))
1416 {
1417 unsigned count = (std::min)(other.size(), this->size());
1418 for (unsigned i = 0; i < count; ++i)
1419 this->limbs()[i] = other.limbs()[i];
1420 }
1421 else
1422 #endif
1423 std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size(), this->size()) * sizeof(this->limbs()[0]));
1424 #endif
1425 this->sign(other.sign());
1426 this->normalize();
1427 }
1428
1429 public:
1430 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1431 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1432 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1433 typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = 0)
1434 : base_type()
1435 {
1436 do_assign(
1437 other,
1438 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1439 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1440 }
1441 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1442 explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1443 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1444 typename std::enable_if< !(is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value)>::type* = 0)
1445 : base_type()
1446 {
1447 do_assign(
1448 other,
1449 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1450 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1451 }
1452 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
operator =boost::multiprecision::backends::cpp_int_backend1453 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
1454 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1455 {
1456 do_assign(
1457 other,
1458 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1459 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1460 return *this;
1461 }
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1462 constexpr cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1463 : base_type(static_cast<const base_type&>(a), tag)
1464 {}
1465
operator =boost::multiprecision::backends::cpp_int_backend1466 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(const cpp_int_backend& o) noexcept(noexcept(std::declval<cpp_int_backend>().assign(std::declval<const cpp_int_backend&>())))
1467 {
1468 this->assign(o);
1469 return *this;
1470 }
1471 // rvalue copy:
operator =boost::multiprecision::backends::cpp_int_backend1472 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(cpp_int_backend&& o) noexcept(noexcept(std::declval<base_type&>() = std::declval<base_type>()))
1473 {
1474 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1475 return *this;
1476 }
1477 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
operator =boost::multiprecision::backends::cpp_int_backend1478 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<((MaxBits2 <= MaxBits) || (MaxBits == 0)) && !std::is_void<Allocator>::value, cpp_int_backend&>::type operator=(cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator>&& o) noexcept
1479 {
1480 *static_cast<base_type*>(this) = static_cast<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>::base_type&&>(o);
1481 return *this;
1482 }
1483 private:
1484 template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1485 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_unsigned<A>::value>::type do_assign_arithmetic(A val, const std::integral_constant<bool, true>&)
1486 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1487 {
1488 this->check_in_range(val);
1489 *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1490 this->sign(false);
1491 this->normalize();
1492 }
1493 template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1494 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(boost::multiprecision::detail::is_unsigned<A>::value || !boost::multiprecision::detail::is_integral<A>::value)>::type do_assign_arithmetic(A val, const std::integral_constant<bool, true>&)
1495 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1496 {
1497 this->check_in_range(val);
1498 *this->limbs() = (val < 0) ? static_cast<typename self_type::local_limb_type>(boost::multiprecision::detail::unsigned_abs(val)) : static_cast<typename self_type::local_limb_type>(val);
1499 this->sign(val < 0);
1500 this->normalize();
1501 }
1502 template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1503 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!boost::multiprecision::detail::is_integral<A>::value>::type do_assign_arithmetic(A val, const std::integral_constant<bool, true>&)
1504 {
1505 this->check_in_range(val);
1506 *this->limbs() = (val < 0) ? static_cast<typename self_type::local_limb_type>(boost::multiprecision::detail::abs(val)) : static_cast<typename self_type::local_limb_type>(val);
1507 this->sign(val < 0);
1508 this->normalize();
1509 }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1510 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(limb_type i, const std::integral_constant<bool, false>&) noexcept
1511 {
1512 this->resize(1, 1);
1513 *this->limbs() = i;
1514 this->sign(false);
1515 }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1516 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(signed_limb_type i, const std::integral_constant<bool, false>&) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1517 {
1518 this->resize(1, 1);
1519 *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1520 this->sign(i < 0);
1521 }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1522 BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(double_limb_type i, const std::integral_constant<bool, false>&) noexcept
1523 {
1524 static_assert(sizeof(i) == 2 * sizeof(limb_type), "Failed integer size check");
1525 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count");
1526 typename base_type::limb_pointer p = this->limbs();
1527 #ifdef __MSVC_RUNTIME_CHECKS
1528 *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1529 #else
1530 *p = static_cast<limb_type>(i);
1531 #endif
1532 p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1533 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1534 this->sign(false);
1535 }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1536 BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(signed_double_limb_type i, const std::integral_constant<bool, false>&) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1537 {
1538 static_assert(sizeof(i) == 2 * sizeof(limb_type), "double limb type size check failed");
1539 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count check");
1540 bool s = false;
1541 if (i < 0)
1542 s = true;
1543 double_limb_type ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1544 typename base_type::limb_pointer p = this->limbs();
1545 #ifdef __MSVC_RUNTIME_CHECKS
1546 *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1547 #else
1548 *p = static_cast<limb_type>(ui);
1549 #endif
1550 p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1551 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1552 this->sign(s);
1553 }
1554
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1555 BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(long double a, const std::integral_constant<bool, false>&)
1556 {
1557 using default_ops::eval_add;
1558 using default_ops::eval_subtract;
1559 using std::floor;
1560 using std::frexp;
1561 using std::ldexp;
1562
1563 if (a < 0)
1564 {
1565 do_assign_arithmetic(-a, std::integral_constant<bool, false>());
1566 this->sign(true);
1567 return;
1568 }
1569
1570 if (a == 0)
1571 {
1572 *this = static_cast<limb_type>(0u);
1573 }
1574
1575 if (a == 1)
1576 {
1577 *this = static_cast<limb_type>(1u);
1578 }
1579
1580 if ((boost::math::isinf)(a) || (boost::math::isnan)(a))
1581 {
1582 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
1583 }
1584
1585 int e = 0;
1586 long double f(0), term(0);
1587 *this = static_cast<limb_type>(0u);
1588
1589 f = frexp(a, &e);
1590
1591 #if !(defined(__clang__) && (__clang_major__ <= 7))
1592 constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
1593 #else
1594 // clang 7 has an issue converting long double to unsigned long long in
1595 // release mode (bits get dropped, conversion appears to go via float)
1596 // Never extract more than double bits at a time:
1597 constexpr limb_type shift = std::numeric_limits<limb_type>::digits > std::numeric_limits<double>::digits
1598 ? std::numeric_limits<double>::digits : std::numeric_limits<limb_type>::digits;
1599 #endif
1600
1601 while (f)
1602 {
1603 // extract int sized bits from f:
1604 f = ldexp(f, shift);
1605 term = floor(f);
1606 e -= shift;
1607 eval_left_shift(*this, shift);
1608 #if !(defined(__clang__) && (__clang_major__ <= 7))
1609 if (term > 0)
1610 eval_add(*this, static_cast<limb_type>(term));
1611 else
1612 eval_subtract(*this, static_cast<limb_type>(-term));
1613 #else
1614 // clang 7 requires extra cast to double to avoid buggy code generation:
1615 if (term > 0)
1616 eval_add(*this, static_cast<limb_type>(static_cast<double>(term)));
1617 else
1618 eval_subtract(*this, static_cast<limb_type>(static_cast<double>(-term)));
1619 #endif
1620 f -= term;
1621 }
1622 if (e > 0)
1623 eval_left_shift(*this, e);
1624 else if (e < 0)
1625 eval_right_shift(*this, -e);
1626 }
1627
1628 public:
1629 template <class Arithmetic>
operator =boost::multiprecision::backends::cpp_int_backend1630 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!boost::multiprecision::detail::is_byte_container<Arithmetic>::value, cpp_int_backend&>::type operator=(Arithmetic val) noexcept(noexcept(std::declval<cpp_int_backend>().do_assign_arithmetic(std::declval<Arithmetic>(), trivial_tag())))
1631 {
1632 do_assign_arithmetic(val, trivial_tag());
1633 return *this;
1634 }
1635
1636 private:
do_assign_stringboost::multiprecision::backends::cpp_int_backend1637 void do_assign_string(const char* s, const std::integral_constant<bool, true>&)
1638 {
1639 std::size_t n = s ? std::strlen(s) : 0;
1640 *this = 0;
1641 unsigned radix = 10;
1642 bool isneg = false;
1643 if (n && (*s == '-'))
1644 {
1645 --n;
1646 ++s;
1647 isneg = true;
1648 }
1649 if (n && (*s == '0'))
1650 {
1651 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1652 {
1653 radix = 16;
1654 s += 2;
1655 n -= 2;
1656 }
1657 else
1658 {
1659 radix = 8;
1660 n -= 1;
1661 }
1662 }
1663 if (n)
1664 {
1665 unsigned val;
1666 while (*s)
1667 {
1668 if (*s >= '0' && *s <= '9')
1669 val = *s - '0';
1670 else if (*s >= 'a' && *s <= 'f')
1671 val = 10 + *s - 'a';
1672 else if (*s >= 'A' && *s <= 'F')
1673 val = 10 + *s - 'A';
1674 else
1675 val = radix + 1;
1676 if (val >= radix)
1677 {
1678 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1679 }
1680 *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1681 *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1682 ++s;
1683 }
1684 }
1685 if (isneg)
1686 this->negate();
1687 }
do_assign_stringboost::multiprecision::backends::cpp_int_backend1688 void do_assign_string(const char* s, const std::integral_constant<bool, false>&)
1689 {
1690 using default_ops::eval_add;
1691 using default_ops::eval_multiply;
1692 std::size_t n = s ? std::strlen(s) : 0;
1693 *this = static_cast<limb_type>(0u);
1694 unsigned radix = 10;
1695 bool isneg = false;
1696 if (n && (*s == '-'))
1697 {
1698 --n;
1699 ++s;
1700 isneg = true;
1701 }
1702 if (n && (*s == '0'))
1703 {
1704 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1705 {
1706 radix = 16;
1707 s += 2;
1708 n -= 2;
1709 }
1710 else
1711 {
1712 radix = 8;
1713 n -= 1;
1714 }
1715 }
1716 //
1717 // Exception guarantee: create the result in stack variable "result"
1718 // then do a swap at the end. In the event of a throw, *this will
1719 // be left unchanged.
1720 //
1721 cpp_int_backend result;
1722 if (n)
1723 {
1724 if (radix == 16)
1725 {
1726 while (*s == '0')
1727 ++s;
1728 std::size_t bitcount = 4 * std::strlen(s);
1729 limb_type val;
1730 std::size_t limb, shift;
1731 if (bitcount > 4)
1732 bitcount -= 4;
1733 else
1734 bitcount = 0;
1735 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1736 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1737 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1738 while (*s)
1739 {
1740 if (*s >= '0' && *s <= '9')
1741 val = *s - '0';
1742 else if (*s >= 'a' && *s <= 'f')
1743 val = 10 + *s - 'a';
1744 else if (*s >= 'A' && *s <= 'F')
1745 val = 10 + *s - 'A';
1746 else
1747 {
1748 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1749 }
1750 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1751 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1752 val <<= shift;
1753 if (result.size() > limb)
1754 {
1755 result.limbs()[limb] |= val;
1756 }
1757 ++s;
1758 bitcount -= 4;
1759 }
1760 result.normalize();
1761 }
1762 else if (radix == 8)
1763 {
1764 while (*s == '0')
1765 ++s;
1766 std::size_t bitcount = 3 * std::strlen(s);
1767 limb_type val;
1768 std::size_t limb, shift;
1769 if (bitcount > 3)
1770 bitcount -= 3;
1771 else
1772 bitcount = 0;
1773 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1774 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1775 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1776 while (*s)
1777 {
1778 if (*s >= '0' && *s <= '7')
1779 val = *s - '0';
1780 else
1781 {
1782 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1783 }
1784 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1785 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1786 if (result.size() > limb)
1787 {
1788 result.limbs()[limb] |= (val << shift);
1789 if (shift > sizeof(limb_type) * CHAR_BIT - 3)
1790 {
1791 // Deal with the bits in val that overflow into the next limb:
1792 val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1793 if (val)
1794 {
1795 // If this is the most-significant-limb, we may need to allocate an extra one for the overflow:
1796 if (limb + 1 == newsize)
1797 result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1798 if (result.size() > limb + 1)
1799 {
1800 result.limbs()[limb + 1] |= val;
1801 }
1802 }
1803 }
1804 }
1805 ++s;
1806 bitcount -= 3;
1807 }
1808 result.normalize();
1809 }
1810 else
1811 {
1812 // Base 10, we extract blocks of size 10^9 at a time, that way
1813 // the number of multiplications is kept to a minimum:
1814 limb_type block_mult = max_block_10;
1815 while (*s)
1816 {
1817 limb_type block = 0;
1818 for (unsigned i = 0; i < digits_per_block_10; ++i)
1819 {
1820 limb_type val;
1821 if (*s >= '0' && *s <= '9')
1822 val = *s - '0';
1823 else
1824 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1825 block *= 10;
1826 block += val;
1827 if (!*++s)
1828 {
1829 block_mult = block_multiplier(i);
1830 break;
1831 }
1832 }
1833 eval_multiply(result, block_mult);
1834 eval_add(result, block);
1835 }
1836 }
1837 }
1838 if (isneg)
1839 result.negate();
1840 result.swap(*this);
1841 }
1842
1843 public:
operator =boost::multiprecision::backends::cpp_int_backend1844 cpp_int_backend& operator=(const char* s)
1845 {
1846 do_assign_string(s, trivial_tag());
1847 return *this;
1848 }
swapboost::multiprecision::backends::cpp_int_backend1849 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) noexcept
1850 {
1851 this->do_swap(o);
1852 }
1853
1854 private:
do_get_trivial_stringboost::multiprecision::backends::cpp_int_backend1855 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
1856 {
1857 using io_type = typename std::conditional<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type;
1858 if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1859 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1860 std::stringstream ss;
1861 ss.flags(f & ~std::ios_base::showpos);
1862 ss << static_cast<io_type>(*this->limbs());
1863 std::string result;
1864 if (this->sign())
1865 result += '-';
1866 else if (f & std::ios_base::showpos)
1867 result += '+';
1868 result += ss.str();
1869 return result;
1870 }
do_get_trivial_stringboost::multiprecision::backends::cpp_int_backend1871 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
1872 {
1873 // Even though we have only one limb, we can't do IO on it :-(
1874 int base = 10;
1875 if ((f & std::ios_base::oct) == std::ios_base::oct)
1876 base = 8;
1877 else if ((f & std::ios_base::hex) == std::ios_base::hex)
1878 base = 16;
1879 std::string result;
1880
1881 unsigned Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1882
1883 if (base == 8 || base == 16)
1884 {
1885 if (this->sign())
1886 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1887 limb_type shift = base == 8 ? 3 : 4;
1888 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1889 typename base_type::local_limb_type v = *this->limbs();
1890 result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1891 std::string::difference_type pos = result.size() - 1;
1892 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1893 for (unsigned i = 0; i < Bits / shift; ++i)
1894 {
1895 char c = '0' + static_cast<char>(v & mask);
1896 if (c > '9')
1897 c += letter_a - '9' - 1;
1898 result[pos--] = c;
1899 v >>= shift;
1900 }
1901 if (Bits % shift)
1902 {
1903 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1904 char c = '0' + static_cast<char>(v & mask);
1905 if (c > '9')
1906 c += letter_a - '9';
1907 result[pos] = c;
1908 }
1909 //
1910 // Get rid of leading zeros:
1911 //
1912 std::string::size_type n = result.find_first_not_of('0');
1913 if (!result.empty() && (n == std::string::npos))
1914 n = result.size() - 1;
1915 result.erase(0, n);
1916 if (f & std::ios_base::showbase)
1917 {
1918 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
1919 result.insert(static_cast<std::string::size_type>(0), pp);
1920 }
1921 }
1922 else
1923 {
1924 result.assign(Bits / 3 + 1, '0');
1925 std::string::difference_type pos = result.size() - 1;
1926 typename base_type::local_limb_type v(*this->limbs());
1927 bool neg = false;
1928 if (this->sign())
1929 {
1930 neg = true;
1931 }
1932 while (v)
1933 {
1934 result[pos] = (v % 10) + '0';
1935 --pos;
1936 v /= 10;
1937 }
1938 std::string::size_type n = result.find_first_not_of('0');
1939 result.erase(0, n);
1940 if (result.empty())
1941 result = "0";
1942 if (neg)
1943 result.insert(static_cast<std::string::size_type>(0), 1, '-');
1944 else if (f & std::ios_base::showpos)
1945 result.insert(static_cast<std::string::size_type>(0), 1, '+');
1946 }
1947 return result;
1948 }
do_get_stringboost::multiprecision::backends::cpp_int_backend1949 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
1950 {
1951 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
1952 return do_get_trivial_string(f, std::integral_constant<bool, std::is_same<typename base_type::local_limb_type, double_limb_type>::value>());
1953 #else
1954 return do_get_trivial_string(f, std::integral_constant<bool, false>());
1955 #endif
1956 }
do_get_stringboost::multiprecision::backends::cpp_int_backend1957 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
1958 {
1959 using default_ops::eval_get_sign;
1960 int base = 10;
1961 if ((f & std::ios_base::oct) == std::ios_base::oct)
1962 base = 8;
1963 else if ((f & std::ios_base::hex) == std::ios_base::hex)
1964 base = 16;
1965 std::string result;
1966
1967 unsigned Bits = this->size() * base_type::limb_bits;
1968
1969 if (base == 8 || base == 16)
1970 {
1971 if (this->sign())
1972 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1973 limb_type shift = base == 8 ? 3 : 4;
1974 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1975 cpp_int_backend t(*this);
1976 result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
1977 std::string::difference_type pos = result.size() - 1;
1978 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1979 for (unsigned i = 0; i < Bits / shift; ++i)
1980 {
1981 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1982 if (c > '9')
1983 c += letter_a - '9' - 1;
1984 result[pos--] = c;
1985 eval_right_shift(t, shift);
1986 }
1987 if (Bits % shift)
1988 {
1989 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1990 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1991 if (c > '9')
1992 c += letter_a - '9';
1993 result[pos] = c;
1994 }
1995 //
1996 // Get rid of leading zeros:
1997 //
1998 std::string::size_type n = result.find_first_not_of('0');
1999 if (!result.empty() && (n == std::string::npos))
2000 n = result.size() - 1;
2001 result.erase(0, n);
2002 if (f & std::ios_base::showbase)
2003 {
2004 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
2005 result.insert(static_cast<std::string::size_type>(0), pp);
2006 }
2007 }
2008 else
2009 {
2010 result.assign(Bits / 3 + 1, '0');
2011 std::string::difference_type pos = result.size() - 1;
2012 cpp_int_backend t(*this);
2013 cpp_int_backend r;
2014 bool neg = false;
2015 if (t.sign())
2016 {
2017 t.negate();
2018 neg = true;
2019 }
2020 if (this->size() == 1)
2021 {
2022 result = boost::lexical_cast<std::string>(t.limbs()[0]);
2023 }
2024 else
2025 {
2026 cpp_int_backend block10;
2027 block10 = max_block_10;
2028 while (eval_get_sign(t) != 0)
2029 {
2030 cpp_int_backend t2;
2031 divide_unsigned_helper(&t2, t, block10, r);
2032 t = t2;
2033 limb_type v = r.limbs()[0];
2034 for (unsigned i = 0; i < digits_per_block_10; ++i)
2035 {
2036 char c = '0' + v % 10;
2037 v /= 10;
2038 result[pos] = c;
2039 if (pos-- == 0)
2040 break;
2041 }
2042 }
2043 }
2044 std::string::size_type n = result.find_first_not_of('0');
2045 result.erase(0, n);
2046 if (result.empty())
2047 result = "0";
2048 if (neg)
2049 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2050 else if (f & std::ios_base::showpos)
2051 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2052 }
2053 return result;
2054 }
2055
2056 public:
strboost::multiprecision::backends::cpp_int_backend2057 std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
2058 {
2059 return do_get_string(f, trivial_tag());
2060 }
2061
2062 private:
2063 template <class Container>
construct_from_containerboost::multiprecision::backends::cpp_int_backend2064 void construct_from_container(const Container& c, const std::integral_constant<bool, false>&)
2065 {
2066 //
2067 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2068 //
2069 unsigned newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
2070 if (c.size() % sizeof(limb_type))
2071 {
2072 ++newsize;
2073 }
2074 if (newsize)
2075 {
2076 this->resize(newsize, newsize); // May throw
2077 std::memset(this->limbs(), 0, this->size());
2078 typename Container::const_iterator i(c.begin()), j(c.end());
2079 unsigned byte_location = static_cast<unsigned>(c.size() - 1);
2080 while (i != j)
2081 {
2082 unsigned limb = byte_location / sizeof(limb_type);
2083 unsigned shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
2084 if (this->size() > limb)
2085 this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2086 ++i;
2087 --byte_location;
2088 }
2089 }
2090 }
2091 template <class Container>
construct_from_containerboost::multiprecision::backends::cpp_int_backend2092 BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const std::integral_constant<bool, true>&)
2093 {
2094 //
2095 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2096 //
2097 using local_limb_type = typename base_type::local_limb_type;
2098 *this->limbs() = 0;
2099 if (c.size())
2100 {
2101 typename Container::const_iterator i(c.begin()), j(c.end());
2102 unsigned byte_location = static_cast<unsigned>(c.size() - 1);
2103 while (i != j)
2104 {
2105 unsigned limb = byte_location / sizeof(local_limb_type);
2106 unsigned shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
2107 if (limb == 0)
2108 this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2109 ++i;
2110 --byte_location;
2111 }
2112 }
2113 }
2114
2115 public:
2116 template <class Container>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend2117 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(const Container& c, typename std::enable_if<boost::multiprecision::detail::is_byte_container<Container>::value>::type const* = 0)
2118 {
2119 //
2120 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2121 //
2122 construct_from_container(c, trivial_tag());
2123 }
2124 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2125 BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&) const noexcept
2126 {
2127 if (this->sign() != o.sign())
2128 return this->sign() ? -1 : 1;
2129
2130 // Only do the compare if the same sign:
2131 int result = compare_unsigned(o);
2132
2133 if (this->sign())
2134 result = -result;
2135 return result;
2136 }
2137 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2138 BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&) const
2139 {
2140 cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
2141 return t.compare(o);
2142 }
2143 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2144 BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&) const
2145 {
2146 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
2147 return compare(t);
2148 }
2149 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2150 BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&) const noexcept
2151 {
2152 if (this->sign())
2153 {
2154 if (o.sign())
2155 {
2156 return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
2157 }
2158 else
2159 return -1;
2160 }
2161 else
2162 {
2163 if (o.sign())
2164 return 1;
2165 return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
2166 }
2167 }
2168 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compareboost::multiprecision::backends::cpp_int_backend2169 BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2170 {
2171 using t1 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> ;
2172 using t2 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>;
2173 return compare_imp(o, t1(), t2());
2174 }
2175 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_unsignedboost::multiprecision::backends::cpp_int_backend2176 BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2177 {
2178 if (this->size() != o.size())
2179 {
2180 return this->size() > o.size() ? 1 : -1;
2181 }
2182 typename base_type::const_limb_pointer pa = this->limbs();
2183 typename base_type::const_limb_pointer pb = o.limbs();
2184 for (int i = this->size() - 1; i >= 0; --i)
2185 {
2186 if (pa[i] != pb[i])
2187 return pa[i] > pb[i] ? 1 : -1;
2188 }
2189 return 0;
2190 }
2191 template <class Arithmetic>
compareboost::multiprecision::backends::cpp_int_backend2192 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Arithmetic>::value, int>::type compare(Arithmetic i) const
2193 {
2194 // braindead version:
2195 cpp_int_backend t;
2196 t = i;
2197 return compare(t);
2198 }
2199 };
2200
2201 } // namespace backends
2202
2203 namespace default_ops {
2204
2205 template <class Backend>
2206 struct double_precision_type;
2207
2208 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2209 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2210 {
2211 using type = typename std::conditional<
2212 backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2213 backends::cpp_int_backend<
2214 (std::is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
2215 : MinBits),
2216 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2217 SignType,
2218 Checked,
2219 Allocator>,
2220 backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type;
2221 };
2222
2223 } // namespace default_ops
2224
2225 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2226 struct is_equivalent_number_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
2227 : public std::integral_constant<bool, std::numeric_limits<number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, et_on> >::digits == std::numeric_limits<number<backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, et_on> >::digits>{};
2228
2229 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
2230 struct expression_template_default<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
2231 {
2232 static constexpr const expression_template_option value = et_off;
2233 };
2234
2235 using boost::multiprecision::backends::cpp_int_backend;
2236
2237 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2238 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::integral_constant<int, number_kind_integer>
2239 {};
2240
2241 using cpp_int = number<cpp_int_backend<> > ;
2242 using cpp_rational_backend = rational_adaptor<cpp_int_backend<> >;
2243 using cpp_rational = number<cpp_rational_backend> ;
2244
2245 // Fixed precision unsigned types:
2246 using uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > ;
2247 using uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > ;
2248 using uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > ;
2249 using uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> >;
2250
2251 // Fixed precision signed types:
2252 using int128_t = number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> > ;
2253 using int256_t = number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> > ;
2254 using int512_t = number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> > ;
2255 using int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> >;
2256
2257 // Over again, but with checking enabled this time:
2258 using checked_cpp_int = number<cpp_int_backend<0, 0, signed_magnitude, checked> > ;
2259 using checked_cpp_rational_backend = rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> >;
2260 using checked_cpp_rational = number<checked_cpp_rational_backend> ;
2261 // Fixed precision unsigned types:
2262 using checked_uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> > ;
2263 using checked_uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> > ;
2264 using checked_uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> > ;
2265 using checked_uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> >;
2266
2267 // Fixed precision signed types:
2268 using checked_int128_t = number<cpp_int_backend<128, 128, signed_magnitude, checked, void> > ;
2269 using checked_int256_t = number<cpp_int_backend<256, 256, signed_magnitude, checked, void> > ;
2270 using checked_int512_t = number<cpp_int_backend<512, 512, signed_magnitude, checked, void> > ;
2271 using checked_int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> >;
2272
2273 #ifdef _MSC_VER
2274 #pragma warning(pop)
2275 #endif
2276
2277 }} // namespace boost::multiprecision
2278
2279 //
2280 // Last of all we include the implementations of all the eval_* non member functions:
2281 //
2282 #include <boost/multiprecision/cpp_int/limits.hpp>
2283 #include <boost/multiprecision/cpp_int/comparison.hpp>
2284 #include <boost/multiprecision/cpp_int/add.hpp>
2285 #include <boost/multiprecision/cpp_int/multiply.hpp>
2286 #include <boost/multiprecision/cpp_int/divide.hpp>
2287 #include <boost/multiprecision/cpp_int/bitwise.hpp>
2288 #include <boost/multiprecision/cpp_int/misc.hpp>
2289 #include <boost/multiprecision/cpp_int/literals.hpp>
2290 #include <boost/multiprecision/cpp_int/serialize.hpp>
2291 #include <boost/multiprecision/cpp_int/import_export.hpp>
2292
2293 #endif
2294