]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/safe_numerics/checked_float.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / safe_numerics / checked_float.hpp
1 #ifndef BOOST_NUMERIC_CHECKED_FLOAT_HPP
2 #define BOOST_NUMERIC_CHECKED_FLOAT_HPP
3
4 // Copyright (c) 2017 Robert Ramey
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 // contains operation implementation of arithmetic operators
11 // on built-in floating point types. The default implementation is to just
12 // invoke the operation with no checking. These are overloaded
13 // for specific types such as integer, etc.
14
15 #include <type_traits> // std::is_floating_point, make_unsigned
16
17 namespace boost {
18 namespace safe_numerics {
19 namespace checked {
20
21 ////////////////////////////////////////////////////
22 // layer 0 - implement safe operations for floating
23
24 template<
25 typename R,
26 R Min,
27 R Max,
28 typename T,
29 class F
30 >
31 struct heterogeneous_checked_operation<
32 R,
33 Min,
34 Max,
35 T,
36 F,
37 typename std::enable_if<
38 std::is_floating_point<R>::value
39 && std::is_floating_point<T>::value
40 >::type
41 >{
42 constexpr static checked_result<R>
43 cast(const T & t) noexcept {
44 return t;
45 };
46 }; // checked_unary_operation
47
48 template<
49 typename R,
50 R Min,
51 R Max,
52 typename T,
53 class
54 F
55 >
56 struct heterogeneous_checked_operation<
57 R,
58 Min,
59 Max,
60 T,
61 F,
62 typename std::enable_if<
63 std::is_floating_point<R>::value
64 && std::is_integralt<T>::value
65 >::type
66 >{
67 constexpr static checked_result<R>
68 cast(const T & t) noexcept {
69 return t;
70 };
71 }; // checked_unary_operation
72
73 template<typename R, typename T, typename U>
74 struct checked_operation<R, T, U, F,
75 typename std::enable_if<
76 std::is_floating_point<R>::value
77 >::type
78 >{
79 constexpr static checked_result<R> cast(const T & t) {
80 return
81 cast_impl_detail::cast_impl(
82 t,
83 std::is_signed<R>(),
84 std::is_signed<T>()
85 );
86 }
87 constexpr static checked_result<R> add(const T & t, const U & u) {
88 return t + u;
89 }
90
91 constexpr static checked_result<R> subtract(
92 const T & t,
93 const U & u
94 ) {
95 return t - u;
96 }
97
98 constexpr static checked_result<R> multiply(
99 const T & t,
100 const U & u
101 ) noexcept {
102 return t * u;
103 }
104
105 constexpr static checked_result<R> divide(
106 const T & t,
107 const U & u
108 ) noexcept {
109 return t / u;
110 }
111
112 constexpr static checked_result<R> modulus(
113 const T & t,
114 const U & u
115 ) noexcept {
116 return t % u;
117 }
118
119 constexpr static bool less_than(const T & t, const U & u) noexcept {
120 return t < u;
121 }
122
123 constexpr static bool greater_than(const T & t, const U & u) noexcept {
124 return t > u;
125 }
126
127 constexpr static bool equal(const T & t, const U & u) noexcept {
128 return t < u;
129 }
130
131 }; // checked_binary_operation
132
133 template<class R, class T, class U>
134 typename std::enable_if<
135 std::is_floating_point<R>::value
136 && std::is_floating_point<T>::value
137 && std::is_floating_point<U>::value,
138 checked_result<R>
139 >::type
140 constexpr inline bool less_than(const T & t, const U & u) noexcept {
141 return t < u;
142 }
143
144 template<class R, class T, class U>
145 typename std::enable_if<
146 std::is_floating_point<R>::value
147 && std::is_floating_point<T>::value
148 && std::is_floating_point<U>::value,
149 checked_result<R>
150 >::type
151 constexpr inline bool equal(const T & t, const U & u) noexcept {
152 return t < u;
153 }
154
155 template<class R, class T, class U>
156 typename std::enable_if<
157 std::is_floating_point<R>::value
158 && std::is_floating_point<T>::value
159 && std::is_floating_point<U>::value,
160 checked_result<R>
161 >::type
162 constexpr inline checked_result<R> left_shift(const T & t, const U & u) noexcept {
163 return t << u;
164 }
165
166 template<class R, class T, class U>
167 typename std::enable_if<
168 std::is_floating_point<R>::value
169 && std::is_floating_point<T>::value
170 && std::is_floating_point<U>::value,
171 checked_result<R>
172 >::type
173 constexpr inline checked_result<R> right_shift(const T & t, const U & u) noexcept {
174 return t >> u;
175 }
176
177 template<class R, class T, class U>
178 typename std::enable_if<
179 std::is_floating_point<R>::value
180 && std::is_floating_point<T>::value
181 && std::is_floating_point<U>::value,
182 checked_result<R>
183 >::type
184 constexpr inline checked_result<R> bitwise_or(const T & t, const U & u) noexcept {
185 return t | u;
186 }
187
188 template<class R, class T, class U>
189 typename std::enable_if<
190 std::is_floating_point<R>::value
191 && std::is_floating_point<T>::value
192 && std::is_floating_point<U>::value,
193 checked_result<R>
194 >::type
195 constexpr inline checked_result<R> bitwise_xor(const T & t, const U & u) noexcept {
196 return t ^ u;
197 }
198
199 template<class R, class T, class U>
200 typename std::enable_if<
201 std::is_floating_point<R>::value
202 && std::is_floating_point<T>::value
203 && std::is_floating_point<U>::value,
204 checked_result<R>
205 >::type
206 constexpr inline checked_result<R> bitwise_and(const T & t, const U & u) noexcept {
207 return t & u;
208 }
209
210 } // checked
211 } // safe_numerics
212 } // boost
213
214 #endif // BOOST_NUMERIC_CHECKED_DEFAULT_HPP
215