Index: webrtc/rtc_base/mod_ops.h |
diff --git a/webrtc/rtc_base/mod_ops.h b/webrtc/rtc_base/mod_ops.h |
index 211f2e3553aa0e3672c872a53c6c04170e49cea4..f8cb951e834f5eb9e40052230540e31e6def7082 100644 |
--- a/webrtc/rtc_base/mod_ops.h |
+++ b/webrtc/rtc_base/mod_ops.h |
@@ -59,8 +59,10 @@ inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT |
// ################################################# |
// -->-----> |----->--- |
// |
+// If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the |
+// largest value representable by T. |
template <typename T, T M> |
-inline T ForwardDiff(T a, T b) { |
+inline typename std::enable_if<(M > 0), T>::type ForwardDiff(T a, T b) { |
static_assert(std::is_unsigned<T>::value, |
"Type must be an unsigned integer."); |
RTC_DCHECK_LT(a, M); |
@@ -68,13 +70,18 @@ inline T ForwardDiff(T a, T b) { |
return a <= b ? b - a : M - (a - b); |
} |
-template <typename T> |
-inline T ForwardDiff(T a, T b) { |
+template <typename T, T M> |
+inline typename std::enable_if<(M == 0), T>::type ForwardDiff(T a, T b) { |
static_assert(std::is_unsigned<T>::value, |
"Type must be an unsigned integer."); |
return b - a; |
} |
+template <typename T> |
+inline T ForwardDiff(T a, T b) { |
+ return ForwardDiff<T, 0>(a, b); |
+} |
+ |
// Calculates the reverse difference between two wrapping numbers. |
// |
// Example: |
@@ -97,8 +104,10 @@ inline T ForwardDiff(T a, T b) { |
// ################################################# |
// ---<-----| |<-----<-- |
// |
+// If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the |
+// largest value representable by T. |
template <typename T, T M> |
-inline T ReverseDiff(T a, T b) { |
+inline typename std::enable_if<(M > 0), T>::type ReverseDiff(T a, T b) { |
static_assert(std::is_unsigned<T>::value, |
"Type must be an unsigned integer."); |
RTC_DCHECK_LT(a, M); |
@@ -106,30 +115,28 @@ inline T ReverseDiff(T a, T b) { |
return b <= a ? a - b : M - (b - a); |
} |
-template <typename T> |
-inline T ReverseDiff(T a, T b) { |
+template <typename T, T M> |
+inline typename std::enable_if<(M == 0), T>::type ReverseDiff(T a, T b) { |
static_assert(std::is_unsigned<T>::value, |
"Type must be an unsigned integer."); |
return a - b; |
} |
+template <typename T> |
+inline T ReverseDiff(T a, T b) { |
+ return ReverseDiff<T, 0>(a, b); |
+} |
+ |
// Calculates the minimum distance between to wrapping numbers. |
// |
// The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b)) |
-template <typename T, T M> |
+template <typename T, T M = 0> |
inline T MinDiff(T a, T b) { |
static_assert(std::is_unsigned<T>::value, |
"Type must be an unsigned integer."); |
return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b)); |
} |
-template <typename T> |
-inline T MinDiff(T a, T b) { |
- static_assert(std::is_unsigned<T>::value, |
- "Type must be an unsigned integer."); |
- return std::min(ForwardDiff(a, b), ReverseDiff(a, b)); |
-} |
- |
} // namespace webrtc |
#endif // WEBRTC_RTC_BASE_MOD_OPS_H_ |