checked.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. namespace boost { namespace multiprecision { namespace backends { namespace detail {
  7. //
  8. // Simple routines for performing checked arithmetic with a builtin arithmetic type.
  9. // Note that this is not a complete header, it must be included as part of boost/multiprecision/cpp_int.hpp.
  10. //
  11. inline void raise_overflow(std::string op)
  12. {
  13. BOOST_THROW_EXCEPTION(std::overflow_error("overflow in " + op));
  14. }
  15. inline void raise_add_overflow()
  16. {
  17. raise_overflow("addition");
  18. }
  19. inline void raise_subtract_overflow()
  20. {
  21. BOOST_THROW_EXCEPTION(std::range_error("Subtraction resulted in a negative value, but the type is unsigned"));
  22. }
  23. inline void raise_mul_overflow()
  24. {
  25. raise_overflow("multiplication");
  26. }
  27. inline void raise_div_overflow()
  28. {
  29. raise_overflow("division");
  30. }
  31. template <class A>
  32. inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, true>&)
  33. {
  34. if (a > 0)
  35. {
  36. if ((b > 0) && ((integer_traits<A>::const_max - b) < a))
  37. raise_add_overflow();
  38. }
  39. else
  40. {
  41. if ((b < 0) && ((integer_traits<A>::const_min - b) > a))
  42. raise_add_overflow();
  43. }
  44. return a + b;
  45. }
  46. template <class A>
  47. inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const std::integral_constant<bool, false>&)
  48. {
  49. if ((integer_traits<A>::const_max - b) < a)
  50. raise_add_overflow();
  51. return a + b;
  52. }
  53. template <class A>
  54. inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, checked>&)
  55. {
  56. return checked_add_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value > ());
  57. }
  58. template <class A>
  59. inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const std::integral_constant<int, unchecked>&)
  60. {
  61. return a + b;
  62. }
  63. template <class A>
  64. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, true>&)
  65. {
  66. if (a > 0)
  67. {
  68. if ((b < 0) && ((integer_traits<A>::const_max + b) < a))
  69. raise_subtract_overflow();
  70. }
  71. else
  72. {
  73. if ((b > 0) && ((integer_traits<A>::const_min + b) > a))
  74. raise_subtract_overflow();
  75. }
  76. return a - b;
  77. }
  78. template <class A>
  79. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const std::integral_constant<bool, false>&)
  80. {
  81. if (a < b)
  82. raise_subtract_overflow();
  83. return a - b;
  84. }
  85. template <class A>
  86. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, checked>&)
  87. {
  88. return checked_subtract_imp(a, b, std::integral_constant<bool, boost::multiprecision::detail::is_signed<A>::value && boost::multiprecision::detail::is_integral<A>::value>());
  89. }
  90. template <class A>
  91. inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const std::integral_constant<int, unchecked>&)
  92. {
  93. return a - b;
  94. }
  95. template <class A>
  96. inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, checked>&)
  97. {
  98. BOOST_MP_USING_ABS
  99. if (a && (integer_traits<A>::const_max / abs(a) < abs(b)))
  100. raise_mul_overflow();
  101. return a * b;
  102. }
  103. template <class A>
  104. inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const std::integral_constant<int, unchecked>&)
  105. {
  106. return a * b;
  107. }
  108. template <class A>
  109. inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, checked>&)
  110. {
  111. if (b == 0)
  112. raise_div_overflow();
  113. return a / b;
  114. }
  115. template <class A>
  116. inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const std::integral_constant<int, unchecked>&)
  117. {
  118. return a / b;
  119. }
  120. template <class A>
  121. inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, boost::ulong_long_type shift, const std::integral_constant<int, checked>&)
  122. {
  123. if (a && shift)
  124. {
  125. if ((shift > sizeof(A) * CHAR_BIT) || (a >> (sizeof(A) * CHAR_BIT - shift)))
  126. BOOST_THROW_EXCEPTION(std::overflow_error("Shift out of range"));
  127. }
  128. return a << shift;
  129. }
  130. template <class A>
  131. inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, boost::ulong_long_type shift, const std::integral_constant<int, unchecked>&)
  132. {
  133. return (shift >= sizeof(A) * CHAR_BIT) ? 0 : a << shift;
  134. }
  135. }}}} // namespace boost::multiprecision::backends::detail
  136. #endif