| 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 19 matching lines...) Expand all Loading... |
| 30 std::is_signed<NumericType>::value; | 30 std::is_signed<NumericType>::value; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 // Helper templates for integer manipulations. | 33 // Helper templates for integer manipulations. |
| 34 | 34 |
| 35 template <typename Integer> | 35 template <typename Integer> |
| 36 struct PositionOfSignBit { | 36 struct PositionOfSignBit { |
| 37 static const size_t value = IntegerBitsPlusSign<Integer>::value - 1; | 37 static const size_t value = IntegerBitsPlusSign<Integer>::value - 1; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 template <typename T> | |
| 41 constexpr bool HasSignBit(T x) { | |
| 42 // Cast to unsigned since right shift on signed is undefined. | |
| 43 return (static_cast<typename std::make_unsigned<T>::type>(x) >> | |
| 44 PositionOfSignBit<T>::value) != 0; | |
| 45 } | |
| 46 | |
| 47 // This wrapper undoes the standard integer promotions. | |
| 48 template <typename T> | |
| 49 constexpr T BinaryComplement(T x) { | |
| 50 return static_cast<T>(~x); | |
| 51 } | |
| 52 | |
| 53 // Determines if a numeric value is negative without throwing compiler | 40 // Determines if a numeric value is negative without throwing compiler |
| 54 // warnings on: unsigned(value) < 0. | 41 // warnings on: unsigned(value) < 0. |
| 55 template <typename T, | 42 template <typename T, |
| 56 typename std::enable_if<std::is_signed<T>::value>::type* = nullptr> | 43 typename std::enable_if<std::is_signed<T>::value>::type* = nullptr> |
| 57 constexpr bool IsValueNegative(T value) { | 44 constexpr bool IsValueNegative(T value) { |
| 58 static_assert(std::is_arithmetic<T>::value, "Argument must be numeric."); | 45 static_assert(std::is_arithmetic<T>::value, "Argument must be numeric."); |
| 59 return value < 0; | 46 return value < 0; |
| 60 } | 47 } |
| 61 | 48 |
| 62 template <typename T, | 49 template <typename T, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 84 template <typename T> | 71 template <typename T> |
| 85 constexpr T SignMask(T x) { | 72 constexpr T SignMask(T x) { |
| 86 using SignedT = typename std::make_signed<T>::type; | 73 using SignedT = typename std::make_signed<T>::type; |
| 87 // Right shift on a signed number is implementation defined, but it's often | 74 // Right shift on a signed number is implementation defined, but it's often |
| 88 // implemented as arithmetic shift. If the compiler uses an arithmetic shift, | 75 // implemented as arithmetic shift. If the compiler uses an arithmetic shift, |
| 89 // then use that to avoid the extra negation. | 76 // then use that to avoid the extra negation. |
| 90 return static_cast<T>( | 77 return static_cast<T>( |
| 91 (static_cast<SignedT>(-1) >> PositionOfSignBit<T>::value) == | 78 (static_cast<SignedT>(-1) >> PositionOfSignBit<T>::value) == |
| 92 static_cast<SignedT>(-1) | 79 static_cast<SignedT>(-1) |
| 93 ? (static_cast<SignedT>(x) >> PositionOfSignBit<T>::value) | 80 ? (static_cast<SignedT>(x) >> PositionOfSignBit<T>::value) |
| 94 : -static_cast<SignedT>(HasSignBit(x))); | 81 : -static_cast<SignedT>(static_cast<SignedT>(x) < 0)); |
| 95 } | 82 } |
| 96 static_assert(SignMask(-2) == -1, | 83 static_assert(SignMask(-2) == -1, |
| 97 "Inconsistent handling of signed right shift."); | 84 "Inconsistent handling of signed right shift."); |
| 98 static_assert(SignMask(-3L) == -1L, | 85 static_assert(SignMask(-3L) == -1L, |
| 99 "Inconsistent handling of signed right shift."); | 86 "Inconsistent handling of signed right shift."); |
| 100 static_assert(SignMask(-4LL) == -1LL, | 87 static_assert(SignMask(-4LL) == -1LL, |
| 101 "Inconsistent handling of signed right shift."); | 88 "Inconsistent handling of signed right shift."); |
| 102 | 89 |
| 103 // This performs a safe, non-branching absolute value via unsigned overflow. | 90 // This performs a safe, non-branching absolute value via unsigned overflow. |
| 104 template <typename T, | 91 template <typename T, |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 static_cast<BigType>(static_cast<L>(lhs)), | 729 static_cast<BigType>(static_cast<L>(lhs)), |
| 743 static_cast<BigType>(static_cast<R>(rhs))) | 730 static_cast<BigType>(static_cast<R>(rhs))) |
| 744 // Let the template functions figure it out for mixed types. | 731 // Let the template functions figure it out for mixed types. |
| 745 : C<L, R>::Test(lhs, rhs); | 732 : C<L, R>::Test(lhs, rhs); |
| 746 }; | 733 }; |
| 747 | 734 |
| 748 } // namespace internal | 735 } // namespace internal |
| 749 } // namespace base | 736 } // namespace base |
| 750 | 737 |
| 751 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 738 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| OLD | NEW |