checked.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2012 John Maddock. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying file
  3. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  4. #ifndef BOOST_MP_CPP_INT_CHECKED_HPP
  5. #define BOOST_MP_CPP_INT_CHECKED_HPP
  6. #include <climits>
  7. #include <limits>
  8. #include <type_traits>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <boost/multiprecision/detail/standalone_config.hpp>
  12. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  13. namespace boost { namespace multiprecision { namespace backends { namespace detail {
  14. //
  15. // Simple routines for performing checked arithmetic with a builtin arithmetic type.
  16. // Note that this is not a complete header, it must be included as part of boost/multiprecision/cpp_int.hpp.
  17. //
  18. template <typename T>
  19. inline constexpr T type_max() noexcept
  20. {
  21. return
  22. #ifdef BOOST_HAS_INT128
  23. std::is_same<T, boost::multiprecision::int128_type>::value ? INT128_MAX :
  24. std::is_same<T, boost::multiprecision::uint128_type>::value ? UINT128_MAX :
  25. #endif
  26. (std::numeric_limits<T>::max)();
  27. }
  28. template <typename T>
  29. inline constexpr T type_min() noexcept
  30. {
  31. return
  32. #ifdef BOOST_HAS_INT128
  33. std::is_same<T, boost::multiprecision::int128_type>::value ? INT128_MIN :
  34. std::is_same<T, boost::multiprecision::uint128_type>::value ? T(0) :
  35. #endif
  36. (std::numeric_limits<T>::min)();
  37. }
  38. inline void raise_overflow(std::string op)
  39. {
  40. BOOST_MP_THROW_EXCEPTION(std::overflow_error("overflow in " + op));
  41. }
  42. inline void raise_add_overflow()
  43. {
  44. raise_overflow("addition");
  45. }
  46. inline void raise_subtract_overflow()
  47. {
  48. BOOST_MP_THROW_EXCEPTION(std::range_error("Subtraction resulted in a negative value, but the type is unsigned"));
  49. }
  50. inline void raise_mul_overflow()
  51. {
  52. raise_overflow("multiplication");
  53. }
  54. inline void raise_div_overflow()
  55. {
  56. raise_overflow("division");
  57. }
  58. template <class A>
  59. inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, true>&)
  60. {
  61. if (a > 0)
  62. {
  63. if ((b > 0) && ((type_max<A>() - b) < a))
  64. raise_add_overflow();
  65. }
  66. else
  67. {
  68. if ((b < 0) && ((type_min<A>() - b) > a))
  69. raise_add_overflow();
  70. }
  71. return a + b;
  72. }
  73. template <class A>
  74. inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, false>&)
  75. {
  76. if ((type_max<A>() - b) < a)
  77. raise_add_overflow();
  78. return a + b;
  79. }
  80. template <class A>
  81. inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, checked>&)
  82. {
  83. return checked_add_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value > ());
  84. }
  85. template <class A>
  86. inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, unchecked>&)
  87. {
  88. return a + b;
  89. }
  90. template <class A>
  91. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, true>&)
  92. {
  93. if (a > 0)
  94. {
  95. if ((b < 0) && ((type_max<A>() + b) < a))
  96. raise_subtract_overflow();
  97. }
  98. else
  99. {
  100. if ((b > 0) && ((type_min<A>() + b) > a))
  101. raise_subtract_overflow();
  102. }
  103. return a - b;
  104. }
  105. template <class A>
  106. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, false>&)
  107. {
  108. if (a < b)
  109. raise_subtract_overflow();
  110. return a - b;
  111. }
  112. template <class A>
  113. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, checked>&)
  114. {
  115. return checked_subtract_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value>());
  116. }
  117. template <class A>
  118. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, unchecked>&)
  119. {
  120. return a - b;
  121. }
  122. template <class A>
  123. inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, checked>&)
  124. {
  125. BOOST_MP_USING_ABS
  126. if (a && (type_max<A>() / abs(a) < abs(b)))
  127. raise_mul_overflow();
  128. return a * b;
  129. }
  130. template <class A>
  131. inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, unchecked>&)
  132. {
  133. return a * b;
  134. }
  135. template <class A>
  136. inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, checked>&)
  137. {
  138. if (b == 0)
  139. raise_div_overflow();
  140. return a / b;
  141. }
  142. template <class A>
  143. inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, unchecked>&)
  144. {
  145. return a / b;
  146. }
  147. template <class A>
  148. inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, unsigned long long shift, const std::integral_constant<int, checked>&)
  149. {
  150. if (a && shift)
  151. {
  152. if ((shift > sizeof(A) * CHAR_BIT) || (a >> (sizeof(A) * CHAR_BIT - shift)))
  153. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Shift out of range"));
  154. }
  155. return a << shift;
  156. }
  157. template <class A>
  158. inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, unsigned long long shift, const std::integral_constant<int, unchecked>&)
  159. {
  160. return (shift >= sizeof(A) * CHAR_BIT) ? 0 : a << shift;
  161. }
  162. }}}} // namespace boost::multiprecision::backends::detail
  163. #endif