Debian Codegraph @

podofo/3rdparty/chromium/numerics/clamped_math_impl.h

raw
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_NUMERICS_CLAMPED_MATH_IMPL_H_
6 #define BASE_NUMERICS_CLAMPED_MATH_IMPL_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <climits>
12 #include <cmath>
13 #include <cstdlib>
14 #include <limits>
15 #include <type_traits>
16
17 #include "checked_math.h"
18 #include "safe_conversions.h"
19 #include "safe_math_shared_impl.h"
20
21 namespace chromium {
22 namespace base {
23 namespace internal {
24
25 template <typename T,
26 typename std::enable_if<std::is_integral<T>::value &&
27 std::is_signed<T>::value>::type* = nullptr>
28 constexpr T SaturatedNegWrapper(T value) {
29 return IsConstantEvaluated() || !ClampedNegFastOp<T>::is_supported
30 ? (NegateWrapper(value) != std::numeric_limits<T>::lowest()
31 ? NegateWrapper(value)
32 : std::numeric_limits<T>::max())
33 : ClampedNegFastOp<T>::Do(value);
34 }
35
36 template <typename T,
37 typename std::enable_if<std::is_integral<T>::value &&
38 !std::is_signed<T>::value>::type* = nullptr>
39 constexpr T SaturatedNegWrapper(T value) {
40 (void)value;
41 return T(0);
42 }
43
44 template <
45 typename T,
46 typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
47 constexpr T SaturatedNegWrapper(T value) {
48 return -value;
49 }
50
51 template <typename T,
52 typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
53 constexpr T SaturatedAbsWrapper(T value) {
54 // The calculation below is a static identity for unsigned types, but for
55 // signed integer types it provides a non-branching, saturated absolute value.
56 // This works because SafeUnsignedAbs() returns an unsigned type, which can
57 // represent the absolute value of all negative numbers of an equal-width
58 // integer type. The call to IsValueNegative() then detects overflow in the
59 // special case of numeric_limits<T>::min(), by evaluating the bit pattern as
60 // a signed integer value. If it is the overflow case, we end up subtracting
61 // one from the unsigned result, thus saturating to numeric_limits<T>::max().
62 return static_cast<T>(
63 SafeUnsignedAbs(value) -
64 IsValueNegative<T>(static_cast<T>(SafeUnsignedAbs(value))));
65 }
66
67 template <
68 typename T,
69 typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
70 constexpr T SaturatedAbsWrapper(T value) {
71 return value < 0 ? -value : value;
72 }
73
74 template <typename T, typename U, class Enable = void>
75 struct ClampedAddOp {};
76
77 template <typename T, typename U>
78 struct ClampedAddOp<T,
79 U,
80 typename std::enable_if<std::is_integral<T>::value &&
81 std::is_integral<U>::value>::type> {
82 using result_type = typename MaxExponentPromotion<T, U>::type;
83 template <typename V = result_type>
84 static constexpr V Do(T x, U y) {
85 if (!IsConstantEvaluated() && ClampedAddFastOp<T, U>::is_supported)
86 return ClampedAddFastOp<T, U>::template Do<V>(x, y);
87
88 static_assert(std::is_same<V, result_type>::value ||
89 IsTypeInRangeForNumericType<U, V>::value,
90 "The saturation result cannot be determined from the "
91 "provided types.");
92 const V saturated = CommonMaxOrMin<V>(IsValueNegative(y));
93 V result = {};
94 return BASE_NUMERICS_LIKELY((CheckedAddOp<T, U>::Do(x, y, &result)))
95 ? result
96 : saturated;
97 }
98 };
99
100 template <typename T, typename U, class Enable = void>
101 struct ClampedSubOp {};
102
103 template <typename T, typename U>
104 struct ClampedSubOp<T,
105 U,
106 typename std::enable_if<std::is_integral<T>::value &&
107 std::is_integral<U>::value>::type> {
108 using result_type = typename MaxExponentPromotion<T, U>::type;
109 template <typename V = result_type>
110 static constexpr V Do(T x, U y) {
111 if (!IsConstantEvaluated() && ClampedSubFastOp<T, U>::is_supported)
112 return ClampedSubFastOp<T, U>::template Do<V>(x, y);
113
114 static_assert(std::is_same<V, result_type>::value ||
115 IsTypeInRangeForNumericType<U, V>::value,
116 "The saturation result cannot be determined from the "
117 "provided types.");
118 const V saturated = CommonMaxOrMin<V>(!IsValueNegative(y));
119 V result = {};
120 return BASE_NUMERICS_LIKELY((CheckedSubOp<T, U>::Do(x, y, &result)))
121 ? result
122 : saturated;
123 }
124 };
125
126 template <typename T, typename U, class Enable = void>
127 struct ClampedMulOp {};
128
129 template <typename T, typename U>
130 struct ClampedMulOp<T,
131 U,
132 typename std::enable_if<std::is_integral<T>::value &&
133 std::is_integral<U>::value>::type> {
134 using result_type = typename MaxExponentPromotion<T, U>::type;
135 template <typename V = result_type>
136 static constexpr V Do(T x, U y) {
137 if (!IsConstantEvaluated() && ClampedMulFastOp<T, U>::is_supported)
138 return ClampedMulFastOp<T, U>::template Do<V>(x, y);
139
140 V result = {};
141 const V saturated =
142 CommonMaxOrMin<V>(IsValueNegative(x) ^ IsValueNegative(y));
143 return BASE_NUMERICS_LIKELY((CheckedMulOp<T, U>::Do(x, y, &result)))
144 ? result
145 : saturated;
146 }
147 };
148
149 template <typename T, typename U, class Enable = void>
150 struct ClampedDivOp {};
151
152 template <typename T, typename U>
153 struct ClampedDivOp<T,
154 U,
155 typename std::enable_if<std::is_integral<T>::value &&
156 std::is_integral<U>::value>::type> {
157 using result_type = typename MaxExponentPromotion<T, U>::type;
158 template <typename V = result_type>
159 static constexpr V Do(T x, U y) {
160 V result = {};
161 if (BASE_NUMERICS_LIKELY((CheckedDivOp<T, U>::Do(x, y, &result))))
162 return result;
163 // Saturation goes to max, min, or NaN (if x is zero).
164 return x ? CommonMaxOrMin<V>(IsValueNegative(x) ^ IsValueNegative(y))
165 : SaturationDefaultLimits<V>::NaN();
166 }
167 };
168
169 template <typename T, typename U, class Enable = void>
170 struct ClampedModOp {};
171
172 template <typename T, typename U>
173 struct ClampedModOp<T,
174 U,
175 typename std::enable_if<std::is_integral<T>::value &&
176 std::is_integral<U>::value>::type> {
177 using result_type = typename MaxExponentPromotion<T, U>::type;
178 template <typename V = result_type>
179 static constexpr V Do(T x, U y) {
180 V result = {};
181 return BASE_NUMERICS_LIKELY((CheckedModOp<T, U>::Do(x, y, &result)))
182 ? result
183 : x;
184 }
185 };
186
187 template <typename T, typename U, class Enable = void>
188 struct ClampedLshOp {};
189
190 // Left shift. Non-zero values saturate in the direction of the sign. A zero
191 // shifted by any value always results in zero.
192 template <typename T, typename U>
193 struct ClampedLshOp<T,
194 U,
195 typename std::enable_if<std::is_integral<T>::value &&
196 std::is_integral<U>::value>::type> {
197 using result_type = T;
198 template <typename V = result_type>
199 static constexpr V Do(T x, U shift) {
200 static_assert(!std::is_signed<U>::value, "Shift value must be unsigned.");
201 if (BASE_NUMERICS_LIKELY(shift < std::numeric_limits<T>::digits)) {
202 // Shift as unsigned to avoid undefined behavior.
203 V result = static_cast<V>(as_unsigned(x) << shift);
204 // If the shift can be reversed, we know it was valid.
205 if (BASE_NUMERICS_LIKELY(result >> shift == x))
206 return result;
207 }
208 return x ? CommonMaxOrMin<V>(IsValueNegative(x)) : 0;
209 }
210 };
211
212 template <typename T, typename U, class Enable = void>
213 struct ClampedRshOp {};
214
215 // Right shift. Negative values saturate to -1. Positive or 0 saturates to 0.
216 template <typename T, typename U>
217 struct ClampedRshOp<T,
218 U,
219 typename std::enable_if<std::is_integral<T>::value &&
220 std::is_integral<U>::value>::type> {
221 using result_type = T;
222 template <typename V = result_type>
223 static constexpr V Do(T x, U shift) {
224 static_assert(!std::is_signed<U>::value, "Shift value must be unsigned.");
225 // Signed right shift is odd, because it saturates to -1 or 0.
226 const V saturated = as_unsigned(V(0)) - IsValueNegative(x);
227 return BASE_NUMERICS_LIKELY(shift < IntegerBitsPlusSign<T>::value)
228 ? saturated_cast<V>(x >> shift)
229 : saturated;
230 }
231 };
232
233 template <typename T, typename U, class Enable = void>
234 struct ClampedAndOp {};
235
236 template <typename T, typename U>
237 struct ClampedAndOp<T,
238 U,
239 typename std::enable_if<std::is_integral<T>::value &&
240 std::is_integral<U>::value>::type> {
241 using result_type = typename std::make_unsigned<
242 typename MaxExponentPromotion<T, U>::type>::type;
243 template <typename V>
244 static constexpr V Do(T x, U y) {
245 return static_cast<result_type>(x) & static_cast<result_type>(y);
246 }
247 };
248
249 template <typename T, typename U, class Enable = void>
250 struct ClampedOrOp {};
251
252 // For simplicity we promote to unsigned integers.
253 template <typename T, typename U>
254 struct ClampedOrOp<T,
255 U,
256 typename std::enable_if<std::is_integral<T>::value &&
257 std::is_integral<U>::value>::type> {
258 using result_type = typename std::make_unsigned<
259 typename MaxExponentPromotion<T, U>::type>::type;
260 template <typename V>
261 static constexpr V Do(T x, U y) {
262 return static_cast<result_type>(x) | static_cast<result_type>(y);
263 }
264 };
265
266 template <typename T, typename U, class Enable = void>
267 struct ClampedXorOp {};
268
269 // For simplicity we support only unsigned integers.
270 template <typename T, typename U>
271 struct ClampedXorOp<T,
272 U,
273 typename std::enable_if<std::is_integral<T>::value &&
274 std::is_integral<U>::value>::type> {
275 using result_type = typename std::make_unsigned<
276 typename MaxExponentPromotion<T, U>::type>::type;
277 template <typename V>
278 static constexpr V Do(T x, U y) {
279 return static_cast<result_type>(x) ^ static_cast<result_type>(y);
280 }
281 };
282
283 template <typename T, typename U, class Enable = void>
284 struct ClampedMaxOp {};
285
286 template <typename T, typename U>
287 struct ClampedMaxOp<
288 T,
289 U,
290 typename std::enable_if<std::is_arithmetic<T>::value &&
291 std::is_arithmetic<U>::value>::type> {
292 using result_type = typename MaxExponentPromotion<T, U>::type;
293 template <typename V = result_type>
294 static constexpr V Do(T x, U y) {
295 return IsGreater<T, U>::Test(x, y) ? saturated_cast<V>(x)
296 : saturated_cast<V>(y);
297 }
298 };
299
300 template <typename T, typename U, class Enable = void>
301 struct ClampedMinOp {};
302
303 template <typename T, typename U>
304 struct ClampedMinOp<
305 T,
306 U,
307 typename std::enable_if<std::is_arithmetic<T>::value &&
308 std::is_arithmetic<U>::value>::type> {
309 using result_type = typename LowestValuePromotion<T, U>::type;
310 template <typename V = result_type>
311 static constexpr V Do(T x, U y) {
312 return IsLess<T, U>::Test(x, y) ? saturated_cast<V>(x)
313 : saturated_cast<V>(y);
314 }
315 };
316
317 // This is just boilerplate that wraps the standard floating point arithmetic.
318 // A macro isn't the nicest solution, but it beats rewriting these repeatedly.
319 #define BASE_FLOAT_ARITHMETIC_OPS(NAME, OP) \
320 template <typename T, typename U> \
321 struct Clamped##NAME##Op< \
322 T, U, \
323 typename std::enable_if<std::is_floating_point<T>::value || \
324 std::is_floating_point<U>::value>::type> { \
325 using result_type = typename MaxExponentPromotion<T, U>::type; \
326 template <typename V = result_type> \
327 static constexpr V Do(T x, U y) { \
328 return saturated_cast<V>(x OP y); \
329 } \
330 };
331
332 BASE_FLOAT_ARITHMETIC_OPS(Add, +)
333 BASE_FLOAT_ARITHMETIC_OPS(Sub, -)
334 BASE_FLOAT_ARITHMETIC_OPS(Mul, *)
335 BASE_FLOAT_ARITHMETIC_OPS(Div, /)
336
337 #undef BASE_FLOAT_ARITHMETIC_OPS
338
339 } // namespace internal
340 } // namespace base
341 } // namespace chromium
342
343 #endif // BASE_NUMERICS_CLAMPED_MATH_IMPL_H_