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 SAFE_MATH_IMPL_H_ | 5 #ifndef SAFE_MATH_IMPL_H_ |
6 #define SAFE_MATH_IMPL_H_ | 6 #define SAFE_MATH_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 -static_cast<typename SignedIntegerForSize<T>::type>(value)); | 270 -static_cast<typename SignedIntegerForSize<T>::type>(value)); |
271 } | 271 } |
272 | 272 |
273 template <typename T> | 273 template <typename T> |
274 typename enable_if< | 274 typename enable_if< |
275 std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed, | 275 std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed, |
276 T>::type | 276 T>::type |
277 CheckedAbs(T value, RangeConstraint* validity) { | 277 CheckedAbs(T value, RangeConstraint* validity) { |
278 *validity = | 278 *validity = |
279 value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW; | 279 value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW; |
280 return std::abs(value); | 280 return static_cast<T>(std::abs(value)); |
281 } | 281 } |
282 | 282 |
283 template <typename T> | 283 template <typename T> |
284 typename enable_if< | 284 typename enable_if< |
285 std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed, | 285 std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed, |
286 T>::type | 286 T>::type |
287 CheckedAbs(T value, RangeConstraint* validity) { | 287 CheckedAbs(T value, RangeConstraint* validity) { |
288 // Absolute value of a positive is just its identiy. | 288 // Absolute value of a positive is just its identiy. |
289 *validity = RANGE_VALID; | 289 *validity = RANGE_VALID; |
290 return value; | 290 return value; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 RangeConstraint validity_; | 352 RangeConstraint validity_; |
353 | 353 |
354 public: | 354 public: |
355 template <typename Src, NumericRepresentation type> | 355 template <typename Src, NumericRepresentation type> |
356 friend class CheckedNumericState; | 356 friend class CheckedNumericState; |
357 | 357 |
358 CheckedNumericState() : value_(0), validity_(RANGE_VALID) {} | 358 CheckedNumericState() : value_(0), validity_(RANGE_VALID) {} |
359 | 359 |
360 template <typename Src> | 360 template <typename Src> |
361 CheckedNumericState(Src value, RangeConstraint validity) | 361 CheckedNumericState(Src value, RangeConstraint validity) |
362 : value_(value), | 362 : value_(static_cast<T>(value)), |
363 validity_(GetRangeConstraint(validity | | 363 validity_(GetRangeConstraint(validity | |
364 DstRangeRelationToSrcRange<T>(value))) { | 364 DstRangeRelationToSrcRange<T>(value))) { |
365 COMPILE_ASSERT(std::numeric_limits<Src>::is_specialized, | 365 COMPILE_ASSERT(std::numeric_limits<Src>::is_specialized, |
366 argument_must_be_numeric); | 366 argument_must_be_numeric); |
367 } | 367 } |
368 | 368 |
369 // Copy constructor. | 369 // Copy constructor. |
370 template <typename Src> | 370 template <typename Src> |
371 CheckedNumericState(const CheckedNumericState<Src>& rhs) | 371 CheckedNumericState(const CheckedNumericState<Src>& rhs) |
372 : value_(static_cast<T>(rhs.value())), | 372 : value_(static_cast<T>(rhs.value())), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 sizeof(T) >= (2 * sizeof(Lhs)) && | 493 sizeof(T) >= (2 * sizeof(Lhs)) && |
494 StaticDstRangeRelationToSrcRange<T, Rhs>::value != | 494 StaticDstRangeRelationToSrcRange<T, Rhs>::value != |
495 NUMERIC_RANGE_CONTAINED && | 495 NUMERIC_RANGE_CONTAINED && |
496 sizeof(T) >= (2 * sizeof(Rhs)); | 496 sizeof(T) >= (2 * sizeof(Rhs)); |
497 }; | 497 }; |
498 | 498 |
499 } // namespace internal | 499 } // namespace internal |
500 } // namespace base | 500 } // namespace base |
501 | 501 |
502 #endif // SAFE_MATH_IMPL_H_ | 502 #endif // SAFE_MATH_IMPL_H_ |
OLD | NEW |