1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6 #ifndef BOOST_MP_CPP_INT_CORE_HPP
7 #define BOOST_MP_CPP_INT_CORE_HPP
9 #include <boost/integer.hpp>
10 #include <boost/integer_traits.hpp>
11 #include <boost/mpl/if.hpp>
12 #include <boost/mpl/int.hpp>
13 #include <boost/static_assert.hpp>
14 #include <boost/assert.hpp>
17 namespace multiprecision {
22 // These traits calculate the largest type in the list
23 // [unsigned] boost::long_long_type, long, int, which has the specified number
24 // of bits. Note that intN_t and boost::int_t<N> find the first
25 // member of the above list, not the last. We want the last in the
26 // list to ensure that mixed arithmetic operations are as efficient
30 struct largest_signed_type
32 typedef typename mpl::if_c<
33 1 + std::numeric_limits<boost::long_long_type>::digits == N,
34 boost::long_long_type,
36 1 + std::numeric_limits<long>::digits == N,
39 1 + std::numeric_limits<int>::digits == N,
41 typename boost::int_t<N>::exact>::type>::type>::type type;
45 struct largest_unsigned_type
47 typedef typename mpl::if_c<
48 std::numeric_limits<boost::ulong_long_type>::digits == N,
49 boost::ulong_long_type,
51 std::numeric_limits<unsigned long>::digits == N,
54 std::numeric_limits<unsigned int>::digits == N,
56 typename boost::uint_t<N>::exact>::type>::type>::type type;
61 #if defined(BOOST_HAS_INT128)
63 typedef detail::largest_unsigned_type<64>::type limb_type;
64 typedef detail::largest_signed_type<64>::type signed_limb_type;
65 typedef boost::uint128_type double_limb_type;
66 typedef boost::int128_type signed_double_limb_type;
67 static const limb_type max_block_10 = 1000000000000000000uLL;
68 static const limb_type digits_per_block_10 = 18;
70 inline BOOST_MP_CXX14_CONSTEXPR limb_type block_multiplier(unsigned count)
72 #ifdef BOOST_NO_CXX14_CONSTEXPR
77 const limb_type values[digits_per_block_10] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000};
78 BOOST_ASSERT(count < digits_per_block_10);
82 // Can't do formatted IO on an __int128
83 #define BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
85 // Need to specialise integer_traits for __int128 as it's not a normal native type:
86 } // namespace multiprecision
89 class integer_traits<multiprecision::double_limb_type>
90 : public std::numeric_limits<multiprecision::double_limb_type>,
91 public detail::integer_traits_base<multiprecision::double_limb_type, 0, ~static_cast<multiprecision::double_limb_type>(0)>
94 class integer_traits<multiprecision::signed_double_limb_type>
95 : public std::numeric_limits<multiprecision::signed_double_limb_type>,
96 public detail::integer_traits_base<multiprecision::signed_double_limb_type, static_cast<multiprecision::signed_double_limb_type>((static_cast<multiprecision::double_limb_type>(1) << 127)), static_cast<multiprecision::signed_double_limb_type>(((~static_cast<multiprecision::double_limb_type>(0)) >> 1))>
99 namespace multiprecision {
103 typedef detail::largest_unsigned_type<32>::type limb_type;
104 typedef detail::largest_signed_type<32>::type signed_limb_type;
105 typedef detail::largest_unsigned_type<64>::type double_limb_type;
106 typedef detail::largest_signed_type<64>::type signed_double_limb_type;
107 static const limb_type max_block_10 = 1000000000;
108 static const limb_type digits_per_block_10 = 9;
110 inline limb_type block_multiplier(unsigned count)
112 #ifdef BOOST_NO_CXX14_CONSTEXPR
117 const limb_type values[digits_per_block_10] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
118 BOOST_ASSERT(count < digits_per_block_10);
119 return values[count];
124 static const unsigned bits_per_limb = sizeof(limb_type) * CHAR_BIT;
127 inline BOOST_MP_CXX14_CONSTEXPR void minmax(const T& a, const T& b, T& aa, T& bb)
141 enum cpp_integer_type
143 signed_magnitude = 1,
144 unsigned_magnitude = 0,
149 enum cpp_int_check_type
155 } // namespace multiprecision
159 // Figure out whether to support user-defined-literals or not:
161 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_USER_DEFINED_LITERALS) && !defined(BOOST_NO_CXX11_CONSTEXPR)
162 #define BOOST_MP_USER_DEFINED_LITERALS
165 #endif // BOOST_MP_CPP_INT_CORE_HPP