Bug 768538 - CheckedInt: code simplification; remove the separate operator/ implementation - r=Ms2ger
authorBenoit Jacob <bjacob@mozilla.com>
Sun, 08 Jul 2012 11:25:24 -0400
changeset 98641 5a372bbee65ccfc6390e33c50ba001b937b0c0a6
parent 98640 c8715136091bfd3cdd6e5d71289e28878e07e06c
child 98642 b7c9802cfef5727208c10ecdc5681e0885b6560a
push id11571
push userbjacob@mozilla.com
push dateSun, 08 Jul 2012 15:25:36 +0000
treeherdermozilla-inbound@5a372bbee65c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs768538
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 768538 - CheckedInt: code simplification; remove the separate operator/ implementation - r=Ms2ger
mfbt/CheckedInt.h
--- a/mfbt/CheckedInt.h
+++ b/mfbt/CheckedInt.h
@@ -707,48 +707,30 @@ class CheckedInt
     bool operator >=(U other) const MOZ_DELETE;
 };
 
 #define MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(NAME, OP)                \
 template<typename T>                                                  \
 inline CheckedInt<T> operator OP(const CheckedInt<T> &lhs,            \
                                  const CheckedInt<T> &rhs)            \
 {                                                                     \
-  T x = lhs.mValue;                                                   \
-  T y = rhs.mValue;                                                   \
-  T isOpValid = detail::Is##NAME##Valid(x, y);                        \
-  if (isOpValid) {                                                    \
-    T result = x OP y;                                                \
-    return CheckedInt<T>(result, lhs.mIsValid && rhs.mIsValid);       \
-  } else {                                                            \
-    return CheckedInt<T>(T(0), false);                                \
-  }                                                                   \
+  if (!detail::Is##NAME##Valid(lhs.mValue, rhs.mValue))               \
+    return CheckedInt<T>(0, false);                                   \
+                                                                      \
+  return CheckedInt<T>(lhs.mValue OP rhs.mValue,                      \
+                       lhs.mIsValid && rhs.mIsValid);                 \
 }
 
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Add, +)
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Sub, -)
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Mul, *)
+MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Div, /)
 
 #undef MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR
 
-// Division can't be implemented by MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR
-// because if rhs == 0, we are not allowed to even try to compute the quotient.
-template<typename T>
-inline CheckedInt<T> operator /(const CheckedInt<T> &lhs,
-                                const CheckedInt<T> &rhs)
-{
-  T x = lhs.mValue;
-  T y = rhs.mValue;
-  bool isOpValid = detail::IsDivValid(x, y);
-  T result = isOpValid ? (x / y) : 0;
-  /* give the compiler a good chance to perform RVO */
-  return CheckedInt<T>(result,
-                       lhs.mIsValid && rhs.mIsValid && isOpValid);
-}
-
 // Implement castToCheckedInt<T>(x), making sure that
 //  - it allows x to be either a CheckedInt<T> or any integer type
 //    that can be casted to T
 //  - if x is already a CheckedInt<T>, we just return a reference to it,
 //    instead of copying it (optimization)
 
 namespace detail {