OLD | NEW |
(Empty) | |
| 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 |
| 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 |
| 19 #define MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T) \ |
| 20 static_assert(std::numeric_limits<T>::is_integer && \ |
| 21 !std::numeric_limits<T>::is_signed, \ |
| 22 "ModNum type must be of unsigned integer.") |
| 23 |
| 24 namespace webrtc { |
| 25 |
| 26 template <uint64_t M> |
| 27 inline uint64_t Add(uint64_t a, uint64_t b) { |
| 28 RTC_DCHECK_LT(a, M); |
| 29 uint64_t t = M - b % M; |
| 30 uint64_t res = a - t; |
| 31 if (t > a) |
| 32 return res + M; |
| 33 return res; |
| 34 } |
| 35 |
| 36 template <uint64_t M> |
| 37 inline uint64_t Subtract(uint64_t a, uint64_t b) { |
| 38 RTC_DCHECK_LT(a, M); |
| 39 uint64_t sub = b % M; |
| 40 if (a < sub) |
| 41 return M - (sub - a); |
| 42 return a - sub; |
| 43 } |
| 44 |
| 45 // Calculates the forward difference between two numbers. |
| 46 // |
| 47 // Example: |
| 48 // uint8_t a = 253; |
| 49 // uint8_t b = 2; |
| 50 // |
| 51 // ForwardDiff(a, b) == 4 |
| 52 // |
| 53 // 252 253 254 255 0 1 2 3 |
| 54 // ################################################# |
| 55 // | | a | | | | | b | | |
| 56 // ################################################# |
| 57 // |----->----->----->----->-----> |
| 58 // |
| 59 // ForwardDiff(b, a) == 251 |
| 60 // |
| 61 // 252 253 254 255 0 1 2 3 |
| 62 // ################################################# |
| 63 // | | a | | | | | b | | |
| 64 // ################################################# |
| 65 // -->-----> |----->--- |
| 66 // |
| 67 template <typename T> |
| 68 inline T ForwardDiff(T a, T b) { |
| 69 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
| 70 return a <= b ? b - a : std::numeric_limits<T>::max() - (a - b); |
| 71 } |
| 72 |
| 73 // Calculates the reverse difference between two numbers. |
| 74 // |
| 75 // Example: |
| 76 // uint8_t a = 253; |
| 77 // uint8_t b = 2; |
| 78 // |
| 79 // ReverseDiff(a, b) == 251 |
| 80 // |
| 81 // 252 253 254 255 0 1 2 3 |
| 82 // ################################################# |
| 83 // | | a | | | | | b | | |
| 84 // ################################################# |
| 85 // <-----<-----<-----<-----<-----| |
| 86 // |
| 87 // ReverseDiff(b, a) == 5 |
| 88 // |
| 89 // 252 253 254 255 0 1 2 3 |
| 90 // ################################################# |
| 91 // | | a | | | | | b | | |
| 92 // ################################################# |
| 93 // ---<-----| |<-----<-- |
| 94 // |
| 95 template <typename T> |
| 96 inline T ReverseDiff(T a, T b) { |
| 97 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
| 98 return a < b ? std::numeric_limits<T>::max() - (b - a) : a - b; |
| 99 } |
| 100 |
| 101 template <typename T> |
| 102 inline bool AheadOrAt(T a, T b) { |
| 103 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
| 104 return ForwardDiff(b, a) < std::numeric_limits<T>::max() / 2; |
| 105 } |
| 106 |
| 107 template <typename T> |
| 108 inline bool AheadOf(T a, T b) { |
| 109 MOD_OPS_ASSERT_TYPE_IS_UNSIGNED(T); |
| 110 return a != b && AheadOrAt(a, b); |
| 111 } |
| 112 |
| 113 } // namespace webrtc |
| 114 |
| 115 #endif // WEBRTC_BASE_MOD_OPS_H_ |
OLD | NEW |