Chris@101: //////////////////3///////////////////////////////////////////// Chris@16: // Copyright 2012 John Maddock. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_ Chris@16: Chris@16: #ifndef BOOST_MP_CPP_INT_HPP Chris@16: #define BOOST_MP_CPP_INT_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #ifdef BOOST_MP_USER_DEFINED_LITERALS Chris@16: #include Chris@16: #endif Chris@16: Chris@16: namespace boost{ Chris@16: namespace multiprecision{ Chris@16: namespace backends{ Chris@16: Chris@16: using boost::enable_if; Chris@16: Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: // warning C4127: conditional expression is constant Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4127 4351 4293 4996 4307 4702) Chris@16: #endif Chris@16: Chris@16: template >::type > Chris@16: struct cpp_int_backend; Chris@16: Chris@16: template Chris@16: struct cpp_int_base; Chris@16: // Chris@16: // Traits class determines the maximum and minimum precision values: Chris@16: // Chris@16: template struct max_precision; Chris@16: Chris@16: template Chris@16: struct max_precision > Chris@16: { Chris@16: static const unsigned value = is_void::value ? Chris@16: static_unsigned_max::value Chris@16: : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : UINT_MAX); Chris@16: }; Chris@16: Chris@16: template struct min_precision; Chris@16: Chris@16: template Chris@16: struct min_precision > Chris@16: { Chris@16: static const unsigned value = (is_void::value ? static_unsigned_max::value : MinBits); Chris@16: }; Chris@16: // Chris@16: // Traits class determines whether the number of bits precision requested could fit in a native type, Chris@16: // we call this a "trivial" cpp_int: Chris@16: // Chris@16: template Chris@16: struct is_trivial_cpp_int Chris@16: { Chris@16: static const bool value = false; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_trivial_cpp_int > Chris@16: { Chris@16: typedef cpp_int_backend self; Chris@16: static const bool value = is_void::value && (max_precision::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0)); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_trivial_cpp_int > Chris@16: { Chris@16: static const bool value = true; Chris@16: }; Chris@16: Chris@16: } // namespace backends Chris@16: // Chris@16: // Traits class to determine whether a cpp_int_backend is signed or not: Chris@16: // Chris@16: template Chris@16: struct is_unsigned_number > Chris@16: : public mpl::bool_<(SignType == unsigned_magnitude) || (SignType == unsigned_packed)>{}; Chris@16: Chris@16: namespace backends{ Chris@16: // Chris@16: // Traits class determines whether T should be implicitly convertible to U, or Chris@16: // whether the constructor should be made explicit. The latter happens if we Chris@16: // are losing the sign, or have fewer digits precision in the target type: Chris@16: // Chris@16: template Chris@16: struct is_implicit_cpp_int_conversion; Chris@16: Chris@16: template Chris@16: struct is_implicit_cpp_int_conversion, cpp_int_backend > Chris@16: { Chris@16: typedef cpp_int_backend t1; Chris@16: typedef cpp_int_backend t2; Chris@16: static const bool value = Chris@16: (is_signed_number::value || !is_signed_number::value) Chris@16: && (max_precision::value <= max_precision::value); Chris@16: }; Chris@16: Chris@16: // Chris@16: // Traits class to determine whether operations on a cpp_int may throw: Chris@16: // Chris@16: template Chris@16: struct is_non_throwing_cpp_int : public mpl::false_{}; Chris@16: template Chris@16: struct is_non_throwing_cpp_int > : public mpl::true_ {}; Chris@16: Chris@16: // Chris@16: // Traits class, determines whether the cpp_int is fixed precision or not: Chris@16: // Chris@16: template Chris@16: struct is_fixed_precision; Chris@16: template Chris@16: struct is_fixed_precision > Chris@16: : public mpl::bool_ >::value != UINT_MAX> {}; Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: inline void verify_new_size(unsigned new_size, unsigned min_size, const mpl::int_&) Chris@16: { Chris@16: if(new_size < min_size) Chris@16: BOOST_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude.")); Chris@16: } Chris@16: inline void verify_new_size(unsigned /*new_size*/, unsigned /*min_size*/, const mpl::int_&){} Chris@16: Chris@16: template Chris@16: inline void verify_limb_mask(bool b, U limb, U mask, const mpl::int_&) Chris@16: { Chris@16: // When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error: Chris@16: if(b && (limb & ~mask)) Chris@16: BOOST_THROW_EXCEPTION(std::overflow_error("Overflow in cpp_int arithmetic: there is insufficient precision in the target type to hold all of the bits of the result.")); Chris@16: } Chris@16: template Chris@16: inline void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const mpl::int_&){} Chris@16: Chris@16: } Chris@16: Chris@16: // Chris@16: // Now define the various data layouts that are possible as partial specializations of the base class, Chris@16: // starting with the default arbitrary precision signed integer type: Chris@16: // Chris@16: template Chris@16: struct cpp_int_base : private Allocator::template rebind::other Chris@16: { Chris@16: typedef typename Allocator::template rebind::other allocator_type; Chris@16: typedef typename allocator_type::pointer limb_pointer; Chris@16: typedef typename allocator_type::const_pointer const_limb_pointer; Chris@16: typedef mpl::int_ checked_type; Chris@16: Chris@16: // Chris@16: // Interface invariants: Chris@16: // Chris@16: BOOST_STATIC_ASSERT(!is_void::value); Chris@16: Chris@16: private: Chris@16: struct limb_data Chris@16: { Chris@16: unsigned capacity; Chris@16: limb_pointer data; Chris@16: }; Chris@16: Chris@16: public: Chris@16: BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT); Chris@16: BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast(0u)); Chris@16: BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1)); Chris@16: BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = Chris@16: MinBits Chris@16: ? MinBits / limb_bits + (MinBits % limb_bits ? 1 : 0) Chris@16: : sizeof(limb_data) / sizeof(limb_type)); Chris@16: BOOST_STATIC_CONSTANT(bool, variable = true); Chris@16: Chris@16: private: Chris@16: union data_type Chris@16: { Chris@16: limb_data ld; Chris@16: limb_type la[internal_limb_count]; Chris@16: limb_type first; Chris@16: double_limb_type double_first; Chris@16: Chris@16: BOOST_CONSTEXPR data_type() : first(0) {} Chris@16: BOOST_CONSTEXPR data_type(limb_type i) : first(i) {} Chris@101: BOOST_CONSTEXPR data_type(signed_limb_type i) : first(i < 0 ? static_cast(boost::multiprecision::detail::unsigned_abs(i)) : i) {} Chris@16: #ifdef BOOST_LITTLE_ENDIAN Chris@16: BOOST_CONSTEXPR data_type(double_limb_type i) : double_first(i) {} Chris@101: BOOST_CONSTEXPR data_type(signed_double_limb_type i) : double_first(i < 0 ? static_cast(boost::multiprecision::detail::unsigned_abs(i)) : i) {} Chris@16: #endif Chris@16: }; Chris@16: Chris@16: data_type m_data; Chris@16: unsigned m_limbs; Chris@16: bool m_sign, m_internal; Chris@16: Chris@16: public: Chris@16: // Chris@16: // Direct construction: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT Chris@16: : m_data(i), m_limbs(1), m_sign(false), m_internal(true) { } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT Chris@16: : m_data(i), m_limbs(1), m_sign(i < 0), m_internal(true) { } Chris@16: #if defined(BOOST_LITTLE_ENDIAN) Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT Chris@16: : m_data(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false), m_internal(true) { } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT Chris@101: : m_data(i), m_limbs(i < 0 ? (static_cast(boost::multiprecision::detail::unsigned_abs(i)) > static_cast(max_limb_value) ? 2 : 1) : (i > max_limb_value ? 2 : 1)), Chris@16: m_sign(i < 0), m_internal(true) { } Chris@16: #endif Chris@16: // Chris@16: // Helper functions for getting at our internal data, and manipulating storage: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE allocator_type& allocator() BOOST_NOEXCEPT { return *this; } Chris@16: BOOST_MP_FORCEINLINE const allocator_type& allocator()const BOOST_NOEXCEPT { return *this; } Chris@16: BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; } Chris@16: BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; } Chris@16: BOOST_MP_FORCEINLINE const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; } Chris@16: BOOST_MP_FORCEINLINE unsigned capacity()const BOOST_NOEXCEPT { return m_internal ? internal_limb_count : m_data.ld.capacity; } Chris@16: BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; } Chris@16: void sign(bool b) BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = b; Chris@16: // Check for zero value: Chris@16: if(m_sign && (m_limbs == 1)) Chris@16: { Chris@16: if(limbs()[0] == 0) Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: void resize(unsigned new_size, unsigned min_size) Chris@16: { Chris@16: static const unsigned max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + (MaxBits % (CHAR_BIT * sizeof(limb_type)) ? 1 : 0); Chris@16: // We never resize beyond MaxSize: Chris@16: if(new_size > max_limbs) Chris@16: new_size = max_limbs; Chris@16: detail::verify_new_size(new_size, min_size, checked_type()); Chris@16: // See if we have enough capacity already: Chris@16: unsigned cap = capacity(); Chris@16: if(new_size > cap) Chris@16: { Chris@16: // Allocate a new buffer and copy everything over: Chris@16: cap = (std::min)((std::max)(cap * 4, new_size), max_limbs); Chris@16: limb_pointer pl = allocator().allocate(cap); Chris@101: std::memcpy(pl, limbs(), size() * sizeof(limbs()[0])); Chris@16: if(!m_internal) Chris@16: allocator().deallocate(limbs(), capacity()); Chris@16: else Chris@16: m_internal = false; Chris@16: m_limbs = new_size; Chris@16: m_data.ld.capacity = cap; Chris@16: m_data.ld.data = pl; Chris@16: } Chris@16: else Chris@16: { Chris@16: m_limbs = new_size; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT Chris@16: { Chris@16: limb_pointer p = limbs(); Chris@16: while((m_limbs-1) && !p[m_limbs - 1])--m_limbs; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(), m_limbs(1), m_sign(false), m_internal(true) {} Chris@16: BOOST_MP_FORCEINLINE cpp_int_base(const cpp_int_base& o) : allocator_type(o), m_limbs(0), m_internal(true) Chris@16: { Chris@16: resize(o.size(), o.size()); Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0])); Chris@16: m_sign = o.m_sign; Chris@16: } Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: cpp_int_base(cpp_int_base&& o) Chris@101: : allocator_type(static_cast(o)), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal) Chris@16: { Chris@16: if(m_internal) Chris@16: { Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0])); Chris@16: } Chris@16: else Chris@16: { Chris@16: m_data.ld = o.m_data.ld; Chris@16: o.m_limbs = 0; Chris@16: o.m_internal = true; Chris@16: } Chris@16: } Chris@16: cpp_int_base& operator = (cpp_int_base&& o) BOOST_NOEXCEPT Chris@16: { Chris@16: if(!m_internal) Chris@16: allocator().deallocate(m_data.ld.data, m_data.ld.capacity); Chris@101: *static_cast(this) = static_cast(o); Chris@16: m_limbs = o.m_limbs; Chris@16: m_sign = o.m_sign; Chris@16: m_internal = o.m_internal; Chris@16: if(m_internal) Chris@16: { Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0])); Chris@16: } Chris@16: else Chris@16: { Chris@16: m_data.ld = o.m_data.ld; Chris@16: o.m_limbs = 0; Chris@16: o.m_internal = true; Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: BOOST_MP_FORCEINLINE ~cpp_int_base() BOOST_NOEXCEPT Chris@16: { Chris@16: if(!m_internal) Chris@16: allocator().deallocate(limbs(), capacity()); Chris@16: } Chris@16: void assign(const cpp_int_base& o) Chris@16: { Chris@16: if(this != &o) Chris@16: { Chris@16: static_cast(*this) = static_cast(o); Chris@16: m_limbs = 0; Chris@16: resize(o.size(), o.size()); Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0])); Chris@16: m_sign = o.m_sign; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = !m_sign; Chris@16: // Check for zero value: Chris@16: if(m_sign && (m_limbs == 1)) Chris@16: { Chris@16: if(limbs()[0] == 0) Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT Chris@16: { Chris@16: return m_sign; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(m_data, o.m_data); Chris@16: std::swap(m_sign, o.m_sign); Chris@16: std::swap(m_internal, o.m_internal); Chris@16: std::swap(m_limbs, o.m_limbs); Chris@16: } Chris@101: protected: Chris@101: template Chris@101: void check_in_range(const A&) BOOST_NOEXCEPT {} Chris@16: }; Chris@16: Chris@16: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION Chris@16: Chris@16: template Chris@16: const unsigned cpp_int_base::limb_bits; Chris@16: template Chris@16: const limb_type cpp_int_base::max_limb_value; Chris@16: template Chris@16: const limb_type cpp_int_base::sign_bit_mask; Chris@16: template Chris@16: const unsigned cpp_int_base::internal_limb_count; Chris@16: template Chris@16: const bool cpp_int_base::variable; Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: struct cpp_int_base : private Allocator::template rebind::other Chris@16: { Chris@16: // Chris@16: // There is currently no support for unsigned arbitrary precision arithmetic, largely Chris@16: // because it's not clear what subtraction should do: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG(((sizeof(Allocator) == 0) && !is_void::value), "There is curently no support for unsigned arbitrary precision integers."); Chris@16: }; Chris@16: // Chris@16: // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count: Chris@16: // Chris@16: template Chris@16: struct cpp_int_base Chris@16: { Chris@16: typedef limb_type* limb_pointer; Chris@16: typedef const limb_type* const_limb_pointer; Chris@16: typedef mpl::int_ checked_type; Chris@16: Chris@16: // Chris@16: // Interface invariants: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?"); Chris@16: Chris@16: public: Chris@16: BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT); Chris@16: BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast(0u)); Chris@16: BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1)); Chris@16: BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + (MinBits % limb_bits ? 1 : 0)); Chris@16: BOOST_STATIC_CONSTANT(bool, variable = false); Chris@16: BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = MinBits % limb_bits ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0))); Chris@16: BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs"); Chris@16: Chris@16: private: Chris@16: union data_type{ Chris@16: limb_type m_data[internal_limb_count]; Chris@16: limb_type m_first_limb; Chris@16: double_limb_type m_double_first_limb; Chris@16: Chris@16: BOOST_CONSTEXPR data_type() : m_first_limb(0) {} Chris@16: BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {} Chris@16: BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {} Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@16: template Chris@16: BOOST_CONSTEXPR data_type(literals::detail::value_pack) : m_data{ VALUES... } {} Chris@16: #endif Chris@16: } m_wrapper; Chris@16: boost::uint16_t m_limbs; Chris@16: bool m_sign; Chris@16: Chris@16: public: Chris@16: // Chris@16: // Direct construction: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT Chris@16: : m_wrapper(i), m_limbs(1), m_sign(false) {} Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT Chris@101: : m_wrapper(limb_type(i < 0 ? static_cast(-static_cast(i)) : i)), m_limbs(1), m_sign(i < 0) {} Chris@16: #if defined(BOOST_LITTLE_ENDIAN) Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT Chris@16: : m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false) {} Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT Chris@101: : m_wrapper(double_limb_type(i < 0 ? static_cast(boost::multiprecision::detail::unsigned_abs(i)) : i)), Chris@101: m_limbs(i < 0 ? (static_cast(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1)), Chris@16: m_sign(i < 0) {} Chris@16: #endif Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@16: template Chris@16: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack i) Chris@16: : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false) {} Chris@16: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<> i) Chris@16: : m_wrapper(i), m_limbs(1), m_sign(false) {} Chris@16: BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) Chris@16: : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {} Chris@16: #endif Chris@16: // Chris@16: // Helper functions for getting at our internal data, and manipulating storage: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; } Chris@16: BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return m_sign; } Chris@16: BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = b; Chris@16: // Check for zero value: Chris@16: if(m_sign && (m_limbs == 1)) Chris@16: { Chris@16: if(limbs()[0] == 0) Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: m_limbs = static_cast((std::min)(new_size, internal_limb_count)); Chris@16: detail::verify_new_size(m_limbs, min_size, checked_type()); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: limb_pointer p = limbs(); Chris@16: detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type()); Chris@16: p[internal_limb_count-1] &= upper_limb_mask; Chris@16: while((m_limbs-1) && !p[m_limbs - 1])--m_limbs; Chris@16: if((m_limbs == 1) && (!*p)) m_sign = false; // zero is always unsigned Chris@16: } Chris@16: Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base()BOOST_NOEXCEPT : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {} Chris@101: // Not defaulted, it breaks constexpr support in the Intel compiler for some reason: Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o)BOOST_NOEXCEPT Chris@101: : m_wrapper(o.m_wrapper), m_limbs(o.m_limbs), m_sign(o.m_sign) {} Chris@16: // Defaulted functions: Chris@16: //~cpp_int_base() BOOST_NOEXCEPT {} Chris@16: Chris@16: void assign(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: if(this != &o) Chris@16: { Chris@101: m_limbs = o.m_limbs; Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0])); Chris@16: m_sign = o.m_sign; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = !m_sign; Chris@16: // Check for zero value: Chris@16: if(m_sign && (m_limbs == 1)) Chris@16: { Chris@16: if(limbs()[0] == 0) Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT Chris@16: { Chris@16: return m_sign; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i) Chris@16: std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]); Chris@16: std::swap(m_sign, o.m_sign); Chris@16: std::swap(m_limbs, o.m_limbs); Chris@16: } Chris@101: protected: Chris@101: template Chris@101: void check_in_range(const A&) BOOST_NOEXCEPT {} Chris@16: }; Chris@16: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION Chris@16: Chris@16: template Chris@16: const unsigned cpp_int_base::limb_bits; Chris@16: template Chris@16: const limb_type cpp_int_base::max_limb_value; Chris@16: template Chris@16: const limb_type cpp_int_base::sign_bit_mask; Chris@16: template Chris@16: const unsigned cpp_int_base::internal_limb_count; Chris@16: template Chris@16: const bool cpp_int_base::variable; Chris@16: Chris@16: #endif Chris@16: // Chris@16: // Fixed precision (i.e. no allocator), unsigned type with limb-usage count: Chris@16: // Chris@16: template Chris@16: struct cpp_int_base Chris@16: { Chris@16: typedef limb_type* limb_pointer; Chris@16: typedef const limb_type* const_limb_pointer; Chris@16: typedef mpl::int_ checked_type; Chris@16: Chris@16: // Chris@16: // Interface invariants: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?"); Chris@16: Chris@16: public: Chris@16: BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT); Chris@16: BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast(0u)); Chris@16: BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1)); Chris@16: BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + (MinBits % limb_bits ? 1 : 0)); Chris@16: BOOST_STATIC_CONSTANT(bool, variable = false); Chris@16: BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = MinBits % limb_bits ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0))); Chris@16: BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs"); Chris@16: Chris@16: private: Chris@16: union data_type{ Chris@16: limb_type m_data[internal_limb_count]; Chris@16: limb_type m_first_limb; Chris@16: double_limb_type m_double_first_limb; Chris@16: Chris@16: BOOST_CONSTEXPR data_type() : m_first_limb(0) {} Chris@16: BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {} Chris@16: BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {} Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@16: template Chris@16: BOOST_CONSTEXPR data_type(literals::detail::value_pack) : m_data{ VALUES... } {} Chris@16: #endif Chris@16: } m_wrapper; Chris@16: limb_type m_limbs; Chris@16: Chris@16: public: Chris@16: // Chris@16: // Direct construction: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT Chris@16: : m_wrapper(i), m_limbs(1) {} Chris@16: BOOST_MP_FORCEINLINE cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@101: : m_wrapper(limb_type(i < 0 ? static_cast(-static_cast(i)) : i)), m_limbs(1) { if(i < 0) negate(); } Chris@16: #ifdef BOOST_LITTLE_ENDIAN Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT Chris@16: : m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1) {} Chris@16: BOOST_MP_FORCEINLINE cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@101: : m_wrapper(double_limb_type(i < 0 ? static_cast(boost::multiprecision::detail::unsigned_abs(i)) : i)), Chris@101: m_limbs(i < 0 ? (static_cast(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1)) Chris@101: { Chris@101: if (i < 0) negate(); Chris@101: } Chris@16: #endif Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@16: template Chris@16: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack i) Chris@16: : m_wrapper(i), m_limbs(sizeof...(VALUES)) {} Chris@16: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) Chris@16: : m_wrapper(static_cast(0u)), m_limbs(1) {} Chris@16: #endif Chris@16: // Chris@16: // Helper functions for getting at our internal data, and manipulating storage: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; } Chris@16: BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; } Chris@16: BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT_IF((Checked == unchecked)) { if(b) negate(); } Chris@16: BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: m_limbs = (std::min)(new_size, internal_limb_count); Chris@16: detail::verify_new_size(m_limbs, min_size, checked_type()); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: limb_pointer p = limbs(); Chris@16: detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type()); Chris@16: p[internal_limb_count-1] &= upper_limb_mask; Chris@16: while((m_limbs-1) && !p[m_limbs - 1])--m_limbs; Chris@16: } Chris@16: Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT Chris@16: : m_wrapper(limb_type(0u)), m_limbs(1) {} Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT Chris@101: : m_wrapper(o.m_wrapper), m_limbs(o.m_limbs) {} Chris@16: // Defaulted functions: Chris@16: //~cpp_int_base() BOOST_NOEXCEPT {} Chris@16: Chris@16: BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: if(this != &o) Chris@16: { Chris@101: m_limbs = o.m_limbs; Chris@101: std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0])); Chris@16: } Chris@16: } Chris@16: private: Chris@16: void check_negate(const mpl::int_&) Chris@16: { Chris@16: BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number.")); Chris@16: } Chris@16: void check_negate(const mpl::int_&){} Chris@16: public: Chris@16: void negate() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: // Not so much a negate as a complement - this gets called when subtraction Chris@16: // would result in a "negative" number: Chris@16: unsigned i; Chris@16: if((m_limbs == 1) && (m_wrapper.m_data[0] == 0)) Chris@16: return; // negating zero is always zero, and always OK. Chris@16: check_negate(checked_type()); Chris@16: for(i = m_limbs; i < internal_limb_count; ++i) Chris@16: m_wrapper.m_data[i] = 0; Chris@16: m_limbs = internal_limb_count; Chris@16: for(i = 0; i < internal_limb_count; ++i) Chris@16: m_wrapper.m_data[i] = ~m_wrapper.m_data[i]; Chris@16: normalize(); Chris@16: eval_increment(static_cast& >(*this)); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT Chris@16: { Chris@16: return false; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i) Chris@16: std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]); Chris@16: std::swap(m_limbs, o.m_limbs); Chris@16: } Chris@101: protected: Chris@101: template Chris@101: void check_in_range(const A&) BOOST_NOEXCEPT {} Chris@16: }; Chris@16: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION Chris@16: Chris@16: template Chris@16: const unsigned cpp_int_base::limb_bits; Chris@16: template Chris@16: const limb_type cpp_int_base::max_limb_value; Chris@16: template Chris@16: const limb_type cpp_int_base::sign_bit_mask; Chris@16: template Chris@16: const unsigned cpp_int_base::internal_limb_count; Chris@16: template Chris@16: const bool cpp_int_base::variable; Chris@16: Chris@16: #endif Chris@16: // Chris@16: // Traits classes to figure out a native type with N bits, these vary from boost::uint_t only Chris@16: // because some platforms have native integer types longer than long long, "really long long" anyone?? Chris@16: // Chris@16: template Chris@16: struct trivial_limb_type_imp Chris@16: { Chris@16: typedef double_limb_type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct trivial_limb_type_imp Chris@16: { Chris@16: typedef typename boost::uint_t::least type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct trivial_limb_type : public trivial_limb_type_imp {}; Chris@16: // Chris@16: // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type": Chris@16: // Chris@16: template Chris@16: struct cpp_int_base Chris@16: { Chris@16: typedef typename trivial_limb_type::type local_limb_type; Chris@16: typedef local_limb_type* limb_pointer; Chris@16: typedef const local_limb_type* const_limb_pointer; Chris@16: typedef mpl::int_ checked_type; Chris@16: protected: Chris@16: BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT); Chris@16: BOOST_STATIC_CONSTANT(local_limb_type, limb_mask = (MinBits < limb_bits) ? local_limb_type((local_limb_type(~local_limb_type(0))) >> (limb_bits - MinBits)) : local_limb_type(~local_limb_type(0))); Chris@16: private: Chris@16: local_limb_type m_data; Chris@16: bool m_sign; Chris@16: Chris@16: // Chris@16: // Interface invariants: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?"); Chris@16: protected: Chris@16: template Chris@101: typename boost::disable_if_c::value || (std::numeric_limits::is_specialized && (std::numeric_limits::digits <= (int)MinBits))>::type Chris@16: check_in_range(T val, const mpl::int_&) Chris@16: { Chris@101: typedef typename common_type::type, local_limb_type>::type common_type; Chris@16: Chris@101: if(static_cast(boost::multiprecision::detail::unsigned_abs(val)) > static_cast(limb_mask)) Chris@16: BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent.")); Chris@16: } Chris@101: template Chris@101: typename boost::disable_if_c::value || (std::numeric_limits::is_specialized && (std::numeric_limits::digits <= (int)MinBits))>::type Chris@101: check_in_range(T val, const mpl::int_&) Chris@101: { Chris@101: using std::abs; Chris@101: typedef typename common_type::type common_type; Chris@101: Chris@101: if (static_cast(abs(val)) > static_cast(limb_mask)) Chris@101: BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent.")); Chris@101: } Chris@16: template Chris@101: void check_in_range(T, const mpl::int_&) BOOST_NOEXCEPT {} Chris@16: Chris@16: template Chris@101: void check_in_range(T val) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval(), checked_type()))) Chris@16: { Chris@16: check_in_range(val, checked_type()); Chris@16: } Chris@16: Chris@16: public: Chris@16: // Chris@16: // Direct construction: Chris@16: // Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(SI i, typename boost::enable_if_c::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@101: : m_data(i < 0 ? static_cast(static_cast::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask) : static_cast(i)& limb_mask), m_sign(i < 0) {} Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_base(SI i, typename boost::enable_if_c::value && (Checked == checked) >::type const* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@101: : m_data(i < 0 ? (static_cast(static_cast::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask)) : static_cast(i)& limb_mask), m_sign(i < 0) Chris@101: { check_in_range(i); } Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(UI i, typename boost::enable_if_c::value && (Checked == unchecked)>::type const* = 0) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(i) & limb_mask), m_sign(false) {} Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_base(UI i, typename boost::enable_if_c::value && (Checked == checked)>::type const* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@16: : m_data(static_cast(i) & limb_mask), m_sign(false) { check_in_range(i); } Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(F i, typename boost::enable_if_c::value && (Checked == unchecked)>::type const* = 0) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(std::fabs(i)) & limb_mask), m_sign(i < 0) {} Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_base(F i, typename boost::enable_if_c::value && (Checked == checked)>::type const* = 0) Chris@16: : m_data(static_cast(std::fabs(i)) & limb_mask), m_sign(i < 0) { check_in_range(i); } Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(0u)), m_sign(false) {} Chris@16: template Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack)BOOST_NOEXCEPT Chris@16: : m_data(static_cast(a)), m_sign(false) {} Chris@16: template Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack)BOOST_NOEXCEPT Chris@16: : m_data(static_cast(a) | (static_cast(b) << bits_per_limb)), m_sign(false) {} Chris@101: BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)BOOST_NOEXCEPT Chris@16: : m_data(a.m_data), m_sign(a.m_data ? !a.m_sign : false) {} Chris@16: #endif Chris@16: // Chris@16: // Helper functions for getting at our internal data, and manipulating storage: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; } Chris@16: BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; } Chris@16: BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; } Chris@16: BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = b; Chris@16: // Check for zero value: Chris@16: if(m_sign && !m_data) Chris@16: { Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) Chris@16: { Chris@16: detail::verify_new_size(2, min_size, checked_type()); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: if(!m_data) Chris@16: m_sign = false; // zero is always unsigned Chris@16: detail::verify_limb_mask(true, m_data, limb_mask, checked_type()); Chris@16: m_data &= limb_mask; Chris@16: } Chris@16: Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0), m_sign(false) {} Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: : m_data(o.m_data), m_sign(o.m_sign) {} Chris@16: //~cpp_int_base() BOOST_NOEXCEPT {} Chris@16: BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: m_data = o.m_data; Chris@16: m_sign = o.m_sign; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT Chris@16: { Chris@16: m_sign = !m_sign; Chris@16: // Check for zero value: Chris@16: if(m_data == 0) Chris@16: { Chris@16: m_sign = false; Chris@16: } Chris@16: } Chris@16: BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT Chris@16: { Chris@16: return m_sign; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(m_sign, o.m_sign); Chris@16: std::swap(m_data, o.m_data); Chris@16: } Chris@16: }; Chris@16: // Chris@16: // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type": Chris@16: // Chris@16: template Chris@16: struct cpp_int_base Chris@16: { Chris@16: typedef typename trivial_limb_type::type local_limb_type; Chris@16: typedef local_limb_type* limb_pointer; Chris@16: typedef const local_limb_type* const_limb_pointer; Chris@16: private: Chris@16: BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT); Chris@16: BOOST_STATIC_CONSTANT(local_limb_type, limb_mask = limb_bits != MinBits ? Chris@16: static_cast(static_cast(~local_limb_type(0)) >> (limb_bits - MinBits)) Chris@16: : static_cast(~local_limb_type(0))); Chris@16: Chris@16: local_limb_type m_data; Chris@16: Chris@16: typedef mpl::int_ checked_type; Chris@16: Chris@16: // Chris@16: // Interface invariants: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?"); Chris@16: protected: Chris@16: template Chris@16: typename boost::disable_if_c::is_specialized && (std::numeric_limits::digits <= (int)MinBits)>::type Chris@101: check_in_range(T val, const mpl::int_&, const boost::false_type&) Chris@16: { Chris@16: typedef typename common_type::type common_type; Chris@16: Chris@16: if(static_cast(val) > limb_mask) Chris@16: BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent.")); Chris@16: } Chris@16: template Chris@101: void check_in_range(T val, const mpl::int_&, const boost::true_type&) Chris@16: { Chris@16: typedef typename common_type::type common_type; Chris@16: Chris@16: if(static_cast(val) > limb_mask) Chris@16: BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent.")); Chris@16: if(val < 0) Chris@16: BOOST_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative.")); Chris@16: } Chris@16: template Chris@101: BOOST_MP_FORCEINLINE void check_in_range(T, const mpl::int_&, const boost::integral_constant&) BOOST_NOEXCEPT {} Chris@16: Chris@16: template Chris@101: BOOST_MP_FORCEINLINE void check_in_range(T val) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval(), checked_type(), is_signed()))) Chris@16: { Chris@16: check_in_range(val, checked_type(), is_signed()); Chris@16: } Chris@16: Chris@16: public: Chris@16: // Chris@16: // Direct construction: Chris@16: // Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(SI i, typename boost::enable_if_c::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT Chris@16: : m_data(i < 0 ? (1 + ~static_cast(-i)) & limb_mask : static_cast(i) & limb_mask) {} Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_base(SI i, typename boost::enable_if_c::value && (Checked == checked) >::type const* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@16: : m_data(i < 0 ? 1 + ~static_cast(-i) : static_cast(i)) { check_in_range(i); } Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(UI i, typename boost::enable_if_c::value && (Checked == unchecked) >::type const* = 0) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(i) & limb_mask) {} Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_base(UI i, typename boost::enable_if_c::value && (Checked == checked) >::type const* = 0) BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@16: : m_data(static_cast(i)) { check_in_range(i); } Chris@16: template Chris@16: BOOST_MP_FORCEINLINE cpp_int_base(F i, typename boost::enable_if >::type const* = 0) BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: : m_data(static_cast(std::fabs(i)) & limb_mask) Chris@16: { Chris@16: check_in_range(i); Chris@16: if(i < 0) Chris@16: negate(); Chris@16: } Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(0u)) {} Chris@16: template Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(a)) {} Chris@16: template Chris@101: BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack) BOOST_NOEXCEPT Chris@16: : m_data(static_cast(a) | (static_cast(b) << bits_per_limb)) {} Chris@16: #endif Chris@16: // Chris@16: // Helper functions for getting at our internal data, and manipulating storage: Chris@16: // Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; } Chris@16: BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; } Chris@16: BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: if(b) Chris@16: negate(); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) Chris@16: { Chris@16: detail::verify_new_size(2, min_size, checked_type()); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: detail::verify_limb_mask(true, m_data, limb_mask, checked_type()); Chris@16: m_data &= limb_mask; Chris@16: } Chris@16: Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0) {} Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: : m_data(o.m_data) {} Chris@16: //~cpp_int_base() BOOST_NOEXCEPT {} Chris@16: BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: m_data = o.m_data; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT_IF((Checked == unchecked)) Chris@16: { Chris@16: if(Checked == checked) Chris@16: { Chris@16: BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type.")); Chris@16: } Chris@16: m_data = ~m_data; Chris@16: ++m_data; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT Chris@16: { Chris@16: return false; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(m_data, o.m_data); Chris@16: } Chris@16: }; Chris@16: // Chris@16: // Traits class, lets us know whether type T can be directly converted to the base type, Chris@16: // used to enable/disable constructors etc: Chris@16: // Chris@16: template Chris@16: struct is_allowed_cpp_int_base_conversion : public mpl::if_c< Chris@16: is_same::value || is_same::value Chris@16: #ifdef BOOST_LITTLE_ENDIAN Chris@16: || is_same::value || is_same::value Chris@16: #endif Chris@16: #if defined(BOOST_MP_USER_DEFINED_LITERALS) Chris@16: || literals::detail::is_value_pack::value Chris@16: #endif Chris@16: || (is_trivial_cpp_int::value && is_arithmetic::value), Chris@16: mpl::true_, Chris@16: mpl::false_ Chris@16: >::type Chris@16: {}; Chris@16: // Chris@16: // Now the actual backend, normalising parameters passed to the base class: Chris@16: // Chris@16: template Chris@16: struct cpp_int_backend Chris@16: : public cpp_int_base< Chris@16: min_precision >::value, Chris@16: max_precision >::value, Chris@16: SignType, Chris@16: Checked, Chris@16: Allocator, Chris@16: is_trivial_cpp_int >::value> Chris@16: { Chris@16: typedef cpp_int_backend self_type; Chris@16: typedef cpp_int_base< Chris@16: min_precision::value, Chris@16: max_precision::value, Chris@16: SignType, Chris@16: Checked, Chris@16: Allocator, Chris@16: is_trivial_cpp_int::value> base_type; Chris@16: typedef mpl::bool_::value> trivial_tag; Chris@16: public: Chris@16: typedef typename mpl::if_< Chris@16: trivial_tag, Chris@16: mpl::list< Chris@16: signed char, short, int, long, Chris@16: long long, signed_double_limb_type>, Chris@16: mpl::list Chris@16: >::type signed_types; Chris@16: typedef typename mpl::if_< Chris@16: trivial_tag, Chris@16: mpl::list, Chris@16: mpl::list Chris@16: >::type unsigned_types; Chris@16: typedef typename mpl::if_< Chris@16: trivial_tag, Chris@16: mpl::list, Chris@16: mpl::list Chris@16: >::type float_types; Chris@16: typedef mpl::int_ checked_type; Chris@16: Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend() BOOST_NOEXCEPT{} Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(const cpp_int_backend& o) BOOST_NOEXCEPT_IF(boost::is_void::value) : base_type(o) {} Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(cpp_int_backend&& o) BOOST_NOEXCEPT Chris@16: : base_type(static_cast(o)) {} Chris@16: #endif Chris@16: // Chris@16: // Direct construction from arithmetic type: Chris@16: // Chris@16: template Chris@101: BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_backend(Arg i, typename boost::enable_if_c::value >::type const* = 0)BOOST_NOEXCEPT_IF(noexcept(base_type(std::declval()))) Chris@16: : base_type(i) {} Chris@16: Chris@16: private: Chris@16: template Chris@16: void do_assign(const cpp_int_backend& other, mpl::true_ const&, mpl::true_ const &) Chris@16: { Chris@16: // Assigning trivial type to trivial type: Chris@16: this->check_in_range(*other.limbs()); Chris@16: *this->limbs() = static_cast(*other.limbs()); Chris@16: this->sign(other.sign()); Chris@16: this->normalize(); Chris@16: } Chris@16: template Chris@16: void do_assign(const cpp_int_backend& other, mpl::true_ const&, mpl::false_ const &) Chris@16: { Chris@16: // non-trivial to trivial narrowing conversion: Chris@16: double_limb_type v = *other.limbs(); Chris@16: if(other.size() > 1) Chris@16: { Chris@16: v |= static_cast(other.limbs()[1]) << bits_per_limb; Chris@16: if((Checked == checked) && (other.size() > 2)) Chris@16: { Chris@16: BOOST_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type.")); Chris@16: } Chris@16: } Chris@16: *this = v; Chris@16: this->sign(other.sign()); Chris@16: this->normalize(); Chris@16: } Chris@16: template Chris@16: void do_assign(const cpp_int_backend& other, mpl::false_ const&, mpl::true_ const &) Chris@16: { Chris@16: // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards: Chris@16: *this = static_cast< Chris@16: typename boost::multiprecision::detail::canonical< Chris@16: typename cpp_int_backend::local_limb_type, Chris@16: cpp_int_backend Chris@16: >::type Chris@16: >(*other.limbs()); Chris@16: this->sign(other.sign()); Chris@16: } Chris@16: template Chris@16: void do_assign(const cpp_int_backend& other, mpl::false_ const&, mpl::false_ const &) Chris@16: { Chris@16: // regular non-trivial to non-trivial assign: Chris@16: this->resize(other.size(), other.size()); Chris@101: std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size(), this->size()) * sizeof(this->limbs()[0])); Chris@16: this->sign(other.sign()); Chris@16: this->normalize(); Chris@16: } Chris@16: public: Chris@16: template Chris@16: cpp_int_backend( Chris@16: const cpp_int_backend& other, Chris@16: typename boost::enable_if_c, self_type>::value>::type* = 0) Chris@16: : base_type() Chris@16: { Chris@16: do_assign( Chris@16: other, Chris@16: mpl::bool_::value>(), Chris@16: mpl::bool_ >::value>()); Chris@16: } Chris@16: template Chris@16: explicit cpp_int_backend( Chris@16: const cpp_int_backend& other, Chris@16: typename boost::disable_if_c, self_type>::value>::type* = 0) Chris@16: : base_type() Chris@16: { Chris@16: do_assign( Chris@16: other, Chris@16: mpl::bool_::value>(), Chris@16: mpl::bool_ >::value>()); Chris@16: } Chris@16: template Chris@16: cpp_int_backend& operator=( Chris@16: const cpp_int_backend& other) Chris@16: { Chris@16: do_assign( Chris@16: other, Chris@16: mpl::bool_::value>(), Chris@16: mpl::bool_ >::value>()); Chris@16: return *this; Chris@16: } Chris@16: #ifdef BOOST_MP_USER_DEFINED_LITERALS Chris@16: BOOST_CONSTEXPR cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag) Chris@16: : base_type(static_cast(a), tag){} Chris@16: #endif Chris@16: Chris@101: BOOST_MP_FORCEINLINE cpp_int_backend& operator = (const cpp_int_backend& o) BOOST_NOEXCEPT_IF(noexcept(std::declval().assign(std::declval()))) Chris@16: { Chris@16: this->assign(o); Chris@16: return *this; Chris@16: } Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@101: BOOST_MP_FORCEINLINE cpp_int_backend& operator = (cpp_int_backend&& o) BOOST_NOEXCEPT_IF(noexcept(std::declval() = std::declval())) Chris@16: { Chris@16: *static_cast(this) = static_cast(o); Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: private: Chris@16: template Chris@101: typename boost::enable_if >::type do_assign_arithmetic(A val, const mpl::true_&) Chris@101: BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval()))) Chris@16: { Chris@16: this->check_in_range(val); Chris@16: *this->limbs() = static_cast(val); Chris@16: this->normalize(); Chris@16: } Chris@16: template Chris@101: typename boost::disable_if_c::value || !is_integral::value >::type do_assign_arithmetic(A val, const mpl::true_&) Chris@101: BOOST_NOEXCEPT_IF(noexcept(std::declval().check_in_range(std::declval())) && noexcept(std::declval().sign(true))) Chris@16: { Chris@16: this->check_in_range(val); Chris@101: *this->limbs() = (val < 0) ? static_cast(boost::multiprecision::detail::unsigned_abs(val)) : static_cast(val); Chris@101: this->sign(val < 0); Chris@101: this->normalize(); Chris@101: } Chris@101: template Chris@101: typename boost::enable_if_c< !is_integral::value>::type do_assign_arithmetic(A val, const mpl::true_&) Chris@101: { Chris@101: this->check_in_range(val); Chris@101: *this->limbs() = (val < 0) ? static_cast(boost::multiprecision::detail::abs(val)) : static_cast(val); Chris@16: this->sign(val < 0); Chris@16: this->normalize(); Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void do_assign_arithmetic(limb_type i, const mpl::false_&) BOOST_NOEXCEPT Chris@16: { Chris@16: this->resize(1, 1); Chris@16: *this->limbs() = i; Chris@16: this->sign(false); Chris@16: } Chris@101: BOOST_MP_FORCEINLINE void do_assign_arithmetic(signed_limb_type i, const mpl::false_&) BOOST_NOEXCEPT_IF(noexcept(std::declval().sign(true))) Chris@16: { Chris@16: this->resize(1, 1); Chris@101: *this->limbs() = static_cast(boost::multiprecision::detail::unsigned_abs(i)); Chris@16: this->sign(i < 0); Chris@16: } Chris@101: void do_assign_arithmetic(double_limb_type i, const mpl::false_&) BOOST_NOEXCEPT Chris@16: { Chris@16: BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type)); Chris@16: BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2); Chris@16: typename base_type::limb_pointer p = this->limbs(); Chris@16: *p = static_cast(i); Chris@16: p[1] = static_cast(i >> base_type::limb_bits); Chris@16: this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1); Chris@16: this->sign(false); Chris@16: } Chris@101: void do_assign_arithmetic(signed_double_limb_type i, const mpl::false_&) BOOST_NOEXCEPT_IF(noexcept(std::declval().sign(true))) Chris@16: { Chris@16: BOOST_STATIC_ASSERT(sizeof(i) == 2 * sizeof(limb_type)); Chris@16: BOOST_STATIC_ASSERT(base_type::internal_limb_count >= 2); Chris@16: bool s = false; Chris@101: double_limb_type ui; Chris@16: if(i < 0) Chris@16: s = true; Chris@101: ui = static_cast(boost::multiprecision::detail::unsigned_abs(i)); Chris@16: typename base_type::limb_pointer p = this->limbs(); Chris@101: *p = static_cast(ui); Chris@101: p[1] = static_cast(ui >> base_type::limb_bits); Chris@16: this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1); Chris@16: this->sign(s); Chris@16: } Chris@16: Chris@16: void do_assign_arithmetic(long double a, const mpl::false_&) Chris@16: { Chris@16: using default_ops::eval_add; Chris@16: using default_ops::eval_subtract; Chris@16: using std::frexp; Chris@16: using std::ldexp; Chris@16: using std::floor; Chris@16: Chris@16: if (a == 0) { Chris@16: *this = static_cast(0u); Chris@16: } Chris@16: Chris@16: if (a == 1) { Chris@16: *this = static_cast(1u); Chris@16: } Chris@16: Chris@16: BOOST_ASSERT(!(boost::math::isinf)(a)); Chris@16: BOOST_ASSERT(!(boost::math::isnan)(a)); Chris@16: Chris@16: int e; Chris@16: long double f, term; Chris@16: *this = static_cast(0u); Chris@16: Chris@16: f = frexp(a, &e); Chris@16: Chris@16: static const limb_type shift = std::numeric_limits::digits; Chris@16: Chris@16: while(f) Chris@16: { Chris@16: // extract int sized bits from f: Chris@16: f = ldexp(f, shift); Chris@16: term = floor(f); Chris@16: e -= shift; Chris@16: eval_left_shift(*this, shift); Chris@16: if(term > 0) Chris@16: eval_add(*this, static_cast(term)); Chris@16: else Chris@16: eval_subtract(*this, static_cast(-term)); Chris@16: f -= term; Chris@16: } Chris@16: if(e > 0) Chris@16: eval_left_shift(*this, e); Chris@16: else if(e < 0) Chris@16: eval_right_shift(*this, -e); Chris@16: } Chris@16: public: Chris@16: template Chris@101: BOOST_MP_FORCEINLINE cpp_int_backend& operator = (Arithmetic val) BOOST_NOEXCEPT_IF(noexcept(std::declval().do_assign_arithmetic(std::declval(), trivial_tag()))) Chris@16: { Chris@16: do_assign_arithmetic(val, trivial_tag()); Chris@16: return *this; Chris@16: } Chris@16: private: Chris@16: void do_assign_string(const char* s, const mpl::true_&) Chris@16: { Chris@16: std::size_t n = s ? std::strlen(s) : 0; Chris@101: *this = 0; Chris@16: unsigned radix = 10; Chris@16: bool isneg = false; Chris@16: if(n && (*s == '-')) Chris@16: { Chris@16: --n; Chris@16: ++s; Chris@16: isneg = true; Chris@16: } Chris@16: if(n && (*s == '0')) Chris@16: { Chris@16: if((n > 1) && ((s[1] == 'x') || (s[1] == 'X'))) Chris@16: { Chris@16: radix = 16; Chris@16: s +=2; Chris@16: n -= 2; Chris@16: } Chris@16: else Chris@16: { Chris@16: radix = 8; Chris@16: n -= 1; Chris@16: } Chris@16: } Chris@16: if(n) Chris@16: { Chris@16: unsigned val; Chris@16: while(*s) Chris@16: { Chris@16: if(*s >= '0' && *s <= '9') Chris@16: val = *s - '0'; Chris@16: else if(*s >= 'a' && *s <= 'f') Chris@16: val = 10 + *s - 'a'; Chris@16: else if(*s >= 'A' && *s <= 'F') Chris@16: val = 10 + *s - 'A'; Chris@16: else Chris@16: val = radix + 1; Chris@16: if(val >= radix) Chris@16: { Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string.")); Chris@16: } Chris@16: *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast(radix), checked_type()); Chris@16: *this->limbs() = detail::checked_add(*this->limbs(), static_cast(val), checked_type()); Chris@16: ++s; Chris@16: } Chris@16: } Chris@16: if(isneg) Chris@16: this->negate(); Chris@16: } Chris@16: void do_assign_string(const char* s, const mpl::false_&) Chris@16: { Chris@16: using default_ops::eval_multiply; Chris@16: using default_ops::eval_add; Chris@16: std::size_t n = s ? std::strlen(s) : 0; Chris@16: *this = static_cast(0u); Chris@16: unsigned radix = 10; Chris@16: bool isneg = false; Chris@16: if(n && (*s == '-')) Chris@16: { Chris@16: --n; Chris@16: ++s; Chris@16: isneg = true; Chris@16: } Chris@16: if(n && (*s == '0')) Chris@16: { Chris@16: if((n > 1) && ((s[1] == 'x') || (s[1] == 'X'))) Chris@16: { Chris@16: radix = 16; Chris@16: s +=2; Chris@16: n -= 2; Chris@16: } Chris@16: else Chris@16: { Chris@16: radix = 8; Chris@16: n -= 1; Chris@16: } Chris@16: } Chris@16: if(n) Chris@16: { Chris@16: if(radix == 8 || radix == 16) Chris@16: { Chris@16: unsigned shift = radix == 8 ? 3 : 4; Chris@16: unsigned block_count = base_type::limb_bits / shift; Chris@16: unsigned block_shift = shift * block_count; Chris@16: limb_type val, block; Chris@16: while(*s) Chris@16: { Chris@16: block = 0; Chris@16: for(unsigned i = 0; (i < block_count); ++i) Chris@16: { Chris@16: if(*s >= '0' && *s <= '9') Chris@16: val = *s - '0'; Chris@16: else if(*s >= 'a' && *s <= 'f') Chris@16: val = 10 + *s - 'a'; Chris@16: else if(*s >= 'A' && *s <= 'F') Chris@16: val = 10 + *s - 'A'; Chris@16: else Chris@16: val = base_type::max_limb_value; Chris@16: if(val >= radix) Chris@16: { Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string.")); Chris@16: } Chris@16: block <<= shift; Chris@16: block |= val; Chris@16: if(!*++s) Chris@16: { Chris@16: // final shift is different: Chris@16: block_shift = (i + 1) * shift; Chris@16: break; Chris@16: } Chris@16: } Chris@16: eval_left_shift(*this, block_shift); Chris@16: this->limbs()[0] |= block; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // Base 10, we extract blocks of size 10^9 at a time, that way Chris@16: // the number of multiplications is kept to a minimum: Chris@16: limb_type block_mult = max_block_10; Chris@16: while(*s) Chris@16: { Chris@16: limb_type block = 0; Chris@16: for(unsigned i = 0; i < digits_per_block_10; ++i) Chris@16: { Chris@16: limb_type val; Chris@16: if(*s >= '0' && *s <= '9') Chris@16: val = *s - '0'; Chris@16: else Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input.")); Chris@16: block *= 10; Chris@16: block += val; Chris@16: if(!*++s) Chris@16: { Chris@16: block_mult = block_multiplier(i); Chris@16: break; Chris@16: } Chris@16: } Chris@16: eval_multiply(*this, block_mult); Chris@16: eval_add(*this, block); Chris@16: } Chris@16: } Chris@16: } Chris@16: if(isneg) Chris@16: this->negate(); Chris@16: } Chris@16: public: Chris@16: cpp_int_backend& operator = (const char* s) Chris@16: { Chris@16: do_assign_string(s, trivial_tag()); Chris@16: return *this; Chris@16: } Chris@16: BOOST_MP_FORCEINLINE void swap(cpp_int_backend& o) BOOST_NOEXCEPT Chris@16: { Chris@16: this->do_swap(o); Chris@16: } Chris@16: private: Chris@16: std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::false_&)const Chris@16: { Chris@101: typedef typename mpl::if_c::type io_type; Chris@16: if(this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct))) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported.")); Chris@16: std::stringstream ss; Chris@16: ss.flags(f & ~std::ios_base::showpos); Chris@16: ss << static_cast(*this->limbs()); Chris@16: std::string result; Chris@16: if(this->sign()) Chris@16: result += '-'; Chris@16: else if(f & std::ios_base::showpos) Chris@16: result += '+'; Chris@16: result += ss.str(); Chris@16: return result; Chris@16: } Chris@16: std::string do_get_trivial_string(std::ios_base::fmtflags f, const mpl::true_&)const Chris@16: { Chris@16: // Even though we have only one limb, we can't do IO on it :-( Chris@16: int base = 10; Chris@16: if((f & std::ios_base::oct) == std::ios_base::oct) Chris@16: base = 8; Chris@16: else if((f & std::ios_base::hex) == std::ios_base::hex) Chris@16: base = 16; Chris@16: std::string result; Chris@16: Chris@16: unsigned Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT; Chris@16: Chris@16: if(base == 8 || base == 16) Chris@16: { Chris@16: if(this->sign()) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported.")); Chris@16: limb_type shift = base == 8 ? 3 : 4; Chris@16: limb_type mask = static_cast((1u << shift) - 1); Chris@16: typename base_type::local_limb_type v = *this->limbs(); Chris@16: result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0'); Chris@16: std::string::difference_type pos = result.size() - 1; Chris@16: for(unsigned i = 0; i < Bits / shift; ++i) Chris@16: { Chris@16: char c = '0' + static_cast(v & mask); Chris@16: if(c > '9') Chris@16: c += 'A' - '9' - 1; Chris@16: result[pos--] = c; Chris@16: v >>= shift; Chris@16: } Chris@16: if(Bits % shift) Chris@16: { Chris@16: mask = static_cast((1u << (Bits % shift)) - 1); Chris@16: char c = '0' + static_cast(v & mask); Chris@16: if(c > '9') Chris@16: c += 'A' - '9'; Chris@16: result[pos] = c; Chris@16: } Chris@16: // Chris@16: // Get rid of leading zeros: Chris@16: // Chris@16: std::string::size_type n = result.find_first_not_of('0'); Chris@16: if(!result.empty() && (n == std::string::npos)) Chris@16: n = result.size() - 1; Chris@16: result.erase(0, n); Chris@16: if(f & std::ios_base::showbase) Chris@16: { Chris@16: const char* pp = base == 8 ? "0" : "0x"; Chris@101: result.insert(static_cast(0), pp); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: result.assign(Bits / 3 + 1, '0'); Chris@16: std::string::difference_type pos = result.size() - 1; Chris@16: typename base_type::local_limb_type v(*this->limbs()); Chris@16: bool neg = false; Chris@16: if(this->sign()) Chris@16: { Chris@16: neg = true; Chris@16: } Chris@16: while(v) Chris@16: { Chris@16: result[pos] = (v % 10) + '0'; Chris@16: --pos; Chris@16: v /= 10; Chris@16: } Chris@16: std::string::size_type n = result.find_first_not_of('0'); Chris@16: result.erase(0, n); Chris@16: if(result.empty()) Chris@16: result = "0"; Chris@16: if(neg) Chris@101: result.insert(static_cast(0), 1, '-'); Chris@16: else if(f & std::ios_base::showpos) Chris@101: result.insert(static_cast(0), 1, '+'); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: std::string do_get_string(std::ios_base::fmtflags f, const mpl::true_&)const Chris@16: { Chris@16: #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO Chris@16: return do_get_trivial_string(f, mpl::bool_::value>()); Chris@16: #else Chris@16: return do_get_trivial_string(f, mpl::bool_()); Chris@16: #endif Chris@16: } Chris@16: std::string do_get_string(std::ios_base::fmtflags f, const mpl::false_&)const Chris@16: { Chris@16: using default_ops::eval_get_sign; Chris@16: int base = 10; Chris@16: if((f & std::ios_base::oct) == std::ios_base::oct) Chris@16: base = 8; Chris@16: else if((f & std::ios_base::hex) == std::ios_base::hex) Chris@16: base = 16; Chris@16: std::string result; Chris@16: Chris@16: unsigned Bits = this->size() * base_type::limb_bits; Chris@16: Chris@16: if(base == 8 || base == 16) Chris@16: { Chris@16: if(this->sign()) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported.")); Chris@16: limb_type shift = base == 8 ? 3 : 4; Chris@16: limb_type mask = static_cast((1u << shift) - 1); Chris@16: cpp_int_backend t(*this); Chris@16: result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0'); Chris@16: std::string::difference_type pos = result.size() - 1; Chris@16: for(unsigned i = 0; i < Bits / shift; ++i) Chris@16: { Chris@16: char c = '0' + static_cast(t.limbs()[0] & mask); Chris@16: if(c > '9') Chris@16: c += 'A' - '9' - 1; Chris@16: result[pos--] = c; Chris@16: eval_right_shift(t, shift); Chris@16: } Chris@16: if(Bits % shift) Chris@16: { Chris@16: mask = static_cast((1u << (Bits % shift)) - 1); Chris@16: char c = '0' + static_cast(t.limbs()[0] & mask); Chris@16: if(c > '9') Chris@16: c += 'A' - '9'; Chris@16: result[pos] = c; Chris@16: } Chris@16: // Chris@16: // Get rid of leading zeros: Chris@16: // Chris@16: std::string::size_type n = result.find_first_not_of('0'); Chris@16: if(!result.empty() && (n == std::string::npos)) Chris@16: n = result.size() - 1; Chris@16: result.erase(0, n); Chris@16: if(f & std::ios_base::showbase) Chris@16: { Chris@16: const char* pp = base == 8 ? "0" : "0x"; Chris@101: result.insert(static_cast(0), pp); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: result.assign(Bits / 3 + 1, '0'); Chris@16: std::string::difference_type pos = result.size() - 1; Chris@16: cpp_int_backend t(*this); Chris@16: cpp_int_backend r; Chris@16: bool neg = false; Chris@16: if(t.sign()) Chris@16: { Chris@16: t.negate(); Chris@16: neg = true; Chris@16: } Chris@16: if(this->size() == 1) Chris@16: { Chris@16: result = boost::lexical_cast(t.limbs()[0]); Chris@16: } Chris@16: else Chris@16: { Chris@16: cpp_int_backend block10; Chris@16: block10 = max_block_10; Chris@16: while(eval_get_sign(t) != 0) Chris@16: { Chris@16: cpp_int_backend t2; Chris@16: divide_unsigned_helper(&t2, t, block10, r); Chris@16: t = t2; Chris@16: limb_type v = r.limbs()[0]; Chris@16: for(unsigned i = 0; i < digits_per_block_10; ++i) Chris@16: { Chris@16: char c = '0' + v % 10; Chris@16: v /= 10; Chris@16: result[pos] = c; Chris@16: if(pos-- == 0) Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: std::string::size_type n = result.find_first_not_of('0'); Chris@16: result.erase(0, n); Chris@16: if(result.empty()) Chris@16: result = "0"; Chris@16: if(neg) Chris@101: result.insert(static_cast(0), 1, '-'); Chris@16: else if(f & std::ios_base::showpos) Chris@101: result.insert(static_cast(0), 1, '+'); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: public: Chris@16: std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f)const Chris@16: { Chris@16: return do_get_string(f, trivial_tag()); Chris@16: } Chris@16: template Chris@16: int compare_imp(const cpp_int_backend& o, const mpl::false_&, const mpl::false_&)const BOOST_NOEXCEPT Chris@16: { Chris@16: if(this->sign() != o.sign()) Chris@16: return this->sign() ? -1 : 1; Chris@16: Chris@16: // Only do the compare if the same sign: Chris@16: int result = compare_unsigned(o); Chris@16: Chris@16: if(this->sign()) Chris@16: result = -result; Chris@16: return result; Chris@16: } Chris@16: template Chris@16: int compare_imp(const cpp_int_backend& o, const mpl::true_&, const mpl::false_&)const Chris@16: { Chris@16: cpp_int_backend t(*this); Chris@16: return t.compare(o); Chris@16: } Chris@16: template Chris@16: int compare_imp(const cpp_int_backend& o, const mpl::false_&, const mpl::true_&)const Chris@16: { Chris@16: cpp_int_backend t(o); Chris@16: return compare(t); Chris@16: } Chris@16: template Chris@16: int compare_imp(const cpp_int_backend& o, const mpl::true_&, const mpl::true_&)const BOOST_NOEXCEPT Chris@16: { Chris@16: if(this->sign()) Chris@16: { Chris@16: if(o.sign()) Chris@16: { Chris@16: return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0); Chris@16: } Chris@16: else Chris@16: return -1; Chris@16: } Chris@16: else Chris@16: { Chris@16: if(o.sign()) Chris@16: return 1; Chris@16: return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0); Chris@16: } Chris@16: } Chris@16: template Chris@16: int compare(const cpp_int_backend& o)const BOOST_NOEXCEPT Chris@16: { Chris@16: typedef mpl::bool_ >::value> t1; Chris@16: typedef mpl::bool_ >::value> t2; Chris@16: return compare_imp(o, t1(), t2()); Chris@16: } Chris@16: template Chris@16: int compare_unsigned(const cpp_int_backend& o)const BOOST_NOEXCEPT Chris@16: { Chris@16: if(this->size() != o.size()) Chris@16: { Chris@16: return this->size() > o.size() ? 1 : -1; Chris@16: } Chris@16: typename base_type::const_limb_pointer pa = this->limbs(); Chris@16: typename base_type::const_limb_pointer pb = o.limbs(); Chris@16: for(int i = this->size() - 1; i >= 0; --i) Chris@16: { Chris@16: if(pa[i] != pb[i]) Chris@16: return pa[i] > pb[i] ? 1 : -1; Chris@16: } Chris@16: return 0; Chris@16: } Chris@16: template Chris@16: BOOST_MP_FORCEINLINE typename boost::enable_if, int>::type compare(Arithmetic i)const Chris@16: { Chris@16: // braindead version: Chris@16: cpp_int_backend t; Chris@16: t = i; Chris@16: return compare(t); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace backends Chris@16: Chris@16: namespace default_ops{ Chris@16: Chris@16: template Chris@16: struct double_precision_type; Chris@16: Chris@16: template Chris@16: struct double_precision_type > Chris@16: { Chris@16: typedef typename mpl::if_c< Chris@16: backends::is_fixed_precision >::value, Chris@16: backends::cpp_int_backend< Chris@16: (is_void::value ? Chris@16: 2 * backends::max_precision >::value Chris@16: : MinBits), Chris@16: 2 * backends::max_precision >::value, Chris@16: SignType, Chris@16: Checked, Chris@16: Allocator>, Chris@16: backends::cpp_int_backend Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: Chris@16: } Chris@16: Chris@16: template Chris@16: struct expression_template_default > Chris@16: { Chris@16: static const expression_template_option value = et_off; Chris@16: }; Chris@16: Chris@16: using boost::multiprecision::backends::cpp_int_backend; Chris@16: Chris@16: template Chris@16: struct number_category > : public mpl::int_{}; Chris@16: Chris@16: typedef number > cpp_int; Chris@16: typedef rational_adaptor > cpp_rational_backend; Chris@16: typedef number cpp_rational; Chris@16: Chris@16: // Fixed precision unsigned types: Chris@16: typedef number > uint128_t; Chris@16: typedef number > uint256_t; Chris@16: typedef number > uint512_t; Chris@16: typedef number > uint1024_t; Chris@16: Chris@16: // Fixed precision signed types: Chris@16: typedef number > int128_t; Chris@16: typedef number > int256_t; Chris@16: typedef number > int512_t; Chris@16: typedef number > int1024_t; Chris@16: Chris@16: // Over again, but with checking enabled this time: Chris@101: typedef number > checked_cpp_int; Chris@101: typedef rational_adaptor > checked_cpp_rational_backend; Chris@101: typedef number checked_cpp_rational; Chris@16: // Fixed precision unsigned types: Chris@16: typedef number > checked_uint128_t; Chris@16: typedef number > checked_uint256_t; Chris@16: typedef number > checked_uint512_t; Chris@16: typedef number > checked_uint1024_t; Chris@16: Chris@16: // Fixed precision signed types: Chris@16: typedef number > checked_int128_t; Chris@16: typedef number > checked_int256_t; Chris@16: typedef number > checked_int512_t; Chris@16: typedef number > checked_int1024_t; Chris@16: Chris@16: #ifdef BOOST_NO_SFINAE_EXPR Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: template Chris@16: struct is_explicitly_convertible, cpp_int_backend > : public mpl::true_ {}; Chris@16: Chris@16: } Chris@16: #endif Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: // Chris@16: // Last of all we include the implementations of all the eval_* non member functions: Chris@16: // Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #ifdef BOOST_MP_USER_DEFINED_LITERALS Chris@16: #include Chris@16: #endif Chris@16: #include Chris@16: Chris@16: #endif