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..da92578234ab2d6c9bdd674fad336ba12189a166 |
| --- /dev/null |
| +++ b/webrtc/base/mod_ops.h |
| @@ -0,0 +1,107 @@ |
| +/* |
| + * 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) { |
|
torbjorng (webrtc)
2016/02/29 17:23:46
Why use size_t and not T? Using T seems cleaner, b
philipel
2016/03/01 09:29:34
Having to specify the type makes it less practical
torbjorng (webrtc)
2016/03/01 14:13:35
On 32-bit machines, uint64_t is a quite poor choic
|
| + size_t add = b % M; |
| + if (a + add < a) |
| + return M - a + add; |
|
torbjorng (webrtc)
2016/02/29 17:23:46
This looks incorrect, a should not be subtracted a
philipel
2016/03/01 09:29:34
Yepp, this implementation is clearly wrong. A was
torbjorng (webrtc)
2016/03/01 14:13:35
Whether a principal residue or not, that code was
philipel
2016/03/01 15:28:38
Reading my own comment in hindsight I realize I wa
|
| + 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; |
|
torbjorng (webrtc)
2016/02/29 17:23:46
Note that if a is not a principal residue (mod M)
philipel
2016/03/01 09:29:34
True.
|
| +} |
| + |
| + |
| +// 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_ |