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 return static_cast<typename std::make_signed<T>::type>(x) < | 50 return static_cast<typename std::make_signed<T>::type>(x) < |
50 static_cast<typename std::make_signed<T>::type>(0); | 51 static_cast<typename std::make_signed<T>::type>(0); |
51 } | 52 } |
52 | 53 |
53 // This wrapper undoes the standard integer promotions. | 54 // This wrapper undoes the standard integer promotions. |
54 template <typename T> | 55 template <typename T> |
55 constexpr T BinaryComplement(T x) { | 56 constexpr T BinaryComplement(T x) { |
56 return static_cast<T>(~x); | 57 return static_cast<T>(~x); |
57 } | 58 } |
58 | 59 |
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 using math = M<typename UnderlyingType<L>::type, | 730 using math = M<typename UnderlyingType<L>::type, |
730 typename UnderlyingType<R>::type, | 731 typename UnderlyingType<R>::type, |
731 void>; | 732 void>; |
732 using type = typename math::result_type; | 733 using type = typename math::result_type; |
733 }; | 734 }; |
734 | 735 |
735 } // namespace internal | 736 } // namespace internal |
736 } // namespace base | 737 } // namespace base |
737 | 738 |
738 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 739 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
OLD | NEW |