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

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

Issue 2566733002: Performance optimizations for base/numerics absolute value and multiply (Closed)
Patch Set: more shuffling 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.h ('k') | base/numerics/safe_math_impl.h » ('j') | 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 12 matching lines...) Expand all
23 }; 23 };
24 24
25 // The number of bits (including the sign) in an integer. Eliminates sizeof 25 // The number of bits (including the sign) in an integer. Eliminates sizeof
26 // hacks. 26 // hacks.
27 template <typename NumericType> 27 template <typename NumericType>
28 struct IntegerBitsPlusSign { 28 struct IntegerBitsPlusSign {
29 static const int value = std::numeric_limits<NumericType>::digits + 29 static const int value = std::numeric_limits<NumericType>::digits +
30 std::is_signed<NumericType>::value; 30 std::is_signed<NumericType>::value;
31 }; 31 };
32 32
33 // Helper templates for integer manipulations.
34
35 template <typename Integer>
36 struct PositionOfSignBit {
37 static const size_t value = IntegerBitsPlusSign<Integer>::value - 1;
38 };
39
40 template <typename T>
41 constexpr bool HasSignBit(T x) {
42 // Cast to unsigned since right shift on signed is undefined.
43 return !!(static_cast<typename std::make_unsigned<T>::type>(x) >>
44 PositionOfSignBit<T>::value);
45 }
46
47 // This wrapper undoes the standard integer promotions.
scottmg 2016/12/09 22:48:24 I don't see this getting used?
jschuh 2016/12/09 22:58:05 It is in safe_math_impl.h. I just moved it because
48 template <typename T>
49 constexpr T BinaryComplement(T x) {
50 return static_cast<T>(~x);
51 }
52
53 // This performs a safe, non-branching absolute value via unsigned overflow.
54 template <typename T,
55 typename std::enable_if<!std::is_signed<T>::value>::type* = nullptr>
scottmg 2016/12/09 23:27:02 std::is_integral<T>::value && in the enable_if fo
jschuh 2016/12/09 23:31:28 I had that originally, but there are no unsigned n
56 constexpr T SafeUnsignedAbsImpl(T value, T sign_mask) {
57 return (value + sign_mask) ^ sign_mask;
58 }
59
60 template <typename T,
61 typename std::enable_if<std::is_integral<T>::value &&
62 std::is_signed<T>::value>::type* = nullptr>
63 constexpr typename std::make_unsigned<T>::type SafeUnsignedAbs(T value) {
64 using UnsignedT = typename std::make_unsigned<T>::type;
65 return SafeUnsignedAbsImpl(
66 static_cast<UnsignedT>(value),
67 // The sign mask is all ones for negative and zero otherwise.
68 static_cast<UnsignedT>(-static_cast<T>(HasSignBit(value))));
69 }
70
71 template <typename T,
72 typename std::enable_if<std::is_integral<T>::value &&
73 !std::is_signed<T>::value>::type* = nullptr>
74 constexpr T SafeUnsignedAbs(T value) {
75 // T is unsigned, so |value| must already be positive.
76 return static_cast<T>(value);
77 }
78
33 enum IntegerRepresentation { 79 enum IntegerRepresentation {
34 INTEGER_REPRESENTATION_UNSIGNED, 80 INTEGER_REPRESENTATION_UNSIGNED,
35 INTEGER_REPRESENTATION_SIGNED 81 INTEGER_REPRESENTATION_SIGNED
36 }; 82 };
37 83
38 // A range for a given nunmeric Src type is contained for a given numeric Dst 84 // A range for a given nunmeric Src type is contained for a given numeric Dst
39 // type if both numeric_limits<Src>::max() <= numeric_limits<Dst>::max() and 85 // type if both numeric_limits<Src>::max() <= numeric_limits<Dst>::max() and
40 // numeric_limits<Src>::lowest() >= numeric_limits<Dst>::lowest() are true. 86 // numeric_limits<Src>::lowest() >= numeric_limits<Dst>::lowest() are true.
41 // We implement this as template specializations rather than simple static 87 // We implement this as template specializations rather than simple static
42 // comparisons to ensure type correctness in our comparisons. 88 // comparisons to ensure type correctness in our comparisons.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 static_assert(IntegerBitsPlusSign<intmax_t>::value == 64, 327 static_assert(IntegerBitsPlusSign<intmax_t>::value == 64,
282 "Max integer size not supported for this toolchain."); 328 "Max integer size not supported for this toolchain.");
283 329
284 template <typename Integer, bool IsSigned = std::is_signed<Integer>::value> 330 template <typename Integer, bool IsSigned = std::is_signed<Integer>::value>
285 struct TwiceWiderInteger { 331 struct TwiceWiderInteger {
286 using type = 332 using type =
287 typename IntegerForDigitsAndSign<IntegerBitsPlusSign<Integer>::value * 2, 333 typename IntegerForDigitsAndSign<IntegerBitsPlusSign<Integer>::value * 2,
288 IsSigned>::type; 334 IsSigned>::type;
289 }; 335 };
290 336
291 template <typename Integer>
292 struct PositionOfSignBit {
293 static const size_t value = IntegerBitsPlusSign<Integer>::value - 1;
294 };
295
296 enum ArithmeticPromotionCategory { 337 enum ArithmeticPromotionCategory {
297 LEFT_PROMOTION, // Use the type of the left-hand argument. 338 LEFT_PROMOTION, // Use the type of the left-hand argument.
298 RIGHT_PROMOTION // Use the type of the right-hand argument. 339 RIGHT_PROMOTION // Use the type of the right-hand argument.
299 }; 340 };
300 341
301 // Determines the type that can represent the largest positive value. 342 // Determines the type that can represent the largest positive value.
302 template <typename Lhs, 343 template <typename Lhs,
303 typename Rhs, 344 typename Rhs,
304 ArithmeticPromotionCategory Promotion = 345 ArithmeticPromotionCategory Promotion =
305 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value) 346 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value)
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 static_cast<BigType>(static_cast<L>(lhs)), 648 static_cast<BigType>(static_cast<L>(lhs)),
608 static_cast<BigType>(static_cast<R>(rhs))) 649 static_cast<BigType>(static_cast<R>(rhs)))
609 // Let the template functions figure it out for mixed types. 650 // Let the template functions figure it out for mixed types.
610 : C<L, R>::Test(lhs, rhs); 651 : C<L, R>::Test(lhs, rhs);
611 }; 652 };
612 653
613 } // namespace internal 654 } // namespace internal
614 } // namespace base 655 } // namespace base
615 656
616 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 657 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW
« no previous file with comments | « base/numerics/safe_conversions.h ('k') | base/numerics/safe_math_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698