cpp_int_config.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 - 2021 John Maddock.
  3. // Copyright 2021 Matt Borland.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_MP_CPP_INT_CONFIG_HPP
  7. #define BOOST_MP_CPP_INT_CONFIG_HPP
  8. #include <cstdint>
  9. #include <type_traits>
  10. #include <limits>
  11. #include <boost/multiprecision/detail/standalone_config.hpp>
  12. #include <boost/multiprecision/detail/assert.hpp>
  13. namespace boost {
  14. namespace multiprecision {
  15. namespace detail {
  16. //
  17. // These traits calculate the largest type in the list
  18. // [unsigned] long long, long, int, which has the specified number
  19. // of bits. Note that int_t and uint_t find the first
  20. // member of the above list, not the last. We want the last in the
  21. // list to ensure that mixed arithmetic operations are as efficient
  22. // as possible.
  23. //
  24. template <std::size_t Bits>
  25. struct int_t
  26. {
  27. using exact = typename std::conditional<Bits <= sizeof(signed char) * CHAR_BIT, signed char,
  28. typename std::conditional<Bits <= sizeof(short) * CHAR_BIT, short,
  29. typename std::conditional<Bits <= sizeof(int) * CHAR_BIT, int,
  30. typename std::conditional<Bits <= sizeof(long) * CHAR_BIT, long,
  31. typename std::conditional<Bits <= sizeof(long long) * CHAR_BIT, long long, void
  32. >::type>::type>::type>::type>::type;
  33. using least = typename std::conditional<Bits-1 <= std::numeric_limits<signed char>::digits, signed char,
  34. typename std::conditional<Bits-1 <= std::numeric_limits<short>::digits, short,
  35. typename std::conditional<Bits-1 <= std::numeric_limits<int>::digits, int,
  36. typename std::conditional<Bits-1 <= std::numeric_limits<long>::digits, long,
  37. typename std::conditional<Bits-1 <= std::numeric_limits<long long>::digits, long long, void
  38. >::type>::type>::type>::type>::type;
  39. static_assert(!std::is_same<void, exact>::value && !std::is_same<void, least>::value, "Number of bits does not match any standard data type. \
  40. Please file an issue at https://github.com/boostorg/multiprecision/ referencing this error from cpp_int_config.hpp");
  41. };
  42. template <std::size_t Bits>
  43. struct uint_t
  44. {
  45. using exact = typename std::conditional<Bits <= sizeof(unsigned char) * CHAR_BIT, unsigned char,
  46. typename std::conditional<Bits <= sizeof(unsigned short) * CHAR_BIT, unsigned short,
  47. typename std::conditional<Bits <= sizeof(unsigned int) * CHAR_BIT, unsigned int,
  48. typename std::conditional<Bits <= sizeof(unsigned long) * CHAR_BIT, unsigned long,
  49. typename std::conditional<Bits <= sizeof(unsigned long long) * CHAR_BIT, unsigned long long, void
  50. >::type>::type>::type>::type>::type;
  51. using least = typename std::conditional<Bits <= std::numeric_limits<unsigned char>::digits, unsigned char,
  52. typename std::conditional<Bits <= std::numeric_limits<unsigned short>::digits, unsigned short,
  53. typename std::conditional<Bits <= std::numeric_limits<unsigned int>::digits, unsigned int,
  54. typename std::conditional<Bits <= std::numeric_limits<unsigned long>::digits, unsigned long,
  55. typename std::conditional<Bits <= std::numeric_limits<unsigned long long>::digits, unsigned long long, void
  56. >::type>::type>::type>::type>::type;
  57. static_assert(!std::is_same<void, exact>::value && !std::is_same<void, least>::value, "Number of bits does not match any standard data type. \
  58. Please file an issue at https://github.com/boostorg/multiprecision/ referencing this error from cpp_int_config.hpp");
  59. };
  60. template <std::size_t N>
  61. struct largest_signed_type
  62. {
  63. using type = typename std::conditional<
  64. 1 + std::numeric_limits<long long>::digits == N,
  65. long long,
  66. typename std::conditional<
  67. 1 + std::numeric_limits<long>::digits == N,
  68. long,
  69. typename std::conditional<
  70. 1 + std::numeric_limits<int>::digits == N,
  71. int,
  72. typename int_t<N>::exact>::type>::type>::type;
  73. };
  74. template <std::size_t N>
  75. struct largest_unsigned_type
  76. {
  77. using type = typename std::conditional<
  78. std::numeric_limits<unsigned long long>::digits == N,
  79. unsigned long long,
  80. typename std::conditional<
  81. std::numeric_limits<unsigned long>::digits == N,
  82. unsigned long,
  83. typename std::conditional<
  84. std::numeric_limits<unsigned int>::digits == N,
  85. unsigned int,
  86. typename uint_t<N>::exact>::type>::type>::type;
  87. };
  88. } // namespace detail
  89. #if defined(BOOST_HAS_INT128)
  90. using limb_type = detail::largest_unsigned_type<64>::type;
  91. using signed_limb_type = detail::largest_signed_type<64>::type;
  92. using double_limb_type = boost::multiprecision::uint128_type;
  93. using signed_double_limb_type = boost::multiprecision::int128_type;
  94. constexpr limb_type max_block_10 = 1000000000000000000uLL;
  95. constexpr limb_type digits_per_block_10 = 18;
  96. inline BOOST_MP_CXX14_CONSTEXPR limb_type block_multiplier(std::size_t count)
  97. {
  98. constexpr 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};
  99. BOOST_MP_ASSERT(count < digits_per_block_10);
  100. return values[count];
  101. }
  102. // Can't do formatted IO on an __int128
  103. #define BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
  104. #else
  105. using limb_type = detail::largest_unsigned_type<32>::type;
  106. using signed_limb_type = detail::largest_signed_type<32>::type ;
  107. using double_limb_type = detail::largest_unsigned_type<64>::type;
  108. using signed_double_limb_type = detail::largest_signed_type<64>::type ;
  109. constexpr limb_type max_block_10 = 1000000000;
  110. constexpr limb_type digits_per_block_10 = 9;
  111. inline limb_type block_multiplier(std::size_t count)
  112. {
  113. constexpr limb_type values[digits_per_block_10] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  114. BOOST_MP_ASSERT(count < digits_per_block_10);
  115. return values[count];
  116. }
  117. #endif
  118. constexpr std::size_t bits_per_limb = sizeof(limb_type) * CHAR_BIT;
  119. template <class T>
  120. inline BOOST_MP_CXX14_CONSTEXPR void minmax(const T& a, const T& b, T& aa, T& bb)
  121. {
  122. if (a < b)
  123. {
  124. aa = a;
  125. bb = b;
  126. }
  127. else
  128. {
  129. aa = b;
  130. bb = a;
  131. }
  132. }
  133. } // namespace multiprecision
  134. } // namespace boost
  135. #endif // BOOST_MP_CPP_INT_CONFIG_HPP