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_MATH_IMPL_H_ | 5 #ifndef BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
6 #define BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 6 #define BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 static_cast<IntermediateType>(x) * static_cast<IntermediateType>(y); | 169 static_cast<IntermediateType>(x) * static_cast<IntermediateType>(y); |
170 *validity = DstRangeRelationToSrcRange<T>(tmp); | 170 *validity = DstRangeRelationToSrcRange<T>(tmp); |
171 return static_cast<T>(tmp); | 171 return static_cast<T>(tmp); |
172 } | 172 } |
173 | 173 |
174 template <typename T> | 174 template <typename T> |
175 typename enable_if<std::numeric_limits<T>::is_integer&& std::numeric_limits< | 175 typename enable_if<std::numeric_limits<T>::is_integer&& std::numeric_limits< |
176 T>::is_signed&&(sizeof(T) * 2 > sizeof(uintmax_t)), | 176 T>::is_signed&&(sizeof(T) * 2 > sizeof(uintmax_t)), |
177 T>::type | 177 T>::type |
178 CheckedMul(T x, T y, RangeConstraint* validity) { | 178 CheckedMul(T x, T y, RangeConstraint* validity) { |
179 // if either side is zero then the result will be zero. | 179 // If either side is zero then the result will be zero. |
180 if (!(x || y)) { | 180 if (!x || !y) { |
jschuh
2015/05/18 14:28:04
fwiw, I think I intended this:
if (!(x | y)) {
| |
181 return RANGE_VALID; | 181 return RANGE_VALID; |
182 | 182 |
183 } else if (x > 0) { | 183 } else if (x > 0) { |
184 if (y > 0) | 184 if (y > 0) |
185 *validity = | 185 *validity = |
186 x <= std::numeric_limits<T>::max() / y ? RANGE_VALID : RANGE_OVERFLOW; | 186 x <= std::numeric_limits<T>::max() / y ? RANGE_VALID : RANGE_OVERFLOW; |
187 else | 187 else |
188 *validity = y >= std::numeric_limits<T>::min() / x ? RANGE_VALID | 188 *validity = y >= std::numeric_limits<T>::min() / x ? RANGE_VALID |
189 : RANGE_UNDERFLOW; | 189 : RANGE_UNDERFLOW; |
190 | 190 |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 sizeof(T) >= (2 * sizeof(Lhs)) && | 492 sizeof(T) >= (2 * sizeof(Lhs)) && |
493 StaticDstRangeRelationToSrcRange<T, Rhs>::value != | 493 StaticDstRangeRelationToSrcRange<T, Rhs>::value != |
494 NUMERIC_RANGE_CONTAINED && | 494 NUMERIC_RANGE_CONTAINED && |
495 sizeof(T) >= (2 * sizeof(Rhs)); | 495 sizeof(T) >= (2 * sizeof(Rhs)); |
496 }; | 496 }; |
497 | 497 |
498 } // namespace internal | 498 } // namespace internal |
499 } // namespace base | 499 } // namespace base |
500 | 500 |
501 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ | 501 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ |
OLD | NEW |