]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/include/boost/multiprecision/cpp_int/checked.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / multiprecision / include / boost / multiprecision / cpp_int / checked.hpp
CommitLineData
7c673cae
FG
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 http://www.boost.org/LICENSE_1_
5
6#ifndef BOOST_MP_CPP_INT_CHECKED_HPP
7#define BOOST_MP_CPP_INT_CHECKED_HPP
8
9namespace 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
16inline void raise_overflow(std::string op)
17{
18 BOOST_THROW_EXCEPTION(std::overflow_error("overflow in " + op));
19}
20inline void raise_add_overflow()
21{
22 raise_overflow("addition");
23}
24inline void raise_subtract_overflow()
25{
26 BOOST_THROW_EXCEPTION(std::range_error("Subtraction resulted in a negative value, but the type is unsigned"));
27}
28inline void raise_mul_overflow()
29{
30 raise_overflow("multiplication");
31}
32inline void raise_div_overflow()
33{
34 raise_overflow("division");
35}
36
37template <class A>
38inline 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}
52template <class A>
53inline 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}
59template <class A>
60inline A checked_add(A a, A b, const mpl::int_<checked>&)
61{
62 return checked_add_imp(a, b, boost::is_signed<A>());
63}
64template <class A>
65inline A checked_add(A a, A b, const mpl::int_<unchecked>&)
66{
67 return a + b;
68}
69
70template <class A>
71inline 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}
85template <class A>
86inline 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}
92template <class A>
93inline A checked_subtract(A a, A b, const mpl::int_<checked>&)
94{
95 return checked_subtract_imp(a, b, boost::is_signed<A>());
96}
97template <class A>
98inline A checked_subtract(A a, A b, const mpl::int_<unchecked>&)
99{
100 return a - b;
101}
102
103template <class A>
104inline 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}
111template <class A>
112inline A checked_multiply(A a, A b, const mpl::int_<unchecked>&)
113{
114 return a * b;
115}
116
117template <class A>
118inline 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}
124template <class A>
125inline A checked_divide(A a, A b, const mpl::int_<unchecked>&)
126{
127 return a / b;
128}
129
130template <class A>
131inline 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}
140template <class A>
141inline 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}}}} // namespaces
147
148#endif
149