Index: webrtc/base/mod_ops.h |
diff --git a/webrtc/base/mod_ops.h b/webrtc/base/mod_ops.h |
index 9e649b75d21a78ff9de977167ed2cb0a3bf08253..ffda54ad8d1c3095d06b1f0e7aaf7bd425de1660 100644 |
--- a/webrtc/base/mod_ops.h |
+++ b/webrtc/base/mod_ops.h |
@@ -16,11 +16,6 @@ |
#include "webrtc/base/checks.h" |
-#define MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T) \ |
- static_assert(std::numeric_limits<T>::is_integer && \ |
- !std::numeric_limits<T>::is_signed, \ |
- "Type must be of unsigned integer.") |
- |
namespace webrtc { |
template <unsigned long M> // NOLINT |
@@ -42,7 +37,7 @@ inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT |
return a - sub; |
} |
-// Calculates the forward difference between two numbers. |
+// Calculates the forward difference between two wrapping numbers. |
// |
// Example: |
// uint8_t x = 253; |
@@ -64,13 +59,23 @@ inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT |
// ################################################# |
// -->-----> |----->--- |
// |
+template <typename T, T M> |
torbjorng (webrtc)
2016/03/11 14:54:22
Do we want these functions to work for e.g., T=uin
philipel
2016/03/11 16:04:38
The other version is still available so you can do
|
+inline T ForwardDiff(T a, T b) { |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
torbjorng (webrtc)
2016/03/11 14:54:22
Please make more idiomatic, e.g., "Type must be an
philipel
2016/03/11 16:04:38
Done.
|
+ RTC_DCHECK_LT(a, M); |
+ RTC_DCHECK_LT(b, M); |
+ return a <= b ? b - a : M - (a - b); |
+} |
+ |
template <typename T> |
inline T ForwardDiff(T a, T b) { |
- MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
return b - a; |
} |
-// Calculates the reverse difference between two numbers. |
+// Calculates the reverse difference between two wrapping numbers. |
// |
// Example: |
// uint8_t x = 253; |
@@ -92,9 +97,19 @@ inline T ForwardDiff(T a, T b) { |
// ################################################# |
// ---<-----| |<-----<-- |
// |
+template <typename T, T M> |
+inline T ReverseDiff(T a, T b) { |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
+ RTC_DCHECK_LT(a, M); |
+ RTC_DCHECK_LT(b, M); |
+ return b <= a ? a - b : M - (b - a); |
+} |
+ |
template <typename T> |
inline T ReverseDiff(T a, T b) { |
- MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
return a - b; |
} |
@@ -104,7 +119,8 @@ inline T ReverseDiff(T a, T b) { |
// be ahead. |
template <typename T> |
inline bool AheadOrAt(T a, T b) { |
- MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
const T maxDist = std::numeric_limits<T>::max() / 2 + T(1); |
if (a - b == maxDist) |
return b < a; |
@@ -114,7 +130,8 @@ inline bool AheadOrAt(T a, T b) { |
// Test if sequence number a is ahead of sequence number b. |
template <typename T> |
inline bool AheadOf(T a, T b) { |
- MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ static_assert(std::is_unsigned<T>::value, |
+ "Type must be of unsigned integer."); |
return a != b && AheadOrAt(a, b); |
} |