| 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_CONVERSIONS_IMPL_H_ | 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // This wrapper undoes the standard integer promotions. | 47 // This wrapper undoes the standard integer promotions. |
| 48 template <typename T> | 48 template <typename T> |
| 49 constexpr T BinaryComplement(T x) { | 49 constexpr T BinaryComplement(T x) { |
| 50 return static_cast<T>(~x); | 50 return static_cast<T>(~x); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // This performs a safe, non-branching absolute value via unsigned overflow. | 53 // This performs a safe, non-branching absolute value via unsigned overflow. |
| 54 template <typename T> | 54 template <typename T> |
| 55 constexpr T SafeUnsignedAbsImpl(T value, T sign_mask) { | 55 constexpr T SafeUnsignedAbsImpl(T value, T sign_mask) { |
| 56 static_assert(!std::is_signed<T>::value, "Types must be unsigned."); | 56 static_assert(!std::is_signed<T>::value, "Types must be unsigned."); |
| 57 return (value + sign_mask) ^ sign_mask; | 57 return (value ^ sign_mask) - sign_mask; |
| 58 } | 58 } |
| 59 | 59 |
| 60 template <typename T, | 60 template <typename T, |
| 61 typename std::enable_if<std::is_integral<T>::value && | 61 typename std::enable_if<std::is_integral<T>::value && |
| 62 std::is_signed<T>::value>::type* = nullptr> | 62 std::is_signed<T>::value>::type* = nullptr> |
| 63 constexpr typename std::make_unsigned<T>::type SafeUnsignedAbs(T value) { | 63 constexpr typename std::make_unsigned<T>::type SafeUnsignedAbs(T value) { |
| 64 using UnsignedT = typename std::make_unsigned<T>::type; | 64 using UnsignedT = typename std::make_unsigned<T>::type; |
| 65 return SafeUnsignedAbsImpl( | 65 return SafeUnsignedAbsImpl( |
| 66 static_cast<UnsignedT>(value), | 66 static_cast<UnsignedT>(value), |
| 67 // The sign mask is all ones for negative and zero otherwise. | 67 // The sign mask is all ones for negative and zero otherwise. |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 static_cast<BigType>(static_cast<L>(lhs)), | 648 static_cast<BigType>(static_cast<L>(lhs)), |
| 649 static_cast<BigType>(static_cast<R>(rhs))) | 649 static_cast<BigType>(static_cast<R>(rhs))) |
| 650 // Let the template functions figure it out for mixed types. | 650 // Let the template functions figure it out for mixed types. |
| 651 : C<L, R>::Test(lhs, rhs); | 651 : C<L, R>::Test(lhs, rhs); |
| 652 }; | 652 }; |
| 653 | 653 |
| 654 } // namespace internal | 654 } // namespace internal |
| 655 } // namespace base | 655 } // namespace base |
| 656 | 656 |
| 657 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 657 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| OLD | NEW |