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