Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ | 5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ |
| 6 #define BASE_NUMERICS_SAFE_MATH_H_ | 6 #define BASE_NUMERICS_SAFE_MATH_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 // values it returns are unintuitive and likely to change in the future. | 110 // values it returns are unintuitive and likely to change in the future. |
| 111 // Returns: the raw numeric value, regardless of the current state. | 111 // Returns: the raw numeric value, regardless of the current state. |
| 112 T ValueUnsafe() const { return state_.value(); } | 112 T ValueUnsafe() const { return state_.value(); } |
| 113 | 113 |
| 114 // Prototypes for the supported arithmetic operator overloads. | 114 // Prototypes for the supported arithmetic operator overloads. |
| 115 template <typename Src> CheckedNumeric& operator+=(Src rhs); | 115 template <typename Src> CheckedNumeric& operator+=(Src rhs); |
| 116 template <typename Src> CheckedNumeric& operator-=(Src rhs); | 116 template <typename Src> CheckedNumeric& operator-=(Src rhs); |
| 117 template <typename Src> CheckedNumeric& operator*=(Src rhs); | 117 template <typename Src> CheckedNumeric& operator*=(Src rhs); |
| 118 template <typename Src> CheckedNumeric& operator/=(Src rhs); | 118 template <typename Src> CheckedNumeric& operator/=(Src rhs); |
| 119 template <typename Src> CheckedNumeric& operator%=(Src rhs); | 119 template <typename Src> CheckedNumeric& operator%=(Src rhs); |
| 120 template <typename Src> | |
|
Tom Sepez
2016/11/17 00:29:45
nit: format as above?
jschuh
2016/11/17 00:47:51
Done. Stupid git cl format.
| |
| 121 CheckedNumeric& operator<<=(Src rhs); | |
| 122 template <typename Src> | |
| 123 CheckedNumeric& operator>>=(Src rhs); | |
| 120 | 124 |
| 121 CheckedNumeric operator-() const { | 125 CheckedNumeric operator-() const { |
| 122 // Negation is always valid for floating point. | 126 // Negation is always valid for floating point. |
| 123 T value = 0; | 127 T value = 0; |
| 124 bool is_valid = (std::numeric_limits<T>::is_iec559 || IsValid()) && | 128 bool is_valid = (std::numeric_limits<T>::is_iec559 || IsValid()) && |
| 125 CheckedNeg(state_.value(), &value); | 129 CheckedNeg(state_.value(), &value); |
| 126 return CheckedNumeric<T>(value, is_valid); | 130 return CheckedNumeric<T>(value, is_valid); |
| 127 } | 131 } |
| 128 | 132 |
| 129 CheckedNumeric Abs() const { | 133 CheckedNumeric Abs() const { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \ | 237 CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \ |
| 234 operator OP(L lhs, const CheckedNumeric<R>& rhs) { \ | 238 operator OP(L lhs, const CheckedNumeric<R>& rhs) { \ |
| 235 return CheckedNumeric<L>(lhs) OP rhs; \ | 239 return CheckedNumeric<L>(lhs) OP rhs; \ |
| 236 } | 240 } |
| 237 | 241 |
| 238 BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=, MAX_EXPONENT_PROMOTION) | 242 BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=, MAX_EXPONENT_PROMOTION) |
| 239 BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=, MAX_EXPONENT_PROMOTION) | 243 BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=, MAX_EXPONENT_PROMOTION) |
| 240 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=, MAX_EXPONENT_PROMOTION) | 244 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=, MAX_EXPONENT_PROMOTION) |
| 241 BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=, MAX_EXPONENT_PROMOTION) | 245 BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=, MAX_EXPONENT_PROMOTION) |
| 242 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=, MAX_EXPONENT_PROMOTION) | 246 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=, MAX_EXPONENT_PROMOTION) |
| 247 BASE_NUMERIC_ARITHMETIC_OPERATORS(LeftShift, <<, <<=, LEFT_PROMOTION) | |
| 248 BASE_NUMERIC_ARITHMETIC_OPERATORS(RightShift, >>, >>=, LEFT_PROMOTION) | |
| 243 | 249 |
| 244 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS | 250 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS |
| 245 | 251 |
| 246 } // namespace internal | 252 } // namespace internal |
| 247 | 253 |
| 248 using internal::CheckedNumeric; | 254 using internal::CheckedNumeric; |
| 249 | 255 |
| 250 } // namespace base | 256 } // namespace base |
| 251 | 257 |
| 252 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 258 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
| OLD | NEW |