Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: webrtc/base/mod_ops.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/messagequeue_unittest.cc ('k') | webrtc/base/mod_ops_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_MOD_OPS_H_ 11 #ifndef WEBRTC_BASE_MOD_OPS_H_
12 #define WEBRTC_BASE_MOD_OPS_H_ 12 #define WEBRTC_BASE_MOD_OPS_H_
13 13
14 #include <limits>
15 #include <type_traits>
16 14
17 #include "webrtc/base/checks.h" 15 // This header is deprecated and is just left here temporarily during
18 16 // refactoring. See https://bugs.webrtc.org/7634 for more details.
19 namespace webrtc { 17 #include "webrtc/rtc_base/mod_ops.h"
20
21 template <unsigned long M> // NOLINT
22 inline unsigned long Add(unsigned long a, unsigned long b) { // NOLINT
23 RTC_DCHECK_LT(a, M);
24 unsigned long t = M - b % M; // NOLINT
25 unsigned long res = a - t; // NOLINT
26 if (t > a)
27 return res + M;
28 return res;
29 }
30
31 template <unsigned long M> // NOLINT
32 inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT
33 RTC_DCHECK_LT(a, M);
34 unsigned long sub = b % M; // NOLINT
35 if (a < sub)
36 return M - (sub - a);
37 return a - sub;
38 }
39
40 // Calculates the forward difference between two wrapping numbers.
41 //
42 // Example:
43 // uint8_t x = 253;
44 // uint8_t y = 2;
45 //
46 // ForwardDiff(x, y) == 5
47 //
48 // 252 253 254 255 0 1 2 3
49 // #################################################
50 // | | x | | | | | y | |
51 // #################################################
52 // |----->----->----->----->----->
53 //
54 // ForwardDiff(y, x) == 251
55 //
56 // 252 253 254 255 0 1 2 3
57 // #################################################
58 // | | x | | | | | y | |
59 // #################################################
60 // -->-----> |----->---
61 //
62 template <typename T, T M>
63 inline T ForwardDiff(T a, T b) {
64 static_assert(std::is_unsigned<T>::value,
65 "Type must be an unsigned integer.");
66 RTC_DCHECK_LT(a, M);
67 RTC_DCHECK_LT(b, M);
68 return a <= b ? b - a : M - (a - b);
69 }
70
71 template <typename T>
72 inline T ForwardDiff(T a, T b) {
73 static_assert(std::is_unsigned<T>::value,
74 "Type must be an unsigned integer.");
75 return b - a;
76 }
77
78 // Calculates the reverse difference between two wrapping numbers.
79 //
80 // Example:
81 // uint8_t x = 253;
82 // uint8_t y = 2;
83 //
84 // ReverseDiff(y, x) == 5
85 //
86 // 252 253 254 255 0 1 2 3
87 // #################################################
88 // | | x | | | | | y | |
89 // #################################################
90 // <-----<-----<-----<-----<-----|
91 //
92 // ReverseDiff(x, y) == 251
93 //
94 // 252 253 254 255 0 1 2 3
95 // #################################################
96 // | | x | | | | | y | |
97 // #################################################
98 // ---<-----| |<-----<--
99 //
100 template <typename T, T M>
101 inline T ReverseDiff(T a, T b) {
102 static_assert(std::is_unsigned<T>::value,
103 "Type must be an unsigned integer.");
104 RTC_DCHECK_LT(a, M);
105 RTC_DCHECK_LT(b, M);
106 return b <= a ? a - b : M - (b - a);
107 }
108
109 template <typename T>
110 inline T ReverseDiff(T a, T b) {
111 static_assert(std::is_unsigned<T>::value,
112 "Type must be an unsigned integer.");
113 return a - b;
114 }
115
116 // Calculates the minimum distance between to wrapping numbers.
117 //
118 // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b))
119 template <typename T, T M>
120 inline T MinDiff(T a, T b) {
121 static_assert(std::is_unsigned<T>::value,
122 "Type must be an unsigned integer.");
123 return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b));
124 }
125
126 template <typename T>
127 inline T MinDiff(T a, T b) {
128 static_assert(std::is_unsigned<T>::value,
129 "Type must be an unsigned integer.");
130 return std::min(ForwardDiff(a, b), ReverseDiff(a, b));
131 }
132
133 } // namespace webrtc
134 18
135 #endif // WEBRTC_BASE_MOD_OPS_H_ 19 #endif // WEBRTC_BASE_MOD_OPS_H_
OLDNEW
« no previous file with comments | « webrtc/base/messagequeue_unittest.cc ('k') | webrtc/base/mod_ops_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698