| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 enum RangeConstraint { | 92 enum RangeConstraint { |
| 93 RANGE_VALID = 0x0, // Value can be represented by the destination type. | 93 RANGE_VALID = 0x0, // Value can be represented by the destination type. |
| 94 RANGE_UNDERFLOW = 0x1, // Value would overflow. | 94 RANGE_UNDERFLOW = 0x1, // Value would overflow. |
| 95 RANGE_OVERFLOW = 0x2, // Value would underflow. | 95 RANGE_OVERFLOW = 0x2, // Value would underflow. |
| 96 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). | 96 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 // Helper function for coercing an int back to a RangeContraint. | 99 // Helper function for coercing an int back to a RangeContraint. |
| 100 inline constexpr RangeConstraint GetRangeConstraint( | 100 constexpr RangeConstraint GetRangeConstraint(int integer_range_constraint) { |
| 101 int integer_range_constraint) { | |
| 102 // TODO(jschuh): Once we get full C++14 support we want this | 101 // TODO(jschuh): Once we get full C++14 support we want this |
| 103 // assert(integer_range_constraint >= RANGE_VALID && | 102 // assert(integer_range_constraint >= RANGE_VALID && |
| 104 // integer_range_constraint <= RANGE_INVALID) | 103 // integer_range_constraint <= RANGE_INVALID) |
| 105 return static_cast<RangeConstraint>(integer_range_constraint); | 104 return static_cast<RangeConstraint>(integer_range_constraint); |
| 106 } | 105 } |
| 107 | 106 |
| 108 // This function creates a RangeConstraint from an upper and lower bound | 107 // This function creates a RangeConstraint from an upper and lower bound |
| 109 // check by taking advantage of the fact that only NaN can be out of range in | 108 // check by taking advantage of the fact that only NaN can be out of range in |
| 110 // both directions at once. | 109 // both directions at once. |
| 111 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, | 110 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 static constexpr RangeConstraint Check(Src value) { | 244 static constexpr RangeConstraint Check(Src value) { |
| 246 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) | 245 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
| 247 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) | 246 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
| 248 : GetRangeConstraint( | 247 : GetRangeConstraint( |
| 249 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), | 248 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| 250 value >= static_cast<Src>(0)); | 249 value >= static_cast<Src>(0)); |
| 251 } | 250 } |
| 252 }; | 251 }; |
| 253 | 252 |
| 254 template <typename Dst, typename Src> | 253 template <typename Dst, typename Src> |
| 255 inline constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) { | 254 constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) { |
| 256 static_assert(std::numeric_limits<Src>::is_specialized, | 255 static_assert(std::numeric_limits<Src>::is_specialized, |
| 257 "Argument must be numeric."); | 256 "Argument must be numeric."); |
| 258 static_assert(std::numeric_limits<Dst>::is_specialized, | 257 static_assert(std::numeric_limits<Dst>::is_specialized, |
| 259 "Result must be numeric."); | 258 "Result must be numeric."); |
| 260 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | 259 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
| 261 } | 260 } |
| 262 | 261 |
| 263 } // namespace internal | 262 } // namespace internal |
| 264 } // namespace base | 263 } // namespace base |
| 265 | 264 |
| 266 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 265 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| OLD | NEW |