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