philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 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 | |
Karl Wiberg | 65c3922 | 2017-11-22 12:25:14 +0100 | [diff] [blame^] | 11 | #ifndef RTC_BASE_NUMERICS_MOD_OPS_H_ |
| 12 | #define RTC_BASE_NUMERICS_MOD_OPS_H_ |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 13 | |
Karl Wiberg | 65c3922 | 2017-11-22 12:25:14 +0100 | [diff] [blame^] | 14 | #include <algorithm> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <type_traits> |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | template <unsigned long M> // NOLINT |
| 23 | inline 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 | |
| 32 | template <unsigned long M> // NOLINT |
| 33 | inline 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 | // |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 63 | // If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the |
| 64 | // largest value representable by T. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | template <typename T, T M> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 66 | inline typename std::enable_if<(M > 0), T>::type ForwardDiff(T a, T b) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | 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 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 74 | template <typename T, T M> |
| 75 | inline typename std::enable_if<(M == 0), T>::type ForwardDiff(T a, T b) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 76 | static_assert(std::is_unsigned<T>::value, |
| 77 | "Type must be an unsigned integer."); |
| 78 | return b - a; |
| 79 | } |
| 80 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 81 | template <typename T> |
| 82 | inline T ForwardDiff(T a, T b) { |
| 83 | return ForwardDiff<T, 0>(a, b); |
| 84 | } |
| 85 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | // 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 | // |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 108 | // If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the |
| 109 | // largest value representable by T. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | template <typename T, T M> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 111 | inline typename std::enable_if<(M > 0), T>::type ReverseDiff(T a, T b) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 112 | 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 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 119 | template <typename T, T M> |
| 120 | inline typename std::enable_if<(M == 0), T>::type ReverseDiff(T a, T b) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 121 | static_assert(std::is_unsigned<T>::value, |
| 122 | "Type must be an unsigned integer."); |
| 123 | return a - b; |
| 124 | } |
| 125 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 126 | template <typename T> |
| 127 | inline T ReverseDiff(T a, T b) { |
| 128 | return ReverseDiff<T, 0>(a, b); |
| 129 | } |
| 130 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 131 | // Calculates the minimum distance between to wrapping numbers. |
| 132 | // |
| 133 | // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b)) |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 134 | template <typename T, T M = 0> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | inline 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | } // namespace webrtc |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 142 | |
Karl Wiberg | 65c3922 | 2017-11-22 12:25:14 +0100 | [diff] [blame^] | 143 | #endif // RTC_BASE_NUMERICS_MOD_OPS_H_ |