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

Unified Diff: base/numerics/safe_math.h

Issue 2502083002: Restructure ArithmeticPromotion to support alternate promotions (Closed)
Patch Set: nit Created 4 years, 1 month 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') | base/numerics/safe_math_impl.h » ('J')
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 85847a4ef51b153292e6bb817e5d9dddd5d8fc07..41aa0515c1dfb17b2feccf28cff379965d720e18 100644
--- a/base/numerics/safe_math.h
+++ b/base/numerics/safe_math.h
@@ -195,12 +195,12 @@ class CheckedNumeric {
// * We skip range checks for floating points.
// * We skip range checks for destination integers with sufficient range.
// TODO(jschuh): extract these out into templates.
-#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \
+#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP, PROMOTION) \
/* Binary arithmetic operator for CheckedNumerics of the same type. */ \
template <typename L, typename R> \
- CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
- const CheckedNumeric<L>& lhs, const CheckedNumeric<R>& rhs) { \
- using P = typename ArithmeticPromotion<L, R>::type; \
+ CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \
+ operator OP(const CheckedNumeric<L>& lhs, const CheckedNumeric<R>& rhs) { \
+ using P = typename ArithmeticPromotion<PROMOTION, L, R>::type; \
if (!rhs.IsValid() || !lhs.IsValid()) \
return CheckedNumeric<P>(0, false); \
/* Floating point always takes the fast path */ \
@@ -222,24 +222,24 @@ class CheckedNumeric {
template <typename L, typename R, \
typename std::enable_if<std::is_arithmetic<R>::value>::type* = \
nullptr> \
- CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
- const CheckedNumeric<L>& lhs, R rhs) { \
+ CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \
+ operator OP(const CheckedNumeric<L>& lhs, R rhs) { \
return lhs OP CheckedNumeric<R>(rhs); \
} \
/* Binary arithmetic operator for left numeric and right CheckedNumeric. */ \
template <typename L, typename R, \
typename std::enable_if<std::is_arithmetic<L>::value>::type* = \
nullptr> \
- CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
- L lhs, const CheckedNumeric<R>& rhs) { \
+ CheckedNumeric<typename ArithmeticPromotion<PROMOTION, L, R>::type> \
+ operator OP(L lhs, const CheckedNumeric<R>& rhs) { \
return CheckedNumeric<L>(lhs) OP rhs; \
}
-BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, += )
-BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -= )
-BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *= )
-BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /= )
-BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %= )
+BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=, MAX_EXPONENT_PROMOTION)
+BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=, MAX_EXPONENT_PROMOTION)
+BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=, MAX_EXPONENT_PROMOTION)
Tom Sepez 2016/11/15 22:31:17 Shouldn't multiplicaton be BIG_ENOUGH_PROMOTION?
jschuh 2016/11/15 22:38:49 It is, but in the intermediate representation for
+BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=, MAX_EXPONENT_PROMOTION)
+BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=, MAX_EXPONENT_PROMOTION)
#undef BASE_NUMERIC_ARITHMETIC_OPERATORS
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | base/numerics/safe_math_impl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698