| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 return CheckedNumeric<T>::cast(*this).ValueUnsafe(); | 105 return CheckedNumeric<T>::cast(*this).ValueUnsafe(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // ValueUnsafe() - DO NOT USE THIS IN EXTERNAL CODE - It is public right now | 108 // ValueUnsafe() - DO NOT USE THIS IN EXTERNAL CODE - It is public right now |
| 109 // for tests and to avoid a big matrix of friend operator overloads. But the | 109 // for tests and to avoid a big matrix of friend operator overloads. But the |
| 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> |
| 116 template <typename Src> CheckedNumeric& operator-=(Src rhs); | 116 CheckedNumeric& operator+=(Src rhs); |
| 117 template <typename Src> CheckedNumeric& operator*=(Src rhs); | 117 template <typename Src> |
| 118 template <typename Src> CheckedNumeric& operator/=(Src rhs); | 118 CheckedNumeric& operator-=(Src rhs); |
| 119 template <typename Src> CheckedNumeric& operator%=(Src rhs); | 119 template <typename Src> |
| 120 CheckedNumeric& operator*=(Src rhs); |
| 121 template <typename Src> |
| 122 CheckedNumeric& operator/=(Src rhs); |
| 123 template <typename Src> |
| 124 CheckedNumeric& operator%=(Src rhs); |
| 125 template <typename Src> |
| 126 CheckedNumeric& operator<<=(Src rhs); |
| 127 template <typename Src> |
| 128 CheckedNumeric& operator>>=(Src rhs); |
| 120 | 129 |
| 121 CheckedNumeric operator-() const { | 130 CheckedNumeric operator-() const { |
| 122 // Negation is always valid for floating point. | 131 // Negation is always valid for floating point. |
| 123 T value = 0; | 132 T value = 0; |
| 124 bool is_valid = (std::numeric_limits<T>::is_iec559 || IsValid()) && | 133 bool is_valid = (std::numeric_limits<T>::is_iec559 || IsValid()) && |
| 125 CheckedNeg(state_.value(), &value); | 134 CheckedNeg(state_.value(), &value); |
| 126 return CheckedNumeric<T>(value, is_valid); | 135 return CheckedNumeric<T>(value, is_valid); |
| 127 } | 136 } |
| 128 | 137 |
| 129 CheckedNumeric Abs() const { | 138 CheckedNumeric Abs() const { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \ | 242 CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \ |
| 234 operator OP(L lhs, const CheckedNumeric<R>& rhs) { \ | 243 operator OP(L lhs, const CheckedNumeric<R>& rhs) { \ |
| 235 return CheckedNumeric<L>(lhs) OP rhs; \ | 244 return CheckedNumeric<L>(lhs) OP rhs; \ |
| 236 } | 245 } |
| 237 | 246 |
| 238 BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=, MAX_EXPONENT_PROMOTION) | 247 BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=, MAX_EXPONENT_PROMOTION) |
| 239 BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=, MAX_EXPONENT_PROMOTION) | 248 BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=, MAX_EXPONENT_PROMOTION) |
| 240 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=, MAX_EXPONENT_PROMOTION) | 249 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=, MAX_EXPONENT_PROMOTION) |
| 241 BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=, MAX_EXPONENT_PROMOTION) | 250 BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=, MAX_EXPONENT_PROMOTION) |
| 242 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=, MAX_EXPONENT_PROMOTION) | 251 BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=, MAX_EXPONENT_PROMOTION) |
| 252 BASE_NUMERIC_ARITHMETIC_OPERATORS(LeftShift, <<, <<=, LEFT_PROMOTION) |
| 253 BASE_NUMERIC_ARITHMETIC_OPERATORS(RightShift, >>, >>=, LEFT_PROMOTION) |
| 243 | 254 |
| 244 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS | 255 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS |
| 245 | 256 |
| 246 } // namespace internal | 257 } // namespace internal |
| 247 | 258 |
| 248 using internal::CheckedNumeric; | 259 using internal::CheckedNumeric; |
| 249 | 260 |
| 250 } // namespace base | 261 } // namespace base |
| 251 | 262 |
| 252 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 263 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
| OLD | NEW |