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 <unsigned long M> // NOLINT | |
27 inline unsigned long Add(unsigned long a, unsigned long b) { // NOLINT | |
28 RTC_DCHECK_LT(a, M); | |
29 unsigned long t = M - b % M; // NOLINT | |
30 unsigned long res = a - t; // NOLINT | |
31 if (t > a) | |
32 return res + M; | |
33 return res; | |
34 } | |
35 | |
36 template <unsigned long M> // NOLINT | |
37 inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT | |
38 RTC_DCHECK_LT(a, M); // NOLINT | |
torbjorng (webrtc)
2016/03/02 16:20:50
Nit: this NOLINT seems unneeded.
philipel
2016/03/03 12:21:36
Done.
| |
39 unsigned long sub = b % M; // NOLINT | |
40 if (a < sub) | |
41 return M - (sub - a); | |
42 return a - sub; | |
43 } | |
44 | |
45 // Calculates the forward difference between two numbers. | |
46 // | |
47 // Example: | |
48 // uint8_t a = 253; | |
49 // uint8_t b = 2; | |
50 // | |
51 // ForwardDiff(a, b) == 4 | |
52 // | |
53 // 252 253 254 255 0 1 2 3 | |
54 // ################################################# | |
55 // | | a | | | | | b | | | |
56 // ################################################# | |
57 // |----->----->----->----->-----> | |
58 // | |
59 // ForwardDiff(b, a) == 251 | |
60 // | |
61 // 252 253 254 255 0 1 2 3 | |
62 // ################################################# | |
63 // | | a | | | | | b | | | |
64 // ################################################# | |
65 // -->-----> |----->--- | |
66 // | |
67 template <typename T> | |
68 inline T ForwardDiff(T a, T b) { | |
torbjorng (webrtc)
2016/03/02 16:20:50
See comment about ReverseDiff.
philipel
2016/03/03 12:21:36
Done.
| |
69 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
70 return a <= b ? b - a : std::numeric_limits<T>::max() - (a - b); | |
71 } | |
72 | |
73 // Calculates the reverse difference between two numbers. | |
74 // | |
75 // Example: | |
76 // uint8_t a = 253; | |
77 // uint8_t b = 2; | |
78 // | |
79 // ReverseDiff(a, b) == 251 | |
80 // | |
81 // 252 253 254 255 0 1 2 3 | |
82 // ################################################# | |
83 // | | a | | | | | b | | | |
84 // ################################################# | |
85 // <-----<-----<-----<-----<-----| | |
86 // | |
87 // ReverseDiff(b, a) == 5 | |
88 // | |
89 // 252 253 254 255 0 1 2 3 | |
90 // ################################################# | |
91 // | | a | | | | | b | | | |
92 // ################################################# | |
93 // ---<-----| |<-----<-- | |
94 // | |
95 template <typename T> | |
torbjorng (webrtc)
2016/03/02 16:20:50
What's the intended range here? Presumably 0..255
philipel
2016/03/03 12:21:36
I have now unstupidified the code and implemented
| |
96 inline T ReverseDiff(T a, T b) { | |
97 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
98 return a < b ? std::numeric_limits<T>::max() - (b - a) : a - b; | |
99 } | |
100 | |
101 template <typename T> | |
torbjorng (webrtc)
2016/03/02 16:20:50
The criteria for "ahead of" is a bit arbitrary. Pl
philipel
2016/03/03 12:21:36
Done.
| |
102 inline bool AheadOrAt(T a, T b) { | |
103 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
104 return ForwardDiff(b, a) < std::numeric_limits<T>::max() / 2; | |
105 } | |
106 | |
107 template <typename T> | |
108 inline bool AheadOf(T a, T b) { | |
109 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); | |
110 return a != b && AheadOrAt(a, b); | |
111 } | |
112 | |
113 } // namespace webrtc | |
114 | |
115 #endif // WEBRTC_BASE_MOD_OPS_H_ | |
OLD | NEW |