1 ////////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6 #ifndef BOOST_MP_CPP_INT_HPP
7 #define BOOST_MP_CPP_INT_HPP
13 #include <type_traits>
15 #include <boost/multiprecision/detail/standalone_config.hpp>
16 #include <boost/multiprecision/detail/endian.hpp>
17 #include <boost/multiprecision/number.hpp>
18 #include <boost/multiprecision/detail/integer_ops.hpp>
19 #include <boost/multiprecision/detail/rebind.hpp>
20 #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
21 #include <boost/multiprecision/rational_adaptor.hpp>
22 #include <boost/multiprecision/traits/is_byte_container.hpp>
23 #include <boost/multiprecision/cpp_int/checked.hpp>
24 #include <boost/multiprecision/detail/constexpr.hpp>
25 #include <boost/multiprecision/detail/float128_functions.hpp>
26 #include <boost/multiprecision/cpp_int/value_pack.hpp>
27 #include <boost/multiprecision/detail/empty_value.hpp>
28 #include <boost/multiprecision/detail/no_exceptions_support.hpp>
29 #include <boost/multiprecision/detail/assert.hpp>
30 #include <boost/multiprecision/detail/fpclassify.hpp>
33 namespace multiprecision {
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)
42 #if defined(__GNUC__) && !defined(__clang__)
43 // see https://github.com/boostorg/multiprecision/issues/413
44 // and https://github.com/boostorg/multiprecision/issues/431
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
49 template <std::size_t MinBits = 0, std::size_t 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>
50 struct cpp_int_backend;
52 } // namespace backends
56 template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
57 struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::false_type
65 template <std::size_t Value1, std::size_t Value2>
66 struct static_unsigned_max
68 static constexpr const std::size_t value = (Value1 > Value2) ? Value1 : Value2;
72 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
75 // Traits class determines the maximum and minimum precision values:
80 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
81 struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
83 static constexpr const std::size_t value = std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value
84 : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : SIZE_MAX);
90 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
91 struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
93 static constexpr const std::size_t value = (std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value : MinBits);
96 // Traits class determines whether the number of bits precision requested could fit in a native type,
97 // we call this a "trivial" cpp_int:
100 struct is_trivial_cpp_int
102 static constexpr const bool value = false;
105 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
106 struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
108 using self = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
109 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));
112 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
113 struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
115 static constexpr const bool value = true;
118 } // namespace backends
120 // Traits class to determine whether a cpp_int_backend is signed or not:
122 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
123 struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
124 : public std::integral_constant<bool, (SignType == unsigned_magnitude) || (SignType == unsigned_packed)>
129 // Traits class determines whether T should be implicitly convertible to U, or
130 // whether the constructor should be made explicit. The latter happens if we
131 // are losing the sign, or have fewer digits precision in the target type:
133 template <class T, class U>
134 struct is_implicit_cpp_int_conversion;
136 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
137 struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
139 using t1 = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> ;
140 using t2 = cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>;
141 static constexpr const bool value =
142 (is_signed_number<t2>::value || !is_signed_number<t1>::value) && (max_precision<t1>::value <= max_precision<t2>::value);
146 // Traits class to determine whether operations on a cpp_int may throw:
149 struct is_non_throwing_cpp_int : public std::integral_constant<bool, false>
151 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType>
152 struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public std::integral_constant<bool, true>
156 // Traits class, determines whether the cpp_int is fixed precision or not:
159 struct is_fixed_precision;
160 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
161 struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
162 : public std::integral_constant<bool, max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != SIZE_MAX>
167 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(std::size_t new_size, std::size_t min_size, const std::integral_constant<int, checked>&)
169 if (new_size < min_size)
170 BOOST_MP_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
172 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(std::size_t /*new_size*/, std::size_t /*min_size*/, const std::integral_constant<int, unchecked>&) {}
175 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool b, U limb, U mask, const std::integral_constant<int, checked>&)
177 // When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error:
178 if (b && (limb & ~mask))
179 BOOST_MP_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."));
182 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const std::integral_constant<int, unchecked>&) {}
184 } // namespace detail
187 // Now define the various data layouts that are possible as partial specializations of the base class,
188 // starting with the default arbitrary precision signed integer type:
190 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
191 struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>
192 : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
194 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, bool trivial2>
195 friend struct cpp_int_base;
197 using allocator_type = typename detail::rebind<limb_type, Allocator>::type;
198 using limb_pointer = typename std::allocator_traits<allocator_type>::pointer ;
199 using const_limb_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
200 using checked_type = std::integral_constant<int, Checked>;
203 // Interface invariants:
205 static_assert(!std::is_void<Allocator>::value, "Allocator must not be void here");
207 using base_type = boost::multiprecision::detail::empty_value<allocator_type>;
212 std::size_t capacity;
217 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
218 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
219 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
220 static constexpr std::size_t internal_limb_count =
222 ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
223 : (sizeof(limb_data) / sizeof(limb_type)) > 1 ? (sizeof(limb_data) / sizeof(limb_type)) : 2;
228 limb_type la[internal_limb_count];
230 double_limb_type double_first;
232 constexpr data_type() noexcept : first(0) {}
233 constexpr data_type(limb_type i) noexcept : first(i) {}
234 constexpr data_type(signed_limb_type i) noexcept : first(i < 0 ? static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
235 #if BOOST_MP_ENDIAN_LITTLE_BYTE
236 constexpr data_type(double_limb_type i) noexcept : double_first(i)
238 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) {}
240 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !(defined(BOOST_MSVC) && (BOOST_MSVC < 1900))
241 constexpr data_type(limb_type* limbs, std::size_t len) noexcept : ld{ len, limbs }
244 constexpr data_type(limb_type* limbs, std::size_t len) noexcept
254 bool m_sign, m_internal, m_alias;
258 // Direct construction:
260 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
266 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
272 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
273 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
275 m_limbs(i > max_limb_value ? 2 : 1),
280 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
282 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)),
288 // Aliasing constructor aliases data:
290 struct scoped_shared_storage : private boost::multiprecision::detail::empty_value<allocator_type>
294 std::size_t capacity;
295 std::size_t allocated;
297 allocator_type& allocator() noexcept { return boost::multiprecision::detail::empty_value<allocator_type>::get(); }
300 scoped_shared_storage(const allocator_type& a, std::size_t len)
301 : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), a), capacity(len), allocated(0), is_alias(false)
303 data = allocator().allocate(len);
305 scoped_shared_storage(const cpp_int_base& i, std::size_t len)
306 : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), i.allocator()), capacity(len), allocated(0), is_alias(false)
308 data = allocator().allocate(len);
310 scoped_shared_storage(limb_type* limbs, std::size_t n) : data(limbs), capacity(n), allocated(0), is_alias(true) {}
311 ~scoped_shared_storage()
314 allocator().deallocate(data, capacity);
316 limb_type* allocate(std::size_t n) noexcept
318 limb_type* result = data + allocated;
320 BOOST_MP_ASSERT(allocated <= capacity);
323 void deallocate(std::size_t n)
325 BOOST_MP_ASSERT(n <= allocated);
329 explicit constexpr cpp_int_base(limb_type* data, std::size_t offset, std::size_t len) noexcept
330 : m_data(data + offset, len),
335 // This next constructor is for constructing const objects from const limb_type*'s only.
336 // Unfortunately we appear to have no way to assert that within the language, and the const_cast
337 // is a side effect of that :(
338 explicit constexpr cpp_int_base(const limb_type* data, std::size_t offset, std::size_t len) noexcept
339 : m_data(const_cast<limb_type*>(data) + offset, len),
344 explicit cpp_int_base(scoped_shared_storage& data, std::size_t len) noexcept
345 : m_data(data.allocate(len), len),
351 // Helper functions for getting at our internal data, and manipulating storage:
353 BOOST_MP_FORCEINLINE allocator_type& allocator() noexcept { return base_type::get(); }
354 BOOST_MP_FORCEINLINE const allocator_type& allocator() const noexcept { return base_type::get(); }
355 BOOST_MP_FORCEINLINE std::size_t size() const noexcept { return m_limbs; }
356 BOOST_MP_FORCEINLINE limb_pointer limbs() noexcept { return m_internal ? m_data.la : m_data.ld.data; }
357 BOOST_MP_FORCEINLINE const_limb_pointer limbs() const noexcept { return m_internal ? m_data.la : m_data.ld.data; }
358 BOOST_MP_FORCEINLINE std::size_t capacity() const noexcept { return m_internal ? internal_limb_count : m_data.ld.capacity; }
359 BOOST_MP_FORCEINLINE bool sign() const noexcept { return m_sign; }
360 void sign(bool b) noexcept
363 // Check for zero value:
364 if (m_sign && (m_limbs == 1))
370 void resize(std::size_t new_size, std::size_t min_size)
372 constexpr const std::size_t max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
373 // We never resize beyond MaxSize:
374 if (new_size > max_limbs)
375 new_size = max_limbs;
376 detail::verify_new_size(new_size, min_size, checked_type());
377 // See if we have enough capacity already:
378 std::size_t cap = capacity();
381 // We must not be an alias, memory allocation here defeats the whole point of aliasing:
382 BOOST_MP_ASSERT(!m_alias);
383 // Allocate a new buffer and copy everything over:
384 cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
385 limb_pointer pl = allocator().allocate(cap);
386 std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
387 if (!m_internal && !m_alias)
388 allocator().deallocate(limbs(), capacity());
392 m_data.ld.capacity = cap;
400 BOOST_MP_FORCEINLINE void normalize() noexcept
402 limb_pointer p = limbs();
403 while ((m_limbs - 1) && !p[m_limbs - 1])
406 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(), m_limbs(1), m_sign(false), m_internal(true), m_alias(false){}
407 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)
411 m_data.ld = o.m_data.ld;
415 resize(o.size(), o.size());
416 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
420 cpp_int_base(cpp_int_base&& o)
421 : 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)
425 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
429 m_data.ld = o.m_data.ld;
434 cpp_int_base& operator=(cpp_int_base&& o) noexcept
436 if (!m_internal && !m_alias)
437 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
438 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
441 m_internal = o.m_internal;
445 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
449 m_data.ld = o.m_data.ld;
455 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_int_check_type Checked2>
456 cpp_int_base& operator=(cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>&& o) noexcept
461 this->resize(o.size(), o.size());
462 std::memcpy(this->limbs(), o.limbs(), o.size() * sizeof(*(o.limbs())));
465 if (!m_internal && !m_alias)
466 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
467 *static_cast<base_type*>(this) = static_cast<typename cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>::base_type&&>(o);
470 m_internal = o.m_internal;
472 m_data.ld.capacity = o.m_data.ld.capacity;
473 m_data.ld.data = o.limbs();
478 BOOST_MP_FORCEINLINE ~cpp_int_base() noexcept
480 if (!m_internal && !m_alias)
481 allocator().deallocate(limbs(), capacity());
483 void assign(const cpp_int_base& o)
487 static_cast<base_type&>(*this) = static_cast<const base_type&>(o);
489 resize(o.size(), o.size());
490 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
494 BOOST_MP_FORCEINLINE void negate() noexcept
497 // Check for zero value:
498 if (m_sign && (m_limbs == 1))
504 BOOST_MP_FORCEINLINE bool isneg() const noexcept
508 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) noexcept
510 std::swap(m_data, o.m_data);
511 std::swap(m_sign, o.m_sign);
512 std::swap(m_internal, o.m_internal);
513 std::swap(m_limbs, o.m_limbs);
514 std::swap(m_alias, o.m_alias);
519 void check_in_range(const A&) noexcept {}
522 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
523 const std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
524 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
525 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
526 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
527 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
528 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
529 const std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
531 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
532 struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false>
533 : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
536 // There is currently no support for unsigned arbitrary precision arithmetic, largely
537 // because it's not clear what subtraction should do:
539 static_assert(((sizeof(Allocator) == 0) && !std::is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
542 // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
544 template <std::size_t MinBits, cpp_int_check_type Checked>
545 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
547 using limb_pointer = limb_type* ;
548 using const_limb_pointer = const limb_type* ;
549 using checked_type = std::integral_constant<int, Checked>;
551 struct scoped_shared_storage
553 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
554 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
558 // Interface invariants:
560 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?");
563 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
564 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
565 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
566 static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
567 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
568 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
573 limb_type m_data[internal_limb_count];
574 limb_type m_first_limb;
575 double_limb_type m_double_first_limb;
577 constexpr data_type()
580 constexpr data_type(limb_type i)
583 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
584 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
587 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
589 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
590 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
592 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
597 template <limb_type... VALUES>
598 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
601 std::uint16_t m_limbs;
606 // Direct construction:
608 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
612 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
613 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)),
616 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
617 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
619 m_limbs(i > max_limb_value ? 2 : 1),
622 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
623 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
624 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)),
627 template <limb_type... VALUES>
628 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
629 : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false)
631 constexpr cpp_int_base(literals::detail::value_pack<> i)
632 : m_wrapper(i), m_limbs(1), m_sign(false) {}
633 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
634 : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
635 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(0), m_sign(false)
638 // These are deprecated in C++20 unless we make them explicit:
640 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
642 // Helper functions for getting at our internal data, and manipulating storage:
644 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
645 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
646 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
647 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return m_sign; }
648 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
651 // Check for zero value:
652 if (m_sign && (m_limbs == 1))
658 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
660 m_limbs = static_cast<std::uint16_t>((std::min)(new_size, internal_limb_count));
661 detail::verify_new_size(m_limbs, min_size, checked_type());
663 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
665 limb_pointer p = limbs();
666 detail::verify_limb_mask(m_limbs == internal_limb_count, p[m_limbs - 1], upper_limb_mask, checked_type());
667 p[internal_limb_count - 1] &= upper_limb_mask;
668 while ((m_limbs - 1) && !p[m_limbs - 1])
670 if ((m_limbs == 1) && (!*p))
671 m_sign = false; // zero is always unsigned
674 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
675 // Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
676 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
677 : m_wrapper(o.m_wrapper),
680 // Defaulted functions:
681 //~cpp_int_base() noexcept {}
683 void BOOST_MP_CXX14_CONSTEXPR assign(const cpp_int_base& o) noexcept
688 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
689 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
691 for (std::size_t i = 0; i < m_limbs; ++i)
692 limbs()[i] = o.limbs()[i];
696 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
700 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
703 // Check for zero value:
704 if (m_sign && (m_limbs == 1))
710 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
714 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
716 for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
717 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
718 std_constexpr::swap(m_sign, o.m_sign);
719 std_constexpr::swap(m_limbs, o.m_limbs);
724 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
727 template <std::size_t MinBits, cpp_int_check_type Checked>
728 const std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
729 template <std::size_t MinBits, cpp_int_check_type Checked>
730 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
731 template <std::size_t MinBits, cpp_int_check_type Checked>
732 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
733 template <std::size_t MinBits, cpp_int_check_type Checked>
734 const std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
736 // Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
738 template <std::size_t MinBits, cpp_int_check_type Checked>
739 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
741 using limb_pointer = limb_type* ;
742 using const_limb_pointer = const limb_type* ;
743 using checked_type = std::integral_constant<int, Checked>;
745 struct scoped_shared_storage
747 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
748 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
751 // Interface invariants:
753 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?");
756 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
757 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
758 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
759 static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
760 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
761 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
766 limb_type m_data[internal_limb_count];
767 limb_type m_first_limb;
768 double_limb_type m_double_first_limb;
770 constexpr data_type()
773 constexpr data_type(limb_type i)
776 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
777 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
780 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
782 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
783 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
785 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
790 template <limb_type... VALUES>
791 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
798 // Direct construction:
800 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
803 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_limb_type i) noexcept((Checked == unchecked))
804 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)), m_limbs(1)
809 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
810 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
812 m_limbs(i > max_limb_value ? 2 : 1)
814 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_double_limb_type i) noexcept((Checked == unchecked))
815 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
816 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))
822 template <limb_type... VALUES>
823 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
824 : m_wrapper(i), m_limbs(sizeof...(VALUES))
826 constexpr cpp_int_base(literals::detail::value_pack<>)
827 : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
828 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(1)
831 // Helper functions for getting at our internal data, and manipulating storage:
833 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
834 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
835 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
836 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
837 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
842 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
844 m_limbs = (std::min)(new_size, internal_limb_count);
845 detail::verify_new_size(m_limbs, min_size, checked_type());
847 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
849 limb_pointer p = limbs();
850 detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
851 p[internal_limb_count - 1] &= upper_limb_mask;
852 while ((m_limbs - 1) && !p[m_limbs - 1])
856 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept
857 : m_wrapper(limb_type(0u)),
859 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
860 : m_wrapper(o.m_wrapper),
861 m_limbs(o.m_limbs) {}
862 // Defaulted functions:
863 //~cpp_int_base() noexcept {}
865 // These are deprecated in C++20 unless we make them explicit:
867 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
869 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
874 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
875 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
877 for (std::size_t i = 0; i < m_limbs; ++i)
878 limbs()[i] = o.limbs()[i];
882 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
887 void check_negate(const std::integral_constant<int, checked>&)
889 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
891 BOOST_MP_CXX14_CONSTEXPR void check_negate(const std::integral_constant<int, unchecked>&) {}
894 BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
896 // Not so much a negate as a complement - this gets called when subtraction
897 // would result in a "negative" number:
898 if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
899 return; // negating zero is always zero, and always OK.
900 check_negate(checked_type());
901 std::size_t i = m_limbs;
902 for (; i < internal_limb_count; ++i)
903 m_wrapper.m_data[i] = 0;
904 m_limbs = internal_limb_count;
905 for (i = 0; i < internal_limb_count; ++i)
906 m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
908 eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
910 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
914 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
916 for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
917 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
918 std_constexpr::swap(m_limbs, o.m_limbs);
923 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
926 template <std::size_t MinBits, cpp_int_check_type Checked>
927 const std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
928 template <std::size_t MinBits, cpp_int_check_type Checked>
929 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
930 template <std::size_t MinBits, cpp_int_check_type Checked>
931 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
932 template <std::size_t MinBits, cpp_int_check_type Checked>
933 const std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
935 // Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
936 // because some platforms have native integer types longer than long long, "really long long" anyone??
938 template <unsigned N, bool s>
939 struct trivial_limb_type_imp
941 using type = double_limb_type;
944 template <unsigned N>
945 struct trivial_limb_type_imp<N, true>
947 using type = typename boost::multiprecision::detail::uint_t<N>::least;
950 template <unsigned N>
951 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(long long) * CHAR_BIT>
954 // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
956 template <std::size_t MinBits, cpp_int_check_type Checked>
957 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
959 using local_limb_type = typename trivial_limb_type<MinBits>::type;
960 using limb_pointer = local_limb_type*;
961 using const_limb_pointer = const local_limb_type*;
962 using checked_type = std::integral_constant<int, Checked>;
964 struct scoped_shared_storage
966 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
967 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
971 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
972 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));
975 local_limb_type m_data;
979 // Interface invariants:
981 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?");
985 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 <= static_cast<int>(MinBits))))>::type
986 check_in_range(T val, const std::integral_constant<int, checked>&)
988 using common_type = typename std::common_type<typename boost::multiprecision::detail::make_unsigned<T>::type, local_limb_type>::type;
990 if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
991 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
994 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 <= static_cast<int>(MinBits))))>::type
995 check_in_range(T val, const std::integral_constant<int, checked>&)
998 using common_type = typename std::common_type<T, local_limb_type>::type;
1000 if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
1001 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1003 template <class T, int C>
1004 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&) noexcept {}
1007 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())))
1009 check_in_range(val, checked_type());
1014 // Direct construction:
1017 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1018 : 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) {}
1020 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1021 : 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)
1026 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
1027 : m_data(static_cast<local_limb_type>(i) & limb_mask),
1030 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1031 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
1032 #if !(defined(__clang__) && defined(__MINGW32__))
1034 BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
1035 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask),
1038 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* = nullptr)
1039 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1042 // conversion from float to __int128 is broken on clang/mingw,
1043 // see: https://bugs.llvm.org/show_bug.cgi?id=48940
1044 // Since no floating point type has more than 64 bits of
1045 // precision, we can simply cast to an intermediate type to
1049 BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
1050 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask),
1053 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* = nullptr)
1054 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1057 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1058 : m_data(static_cast<local_limb_type>(0u)),
1061 template <limb_type a>
1062 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1063 : m_data(static_cast<local_limb_type>(a)),
1065 template <limb_type a, limb_type b>
1066 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1067 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
1069 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) noexcept
1071 m_sign(a.m_data ? !a.m_sign : false) {}
1073 // These are deprecated in C++20 unless we make them explicit:
1075 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1077 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0), m_sign(false)
1080 // Helper functions for getting at our internal data, and manipulating storage:
1082 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1083 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1084 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1085 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool sign() const noexcept { return m_sign; }
1086 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
1089 // Check for zero value:
1090 if (m_sign && !m_data)
1095 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t /* new_size */, std::size_t min_size)
1097 detail::verify_new_size(2, min_size, checked_type());
1099 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1102 m_sign = false; // zero is always unsigned
1103 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1104 m_data &= limb_mask;
1107 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0), m_sign(false) {}
1108 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1111 //~cpp_int_base() noexcept {}
1112 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1117 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
1120 // Check for zero value:
1126 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
1130 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1132 std_constexpr::swap(m_sign, o.m_sign);
1133 std_constexpr::swap(m_data, o.m_data);
1137 // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
1139 template <std::size_t MinBits, cpp_int_check_type Checked>
1140 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
1142 using local_limb_type = typename trivial_limb_type<MinBits>::type;
1143 using limb_pointer = local_limb_type* ;
1144 using const_limb_pointer = const local_limb_type* ;
1146 struct scoped_shared_storage
1148 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
1149 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
1153 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
1154 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))
1155 : static_cast<local_limb_type>(~local_limb_type(0));
1157 local_limb_type m_data;
1159 using checked_type = std::integral_constant<int, Checked>;
1162 // Interface invariants:
1164 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?");
1168 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits)))>::type
1169 check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, false>&)
1171 using common_type = typename std::common_type<T, local_limb_type>::type;
1173 if (static_cast<common_type>(val) > limb_mask)
1174 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1177 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, true>&)
1179 using common_type = typename std::common_type<T, local_limb_type>::type;
1181 if (static_cast<common_type>(val) > static_cast<common_type>(limb_mask))
1182 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1184 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
1186 template <class T, int C, bool B>
1187 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 {}
1190 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>())))
1192 check_in_range(val, checked_type(), boost::multiprecision::detail::is_signed<T>());
1197 // Direct construction:
1199 #ifdef __MSVC_RUNTIME_CHECKS
1201 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* = nullptr) noexcept
1202 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
1205 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1206 : 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); }
1208 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
1209 : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
1211 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1212 : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1215 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* = nullptr) noexcept
1216 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
1219 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
1220 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
1222 BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
1223 : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
1225 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* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
1226 : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
1229 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value >::type const* = nullptr) noexcept((Checked == unchecked))
1230 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask)
1236 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1237 : m_data(static_cast<local_limb_type>(0u))
1239 template <limb_type a>
1240 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1241 : m_data(static_cast<local_limb_type>(a)) {}
1242 template <limb_type a, limb_type b>
1243 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1244 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
1246 // These are deprecated in C++20 unless we make them explicit:
1248 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1250 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0)
1253 // Helper functions for getting at our internal data, and manipulating storage:
1255 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1256 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1257 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1258 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
1259 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
1264 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, std::size_t min_size)
1266 detail::verify_new_size(2, min_size, checked_type());
1268 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1270 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1271 m_data &= limb_mask;
1274 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0) {}
1275 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1276 : m_data(o.m_data) {}
1277 //~cpp_int_base() noexcept {}
1278 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1282 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate()
1283 #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
1284 noexcept((Checked == unchecked))
1287 BOOST_IF_CONSTEXPR(Checked == checked)
1289 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1294 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
1298 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1300 std_constexpr::swap(m_data, o.m_data);
1304 // Traits class, lets us know whether type T can be directly converted to the base type,
1305 // used to enable/disable constructors etc:
1307 template <class Arg, class Base>
1308 struct is_allowed_cpp_int_base_conversion : public std::conditional<
1309 std::is_same<Arg, limb_type>::value || std::is_same<Arg, signed_limb_type>::value
1310 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
1311 || std::is_same<Arg, double_limb_type>::value || std::is_same<Arg, signed_double_limb_type>::value
1313 || literals::detail::is_value_pack<Arg>::value || (is_trivial_cpp_int<Base>::value && boost::multiprecision::detail::is_arithmetic<Arg>::value),
1314 std::integral_constant<bool, true>,
1315 std::integral_constant<bool, false>>::type
1318 // Now the actual backend, normalising parameters passed to the base class:
1320 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1321 struct cpp_int_backend
1322 : public cpp_int_base<
1323 min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1324 max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1328 is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1330 using self_type = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
1331 using base_type = cpp_int_base<
1332 min_precision<self_type>::value,
1333 max_precision<self_type>::value,
1337 is_trivial_cpp_int<self_type>::value>;
1338 using trivial_tag = std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>;
1341 using signed_types = typename std::conditional<
1342 is_trivial_cpp_int<self_type>::value,
1344 signed char, short, int, long,
1345 long long, signed_double_limb_type>,
1346 std::tuple<signed_limb_type, signed_double_limb_type> >::type;
1347 using unsigned_types = typename std::conditional<
1348 is_trivial_cpp_int<self_type>::value,
1349 std::tuple<unsigned char, unsigned short, unsigned,
1350 unsigned long, unsigned long long, double_limb_type>,
1351 std::tuple<limb_type, double_limb_type> >::type;
1352 using float_types = typename std::conditional<
1353 is_trivial_cpp_int<self_type>::value,
1354 std::tuple<float, double, long double>,
1355 std::tuple<long double> >::type;
1356 using checked_type = std::integral_constant<int, Checked> ;
1358 BOOST_MP_FORCEINLINE constexpr cpp_int_backend() noexcept {}
1359 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(const cpp_int_backend& o) noexcept(std::is_void<Allocator>::value) : base_type(o) {}
1361 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(cpp_int_backend&& o) noexcept
1362 : base_type(static_cast<base_type&&>(o))
1364 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1365 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* = nullptr) noexcept
1367 *this = static_cast<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&&>(o);
1370 // Direct construction from arithmetic type:
1372 template <class Arg>
1373 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* = nullptr) noexcept(noexcept(base_type(std::declval<Arg>())))
1376 // Aliasing constructor: the result will alias the memory referenced, unless
1377 // we have fixed precision and storage, in which case we copy the memory:
1379 explicit constexpr cpp_int_backend(limb_type* data, std::size_t offset, std::size_t len) noexcept
1380 : base_type(data, offset, len) {}
1381 explicit cpp_int_backend(const limb_type* data, std::size_t offset, std::size_t len) noexcept
1382 : base_type(data, offset, len) { this->normalize(); }
1383 explicit constexpr cpp_int_backend(typename base_type::scoped_shared_storage& data, std::size_t len) noexcept
1384 : base_type(data, len) {}
1387 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1388 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::true_type const&, std::true_type const&)
1390 // Assigning trivial type to trivial type:
1391 this->check_in_range(*other.limbs());
1392 *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1393 this->sign(other.sign());
1396 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1397 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::true_type const&, std::false_type const&)
1399 // non-trivial to trivial narrowing conversion:
1400 double_limb_type v = *other.limbs();
1401 if (other.size() > 1)
1403 v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1404 BOOST_IF_CONSTEXPR(Checked == checked)
1406 if (other.size() > 2)
1408 BOOST_MP_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1413 this->sign(other.sign());
1416 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1417 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::false_type const&, std::true_type const&)
1419 // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards:
1420 *this = static_cast<
1421 typename boost::multiprecision::detail::canonical<
1422 typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1423 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
1424 this->sign(other.sign());
1426 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1427 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::false_type const&, std::false_type const&)
1429 // regular non-trivial to non-trivial assign:
1430 this->resize(other.size(), other.size());
1432 #if !defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
1433 std::size_t count = (std::min)(other.size(), this->size());
1434 for (std::size_t i = 0; i < count; ++i)
1435 this->limbs()[i] = other.limbs()[i];
1437 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1438 if (BOOST_MP_IS_CONST_EVALUATED(other.size()))
1440 std::size_t count = (std::min)(other.size(), this->size());
1441 for (std::size_t i = 0; i < count; ++i)
1442 this->limbs()[i] = other.limbs()[i];
1447 static_assert(sizeof(other.limbs()[0]) == sizeof(this->limbs()[0]), "This method requires equal limb sizes");
1448 std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size() * sizeof(other.limbs()[0]), this->size() * sizeof(this->limbs()[0])));
1451 this->sign(other.sign());
1456 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1457 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1458 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1459 typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = nullptr)
1464 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1465 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1467 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1468 explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1469 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1470 typename std::enable_if< !(is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value)>::type* = nullptr)
1475 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1476 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1478 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1479 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
1480 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1484 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1485 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1488 constexpr cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1489 : base_type(static_cast<const base_type&>(a), tag)
1492 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&>())))
1498 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>()))
1500 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1503 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1504 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
1506 *static_cast<base_type*>(this) = static_cast<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>::base_type&&>(o);
1511 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1512 boost::multiprecision::detail::is_unsigned<A>::value
1513 && trivial_tag::value, cpp_int_backend&>::type
1514 operator=(const A& val)
1515 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1517 this->check_in_range(val);
1518 *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1524 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1525 !(boost::multiprecision::detail::is_unsigned<A>::value || !boost::multiprecision::detail::is_integral<A>::value)
1526 && trivial_tag::value, cpp_int_backend&>::type
1527 operator=(const A& val)
1528 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1530 this->check_in_range(val);
1531 *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);
1532 this->sign(val < 0);
1537 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1538 std::is_convertible<A, limb_type>::value
1539 && !boost::multiprecision::detail::is_integral<A>::value
1540 && trivial_tag::value, cpp_int_backend&>::type
1541 operator=(const A& val)
1543 this->check_in_range(val);
1544 *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);
1545 this->sign(val < 0);
1550 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1551 std::is_same<A, limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1552 operator=(A i) noexcept
1560 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1561 std::is_same<A, signed_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1562 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1565 *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1570 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1571 std::is_same<A, double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1572 operator=(A i) noexcept
1574 static_assert(sizeof(i) == 2 * sizeof(limb_type), "Failed integer size check");
1575 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count");
1576 typename base_type::limb_pointer p = this->limbs();
1577 #ifdef __MSVC_RUNTIME_CHECKS
1578 *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1580 *p = static_cast<limb_type>(i);
1582 p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1583 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1588 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1589 std::is_same<A, signed_double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1590 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1592 static_assert(sizeof(i) == 2 * sizeof(limb_type), "double limb type size check failed");
1593 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count check");
1597 double_limb_type ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1598 typename base_type::limb_pointer p = this->limbs();
1599 #ifdef __MSVC_RUNTIME_CHECKS
1600 *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1602 *p = static_cast<limb_type>(ui);
1604 p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1605 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1611 BOOST_MP_CXX14_CONSTEXPR void do_assign_float(F a)
1613 using default_ops::eval_add;
1614 using default_ops::eval_subtract;
1615 BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
1619 do_assign_float(-a);
1626 *this = static_cast<limb_type>(0u);
1631 *this = static_cast<limb_type>(1u);
1634 if (!BOOST_MP_ISFINITE(a))
1636 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
1641 *this = static_cast<limb_type>(0u);
1645 #if !(defined(__clang__) && (__clang_major__ <= 7))
1646 constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
1648 // clang 7 has an issue converting long double to unsigned long long in
1649 // release mode (bits get dropped, conversion appears to go via float)
1650 // Never extract more than double bits at a time:
1651 constexpr limb_type shift = std::numeric_limits<limb_type>::digits > std::numeric_limits<double>::digits
1652 ? std::numeric_limits<double>::digits : std::numeric_limits<limb_type>::digits;
1657 // extract int sized bits from f:
1658 f = ldexp(f, shift);
1661 eval_left_shift(*this, shift);
1662 #if !(defined(__clang__) && (__clang_major__ <= 7))
1664 eval_add(*this, static_cast<limb_type>(term));
1666 eval_subtract(*this, static_cast<limb_type>(-term));
1668 // clang 7 requires extra cast to double to avoid buggy code generation:
1670 eval_add(*this, static_cast<limb_type>(static_cast<double>(term)));
1672 eval_subtract(*this, static_cast<limb_type>(static_cast<double>(-term)));
1677 eval_left_shift(*this, e);
1679 eval_right_shift(*this, -e);
1683 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1684 std::is_floating_point<A>::value && !trivial_tag::value, cpp_int_backend&>::type
1692 void do_assign_string(const char* s, const std::integral_constant<bool, true>&)
1694 std::size_t n = s ? std::strlen(s) : 0;
1696 unsigned radix = 10;
1698 if (n && (*s == '-'))
1704 if (n && (*s == '0'))
1706 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1723 if (*s >= '0' && *s <= '9')
1725 else if (*s >= 'a' && *s <= 'f')
1726 val = 10 + *s - 'a';
1727 else if (*s >= 'A' && *s <= 'F')
1728 val = 10 + *s - 'A';
1733 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1735 *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1736 *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1743 void do_assign_string(const char* s, const std::integral_constant<bool, false>&)
1745 using default_ops::eval_add;
1746 using default_ops::eval_multiply;
1747 std::size_t n = s ? std::strlen(s) : 0;
1748 *this = static_cast<limb_type>(0u);
1749 unsigned radix = 10;
1751 if (n && (*s == '-'))
1757 if (n && (*s == '0'))
1759 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1772 // Exception guarantee: create the result in stack variable "result"
1773 // then do a swap at the end. In the event of a throw, *this will
1774 // be left unchanged.
1776 cpp_int_backend result;
1783 std::size_t bitcount = 4 * std::strlen(s);
1785 std::size_t limb, shift;
1790 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1791 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1792 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1795 if (*s >= '0' && *s <= '9')
1797 else if (*s >= 'a' && *s <= 'f')
1798 val = 10 + *s - 'a';
1799 else if (*s >= 'A' && *s <= 'F')
1800 val = 10 + *s - 'A';
1803 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1805 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1806 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1808 if (result.size() > limb)
1810 result.limbs()[limb] |= val;
1817 else if (radix == 8)
1821 std::size_t bitcount = 3 * std::strlen(s);
1823 std::size_t limb, shift;
1828 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1829 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1830 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1833 if (*s >= '0' && *s <= '7')
1837 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1839 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1840 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1841 if (result.size() > limb)
1843 result.limbs()[limb] |= (val << shift);
1844 if (shift > sizeof(limb_type) * CHAR_BIT - 3)
1846 // Deal with the bits in val that overflow into the next limb:
1847 val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1850 // If this is the most-significant-limb, we may need to allocate an extra one for the overflow:
1851 if (limb + 1 == newsize)
1852 result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1853 if (result.size() > limb + 1)
1855 result.limbs()[limb + 1] |= val;
1867 // Base 10, we extract blocks of size 10^9 at a time, that way
1868 // the number of multiplications is kept to a minimum:
1869 limb_type block_mult = max_block_10;
1872 limb_type block = 0;
1873 for (unsigned i = 0; i < digits_per_block_10; ++i)
1876 if (*s >= '0' && *s <= '9')
1879 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1884 block_mult = block_multiplier(i);
1888 eval_multiply(result, block_mult);
1889 eval_add(result, block);
1899 cpp_int_backend& operator=(const char* s)
1901 do_assign_string(s, trivial_tag());
1904 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) noexcept
1910 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
1912 using io_type = typename std::conditional<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type;
1913 if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1914 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1915 std::stringstream ss;
1916 ss.flags(f & ~std::ios_base::showpos);
1917 ss << static_cast<io_type>(*this->limbs());
1921 else if (f & std::ios_base::showpos)
1926 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
1928 // Even though we have only one limb, we can't do IO on it :-(
1930 if ((f & std::ios_base::oct) == std::ios_base::oct)
1932 else if ((f & std::ios_base::hex) == std::ios_base::hex)
1936 std::size_t Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1938 if (base == 8 || base == 16)
1941 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1942 limb_type shift = base == 8 ? 3 : 4;
1943 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1944 typename base_type::local_limb_type v = *this->limbs();
1945 result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1946 std::string::difference_type pos = result.size() - 1;
1947 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1948 for (std::size_t i = 0; i < Bits / shift; ++i)
1950 char c = '0' + static_cast<char>(v & mask);
1952 c += letter_a - '9' - 1;
1958 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1959 char c = '0' + static_cast<char>(v & mask);
1961 c += letter_a - '9';
1965 // Get rid of leading zeros:
1967 std::string::size_type n = result.find_first_not_of('0');
1968 if (!result.empty() && (n == std::string::npos))
1969 n = result.size() - 1;
1971 if (f & std::ios_base::showbase)
1973 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
1974 result.insert(static_cast<std::string::size_type>(0), pp);
1979 result.assign(Bits / 3 + 1, '0');
1980 std::string::difference_type pos = result.size() - 1;
1981 typename base_type::local_limb_type v(*this->limbs());
1989 result[pos] = (v % 10) + '0';
1993 std::string::size_type n = result.find_first_not_of('0');
1998 result.insert(static_cast<std::string::size_type>(0), 1, '-');
1999 else if (f & std::ios_base::showpos)
2000 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2004 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
2006 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
2007 return do_get_trivial_string(f, std::integral_constant<bool, std::is_same<typename base_type::local_limb_type, double_limb_type>::value>());
2009 return do_get_trivial_string(f, std::integral_constant<bool, false>());
2012 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
2014 using default_ops::eval_get_sign;
2016 if ((f & std::ios_base::oct) == std::ios_base::oct)
2018 else if ((f & std::ios_base::hex) == std::ios_base::hex)
2022 std::size_t Bits = this->size() * base_type::limb_bits;
2024 if (base == 8 || base == 16)
2027 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
2028 limb_type shift = base == 8 ? 3 : 4;
2029 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
2030 cpp_int_backend t(*this);
2031 result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
2032 std::string::difference_type pos = result.size() - 1;
2033 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
2034 for (std::size_t i = 0; i < Bits / shift; ++i)
2036 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
2038 c += letter_a - '9' - 1;
2040 eval_right_shift(t, shift);
2044 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
2045 char c = '0' + static_cast<char>(t.limbs()[0] & mask);
2047 c += letter_a - '9';
2051 // Get rid of leading zeros:
2053 std::string::size_type n = result.find_first_not_of('0');
2054 if (!result.empty() && (n == std::string::npos))
2055 n = result.size() - 1;
2057 if (f & std::ios_base::showbase)
2059 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
2060 result.insert(static_cast<std::string::size_type>(0), pp);
2065 result.assign(Bits / 3 + 1, '0');
2066 std::string::difference_type pos = result.size() - 1;
2067 cpp_int_backend t(*this);
2075 if (this->size() == 1)
2077 result = std::to_string(t.limbs()[0]);
2081 cpp_int_backend block10;
2082 block10 = max_block_10;
2083 while (eval_get_sign(t) != 0)
2086 divide_unsigned_helper(&t2, t, block10, r);
2088 limb_type v = r.limbs()[0];
2089 for (std::size_t i = 0; i < digits_per_block_10; ++i)
2091 char c = '0' + v % 10;
2099 std::string::size_type n = result.find_first_not_of('0');
2104 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2105 else if (f & std::ios_base::showpos)
2106 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2112 std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
2114 return do_get_string(f, trivial_tag());
2118 template <class Container>
2119 void construct_from_container(const Container& c, const std::integral_constant<bool, false>&)
2122 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2124 std::size_t newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
2125 if (c.size() % sizeof(limb_type))
2131 this->resize(newsize, newsize); // May throw
2132 std::memset(this->limbs(), 0, this->size());
2133 typename Container::const_iterator i(c.begin()), j(c.end());
2134 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2137 std::size_t limb = byte_location / sizeof(limb_type);
2138 std::size_t shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
2139 if (this->size() > limb)
2140 this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2146 template <class Container>
2147 BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const std::integral_constant<bool, true>&)
2150 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2152 using local_limb_type = typename base_type::local_limb_type;
2156 typename Container::const_iterator i(c.begin()), j(c.end());
2157 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2160 std::size_t limb = byte_location / sizeof(local_limb_type);
2161 std::size_t shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
2163 this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2171 template <class Container>
2172 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(const Container& c, typename std::enable_if<boost::multiprecision::detail::is_byte_container<Container>::value>::type const* = nullptr)
2175 // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2177 construct_from_container(c, trivial_tag());
2179 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2180 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
2182 if (this->sign() != o.sign())
2183 return this->sign() ? -1 : 1;
2185 // Only do the compare if the same sign:
2186 int result = compare_unsigned(o);
2192 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2193 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
2195 cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
2196 return t.compare(o);
2198 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2199 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
2201 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
2204 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2205 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
2211 return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
2220 return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
2223 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2224 BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2226 using t1 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> ;
2227 using t2 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>;
2228 return compare_imp(o, t1(), t2());
2230 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2231 BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2233 if (this->size() != o.size())
2235 return this->size() > o.size() ? 1 : -1;
2237 typename base_type::const_limb_pointer pa = this->limbs();
2238 typename base_type::const_limb_pointer pb = o.limbs();
2239 for (std::ptrdiff_t i = this->size() - 1; i >= 0; --i)
2242 return pa[i] > pb[i] ? 1 : -1;
2246 template <class Arithmetic>
2247 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Arithmetic>::value, int>::type compare(Arithmetic i) const
2249 // braindead version:
2256 } // namespace backends
2258 namespace default_ops {
2260 template <class Backend>
2261 struct double_precision_type;
2263 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2264 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2266 using type = typename std::conditional<
2267 backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2268 backends::cpp_int_backend<
2269 (std::is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
2271 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2275 backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type;
2278 } // namespace default_ops
2280 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2281 struct is_equivalent_number_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
2282 : 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>{};
2284 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
2285 struct expression_template_default<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
2287 static constexpr const expression_template_option value = et_off;
2290 using boost::multiprecision::backends::cpp_int_backend;
2292 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2293 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::integral_constant<int, number_kind_integer>
2296 using cpp_int = number<cpp_int_backend<> > ;
2297 using cpp_rational_backend = rational_adaptor<cpp_int_backend<> >;
2298 using cpp_rational = number<cpp_rational_backend> ;
2300 // Fixed precision unsigned types:
2301 using uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > ;
2302 using uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > ;
2303 using uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > ;
2304 using uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> >;
2306 // Fixed precision signed types:
2307 using int128_t = number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> > ;
2308 using int256_t = number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> > ;
2309 using int512_t = number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> > ;
2310 using int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> >;
2312 // Over again, but with checking enabled this time:
2313 using checked_cpp_int = number<cpp_int_backend<0, 0, signed_magnitude, checked> > ;
2314 using checked_cpp_rational_backend = rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> >;
2315 using checked_cpp_rational = number<checked_cpp_rational_backend> ;
2316 // Fixed precision unsigned types:
2317 using checked_uint128_t = number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> > ;
2318 using checked_uint256_t = number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> > ;
2319 using checked_uint512_t = number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> > ;
2320 using checked_uint1024_t = number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> >;
2322 // Fixed precision signed types:
2323 using checked_int128_t = number<cpp_int_backend<128, 128, signed_magnitude, checked, void> > ;
2324 using checked_int256_t = number<cpp_int_backend<256, 256, signed_magnitude, checked, void> > ;
2325 using checked_int512_t = number<cpp_int_backend<512, 512, signed_magnitude, checked, void> > ;
2326 using checked_int1024_t = number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> >;
2328 #if defined(__GNUC__) && !defined(__clang__)
2329 // see https://github.com/boostorg/multiprecision/issues/413
2330 // and https://github.com/boostorg/multiprecision/issues/431
2331 #pragma GCC diagnostic pop
2334 #pragma warning(pop)
2337 }} // namespace boost::multiprecision
2340 // Last of all we include the implementations of all the eval_* non member functions:
2342 #include <boost/multiprecision/cpp_int/limits.hpp>
2343 #include <boost/multiprecision/cpp_int/comparison.hpp>
2344 #include <boost/multiprecision/cpp_int/add.hpp>
2345 #include <boost/multiprecision/cpp_int/multiply.hpp>
2346 #include <boost/multiprecision/cpp_int/divide.hpp>
2347 #include <boost/multiprecision/cpp_int/bitwise.hpp>
2348 #include <boost/multiprecision/cpp_int/misc.hpp>
2349 #include <boost/multiprecision/cpp_int/literals.hpp>
2350 #include <boost/multiprecision/cpp_int/serialize.hpp>
2351 #include <boost/multiprecision/cpp_int/import_export.hpp>