File indexing completed on 2025-01-18 09:42:28
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_MP_CPP_INT_HPP
0009 #define BOOST_MP_CPP_INT_HPP
0010
0011 #include <cstdint>
0012 #include <cstring>
0013 #include <iostream>
0014 #include <iomanip>
0015 #include <type_traits>
0016 #include <string>
0017 #include <boost/multiprecision/detail/standalone_config.hpp>
0018 #include <boost/multiprecision/detail/endian.hpp>
0019 #include <boost/multiprecision/number.hpp>
0020 #include <boost/multiprecision/detail/integer_ops.hpp>
0021 #include <boost/multiprecision/detail/rebind.hpp>
0022 #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
0023 #include <boost/multiprecision/rational_adaptor.hpp>
0024 #include <boost/multiprecision/traits/is_byte_container.hpp>
0025 #include <boost/multiprecision/cpp_int/checked.hpp>
0026 #include <boost/multiprecision/detail/constexpr.hpp>
0027 #include <boost/multiprecision/detail/float128_functions.hpp>
0028 #include <boost/multiprecision/cpp_int/value_pack.hpp>
0029 #include <boost/multiprecision/detail/empty_value.hpp>
0030 #include <boost/multiprecision/detail/no_exceptions_support.hpp>
0031 #include <boost/multiprecision/detail/assert.hpp>
0032 #include <boost/multiprecision/detail/fpclassify.hpp>
0033
0034 namespace boost {
0035 namespace multiprecision {
0036
0037 #ifdef BOOST_MSVC
0038 #pragma warning(push)
0039 #pragma warning(disable : 4307)
0040 #pragma warning(disable : 4127)
0041 #pragma warning(disable : 4702)
0042 #endif
0043 #if defined(__GNUC__) && !defined(__clang__)
0044
0045
0046 #pragma GCC diagnostic push
0047 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
0048 #endif
0049
0050 namespace detail {
0051
0052 template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0053 struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::false_type
0054 {};
0055
0056 }
0057
0058 namespace backends {
0059
0060 namespace detail {
0061 template <std::size_t Value1, std::size_t Value2>
0062 struct static_unsigned_max
0063 {
0064 static constexpr std::size_t value = (Value1 > Value2) ? Value1 : Value2;
0065 };
0066 }
0067
0068 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
0069 struct cpp_int_base;
0070
0071
0072
0073 template <class T>
0074 struct max_precision;
0075
0076 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0077 struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
0078 {
0079 static constexpr std::size_t value = std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value
0080 : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : SIZE_MAX);
0081 };
0082
0083 template <class T>
0084 struct min_precision;
0085
0086 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0087 struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
0088 {
0089 static constexpr std::size_t value = (std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value : MinBits);
0090 };
0091
0092
0093
0094
0095 template <class T>
0096 struct is_trivial_cpp_int
0097 {
0098 static constexpr bool value = false;
0099 };
0100
0101 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0102 struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
0103 {
0104 using self = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
0105 static constexpr bool value = std::is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
0106 };
0107
0108 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0109 struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
0110 {
0111 static constexpr bool value = true;
0112 };
0113
0114 }
0115
0116
0117
0118 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0119 struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
0120 : public std::integral_constant<bool, (SignType == unsigned_magnitude) || (SignType == unsigned_packed)>
0121 {};
0122
0123 namespace backends {
0124
0125
0126
0127
0128
0129 template <class T, class U>
0130 struct is_implicit_cpp_int_conversion;
0131
0132 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>
0133 struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
0134 {
0135 using t1 = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
0136 using t2 = cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>;
0137 static constexpr bool value =
0138 (is_signed_number<t2>::value || !is_signed_number<t1>::value) && (max_precision<t1>::value <= max_precision<t2>::value);
0139 };
0140
0141
0142
0143
0144 template <class T>
0145 struct is_non_throwing_cpp_int : public std::integral_constant<bool, false>
0146 {};
0147 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType>
0148 struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public std::integral_constant<bool, true>
0149 {};
0150
0151
0152
0153
0154 template <class T>
0155 struct is_fixed_precision;
0156 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
0157 struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
0158 : public std::integral_constant<bool, max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != SIZE_MAX>
0159 {};
0160
0161 namespace detail {
0162
0163 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>&)
0164 {
0165 if (new_size < min_size)
0166 BOOST_MP_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
0167 }
0168 inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(std::size_t , std::size_t , const std::integral_constant<int, unchecked>&) {}
0169
0170 template <class U>
0171 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool b, U limb, U mask, const std::integral_constant<int, checked>&)
0172 {
0173
0174 if (b && (limb & ~mask))
0175 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."));
0176 }
0177 template <class U>
0178 inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool , U , U , const std::integral_constant<int, unchecked>&) {}
0179
0180 }
0181
0182
0183
0184
0185
0186 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0187 struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>
0188 : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
0189 {
0190 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, bool trivial2>
0191 friend struct cpp_int_base;
0192
0193 using allocator_type = typename detail::rebind<limb_type, Allocator>::type;
0194 using limb_pointer = typename std::allocator_traits<allocator_type>::pointer ;
0195 using const_limb_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
0196 using checked_type = std::integral_constant<int, Checked>;
0197
0198
0199
0200
0201 static_assert(!std::is_void<Allocator>::value, "Allocator must not be void here");
0202
0203 using base_type = boost::multiprecision::detail::empty_value<allocator_type>;
0204
0205 private:
0206 struct limb_data
0207 {
0208 std::size_t capacity;
0209 limb_pointer data;
0210 };
0211
0212 public:
0213 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
0214 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
0215 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
0216 static constexpr std::size_t internal_limb_count =
0217 MinBits
0218 ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
0219 : (sizeof(limb_data) / sizeof(limb_type)) > 1 ? (sizeof(limb_data) / sizeof(limb_type)) : 2;
0220 private:
0221 union data_type
0222 {
0223 limb_data ld;
0224 limb_type la[internal_limb_count];
0225 limb_type first;
0226 double_limb_type double_first;
0227
0228 constexpr data_type() noexcept : first(0) {}
0229 constexpr data_type(limb_type i) noexcept : first(i) {}
0230 constexpr data_type(signed_limb_type i) noexcept : first(static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i))) {}
0231 #if BOOST_MP_ENDIAN_LITTLE_BYTE
0232 constexpr data_type(double_limb_type i) noexcept : double_first(i)
0233 {}
0234 constexpr data_type(signed_double_limb_type i) noexcept : double_first(static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i))) {}
0235 #endif
0236 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !(defined(BOOST_MSVC) && (BOOST_MSVC < 1900))
0237 constexpr data_type(limb_type* limbs, std::size_t len) noexcept : ld{ len, limbs }
0238 {}
0239 #else
0240 constexpr data_type(limb_type* limbs, std::size_t len) noexcept
0241 {
0242 ld.capacity = len;
0243 ld.data = limbs;
0244 }
0245 #endif
0246 };
0247
0248 data_type m_data;
0249 std::size_t m_limbs;
0250 bool m_sign, m_internal, m_alias;
0251
0252 public:
0253
0254
0255
0256 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
0257 : m_data(i),
0258 m_limbs(1),
0259 m_sign(false),
0260 m_internal(true),
0261 m_alias(false) {}
0262 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
0263 : m_data(i),
0264 m_limbs(1),
0265 m_sign(i < 0),
0266 m_internal(true),
0267 m_alias(false) {}
0268 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
0269 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
0270 : m_data(i),
0271 m_limbs(i > max_limb_value ? 2 : 1),
0272 m_sign(false),
0273 m_internal(true),
0274 m_alias(false)
0275 {}
0276 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
0277 : m_data(i),
0278 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)),
0279 m_sign(i < 0),
0280 m_internal(true),
0281 m_alias(false) {}
0282 #endif
0283
0284
0285
0286 struct scoped_shared_storage : private boost::multiprecision::detail::empty_value<allocator_type>
0287 {
0288 private:
0289 limb_type* data;
0290 std::size_t capacity;
0291 std::size_t allocated;
0292 bool is_alias;
0293 allocator_type& allocator() noexcept { return boost::multiprecision::detail::empty_value<allocator_type>::get(); }
0294
0295 public:
0296 scoped_shared_storage(const allocator_type& a, std::size_t len)
0297 : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), a), capacity(len), allocated(0), is_alias(false)
0298 {
0299 data = allocator().allocate(len);
0300 }
0301 scoped_shared_storage(const cpp_int_base& i, std::size_t len)
0302 : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), i.allocator()), capacity(len), allocated(0), is_alias(false)
0303 {
0304 data = allocator().allocate(len);
0305 }
0306 scoped_shared_storage(limb_type* limbs, std::size_t n) : data(limbs), capacity(n), allocated(0), is_alias(true) {}
0307 ~scoped_shared_storage()
0308 {
0309 if(!is_alias)
0310 allocator().deallocate(data, capacity);
0311 }
0312 limb_type* allocate(std::size_t n) noexcept
0313 {
0314 limb_type* result = data + allocated;
0315 allocated += n;
0316 BOOST_MP_ASSERT(allocated <= capacity);
0317 return result;
0318 }
0319 void deallocate(std::size_t n)
0320 {
0321 BOOST_MP_ASSERT(n <= allocated);
0322 allocated -= n;
0323 }
0324 };
0325 explicit constexpr cpp_int_base(limb_type* data, std::size_t offset, std::size_t len) noexcept
0326 : m_data(data + offset, len),
0327 m_limbs(len),
0328 m_sign(false),
0329 m_internal(false),
0330 m_alias(true) {}
0331
0332
0333
0334 explicit constexpr cpp_int_base(const limb_type* data, std::size_t offset, std::size_t len) noexcept
0335 : m_data(const_cast<limb_type*>(data) + offset, len),
0336 m_limbs(len),
0337 m_sign(false),
0338 m_internal(false),
0339 m_alias(true) {}
0340 explicit cpp_int_base(scoped_shared_storage& data, std::size_t len) noexcept
0341 : m_data(data.allocate(len), len),
0342 m_limbs(len),
0343 m_sign(false),
0344 m_internal(false),
0345 m_alias(true) {}
0346
0347
0348
0349 BOOST_MP_FORCEINLINE allocator_type& allocator() noexcept { return base_type::get(); }
0350 BOOST_MP_FORCEINLINE const allocator_type& allocator() const noexcept { return base_type::get(); }
0351 BOOST_MP_FORCEINLINE std::size_t size() const noexcept { return m_limbs; }
0352 BOOST_MP_FORCEINLINE limb_pointer limbs() noexcept { return m_internal ? m_data.la : m_data.ld.data; }
0353 BOOST_MP_FORCEINLINE const_limb_pointer limbs() const noexcept { return m_internal ? m_data.la : m_data.ld.data; }
0354 BOOST_MP_FORCEINLINE std::size_t capacity() const noexcept { return m_internal ? internal_limb_count : m_data.ld.capacity; }
0355 BOOST_MP_FORCEINLINE bool sign() const noexcept { return m_sign; }
0356 void sign(bool b) noexcept
0357 {
0358 m_sign = b;
0359
0360 if (m_sign && (m_limbs == 1))
0361 {
0362 if (limbs()[0] == 0)
0363 m_sign = false;
0364 }
0365 }
0366 void resize(std::size_t new_size, std::size_t min_size)
0367 {
0368 constexpr std::size_t max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
0369
0370 if (new_size > max_limbs)
0371 new_size = max_limbs;
0372 detail::verify_new_size(new_size, min_size, checked_type());
0373
0374 std::size_t cap = capacity();
0375 if (new_size > cap)
0376 {
0377
0378 BOOST_MP_ASSERT(!m_alias);
0379
0380 cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
0381 limb_pointer pl = allocator().allocate(cap);
0382 std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
0383 if (!m_internal && !m_alias)
0384 allocator().deallocate(limbs(), capacity());
0385 else
0386 m_internal = false;
0387 m_limbs = new_size;
0388 m_data.ld.capacity = cap;
0389 m_data.ld.data = pl;
0390 }
0391 else
0392 {
0393 m_limbs = new_size;
0394 }
0395 }
0396 BOOST_MP_FORCEINLINE void normalize() noexcept
0397 {
0398 limb_pointer p = limbs();
0399 while ((m_limbs - 1) && !p[m_limbs - 1])
0400 --m_limbs;
0401 }
0402 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(), m_limbs(1), m_sign(false), m_internal(true), m_alias(false){}
0403 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)
0404 {
0405 if (m_alias)
0406 {
0407 m_data.ld = o.m_data.ld;
0408 }
0409 else
0410 {
0411 resize(o.size(), o.size());
0412 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0413 }
0414 }
0415
0416 cpp_int_base(cpp_int_base&& o)
0417 : 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)
0418 {
0419 if (m_internal)
0420 {
0421 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0422 }
0423 else
0424 {
0425 m_data.ld = o.m_data.ld;
0426 o.m_limbs = 0;
0427 o.m_internal = true;
0428 }
0429 }
0430 cpp_int_base& operator=(cpp_int_base&& o) noexcept
0431 {
0432 if (!m_internal && !m_alias)
0433 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
0434 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
0435 m_limbs = o.m_limbs;
0436 m_sign = o.m_sign;
0437 m_internal = o.m_internal;
0438 m_alias = o.m_alias;
0439 if (m_internal)
0440 {
0441 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0442 }
0443 else
0444 {
0445 m_data.ld = o.m_data.ld;
0446 o.m_limbs = 0;
0447 o.m_internal = true;
0448 }
0449 return *this;
0450 }
0451 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_int_check_type Checked2>
0452 cpp_int_base& operator=(cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>&& o) noexcept
0453 {
0454 if(o.m_internal)
0455 {
0456 m_sign = o.m_sign;
0457 this->resize(o.size(), o.size());
0458 std::memcpy(this->limbs(), o.limbs(), o.size() * sizeof(*(o.limbs())));
0459 return *this;
0460 }
0461 if (!m_internal && !m_alias)
0462 allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
0463 *static_cast<base_type*>(this) = static_cast<typename cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>::base_type&&>(o);
0464 m_limbs = o.m_limbs;
0465 m_sign = o.m_sign;
0466 m_internal = o.m_internal;
0467 m_alias = o.m_alias;
0468 m_data.ld.capacity = o.m_data.ld.capacity;
0469 m_data.ld.data = o.limbs();
0470 o.m_limbs = 0;
0471 o.m_internal = true;
0472 return *this;
0473 }
0474 BOOST_MP_FORCEINLINE ~cpp_int_base() noexcept
0475 {
0476 if (!m_internal && !m_alias)
0477 allocator().deallocate(limbs(), capacity());
0478 }
0479 void assign(const cpp_int_base& o)
0480 {
0481 if (this != &o)
0482 {
0483 static_cast<base_type&>(*this) = static_cast<const base_type&>(o);
0484 m_limbs = 0;
0485 resize(o.size(), o.size());
0486 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0487 m_sign = o.m_sign;
0488 }
0489 }
0490 BOOST_MP_FORCEINLINE void negate() noexcept
0491 {
0492 m_sign = !m_sign;
0493
0494 if (m_sign && (m_limbs == 1))
0495 {
0496 if (limbs()[0] == 0)
0497 m_sign = false;
0498 }
0499 }
0500 BOOST_MP_FORCEINLINE bool isneg() const noexcept
0501 {
0502 return m_sign;
0503 }
0504 BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) noexcept
0505 {
0506 std::swap(m_data, o.m_data);
0507 std::swap(m_sign, o.m_sign);
0508 std::swap(m_internal, o.m_internal);
0509 std::swap(m_limbs, o.m_limbs);
0510 std::swap(m_alias, o.m_alias);
0511 }
0512
0513 protected:
0514 template <class A>
0515 void check_in_range(const A&) noexcept {}
0516 };
0517
0518 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0519 constexpr std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
0520 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0521 constexpr limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
0522 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0523 constexpr limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
0524 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0525 constexpr std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
0526
0527 template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
0528 struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false>
0529 : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
0530 {
0531
0532
0533
0534
0535 static_assert(((sizeof(Allocator) == 0) && !std::is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
0536 };
0537
0538
0539
0540 template <std::size_t MinBits, cpp_int_check_type Checked>
0541 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
0542 {
0543 using limb_pointer = limb_type* ;
0544 using const_limb_pointer = const limb_type* ;
0545 using checked_type = std::integral_constant<int, Checked>;
0546
0547 struct scoped_shared_storage
0548 {
0549 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
0550 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
0551 };
0552
0553
0554
0555
0556 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?");
0557
0558 public:
0559 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
0560 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
0561 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
0562 static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
0563 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
0564 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
0565
0566 private:
0567 union data_type
0568 {
0569 limb_type m_data[internal_limb_count];
0570 limb_type m_first_limb;
0571 double_limb_type m_double_first_limb;
0572
0573 constexpr data_type()
0574 : m_data{0}
0575 {}
0576 constexpr data_type(limb_type i)
0577 : m_data{i}
0578 {}
0579 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0580 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
0581 {}
0582 #endif
0583 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
0584 {
0585 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0586 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
0587 {
0588 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
0589 *this = t;
0590 }
0591 #endif
0592 }
0593 template <limb_type... VALUES>
0594 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
0595 {}
0596 } m_wrapper;
0597 std::uint16_t m_limbs;
0598 bool m_sign;
0599
0600 public:
0601
0602
0603
0604 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
0605 : m_wrapper(i),
0606 m_limbs(1),
0607 m_sign(false) {}
0608 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
0609 : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)),
0610 m_limbs(1),
0611 m_sign(i < 0) {}
0612 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
0613 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
0614 : m_wrapper(i),
0615 m_limbs(i > max_limb_value ? 2 : 1),
0616 m_sign(false)
0617 {}
0618 BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
0619 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
0620 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)),
0621 m_sign(i < 0) {}
0622 #endif
0623 template <limb_type... VALUES>
0624 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
0625 : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false)
0626 {}
0627 constexpr cpp_int_base(literals::detail::value_pack<> i)
0628 : m_wrapper(i), m_limbs(1), m_sign(false) {}
0629 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
0630 : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
0631 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(0), m_sign(false)
0632 {}
0633
0634
0635
0636 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
0637
0638
0639
0640 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
0641 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
0642 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
0643 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return m_sign; }
0644 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
0645 {
0646 m_sign = b;
0647
0648 if (m_sign && (m_limbs == 1))
0649 {
0650 if (limbs()[0] == 0)
0651 m_sign = false;
0652 }
0653 }
0654 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
0655 {
0656 m_limbs = static_cast<std::uint16_t>((std::min)(new_size, internal_limb_count));
0657 detail::verify_new_size(m_limbs, min_size, checked_type());
0658 }
0659 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
0660 {
0661 limb_pointer p = limbs();
0662 detail::verify_limb_mask(m_limbs == internal_limb_count, p[m_limbs - 1], upper_limb_mask, checked_type());
0663 p[internal_limb_count - 1] &= upper_limb_mask;
0664 while ((m_limbs - 1) && !p[m_limbs - 1])
0665 --m_limbs;
0666 if ((m_limbs == 1) && (!*p))
0667 m_sign = false;
0668 }
0669
0670 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
0671
0672 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
0673 : m_wrapper(o.m_wrapper),
0674 m_limbs(o.m_limbs),
0675 m_sign(o.m_sign) {}
0676
0677
0678
0679 void BOOST_MP_CXX14_CONSTEXPR assign(const cpp_int_base& o) noexcept
0680 {
0681 if (this != &o)
0682 {
0683 m_limbs = o.m_limbs;
0684 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0685 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
0686 {
0687 for (std::size_t i = 0; i < m_limbs; ++i)
0688 limbs()[i] = o.limbs()[i];
0689 }
0690 else
0691 #endif
0692 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
0693 m_sign = o.m_sign;
0694 }
0695 }
0696 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
0697 {
0698 m_sign = !m_sign;
0699
0700 if (m_sign && (m_limbs == 1))
0701 {
0702 if (limbs()[0] == 0)
0703 m_sign = false;
0704 }
0705 }
0706 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
0707 {
0708 return m_sign;
0709 }
0710 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
0711 {
0712 for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
0713 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
0714 std_constexpr::swap(m_sign, o.m_sign);
0715 std_constexpr::swap(m_limbs, o.m_limbs);
0716 }
0717
0718 protected:
0719 template <class A>
0720 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
0721 };
0722
0723 template <std::size_t MinBits, cpp_int_check_type Checked>
0724 constexpr std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
0725 template <std::size_t MinBits, cpp_int_check_type Checked>
0726 constexpr limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
0727 template <std::size_t MinBits, cpp_int_check_type Checked>
0728 constexpr limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
0729 template <std::size_t MinBits, cpp_int_check_type Checked>
0730 constexpr std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
0731
0732
0733
0734 template <std::size_t MinBits, cpp_int_check_type Checked>
0735 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
0736 {
0737 using limb_pointer = limb_type* ;
0738 using const_limb_pointer = const limb_type* ;
0739 using checked_type = std::integral_constant<int, Checked>;
0740
0741 struct scoped_shared_storage
0742 {
0743 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
0744 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
0745 };
0746
0747
0748
0749 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?");
0750
0751 public:
0752 static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
0753 static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
0754 static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
0755 static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
0756 static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
0757 static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
0758
0759 private:
0760 union data_type
0761 {
0762 limb_type m_data[internal_limb_count];
0763 limb_type m_first_limb;
0764 double_limb_type m_double_first_limb;
0765
0766 constexpr data_type()
0767 : m_data{0}
0768 {}
0769 constexpr data_type(limb_type i)
0770 : m_data{i}
0771 {}
0772 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0773 constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
0774 {}
0775 #endif
0776 constexpr data_type(double_limb_type i) : m_double_first_limb(i)
0777 {
0778 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0779 if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
0780 {
0781 data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
0782 *this = t;
0783 }
0784 #endif
0785 }
0786 template <limb_type... VALUES>
0787 constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
0788 {}
0789 } m_wrapper;
0790 std::size_t m_limbs;
0791
0792 public:
0793
0794
0795
0796 BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
0797 : m_wrapper(i),
0798 m_limbs(1) {}
0799 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_limb_type i) noexcept((Checked == unchecked))
0800 : m_wrapper(static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i))),
0801 m_limbs(1)
0802 {
0803 if (i < 0)
0804 negate();
0805 }
0806 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
0807 BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
0808 : m_wrapper(i),
0809 m_limbs(i > max_limb_value ? 2 : 1)
0810 {}
0811 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_double_limb_type i) noexcept((Checked == unchecked))
0812 : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
0813 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))
0814 {
0815 if (i < 0)
0816 negate();
0817 }
0818 #endif
0819 template <limb_type... VALUES>
0820 constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
0821 : m_wrapper(i), m_limbs(sizeof...(VALUES))
0822 {}
0823 constexpr cpp_int_base(literals::detail::value_pack<>)
0824 : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
0825 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(1)
0826 {}
0827
0828
0829
0830 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
0831 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
0832 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
0833 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
0834 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
0835 {
0836 if (b)
0837 negate();
0838 }
0839 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
0840 {
0841 m_limbs = (std::min)(new_size, internal_limb_count);
0842 detail::verify_new_size(m_limbs, min_size, checked_type());
0843 }
0844 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
0845 {
0846 limb_pointer p = limbs();
0847 detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
0848 p[internal_limb_count - 1] &= upper_limb_mask;
0849 while ((m_limbs - 1) && !p[m_limbs - 1])
0850 --m_limbs;
0851 }
0852
0853 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept
0854 : m_wrapper(limb_type(0u)),
0855 m_limbs(1) {}
0856 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
0857 : m_wrapper(o.m_wrapper),
0858 m_limbs(o.m_limbs) {}
0859
0860
0861
0862
0863
0864 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
0865
0866 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
0867 {
0868 if (this != &o)
0869 {
0870 m_limbs = o.m_limbs;
0871 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0872 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
0873 {
0874 for (std::size_t i = 0; i < m_limbs; ++i)
0875 limbs()[i] = o.limbs()[i];
0876 }
0877 else
0878 #endif
0879 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0880 }
0881 }
0882
0883 private:
0884 void check_negate(const std::integral_constant<int, checked>&)
0885 {
0886 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
0887 }
0888 BOOST_MP_CXX14_CONSTEXPR void check_negate(const std::integral_constant<int, unchecked>&) {}
0889
0890 public:
0891 BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
0892 {
0893
0894
0895 if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
0896 return;
0897 check_negate(checked_type());
0898 std::size_t i = m_limbs;
0899 for (; i < internal_limb_count; ++i)
0900 m_wrapper.m_data[i] = 0;
0901 m_limbs = internal_limb_count;
0902 for (i = 0; i < internal_limb_count; ++i)
0903 m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
0904 normalize();
0905 eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
0906 }
0907 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
0908 {
0909 return false;
0910 }
0911 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
0912 {
0913 for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
0914 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
0915 std_constexpr::swap(m_limbs, o.m_limbs);
0916 }
0917
0918 protected:
0919 template <class A>
0920 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
0921 };
0922
0923 template <std::size_t MinBits, cpp_int_check_type Checked>
0924 constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
0925 template <std::size_t MinBits, cpp_int_check_type Checked>
0926 constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
0927 template <std::size_t MinBits, cpp_int_check_type Checked>
0928 constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
0929 template <std::size_t MinBits, cpp_int_check_type Checked>
0930 constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
0931
0932
0933
0934
0935 template <unsigned N, bool s>
0936 struct trivial_limb_type_imp
0937 {
0938 using type = double_limb_type;
0939 };
0940
0941 template <unsigned N>
0942 struct trivial_limb_type_imp<N, true>
0943 {
0944 using type = typename boost::multiprecision::detail::uint_t<N>::least;
0945 };
0946
0947 template <unsigned N>
0948 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(long long) * CHAR_BIT>
0949 {};
0950
0951
0952
0953 template <std::size_t MinBits, cpp_int_check_type Checked>
0954 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
0955 {
0956 using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
0957 using limb_pointer = local_limb_type*;
0958 using const_limb_pointer = const local_limb_type*;
0959 using checked_type = std::integral_constant<int, Checked>;
0960
0961 struct scoped_shared_storage
0962 {
0963 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
0964 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
0965 };
0966
0967 protected:
0968 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
0969 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));
0970
0971 private:
0972 local_limb_type m_data;
0973 bool m_sign;
0974
0975
0976
0977
0978 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?");
0979
0980 protected:
0981 template <class T>
0982 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
0983 check_in_range(T val, const std::integral_constant<int, checked>&)
0984 {
0985 using common_type = typename std::common_type<typename boost::multiprecision::detail::make_unsigned<T>::type, local_limb_type>::type;
0986
0987 if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
0988 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
0989 }
0990 template <class T>
0991 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
0992 check_in_range(T val, const std::integral_constant<int, checked>&)
0993 {
0994 using std::abs;
0995 using common_type = typename std::common_type<T, local_limb_type>::type;
0996
0997 if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
0998 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
0999 }
1000 template <class T, int C>
1001 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&) noexcept {}
1002
1003 template <class T>
1004 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())))
1005 {
1006 check_in_range(val, checked_type());
1007 }
1008
1009 public:
1010
1011
1012
1013 template <class SI>
1014 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>())))
1015 : 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) {}
1016 template <class SI>
1017 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>())))
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 {
1020 check_in_range(i);
1021 }
1022 template <class UI>
1023 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
1024 : m_data(static_cast<local_limb_type>(i) & limb_mask),
1025 m_sign(false) {}
1026 template <class UI>
1027 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>())))
1028 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
1029 #if !(defined(__clang__) && defined(__MINGW32__))
1030 template <class F>
1031 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
1032 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask),
1033 m_sign(i < 0) {}
1034 template <class F>
1035 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)
1036 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1037 #else
1038
1039
1040
1041
1042
1043
1044
1045 template <class F>
1046 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
1047 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask),
1048 m_sign(i < 0) {}
1049 template <class F>
1050 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)
1051 : 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); }
1052 #endif
1053
1054 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1055 : m_data(static_cast<local_limb_type>(0u)),
1056 m_sign(false)
1057 {}
1058 template <limb_type a>
1059 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1060 : m_data(static_cast<local_limb_type>(a)),
1061 m_sign(false) {}
1062 template <limb_type a, limb_type b>
1063 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1064 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
1065 m_sign(false) {}
1066 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) noexcept
1067 : m_data(a.m_data),
1068 m_sign(a.m_data ? !a.m_sign : false) {}
1069
1070
1071
1072 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1073
1074 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0), m_sign(false)
1075 {}
1076
1077
1078
1079 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1080 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1081 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1082 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool sign() const noexcept { return m_sign; }
1083 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
1084 {
1085 m_sign = b;
1086
1087 if (m_sign && !m_data)
1088 {
1089 m_sign = false;
1090 }
1091 }
1092 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t , std::size_t min_size)
1093 {
1094 detail::verify_new_size(2, min_size, checked_type());
1095 }
1096 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1097 {
1098 if (!m_data)
1099 m_sign = false;
1100 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1101 m_data &= limb_mask;
1102 }
1103
1104 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0), m_sign(false) {}
1105 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1106 : m_data(o.m_data),
1107 m_sign(o.m_sign) {}
1108
1109 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1110 {
1111 m_data = o.m_data;
1112 m_sign = o.m_sign;
1113 }
1114 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
1115 {
1116 m_sign = !m_sign;
1117
1118 if (m_data == 0)
1119 {
1120 m_sign = false;
1121 }
1122 }
1123 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
1124 {
1125 return m_sign;
1126 }
1127 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1128 {
1129 std_constexpr::swap(m_sign, o.m_sign);
1130 std_constexpr::swap(m_data, o.m_data);
1131 }
1132 };
1133
1134
1135
1136 template <std::size_t MinBits, cpp_int_check_type Checked>
1137 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
1138 {
1139 using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
1140 using limb_pointer = local_limb_type* ;
1141 using const_limb_pointer = const local_limb_type* ;
1142
1143 struct scoped_shared_storage
1144 {
1145 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
1146 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
1147 };
1148
1149 private:
1150 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
1151 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))
1152 : static_cast<local_limb_type>(~local_limb_type(0));
1153
1154 local_limb_type m_data;
1155
1156 using checked_type = std::integral_constant<int, Checked>;
1157
1158
1159
1160
1161 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?");
1162
1163 protected:
1164 template <class T>
1165 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits)))>::type
1166 check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, false>&)
1167 {
1168 using common_type = typename std::common_type<T, local_limb_type>::type;
1169
1170 if (static_cast<common_type>(val) > limb_mask)
1171 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1172 }
1173 template <class T>
1174 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, true>&)
1175 {
1176 using common_type = typename std::common_type<T, local_limb_type>::type;
1177
1178 if (static_cast<common_type>(val) > static_cast<common_type>(limb_mask))
1179 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1180 if (val < 0)
1181 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
1182 }
1183 template <class T, int C, bool B>
1184 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 {}
1185
1186 template <class T>
1187 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>())))
1188 {
1189 check_in_range(val, checked_type(), boost::multiprecision::detail::is_signed<T>());
1190 }
1191
1192 public:
1193
1194
1195
1196 #ifdef __MSVC_RUNTIME_CHECKS
1197 template <class SI>
1198 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
1199 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
1200 {}
1201 template <class SI>
1202 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>())))
1203 : 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); }
1204 template <class UI>
1205 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
1206 : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
1207 template <class UI>
1208 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>())))
1209 : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1210 #else
1211 template <class SI>
1212 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
1213 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
1214 {}
1215 template <class SI>
1216 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>())))
1217 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
1218 template <class UI>
1219 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
1220 : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
1221 template <class UI>
1222 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>())))
1223 : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
1224 #endif
1225 template <class F>
1226 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))
1227 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask)
1228 {
1229 check_in_range(i);
1230 if (i < 0)
1231 negate();
1232 }
1233 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1234 : m_data(static_cast<local_limb_type>(0u))
1235 {}
1236 template <limb_type a>
1237 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1238 : m_data(static_cast<local_limb_type>(a)) {}
1239 template <limb_type a, limb_type b>
1240 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1241 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
1242
1243
1244
1245 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1246
1247 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0)
1248 {}
1249
1250
1251
1252 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1253 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1254 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1255 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
1256 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
1257 {
1258 if (b)
1259 negate();
1260 }
1261 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, std::size_t min_size)
1262 {
1263 detail::verify_new_size(2, min_size, checked_type());
1264 }
1265 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1266 {
1267 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1268 m_data &= limb_mask;
1269 }
1270
1271 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0) {}
1272 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1273 : m_data(o.m_data) {}
1274
1275 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1276 {
1277 m_data = o.m_data;
1278 }
1279 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate()
1280 #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
1281 noexcept((Checked == unchecked))
1282 #endif
1283 {
1284 BOOST_IF_CONSTEXPR(Checked == checked)
1285 {
1286 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1287 }
1288 m_data = ~m_data;
1289 ++m_data;
1290 }
1291 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
1292 {
1293 return false;
1294 }
1295 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1296 {
1297 std_constexpr::swap(m_data, o.m_data);
1298 }
1299 };
1300
1301
1302
1303
1304 template <class Arg, class Base>
1305 struct is_allowed_cpp_int_base_conversion : public std::conditional<
1306 std::is_same<Arg, limb_type>::value || std::is_same<Arg, signed_limb_type>::value
1307 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
1308 || std::is_same<Arg, double_limb_type>::value || std::is_same<Arg, signed_double_limb_type>::value
1309 #endif
1310 || literals::detail::is_value_pack<Arg>::value || (is_trivial_cpp_int<Base>::value && boost::multiprecision::detail::is_arithmetic<Arg>::value),
1311 std::integral_constant<bool, true>,
1312 std::integral_constant<bool, false>>::type
1313 {};
1314
1315
1316
1317 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1318 struct cpp_int_backend
1319 : public cpp_int_base<
1320 min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1321 max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1322 SignType,
1323 Checked,
1324 Allocator,
1325 is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1326 {
1327 using self_type = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
1328 using base_type = cpp_int_base<
1329 min_precision<self_type>::value,
1330 max_precision<self_type>::value,
1331 SignType,
1332 Checked,
1333 Allocator,
1334 is_trivial_cpp_int<self_type>::value>;
1335 using trivial_tag = std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>;
1336
1337 public:
1338 using signed_types = typename std::conditional<
1339 is_trivial_cpp_int<self_type>::value,
1340 std::tuple<
1341 signed char, short, int, long,
1342 long long, signed_double_limb_type>,
1343 std::tuple<signed_limb_type, signed_double_limb_type> >::type;
1344 using unsigned_types = typename std::conditional<
1345 is_trivial_cpp_int<self_type>::value,
1346 std::tuple<unsigned char, unsigned short, unsigned,
1347 unsigned long, unsigned long long, double_limb_type>,
1348 std::tuple<limb_type, double_limb_type> >::type;
1349 using float_types = typename std::conditional<
1350 is_trivial_cpp_int<self_type>::value,
1351 std::tuple<float, double, long double>,
1352 std::tuple<long double> >::type;
1353 using checked_type = std::integral_constant<int, Checked> ;
1354
1355 BOOST_MP_FORCEINLINE constexpr cpp_int_backend() noexcept {}
1356 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(const cpp_int_backend& o) noexcept(std::is_void<Allocator>::value) : base_type(o) {}
1357
1358 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(cpp_int_backend&& o) noexcept
1359 : base_type(static_cast<base_type&&>(o))
1360 {}
1361 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1362 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
1363 {
1364 *this = static_cast<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&&>(o);
1365 }
1366
1367
1368
1369 template <class Arg>
1370 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>())))
1371 : base_type(i) {}
1372
1373
1374
1375
1376 explicit constexpr cpp_int_backend(limb_type* data, std::size_t offset, std::size_t len) noexcept
1377 : base_type(data, offset, len) {}
1378 explicit cpp_int_backend(const limb_type* data, std::size_t offset, std::size_t len) noexcept
1379 : base_type(data, offset, len) { this->normalize(); }
1380 explicit constexpr cpp_int_backend(typename base_type::scoped_shared_storage& data, std::size_t len) noexcept
1381 : base_type(data, len) {}
1382
1383 private:
1384 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1385 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&)
1386 {
1387
1388 this->check_in_range(*other.limbs());
1389 *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1390 this->sign(other.sign());
1391 this->normalize();
1392 }
1393 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1394 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&)
1395 {
1396
1397 double_limb_type v = *other.limbs();
1398 if (other.size() > 1)
1399 {
1400 v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1401 BOOST_IF_CONSTEXPR(Checked == checked)
1402 {
1403 if (other.size() > 2)
1404 {
1405 BOOST_MP_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1406 }
1407 }
1408 }
1409 *this = v;
1410 this->sign(other.sign());
1411 this->normalize();
1412 }
1413 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1414 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&)
1415 {
1416
1417 *this = static_cast<
1418 typename boost::multiprecision::detail::canonical<
1419 typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1420 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
1421 this->sign(other.sign());
1422 }
1423 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1424 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&)
1425 {
1426
1427 this->resize(other.size(), other.size());
1428
1429 #if !defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
1430 std::size_t count = (std::min)(other.size(), this->size());
1431 for (std::size_t i = 0; i < count; ++i)
1432 this->limbs()[i] = other.limbs()[i];
1433 #else
1434 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1435 if (BOOST_MP_IS_CONST_EVALUATED(other.size()))
1436 {
1437 std::size_t count = (std::min)(other.size(), this->size());
1438 for (std::size_t i = 0; i < count; ++i)
1439 this->limbs()[i] = other.limbs()[i];
1440 }
1441 else
1442 #endif
1443 {
1444 static_assert(sizeof(other.limbs()[0]) == sizeof(this->limbs()[0]), "This method requires equal limb sizes");
1445 std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size() * sizeof(other.limbs()[0]), this->size() * sizeof(this->limbs()[0])));
1446 }
1447 #endif
1448 this->sign(other.sign());
1449 this->normalize();
1450 }
1451
1452 public:
1453 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1454 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1455 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1456 typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = nullptr)
1457 : base_type()
1458 {
1459 do_assign(
1460 other,
1461 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1462 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1463 }
1464 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1465 explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1466 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1467 typename std::enable_if< !(is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value)>::type* = nullptr)
1468 : base_type()
1469 {
1470 do_assign(
1471 other,
1472 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1473 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1474 }
1475 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1476 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
1477 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1478 {
1479 do_assign(
1480 other,
1481 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1482 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1483 return *this;
1484 }
1485 constexpr cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1486 : base_type(static_cast<const base_type&>(a), tag)
1487 {}
1488
1489 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&>())))
1490 {
1491 this->assign(o);
1492 return *this;
1493 }
1494
1495 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>()))
1496 {
1497 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1498 return *this;
1499 }
1500 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1501 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
1502 {
1503 *static_cast<base_type*>(this) = static_cast<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>::base_type&&>(o);
1504 return *this;
1505 }
1506
1507 template <class A>
1508 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1509 boost::multiprecision::detail::is_unsigned<A>::value
1510 && trivial_tag::value, cpp_int_backend&>::type
1511 operator=(const A& val)
1512 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1513 {
1514 this->check_in_range(val);
1515 *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1516 this->sign(false);
1517 this->normalize();
1518 return *this;
1519 }
1520 template <class A>
1521 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1522 !(boost::multiprecision::detail::is_unsigned<A>::value || !boost::multiprecision::detail::is_integral<A>::value)
1523 && trivial_tag::value, cpp_int_backend&>::type
1524 operator=(const A& val)
1525 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1526 {
1527 this->check_in_range(val);
1528 *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);
1529 this->sign(val < 0);
1530 this->normalize();
1531 return *this;
1532 }
1533 template <class A>
1534 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1535 std::is_convertible<A, limb_type>::value
1536 && !boost::multiprecision::detail::is_integral<A>::value
1537 && trivial_tag::value, cpp_int_backend&>::type
1538 operator=(const A& val)
1539 {
1540 this->check_in_range(val);
1541 *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);
1542 this->sign(val < 0);
1543 this->normalize();
1544 return *this;
1545 }
1546 template <class A>
1547 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1548 std::is_same<A, limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1549 operator=(A i) noexcept
1550 {
1551 this->resize(1, 1);
1552 *this->limbs() = i;
1553 this->sign(false);
1554 return *this;
1555 }
1556 template <class A>
1557 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1558 std::is_same<A, signed_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1559 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1560 {
1561 this->resize(1, 1);
1562 *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1563 this->sign(i < 0);
1564 return *this;
1565 }
1566 template <class A>
1567 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1568 std::is_same<A, double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1569 operator=(A i) noexcept
1570 {
1571 static_assert(sizeof(i) == 2 * sizeof(limb_type), "Failed integer size check");
1572 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count");
1573 typename base_type::limb_pointer p = this->limbs();
1574 #ifdef __MSVC_RUNTIME_CHECKS
1575 *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1576 #else
1577 *p = static_cast<limb_type>(i);
1578 #endif
1579 p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1580 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1581 this->sign(false);
1582 return *this;
1583 }
1584 template <class A>
1585 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1586 std::is_same<A, signed_double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1587 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1588 {
1589 static_assert(sizeof(i) == 2 * sizeof(limb_type), "double limb type size check failed");
1590 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count check");
1591 bool s = false;
1592 if (i < 0)
1593 s = true;
1594 double_limb_type ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1595 typename base_type::limb_pointer p = this->limbs();
1596 #ifdef __MSVC_RUNTIME_CHECKS
1597 *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1598 #else
1599 *p = static_cast<limb_type>(ui);
1600 #endif
1601 p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1602 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1603 this->sign(s);
1604 return *this;
1605 }
1606 private:
1607 template <class F>
1608 BOOST_MP_CXX14_CONSTEXPR void do_assign_float(F a)
1609 {
1610 using default_ops::eval_add;
1611 using default_ops::eval_subtract;
1612 BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
1613
1614 if (a < 0)
1615 {
1616 do_assign_float(-a);
1617 this->sign(true);
1618 return;
1619 }
1620
1621 if (a == 0)
1622 {
1623 *this = static_cast<limb_type>(0u);
1624 }
1625
1626 if (a == 1)
1627 {
1628 *this = static_cast<limb_type>(1u);
1629 }
1630
1631 if (!BOOST_MP_ISFINITE(a))
1632 {
1633 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
1634 }
1635
1636 int e = 0;
1637 F f(0), term(0);
1638 *this = static_cast<limb_type>(0u);
1639
1640 f = frexp(a, &e);
1641
1642 #if !(defined(__clang__) && (__clang_major__ <= 7))
1643 constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
1644 #else
1645
1646
1647
1648 constexpr limb_type shift = std::numeric_limits<limb_type>::digits > std::numeric_limits<double>::digits
1649 ? std::numeric_limits<double>::digits : std::numeric_limits<limb_type>::digits;
1650 #endif
1651
1652 while (f != static_cast<F>(0.0f))
1653 {
1654
1655 f = ldexp(f, shift);
1656 term = floor(f);
1657 e = e - static_cast<int>(shift);
1658 eval_left_shift(*this, shift);
1659 #if !(defined(__clang__) && (__clang_major__ <= 7))
1660 if (term > 0)
1661 eval_add(*this, static_cast<limb_type>(term));
1662 else
1663 eval_subtract(*this, static_cast<limb_type>(-term));
1664 #else
1665
1666 if (term > 0)
1667 eval_add(*this, static_cast<limb_type>(static_cast<double>(term)));
1668 else
1669 eval_subtract(*this, static_cast<limb_type>(static_cast<double>(-term)));
1670 #endif
1671 f -= term;
1672 }
1673 if (e > 0)
1674 eval_left_shift(*this, static_cast<unsigned int>(e));
1675 else if (e < 0)
1676 eval_right_shift(*this, static_cast<unsigned int>(-e));
1677 }
1678 public:
1679 template <class A>
1680 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1681 std::is_floating_point<A>::value && !trivial_tag::value, cpp_int_backend&>::type
1682 operator=(A a)
1683 {
1684 do_assign_float(a);
1685 return *this;
1686 }
1687
1688 private:
1689 void do_assign_string(const char* s, const std::integral_constant<bool, true>&)
1690 {
1691 std::size_t n = s ? std::strlen(s) : 0;
1692 *this = 0;
1693 unsigned radix = 10;
1694 bool isneg = false;
1695 if (n && (*s == '-'))
1696 {
1697 --n;
1698 ++s;
1699 isneg = true;
1700 }
1701 if (n && (*s == '0'))
1702 {
1703 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1704 {
1705 radix = 16;
1706 s += 2;
1707 n -= 2;
1708 }
1709 else
1710 {
1711 radix = 8;
1712 n -= 1;
1713 }
1714 }
1715 if (n)
1716 {
1717 unsigned val;
1718 while (*s)
1719 {
1720 if (*s >= '0' && *s <= '9')
1721 val = static_cast<unsigned>(*s - '0');
1722 else if (*s >= 'a' && *s <= 'f')
1723 val = 10u + static_cast<unsigned>(*s - 'a');
1724 else if (*s >= 'A' && *s <= 'F')
1725 val = 10u + static_cast<unsigned>(*s - 'A');
1726 else
1727 val = radix + 1u;
1728 if (val >= radix)
1729 {
1730 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1731 }
1732 *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1733 *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1734 ++s;
1735 }
1736 }
1737 if (isneg)
1738 this->negate();
1739 }
1740 void do_assign_string(const char* s, const std::integral_constant<bool, false>&)
1741 {
1742 using default_ops::eval_add;
1743 using default_ops::eval_multiply;
1744 std::size_t n = s ? std::strlen(s) : 0;
1745 *this = static_cast<limb_type>(0u);
1746 unsigned radix = 10;
1747 bool isneg = false;
1748 if (n && (*s == '-'))
1749 {
1750 --n;
1751 ++s;
1752 isneg = true;
1753 }
1754 if (n && (*s == '0'))
1755 {
1756 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1757 {
1758 radix = 16;
1759 s += 2;
1760 n -= 2;
1761 }
1762 else
1763 {
1764 radix = 8;
1765 n -= 1;
1766 }
1767 }
1768
1769
1770
1771
1772
1773 cpp_int_backend result;
1774 if (n)
1775 {
1776 if (radix == 16)
1777 {
1778 while (*s == '0')
1779 ++s;
1780 std::size_t bitcount = 4 * std::strlen(s);
1781 limb_type val;
1782 std::size_t limb, shift;
1783 if (bitcount > 4)
1784 bitcount -= 4;
1785 else
1786 bitcount = 0;
1787 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1788 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize));
1789 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1790 while (*s)
1791 {
1792 if (*s >= '0' && *s <= '9')
1793 val = static_cast<unsigned>(*s - '0');
1794 else if (*s >= 'a' && *s <= 'f')
1795 val = 10u + static_cast<unsigned>(*s - 'a');
1796 else if (*s >= 'A' && *s <= 'F')
1797 val = 10u + static_cast<unsigned>(*s - 'A');
1798 else
1799 {
1800 #if defined(BOOST_NO_EXCEPTIONS)
1801 val = static_cast<unsigned>('0');
1802 #endif
1803
1804 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1805 }
1806 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1807 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1808 val <<= shift;
1809 if (result.size() > limb)
1810 {
1811 result.limbs()[limb] |= val;
1812 }
1813 ++s;
1814 bitcount -= 4;
1815 }
1816 result.normalize();
1817 }
1818 else if (radix == 8)
1819 {
1820 while (*s == '0')
1821 ++s;
1822 std::size_t bitcount = 3 * std::strlen(s);
1823 limb_type val;
1824 std::size_t limb, shift;
1825 if (bitcount > 3)
1826 bitcount -= 3;
1827 else
1828 bitcount = 0;
1829 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1830 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize));
1831 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1832 while (*s)
1833 {
1834 if (*s >= '0' && *s <= '7')
1835 val = static_cast<unsigned>(*s - '0');
1836 else
1837 {
1838 #if defined(BOOST_NO_EXCEPTIONS)
1839 val = static_cast<unsigned>('0');
1840 #endif
1841
1842 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1843 }
1844 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1845 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1846 if (result.size() > limb)
1847 {
1848 result.limbs()[limb] |= (val << shift);
1849 if (shift > sizeof(limb_type) * CHAR_BIT - 3)
1850 {
1851
1852 val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1853 if (val)
1854 {
1855
1856 if (limb + 1 == newsize)
1857 {
1858 result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1859 result.limbs()[limb + 1] = 0;
1860 }
1861 if (result.size() > limb + 1)
1862 {
1863 result.limbs()[limb + 1] |= val;
1864 }
1865 }
1866 }
1867 }
1868 ++s;
1869 bitcount -= 3;
1870 }
1871 result.normalize();
1872 }
1873 else
1874 {
1875
1876
1877 limb_type block_mult = max_block_10;
1878 while (*s)
1879 {
1880 limb_type block = 0;
1881 for (unsigned i = 0; i < digits_per_block_10; ++i)
1882 {
1883 limb_type val;
1884 if (*s >= '0' && *s <= '9')
1885 val = static_cast<limb_type>(*s - '0');
1886 else
1887 {
1888 #if defined(BOOST_NO_EXCEPTIONS)
1889 val = static_cast<unsigned>('0');
1890 #endif
1891
1892 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1893 }
1894 block *= 10;
1895 block += val;
1896 if (!*++s)
1897 {
1898 block_mult = block_multiplier(i);
1899 break;
1900 }
1901 }
1902 eval_multiply(result, block_mult);
1903 eval_add(result, block);
1904 }
1905 }
1906 }
1907 if (isneg)
1908 result.negate();
1909 result.swap(*this);
1910 }
1911
1912 public:
1913 cpp_int_backend& operator=(const char* s)
1914 {
1915 do_assign_string(s, trivial_tag());
1916 return *this;
1917 }
1918 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) noexcept
1919 {
1920 this->do_swap(o);
1921 }
1922
1923 private:
1924 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
1925 {
1926 using io_type = typename std::conditional<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type;
1927 if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1928 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1929 std::stringstream ss;
1930 ss.flags(f & ~std::ios_base::showpos);
1931 ss << static_cast<io_type>(*this->limbs());
1932 std::string result;
1933 if (this->sign())
1934 result += '-';
1935 else if (f & std::ios_base::showpos)
1936 result += '+';
1937 result += ss.str();
1938 return result;
1939 }
1940 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
1941 {
1942
1943 int base = 10;
1944 if ((f & std::ios_base::oct) == std::ios_base::oct)
1945 base = 8;
1946 else if ((f & std::ios_base::hex) == std::ios_base::hex)
1947 base = 16;
1948 std::string result;
1949
1950 std::size_t Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1951
1952 if (base == 8 || base == 16)
1953 {
1954 if (this->sign())
1955 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1956 limb_type shift = base == 8 ? 3 : 4;
1957 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1958 typename base_type::local_limb_type v = *this->limbs();
1959 result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1960 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
1961 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1962 for (std::size_t i = 0; i < Bits / shift; ++i)
1963 {
1964 char c = static_cast<char>('0' + static_cast<char>(v & mask));
1965 if (c > '9')
1966 c = static_cast<char>(c + letter_a - '9' - 1);
1967 result[static_cast<std::size_t>(pos)] = c;
1968 --pos;
1969 v >>= shift;
1970 }
1971 if (Bits % shift)
1972 {
1973 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1974 char c = static_cast<char>('0' + static_cast<char>(v & mask));
1975 if (c > '9')
1976 c = static_cast<char>(c + letter_a - '9');
1977 result[static_cast<std::size_t>(pos)] = c;
1978 }
1979
1980
1981
1982 std::string::size_type n = result.find_first_not_of('0');
1983 if (!result.empty() && (n == std::string::npos))
1984 n = result.size() - 1;
1985 result.erase(0, n);
1986 if (f & std::ios_base::showbase)
1987 {
1988 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
1989 result.insert(static_cast<std::string::size_type>(0), pp);
1990 }
1991 }
1992 else
1993 {
1994 result.assign(Bits / 3 + 1, '0');
1995 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
1996 typename base_type::local_limb_type v(*this->limbs());
1997 bool neg = false;
1998 if (this->sign())
1999 {
2000 neg = true;
2001 }
2002 while (v)
2003 {
2004 result[static_cast<std::string::size_type>(pos)] = static_cast<char>(static_cast<char>(v % 10) + '0');
2005 --pos;
2006 v /= 10;
2007 }
2008 std::string::size_type n = result.find_first_not_of('0');
2009 result.erase(0, n);
2010 if (result.empty())
2011 result = "0";
2012 if (neg)
2013 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2014 else if (f & std::ios_base::showpos)
2015 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2016 }
2017 return result;
2018 }
2019 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
2020 {
2021 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
2022 return do_get_trivial_string(f, std::integral_constant<bool, std::is_same<typename base_type::local_limb_type, double_limb_type>::value>());
2023 #else
2024 return do_get_trivial_string(f, std::integral_constant<bool, false>());
2025 #endif
2026 }
2027 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
2028 {
2029 using default_ops::eval_get_sign;
2030 int base = 10;
2031 if ((f & std::ios_base::oct) == std::ios_base::oct)
2032 base = 8;
2033 else if ((f & std::ios_base::hex) == std::ios_base::hex)
2034 base = 16;
2035 std::string result;
2036
2037 std::size_t Bits = this->size() * base_type::limb_bits;
2038
2039 if (base == 8 || base == 16)
2040 {
2041 if (this->sign())
2042 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
2043 limb_type shift = base == 8 ? 3 : 4;
2044 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
2045 cpp_int_backend t(*this);
2046 result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
2047 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
2048 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
2049 for (std::size_t i = 0; i < Bits / shift; ++i)
2050 {
2051 char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
2052 if (c > '9')
2053 c = static_cast<char>(c + letter_a - '9' - 1);
2054 result[static_cast<std::size_t>(pos)] = c;
2055 --pos;
2056 eval_right_shift(t, shift);
2057 }
2058 if (Bits % shift)
2059 {
2060 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
2061 char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
2062 if (c > '9')
2063 c = static_cast<char>(c + letter_a - '9');
2064 result[static_cast<std::size_t>(pos)] = c;
2065 }
2066
2067
2068
2069 std::string::size_type n = result.find_first_not_of('0');
2070 if (!result.empty() && (n == std::string::npos))
2071 n = result.size() - 1;
2072 result.erase(0, n);
2073 if (f & std::ios_base::showbase)
2074 {
2075 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
2076 result.insert(static_cast<std::string::size_type>(0), pp);
2077 }
2078 }
2079 else
2080 {
2081 result.assign(Bits / 3 + 1, '0');
2082 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
2083 cpp_int_backend t(*this);
2084 cpp_int_backend r;
2085 bool neg = false;
2086 if (t.sign())
2087 {
2088 t.negate();
2089 neg = true;
2090 }
2091 if (this->size() == 1)
2092 {
2093 result = std::to_string(t.limbs()[0]);
2094 }
2095 else
2096 {
2097 cpp_int_backend block10;
2098 block10 = max_block_10;
2099 while (eval_get_sign(t) != 0)
2100 {
2101 cpp_int_backend t2;
2102 divide_unsigned_helper(&t2, t, block10, r);
2103 t = t2;
2104 limb_type v = r.limbs()[0];
2105 for (std::size_t i = 0; i < digits_per_block_10; ++i)
2106 {
2107 char c = static_cast<char>('0' + static_cast<char>(v % 10));
2108 v /= 10;
2109 result[static_cast<std::size_t>(pos)] = c;
2110 if (pos-- == 0u)
2111 break;
2112 }
2113 }
2114 }
2115 std::string::size_type n = result.find_first_not_of('0');
2116 result.erase(0, n);
2117 if (result.empty())
2118 result = std::string(static_cast<std::size_t>(1u), '0');
2119 if (neg)
2120 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2121 else if (f & std::ios_base::showpos)
2122 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2123 }
2124 return result;
2125 }
2126
2127 public:
2128 std::string str(std::streamsize , std::ios_base::fmtflags f) const
2129 {
2130 return do_get_string(f, trivial_tag());
2131 }
2132
2133 private:
2134 template <class Container>
2135 void construct_from_container(const Container& c, const std::integral_constant<bool, false>&)
2136 {
2137
2138
2139
2140 std::size_t newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
2141 if (c.size() % sizeof(limb_type))
2142 {
2143 ++newsize;
2144 }
2145 if (newsize)
2146 {
2147 this->resize(newsize, newsize);
2148 std::memset(this->limbs(), 0, this->size());
2149 typename Container::const_iterator i(c.begin()), j(c.end());
2150 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2151 while (i != j)
2152 {
2153 std::size_t limb = byte_location / sizeof(limb_type);
2154 std::size_t shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
2155 if (this->size() > limb)
2156 this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2157 ++i;
2158 --byte_location;
2159 }
2160 }
2161 }
2162 template <class Container>
2163 BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const std::integral_constant<bool, true>&)
2164 {
2165
2166
2167
2168 using local_limb_type = typename base_type::local_limb_type;
2169 *this->limbs() = 0;
2170 if (c.size())
2171 {
2172 typename Container::const_iterator i(c.begin()), j(c.end());
2173 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2174 while (i != j)
2175 {
2176 std::size_t limb = byte_location / sizeof(local_limb_type);
2177 std::size_t shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
2178 if (limb == 0)
2179 this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2180 ++i;
2181 --byte_location;
2182 }
2183 }
2184 }
2185
2186 public:
2187 template <class Container>
2188 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)
2189 {
2190
2191
2192
2193 construct_from_container(c, trivial_tag());
2194 }
2195 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2196 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
2197 {
2198 if (this->sign() != o.sign())
2199 return this->sign() ? -1 : 1;
2200
2201
2202 int result = compare_unsigned(o);
2203
2204 if (this->sign())
2205 result = -result;
2206 return result;
2207 }
2208 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2209 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
2210 {
2211 cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
2212 return t.compare(o);
2213 }
2214 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2215 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
2216 {
2217 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
2218 return compare(t);
2219 }
2220 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2221 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
2222 {
2223 if (this->sign())
2224 {
2225 if (o.sign())
2226 {
2227 return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
2228 }
2229 else
2230 return -1;
2231 }
2232 else
2233 {
2234 if (o.sign())
2235 return 1;
2236 return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
2237 }
2238 }
2239 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2240 BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2241 {
2242 using t1 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> ;
2243 using t2 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>;
2244 return compare_imp(o, t1(), t2());
2245 }
2246 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2247 BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2248 {
2249 if (this->size() != o.size())
2250 {
2251 return this->size() > o.size() ? 1 : -1;
2252 }
2253 typename base_type::const_limb_pointer pa = this->limbs();
2254 typename base_type::const_limb_pointer pb = o.limbs();
2255 for (std::ptrdiff_t i = static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(this->size()) - 1); i >= 0; --i)
2256 {
2257 if (pa[i] != pb[i])
2258 return pa[i] > pb[i] ? 1 : -1;
2259 }
2260 return 0;
2261 }
2262 template <class Arithmetic>
2263 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Arithmetic>::value, int>::type compare(Arithmetic i) const
2264 {
2265
2266 cpp_int_backend t;
2267 t = i;
2268 return compare(t);
2269 }
2270 };
2271
2272 }
2273
2274 namespace default_ops {
2275
2276 template <class Backend>
2277 struct double_precision_type;
2278
2279 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2280 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2281 {
2282 using type = typename std::conditional<
2283 backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2284 backends::cpp_int_backend<
2285 (std::is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
2286 : MinBits),
2287 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2288 SignType,
2289 Checked,
2290 Allocator>,
2291 backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type;
2292 };
2293
2294 }
2295
2296 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>
2297 struct is_equivalent_number_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
2298 : 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>{};
2299
2300 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2301 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::integral_constant<int, number_kind_integer>
2302 {};
2303
2304 #ifdef BOOST_HAS_INT128
2305
2306 namespace detail {
2307
2308 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2309 struct is_convertible_arithmetic<int128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2310 {
2311 static constexpr bool value = true;
2312 };
2313 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2314 struct is_convertible_arithmetic<uint128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2315 {
2316 static constexpr bool value = true;
2317 };
2318
2319 }
2320
2321 #endif
2322
2323 #if defined(__GNUC__) && !defined(__clang__)
2324
2325
2326 #pragma GCC diagnostic pop
2327 #endif
2328 #ifdef BOOST_MSVC
2329 #pragma warning(pop)
2330 #endif
2331
2332 }}
2333
2334
2335
2336
2337 #include <boost/multiprecision/cpp_int/limits.hpp>
2338 #include <boost/multiprecision/cpp_int/comparison.hpp>
2339 #include <boost/multiprecision/cpp_int/add.hpp>
2340 #include <boost/multiprecision/cpp_int/multiply.hpp>
2341 #include <boost/multiprecision/cpp_int/divide.hpp>
2342 #include <boost/multiprecision/cpp_int/bitwise.hpp>
2343 #include <boost/multiprecision/cpp_int/misc.hpp>
2344 #include <boost/multiprecision/cpp_int/literals.hpp>
2345 #include <boost/multiprecision/cpp_int/serialize.hpp>
2346 #include <boost/multiprecision/cpp_int/import_export.hpp>
2347
2348 #endif