File indexing completed on 2025-06-30 08:21:15
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
0848 limb_type c = p[internal_limb_count - 1];
0849 bool full_limbs = m_limbs == internal_limb_count;
0850
0851 p[internal_limb_count - 1] &= upper_limb_mask;
0852 while ((m_limbs - 1) && !p[m_limbs - 1])
0853 --m_limbs;
0854
0855
0856
0857 detail::verify_limb_mask(full_limbs, c, upper_limb_mask, checked_type());
0858 }
0859
0860 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept
0861 : m_wrapper(limb_type(0u)),
0862 m_limbs(1) {}
0863 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
0864 : m_wrapper(o.m_wrapper),
0865 m_limbs(o.m_limbs) {}
0866
0867
0868
0869
0870
0871 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
0872
0873 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
0874 {
0875 if (this != &o)
0876 {
0877 m_limbs = o.m_limbs;
0878 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
0879 if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
0880 {
0881 for (std::size_t i = 0; i < m_limbs; ++i)
0882 limbs()[i] = o.limbs()[i];
0883 }
0884 else
0885 #endif
0886 std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
0887 }
0888 }
0889
0890 private:
0891 void check_negate(const std::integral_constant<int, checked>&)
0892 {
0893 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
0894 }
0895 BOOST_MP_CXX14_CONSTEXPR void check_negate(const std::integral_constant<int, unchecked>&) {}
0896
0897 public:
0898 BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
0899 {
0900
0901
0902 if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
0903 return;
0904 check_negate(checked_type());
0905 std::size_t i = m_limbs;
0906 for (; i < internal_limb_count; ++i)
0907 m_wrapper.m_data[i] = 0;
0908 m_limbs = internal_limb_count;
0909 for (i = 0; i < internal_limb_count; ++i)
0910 m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
0911 normalize();
0912 eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
0913 }
0914 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
0915 {
0916 return false;
0917 }
0918 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
0919 {
0920 for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
0921 std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
0922 std_constexpr::swap(m_limbs, o.m_limbs);
0923 }
0924
0925 protected:
0926 template <class A>
0927 BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
0928 };
0929
0930 template <std::size_t MinBits, cpp_int_check_type Checked>
0931 constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
0932 template <std::size_t MinBits, cpp_int_check_type Checked>
0933 constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
0934 template <std::size_t MinBits, cpp_int_check_type Checked>
0935 constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
0936 template <std::size_t MinBits, cpp_int_check_type Checked>
0937 constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
0938
0939
0940
0941
0942 template <unsigned N, bool s>
0943 struct trivial_limb_type_imp
0944 {
0945 using type = double_limb_type;
0946 };
0947
0948 template <unsigned N>
0949 struct trivial_limb_type_imp<N, true>
0950 {
0951 using type = typename boost::multiprecision::detail::uint_t<N>::least;
0952 };
0953
0954 template <unsigned N>
0955 struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(long long) * CHAR_BIT>
0956 {};
0957
0958
0959
0960 template <std::size_t MinBits, cpp_int_check_type Checked>
0961 struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
0962 {
0963 using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
0964 using limb_pointer = local_limb_type*;
0965 using const_limb_pointer = const local_limb_type*;
0966 using checked_type = std::integral_constant<int, Checked>;
0967
0968 struct scoped_shared_storage
0969 {
0970 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
0971 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
0972 };
0973
0974 protected:
0975 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
0976 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));
0977
0978 private:
0979 local_limb_type m_data;
0980 bool m_sign;
0981
0982
0983
0984
0985 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?");
0986
0987 protected:
0988 template <class T>
0989 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
0990 check_in_range(T val, const std::integral_constant<int, checked>&)
0991 {
0992 using common_type = typename std::common_type<typename boost::multiprecision::detail::make_unsigned<T>::type, local_limb_type>::type;
0993
0994 if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
0995 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
0996 }
0997 template <class T>
0998 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
0999 check_in_range(T val, const std::integral_constant<int, checked>&)
1000 {
1001 using std::abs;
1002 using common_type = typename std::common_type<T, local_limb_type>::type;
1003
1004 if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
1005 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1006 }
1007 template <class T, int C>
1008 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&) noexcept {}
1009
1010 template <class T>
1011 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())))
1012 {
1013 check_in_range(val, checked_type());
1014 }
1015
1016 public:
1017
1018
1019
1020 template <class SI>
1021 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>())))
1022 : 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) {}
1023 template <class SI>
1024 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>())))
1025 : m_data(i < 0 ? (static_cast<local_limb_type>(static_cast<typename boost::multiprecision::detail::make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask)) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0)
1026 {
1027 check_in_range(i);
1028 }
1029 template <class UI>
1030 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
1031 : m_data(static_cast<local_limb_type>(i) & limb_mask),
1032 m_sign(false) {}
1033 template <class UI>
1034 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>())))
1035 : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
1036 #if !(defined(__clang__) && defined(__MINGW32__))
1037 template <class F>
1038 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
1039 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask),
1040 m_sign(i < 0) {}
1041 template <class F>
1042 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)
1043 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask), m_sign(i < 0) { check_in_range(i); }
1044 #else
1045
1046
1047
1048
1049
1050
1051
1052 template <class F>
1053 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
1054 : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask),
1055 m_sign(i < 0) {}
1056 template <class F>
1057 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)
1058 : 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); }
1059 #endif
1060
1061 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1062 : m_data(static_cast<local_limb_type>(0u)),
1063 m_sign(false)
1064 {}
1065 template <limb_type a>
1066 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1067 : m_data(static_cast<local_limb_type>(a)),
1068 m_sign(false) {}
1069 template <limb_type a, limb_type b>
1070 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1071 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
1072 m_sign(false) {}
1073 constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) noexcept
1074 : m_data(a.m_data),
1075 m_sign(a.m_data ? !a.m_sign : false) {}
1076
1077
1078
1079 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1080
1081 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0), m_sign(false)
1082 {}
1083
1084
1085
1086 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1087 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1088 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1089 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool sign() const noexcept { return m_sign; }
1090 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
1091 {
1092 m_sign = b;
1093
1094 if (m_sign && !m_data)
1095 {
1096 m_sign = false;
1097 }
1098 }
1099 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t , std::size_t min_size)
1100 {
1101 detail::verify_new_size(2, min_size, checked_type());
1102 }
1103 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1104 {
1105 if (!m_data)
1106 m_sign = false;
1107 detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
1108 m_data &= limb_mask;
1109 }
1110
1111 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0), m_sign(false) {}
1112 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1113 : m_data(o.m_data),
1114 m_sign(o.m_sign) {}
1115
1116 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1117 {
1118 m_data = o.m_data;
1119 m_sign = o.m_sign;
1120 }
1121 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
1122 {
1123 m_sign = !m_sign;
1124
1125 if (m_data == 0)
1126 {
1127 m_sign = false;
1128 }
1129 }
1130 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
1131 {
1132 return m_sign;
1133 }
1134 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1135 {
1136 std_constexpr::swap(m_sign, o.m_sign);
1137 std_constexpr::swap(m_data, o.m_data);
1138 }
1139 };
1140
1141
1142
1143 template <std::size_t MinBits, cpp_int_check_type Checked>
1144 struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
1145 {
1146 using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
1147 using limb_pointer = local_limb_type* ;
1148 using const_limb_pointer = const local_limb_type* ;
1149
1150 struct scoped_shared_storage
1151 {
1152 BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
1153 BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
1154 };
1155
1156 private:
1157 static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
1158 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))
1159 : static_cast<local_limb_type>(~local_limb_type(0));
1160
1161 local_limb_type m_data;
1162
1163 using checked_type = std::integral_constant<int, Checked>;
1164
1165
1166
1167
1168 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?");
1169
1170 protected:
1171 template <class T>
1172 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits)))>::type
1173 check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, false>&)
1174 {
1175 using common_type = typename std::common_type<T, local_limb_type>::type;
1176
1177 if (static_cast<common_type>(val) > limb_mask)
1178 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1179 }
1180 template <class T>
1181 BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, true>&)
1182 {
1183 using common_type = typename std::common_type<T, local_limb_type>::type;
1184
1185 if (static_cast<common_type>(val) > static_cast<common_type>(limb_mask))
1186 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
1187 if (val < 0)
1188 BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
1189 }
1190 template <class T, int C, bool B>
1191 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 {}
1192
1193 template <class T>
1194 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>())))
1195 {
1196 check_in_range(val, checked_type(), boost::multiprecision::detail::is_signed<T>());
1197 }
1198
1199 public:
1200
1201
1202
1203 #ifdef __MSVC_RUNTIME_CHECKS
1204 template <class SI>
1205 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
1206 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
1207 {}
1208 template <class SI>
1209 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>())))
1210 : 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); }
1211 template <class UI>
1212 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
1213 : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
1214 template <class UI>
1215 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>())))
1216 : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
1217 #else
1218 template <class SI>
1219 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
1220 : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
1221 {}
1222 template <class SI>
1223 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>())))
1224 : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
1225 template <class UI>
1226 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
1227 : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
1228 template <class UI>
1229 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>())))
1230 : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
1231 #endif
1232 template <class F>
1233 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))
1234 : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask)
1235 {
1236 check_in_range(i);
1237 if (i < 0)
1238 negate();
1239 }
1240 constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
1241 : m_data(static_cast<local_limb_type>(0u))
1242 {}
1243 template <limb_type a>
1244 constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
1245 : m_data(static_cast<local_limb_type>(a)) {}
1246 template <limb_type a, limb_type b>
1247 constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
1248 : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
1249
1250
1251
1252 BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
1253
1254 explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0)
1255 {}
1256
1257
1258
1259 BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
1260 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
1261 BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
1262 BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
1263 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
1264 {
1265 if (b)
1266 negate();
1267 }
1268 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, std::size_t min_size)
1269 {
1270 detail::verify_new_size(2, min_size, checked_type());
1271 }
1272 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
1273 {
1274 local_limb_type c = m_data;
1275 m_data &= limb_mask;
1276
1277
1278
1279
1280 detail::verify_limb_mask(true, c, limb_mask, checked_type());
1281 }
1282
1283 BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0) {}
1284 BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
1285 : m_data(o.m_data) {}
1286
1287 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
1288 {
1289 m_data = o.m_data;
1290 }
1291 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate()
1292 #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
1293 noexcept((Checked == unchecked))
1294 #endif
1295 {
1296 BOOST_IF_CONSTEXPR(Checked == checked)
1297 {
1298 BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
1299 }
1300 m_data = ~m_data;
1301 ++m_data;
1302 }
1303 BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
1304 {
1305 return false;
1306 }
1307 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
1308 {
1309 std_constexpr::swap(m_data, o.m_data);
1310 }
1311 };
1312
1313
1314
1315
1316 template <class Arg, class Base>
1317 struct is_allowed_cpp_int_base_conversion : public std::conditional<
1318 std::is_same<Arg, limb_type>::value || std::is_same<Arg, signed_limb_type>::value
1319 #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
1320 || std::is_same<Arg, double_limb_type>::value || std::is_same<Arg, signed_double_limb_type>::value
1321 #endif
1322 || literals::detail::is_value_pack<Arg>::value || (is_trivial_cpp_int<Base>::value && boost::multiprecision::detail::is_arithmetic<Arg>::value),
1323 std::integral_constant<bool, true>,
1324 std::integral_constant<bool, false>>::type
1325 {};
1326
1327
1328
1329 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
1330 struct cpp_int_backend
1331 : public cpp_int_base<
1332 min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1333 max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
1334 SignType,
1335 Checked,
1336 Allocator,
1337 is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
1338 {
1339 using self_type = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
1340 using base_type = cpp_int_base<
1341 min_precision<self_type>::value,
1342 max_precision<self_type>::value,
1343 SignType,
1344 Checked,
1345 Allocator,
1346 is_trivial_cpp_int<self_type>::value>;
1347 using trivial_tag = std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>;
1348
1349 public:
1350 using signed_types = typename std::conditional<
1351 is_trivial_cpp_int<self_type>::value,
1352 std::tuple<
1353 signed char, short, int, long,
1354 long long, signed_double_limb_type>,
1355 std::tuple<signed_limb_type, signed_double_limb_type> >::type;
1356 using unsigned_types = typename std::conditional<
1357 is_trivial_cpp_int<self_type>::value,
1358 std::tuple<unsigned char, unsigned short, unsigned,
1359 unsigned long, unsigned long long, double_limb_type>,
1360 std::tuple<limb_type, double_limb_type> >::type;
1361 using float_types = typename std::conditional<
1362 is_trivial_cpp_int<self_type>::value,
1363 std::tuple<float, double, long double>,
1364 std::tuple<long double> >::type;
1365 using checked_type = std::integral_constant<int, Checked> ;
1366
1367 BOOST_MP_FORCEINLINE constexpr cpp_int_backend() noexcept {}
1368 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(const cpp_int_backend& o) noexcept(std::is_void<Allocator>::value) : base_type(o) {}
1369
1370 BOOST_MP_FORCEINLINE constexpr cpp_int_backend(cpp_int_backend&& o) noexcept
1371 : base_type(static_cast<base_type&&>(o))
1372 {}
1373 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1374 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
1375 {
1376 *this = static_cast<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&&>(o);
1377 }
1378
1379
1380
1381 template <class Arg>
1382 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>())))
1383 : base_type(i) {}
1384
1385
1386
1387
1388 explicit constexpr cpp_int_backend(limb_type* data, std::size_t offset, std::size_t len) noexcept
1389 : base_type(data, offset, len) {}
1390 explicit cpp_int_backend(const limb_type* data, std::size_t offset, std::size_t len) noexcept
1391 : base_type(data, offset, len) { this->normalize(); }
1392 explicit constexpr cpp_int_backend(typename base_type::scoped_shared_storage& data, std::size_t len) noexcept
1393 : base_type(data, len) {}
1394
1395 private:
1396 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1397 BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::true_type const&, std::true_type const&)
1398 {
1399
1400 this->check_in_range(*other.limbs());
1401 *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
1402 this->sign(other.sign());
1403 this->normalize();
1404 }
1405 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1406 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&)
1407 {
1408
1409 double_limb_type v = *other.limbs();
1410 if (other.size() > 1)
1411 {
1412 v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
1413 BOOST_IF_CONSTEXPR(Checked == checked)
1414 {
1415 if (other.size() > 2)
1416 {
1417 BOOST_MP_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
1418 }
1419 }
1420 }
1421 *this = v;
1422 this->sign(other.sign());
1423 this->normalize();
1424 }
1425 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1426 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&)
1427 {
1428
1429 *this = static_cast<
1430 typename boost::multiprecision::detail::canonical<
1431 typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
1432 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
1433 this->sign(other.sign());
1434 }
1435 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1436 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&)
1437 {
1438
1439 this->resize(other.size(), other.size());
1440
1441 #if !defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
1442 std::size_t count = (std::min)(other.size(), this->size());
1443 for (std::size_t i = 0; i < count; ++i)
1444 this->limbs()[i] = other.limbs()[i];
1445 #else
1446 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1447 if (BOOST_MP_IS_CONST_EVALUATED(other.size()))
1448 {
1449 std::size_t count = (std::min)(other.size(), this->size());
1450 for (std::size_t i = 0; i < count; ++i)
1451 this->limbs()[i] = other.limbs()[i];
1452 }
1453 else
1454 #endif
1455 {
1456 static_assert(sizeof(other.limbs()[0]) == sizeof(this->limbs()[0]), "This method requires equal limb sizes");
1457 std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size() * sizeof(other.limbs()[0]), this->size() * sizeof(this->limbs()[0])));
1458 }
1459 #endif
1460 this->sign(other.sign());
1461 this->normalize();
1462 }
1463
1464 public:
1465 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1466 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1467 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1468 typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = nullptr)
1469 : base_type()
1470 {
1471 do_assign(
1472 other,
1473 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1474 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1475 }
1476 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1477 explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
1478 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
1479 typename std::enable_if< !(is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value)>::type* = nullptr)
1480 : base_type()
1481 {
1482 do_assign(
1483 other,
1484 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1485 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1486 }
1487 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
1488 BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
1489 const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
1490 {
1491 do_assign(
1492 other,
1493 std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
1494 std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
1495 return *this;
1496 }
1497 constexpr cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
1498 : base_type(static_cast<const base_type&>(a), tag)
1499 {}
1500
1501 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&>())))
1502 {
1503 this->assign(o);
1504 return *this;
1505 }
1506
1507 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>()))
1508 {
1509 *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
1510 return *this;
1511 }
1512 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
1513 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
1514 {
1515 *static_cast<base_type*>(this) = static_cast<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>::base_type&&>(o);
1516 return *this;
1517 }
1518
1519 template <class A>
1520 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1521 boost::multiprecision::detail::is_unsigned<A>::value
1522 && trivial_tag::value, cpp_int_backend&>::type
1523 operator=(const A& val)
1524 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
1525 {
1526 this->check_in_range(val);
1527 *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
1528 this->sign(false);
1529 this->normalize();
1530 return *this;
1531 }
1532 template <class A>
1533 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1534 !(boost::multiprecision::detail::is_unsigned<A>::value || !boost::multiprecision::detail::is_integral<A>::value)
1535 && trivial_tag::value, cpp_int_backend&>::type
1536 operator=(const A& val)
1537 noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
1538 {
1539 this->check_in_range(val);
1540 *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);
1541 this->sign(val < 0);
1542 this->normalize();
1543 return *this;
1544 }
1545 template <class A>
1546 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1547 std::is_convertible<A, limb_type>::value
1548 && !boost::multiprecision::detail::is_integral<A>::value
1549 && trivial_tag::value, cpp_int_backend&>::type
1550 operator=(const A& val)
1551 {
1552 this->check_in_range(val);
1553 *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);
1554 this->sign(val < 0);
1555 this->normalize();
1556 return *this;
1557 }
1558 template <class A>
1559 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
1560 std::is_same<A, limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1561 operator=(A i) noexcept
1562 {
1563 this->resize(1, 1);
1564 *this->limbs() = i;
1565 this->sign(false);
1566 return *this;
1567 }
1568 template <class A>
1569 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1570 std::is_same<A, signed_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1571 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1572 {
1573 this->resize(1, 1);
1574 *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1575 this->sign(i < 0);
1576 return *this;
1577 }
1578 template <class A>
1579 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1580 std::is_same<A, double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1581 operator=(A i) noexcept
1582 {
1583 static_assert(sizeof(i) == 2 * sizeof(limb_type), "Failed integer size check");
1584 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count");
1585 typename base_type::limb_pointer p = this->limbs();
1586 #ifdef __MSVC_RUNTIME_CHECKS
1587 *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
1588 #else
1589 *p = static_cast<limb_type>(i);
1590 #endif
1591 p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
1592 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1593 this->sign(false);
1594 return *this;
1595 }
1596 template <class A>
1597 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1598 std::is_same<A, signed_double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
1599 operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
1600 {
1601 static_assert(sizeof(i) == 2 * sizeof(limb_type), "double limb type size check failed");
1602 static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count check");
1603 bool s = false;
1604 if (i < 0)
1605 s = true;
1606 double_limb_type ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
1607 typename base_type::limb_pointer p = this->limbs();
1608 #ifdef __MSVC_RUNTIME_CHECKS
1609 *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
1610 #else
1611 *p = static_cast<limb_type>(ui);
1612 #endif
1613 p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
1614 this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
1615 this->sign(s);
1616 return *this;
1617 }
1618 private:
1619 template <class F>
1620 BOOST_MP_CXX14_CONSTEXPR void do_assign_float(F a)
1621 {
1622 using default_ops::eval_add;
1623 using default_ops::eval_subtract;
1624 BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
1625
1626 if (a < 0)
1627 {
1628 do_assign_float(-a);
1629 this->sign(true);
1630 return;
1631 }
1632
1633 if (a == 0)
1634 {
1635 *this = static_cast<limb_type>(0u);
1636 }
1637
1638 if (a == 1)
1639 {
1640 *this = static_cast<limb_type>(1u);
1641 }
1642
1643 if (!BOOST_MP_ISFINITE(a))
1644 {
1645 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
1646 }
1647
1648 int e = 0;
1649 F f(0), term(0);
1650 *this = static_cast<limb_type>(0u);
1651
1652 f = frexp(a, &e);
1653
1654 #if !(defined(__clang__) && (__clang_major__ <= 7))
1655 constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
1656 #else
1657
1658
1659
1660 constexpr limb_type shift = std::numeric_limits<limb_type>::digits > std::numeric_limits<double>::digits
1661 ? std::numeric_limits<double>::digits : std::numeric_limits<limb_type>::digits;
1662 #endif
1663
1664 while (f != static_cast<F>(0.0f))
1665 {
1666
1667 f = ldexp(f, shift);
1668 term = floor(f);
1669 e = e - static_cast<int>(shift);
1670 eval_left_shift(*this, shift);
1671 #if !(defined(__clang__) && (__clang_major__ <= 7))
1672 if (term > 0)
1673 eval_add(*this, static_cast<limb_type>(term));
1674 else
1675 eval_subtract(*this, static_cast<limb_type>(-term));
1676 #else
1677
1678 if (term > 0)
1679 eval_add(*this, static_cast<limb_type>(static_cast<double>(term)));
1680 else
1681 eval_subtract(*this, static_cast<limb_type>(static_cast<double>(-term)));
1682 #endif
1683 f -= term;
1684 }
1685 if (e > 0)
1686 eval_left_shift(*this, static_cast<unsigned int>(e));
1687 else if (e < 0)
1688 eval_right_shift(*this, static_cast<unsigned int>(-e));
1689 }
1690 public:
1691 template <class A>
1692 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
1693 std::is_floating_point<A>::value && !trivial_tag::value, cpp_int_backend&>::type
1694 operator=(A a)
1695 {
1696 do_assign_float(a);
1697 return *this;
1698 }
1699
1700 private:
1701 void do_assign_string(const char* s, const std::integral_constant<bool, true>&)
1702 {
1703 std::size_t n = s ? std::strlen(s) : 0;
1704 *this = 0;
1705 unsigned radix = 10;
1706 bool isneg = false;
1707 if (n && (*s == '-'))
1708 {
1709 --n;
1710 ++s;
1711 isneg = true;
1712 }
1713 if (n && (*s == '0'))
1714 {
1715 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1716 {
1717 radix = 16;
1718 s += 2;
1719 n -= 2;
1720 }
1721 else
1722 {
1723 radix = 8;
1724 n -= 1;
1725 }
1726 }
1727 if (n)
1728 {
1729 unsigned val;
1730 while (*s)
1731 {
1732 if (*s >= '0' && *s <= '9')
1733 val = static_cast<unsigned>(*s - '0');
1734 else if (*s >= 'a' && *s <= 'f')
1735 val = 10u + static_cast<unsigned>(*s - 'a');
1736 else if (*s >= 'A' && *s <= 'F')
1737 val = 10u + static_cast<unsigned>(*s - 'A');
1738 else
1739 val = radix + 1u;
1740 if (val >= radix)
1741 {
1742 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1743 }
1744 *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
1745 *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
1746 ++s;
1747 }
1748 }
1749 if (isneg)
1750 this->negate();
1751 }
1752 void do_assign_string(const char* s, const std::integral_constant<bool, false>&)
1753 {
1754 using default_ops::eval_add;
1755 using default_ops::eval_multiply;
1756 std::size_t n = s ? std::strlen(s) : 0;
1757 *this = static_cast<limb_type>(0u);
1758 unsigned radix = 10;
1759 bool isneg = false;
1760 if (n && (*s == '-'))
1761 {
1762 --n;
1763 ++s;
1764 isneg = true;
1765 }
1766 if (n && (*s == '0'))
1767 {
1768 if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
1769 {
1770 radix = 16;
1771 s += 2;
1772 n -= 2;
1773 }
1774 else
1775 {
1776 radix = 8;
1777 n -= 1;
1778 }
1779 }
1780
1781
1782
1783
1784
1785 cpp_int_backend result;
1786 if (n)
1787 {
1788 if (radix == 16)
1789 {
1790 while (*s == '0')
1791 ++s;
1792 std::size_t bitcount = 4 * std::strlen(s);
1793 limb_type val;
1794 std::size_t limb, shift;
1795 if (bitcount > 4)
1796 bitcount -= 4;
1797 else
1798 bitcount = 0;
1799 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1800 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize));
1801 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1802 while (*s)
1803 {
1804 if (*s >= '0' && *s <= '9')
1805 val = static_cast<unsigned>(*s - '0');
1806 else if (*s >= 'a' && *s <= 'f')
1807 val = 10u + static_cast<unsigned>(*s - 'a');
1808 else if (*s >= 'A' && *s <= 'F')
1809 val = 10u + static_cast<unsigned>(*s - 'A');
1810 else
1811 {
1812 #if defined(BOOST_NO_EXCEPTIONS)
1813 val = static_cast<unsigned>('0');
1814 #endif
1815
1816 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1817 }
1818 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1819 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1820 val <<= shift;
1821 if (result.size() > limb)
1822 {
1823 result.limbs()[limb] |= val;
1824 }
1825 ++s;
1826 bitcount -= 4;
1827 }
1828 result.normalize();
1829 }
1830 else if (radix == 8)
1831 {
1832 while (*s == '0')
1833 ++s;
1834 std::size_t bitcount = 3 * std::strlen(s);
1835 limb_type val;
1836 std::size_t limb, shift;
1837 if (bitcount > 3)
1838 bitcount -= 3;
1839 else
1840 bitcount = 0;
1841 std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
1842 result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize));
1843 std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
1844 while (*s)
1845 {
1846 if (*s >= '0' && *s <= '7')
1847 val = static_cast<unsigned>(*s - '0');
1848 else
1849 {
1850 #if defined(BOOST_NO_EXCEPTIONS)
1851 val = static_cast<unsigned>('0');
1852 #endif
1853
1854 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
1855 }
1856 limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
1857 shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
1858 if (result.size() > limb)
1859 {
1860 result.limbs()[limb] |= (val << shift);
1861 if (shift > sizeof(limb_type) * CHAR_BIT - 3)
1862 {
1863
1864 val >>= (sizeof(limb_type) * CHAR_BIT - shift);
1865 if (val)
1866 {
1867
1868 if (limb + 1 == newsize)
1869 {
1870 result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
1871 result.limbs()[limb + 1] = 0;
1872 }
1873 if (result.size() > limb + 1)
1874 {
1875 result.limbs()[limb + 1] |= val;
1876 }
1877 }
1878 }
1879 }
1880 ++s;
1881 bitcount -= 3;
1882 }
1883 result.normalize();
1884 }
1885 else
1886 {
1887
1888
1889 limb_type block_mult = max_block_10;
1890 while (*s)
1891 {
1892 limb_type block = 0;
1893 for (unsigned i = 0; i < digits_per_block_10; ++i)
1894 {
1895 limb_type val;
1896 if (*s >= '0' && *s <= '9')
1897 val = static_cast<limb_type>(*s - '0');
1898 else
1899 {
1900 #if defined(BOOST_NO_EXCEPTIONS)
1901 val = static_cast<unsigned>('0');
1902 #endif
1903
1904 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
1905 }
1906 block *= 10;
1907 block += val;
1908 if (!*++s)
1909 {
1910 block_mult = block_multiplier(i);
1911 break;
1912 }
1913 }
1914 eval_multiply(result, block_mult);
1915 eval_add(result, block);
1916 }
1917 }
1918 }
1919 if (isneg)
1920 result.negate();
1921 result.swap(*this);
1922 }
1923
1924 public:
1925 cpp_int_backend& operator=(const char* s)
1926 {
1927 do_assign_string(s, trivial_tag());
1928 return *this;
1929 }
1930 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) noexcept
1931 {
1932 this->do_swap(o);
1933 }
1934
1935 private:
1936 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
1937 {
1938 using io_type = typename std::conditional<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type;
1939 if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
1940 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1941 std::stringstream ss;
1942 ss.flags(f & ~std::ios_base::showpos);
1943 ss << static_cast<io_type>(*this->limbs());
1944 std::string result;
1945 if (this->sign())
1946 result += '-';
1947 else if (f & std::ios_base::showpos)
1948 result += '+';
1949 result += ss.str();
1950 return result;
1951 }
1952 std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
1953 {
1954
1955 int base = 10;
1956 if ((f & std::ios_base::oct) == std::ios_base::oct)
1957 base = 8;
1958 else if ((f & std::ios_base::hex) == std::ios_base::hex)
1959 base = 16;
1960 std::string result;
1961
1962 std::size_t Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
1963
1964 if (base == 8 || base == 16)
1965 {
1966 if (this->sign())
1967 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
1968 limb_type shift = base == 8 ? 3 : 4;
1969 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
1970 typename base_type::local_limb_type v = *this->limbs();
1971 result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
1972 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
1973 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
1974 for (std::size_t i = 0; i < Bits / shift; ++i)
1975 {
1976 char c = static_cast<char>('0' + static_cast<char>(v & mask));
1977 if (c > '9')
1978 c = static_cast<char>(c + letter_a - '9' - 1);
1979 result[static_cast<std::size_t>(pos)] = c;
1980 --pos;
1981 v >>= shift;
1982 }
1983 if (Bits % shift)
1984 {
1985 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
1986 char c = static_cast<char>('0' + static_cast<char>(v & mask));
1987 if (c > '9')
1988 c = static_cast<char>(c + letter_a - '9');
1989 result[static_cast<std::size_t>(pos)] = c;
1990 }
1991
1992
1993
1994 std::string::size_type n = result.find_first_not_of('0');
1995 if (!result.empty() && (n == std::string::npos))
1996 n = result.size() - 1;
1997 result.erase(0, n);
1998 if (f & std::ios_base::showbase)
1999 {
2000 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
2001 result.insert(static_cast<std::string::size_type>(0), pp);
2002 }
2003 }
2004 else
2005 {
2006 result.assign(Bits / 3 + 1, '0');
2007 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
2008 typename base_type::local_limb_type v(*this->limbs());
2009 bool neg = false;
2010 if (this->sign())
2011 {
2012 neg = true;
2013 }
2014 while (v)
2015 {
2016 result[static_cast<std::string::size_type>(pos)] = static_cast<char>(static_cast<char>(v % 10) + '0');
2017 --pos;
2018 v /= 10;
2019 }
2020 std::string::size_type n = result.find_first_not_of('0');
2021 result.erase(0, n);
2022 if (result.empty())
2023 result = "0";
2024 if (neg)
2025 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2026 else if (f & std::ios_base::showpos)
2027 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2028 }
2029 return result;
2030 }
2031 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
2032 {
2033 #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
2034 return do_get_trivial_string(f, std::integral_constant<bool, std::is_same<typename base_type::local_limb_type, double_limb_type>::value>());
2035 #else
2036 return do_get_trivial_string(f, std::integral_constant<bool, false>());
2037 #endif
2038 }
2039 std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
2040 {
2041 using default_ops::eval_get_sign;
2042 int base = 10;
2043 if ((f & std::ios_base::oct) == std::ios_base::oct)
2044 base = 8;
2045 else if ((f & std::ios_base::hex) == std::ios_base::hex)
2046 base = 16;
2047 std::string result;
2048
2049 std::size_t Bits = this->size() * base_type::limb_bits;
2050
2051 if (base == 8 || base == 16)
2052 {
2053 if (this->sign())
2054 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
2055 limb_type shift = base == 8 ? 3 : 4;
2056 limb_type mask = static_cast<limb_type>((1u << shift) - 1);
2057 cpp_int_backend t(*this);
2058 result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
2059 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
2060 char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
2061 for (std::size_t i = 0; i < Bits / shift; ++i)
2062 {
2063 char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
2064 if (c > '9')
2065 c = static_cast<char>(c + letter_a - '9' - 1);
2066 result[static_cast<std::size_t>(pos)] = c;
2067 --pos;
2068 eval_right_shift(t, shift);
2069 }
2070 if (Bits % shift)
2071 {
2072 mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
2073 char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
2074 if (c > '9')
2075 c = static_cast<char>(c + letter_a - '9');
2076 result[static_cast<std::size_t>(pos)] = c;
2077 }
2078
2079
2080
2081 std::string::size_type n = result.find_first_not_of('0');
2082 if (!result.empty() && (n == std::string::npos))
2083 n = result.size() - 1;
2084 result.erase(0, n);
2085 if (f & std::ios_base::showbase)
2086 {
2087 const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
2088 result.insert(static_cast<std::string::size_type>(0), pp);
2089 }
2090 }
2091 else
2092 {
2093 result.assign(Bits / 3 + 1, '0');
2094 std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
2095 cpp_int_backend t(*this);
2096 cpp_int_backend r;
2097 bool neg = false;
2098 if (t.sign())
2099 {
2100 t.negate();
2101 neg = true;
2102 }
2103 if (this->size() == 1)
2104 {
2105 result = std::to_string(t.limbs()[0]);
2106 }
2107 else
2108 {
2109 cpp_int_backend block10;
2110 block10 = max_block_10;
2111 while (eval_get_sign(t) != 0)
2112 {
2113 cpp_int_backend t2;
2114 divide_unsigned_helper(&t2, t, block10, r);
2115 t = t2;
2116 limb_type v = r.limbs()[0];
2117 for (std::size_t i = 0; i < digits_per_block_10; ++i)
2118 {
2119 char c = static_cast<char>('0' + static_cast<char>(v % 10));
2120 v /= 10;
2121 result[static_cast<std::size_t>(pos)] = c;
2122 if (pos-- == 0u)
2123 break;
2124 }
2125 }
2126 }
2127 std::string::size_type n = result.find_first_not_of('0');
2128 result.erase(0, n);
2129 if (result.empty())
2130 result = std::string(static_cast<std::size_t>(1u), '0');
2131 if (neg)
2132 result.insert(static_cast<std::string::size_type>(0), 1, '-');
2133 else if (f & std::ios_base::showpos)
2134 result.insert(static_cast<std::string::size_type>(0), 1, '+');
2135 }
2136 return result;
2137 }
2138
2139 public:
2140 std::string str(std::streamsize , std::ios_base::fmtflags f) const
2141 {
2142 return do_get_string(f, trivial_tag());
2143 }
2144
2145 private:
2146 template <class Container>
2147 void construct_from_container(const Container& c, const std::integral_constant<bool, false>&)
2148 {
2149
2150
2151
2152 std::size_t newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
2153 if (c.size() % sizeof(limb_type))
2154 {
2155 ++newsize;
2156 }
2157 if (newsize)
2158 {
2159 this->resize(newsize, newsize);
2160 std::memset(this->limbs(), 0, this->size());
2161 typename Container::const_iterator i(c.begin()), j(c.end());
2162 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2163 while (i != j)
2164 {
2165 std::size_t limb = byte_location / sizeof(limb_type);
2166 std::size_t shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
2167 if (this->size() > limb)
2168 this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2169 ++i;
2170 --byte_location;
2171 }
2172 }
2173 }
2174 template <class Container>
2175 BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const std::integral_constant<bool, true>&)
2176 {
2177
2178
2179
2180 using local_limb_type = typename base_type::local_limb_type;
2181 *this->limbs() = 0;
2182 if (c.size())
2183 {
2184 typename Container::const_iterator i(c.begin()), j(c.end());
2185 std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
2186 while (i != j)
2187 {
2188 std::size_t limb = byte_location / sizeof(local_limb_type);
2189 std::size_t shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
2190 if (limb == 0)
2191 this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
2192 ++i;
2193 --byte_location;
2194 }
2195 }
2196 }
2197
2198 public:
2199 template <class Container>
2200 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)
2201 {
2202
2203
2204
2205 construct_from_container(c, trivial_tag());
2206 }
2207 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2208 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
2209 {
2210 if (this->sign() != o.sign())
2211 return this->sign() ? -1 : 1;
2212
2213
2214 int result = compare_unsigned(o);
2215
2216 if (this->sign())
2217 result = -result;
2218 return result;
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, false>&) const
2222 {
2223 cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
2224 return t.compare(o);
2225 }
2226 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2227 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
2228 {
2229 cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
2230 return compare(t);
2231 }
2232 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2233 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
2234 {
2235 if (this->sign())
2236 {
2237 if (o.sign())
2238 {
2239 return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
2240 }
2241 else
2242 return -1;
2243 }
2244 else
2245 {
2246 if (o.sign())
2247 return 1;
2248 return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
2249 }
2250 }
2251 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2252 BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2253 {
2254 using t1 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> ;
2255 using t2 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>;
2256 return compare_imp(o, t1(), t2());
2257 }
2258 template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
2259 BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
2260 {
2261 if (this->size() != o.size())
2262 {
2263 return this->size() > o.size() ? 1 : -1;
2264 }
2265 typename base_type::const_limb_pointer pa = this->limbs();
2266 typename base_type::const_limb_pointer pb = o.limbs();
2267 for (std::ptrdiff_t i = static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(this->size()) - 1); i >= 0; --i)
2268 {
2269 if (pa[i] != pb[i])
2270 return pa[i] > pb[i] ? 1 : -1;
2271 }
2272 return 0;
2273 }
2274 template <class Arithmetic>
2275 BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Arithmetic>::value, int>::type compare(Arithmetic i) const
2276 {
2277
2278 cpp_int_backend t;
2279 t = i;
2280 return compare(t);
2281 }
2282 };
2283
2284 }
2285
2286 namespace default_ops {
2287
2288 template <class Backend>
2289 struct double_precision_type;
2290
2291 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2292 struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2293 {
2294 using type = typename std::conditional<
2295 backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2296 backends::cpp_int_backend<
2297 (std::is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
2298 : MinBits),
2299 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
2300 SignType,
2301 Checked,
2302 Allocator>,
2303 backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type;
2304 };
2305
2306 }
2307
2308 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>
2309 struct is_equivalent_number_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
2310 : 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>{};
2311
2312 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2313 struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::integral_constant<int, number_kind_integer>
2314 {};
2315
2316 #ifdef BOOST_HAS_INT128
2317
2318 namespace detail {
2319
2320 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2321 struct is_convertible_arithmetic<int128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2322 {
2323 static constexpr bool value = true;
2324 };
2325 template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
2326 struct is_convertible_arithmetic<uint128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
2327 {
2328 static constexpr bool value = true;
2329 };
2330
2331 }
2332
2333 #endif
2334
2335 #if defined(__GNUC__) && !defined(__clang__)
2336
2337
2338 #pragma GCC diagnostic pop
2339 #endif
2340 #ifdef BOOST_MSVC
2341 #pragma warning(pop)
2342 #endif
2343
2344 }}
2345
2346
2347
2348
2349 #include <boost/multiprecision/cpp_int/limits.hpp>
2350 #include <boost/multiprecision/cpp_int/comparison.hpp>
2351 #include <boost/multiprecision/cpp_int/add.hpp>
2352 #include <boost/multiprecision/cpp_int/multiply.hpp>
2353 #include <boost/multiprecision/cpp_int/divide.hpp>
2354 #include <boost/multiprecision/cpp_int/bitwise.hpp>
2355 #include <boost/multiprecision/cpp_int/misc.hpp>
2356 #include <boost/multiprecision/cpp_int/literals.hpp>
2357 #include <boost/multiprecision/cpp_int/serialize.hpp>
2358 #include <boost/multiprecision/cpp_int/import_export.hpp>
2359
2360 #endif