OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_BASE_MOD_OPS_H_ | |
12 #define WEBRTC_BASE_MOD_OPS_H_ | |
13 | |
14 #include <limits> | |
15 #include <type_traits> | |
16 | |
17 #include "webrtc/base/checks.h" | |
18 | |
19 #define MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T) \ | |
20 static_assert(std::numeric_limits<T>::is_integer && \ | |
21 !std::numeric_limits<T>::is_signed, \ | |
22 "ModNum type must be of unsigned integer.") | |
23 | |
24 namespace webrtc { | |
25 | |
26 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
| |
27 size_t add = b % M; | |
28 if (a + add < a) | |
29 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
| |
30 return (a + add) % M; | |
31 } | |
32 | |
33 template <size_t M> inline size_t Subtract(size_t a, size_t b) { | |
34 size_t sub = b % M; | |
35 if (a < sub) | |
36 return M - (sub - a); | |
37 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.
| |
38 } | |
39 | |
40 | |
41 // Calculates the forward difference between two numbers. | |
42 // | |
43 // Example: | |
44 // uint8_t a = 253; | |
45 // uint8_t b = 2; | |
46 // | |
47 // ForwardDiff(a, b) == 4 | |
48 // | |
49 // 252 253 254 255 0 1 2 3 | |
50 // ################################################# | |
51 // | | a | | | | | b | | | |
52 // ################################################# | |
53 // |----->----->----->----->-----> | |
54 // | |
55 // ForwardDiff(b, a) == 251 | |
56 // | |
57 // 252 253 254 255 0 1 2 3 | |
58 // ################################################# | |
59 // | | a | | | | | b | | | |
60 // ################################################# | |
61 // -->-----> |----->--- | |
62 // | |
63 template <typename T> inline T ForwardDiff(T a, T b) { | |
64 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
65 return a <= b ? b - a : std::numeric_limits<T>::max() - (a - b); | |
66 } | |
67 | |
68 // Calculates the reverse difference between two numbers. | |
69 // | |
70 // Example: | |
71 // uint8_t a = 253; | |
72 // uint8_t b = 2; | |
73 // | |
74 // ReverseDiff(a, b) == 251 | |
75 // | |
76 // 252 253 254 255 0 1 2 3 | |
77 // ################################################# | |
78 // | | a | | | | | b | | | |
79 // ################################################# | |
80 // <-----<-----<-----<-----<-----| | |
81 // | |
82 // ReverseDiff(b, a) == 5 | |
83 // | |
84 // 252 253 254 255 0 1 2 3 | |
85 // ################################################# | |
86 // | | a | | | | | b | | | |
87 // ################################################# | |
88 // ---<-----| |<-----<-- | |
89 // | |
90 template <typename T> inline T ReverseDiff(T a, T b) { | |
91 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
92 return a < b ? std::numeric_limits<T>::max() - (b - a) : a - b; | |
93 } | |
94 | |
95 template <typename T> inline bool AheadOrAt(T a, T b) { | |
96 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
97 return ForwardDiff(b, a) < std::numeric_limits<T>::max() / 2; | |
98 } | |
99 | |
100 template <typename T> inline bool AheadOf(T a, T b) { | |
101 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
102 return a != b && AheadOrAt(a, b); | |
103 } | |
104 | |
105 } // namespace webrtc | |
106 | |
107 #endif // WEBRTC_BASE_MOD_OPS_H_ | |
OLD | NEW |