Index: webrtc/base/mod_ops.h |
diff --git a/webrtc/base/mod_ops.h b/webrtc/base/mod_ops.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6bac07a2ef8d5e307ab9e5e2863fcd11666fc1d4 |
--- /dev/null |
+++ b/webrtc/base/mod_ops.h |
@@ -0,0 +1,62 @@ |
+/* |
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#ifndef WEBRTC_BASE_MOD_OPS_H_ |
+#define WEBRTC_BASE_MOD_OPS_H_ |
+ |
+#include <limits> |
+#include <type_traits> |
+ |
+#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, \ |
+ "ModNum type must be of unsigned integer.") |
+ |
+namespace webrtc { |
+ |
+template <size_t M> inline size_t Add(size_t a , size_t b) { |
+ size_t add = b % M; |
+ if (a + add < a) |
+ return M - a + add; |
+ return (a + add) % M; |
+} |
+ |
+template <size_t M> inline size_t Subtract(size_t a, size_t b) { |
+ size_t sub = b % M; |
+ if (a < sub) |
+ return M - (sub - a); |
+ return a - sub; |
+} |
+ |
+template <typename T> inline T PositiveDiff(T a, T b) { |
+ MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ return a <= b ? b - a : std::numeric_limits<T>::max() - (a - b); |
+} |
+ |
+template <typename T> inline T NegativeDiff(T a, T b) { |
+ MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ return a < b ? std::numeric_limits<T>::max() - (b - a) : a - b; |
+} |
+ |
+template <typename T> inline bool AheadOrAt(T a, T b) { |
+ MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ return PositiveDiff(b, a) < std::numeric_limits<T>::max() / 2; |
+} |
+ |
+template <typename T> inline bool AheadOf(T a, T b) { |
+ MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
+ return a != b && AheadOrAt(a, b); |
+} |
+ |
+} // namespace webrtc |
+ |
+#endif // WEBRTC_BASE_MOD_OPS_H_ |