OLD | NEW |
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 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 return b <= a ? a - b : M - (b - a); | 106 return b <= a ? a - b : M - (b - a); |
107 } | 107 } |
108 | 108 |
109 template <typename T> | 109 template <typename T> |
110 inline T ReverseDiff(T a, T b) { | 110 inline T ReverseDiff(T a, T b) { |
111 static_assert(std::is_unsigned<T>::value, | 111 static_assert(std::is_unsigned<T>::value, |
112 "Type must be an unsigned integer."); | 112 "Type must be an unsigned integer."); |
113 return a - b; | 113 return a - b; |
114 } | 114 } |
115 | 115 |
116 // Test if the sequence number a is ahead or at sequence number b. | 116 // Calculates the minimum distance between to wrapping numbers. |
117 // If the two sequence numbers are at max distance from each other | 117 // |
118 // then the sequence number with highest value is considered to | 118 // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b)) |
119 // be ahead. | 119 template <typename T, T M> |
120 template <typename T> | 120 inline T MinDiff(T a, T b) { |
121 inline bool AheadOrAt(T a, T b) { | |
122 static_assert(std::is_unsigned<T>::value, | 121 static_assert(std::is_unsigned<T>::value, |
123 "Type must be an unsigned integer."); | 122 "Type must be an unsigned integer."); |
124 const T maxDist = std::numeric_limits<T>::max() / 2 + T(1); | 123 return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b)); |
125 if (a - b == maxDist) | |
126 return b < a; | |
127 return ForwardDiff(b, a) < maxDist; | |
128 } | 124 } |
129 | 125 |
130 // Test if sequence number a is ahead of sequence number b. | |
131 template <typename T> | 126 template <typename T> |
132 inline bool AheadOf(T a, T b) { | 127 inline T MinDiff(T a, T b) { |
133 static_assert(std::is_unsigned<T>::value, | 128 static_assert(std::is_unsigned<T>::value, |
134 "Type must be an unsigned integer."); | 129 "Type must be an unsigned integer."); |
135 return a != b && AheadOrAt(a, b); | 130 return std::min(ForwardDiff(a, b), ReverseDiff(a, b)); |
136 } | 131 } |
137 | 132 |
138 } // namespace webrtc | 133 } // namespace webrtc |
139 | 134 |
140 #endif // WEBRTC_BASE_MOD_OPS_H_ | 135 #endif // WEBRTC_BASE_MOD_OPS_H_ |
OLD | NEW |