Chromium Code Reviews| 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..3cf2104fd5950a900b0619560eb50461a60d17d7 |
| --- /dev/null |
| +++ b/webrtc/base/mod_ops.h |
| @@ -0,0 +1,114 @@ |
| +/* |
| + * 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 <uint64_t M> |
| +inline uint64_t Add(uint64_t a, uint64_t b) { |
| + RTC_DCHECK_LT(a, M); |
|
terelius
2016/03/01 12:27:01
Do you really want to enforce a<=M? I though the p
philipel
2016/03/01 13:31:08
The idea with these functions is that one can more
|
| + uint64_t add = b % M; |
| + if (a + add < a) |
| + return a + add - M; |
| + return (a + add) % M; |
| +} |
| + |
| +template <uint64_t M> |
| +inline uint64_t Subtract(uint64_t a, uint64_t b) { |
| + RTC_DCHECK_LT(a, M); |
| + uint64_t sub = b % M; |
| + if (a < sub) |
| + return M - (sub - a); |
| + return a - sub; |
| +} |
| + |
| +// Calculates the forward difference between two numbers. |
| +// |
| +// Example: |
| +// uint8_t a = 253; |
| +// uint8_t b = 2; |
| +// |
| +// ForwardDiff(a, b) == 4 |
| +// |
| +// 252 253 254 255 0 1 2 3 |
| +// ################################################# |
| +// | | a | | | | | b | | |
| +// ################################################# |
| +// |----->----->----->----->-----> |
| +// |
| +// ForwardDiff(b, a) == 251 |
| +// |
| +// 252 253 254 255 0 1 2 3 |
| +// ################################################# |
| +// | | a | | | | | b | | |
| +// ################################################# |
| +// -->-----> |----->--- |
| +// |
| +template <typename T> |
| +inline T ForwardDiff(T a, T b) { |
| + MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
| + return a <= b ? b - a : std::numeric_limits<T>::max() - (a - b); |
| +} |
| + |
| +// Calculates the reverse difference between two numbers. |
| +// |
| +// Example: |
| +// uint8_t a = 253; |
| +// uint8_t b = 2; |
| +// |
| +// ReverseDiff(a, b) == 251 |
| +// |
| +// 252 253 254 255 0 1 2 3 |
| +// ################################################# |
| +// | | a | | | | | b | | |
| +// ################################################# |
| +// <-----<-----<-----<-----<-----| |
| +// |
| +// ReverseDiff(b, a) == 5 |
| +// |
| +// 252 253 254 255 0 1 2 3 |
| +// ################################################# |
| +// | | a | | | | | b | | |
| +// ################################################# |
| +// ---<-----| |<-----<-- |
| +// |
| +template <typename T> |
| +inline T ReverseDiff(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 ForwardDiff(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_ |