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

Unified Diff: base/numerics/safe_math.h

Issue 2612443002: Convert CheckedNumeric unary operators to constexpr (Closed)
Patch Set: docs Created 3 years, 12 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numerics/safe_math.h
diff --git a/base/numerics/safe_math.h b/base/numerics/safe_math.h
index 32f0dfddabadd624adecddcd86bc1ed74fdeb832..f5007db39c42e450646013b9d92273eec3bbdcc0 100644
--- a/base/numerics/safe_math.h
+++ b/base/numerics/safe_math.h
@@ -204,27 +204,26 @@ class CheckedNumeric {
template <typename Src>
CheckedNumeric& operator^=(const Src rhs);
- CheckedNumeric operator-() const {
- // Negation is always valid for floating point.
- T value = 0;
- bool is_valid = (std::is_floating_point<T>::value || IsValid()) &&
- CheckedNeg(state_.value(), &value);
- return CheckedNumeric<T>(value, is_valid);
+ constexpr CheckedNumeric operator-() const {
+ return CheckedNumeric<T>(
+ NegateWrapper(state_.value()),
+ IsValid() &&
+ (!std::is_signed<T>::value || std::is_floating_point<T>::value ||
+ NegateWrapper(state_.value()) !=
+ std::numeric_limits<T>::lowest()));
}
- CheckedNumeric operator~() const {
- static_assert(!std::is_signed<T>::value, "Type must be unsigned.");
- T value = 0;
- bool is_valid = IsValid() && CheckedInv(state_.value(), &value);
- return CheckedNumeric<T>(value, is_valid);
+ constexpr CheckedNumeric operator~() const {
+ return CheckedNumeric<decltype(InvertWrapper(T()))>(
+ InvertWrapper(state_.value()), IsValid());
}
- CheckedNumeric Abs() const {
- // Absolute value is always valid for floating point.
- T value = 0;
- bool is_valid = (std::is_floating_point<T>::value || IsValid()) &&
- CheckedAbs(state_.value(), &value);
- return CheckedNumeric<T>(value, is_valid);
+ constexpr CheckedNumeric Abs() const {
+ return CheckedNumeric<T>(
+ AbsWrapper(state_.value()),
+ IsValid() &&
+ (!std::is_signed<T>::value || std::is_floating_point<T>::value ||
+ AbsWrapper(state_.value()) != std::numeric_limits<T>::lowest()));
}
template <typename U>
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698