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 28 matching lines...) Expand all Loading... | |
39 | 39 |
40 template <typename Numeric> | 40 template <typename Numeric> |
41 struct UnsignedOrFloatForSize<Numeric, false, true> { | 41 struct UnsignedOrFloatForSize<Numeric, false, true> { |
42 using type = Numeric; | 42 using type = Numeric; |
43 }; | 43 }; |
44 | 44 |
45 // Helper templates for integer manipulations. | 45 // Helper templates for integer manipulations. |
46 | 46 |
47 template <typename T> | 47 template <typename T> |
48 constexpr bool HasSignBit(T x) { | 48 constexpr bool HasSignBit(T x) { |
49 // Cast to unsigned since right shift on signed is undefined. | 49 // Cast to unsigned since right shift on signed is undefined. |
scottmg
2016/12/08 21:05:02
You're not casting to unsigned.
| |
50 return !!(static_cast<typename std::make_unsigned<T>::type>(x) >> | 50 return static_cast<typename std::make_signed<T>::type>(x) < |
51 PositionOfSignBit<T>::value); | 51 static_cast<typename std::make_signed<T>::type>(0); |
52 } | 52 } |
53 | 53 |
54 // This wrapper undoes the standard integer promotions. | 54 // This wrapper undoes the standard integer promotions. |
55 template <typename T> | 55 template <typename T> |
56 constexpr T BinaryComplement(T x) { | 56 constexpr T BinaryComplement(T x) { |
57 return static_cast<T>(~x); | 57 return static_cast<T>(~x); |
58 } | 58 } |
59 | 59 |
60 // Probe for builtin math overflow support on Clang and version check on GCC. | 60 // Probe for builtin math overflow support on Clang and version check on GCC. |
61 #if defined(__has_builtin) | 61 #if defined(__has_builtin) |
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
730 using math = M<typename UnderlyingType<L>::type, | 730 using math = M<typename UnderlyingType<L>::type, |
731 typename UnderlyingType<R>::type, | 731 typename UnderlyingType<R>::type, |
732 void>; | 732 void>; |
733 using type = typename math::result_type; | 733 using type = typename math::result_type; |
734 }; | 734 }; |
735 | 735 |
736 } // namespace internal | 736 } // namespace internal |
737 } // namespace base | 737 } // namespace base |
738 | 738 |
739 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 739 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
OLD | NEW |