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