blob: cf65fdd4df828e193694aeb0f42143e8e339db6e [file] [log] [blame]
philipel5ab4c6d2016-03-08 03:36:15 -08001/*
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
philipel5ab4c6d2016-03-08 03:36:15 -080019namespace webrtc {
20
21template <unsigned long M> // NOLINT
22inline 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
31template <unsigned long M> // NOLINT
32inline 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
philipel4c83c052016-03-14 08:43:35 -070040// Calculates the forward difference between two wrapping numbers.
philipel5ab4c6d2016-03-08 03:36:15 -080041//
42// Example:
43// uint8_t x = 253;
44// uint8_t y = 2;
45//
46// ForwardDiff(x, y) == 4
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//
philipel4c83c052016-03-14 08:43:35 -070062template <typename T, T M>
63inline 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
philipel5ab4c6d2016-03-08 03:36:15 -080071template <typename T>
72inline T ForwardDiff(T a, T b) {
philipel4c83c052016-03-14 08:43:35 -070073 static_assert(std::is_unsigned<T>::value,
74 "Type must be an unsigned integer.");
philipel5ab4c6d2016-03-08 03:36:15 -080075 return b - a;
76}
77
philipel4c83c052016-03-14 08:43:35 -070078// Calculates the reverse difference between two wrapping numbers.
philipel5ab4c6d2016-03-08 03:36:15 -080079//
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//
philipel4c83c052016-03-14 08:43:35 -0700100template <typename T, T M>
101inline 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
philipel5ab4c6d2016-03-08 03:36:15 -0800109template <typename T>
110inline T ReverseDiff(T a, T b) {
philipel4c83c052016-03-14 08:43:35 -0700111 static_assert(std::is_unsigned<T>::value,
112 "Type must be an unsigned integer.");
philipel5ab4c6d2016-03-08 03:36:15 -0800113 return a - b;
114}
115
116// Test if the sequence number a is ahead or at sequence number b.
117// If the two sequence numbers are at max distance from each other
118// then the sequence number with highest value is considered to
119// be ahead.
120template <typename T>
121inline bool AheadOrAt(T a, T b) {
philipel4c83c052016-03-14 08:43:35 -0700122 static_assert(std::is_unsigned<T>::value,
123 "Type must be an unsigned integer.");
philipel5ab4c6d2016-03-08 03:36:15 -0800124 const T maxDist = std::numeric_limits<T>::max() / 2 + T(1);
125 if (a - b == maxDist)
126 return b < a;
127 return ForwardDiff(b, a) < maxDist;
128}
129
130// Test if sequence number a is ahead of sequence number b.
131template <typename T>
132inline bool AheadOf(T a, T b) {
philipel4c83c052016-03-14 08:43:35 -0700133 static_assert(std::is_unsigned<T>::value,
134 "Type must be an unsigned integer.");
philipel5ab4c6d2016-03-08 03:36:15 -0800135 return a != b && AheadOrAt(a, b);
136}
137
138} // namespace webrtc
139
140#endif // WEBRTC_BASE_MOD_OPS_H_