Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / multiprecision / cpp_int / checked.hpp
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
5
6 #ifndef BOOST_MP_CPP_INT_CHECKED_HPP
7 #define BOOST_MP_CPP_INT_CHECKED_HPP
8
9 namespace boost { namespace multiprecision { namespace backends { namespace detail {
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>
38 inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const mpl::true_&)
39 {
40    if (a > 0)
41    {
42       if ((b > 0) && ((integer_traits<A>::const_max - b) < a))
43          raise_add_overflow();
44    }
45    else
46    {
47       if ((b < 0) && ((integer_traits<A>::const_min - b) > a))
48          raise_add_overflow();
49    }
50    return a + b;
51 }
52 template <class A>
53 inline BOOST_MP_CXX14_CONSTEXPR A checked_add_imp(A a, A b, const mpl::false_&)
54 {
55    if ((integer_traits<A>::const_max - b) < a)
56       raise_add_overflow();
57    return a + b;
58 }
59 template <class A>
60 inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const mpl::int_<checked>&)
61 {
62    return checked_add_imp(a, b, mpl::bool_<boost::is_signed<A>::value>());
63 }
64 template <class A>
65 inline BOOST_MP_CXX14_CONSTEXPR A checked_add(A a, A b, const mpl::int_<unchecked>&)
66 {
67    return a + b;
68 }
69
70 template <class A>
71 inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const mpl::true_&)
72 {
73    if (a > 0)
74    {
75       if ((b < 0) && ((integer_traits<A>::const_max + b) < a))
76          raise_subtract_overflow();
77    }
78    else
79    {
80       if ((b > 0) && ((integer_traits<A>::const_min + b) > a))
81          raise_subtract_overflow();
82    }
83    return a - b;
84 }
85 template <class A>
86 inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract_imp(A a, A b, const mpl::false_&)
87 {
88    if (a < b)
89       raise_subtract_overflow();
90    return a - b;
91 }
92 template <class A>
93 inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const mpl::int_<checked>&)
94 {
95    return checked_subtract_imp(a, b, mpl::bool_<boost::is_signed<A>::value>());
96 }
97 template <class A>
98 inline BOOST_MP_CXX14_CONSTEXPR A checked_subtract(A a, A b, const mpl::int_<unchecked>&)
99 {
100    return a - b;
101 }
102
103 template <class A>
104 inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const mpl::int_<checked>&)
105 {
106    BOOST_MP_USING_ABS
107    if (a && (integer_traits<A>::const_max / abs(a) < abs(b)))
108       raise_mul_overflow();
109    return a * b;
110 }
111 template <class A>
112 inline BOOST_MP_CXX14_CONSTEXPR A checked_multiply(A a, A b, const mpl::int_<unchecked>&)
113 {
114    return a * b;
115 }
116
117 template <class A>
118 inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const mpl::int_<checked>&)
119 {
120    if (b == 0)
121       raise_div_overflow();
122    return a / b;
123 }
124 template <class A>
125 inline BOOST_MP_CXX14_CONSTEXPR A checked_divide(A a, A b, const mpl::int_<unchecked>&)
126 {
127    return a / b;
128 }
129
130 template <class A>
131 inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, boost::ulong_long_type shift, const mpl::int_<checked>&)
132 {
133    if (a && shift)
134    {
135       if ((shift > sizeof(A) * CHAR_BIT) || (a >> (sizeof(A) * CHAR_BIT - shift)))
136          BOOST_THROW_EXCEPTION(std::overflow_error("Shift out of range"));
137    }
138    return a << shift;
139 }
140 template <class A>
141 inline BOOST_MP_CXX14_CONSTEXPR A checked_left_shift(A a, boost::ulong_long_type shift, const mpl::int_<unchecked>&)
142 {
143    return (shift >= sizeof(A) * CHAR_BIT) ? 0 : a << shift;
144 }
145
146 }}}} // namespace boost::multiprecision::backends::detail
147
148 #endif