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

Side by Side Diff: base/numerics/safe_conversions_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 | « no previous file | base/numerics/safe_math_impl.h » ('j') | base/numerics/safe_math_impl.h » ('J')
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 <limits.h> 8 #include <limits.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 static_assert(IntegerBitsPlusSign<intmax_t>::value == 64, 283 static_assert(IntegerBitsPlusSign<intmax_t>::value == 64,
284 "Max integer size not supported for this toolchain."); 284 "Max integer size not supported for this toolchain.");
285 285
286 template <typename Integer, bool IsSigned = std::is_signed<Integer>::value> 286 template <typename Integer, bool IsSigned = std::is_signed<Integer>::value>
287 struct TwiceWiderInteger { 287 struct TwiceWiderInteger {
288 using type = 288 using type =
289 typename IntegerForDigitsAndSign<IntegerBitsPlusSign<Integer>::value * 2, 289 typename IntegerForDigitsAndSign<IntegerBitsPlusSign<Integer>::value * 2,
290 IsSigned>::type; 290 IsSigned>::type;
291 }; 291 };
292 292
293 template <typename Integer>
294 struct PositionOfSignBit {
295 static const size_t value = IntegerBitsPlusSign<Integer>::value - 1;
296 };
297
298 enum ArithmeticPromotionCategory { 293 enum ArithmeticPromotionCategory {
299 LEFT_PROMOTION, // Use the type of the left-hand argument. 294 LEFT_PROMOTION, // Use the type of the left-hand argument.
300 RIGHT_PROMOTION // Use the type of the right-hand argument. 295 RIGHT_PROMOTION // Use the type of the right-hand argument.
301 }; 296 };
302 297
303 // Determines the type that can represent the largest positive value. 298 // Determines the type that can represent the largest positive value.
304 template <typename Lhs, 299 template <typename Lhs,
305 typename Rhs, 300 typename Rhs,
306 ArithmeticPromotionCategory Promotion = 301 ArithmeticPromotionCategory Promotion =
307 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value) 302 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value)
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 static_cast<BigType>(static_cast<L>(lhs)), 604 static_cast<BigType>(static_cast<L>(lhs)),
610 static_cast<BigType>(static_cast<R>(rhs))) 605 static_cast<BigType>(static_cast<R>(rhs)))
611 // Let the template functions figure it out for mixed types. 606 // Let the template functions figure it out for mixed types.
612 : C<L, R>::Test(lhs, rhs); 607 : C<L, R>::Test(lhs, rhs);
613 }; 608 };
614 609
615 } // namespace internal 610 } // namespace internal
616 } // namespace base 611 } // namespace base
617 612
618 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 613 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | base/numerics/safe_math_impl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698