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

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

Issue 2574443002: Make base/numerics abs match runtime libraries (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 | « no previous file | 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_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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 // This wrapper undoes the standard integer promotions. 47 // This wrapper undoes the standard integer promotions.
48 template <typename T> 48 template <typename T>
49 constexpr T BinaryComplement(T x) { 49 constexpr T BinaryComplement(T x) {
50 return static_cast<T>(~x); 50 return static_cast<T>(~x);
51 } 51 }
52 52
53 // This performs a safe, non-branching absolute value via unsigned overflow. 53 // This performs a safe, non-branching absolute value via unsigned overflow.
54 template <typename T> 54 template <typename T>
55 constexpr T SafeUnsignedAbsImpl(T value, T sign_mask) { 55 constexpr T SafeUnsignedAbsImpl(T value, T sign_mask) {
56 static_assert(!std::is_signed<T>::value, "Types must be unsigned."); 56 static_assert(!std::is_signed<T>::value, "Types must be unsigned.");
57 return (value + sign_mask) ^ sign_mask; 57 return (value ^ sign_mask) - sign_mask;
58 } 58 }
59 59
60 template <typename T, 60 template <typename T,
61 typename std::enable_if<std::is_integral<T>::value && 61 typename std::enable_if<std::is_integral<T>::value &&
62 std::is_signed<T>::value>::type* = nullptr> 62 std::is_signed<T>::value>::type* = nullptr>
63 constexpr typename std::make_unsigned<T>::type SafeUnsignedAbs(T value) { 63 constexpr typename std::make_unsigned<T>::type SafeUnsignedAbs(T value) {
64 using UnsignedT = typename std::make_unsigned<T>::type; 64 using UnsignedT = typename std::make_unsigned<T>::type;
65 return SafeUnsignedAbsImpl( 65 return SafeUnsignedAbsImpl(
66 static_cast<UnsignedT>(value), 66 static_cast<UnsignedT>(value),
67 // The sign mask is all ones for negative and zero otherwise. 67 // The sign mask is all ones for negative and zero otherwise.
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 static_cast<BigType>(static_cast<L>(lhs)), 648 static_cast<BigType>(static_cast<L>(lhs)),
649 static_cast<BigType>(static_cast<R>(rhs))) 649 static_cast<BigType>(static_cast<R>(rhs)))
650 // Let the template functions figure it out for mixed types. 650 // Let the template functions figure it out for mixed types.
651 : C<L, R>::Test(lhs, rhs); 651 : C<L, R>::Test(lhs, rhs);
652 }; 652 };
653 653
654 } // namespace internal 654 } // namespace internal
655 } // namespace base 655 } // namespace base
656 656
657 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 657 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698