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_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 Loading... |
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 |
293 enum ArithmeticPromotionCategory { | 298 enum ArithmeticPromotionCategory { |
294 LEFT_PROMOTION, // Use the type of the left-hand argument. | 299 LEFT_PROMOTION, // Use the type of the left-hand argument. |
295 RIGHT_PROMOTION // Use the type of the right-hand argument. | 300 RIGHT_PROMOTION // Use the type of the right-hand argument. |
296 }; | 301 }; |
297 | 302 |
298 // Determines the type that can represent the largest positive value. | 303 // Determines the type that can represent the largest positive value. |
299 template <typename Lhs, | 304 template <typename Lhs, |
300 typename Rhs, | 305 typename Rhs, |
301 ArithmeticPromotionCategory Promotion = | 306 ArithmeticPromotionCategory Promotion = |
302 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value) | 307 (MaxExponent<Lhs>::value > MaxExponent<Rhs>::value) |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 static_cast<BigType>(static_cast<L>(lhs)), | 609 static_cast<BigType>(static_cast<L>(lhs)), |
605 static_cast<BigType>(static_cast<R>(rhs))) | 610 static_cast<BigType>(static_cast<R>(rhs))) |
606 // Let the template functions figure it out for mixed types. | 611 // Let the template functions figure it out for mixed types. |
607 : C<L, R>::Test(lhs, rhs); | 612 : C<L, R>::Test(lhs, rhs); |
608 }; | 613 }; |
609 | 614 |
610 } // namespace internal | 615 } // namespace internal |
611 } // namespace base | 616 } // namespace base |
612 | 617 |
613 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 618 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
OLD | NEW |