1 //////////////////3/////////////////////////////////////////////
2 //  Copyright 2012 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5 
6 #ifndef BOOST_MP_CPP_INT_HPP
7 #define BOOST_MP_CPP_INT_HPP
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <boost/cstdint.hpp>
12 #include <boost/multiprecision/number.hpp>
13 #include <boost/multiprecision/detail/integer_ops.hpp>
14 #include <boost/multiprecision/detail/rebind.hpp>
15 #include <boost/core/empty_value.hpp>
16 #include <boost/array.hpp>
17 #include <boost/type_traits/is_integral.hpp>
18 #include <boost/type_traits/is_floating_point.hpp>
19 #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
20 #include <boost/multiprecision/rational_adaptor.hpp>
21 #include <boost/multiprecision/traits/is_byte_container.hpp>
22 #include <boost/predef/other/endian.h>
23 #include <boost/integer/static_min_max.hpp>
24 #include <boost/type_traits/common_type.hpp>
25 #include <boost/type_traits/make_signed.hpp>
26 #include <boost/multiprecision/cpp_int/checked.hpp>
27 #include <boost/multiprecision/detail/constexpr.hpp>
28 #ifdef BOOST_MP_USER_DEFINED_LITERALS
29 #include <boost/multiprecision/cpp_int/value_pack.hpp>
30 #endif
31 
32 namespace boost {
33 namespace multiprecision {
34 namespace backends {
35 
36 using boost::enable_if;
37 
38 #ifdef BOOST_MSVC
39 #pragma warning(push)
40 #pragma warning(disable : 4307) // integral constant overflow (oveflow is in a branch not taken when it would overflow)
41 #pragma warning(disable : 4127) // conditional expression is constant
42 #pragma warning(disable : 4702) // Unreachable code (reachability depends on template params)
43 #endif
44 
45 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>
46 struct cpp_int_backend;
47 
48 } // namespace backends
49 
50 namespace detail {
51 
52 template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
53 struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public boost::false_type
54 {};
55 
56 } // namespace detail
57 
58 namespace backends {
59 
60 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
61 struct cpp_int_base;
62 //
63 // Traits class determines the maximum and minimum precision values:
64 //
65 template <class T>
66 struct max_precision;
67 
68 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
69 struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
70 {
71    static const unsigned value = is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value
72                                                            : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : UINT_MAX);
73 };
74 
75 template <class T>
76 struct min_precision;
77 
78 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
79 struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
80 {
81    static const unsigned value = (is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value : MinBits);
82 };
83 //
84 // Traits class determines whether the number of bits precision requested could fit in a native type,
85 // we call this a "trivial" cpp_int:
86 //
87 template <class T>
88 struct is_trivial_cpp_int
89 {
90    static const bool value = false;
91 };
92 
93 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
94 struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
95 {
96    typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> self;
97    static const bool                                                       value = is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
98 };
99 
100 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
101 struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
102 {
103    static const bool value = true;
104 };
105 
106 } // namespace backends
107 //
108 // Traits class to determine whether a cpp_int_backend is signed or not:
109 //
110 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
111 struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
112     : public mpl::bool_<(SignType == unsigned_magnitude) || (SignType == unsigned_packed)>
113 {};
114 
115 namespace backends {
116 //
117 // Traits class determines whether T should be implicitly convertible to U, or
118 // whether the constructor should be made explicit.  The latter happens if we
119 // are losing the sign, or have fewer digits precision in the target type:
120 //
121 template <class T, class U>
122 struct is_implicit_cpp_int_conversion;
123 
124 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>
125 struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
126 {
127    typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>      t1;
128    typedef cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t2;
129    static const bool                                                            value =
130        (is_signed_number<t2>::value || !is_signed_number<t1>::value) && (max_precision<t1>::value <= max_precision<t2>::value);
131 };
132 
133 //
134 // Traits class to determine whether operations on a cpp_int may throw:
135 //
136 template <class T>
137 struct is_non_throwing_cpp_int : public mpl::false_
138 {};
139 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType>
140 struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public mpl::true_
141 {};
142 
143 //
144 // Traits class, determines whether the cpp_int is fixed precision or not:
145 //
146 template <class T>
147 struct is_fixed_precision;
148 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
149 struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
150     : public mpl::bool_<max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != UINT_MAX>
151 {};
152 
153 namespace detail {
154 
verify_new_size(unsigned new_size,unsigned min_size,const mpl::int_<checked> &)155 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(unsigned new_size, unsigned min_size, const mpl::int_<checked>&)
156 {
157    if (new_size < min_size)
158       BOOST_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
159 }
verify_new_size(unsigned,unsigned,const mpl::int_<unchecked> &)160 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(unsigned /*new_size*/, unsigned /*min_size*/, const mpl::int_<unchecked>&) {}
161 
162 template <class U>
verify_limb_mask(bool b,U limb,U mask,const mpl::int_<checked> &)163 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool b, U limb, U mask, const mpl::int_<checked>&)
164 {
165    // When we mask out "limb" with "mask", do we loose bits?  If so it's an overflow error:
166    if (b && (limb & ~mask))
167       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."));
168 }
169 template <class U>
verify_limb_mask(bool,U,U,const mpl::int_<unchecked> &)170 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const mpl::int_<unchecked>&) {}
171 
172 } // namespace detail
173 
174 //
175 // Now define the various data layouts that are possible as partial specializations of the base class,
176 // starting with the default arbitrary precision signed integer type:
177 //
178 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
179 struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>
180     : private boost::empty_value<typename detail::rebind<limb_type, Allocator>::type>
181 {
182    typedef typename detail::rebind<limb_type, Allocator>::type allocator_type;
183 #ifdef BOOST_NO_CXX11_ALLOCATOR
184    typedef typename allocator_type::pointer       limb_pointer;
185    typedef typename allocator_type::const_pointer const_limb_pointer;
186 #else
187    typedef typename std::allocator_traits<allocator_type>::pointer       limb_pointer;
188    typedef typename std::allocator_traits<allocator_type>::const_pointer const_limb_pointer;
189 #endif
190    typedef mpl::int_<Checked> checked_type;
191 
192    //
193    // Interface invariants:
194    //
195    BOOST_STATIC_ASSERT(!is_void<Allocator>::value);
196 
197  private:
198    typedef boost::empty_value<allocator_type> base_type;
199 
200    struct limb_data
201    {
202       unsigned        capacity;
203       limb_pointer    data;
204    };
205 
206  public:
207    BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
208    BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
209    BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
210    BOOST_STATIC_CONSTANT(unsigned, internal_limb_count =
211                                        MinBits
212                                            ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
213                                            : (sizeof(limb_data) / sizeof(limb_type)) > 1 ? (sizeof(limb_data) / sizeof(limb_type)) : 2);
214  private:
215    union data_type
216    {
217       limb_data        ld;
218       limb_type        la[internal_limb_count];
219       limb_type        first;
220       double_limb_type double_first;
221 
data_type()222       BOOST_CONSTEXPR data_type() BOOST_NOEXCEPT : first(0) {}
data_type(limb_type i)223       BOOST_CONSTEXPR data_type(limb_type i) BOOST_NOEXCEPT : first(i) {}
data_type(signed_limb_type i)224       BOOST_CONSTEXPR data_type(signed_limb_type i) BOOST_NOEXCEPT : first(i < 0 ? static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
225 #if BOOST_ENDIAN_LITTLE_BYTE
data_type(double_limb_type i)226       BOOST_CONSTEXPR data_type(double_limb_type i) BOOST_NOEXCEPT : double_first(i)
227       {}
data_type(signed_double_limb_type i)228       BOOST_CONSTEXPR data_type(signed_double_limb_type i) BOOST_NOEXCEPT : double_first(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
229 #endif
230 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !(defined(BOOST_MSVC) && (BOOST_MSVC < 1900))
data_type(limb_type * limbs,unsigned len)231       BOOST_CONSTEXPR data_type(limb_type* limbs, unsigned len) BOOST_NOEXCEPT : ld{ len, limbs }
232       {}
233 #else
data_type(limb_type * limbs,unsigned len)234       BOOST_CONSTEXPR data_type(limb_type* limbs, unsigned len) BOOST_NOEXCEPT
235       {
236          ld.capacity = len;
237          ld.data = limbs;
238       }
239 #endif
240    };
241 
242    data_type m_data;
243    unsigned  m_limbs;
244    bool      m_sign, m_internal, m_alias;
245 
246  public:
247    //
248    // Direct construction:
249    //
cpp_int_baseboost::multiprecision::backends::cpp_int_base250    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i) BOOST_NOEXCEPT
251        : m_data(i),
252          m_limbs(1),
253          m_sign(false),
254          m_internal(true),
255          m_alias(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base256    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i) BOOST_NOEXCEPT
257        : m_data(i),
258          m_limbs(1),
259          m_sign(i < 0),
260          m_internal(true),
261          m_alias(false) {}
262 #if BOOST_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base263    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i) BOOST_NOEXCEPT
264        : m_data(i),
265          m_limbs(i > max_limb_value ? 2 : 1),
266          m_sign(false),
267          m_internal(true),
268          m_alias(false)
269    {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base270    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i) BOOST_NOEXCEPT
271        : m_data(i),
272          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)),
273          m_sign(i < 0),
274          m_internal(true),
275          m_alias(false) {}
276 #endif
277    //
278    // Aliasing constructor aliases data:
279    //
280    struct scoped_shared_storage : private boost::empty_value<allocator_type>
281    {
282     private:
283       limb_type*      data;
284       unsigned        capacity;
285       unsigned        allocated;
286       bool            is_alias;
allocatorboost::multiprecision::backends::cpp_int_base::scoped_shared_storage287       allocator_type& allocator() BOOST_NOEXCEPT { return boost::empty_value<allocator_type>::get(); }
288 
289     public:
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage290       scoped_shared_storage(const allocator_type& a, unsigned len)
291           : boost::empty_value<allocator_type>(boost::empty_init_t(), a), capacity(len), allocated(0), is_alias(false)
292       {
293          data = allocator().allocate(len);
294       }
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage295       scoped_shared_storage(const cpp_int_base& i, unsigned len)
296           : boost::empty_value<allocator_type>(boost::empty_init_t(), i.allocator()), capacity(len), allocated(0), is_alias(false)
297       {
298          data = allocator().allocate(len);
299       }
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage300       scoped_shared_storage(limb_type* limbs, unsigned n) : data(limbs), capacity(n), allocated(0), is_alias(true) {}
~scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage301       ~scoped_shared_storage()
302       {
303          if(!is_alias)
304             allocator().deallocate(data, capacity);
305       }
allocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage306       limb_type* allocate(unsigned n) BOOST_NOEXCEPT
307       {
308          limb_type* result = data + allocated;
309          allocated += n;
310          BOOST_ASSERT(allocated <= capacity);
311          return result;
312       }
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage313       void deallocate(unsigned n)
314       {
315          BOOST_ASSERT(n <= allocated);
316          allocated -= n;
317       }
318    };
cpp_int_baseboost::multiprecision::backends::cpp_int_base319    explicit BOOST_CONSTEXPR cpp_int_base(limb_type* data, unsigned offset, unsigned len) BOOST_NOEXCEPT
320        : m_data(data + offset, len),
321          m_limbs(len),
322          m_sign(false),
323          m_internal(false),
324          m_alias(true) {}
325    // This next constructor is for constructing const objects from const limb_type*'s only.
326    // Unfortunately we appear to have no way to assert that within the language, and the const_cast
327    // is a side effect of that :(
cpp_int_baseboost::multiprecision::backends::cpp_int_base328    explicit BOOST_CONSTEXPR cpp_int_base(const limb_type* data, unsigned offset, unsigned len) BOOST_NOEXCEPT
329        : m_data(const_cast<limb_type*>(data) + offset, len),
330          m_limbs(len),
331          m_sign(false),
332          m_internal(false),
333          m_alias(true) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base334    explicit cpp_int_base(scoped_shared_storage& data, unsigned len) BOOST_NOEXCEPT
335        : m_data(data.allocate(len), len),
336          m_limbs(len),
337          m_sign(false),
338          m_internal(false),
339          m_alias(true) {}
340    //
341    // Helper functions for getting at our internal data, and manipulating storage:
342    //
allocatorboost::multiprecision::backends::cpp_int_base343    BOOST_MP_FORCEINLINE allocator_type&       allocator() BOOST_NOEXCEPT { return base_type::get(); }
allocatorboost::multiprecision::backends::cpp_int_base344    BOOST_MP_FORCEINLINE const allocator_type& allocator() const BOOST_NOEXCEPT { return base_type::get(); }
sizeboost::multiprecision::backends::cpp_int_base345    BOOST_MP_FORCEINLINE unsigned              size() const BOOST_NOEXCEPT { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base346    BOOST_MP_FORCEINLINE limb_pointer          limbs() BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
limbsboost::multiprecision::backends::cpp_int_base347    BOOST_MP_FORCEINLINE const_limb_pointer    limbs() const BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
capacityboost::multiprecision::backends::cpp_int_base348    BOOST_MP_FORCEINLINE unsigned              capacity() const BOOST_NOEXCEPT { return m_internal ? internal_limb_count : m_data.ld.capacity; }
signboost::multiprecision::backends::cpp_int_base349    BOOST_MP_FORCEINLINE bool                  sign() const BOOST_NOEXCEPT { return m_sign; }
signboost::multiprecision::backends::cpp_int_base350    void                                       sign(bool b) BOOST_NOEXCEPT
351    {
352       m_sign = b;
353       // Check for zero value:
354       if (m_sign && (m_limbs == 1))
355       {
356          if (limbs()[0] == 0)
357             m_sign = false;
358       }
359    }
resizeboost::multiprecision::backends::cpp_int_base360    void resize(unsigned new_size, unsigned min_size)
361    {
362       static const unsigned max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
363       // We never resize beyond MaxSize:
364       if (new_size > max_limbs)
365          new_size = max_limbs;
366       detail::verify_new_size(new_size, min_size, checked_type());
367       // See if we have enough capacity already:
368       unsigned cap = capacity();
369       if (new_size > cap)
370       {
371          // We must not be an alias, memory allocation here defeats the whole point of aliasing:
372          BOOST_ASSERT(!m_alias);
373          // Allocate a new buffer and copy everything over:
374          cap             = (std::min)((std::max)(cap * 4, new_size), max_limbs);
375          limb_pointer pl = allocator().allocate(cap);
376          std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
377          if (!m_internal && !m_alias)
378             allocator().deallocate(limbs(), capacity());
379          else
380             m_internal = false;
381          m_limbs            = new_size;
382          m_data.ld.capacity = cap;
383          m_data.ld.data     = pl;
384       }
385       else
386       {
387          m_limbs = new_size;
388       }
389    }
normalizeboost::multiprecision::backends::cpp_int_base390    BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT
391    {
392       limb_pointer p = limbs();
393       while ((m_limbs - 1) && !p[m_limbs - 1])
394          --m_limbs;
395    }
cpp_int_baseboost::multiprecision::backends::cpp_int_base396    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(), m_limbs(1), m_sign(false), m_internal(true), m_alias(false){}
cpp_int_baseboost::multiprecision::backends::cpp_int_base397    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)
398    {
399       if (m_alias)
400       {
401          m_data.ld = o.m_data.ld;
402       }
403       else
404       {
405          resize(o.size(), o.size());
406          std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
407       }
408    }
409 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
cpp_int_baseboost::multiprecision::backends::cpp_int_base410    cpp_int_base(cpp_int_base&& o)
411        : 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)
412    {
413       if (m_internal)
414       {
415          std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
416       }
417       else
418       {
419          m_data.ld    = o.m_data.ld;
420          o.m_limbs    = 0;
421          o.m_internal = true;
422       }
423    }
operator =boost::multiprecision::backends::cpp_int_base424    cpp_int_base& operator=(cpp_int_base&& o) BOOST_NOEXCEPT
425    {
426       if (!m_internal && !m_alias)
427          allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
428       *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
429       m_limbs                        = o.m_limbs;
430       m_sign                         = o.m_sign;
431       m_internal                     = o.m_internal;
432       m_alias                        = o.m_alias;
433       if (m_internal)
434       {
435          std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
436       }
437       else
438       {
439          m_data.ld    = o.m_data.ld;
440          o.m_limbs    = 0;
441          o.m_internal = true;
442       }
443       return *this;
444    }
445 #endif
~cpp_int_baseboost::multiprecision::backends::cpp_int_base446    BOOST_MP_FORCEINLINE ~cpp_int_base() BOOST_NOEXCEPT
447    {
448       if (!m_internal && !m_alias)
449          allocator().deallocate(limbs(), capacity());
450    }
assignboost::multiprecision::backends::cpp_int_base451    void assign(const cpp_int_base& o)
452    {
453       if (this != &o)
454       {
455          static_cast<base_type&>(*this) = static_cast<const base_type&>(o);
456          m_limbs                        = 0;
457          resize(o.size(), o.size());
458          std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
459          m_sign = o.m_sign;
460       }
461    }
negateboost::multiprecision::backends::cpp_int_base462    BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
463    {
464       m_sign = !m_sign;
465       // Check for zero value:
466       if (m_sign && (m_limbs == 1))
467       {
468          if (limbs()[0] == 0)
469             m_sign = false;
470       }
471    }
isnegboost::multiprecision::backends::cpp_int_base472    BOOST_MP_FORCEINLINE bool isneg() const BOOST_NOEXCEPT
473    {
474       return m_sign;
475    }
do_swapboost::multiprecision::backends::cpp_int_base476    BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
477    {
478       std::swap(m_data, o.m_data);
479       std::swap(m_sign, o.m_sign);
480       std::swap(m_internal, o.m_internal);
481       std::swap(m_limbs, o.m_limbs);
482       std::swap(m_alias, o.m_alias);
483    }
484 
485  protected:
486    template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base487    void check_in_range(const A&) BOOST_NOEXCEPT {}
488 };
489 
490 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
491 
492 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
493 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
494 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
495 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
496 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
497 const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
498 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
499 const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
500 
501 #endif
502 
503 template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
504 struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false>
505     : private boost::empty_value<typename detail::rebind<limb_type, Allocator>::type>
506 {
507    //
508    // There is currently no support for unsigned arbitrary precision arithmetic, largely
509    // because it's not clear what subtraction should do:
510    //
511    BOOST_STATIC_ASSERT_MSG(((sizeof(Allocator) == 0) && !is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
512 };
513 //
514 // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
515 //
516 template <unsigned MinBits, cpp_int_check_type Checked>
517 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
518 {
519    typedef limb_type*         limb_pointer;
520    typedef const limb_type*   const_limb_pointer;
521    typedef mpl::int_<Checked> checked_type;
522 
523    struct scoped_shared_storage
524    {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage525       BOOST_MP_CXX14_CONSTEXPR  scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage526       BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
527    };
528 
529    //
530    // Interface invariants:
531    //
532    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?");
533 
534  public:
535    BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
536    BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
537    BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
538    BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
539    BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0)));
540    BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
541 
542  private:
543    union data_type
544    {
545       limb_type        m_data[internal_limb_count];
546       limb_type        m_first_limb;
547       double_limb_type m_double_first_limb;
548 
data_type()549       BOOST_CONSTEXPR data_type()
550 #if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX14_CONSTEXPR)
551           : m_first_limb(0)
552       {}
553 #else
554           : m_data{0}
555       {}
556 #endif
data_type(limb_type i)557       BOOST_CONSTEXPR data_type(limb_type i)
558 #if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX14_CONSTEXPR)
559           : m_first_limb(i)
560       {}
561 #else
562           : m_data{i}
563       {}
564 #endif
565 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
data_type(limb_type i,limb_type j)566       BOOST_CONSTEXPR data_type(limb_type i, limb_type j) : m_data{i, j}
567       {}
568 #endif
data_type(double_limb_type i)569       BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i)
570       {
571 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
572          if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
573          {
574             data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
575             *this = t;
576          }
577 #endif
578       }
579 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
580       template <limb_type... VALUES>
data_type(literals::detail::value_pack<VALUES...>)581       BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
582       {}
583 #endif
584    } m_wrapper;
585    boost::uint16_t m_limbs;
586    bool            m_sign;
587 
588  public:
589    //
590    // Direct construction:
591    //
cpp_int_baseboost::multiprecision::backends::cpp_int_base592    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i) BOOST_NOEXCEPT
593        : m_wrapper(i),
594          m_limbs(1),
595          m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base596    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i) BOOST_NOEXCEPT
597        : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)),
598          m_limbs(1),
599          m_sign(i < 0) {}
600 #if BOOST_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base601    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i) BOOST_NOEXCEPT
602        : m_wrapper(i),
603          m_limbs(i > max_limb_value ? 2 : 1),
604          m_sign(false)
605    {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base606    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i) BOOST_NOEXCEPT
607        : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
608          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          m_sign(i < 0) {}
610 #endif
611 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
612    template <limb_type... VALUES>
cpp_int_baseboost::multiprecision::backends::cpp_int_base613    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
614        : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false)
615    {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base616    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<> i)
617        : m_wrapper(i), m_limbs(1), m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base618    BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
619        : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
620 #endif
cpp_int_baseboost::multiprecision::backends::cpp_int_base621    explicit BOOST_CONSTEXPR cpp_int_base(scoped_shared_storage&, unsigned) BOOST_NOEXCEPT : m_wrapper(), m_limbs(0), m_sign(false)
622    {}
623 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
624    //
625    // These are deprecated in C++20 unless we make them explicit:
626    //
627    constexpr cpp_int_base& operator=(const cpp_int_base&) = default;
628 #endif
629    //
630    // Helper functions for getting at our internal data, and manipulating storage:
631    //
sizeboost::multiprecision::backends::cpp_int_base632    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned              size() const BOOST_NOEXCEPT { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base633    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
limbsboost::multiprecision::backends::cpp_int_base634    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer    limbs() const BOOST_NOEXCEPT { return m_wrapper.m_data; }
signboost::multiprecision::backends::cpp_int_base635    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool                  sign() const BOOST_NOEXCEPT { return m_sign; }
signboost::multiprecision::backends::cpp_int_base636    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void         sign(bool b) BOOST_NOEXCEPT
637    {
638       m_sign = b;
639       // Check for zero value:
640       if (m_sign && (m_limbs == 1))
641       {
642          if (limbs()[0] == 0)
643             m_sign = false;
644       }
645    }
resizeboost::multiprecision::backends::cpp_int_base646    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
647    {
648       m_limbs = static_cast<boost::uint16_t>((std::min)(new_size, internal_limb_count));
649       detail::verify_new_size(m_limbs, min_size, checked_type());
650    }
normalizeboost::multiprecision::backends::cpp_int_base651    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
652    {
653       limb_pointer p = limbs();
654       detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
655       p[internal_limb_count - 1] &= upper_limb_mask;
656       while ((m_limbs - 1) && !p[m_limbs - 1])
657          --m_limbs;
658       if ((m_limbs == 1) && (!*p))
659          m_sign = false; // zero is always unsigned
660    }
661 
cpp_int_baseboost::multiprecision::backends::cpp_int_base662    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
663    // Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
cpp_int_baseboost::multiprecision::backends::cpp_int_base664    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
665        : m_wrapper(o.m_wrapper),
666          m_limbs(o.m_limbs),
667          m_sign(o.m_sign) {}
668    // Defaulted functions:
669    //~cpp_int_base() BOOST_NOEXCEPT {}
670 
assignboost::multiprecision::backends::cpp_int_base671    void BOOST_MP_CXX14_CONSTEXPR assign(const cpp_int_base& o) BOOST_NOEXCEPT
672    {
673       if (this != &o)
674       {
675          m_limbs = o.m_limbs;
676 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
677          if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
678          {
679             for (unsigned i = 0; i < m_limbs; ++i)
680                limbs()[i] = o.limbs()[i];
681          }
682          else
683 #endif
684             std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
685          m_sign = o.m_sign;
686       }
687    }
negateboost::multiprecision::backends::cpp_int_base688    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() BOOST_NOEXCEPT
689    {
690       m_sign = !m_sign;
691       // Check for zero value:
692       if (m_sign && (m_limbs == 1))
693       {
694          if (limbs()[0] == 0)
695             m_sign = false;
696       }
697    }
isnegboost::multiprecision::backends::cpp_int_base698    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const BOOST_NOEXCEPT
699    {
700       return m_sign;
701    }
do_swapboost::multiprecision::backends::cpp_int_base702    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
703    {
704       for (unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
705          std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
706       std_constexpr::swap(m_sign, o.m_sign);
707       std_constexpr::swap(m_limbs, o.m_limbs);
708    }
709 
710  protected:
711    template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base712    BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) BOOST_NOEXCEPT {}
713 };
714 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
715 
716 template <unsigned MinBits, cpp_int_check_type Checked>
717 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
718 template <unsigned MinBits, cpp_int_check_type Checked>
719 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
720 template <unsigned MinBits, cpp_int_check_type Checked>
721 const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
722 template <unsigned MinBits, cpp_int_check_type Checked>
723 const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
724 
725 #endif
726 //
727 // Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
728 //
729 template <unsigned MinBits, cpp_int_check_type Checked>
730 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
731 {
732    typedef limb_type*         limb_pointer;
733    typedef const limb_type*   const_limb_pointer;
734    typedef mpl::int_<Checked> checked_type;
735 
736    struct scoped_shared_storage
737    {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage738       BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage739       BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
740    };
741    //
742    // Interface invariants:
743    //
744    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?");
745 
746  public:
747    BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
748    BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
749    BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
750    BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
751    BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0)));
752    BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
753 
754  private:
755    union data_type
756    {
757       limb_type        m_data[internal_limb_count];
758       limb_type        m_first_limb;
759       double_limb_type m_double_first_limb;
760 
data_type()761       BOOST_CONSTEXPR data_type()
762 #if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX14_CONSTEXPR)
763           : m_first_limb(0)
764       {}
765 #else
766           : m_data{0}
767       {}
768 #endif
data_type(limb_type i)769       BOOST_CONSTEXPR data_type(limb_type i)
770 #if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX14_CONSTEXPR)
771           : m_first_limb(i)
772       {}
773 #else
774           : m_data{i}
775       {}
776 #endif
777 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
data_type(limb_type i,limb_type j)778       BOOST_CONSTEXPR data_type(limb_type i, limb_type j) : m_data{i, j}
779       {}
780 #endif
data_type(double_limb_type i)781       BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i)
782       {
783 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
784          if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
785          {
786             data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
787             *this = t;
788          }
789 #endif
790       }
791 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
792       template <limb_type... VALUES>
data_type(literals::detail::value_pack<VALUES...>)793       BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
794       {}
795 #endif
796    } m_wrapper;
797    limb_type m_limbs;
798 
799  public:
800    //
801    // Direct construction:
802    //
cpp_int_baseboost::multiprecision::backends::cpp_int_base803    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i) BOOST_NOEXCEPT
804        : m_wrapper(i),
805          m_limbs(1) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base806    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_limb_type i) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
807        : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)), m_limbs(1)
808    {
809       if (i < 0)
810          negate();
811    }
812 #if BOOST_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
cpp_int_baseboost::multiprecision::backends::cpp_int_base813    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i) BOOST_NOEXCEPT
814        : m_wrapper(i),
815          m_limbs(i > max_limb_value ? 2 : 1)
816    {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base817    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_double_limb_type i) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
818        : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
819          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))
820    {
821       if (i < 0)
822          negate();
823    }
824 #endif
825 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
826    template <limb_type... VALUES>
cpp_int_baseboost::multiprecision::backends::cpp_int_base827    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
828        : m_wrapper(i), m_limbs(sizeof...(VALUES))
829    {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base830    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>)
831        : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
832 #endif
cpp_int_baseboost::multiprecision::backends::cpp_int_base833    explicit BOOST_CONSTEXPR cpp_int_base(scoped_shared_storage&, unsigned) BOOST_NOEXCEPT : m_wrapper(), m_limbs(1)
834    {}
835        //
836    // Helper functions for getting at our internal data, and manipulating storage:
837    //
sizeboost::multiprecision::backends::cpp_int_base838    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned              size() const BOOST_NOEXCEPT { return m_limbs; }
limbsboost::multiprecision::backends::cpp_int_base839    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
limbsboost::multiprecision::backends::cpp_int_base840    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer    limbs() const BOOST_NOEXCEPT { return m_wrapper.m_data; }
signboost::multiprecision::backends::cpp_int_base841    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool                  sign() const BOOST_NOEXCEPT { return false; }
signboost::multiprecision::backends::cpp_int_base842    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void         sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
843    {
844       if (b)
845          negate();
846    }
resizeboost::multiprecision::backends::cpp_int_base847    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
848    {
849       m_limbs = (std::min)(new_size, internal_limb_count);
850       detail::verify_new_size(m_limbs, min_size, checked_type());
851    }
normalizeboost::multiprecision::backends::cpp_int_base852    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
853    {
854       limb_pointer p = limbs();
855       detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
856       p[internal_limb_count - 1] &= upper_limb_mask;
857       while ((m_limbs - 1) && !p[m_limbs - 1])
858          --m_limbs;
859    }
860 
cpp_int_baseboost::multiprecision::backends::cpp_int_base861    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT
862        : m_wrapper(limb_type(0u)),
863          m_limbs(1) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base864    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
865        : m_wrapper(o.m_wrapper),
866          m_limbs(o.m_limbs) {}
867    // Defaulted functions:
868    //~cpp_int_base() BOOST_NOEXCEPT {}
869 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
870    //
871    // These are deprecated in C++20 unless we make them explicit:
872    //
873    constexpr cpp_int_base& operator=(const cpp_int_base&) = default;
874 #endif
875 
assignboost::multiprecision::backends::cpp_int_base876    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) BOOST_NOEXCEPT
877    {
878       if (this != &o)
879       {
880          m_limbs = o.m_limbs;
881 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
882          if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
883          {
884             for (unsigned i = 0; i < m_limbs; ++i)
885                limbs()[i] = o.limbs()[i];
886          }
887          else
888 #endif
889             std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
890       }
891    }
892 
893  private:
check_negateboost::multiprecision::backends::cpp_int_base894    void check_negate(const mpl::int_<checked>&)
895    {
896       BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
897    }
check_negateboost::multiprecision::backends::cpp_int_base898    BOOST_MP_CXX14_CONSTEXPR void check_negate(const mpl::int_<unchecked>&) {}
899 
900  public:
negateboost::multiprecision::backends::cpp_int_base901    BOOST_MP_CXX14_CONSTEXPR void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
902    {
903       // Not so much a negate as a complement - this gets called when subtraction
904       // would result in a "negative" number:
905       if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
906          return; // negating zero is always zero, and always OK.
907       check_negate(checked_type());
908       unsigned i = m_limbs;
909       for (; i < internal_limb_count; ++i)
910          m_wrapper.m_data[i] = 0;
911       m_limbs = internal_limb_count;
912       for (i = 0; i < internal_limb_count; ++i)
913          m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
914       normalize();
915       eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
916    }
isnegboost::multiprecision::backends::cpp_int_base917    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg() const BOOST_NOEXCEPT
918    {
919       return false;
920    }
do_swapboost::multiprecision::backends::cpp_int_base921    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
922    {
923       for (unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
924          std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
925       std_constexpr::swap(m_limbs, o.m_limbs);
926    }
927 
928  protected:
929    template <class A>
check_in_rangeboost::multiprecision::backends::cpp_int_base930    BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) BOOST_NOEXCEPT {}
931 };
932 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
933 
934 template <unsigned MinBits, cpp_int_check_type Checked>
935 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
936 template <unsigned MinBits, cpp_int_check_type Checked>
937 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
938 template <unsigned MinBits, cpp_int_check_type Checked>
939 const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
940 template <unsigned MinBits, cpp_int_check_type Checked>
941 const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
942 
943 #endif
944 //
945 // Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
946 // because some platforms have native integer types longer than boost::long_long_type, "really boost::long_long_type" anyone??
947 //
948 template <unsigned N, bool s>
949 struct trivial_limb_type_imp
950 {
951    typedef double_limb_type type;
952 };
953 
954 template <unsigned N>
955 struct trivial_limb_type_imp<N, true>
956 {
957    typedef typename boost::uint_t<N>::least type;
958 };
959 
960 template <unsigned N>
961 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(boost::long_long_type) * CHAR_BIT>
962 {};
963 //
964 // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
965 //
966 template <unsigned MinBits, cpp_int_check_type Checked>
967 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
968 {
969    typedef typename trivial_limb_type<MinBits>::type local_limb_type;
970    typedef local_limb_type*                          limb_pointer;
971    typedef const local_limb_type*                    const_limb_pointer;
972    typedef mpl::int_<Checked>                        checked_type;
973 
974    struct scoped_shared_storage
975    {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage976       BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage977       BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
978    };
979 
980  protected:
981    BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
982    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)));
983 
984  private:
985    local_limb_type m_data;
986    bool            m_sign;
987 
988    //
989    // Interface invariants:
990    //
991    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?");
992 
993  protected:
994    template <class T>
995    BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<!boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base996    check_in_range(T val, const mpl::int_<checked>&)
997    {
998       typedef typename common_type<typename make_unsigned<T>::type, local_limb_type>::type common_type;
999 
1000       if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
1001          BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1002    }
1003    template <class T>
1004    BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base1005    check_in_range(T val, const mpl::int_<checked>&)
1006    {
1007       using std::abs;
1008       typedef typename common_type<T, local_limb_type>::type common_type;
1009 
1010       if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
1011          BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1012    }
1013    template <class T, int C>
check_in_rangeboost::multiprecision::backends::cpp_int_base1014    BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const mpl::int_<C>&) BOOST_NOEXCEPT {}
1015 
1016    template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base1017    BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type())))
1018    {
1019       check_in_range(val, checked_type());
1020    }
1021 
1022  public:
1023    //
1024    // Direct construction:
1025    //
1026    template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1027    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>())))
1028        : 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) {}
1029    template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1030    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1031        : 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)
1032    {
1033       check_in_range(i);
1034    }
1035    template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1036    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
1037        : m_data(static_cast<local_limb_type>(i) & limb_mask),
1038          m_sign(false) {}
1039    template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1040    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1041        : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
1042    template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1043    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
1044        : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask),
1045          m_sign(i < 0) {}
1046    template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1047    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename boost::enable_if_c<is_floating_point<F>::value && (Checked == checked)>::type const* = 0)
1048        : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1049 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
cpp_int_baseboost::multiprecision::backends::cpp_int_base1050    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
1051        : m_data(static_cast<local_limb_type>(0u)),
1052          m_sign(false)
1053    {}
1054    template <limb_type a>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1055    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>) BOOST_NOEXCEPT
1056        : m_data(static_cast<local_limb_type>(a)),
1057          m_sign(false) {}
1058    template <limb_type a, limb_type b>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1059    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>) BOOST_NOEXCEPT
1060        : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
1061          m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1062    BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) BOOST_NOEXCEPT
1063        : m_data(a.m_data),
1064          m_sign(a.m_data ? !a.m_sign : false) {}
1065 #endif
1066 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1067    //
1068    // These are deprecated in C++20 unless we make them explicit:
1069    //
1070    constexpr cpp_int_base& operator=(const cpp_int_base&) = default;
1071 #endif
cpp_int_baseboost::multiprecision::backends::cpp_int_base1072    explicit BOOST_CONSTEXPR cpp_int_base(scoped_shared_storage&, unsigned) BOOST_NOEXCEPT : m_data(0), m_sign(false)
1073    {}
1074        //
1075    // Helper functions for getting at our internal data, and manipulating storage:
1076    //
sizeboost::multiprecision::backends::cpp_int_base1077    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned              size() const BOOST_NOEXCEPT { return 1; }
limbsboost::multiprecision::backends::cpp_int_base1078    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
limbsboost::multiprecision::backends::cpp_int_base1079    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer    limbs() const BOOST_NOEXCEPT { return &m_data; }
signboost::multiprecision::backends::cpp_int_base1080    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool         sign() const BOOST_NOEXCEPT { return m_sign; }
signboost::multiprecision::backends::cpp_int_base1081    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void         sign(bool b) BOOST_NOEXCEPT
1082    {
1083       m_sign = b;
1084       // Check for zero value:
1085       if (m_sign && !m_data)
1086       {
1087          m_sign = false;
1088       }
1089    }
resizeboost::multiprecision::backends::cpp_int_base1090    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned /* new_size */, unsigned min_size)
1091    {
1092       detail::verify_new_size(2, min_size, checked_type());
1093    }
normalizeboost::multiprecision::backends::cpp_int_base1094    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
1095    {
1096       if (!m_data)
1097          m_sign = false; // zero is always unsigned
1098       detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1099       m_data &= limb_mask;
1100    }
1101 
cpp_int_baseboost::multiprecision::backends::cpp_int_base1102    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0), m_sign(false) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1103    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
1104        : m_data(o.m_data),
1105          m_sign(o.m_sign) {}
1106    //~cpp_int_base() BOOST_NOEXCEPT {}
assignboost::multiprecision::backends::cpp_int_base1107    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) BOOST_NOEXCEPT
1108    {
1109       m_data = o.m_data;
1110       m_sign = o.m_sign;
1111    }
negateboost::multiprecision::backends::cpp_int_base1112    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() BOOST_NOEXCEPT
1113    {
1114       m_sign = !m_sign;
1115       // Check for zero value:
1116       if (m_data == 0)
1117       {
1118          m_sign = false;
1119       }
1120    }
isnegboost::multiprecision::backends::cpp_int_base1121    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const BOOST_NOEXCEPT
1122    {
1123       return m_sign;
1124    }
do_swapboost::multiprecision::backends::cpp_int_base1125    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
1126    {
1127       std_constexpr::swap(m_sign, o.m_sign);
1128       std_constexpr::swap(m_data, o.m_data);
1129    }
1130 };
1131 //
1132 // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
1133 //
1134 template <unsigned MinBits, cpp_int_check_type Checked>
1135 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
1136 {
1137    typedef typename trivial_limb_type<MinBits>::type local_limb_type;
1138    typedef local_limb_type*                          limb_pointer;
1139    typedef const local_limb_type*                    const_limb_pointer;
1140 
1141    struct scoped_shared_storage
1142    {
scoped_shared_storageboost::multiprecision::backends::cpp_int_base::scoped_shared_storage1143       BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, unsigned) {}
deallocateboost::multiprecision::backends::cpp_int_base::scoped_shared_storage1144       BOOST_MP_CXX14_CONSTEXPR void deallocate(unsigned) {}
1145    };
1146 
1147  private:
1148    BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
1149    BOOST_STATIC_CONSTANT(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))
1150                                                                            : static_cast<local_limb_type>(~local_limb_type(0)));
1151 
1152    local_limb_type m_data;
1153 
1154    typedef mpl::int_<Checked> checked_type;
1155 
1156    //
1157    // Interface invariants:
1158    //
1159    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?");
1160 
1161  protected:
1162    template <class T>
1163    BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits)>::type
check_in_rangeboost::multiprecision::backends::cpp_int_base1164    check_in_range(T val, const mpl::int_<checked>&, const boost::false_type&)
1165    {
1166       typedef typename common_type<T, local_limb_type>::type common_type;
1167 
1168       if (static_cast<common_type>(val) > limb_mask)
1169          BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1170    }
1171    template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base1172    BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const mpl::int_<checked>&, const boost::true_type&)
1173    {
1174       typedef typename common_type<T, local_limb_type>::type common_type;
1175 
1176       if (static_cast<common_type>(val) > limb_mask)
1177          BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1178       if (val < 0)
1179          BOOST_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
1180    }
1181    template <class T, int C, bool B>
check_in_rangeboost::multiprecision::backends::cpp_int_base1182    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const mpl::int_<C>&, const boost::integral_constant<bool, B>&) BOOST_NOEXCEPT {}
1183 
1184    template <class T>
check_in_rangeboost::multiprecision::backends::cpp_int_base1185    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1186    {
1187       check_in_range(val, checked_type(), is_signed<T>());
1188    }
1189 
1190  public:
1191    //
1192    // Direct construction:
1193    //
1194 #ifdef __MSVC_RUNTIME_CHECKS
1195    template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1196    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
1197        : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
1198    {}
1199    template <class SI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1200    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1201        : 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); }
1202    template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1203    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
1204        : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
1205    template <class UI>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1206    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1207        : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1208 #else
1209    template <class SI>
1210    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
1211        : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
1212    {}
1213    template <class SI>
1214    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1215        : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
1216    template <class UI>
1217    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
1218        : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
1219    template <class UI>
1220    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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>())))
1221        : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
1222 #endif
1223    template <class F>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1224    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename boost::enable_if<is_floating_point<F> >::type const* = 0) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
1225        : m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask)
1226    {
1227       check_in_range(i);
1228       if (i < 0)
1229          negate();
1230    }
1231 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
cpp_int_baseboost::multiprecision::backends::cpp_int_base1232    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
1233        : m_data(static_cast<local_limb_type>(0u))
1234    {}
1235    template <limb_type a>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1236    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>) BOOST_NOEXCEPT
1237        : m_data(static_cast<local_limb_type>(a)) {}
1238    template <limb_type a, limb_type b>
cpp_int_baseboost::multiprecision::backends::cpp_int_base1239    BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>) BOOST_NOEXCEPT
1240        : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
1241 #endif
1242 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1243    //
1244    // These are deprecated in C++20 unless we make them explicit:
1245    //
1246    constexpr cpp_int_base& operator=(const cpp_int_base&) = default;
1247 #endif
cpp_int_baseboost::multiprecision::backends::cpp_int_base1248    explicit BOOST_CONSTEXPR cpp_int_base(scoped_shared_storage&, unsigned) BOOST_NOEXCEPT : m_data(0)
1249    {}
1250        //
1251    // Helper functions for getting at our internal data, and manipulating storage:
1252    //
sizeboost::multiprecision::backends::cpp_int_base1253    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned              size() const BOOST_NOEXCEPT { return 1; }
limbsboost::multiprecision::backends::cpp_int_base1254    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
limbsboost::multiprecision::backends::cpp_int_base1255    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer    limbs() const BOOST_NOEXCEPT { return &m_data; }
signboost::multiprecision::backends::cpp_int_base1256    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool                  sign() const BOOST_NOEXCEPT { return false; }
signboost::multiprecision::backends::cpp_int_base1257    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void         sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
1258    {
1259       if (b)
1260          negate();
1261    }
resizeboost::multiprecision::backends::cpp_int_base1262    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, unsigned min_size)
1263    {
1264       detail::verify_new_size(2, min_size, checked_type());
1265    }
normalizeboost::multiprecision::backends::cpp_int_base1266    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
1267    {
1268       detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1269       m_data &= limb_mask;
1270    }
1271 
cpp_int_baseboost::multiprecision::backends::cpp_int_base1272    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0) {}
cpp_int_baseboost::multiprecision::backends::cpp_int_base1273    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
1274        : m_data(o.m_data) {}
1275    //~cpp_int_base() BOOST_NOEXCEPT {}
assignboost::multiprecision::backends::cpp_int_base1276    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) BOOST_NOEXCEPT
1277    {
1278       m_data = o.m_data;
1279    }
negateboost::multiprecision::backends::cpp_int_base1280    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
1281    {
1282       if (Checked == checked)
1283       {
1284          BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1285       }
1286       m_data = ~m_data;
1287       ++m_data;
1288    }
isnegboost::multiprecision::backends::cpp_int_base1289    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg() const BOOST_NOEXCEPT
1290    {
1291       return false;
1292    }
do_swapboost::multiprecision::backends::cpp_int_base1293    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
1294    {
1295       std_constexpr::swap(m_data, o.m_data);
1296    }
1297 };
1298 //
1299 // Traits class, lets us know whether type T can be directly converted to the base type,
1300 // used to enable/disable constructors etc:
1301 //
1302 template <class Arg, class Base>
1303 struct is_allowed_cpp_int_base_conversion : public mpl::if_c<
1304                                                 is_same<Arg, limb_type>::value || is_same<Arg, signed_limb_type>::value
1305 #if BOOST_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
1306                                                     || is_same<Arg, double_limb_type>::value || is_same<Arg, signed_double_limb_type>::value
1307 #endif
1308 #if defined(BOOST_MP_USER_DEFINED_LITERALS)
1309                                                     || literals::detail::is_value_pack<Arg>::value
1310 #endif
1311                                                     || (is_trivial_cpp_int<Base>::value && is_arithmetic<Arg>::value),
1312                                                 mpl::true_,
1313                                                 mpl::false_>::type
1314 {};
1315 //
1316 // Now the actual backend, normalising parameters passed to the base class:
1317 //
1318 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1319 struct cpp_int_backend
1320     : public cpp_int_base<
1321           min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1322           max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1323           SignType,
1324           Checked,
1325           Allocator,
1326           is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1327 {
1328    typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> self_type;
1329    typedef cpp_int_base<
1330        min_precision<self_type>::value,
1331        max_precision<self_type>::value,
1332        SignType,
1333        Checked,
1334        Allocator,
1335        is_trivial_cpp_int<self_type>::value>
1336        base_type;
1337    typedef mpl::bool_<is_trivial_cpp_int<self_type>::value> trivial_tag;
1338 
1339  public:
1340    typedef typename mpl::if_<
1341        trivial_tag,
1342        mpl::list<
1343            signed char, short, int, long,
1344            boost::long_long_type, signed_double_limb_type>,
1345        mpl::list<signed_limb_type, signed_double_limb_type> >::type signed_types;
1346    typedef typename mpl::if_<
1347        trivial_tag,
1348        mpl::list<unsigned char, unsigned short, unsigned,
1349                  unsigned long, boost::ulong_long_type, double_limb_type>,
1350        mpl::list<limb_type, double_limb_type> >::type unsigned_types;
1351    typedef typename mpl::if_<
1352        trivial_tag,
1353        mpl::list<float, double, long double>,
1354        mpl::list<long double> >::type float_types;
1355    typedef mpl::int_<Checked>         checked_type;
1356 
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1357    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend() BOOST_NOEXCEPT {}
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1358    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) {}
1359 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1360    BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(cpp_int_backend&& o) BOOST_NOEXCEPT
1361        : base_type(static_cast<base_type&&>(o))
1362    {}
1363 #endif
1364    //
1365    // Direct construction from arithmetic type:
1366    //
1367    template <class Arg>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1368    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>())))
1369        : base_type(i) {}
1370    //
1371    // Aliasing constructor: the result will alias the memory referenced, unless
1372    // we have fixed precision and storage, in which case we copy the memory:
1373    //
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1374    explicit BOOST_CONSTEXPR cpp_int_backend(limb_type* data, unsigned offset, unsigned len) BOOST_NOEXCEPT
1375        : base_type(data, offset, len) {}
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1376    explicit cpp_int_backend(const limb_type* data, unsigned offset, unsigned len) BOOST_NOEXCEPT
1377        : base_type(data, offset, len) { this->normalize(); }
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1378    explicit BOOST_CONSTEXPR cpp_int_backend(typename base_type::scoped_shared_storage& data, unsigned len) BOOST_NOEXCEPT
1379        : base_type(data, len) {}
1380 
1381  private:
1382    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1383    BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::true_ const&, mpl::true_ const&)
1384    {
1385       // Assigning trivial type to trivial type:
1386       this->check_in_range(*other.limbs());
1387       *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1388       this->sign(other.sign());
1389       this->normalize();
1390    }
1391    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1392    BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::true_ const&, mpl::false_ const&)
1393    {
1394       // non-trivial to trivial narrowing conversion:
1395       double_limb_type v = *other.limbs();
1396       if (other.size() > 1)
1397       {
1398          v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1399          if ((Checked == checked) && (other.size() > 2))
1400          {
1401             BOOST_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1402          }
1403       }
1404       *this = v;
1405       this->sign(other.sign());
1406       this->normalize();
1407    }
1408    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1409    BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::false_ const&, mpl::true_ const&)
1410    {
1411       // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards:
1412       *this = static_cast<
1413           typename boost::multiprecision::detail::canonical<
1414               typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1415               cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
1416       this->sign(other.sign());
1417    }
1418    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
do_assignboost::multiprecision::backends::cpp_int_backend1419    BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, mpl::false_ const&, mpl::false_ const&)
1420    {
1421       // regular non-trivial to non-trivial assign:
1422       this->resize(other.size(), other.size());
1423 
1424       unsigned count = (std::min)(other.size(), this->size());
1425       for (unsigned i = 0; i < count; ++i)
1426          this->limbs()[i] = other.limbs()[i];
1427       //std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size(), this->size()) * sizeof(this->limbs()[0]));
1428 
1429       this->sign(other.sign());
1430       this->normalize();
1431    }
1432 
1433  public:
1434    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1435    BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1436        const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1437        typename boost::enable_if_c<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = 0)
1438        : base_type()
1439    {
1440       do_assign(
1441           other,
1442           mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1443           mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1444    }
1445    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1446    explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1447        const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1448        typename boost::disable_if_c<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = 0)
1449        : base_type()
1450    {
1451       do_assign(
1452           other,
1453           mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1454           mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1455    }
1456    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
operator =boost::multiprecision::backends::cpp_int_backend1457    BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
1458        const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1459    {
1460       do_assign(
1461           other,
1462           mpl::bool_<is_trivial_cpp_int<self_type>::value>(),
1463           mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1464       return *this;
1465    }
1466 #ifdef BOOST_MP_USER_DEFINED_LITERALS
cpp_int_backendboost::multiprecision::backends::cpp_int_backend1467    BOOST_CONSTEXPR cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1468        : base_type(static_cast<const base_type&>(a), tag)
1469    {}
1470 #endif
1471 
operator =boost::multiprecision::backends::cpp_int_backend1472    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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&>())))
1473    {
1474       this->assign(o);
1475       return *this;
1476    }
1477 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
operator =boost::multiprecision::backends::cpp_int_backend1478    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(cpp_int_backend&& o) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<base_type&>() = std::declval<base_type>()))
1479    {
1480       *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1481       return *this;
1482    }
1483 #endif
1484  private:
1485    template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1486    BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if<is_unsigned<A> >::type do_assign_arithmetic(A val, const mpl::true_&)
1487        BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1488    {
1489       this->check_in_range(val);
1490       *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1491       this->sign(false);
1492       this->normalize();
1493    }
1494    template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1495    BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<is_unsigned<A>::value || !is_integral<A>::value>::type do_assign_arithmetic(A val, const mpl::true_&)
1496        BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1497    {
1498       this->check_in_range(val);
1499       *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);
1500       this->sign(val < 0);
1501       this->normalize();
1502    }
1503    template <class A>
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1504    BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<!is_integral<A>::value>::type do_assign_arithmetic(A val, const mpl::true_&)
1505    {
1506       this->check_in_range(val);
1507       *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);
1508       this->sign(val < 0);
1509       this->normalize();
1510    }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1511    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(limb_type i, const mpl::false_&) BOOST_NOEXCEPT
1512    {
1513       this->resize(1, 1);
1514       *this->limbs() = i;
1515       this->sign(false);
1516    }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1517    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(signed_limb_type i, const mpl::false_&) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().sign(true)))
1518    {
1519       this->resize(1, 1);
1520       *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1521       this->sign(i < 0);
1522    }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1523    BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(double_limb_type i, const mpl::false_&) BOOST_NOEXCEPT
1524    {
1525       BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type));
1526       BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2);
1527       typename base_type::limb_pointer p = this->limbs();
1528 #ifdef __MSVC_RUNTIME_CHECKS
1529       *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1530 #else
1531       *p                        = static_cast<limb_type>(i);
1532 #endif
1533       p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1534       this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1535       this->sign(false);
1536    }
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1537    BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(signed_double_limb_type i, const mpl::false_&) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_backend>().sign(true)))
1538    {
1539       BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type));
1540       BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2);
1541       bool s = false;
1542       if (i < 0)
1543          s = true;
1544       double_limb_type                 ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1545       typename base_type::limb_pointer p  = this->limbs();
1546 #ifdef __MSVC_RUNTIME_CHECKS
1547       *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1548 #else
1549       *p                        = static_cast<limb_type>(ui);
1550 #endif
1551       p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1552       this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1553       this->sign(s);
1554    }
1555 
do_assign_arithmeticboost::multiprecision::backends::cpp_int_backend1556    BOOST_MP_CXX14_CONSTEXPR void do_assign_arithmetic(long double a, const mpl::false_&)
1557    {
1558       using default_ops::eval_add;
1559       using default_ops::eval_subtract;
1560       using std::floor;
1561       using std::frexp;
1562       using std::ldexp;
1563 
1564       if (a < 0)
1565       {
1566          do_assign_arithmetic(-a, mpl::false_());
1567          this->sign(true);
1568          return;
1569       }
1570 
1571       if (a == 0)
1572       {
1573          *this = static_cast<limb_type>(0u);
1574       }
1575 
1576       if (a == 1)
1577       {
1578          *this = static_cast<limb_type>(1u);
1579       }
1580 
1581       if ((boost::math::isinf)(a) || (boost::math::isnan)(a))
1582       {
1583          BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
1584       }
1585 
1586       int         e = 0;
1587       long double f(0), term(0);
1588       *this = static_cast<limb_type>(0u);
1589 
1590       f = frexp(a, &e);
1591 
1592 #ifdef BOOST_NO_CXX11_CONSTEXPR
1593       static const limb_type shift = std::numeric_limits<limb_type>::digits;
1594 #else
1595       constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
1596 #endif
1597 
1598       while (f)
1599       {
1600          // extract int sized bits from f:
1601          f    = ldexp(f, shift);
1602          term = floor(f);
1603          e -= shift;
1604          eval_left_shift(*this, shift);
1605          if (term > 0)
1606             eval_add(*this, static_cast<limb_type>(term));
1607          else
1608             eval_subtract(*this, static_cast<limb_type>(-term));
1609          f -= term;
1610       }
1611       if (e > 0)
1612          eval_left_shift(*this, e);
1613       else if (e < 0)
1614          eval_right_shift(*this, -e);
1615    }
1616 
1617  public:
1618    template <class Arithmetic>
operator =boost::multiprecision::backends::cpp_int_backend1619    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR 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())))
1620    {
1621       do_assign_arithmetic(val, trivial_tag());
1622       return *this;
1623    }
1624 
1625  private:
do_assign_stringboost::multiprecision::backends::cpp_int_backend1626    void do_assign_string(const char* s, const mpl::true_&)
1627    {
1628       std::size_t n  = s ? std::strlen(s) : 0;
1629       *this          = 0;
1630       unsigned radix = 10;
1631       bool     isneg = false;
1632       if (n && (*s == '-'))
1633       {
1634          --n;
1635          ++s;
1636          isneg = true;
1637       }
1638       if (n && (*s == '0'))
1639       {
1640          if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1641          {
1642             radix = 16;
1643             s += 2;
1644             n -= 2;
1645          }
1646          else
1647          {
1648             radix = 8;
1649             n -= 1;
1650          }
1651       }
1652       if (n)
1653       {
1654          unsigned val;
1655          while (*s)
1656          {
1657             if (*s >= '0' && *s <= '9')
1658                val = *s - '0';
1659             else if (*s >= 'a' && *s <= 'f')
1660                val = 10 + *s - 'a';
1661             else if (*s >= 'A' && *s <= 'F')
1662                val = 10 + *s - 'A';
1663             else
1664                val = radix + 1;
1665             if (val >= radix)
1666             {
1667                BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1668             }
1669             *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1670             *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1671             ++s;
1672          }
1673       }
1674       if (isneg)
1675          this->negate();
1676    }
do_assign_stringboost::multiprecision::backends::cpp_int_backend1677    void do_assign_string(const char* s, const mpl::false_&)
1678    {
1679       using default_ops::eval_add;
1680       using default_ops::eval_multiply;
1681       std::size_t n  = s ? std::strlen(s) : 0;
1682       *this          = static_cast<limb_type>(0u);
1683       unsigned radix = 10;
1684       bool     isneg = false;
1685       if (n && (*s == '-'))
1686       {
1687          --n;
1688          ++s;
1689          isneg = true;
1690       }
1691       if (n && (*s == '0'))
1692       {
1693          if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1694          {
1695             radix = 16;
1696             s += 2;
1697             n -= 2;
1698          }
1699          else
1700          {
1701             radix = 8;
1702             n -= 1;
1703          }
1704       }
1705       //
1706       // Exception guarantee: create the result in stack variable "result"
1707       // then do a swap at the end.  In the event of a throw, *this will
1708       // be left unchanged.
1709       //
1710       cpp_int_backend result;
1711       if (n)
1712       {
1713          if (radix == 16)
1714          {
1715             while (*s == '0')
1716                ++s;
1717             std::size_t bitcount = 4 * std::strlen(s);
1718             limb_type   val;
1719             std::size_t limb, shift;
1720             if (bitcount > 4)
1721                bitcount -= 4;
1722             else
1723                bitcount = 0;
1724             std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1725             result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1726             std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1727             while (*s)
1728             {
1729                if (*s >= '0' && *s <= '9')
1730                   val = *s - '0';
1731                else if (*s >= 'a' && *s <= 'f')
1732                   val = 10 + *s - 'a';
1733                else if (*s >= 'A' && *s <= 'F')
1734                   val = 10 + *s - 'A';
1735                else
1736                {
1737                   BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1738                }
1739                limb  = bitcount / (sizeof(limb_type) * CHAR_BIT);
1740                shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1741                val <<= shift;
1742                if (result.size() > limb)
1743                {
1744                   result.limbs()[limb] |= val;
1745                }
1746                ++s;
1747                bitcount -= 4;
1748             }
1749             result.normalize();
1750          }
1751          else if (radix == 8)
1752          {
1753             while (*s == '0')
1754                ++s;
1755             std::size_t bitcount = 3 * std::strlen(s);
1756             limb_type   val;
1757             std::size_t limb, shift;
1758             if (bitcount > 3)
1759                bitcount -= 3;
1760             else
1761                bitcount = 0;
1762             std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1763             result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
1764             std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1765             while (*s)
1766             {
1767                if (*s >= '0' && *s <= '7')
1768                   val = *s - '0';
1769                else
1770                {
1771                   BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1772                }
1773                limb  = bitcount / (sizeof(limb_type) * CHAR_BIT);
1774                shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1775                if (result.size() > limb)
1776                {
1777                   result.limbs()[limb] |= (val << shift);
1778                   if (shift > sizeof(limb_type) * CHAR_BIT - 3)
1779                   {
1780                      // Deal with the bits in val that overflow into the next limb:
1781                      val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1782                      if (val)
1783                      {
1784                         // If this is the most-significant-limb, we may need to allocate an extra one for the overflow:
1785                         if (limb + 1 == newsize)
1786                            result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1787                         if (result.size() > limb + 1)
1788                         {
1789                            result.limbs()[limb + 1] |= val;
1790                         }
1791                      }
1792                   }
1793                }
1794                ++s;
1795                bitcount -= 3;
1796             }
1797             result.normalize();
1798          }
1799          else
1800          {
1801             // Base 10, we extract blocks of size 10^9 at a time, that way
1802             // the number of multiplications is kept to a minimum:
1803             limb_type block_mult = max_block_10;
1804             while (*s)
1805             {
1806                limb_type block = 0;
1807                for (unsigned i = 0; i < digits_per_block_10; ++i)
1808                {
1809                   limb_type val;
1810                   if (*s >= '0' && *s <= '9')
1811                      val = *s - '0';
1812                   else
1813                      BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1814                   block *= 10;
1815                   block += val;
1816                   if (!*++s)
1817                   {
1818                      block_mult = block_multiplier(i);
1819                      break;
1820                   }
1821                }
1822                eval_multiply(result, block_mult);
1823                eval_add(result, block);
1824             }
1825          }
1826       }
1827       if (isneg)
1828          result.negate();
1829       result.swap(*this);
1830    }
1831 
1832  public:
operator =boost::multiprecision::backends::cpp_int_backend1833    cpp_int_backend& operator=(const char* s)
1834    {
1835       do_assign_string(s, trivial_tag());
1836       return *this;
1837    }
swapboost::multiprecision::backends::cpp_int_backend1838    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) BOOST_NOEXCEPT
1839    {
1840       this->do_swap(o);
1841    }
1842 
1843  private:
do_get_trivial_stringboost::multiprecision::backends::cpp_int_backend1844    std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::false_&) const
1845    {
1846       typedef typename mpl::if_c<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type io_type;
1847       if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1848          BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1849       std::stringstream ss;
1850       ss.flags(f & ~std::ios_base::showpos);
1851       ss << static_cast<io_type>(*this->limbs());
1852       std::string result;
1853       if (this->sign())
1854          result += '-';
1855       else if (f & std::ios_base::showpos)
1856          result += '+';
1857       result += ss.str();
1858       return result;
1859    }
do_get_trivial_stringboost::multiprecision::backends::cpp_int_backend1860    std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::true_&) const
1861    {
1862       // Even though we have only one limb, we can't do IO on it :-(
1863       int base = 10;
1864       if ((f & std::ios_base::oct) == std::ios_base::oct)
1865          base = 8;
1866       else if ((f & std::ios_base::hex) == std::ios_base::hex)
1867          base = 16;
1868       std::string result;
1869 
1870       unsigned Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1871 
1872       if (base == 8 || base == 16)
1873       {
1874          if (this->sign())
1875             BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1876          limb_type                           shift = base == 8 ? 3 : 4;
1877          limb_type                           mask  = static_cast<limb_type>((1u << shift) - 1);
1878          typename base_type::local_limb_type v     = *this->limbs();
1879          result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1880          std::string::difference_type pos      = result.size() - 1;
1881          char                         letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1882          for (unsigned i = 0; i < Bits / shift; ++i)
1883          {
1884             char c = '0' + static_cast<char>(v & mask);
1885             if (c > '9')
1886                c += letter_a - '9' - 1;
1887             result[pos--] = c;
1888             v >>= shift;
1889          }
1890          if (Bits % shift)
1891          {
1892             mask   = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1893             char c = '0' + static_cast<char>(v & mask);
1894             if (c > '9')
1895                c += letter_a - '9';
1896             result[pos] = c;
1897          }
1898          //
1899          // Get rid of leading zeros:
1900          //
1901          std::string::size_type n = result.find_first_not_of('0');
1902          if (!result.empty() && (n == std::string::npos))
1903             n = result.size() - 1;
1904          result.erase(0, n);
1905          if (f & std::ios_base::showbase)
1906          {
1907             const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
1908             result.insert(static_cast<std::string::size_type>(0), pp);
1909          }
1910       }
1911       else
1912       {
1913          result.assign(Bits / 3 + 1, '0');
1914          std::string::difference_type        pos = result.size() - 1;
1915          typename base_type::local_limb_type v(*this->limbs());
1916          bool                                neg = false;
1917          if (this->sign())
1918          {
1919             neg = true;
1920          }
1921          while (v)
1922          {
1923             result[pos] = (v % 10) + '0';
1924             --pos;
1925             v /= 10;
1926          }
1927          std::string::size_type n = result.find_first_not_of('0');
1928          result.erase(0, n);
1929          if (result.empty())
1930             result = "0";
1931          if (neg)
1932             result.insert(static_cast<std::string::size_type>(0), 1, '-');
1933          else if (f & std::ios_base::showpos)
1934             result.insert(static_cast<std::string::size_type>(0), 1, '+');
1935       }
1936       return result;
1937    }
do_get_stringboost::multiprecision::backends::cpp_int_backend1938    std::string do_get_string(std::ios_base::fmtflags f, const mpl::true_&) const
1939    {
1940 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
1941       return do_get_trivial_string(f, mpl::bool_<is_same<typename base_type::local_limb_type, double_limb_type>::value>());
1942 #else
1943       return do_get_trivial_string(f, mpl::bool_<false>());
1944 #endif
1945    }
do_get_stringboost::multiprecision::backends::cpp_int_backend1946    std::string do_get_string(std::ios_base::fmtflags f, const mpl::false_&) const
1947    {
1948       using default_ops::eval_get_sign;
1949       int base = 10;
1950       if ((f & std::ios_base::oct) == std::ios_base::oct)
1951          base = 8;
1952       else if ((f & std::ios_base::hex) == std::ios_base::hex)
1953          base = 16;
1954       std::string result;
1955 
1956       unsigned Bits = this->size() * base_type::limb_bits;
1957 
1958       if (base == 8 || base == 16)
1959       {
1960          if (this->sign())
1961             BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1962          limb_type       shift = base == 8 ? 3 : 4;
1963          limb_type       mask  = static_cast<limb_type>((1u << shift) - 1);
1964          cpp_int_backend t(*this);
1965          result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
1966          std::string::difference_type pos      = result.size() - 1;
1967          char                         letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1968          for (unsigned i = 0; i < Bits / shift; ++i)
1969          {
1970             char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1971             if (c > '9')
1972                c += letter_a - '9' - 1;
1973             result[pos--] = c;
1974             eval_right_shift(t, shift);
1975          }
1976          if (Bits % shift)
1977          {
1978             mask   = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1979             char c = '0' + static_cast<char>(t.limbs()[0] & mask);
1980             if (c > '9')
1981                c += letter_a - '9';
1982             result[pos] = c;
1983          }
1984          //
1985          // Get rid of leading zeros:
1986          //
1987          std::string::size_type n = result.find_first_not_of('0');
1988          if (!result.empty() && (n == std::string::npos))
1989             n = result.size() - 1;
1990          result.erase(0, n);
1991          if (f & std::ios_base::showbase)
1992          {
1993             const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
1994             result.insert(static_cast<std::string::size_type>(0), pp);
1995          }
1996       }
1997       else
1998       {
1999          result.assign(Bits / 3 + 1, '0');
2000          std::string::difference_type pos = result.size() - 1;
2001          cpp_int_backend              t(*this);
2002          cpp_int_backend              r;
2003          bool                         neg = false;
2004          if (t.sign())
2005          {
2006             t.negate();
2007             neg = true;
2008          }
2009          if (this->size() == 1)
2010          {
2011             result = boost::lexical_cast<std::string>(t.limbs()[0]);
2012          }
2013          else
2014          {
2015             cpp_int_backend block10;
2016             block10 = max_block_10;
2017             while (eval_get_sign(t) != 0)
2018             {
2019                cpp_int_backend t2;
2020                divide_unsigned_helper(&t2, t, block10, r);
2021                t           = t2;
2022                limb_type v = r.limbs()[0];
2023                for (unsigned i = 0; i < digits_per_block_10; ++i)
2024                {
2025                   char c = '0' + v % 10;
2026                   v /= 10;
2027                   result[pos] = c;
2028                   if (pos-- == 0)
2029                      break;
2030                }
2031             }
2032          }
2033          std::string::size_type n = result.find_first_not_of('0');
2034          result.erase(0, n);
2035          if (result.empty())
2036             result = "0";
2037          if (neg)
2038             result.insert(static_cast<std::string::size_type>(0), 1, '-');
2039          else if (f & std::ios_base::showpos)
2040             result.insert(static_cast<std::string::size_type>(0), 1, '+');
2041       }
2042       return result;
2043    }
2044 
2045  public:
strboost::multiprecision::backends::cpp_int_backend2046    std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
2047    {
2048       return do_get_string(f, trivial_tag());
2049    }
2050 
2051  private:
2052    template <class Container>
construct_from_containerboost::multiprecision::backends::cpp_int_backend2053    void construct_from_container(const Container& c, const mpl::false_&)
2054    {
2055       //
2056       // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2057       //
2058       unsigned newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
2059       if (c.size() % sizeof(limb_type))
2060       {
2061          ++newsize;
2062       }
2063       if (newsize)
2064       {
2065          this->resize(newsize, newsize); // May throw
2066          std::memset(this->limbs(), 0, this->size());
2067          typename Container::const_iterator i(c.begin()), j(c.end());
2068          unsigned                           byte_location = static_cast<unsigned>(c.size() - 1);
2069          while (i != j)
2070          {
2071             unsigned limb  = byte_location / sizeof(limb_type);
2072             unsigned shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
2073             if (this->size() > limb)
2074                this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2075             ++i;
2076             --byte_location;
2077          }
2078       }
2079    }
2080    template <class Container>
construct_from_containerboost::multiprecision::backends::cpp_int_backend2081    BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const mpl::true_&)
2082    {
2083       //
2084       // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2085       //
2086       typedef typename base_type::local_limb_type local_limb_type;
2087       *this->limbs() = 0;
2088       if (c.size())
2089       {
2090          typename Container::const_iterator i(c.begin()), j(c.end());
2091          unsigned                           byte_location = static_cast<unsigned>(c.size() - 1);
2092          while (i != j)
2093          {
2094             unsigned limb  = byte_location / sizeof(local_limb_type);
2095             unsigned shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
2096             if (limb == 0)
2097                this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2098             ++i;
2099             --byte_location;
2100          }
2101       }
2102    }
2103 
2104  public:
2105    template <class Container>
cpp_int_backendboost::multiprecision::backends::cpp_int_backend2106    BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(const Container& c, typename boost::enable_if_c<boost::multiprecision::detail::is_byte_container<Container>::value>::type const* = 0)
2107    {
2108       //
2109       // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
2110       //
2111       construct_from_container(c, trivial_tag());
2112    }
2113    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2114    BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::false_&, const mpl::false_&) const BOOST_NOEXCEPT
2115    {
2116       if (this->sign() != o.sign())
2117          return this->sign() ? -1 : 1;
2118 
2119       // Only do the compare if the same sign:
2120       int result = compare_unsigned(o);
2121 
2122       if (this->sign())
2123          result = -result;
2124       return result;
2125    }
2126    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2127    BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::true_&, const mpl::false_&) const
2128    {
2129       cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
2130       return t.compare(o);
2131    }
2132    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2133    BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::false_&, const mpl::true_&) const
2134    {
2135       cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
2136       return compare(t);
2137    }
2138    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_impboost::multiprecision::backends::cpp_int_backend2139    BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const mpl::true_&, const mpl::true_&) const BOOST_NOEXCEPT
2140    {
2141       if (this->sign())
2142       {
2143          if (o.sign())
2144          {
2145             return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
2146          }
2147          else
2148             return -1;
2149       }
2150       else
2151       {
2152          if (o.sign())
2153             return 1;
2154          return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
2155       }
2156    }
2157    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compareboost::multiprecision::backends::cpp_int_backend2158    BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const BOOST_NOEXCEPT
2159    {
2160       typedef mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>      t1;
2161       typedef mpl::bool_<is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value> t2;
2162       return compare_imp(o, t1(), t2());
2163    }
2164    template <unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
compare_unsignedboost::multiprecision::backends::cpp_int_backend2165    BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const BOOST_NOEXCEPT
2166    {
2167       if (this->size() != o.size())
2168       {
2169          return this->size() > o.size() ? 1 : -1;
2170       }
2171       typename base_type::const_limb_pointer pa = this->limbs();
2172       typename base_type::const_limb_pointer pb = o.limbs();
2173       for (int i = this->size() - 1; i >= 0; --i)
2174       {
2175          if (pa[i] != pb[i])
2176             return pa[i] > pb[i] ? 1 : -1;
2177       }
2178       return 0;
2179    }
2180    template <class Arithmetic>
compareboost::multiprecision::backends::cpp_int_backend2181    BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if<is_arithmetic<Arithmetic>, int>::type compare(Arithmetic i) const
2182    {
2183       // braindead version:
2184       cpp_int_backend t;
2185       t = i;
2186       return compare(t);
2187    }
2188 };
2189 
2190 } // namespace backends
2191 
2192 namespace default_ops {
2193 
2194 template <class Backend>
2195 struct double_precision_type;
2196 
2197 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2198 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2199 {
2200    typedef typename mpl::if_c<
2201        backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2202        backends::cpp_int_backend<
2203            (is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
2204                                       : MinBits),
2205            2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2206            SignType,
2207            Checked,
2208            Allocator>,
2209        backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type type;
2210 };
2211 
2212 } // namespace default_ops
2213 
2214 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked>
2215 struct expression_template_default<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, void> >
2216 {
2217    static const expression_template_option value = et_off;
2218 };
2219 
2220 using boost::multiprecision::backends::cpp_int_backend;
2221 
2222 template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2223 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public mpl::int_<number_kind_integer>
2224 {};
2225 
2226 typedef number<cpp_int_backend<> >           cpp_int;
2227 typedef rational_adaptor<cpp_int_backend<> > cpp_rational_backend;
2228 typedef number<cpp_rational_backend>         cpp_rational;
2229 
2230 // Fixed precision unsigned types:
2231 typedef number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> >   uint128_t;
2232 typedef number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> >   uint256_t;
2233 typedef number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> >   uint512_t;
2234 typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> > uint1024_t;
2235 
2236 // Fixed precision signed types:
2237 typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, void> >   int128_t;
2238 typedef number<cpp_int_backend<256, 256, signed_magnitude, unchecked, void> >   int256_t;
2239 typedef number<cpp_int_backend<512, 512, signed_magnitude, unchecked, void> >   int512_t;
2240 typedef number<cpp_int_backend<1024, 1024, signed_magnitude, unchecked, void> > int1024_t;
2241 
2242 // Over again, but with checking enabled this time:
2243 typedef number<cpp_int_backend<0, 0, signed_magnitude, checked> >           checked_cpp_int;
2244 typedef rational_adaptor<cpp_int_backend<0, 0, signed_magnitude, checked> > checked_cpp_rational_backend;
2245 typedef number<checked_cpp_rational_backend>                                checked_cpp_rational;
2246 // Fixed precision unsigned types:
2247 typedef number<cpp_int_backend<128, 128, unsigned_magnitude, checked, void> >   checked_uint128_t;
2248 typedef number<cpp_int_backend<256, 256, unsigned_magnitude, checked, void> >   checked_uint256_t;
2249 typedef number<cpp_int_backend<512, 512, unsigned_magnitude, checked, void> >   checked_uint512_t;
2250 typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, checked, void> > checked_uint1024_t;
2251 
2252 // Fixed precision signed types:
2253 typedef number<cpp_int_backend<128, 128, signed_magnitude, checked, void> >   checked_int128_t;
2254 typedef number<cpp_int_backend<256, 256, signed_magnitude, checked, void> >   checked_int256_t;
2255 typedef number<cpp_int_backend<512, 512, signed_magnitude, checked, void> >   checked_int512_t;
2256 typedef number<cpp_int_backend<1024, 1024, signed_magnitude, checked, void> > checked_int1024_t;
2257 
2258 #ifdef BOOST_NO_SFINAE_EXPR
2259 
2260 namespace detail {
2261 
2262 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>
2263 struct is_explicitly_convertible<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> > : public mpl::true_
2264 {};
2265 
2266 } // namespace detail
2267 #endif
2268 
2269 #ifdef _MSC_VER
2270 #pragma warning(pop)
2271 #endif
2272 
2273 }} // namespace boost::multiprecision
2274 
2275 //
2276 // Last of all we include the implementations of all the eval_* non member functions:
2277 //
2278 #include <boost/multiprecision/cpp_int/comparison.hpp>
2279 #include <boost/multiprecision/cpp_int/add.hpp>
2280 #include <boost/multiprecision/cpp_int/multiply.hpp>
2281 #include <boost/multiprecision/cpp_int/divide.hpp>
2282 #include <boost/multiprecision/cpp_int/bitwise.hpp>
2283 #include <boost/multiprecision/cpp_int/misc.hpp>
2284 #include <boost/multiprecision/cpp_int/limits.hpp>
2285 #ifdef BOOST_MP_USER_DEFINED_LITERALS
2286 #include <boost/multiprecision/cpp_int/literals.hpp>
2287 #endif
2288 #include <boost/multiprecision/cpp_int/serialize.hpp>
2289 #include <boost/multiprecision/cpp_int/import_export.hpp>
2290 
2291 #endif
2292