Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: base/numerics/safe_math_impl.h

Issue 2564493003: Cleanup sign checking in safe math code (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/numerics/safe_conversions_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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_
OLDNEW
« no previous file with comments | « base/numerics/safe_conversions_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698