| 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_IMPL_H_ | 5 #ifndef BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
| 6 #define BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 6 #define BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 template <typename Numeric> | 110 template <typename Numeric> |
| 111 struct UnsignedOrFloatForSize<Numeric, false, true> { | 111 struct UnsignedOrFloatForSize<Numeric, false, true> { |
| 112 typedef Numeric type; | 112 typedef Numeric type; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 // Helper templates for integer manipulations. | 115 // Helper templates for integer manipulations. |
| 116 | 116 |
| 117 template <typename T> | 117 template <typename T> |
| 118 bool HasSignBit(T x) { | 118 constexpr bool HasSignBit(T x) { |
| 119 // Cast to unsigned since right shift on signed is undefined. | 119 // Cast to unsigned since right shift on signed is undefined. |
| 120 return !!(static_cast<typename UnsignedIntegerForSize<T>::type>(x) >> | 120 return !!(static_cast<typename UnsignedIntegerForSize<T>::type>(x) >> |
| 121 PositionOfSignBit<T>::value); | 121 PositionOfSignBit<T>::value); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // This wrapper undoes the standard integer promotions. | 124 // This wrapper undoes the standard integer promotions. |
| 125 template <typename T> | 125 template <typename T> |
| 126 T BinaryComplement(T x) { | 126 constexpr T BinaryComplement(T x) { |
| 127 return static_cast<T>(~x); | 127 return static_cast<T>(~x); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Here are the actual portable checked integer math implementations. | 130 // Here are the actual portable checked integer math implementations. |
| 131 // TODO(jschuh): Break this code out from the enable_if pattern and find a clean | 131 // TODO(jschuh): Break this code out from the enable_if pattern and find a clean |
| 132 // way to coalesce things into the CheckedNumericState specializations below. | 132 // way to coalesce things into the CheckedNumericState specializations below. |
| 133 | 133 |
| 134 template <typename T> | 134 template <typename T> |
| 135 typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type | 135 typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type |
| 136 CheckedAdd(T x, T y, RangeConstraint* validity) { | 136 CheckedAdd(T x, T y, RangeConstraint* validity) { |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 sizeof(T) >= (2 * sizeof(Lhs)) && | 523 sizeof(T) >= (2 * sizeof(Lhs)) && |
| 524 StaticDstRangeRelationToSrcRange<T, Rhs>::value != | 524 StaticDstRangeRelationToSrcRange<T, Rhs>::value != |
| 525 NUMERIC_RANGE_CONTAINED && | 525 NUMERIC_RANGE_CONTAINED && |
| 526 sizeof(T) >= (2 * sizeof(Rhs)); | 526 sizeof(T) >= (2 * sizeof(Rhs)); |
| 527 }; | 527 }; |
| 528 | 528 |
| 529 } // namespace internal | 529 } // namespace internal |
| 530 } // namespace base | 530 } // namespace base |
| 531 | 531 |
| 532 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 532 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
| OLD | NEW |