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 http://www.boost.org/LICENSE_1_
6 #ifndef BOOST_MP_CPP_INT_HPP
7 #define BOOST_MP_CPP_INT_HPP
11 #include <boost/cstdint.hpp>
12 #include <boost/multiprecision/number.hpp>
13 #include <boost/multiprecision/detail/integer_ops.hpp>
14 #include <boost/array.hpp>
15 #include <boost/type_traits/is_integral.hpp>
16 #include <boost/type_traits/is_floating_point.hpp>
17 #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
18 #include <boost/multiprecision/rational_adaptor.hpp>
19 #include <boost/multiprecision/traits/is_byte_container.hpp>
20 #include <boost/detail/endian.hpp>
21 #include <boost/integer/static_min_max.hpp>
22 #include <boost/type_traits/common_type.hpp>
23 #include <boost/type_traits/make_signed.hpp>
24 #include <boost/multiprecision/cpp_int/checked.hpp>
25 #ifdef BOOST_MP_USER_DEFINED_LITERALS
26 #include <boost/multiprecision/cpp_int/value_pack.hpp>
30 namespace multiprecision{
33 using boost::enable_if;
38 #pragma warning(disable:4307) // integral constant overflow (oveflow is in a branch not taken when it would overflow)
39 #pragma warning(disable:4127) // conditional expression is constant
40 #pragma warning(disable:4702) // Unreachable code (reachability depends on template params)
43 template <unsigned MinBits = 0, unsigned MaxBits = 0, boost::multiprecision::cpp_integer_type SignType = signed_magnitude, cpp_int_check_type Checked = unchecked, class Allocator = typename mpl::if_c<MinBits && (MinBits == MaxBits), void, std::allocator<limb_type> >::type >
44 struct cpp_int_backend;
46 } // namespace backends
50 template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
51 struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public boost::false_type {};
57 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
60 // Traits class determines the maximum and minimum precision values:
62 template <class T> struct max_precision;
64 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
65 struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
67 static const unsigned value = is_void<Allocator>::value ?
68 static_unsigned_max<MinBits, MaxBits>::value
69 : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : UINT_MAX);
72 template <class T> struct min_precision;
74 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
75 struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
77 static const unsigned value = (is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value : MinBits);
80 // Traits class determines whether the number of bits precision requested could fit in a native type,
81 // we call this a "trivial" cpp_int:
84 struct is_trivial_cpp_int
86 static const bool value = false;
89 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
90 struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
92 typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> self;
93 static const bool value = is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
96 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
97 struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
99 static const bool value = true;
102 } // namespace backends
104 // Traits class to determine whether a cpp_int_backend is signed or not:
106 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
107 struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
108 : public mpl::bool_<(SignType == unsigned_magnitude) || (SignType == unsigned_packed)>{};
112 // Traits class determines whether T should be implicitly convertible to U, or
113 // whether the constructor should be made explicit. The latter happens if we
114 // are losing the sign, or have fewer digits precision in the target type:
116 template <class T, class U>
117 struct is_implicit_cpp_int_conversion;
119 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>
120 struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
122 typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t1;
123 typedef cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t2;
124 static const bool value =
125 (is_signed_number<t2>::value || !is_signed_number<t1>::value)
126 && (max_precision<t1>::value <= max_precision<t2>::value);
130 // Traits class to determine whether operations on a cpp_int may throw:
133 struct is_non_throwing_cpp_int : public mpl::false_{};
134 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType>
135 struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public mpl::true_ {};
138 // Traits class, determines whether the cpp_int is fixed precision or not:
141 struct is_fixed_precision;
142 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
143 struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
144 : public mpl::bool_<max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != UINT_MAX> {};
148 inline void verify_new_size(unsigned new_size, unsigned min_size, const mpl::int_<checked>&)
150 if(new_size < min_size)
151 BOOST_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
153 inline void verify_new_size(unsigned /*new_size*/, unsigned /*min_size*/, const mpl::int_<unchecked>&){}
156 inline void verify_limb_mask(bool b, U limb, U mask, const mpl::int_<checked>&)
158 // When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error:
159 if(b && (limb & ~mask))
160 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."));
163 inline void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const mpl::int_<unchecked>&){}
168 // Now define the various data layouts that are possible as partial specializations of the base class,
169 // starting with the default arbitrary precision signed integer type:
171 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
172 struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false> : private Allocator::template rebind<limb_type>::other
174 typedef typename Allocator::template rebind<limb_type>::other allocator_type;
175 typedef typename allocator_type::pointer limb_pointer;
176 typedef typename allocator_type::const_pointer const_limb_pointer;
177 typedef mpl::int_<Checked> checked_type;
180 // Interface invariants:
182 BOOST_STATIC_ASSERT(!is_void<Allocator>::value);
192 BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
193 BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
194 BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
195 BOOST_STATIC_CONSTANT(unsigned, internal_limb_count =
197 ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
198 : (sizeof(limb_data) / sizeof(limb_type)));
199 BOOST_STATIC_CONSTANT(bool, variable = true);
205 limb_type la[internal_limb_count];
207 double_limb_type double_first;
209 BOOST_CONSTEXPR data_type() : first(0) {}
210 BOOST_CONSTEXPR data_type(limb_type i) : first(i) {}
211 BOOST_CONSTEXPR data_type(signed_limb_type i) : first(i < 0 ? static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
212 #ifdef BOOST_LITTLE_ENDIAN
213 BOOST_CONSTEXPR data_type(double_limb_type i) : double_first(i) {}
214 BOOST_CONSTEXPR data_type(signed_double_limb_type i) : double_first(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
220 bool m_sign, m_internal;
224 // Direct construction:
226 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
227 : m_data(i), m_limbs(1), m_sign(false), m_internal(true) { }
228 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT
229 : m_data(i), m_limbs(1), m_sign(i < 0), m_internal(true) { }
230 #if defined(BOOST_LITTLE_ENDIAN) && !defined(BOOST_MP_TEST_NO_LE)
231 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
232 : m_data(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false), m_internal(true) { }
233 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT
234 : m_data(i), 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)),
235 m_sign(i < 0), m_internal(true) { }
238 // Helper functions for getting at our internal data, and manipulating storage:
240 BOOST_MP_FORCEINLINE allocator_type& allocator() BOOST_NOEXCEPT { return *this; }
241 BOOST_MP_FORCEINLINE const allocator_type& allocator()const BOOST_NOEXCEPT { return *this; }
242 BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
243 BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
244 BOOST_MP_FORCEINLINE const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
245 BOOST_MP_FORCEINLINE unsigned capacity()const BOOST_NOEXCEPT { return m_internal ? internal_limb_count : m_data.ld.capacity; }
246 BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; }
247 void sign(bool b) BOOST_NOEXCEPT
250 // Check for zero value:
251 if(m_sign && (m_limbs == 1))
257 void resize(unsigned new_size, unsigned min_size)
259 static const unsigned max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
260 // We never resize beyond MaxSize:
261 if(new_size > max_limbs)
262 new_size = max_limbs;
263 detail::verify_new_size(new_size, min_size, checked_type());
264 // See if we have enough capacity already:
265 unsigned cap = capacity();
268 // Allocate a new buffer and copy everything over:
269 cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
270 limb_pointer pl = allocator().allocate(cap);
271 std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
273 allocator().deallocate(limbs(), capacity());
277 m_data.ld.capacity = cap;
285 BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT
287 limb_pointer p = limbs();
288 while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
290 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(), m_limbs(1), m_sign(false), m_internal(true) {}
291 BOOST_MP_FORCEINLINE cpp_int_base(const cpp_int_base& o) : allocator_type(o), m_limbs(0), m_internal(true)
293 resize(o.size(), o.size());
294 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
297 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
298 cpp_int_base(cpp_int_base&& o)
299 : allocator_type(static_cast<allocator_type&&>(o)), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal)
303 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
307 m_data.ld = o.m_data.ld;
312 cpp_int_base& operator = (cpp_int_base&& o) BOOST_NOEXCEPT
315 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
316 *static_cast<allocator_type*>(this) = static_cast<allocator_type&&>(o);
319 m_internal = o.m_internal;
322 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
326 m_data.ld = o.m_data.ld;
333 BOOST_MP_FORCEINLINE ~cpp_int_base() BOOST_NOEXCEPT
336 allocator().deallocate(limbs(), capacity());
338 void assign(const cpp_int_base& o)
342 static_cast<allocator_type&>(*this) = static_cast<const allocator_type&>(o);
344 resize(o.size(), o.size());
345 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
349 BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
352 // Check for zero value:
353 if(m_sign && (m_limbs == 1))
359 BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
363 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
365 std::swap(m_data, o.m_data);
366 std::swap(m_sign, o.m_sign);
367 std::swap(m_internal, o.m_internal);
368 std::swap(m_limbs, o.m_limbs);
372 void check_in_range(const A&) BOOST_NOEXCEPT {}
375 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
377 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
378 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
379 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
380 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
381 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
382 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
383 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
384 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
385 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
386 const bool cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::variable;
390 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
391 struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false> : private Allocator::template rebind<limb_type>::other
394 // There is currently no support for unsigned arbitrary precision arithmetic, largely
395 // because it's not clear what subtraction should do:
397 BOOST_STATIC_ASSERT_MSG(((sizeof(Allocator) == 0) && !is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
400 // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
402 template <unsigned MinBits, cpp_int_check_type Checked>
403 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
405 typedef limb_type* limb_pointer;
406 typedef const limb_type* const_limb_pointer;
407 typedef mpl::int_<Checked> checked_type;
410 // Interface invariants:
412 BOOST_STATIC_ASSERT_MSG(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?");
415 BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
416 BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
417 BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
418 BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
419 BOOST_STATIC_CONSTANT(bool, variable = false);
420 BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0)));
421 BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
425 limb_type m_data[internal_limb_count];
426 limb_type m_first_limb;
427 double_limb_type m_double_first_limb;
429 BOOST_CONSTEXPR data_type() : m_first_limb(0) {}
430 BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {}
431 BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {}
432 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
433 template <limb_type...VALUES>
434 BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{ VALUES... } {}
437 boost::uint16_t m_limbs;
442 // Direct construction:
444 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
445 : m_wrapper(i), m_limbs(1), m_sign(false) {}
446 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT
447 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)), m_limbs(1), m_sign(i < 0) {}
448 #if defined(BOOST_LITTLE_ENDIAN) && !defined(BOOST_MP_TEST_NO_LE)
449 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
450 : m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false) {}
451 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT
452 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
453 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)),
456 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
457 template <limb_type...VALUES>
458 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
459 : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false) {}
460 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<> i)
461 : m_wrapper(i), m_limbs(1), m_sign(false) {}
462 BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
463 : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
466 // Helper functions for getting at our internal data, and manipulating storage:
468 BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
469 BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
470 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; }
471 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return m_sign; }
472 BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT
475 // Check for zero value:
476 if(m_sign && (m_limbs == 1))
482 BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
484 m_limbs = static_cast<boost::uint16_t>((std::min)(new_size, internal_limb_count));
485 detail::verify_new_size(m_limbs, min_size, checked_type());
487 BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
489 limb_pointer p = limbs();
490 detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type());
491 p[internal_limb_count-1] &= upper_limb_mask;
492 while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
493 if((m_limbs == 1) && (!*p)) m_sign = false; // zero is always unsigned
496 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base()BOOST_NOEXCEPT : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
497 // Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
498 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o)BOOST_NOEXCEPT
499 : m_wrapper(o.m_wrapper), m_limbs(o.m_limbs), m_sign(o.m_sign) {}
500 // Defaulted functions:
501 //~cpp_int_base() BOOST_NOEXCEPT {}
503 void assign(const cpp_int_base& o) BOOST_NOEXCEPT
508 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
512 BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
515 // Check for zero value:
516 if(m_sign && (m_limbs == 1))
522 BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
526 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
528 for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
529 std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
530 std::swap(m_sign, o.m_sign);
531 std::swap(m_limbs, o.m_limbs);
535 void check_in_range(const A&) BOOST_NOEXCEPT {}
537 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
539 template <unsigned MinBits, cpp_int_check_type Checked>
540 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
541 template <unsigned MinBits, cpp_int_check_type Checked>
542 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
543 template <unsigned MinBits, cpp_int_check_type Checked>
544 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
545 template <unsigned MinBits, cpp_int_check_type Checked>
546 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
547 template <unsigned MinBits, cpp_int_check_type Checked>
548 const bool cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::variable;
552 // Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
554 template <unsigned MinBits, cpp_int_check_type Checked>
555 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
557 typedef limb_type* limb_pointer;
558 typedef const limb_type* const_limb_pointer;
559 typedef mpl::int_<Checked> checked_type;
562 // Interface invariants:
564 BOOST_STATIC_ASSERT_MSG(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?");
567 BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
568 BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
569 BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
570 BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
571 BOOST_STATIC_CONSTANT(bool, variable = false);
572 BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0)));
573 BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
577 limb_type m_data[internal_limb_count];
578 limb_type m_first_limb;
579 double_limb_type m_double_first_limb;
581 BOOST_CONSTEXPR data_type() : m_first_limb(0) {}
582 BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {}
583 BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {}
584 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
585 template <limb_type...VALUES>
586 BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{ VALUES... } {}
593 // Direct construction:
595 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
596 : m_wrapper(i), m_limbs(1) {}
597 BOOST_MP_FORCEINLINE cpp_int_base(signed_limb_type i)BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
598 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)), m_limbs(1) { if(i < 0) negate(); }
599 #if defined(BOOST_LITTLE_ENDIAN) && !defined(BOOST_MP_TEST_NO_LE)
600 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
601 : m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1) {}
602 BOOST_MP_FORCEINLINE cpp_int_base(signed_double_limb_type i)BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
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))
609 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
610 template <limb_type...VALUES>
611 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
612 : m_wrapper(i), m_limbs(sizeof...(VALUES)) {}
613 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>)
614 : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
617 // Helper functions for getting at our internal data, and manipulating storage:
619 BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
620 BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
621 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; }
622 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; }
623 BOOST_MP_FORCEINLINE void sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked)) { if(b) negate(); }
624 BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
626 m_limbs = (std::min)(new_size, internal_limb_count);
627 detail::verify_new_size(m_limbs, min_size, checked_type());
629 BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
631 limb_pointer p = limbs();
632 detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type());
633 p[internal_limb_count-1] &= upper_limb_mask;
634 while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
637 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT
638 : m_wrapper(limb_type(0u)), m_limbs(1) {}
639 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
640 : m_wrapper(o.m_wrapper), m_limbs(o.m_limbs) {}
641 // Defaulted functions:
642 //~cpp_int_base() BOOST_NOEXCEPT {}
644 BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
649 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
653 void check_negate(const mpl::int_<checked>&)
655 BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
657 void check_negate(const mpl::int_<unchecked>&){}
659 void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
661 // Not so much a negate as a complement - this gets called when subtraction
662 // would result in a "negative" number:
664 if((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
665 return; // negating zero is always zero, and always OK.
666 check_negate(checked_type());
667 for(i = m_limbs; i < internal_limb_count; ++i)
668 m_wrapper.m_data[i] = 0;
669 m_limbs = internal_limb_count;
670 for(i = 0; i < internal_limb_count; ++i)
671 m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
673 eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>& >(*this));
675 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT
679 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
681 for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
682 std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
683 std::swap(m_limbs, o.m_limbs);
687 void check_in_range(const A&) BOOST_NOEXCEPT {}
689 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
691 template <unsigned MinBits, cpp_int_check_type Checked>
692 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
693 template <unsigned MinBits, cpp_int_check_type Checked>
694 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
695 template <unsigned MinBits, cpp_int_check_type Checked>
696 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
697 template <unsigned MinBits, cpp_int_check_type Checked>
698 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
699 template <unsigned MinBits, cpp_int_check_type Checked>
700 const bool cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::variable;
704 // Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
705 // because some platforms have native integer types longer than boost::long_long_type, "really boost::long_long_type" anyone??
707 template <unsigned N, bool s>
708 struct trivial_limb_type_imp
710 typedef double_limb_type type;
713 template <unsigned N>
714 struct trivial_limb_type_imp<N, true>
716 typedef typename boost::uint_t<N>::least type;
719 template <unsigned N>
720 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(boost::long_long_type) * CHAR_BIT> {};
722 // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
724 template <unsigned MinBits, cpp_int_check_type Checked>
725 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
727 typedef typename trivial_limb_type<MinBits>::type local_limb_type;
728 typedef local_limb_type* limb_pointer;
729 typedef const local_limb_type* const_limb_pointer;
730 typedef mpl::int_<Checked> checked_type;
732 BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
733 BOOST_STATIC_CONSTANT(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)));
735 local_limb_type m_data;
739 // Interface invariants:
741 BOOST_STATIC_ASSERT_MSG(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?");
744 typename boost::disable_if_c<!boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
745 check_in_range(T val, const mpl::int_<checked>&)
747 typedef typename common_type<typename make_unsigned<T>::type, local_limb_type>::type common_type;
749 if(static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
750 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
753 typename boost::disable_if_c<boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
754 check_in_range(T val, const mpl::int_<checked>&)
757 typedef typename common_type<T, local_limb_type>::type common_type;
759 if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
760 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
762 template <class T, int C>
763 void check_in_range(T, const mpl::int_<C>&) BOOST_NOEXCEPT {}
766 void check_in_range(T val) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type())))
768 check_in_range(val, checked_type());
773 // Direct construction:
776 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == unchecked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
777 : m_data(i < 0 ? static_cast<local_limb_type>(static_cast<typename make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0) {}
779 BOOST_MP_FORCEINLINE cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == checked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
780 : m_data(i < 0 ? (static_cast<local_limb_type>(static_cast<typename make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask)) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0)
781 { check_in_range(i); }
783 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == unchecked)>::type const* = 0) BOOST_NOEXCEPT
784 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) {}
786 BOOST_MP_FORCEINLINE cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == checked)>::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
787 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
789 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(F i, typename boost::enable_if_c<is_floating_point<F>::value && (Checked == unchecked)>::type const* = 0) BOOST_NOEXCEPT
790 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) {}
792 BOOST_MP_FORCEINLINE cpp_int_base(F i, typename boost::enable_if_c<is_floating_point<F>::value && (Checked == checked)>::type const* = 0)
793 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
794 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
795 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
796 : m_data(static_cast<local_limb_type>(0u)), m_sign(false) {}
797 template <limb_type a>
798 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>)BOOST_NOEXCEPT
799 : m_data(static_cast<local_limb_type>(a)), m_sign(false) {}
800 template <limb_type a, limb_type b>
801 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>)BOOST_NOEXCEPT
802 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)), m_sign(false) {}
803 BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)BOOST_NOEXCEPT
804 : m_data(a.m_data), m_sign(a.m_data ? !a.m_sign : false) {}
807 // Helper functions for getting at our internal data, and manipulating storage:
809 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; }
810 BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
811 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; }
812 BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; }
813 BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT
816 // Check for zero value:
817 if(m_sign && !m_data)
822 BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size)
824 detail::verify_new_size(2, min_size, checked_type());
826 BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
829 m_sign = false; // zero is always unsigned
830 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
834 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0), m_sign(false) {}
835 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
836 : m_data(o.m_data), m_sign(o.m_sign) {}
837 //~cpp_int_base() BOOST_NOEXCEPT {}
838 BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
843 BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
846 // Check for zero value:
852 BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
856 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
858 std::swap(m_sign, o.m_sign);
859 std::swap(m_data, o.m_data);
863 // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
865 template <unsigned MinBits, cpp_int_check_type Checked>
866 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
868 typedef typename trivial_limb_type<MinBits>::type local_limb_type;
869 typedef local_limb_type* limb_pointer;
870 typedef const local_limb_type* const_limb_pointer;
872 BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
873 BOOST_STATIC_CONSTANT(local_limb_type, limb_mask = limb_bits != MinBits ?
874 static_cast<local_limb_type>(static_cast<local_limb_type>(~local_limb_type(0)) >> (limb_bits - MinBits))
875 : static_cast<local_limb_type>(~local_limb_type(0)));
877 local_limb_type m_data;
879 typedef mpl::int_<Checked> checked_type;
882 // Interface invariants:
884 BOOST_STATIC_ASSERT_MSG(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?");
887 typename boost::disable_if_c<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits)>::type
888 check_in_range(T val, const mpl::int_<checked>&, const boost::false_type&)
890 typedef typename common_type<T, local_limb_type>::type common_type;
892 if(static_cast<common_type>(val) > limb_mask)
893 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
896 void check_in_range(T val, const mpl::int_<checked>&, const boost::true_type&)
898 typedef typename common_type<T, local_limb_type>::type common_type;
900 if(static_cast<common_type>(val) > limb_mask)
901 BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
903 BOOST_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
905 template <class T, int C, bool B>
906 BOOST_MP_FORCEINLINE void check_in_range(T, const mpl::int_<C>&, const boost::integral_constant<bool, B>&) BOOST_NOEXCEPT {}
909 BOOST_MP_FORCEINLINE void check_in_range(T val) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type(), is_signed<T>())))
911 check_in_range(val, checked_type(), is_signed<T>());
916 // Direct construction:
918 #ifdef __MSVC_RUNTIME_CHECKS
920 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT
921 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask)) {}
923 BOOST_MP_FORCEINLINE cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == checked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
924 : 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); }
926 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT
927 : m_data(static_cast<local_limb_type>(i & limb_mask)) {}
929 BOOST_MP_FORCEINLINE cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == checked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
930 : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
933 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT
934 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask) {}
936 BOOST_MP_FORCEINLINE cpp_int_base(SI i, typename boost::enable_if_c<is_signed<SI>::value && (Checked == checked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
937 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
939 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT
940 : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
942 BOOST_MP_FORCEINLINE cpp_int_base(UI i, typename boost::enable_if_c<is_unsigned<UI>::value && (Checked == checked) >::type const* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
943 : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
946 BOOST_MP_FORCEINLINE cpp_int_base(F i, typename boost::enable_if<is_floating_point<F> >::type const* = 0) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
947 : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask)
953 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
954 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
955 : m_data(static_cast<local_limb_type>(0u)) {}
956 template <limb_type a>
957 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>) BOOST_NOEXCEPT
958 : m_data(static_cast<local_limb_type>(a)) {}
959 template <limb_type a, limb_type b>
960 BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>) BOOST_NOEXCEPT
961 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
964 // Helper functions for getting at our internal data, and manipulating storage:
966 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; }
967 BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
968 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; }
969 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; }
970 BOOST_MP_FORCEINLINE void sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
975 BOOST_MP_FORCEINLINE void resize(unsigned, unsigned min_size)
977 detail::verify_new_size(2, min_size, checked_type());
979 BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
981 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
985 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0) {}
986 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
987 : m_data(o.m_data) {}
988 //~cpp_int_base() BOOST_NOEXCEPT {}
989 BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
993 BOOST_MP_FORCEINLINE void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
995 if(Checked == checked)
997 BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1002 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT
1006 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
1008 std::swap(m_data, o.m_data);
1012 // Traits class, lets us know whether type T can be directly converted to the base type,
1013 // used to enable/disable constructors etc:
1015 template <class Arg, class Base>
1016 struct is_allowed_cpp_int_base_conversion : public mpl::if_c<
1017 is_same<Arg, limb_type>::value || is_same<Arg, signed_limb_type>::value
1018 #if defined(BOOST_LITTLE_ENDIAN) && !defined(BOOST_MP_TEST_NO_LE)
1019 || is_same<Arg, double_limb_type>::value || is_same<Arg, signed_double_limb_type>::value
1021 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
1022 || literals::detail::is_value_pack<Arg>::value
1024 || (is_trivial_cpp_int<Base>::value && is_arithmetic<Arg>::value),
1030 // Now the actual backend, normalising parameters passed to the base class:
1032 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1033 struct cpp_int_backend
1034 : public cpp_int_base<
1035 min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1036 max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1040 is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1042 typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> self_type;
1043 typedef cpp_int_base<
1044 min_precision<self_type>::value,
1045 max_precision<self_type>::value,
1049 is_trivial_cpp_int<self_type>::value> base_type;
1050 typedef mpl::bool_<is_trivial_cpp_int<self_type>::value> trivial_tag;
1052 typedef typename mpl::if_<
1055 signed char, short, int, long,
1056 boost::long_long_type, signed_double_limb_type>,
1057 mpl::list<signed_limb_type, signed_double_limb_type>
1058 >::type signed_types;
1059 typedef typename mpl::if_<
1061 mpl::list<unsigned char, unsigned short, unsigned,
1062 unsigned long, boost::ulong_long_type, double_limb_type>,
1063 mpl::list<limb_type, double_limb_type>
1064 >::type unsigned_types;
1065 typedef typename mpl::if_<
1067 mpl::list<float, double, long double>,
1068 mpl::list<long double>
1069 >::type float_types;
1070 typedef mpl::int_<Checked> checked_type;
1072 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend() BOOST_NOEXCEPT{}
1073 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(const cpp_int_backend& o) BOOST_MP_NOEXCEPT_IF(boost::is_void<Allocator>::value) : base_type(o) {}
1074 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1075 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(cpp_int_backend&& o) BOOST_NOEXCEPT
1076 : base_type(static_cast<base_type&&>(o)) {}
1079 // Direct construction from arithmetic type:
1081 template <class Arg>
1082 BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(Arg i, typename boost::enable_if_c<is_allowed_cpp_int_base_conversion<Arg, base_type>::value >::type const* = 0)BOOST_MP_NOEXCEPT_IF(noexcept(base_type(std::declval<Arg>())))
1086 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1087 void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::true_ const&, mpl::true_ const &)
1089 // Assigning trivial type to trivial type:
1090 this->check_in_range(*other.limbs());
1091 *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1092 this->sign(other.sign());
1095 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1096 void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::true_ const&, mpl::false_ const &)
1098 // non-trivial to trivial narrowing conversion:
1099 double_limb_type v = *other.limbs();
1100 if(other.size() > 1)
1102 v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1103 if((Checked == checked) && (other.size() > 2))
1105 BOOST_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1109 this->sign(other.sign());
1112 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1113 void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::false_ const&, mpl::true_ const &)
1115 // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards:
1116 *this = static_cast<
1117 typename boost::multiprecision::detail::canonical<
1118 typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1119 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>
1122 this->sign(other.sign());
1124 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1125 void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::false_ const&, mpl::false_ const &)
1127 // regular non-trivial to non-trivial assign:
1128 this->resize(other.size(), other.size());
1129 std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size(), this->size()) * sizeof(this->limbs()[0]));
1130 this->sign(other.sign());
1134 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1136 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1137 typename boost::enable_if_c<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = 0)
1142 mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1143 mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1145 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1146 explicit cpp_int_backend(
1147 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1148 typename boost::disable_if_c<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = 0)
1153 mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1154 mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1156 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1157 cpp_int_backend& operator=(
1158 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1162 mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1163 mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1166 #ifdef BOOST_MP_USER_DEFINED_LITERALS
1167 BOOST_CONSTEXPR cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1168 : base_type(static_cast<const base_type&>(a), tag){}
1171 BOOST_MP_FORCEINLINE cpp_int_backend& operator = (const cpp_int_backend& o) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().assign(std::declval<const cpp_int_backend&>())))
1176 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1177 BOOST_MP_FORCEINLINE cpp_int_backend& operator = (cpp_int_backend&& o) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<base_type&>() = std::declval<base_type>()))
1179 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1185 typename boost::enable_if<is_unsigned<A> >::type do_assign_arithmetic(A val, const mpl::true_&)
1186 BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1188 this->check_in_range(val);
1189 *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1193 typename boost::disable_if_c<is_unsigned<A>::value || !is_integral<A>::value >::type do_assign_arithmetic(A val, const mpl::true_&)
1194 BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1196 this->check_in_range(val);
1197 *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);
1198 this->sign(val < 0);
1202 typename boost::enable_if_c< !is_integral<A>::value>::type do_assign_arithmetic(A val, const mpl::true_&)
1204 this->check_in_range(val);
1205 *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);
1206 this->sign(val < 0);
1209 BOOST_MP_FORCEINLINE void do_assign_arithmetic(limb_type i, const mpl::false_&) BOOST_NOEXCEPT
1215 BOOST_MP_FORCEINLINE void do_assign_arithmetic(signed_limb_type i, const mpl::false_&) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().sign(true)))
1218 *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1221 void do_assign_arithmetic(double_limb_type i, const mpl::false_&) BOOST_NOEXCEPT
1223 BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type));
1224 BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2);
1225 typename base_type::limb_pointer p = this->limbs();
1226 #ifdef __MSVC_RUNTIME_CHECKS
1227 *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1229 *p = static_cast<limb_type>(i);
1231 p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1232 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1235 void do_assign_arithmetic(signed_double_limb_type i, const mpl::false_&) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().sign(true)))
1237 BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type));
1238 BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2);
1240 double_limb_type ui;
1243 ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1244 typename base_type::limb_pointer p = this->limbs();
1245 #ifdef __MSVC_RUNTIME_CHECKS
1246 *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1248 *p = static_cast<limb_type>(ui);
1250 p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1251 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1255 void do_assign_arithmetic(long double a, const mpl::false_&)
1257 using default_ops::eval_add;
1258 using default_ops::eval_subtract;
1265 do_assign_arithmetic(-a, mpl::false_());
1271 *this = static_cast<limb_type>(0u);
1275 *this = static_cast<limb_type>(1u);
1278 BOOST_ASSERT(!(boost::math::isinf)(a));
1279 BOOST_ASSERT(!(boost::math::isnan)(a));
1282 long double f, term;
1283 *this = static_cast<limb_type>(0u);
1287 static const limb_type shift = std::numeric_limits<limb_type>::digits;
1291 // extract int sized bits from f:
1292 f = ldexp(f, shift);
1295 eval_left_shift(*this, shift);
1297 eval_add(*this, static_cast<limb_type>(term));
1299 eval_subtract(*this, static_cast<limb_type>(-term));
1303 eval_left_shift(*this, e);
1305 eval_right_shift(*this, -e);
1308 template <class Arithmetic>
1309 BOOST_MP_FORCEINLINE typename boost::enable_if_c<!boost::multiprecision::detail::is_byte_container<Arithmetic>::value, cpp_int_backend&>::type operator = (Arithmetic val) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().do_assign_arithmetic(std::declval<Arithmetic>(), trivial_tag())))
1311 do_assign_arithmetic(val, trivial_tag());
1315 void do_assign_string(const char* s, const mpl::true_&)
1317 std::size_t n = s ? std::strlen(s) : 0;
1319 unsigned radix = 10;
1321 if(n && (*s == '-'))
1327 if(n && (*s == '0'))
1329 if((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1346 if(*s >= '0' && *s <= '9')
1348 else if(*s >= 'a' && *s <= 'f')
1349 val = 10 + *s - 'a';
1350 else if(*s >= 'A' && *s <= 'F')
1351 val = 10 + *s - 'A';
1356 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1358 *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1359 *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1366 void do_assign_string(const char* s, const mpl::false_&)
1368 using default_ops::eval_multiply;
1369 using default_ops::eval_add;
1370 std::size_t n = s ? std::strlen(s) : 0;
1371 *this = static_cast<limb_type>(0u);
1372 unsigned radix = 10;
1374 if(n && (*s == '-'))
1380 if(n && (*s == '0'))
1382 if((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1395 // Exception guarantee: create the result in stack variable "result"
1396 // then do a swap at the end. In the event of a throw, *this will
1397 // be left unchanged.
1399 cpp_int_backend result;
1404 while(*s == '0') ++s;
1405 std::size_t bitcount = 4 * std::strlen(s);
1407 std::size_t limb, shift;
1412 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1413 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1414 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1417 if(*s >= '0' && *s <= '9')
1419 else if(*s >= 'a' && *s <= 'f')
1420 val = 10 + *s - 'a';
1421 else if(*s >= 'A' && *s <= 'F')
1422 val = 10 + *s - 'A';
1425 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1427 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1428 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1430 if(result.size() > limb)
1432 result.limbs()[limb] |= val;
1441 while(*s == '0') ++s;
1442 std::size_t bitcount = 3 * std::strlen(s);
1444 std::size_t limb, shift;
1449 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1450 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1451 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1454 if(*s >= '0' && *s <= '7')
1458 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1460 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1461 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1462 if(result.size() > limb)
1464 result.limbs()[limb] |= (val << shift);
1465 if(shift > sizeof(limb_type) * CHAR_BIT - 3)
1467 // Deal with the bits in val that overflow into the next limb:
1468 val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1471 // If this is the most-significant-limb, we may need to allocate an extra one for the overflow:
1472 if(limb + 1 == newsize)
1473 result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1474 if(result.size() > limb + 1)
1476 result.limbs()[limb + 1] |= val;
1488 // Base 10, we extract blocks of size 10^9 at a time, that way
1489 // the number of multiplications is kept to a minimum:
1490 limb_type block_mult = max_block_10;
1493 limb_type block = 0;
1494 for(unsigned i = 0; i < digits_per_block_10; ++i)
1497 if(*s >= '0' && *s <= '9')
1500 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1505 block_mult = block_multiplier(i);
1509 eval_multiply(result, block_mult);
1510 eval_add(result, block);
1519 cpp_int_backend& operator = (const char* s)
1521 do_assign_string(s, trivial_tag());
1524 BOOST_MP_FORCEINLINE void swap(cpp_int_backend& o) BOOST_NOEXCEPT
1529 std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::false_&)const
1531 typedef typename mpl::if_c<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type io_type;
1532 if(this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1533 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1534 std::stringstream ss;
1535 ss.flags(f & ~std::ios_base::showpos);
1536 ss << static_cast<io_type>(*this->limbs());
1540 else if(f & std::ios_base::showpos)
1545 std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::true_&)const
1547 // Even though we have only one limb, we can't do IO on it :-(
1549 if((f & std::ios_base::oct) == std::ios_base::oct)
1551 else if((f & std::ios_base::hex) == std::ios_base::hex)
1555 unsigned Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1557 if(base == 8 || base == 16)
1560 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1561 limb_type shift = base == 8 ? 3 : 4;
1562 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1563 typename base_type::local_limb_type v = *this->limbs();
1564 result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1565 std::string::difference_type pos = result.size() - 1;
1566 for(unsigned i = 0; i < Bits / shift; ++i)
1568 char c = '0' + static_cast<char>(v & mask);
1576 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1577 char c = '0' + static_cast<char>(v & mask);
1583 // Get rid of leading zeros:
1585 std::string::size_type n = result.find_first_not_of('0');
1586 if(!result.empty() && (n == std::string::npos))
1587 n = result.size() - 1;
1589 if(f & std::ios_base::showbase)
1591 const char* pp = base == 8 ? "0" : "0x";
1592 result.insert(static_cast<std::string::size_type>(0), pp);
1597 result.assign(Bits / 3 + 1, '0');
1598 std::string::difference_type pos = result.size() - 1;
1599 typename base_type::local_limb_type v(*this->limbs());
1607 result[pos] = (v % 10) + '0';
1611 std::string::size_type n = result.find_first_not_of('0');
1616 result.insert(static_cast<std::string::size_type>(0), 1, '-');
1617 else if(f & std::ios_base::showpos)
1618 result.insert(static_cast<std::string::size_type>(0), 1, '+');
1622 std::string do_get_string(std::ios_base::fmtflags f, const mpl::true_&)const
1624 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
1625 return do_get_trivial_string(f, mpl::bool_<is_same<typename base_type::local_limb_type, double_limb_type>::value>());
1627 return do_get_trivial_string(f, mpl::bool_<false>());
1630 std::string do_get_string(std::ios_base::fmtflags f, const mpl::false_&)const
1632 using default_ops::eval_get_sign;
1634 if((f & std::ios_base::oct) == std::ios_base::oct)
1636 else if((f & std::ios_base::hex) == std::ios_base::hex)
1640 unsigned Bits = this->size() * base_type::limb_bits;
1642 if(base == 8 || base == 16)
1645 BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1646 limb_type shift = base == 8 ? 3 : 4;
1647 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1648 cpp_int_backend t(*this);
1649 result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
1650 std::string::difference_type pos = result.size() - 1;
1651 for(unsigned i = 0; i < Bits / shift; ++i)
1653 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1657 eval_right_shift(t, shift);
1661 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1662 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1668 // Get rid of leading zeros:
1670 std::string::size_type n = result.find_first_not_of('0');
1671 if(!result.empty() && (n == std::string::npos))
1672 n = result.size() - 1;
1674 if(f & std::ios_base::showbase)
1676 const char* pp = base == 8 ? "0" : "0x";
1677 result.insert(static_cast<std::string::size_type>(0), pp);
1682 result.assign(Bits / 3 + 1, '0');
1683 std::string::difference_type pos = result.size() - 1;
1684 cpp_int_backend t(*this);
1692 if(this->size() == 1)
1694 result = boost::lexical_cast<std::string>(t.limbs()[0]);
1698 cpp_int_backend block10;
1699 block10 = max_block_10;
1700 while(eval_get_sign(t) != 0)
1703 divide_unsigned_helper(&t2, t, block10, r);
1705 limb_type v = r.limbs()[0];
1706 for(unsigned i = 0; i < digits_per_block_10; ++i)
1708 char c = '0' + v % 10;
1716 std::string::size_type n = result.find_first_not_of('0');
1721 result.insert(static_cast<std::string::size_type>(0), 1, '-');
1722 else if(f & std::ios_base::showpos)
1723 result.insert(static_cast<std::string::size_type>(0), 1, '+');
1728 std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f)const
1730 return do_get_string(f, trivial_tag());
1733 template <class Container>
1734 void construct_from_container(const Container& c, const mpl::false_&)
1737 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
1739 unsigned newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
1740 if(c.size() % sizeof(limb_type))
1746 this->resize(newsize, newsize); // May throw
1747 std::memset(this->limbs(), 0, this->size());
1748 typename Container::const_iterator i(c.begin()), j(c.end());
1749 unsigned byte_location = static_cast<unsigned>(c.size() - 1);
1752 unsigned limb = byte_location / sizeof(limb_type);
1753 unsigned shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
1754 if(this->size() > limb)
1755 this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
1761 template <class Container>
1762 void construct_from_container(const Container& c, const mpl::true_&)
1765 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
1767 typedef typename base_type::local_limb_type local_limb_type;
1771 typename Container::const_iterator i(c.begin()), j(c.end());
1772 unsigned byte_location = static_cast<unsigned>(c.size() - 1);
1775 unsigned limb = byte_location / sizeof(local_limb_type);
1776 unsigned shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
1778 this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
1785 template <class Container>
1786 cpp_int_backend(const Container& c, typename boost::enable_if_c<boost::multiprecision::detail::is_byte_container<Container>::value>::type const* = 0)
1789 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
1791 construct_from_container(c, trivial_tag());
1793 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1794 int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::false_&, const mpl::false_&)const BOOST_NOEXCEPT
1796 if(this->sign() != o.sign())
1797 return this->sign() ? -1 : 1;
1799 // Only do the compare if the same sign:
1800 int result = compare_unsigned(o);
1806 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1807 int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::true_&, const mpl::false_&)const
1809 cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
1810 return t.compare(o);
1812 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1813 int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::false_&, const mpl::true_&)const
1815 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
1818 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1819 int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::true_&, const mpl::true_&)const BOOST_NOEXCEPT
1825 return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
1834 return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
1837 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1838 int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o)const BOOST_NOEXCEPT
1840 typedef mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> t1;
1841 typedef mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value> t2;
1842 return compare_imp(o, t1(), t2());
1844 template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1845 int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o)const BOOST_NOEXCEPT
1847 if(this->size() != o.size())
1849 return this->size() > o.size() ? 1 : -1;
1851 typename base_type::const_limb_pointer pa = this->limbs();
1852 typename base_type::const_limb_pointer pb = o.limbs();
1853 for(int i = this->size() - 1; i >= 0; --i)
1856 return pa[i] > pb[i] ? 1 : -1;
1860 template <class Arithmetic>
1861 BOOST_MP_FORCEINLINE typename boost::enable_if<is_arithmetic<Arithmetic>, int>::type compare(Arithmetic i)const
1863 // braindead version:
1870 } // namespace backends
1872 namespace default_ops{
1874 template <class Backend>
1875 struct double_precision_type;
1877 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1878 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
1880 typedef typename mpl::if_c<
1881 backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1882 backends::cpp_int_backend<
1883 (is_void<Allocator>::value ?
1884 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
1886 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1890 backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>
1897 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
1898 struct expression_template_default<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
1900 static const expression_template_option value = et_off;
1903 using boost::multiprecision::backends::cpp_int_backend;
1905 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1906 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public mpl::int_<number_kind_integer>{};
1908 typedef number<cpp_int_backend<> > cpp_int;
1909 typedef rational_adaptor<cpp_int_backend<> > cpp_rational_backend;
1910 typedef number<cpp_rational_backend> cpp_rational;
1912 // Fixed precision unsigned types:
1913 typedef number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > uint128_t;
1914 typedef number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > uint256_t;
1915 typedef number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > uint512_t;
1916 typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> > uint1024_t;
1918 // Fixed precision signed types:
1919 typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> > int128_t;
1920 typedef number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> > int256_t;
1921 typedef number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> > int512_t;
1922 typedef number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> > int1024_t;
1924 // Over again, but with checking enabled this time:
1925 typedef number<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_int;
1926 typedef rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_rational_backend;
1927 typedef number<checked_cpp_rational_backend> checked_cpp_rational;
1928 // Fixed precision unsigned types:
1929 typedef number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> > checked_uint128_t;
1930 typedef number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> > checked_uint256_t;
1931 typedef number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> > checked_uint512_t;
1932 typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> > checked_uint1024_t;
1934 // Fixed precision signed types:
1935 typedef number<cpp_int_backend<128, 128, signed_magnitude, checked, void> > checked_int128_t;
1936 typedef number<cpp_int_backend<256, 256, signed_magnitude, checked, void> > checked_int256_t;
1937 typedef number<cpp_int_backend<512, 512, signed_magnitude, checked, void> > checked_int512_t;
1938 typedef number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> > checked_int1024_t;
1940 #ifdef BOOST_NO_SFINAE_EXPR
1944 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>
1945 struct is_explicitly_convertible<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> > : public mpl::true_ {};
1951 #pragma warning(pop)
1957 // Last of all we include the implementations of all the eval_* non member functions:
1959 #include <boost/multiprecision/cpp_int/comparison.hpp>
1960 #include <boost/multiprecision/cpp_int/add.hpp>
1961 #include <boost/multiprecision/cpp_int/multiply.hpp>
1962 #include <boost/multiprecision/cpp_int/divide.hpp>
1963 #include <boost/multiprecision/cpp_int/bitwise.hpp>
1964 #include <boost/multiprecision/cpp_int/misc.hpp>
1965 #include <boost/multiprecision/cpp_int/limits.hpp>
1966 #ifdef BOOST_MP_USER_DEFINED_LITERALS
1967 #include <boost/multiprecision/cpp_int/literals.hpp>
1969 #include <boost/multiprecision/cpp_int/serialize.hpp>
1970 #include <boost/multiprecision/cpp_int/import_export.hpp>