blob: 90d3ed804f8098cbfef9fb9c3ca431ded73ceb24 [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
Karl Wiberg65c39222017-11-22 12:25:14 +010011#ifndef RTC_BASE_NUMERICS_MOD_OPS_H_
12#define RTC_BASE_NUMERICS_MOD_OPS_H_
philipel5ab4c6d2016-03-08 03:36:15 -080013
Karl Wiberg65c39222017-11-22 12:25:14 +010014#include <algorithm>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <limits>
16#include <type_traits>
philipel5ab4c6d2016-03-08 03:36:15 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace webrtc {
21
22template <unsigned long M> // NOLINT
23inline unsigned long Add(unsigned long a, unsigned long b) { // NOLINT
24 RTC_DCHECK_LT(a, M);
25 unsigned long t = M - b % M; // NOLINT
26 unsigned long res = a - t; // NOLINT
27 if (t > a)
28 return res + M;
29 return res;
30}
31
32template <unsigned long M> // NOLINT
33inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT
34 RTC_DCHECK_LT(a, M);
35 unsigned long sub = b % M; // NOLINT
36 if (a < sub)
37 return M - (sub - a);
38 return a - sub;
39}
40
41// Calculates the forward difference between two wrapping numbers.
42//
43// Example:
44// uint8_t x = 253;
45// uint8_t y = 2;
46//
47// ForwardDiff(x, y) == 5
48//
49// 252 253 254 255 0 1 2 3
50// #################################################
51// | | x | | | | | y | |
52// #################################################
53// |----->----->----->----->----->
54//
55// ForwardDiff(y, x) == 251
56//
57// 252 253 254 255 0 1 2 3
58// #################################################
59// | | x | | | | | y | |
60// #################################################
61// -->-----> |----->---
62//
philipel7956c0f2017-07-26 07:48:15 -070063// If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the
64// largest value representable by T.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065template <typename T, T M>
philipel7956c0f2017-07-26 07:48:15 -070066inline typename std::enable_if<(M > 0), T>::type ForwardDiff(T a, T b) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067 static_assert(std::is_unsigned<T>::value,
68 "Type must be an unsigned integer.");
69 RTC_DCHECK_LT(a, M);
70 RTC_DCHECK_LT(b, M);
71 return a <= b ? b - a : M - (a - b);
72}
73
philipel7956c0f2017-07-26 07:48:15 -070074template <typename T, T M>
75inline typename std::enable_if<(M == 0), T>::type ForwardDiff(T a, T b) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 static_assert(std::is_unsigned<T>::value,
77 "Type must be an unsigned integer.");
78 return b - a;
79}
80
philipel7956c0f2017-07-26 07:48:15 -070081template <typename T>
82inline T ForwardDiff(T a, T b) {
83 return ForwardDiff<T, 0>(a, b);
84}
85
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086// Calculates the reverse difference between two wrapping numbers.
87//
88// Example:
89// uint8_t x = 253;
90// uint8_t y = 2;
91//
92// ReverseDiff(y, x) == 5
93//
94// 252 253 254 255 0 1 2 3
95// #################################################
96// | | x | | | | | y | |
97// #################################################
98// <-----<-----<-----<-----<-----|
99//
100// ReverseDiff(x, y) == 251
101//
102// 252 253 254 255 0 1 2 3
103// #################################################
104// | | x | | | | | y | |
105// #################################################
106// ---<-----| |<-----<--
107//
philipel7956c0f2017-07-26 07:48:15 -0700108// If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the
109// largest value representable by T.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110template <typename T, T M>
philipel7956c0f2017-07-26 07:48:15 -0700111inline typename std::enable_if<(M > 0), T>::type ReverseDiff(T a, T b) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 static_assert(std::is_unsigned<T>::value,
113 "Type must be an unsigned integer.");
114 RTC_DCHECK_LT(a, M);
115 RTC_DCHECK_LT(b, M);
116 return b <= a ? a - b : M - (b - a);
117}
118
philipel7956c0f2017-07-26 07:48:15 -0700119template <typename T, T M>
120inline typename std::enable_if<(M == 0), T>::type ReverseDiff(T a, T b) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 static_assert(std::is_unsigned<T>::value,
122 "Type must be an unsigned integer.");
123 return a - b;
124}
125
philipel7956c0f2017-07-26 07:48:15 -0700126template <typename T>
127inline T ReverseDiff(T a, T b) {
128 return ReverseDiff<T, 0>(a, b);
129}
130
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131// Calculates the minimum distance between to wrapping numbers.
132//
133// The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b))
philipel7956c0f2017-07-26 07:48:15 -0700134template <typename T, T M = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135inline T MinDiff(T a, T b) {
136 static_assert(std::is_unsigned<T>::value,
137 "Type must be an unsigned integer.");
138 return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b));
139}
140
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141} // namespace webrtc
philipel5ab4c6d2016-03-08 03:36:15 -0800142
Karl Wiberg65c39222017-11-22 12:25:14 +0100143#endif // RTC_BASE_NUMERICS_MOD_OPS_H_