philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [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 | |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_ |
| 12 | #define RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_ |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <type_traits> |
| 17 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Karl Wiberg | 65c3922 | 2017-11-22 12:25:14 +0100 | [diff] [blame] | 20 | #include "rtc_base/numerics/mod_ops.h" |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | // Test if the sequence number |a| is ahead or at sequence number |b|. |
| 25 | // |
| 26 | // If |M| is an even number and the two sequence numbers are at max distance |
| 27 | // from each other, then the sequence number with the highest value is |
| 28 | // considered to be ahead. |
| 29 | template <typename T, T M> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 30 | inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 31 | static_assert(std::is_unsigned<T>::value, |
| 32 | "Type must be an unsigned integer."); |
| 33 | const T maxDist = M / 2; |
| 34 | if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist) |
| 35 | return b < a; |
| 36 | return ForwardDiff<T, M>(b, a) <= maxDist; |
| 37 | } |
| 38 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 39 | template <typename T, T M> |
| 40 | inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 41 | static_assert(std::is_unsigned<T>::value, |
| 42 | "Type must be an unsigned integer."); |
| 43 | const T maxDist = std::numeric_limits<T>::max() / 2 + T(1); |
| 44 | if (a - b == maxDist) |
| 45 | return b < a; |
| 46 | return ForwardDiff(b, a) < maxDist; |
| 47 | } |
| 48 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 49 | template <typename T> |
| 50 | inline bool AheadOrAt(T a, T b) { |
| 51 | return AheadOrAt<T, 0>(a, b); |
| 52 | } |
| 53 | |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 54 | // Test if the sequence number |a| is ahead of sequence number |b|. |
| 55 | // |
| 56 | // If |M| is an even number and the two sequence numbers are at max distance |
| 57 | // from each other, then the sequence number with the highest value is |
| 58 | // considered to be ahead. |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 59 | template <typename T, T M = 0> |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 60 | inline bool AheadOf(T a, T b) { |
| 61 | static_assert(std::is_unsigned<T>::value, |
| 62 | "Type must be an unsigned integer."); |
| 63 | return a != b && AheadOrAt<T, M>(a, b); |
| 64 | } |
| 65 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 66 | // Comparator used to compare sequence numbers in a continuous fashion. |
| 67 | // |
| 68 | // WARNING! If used to sort sequence numbers of length M then the interval |
| 69 | // covered by the sequence numbers may not be larger than floor(M/2). |
| 70 | template <typename T, T M = 0> |
| 71 | struct AscendingSeqNumComp { |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 72 | bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); } |
| 73 | }; |
| 74 | |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 75 | // Comparator used to compare sequence numbers in a continuous fashion. |
| 76 | // |
| 77 | // WARNING! If used to sort sequence numbers of length M then the interval |
| 78 | // covered by the sequence numbers may not be larger than floor(M/2). |
| 79 | template <typename T, T M = 0> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 80 | struct DescendingSeqNumComp { |
| 81 | bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); } |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 84 | // A sequencer number unwrapper where the start value of the unwrapped sequence |
| 85 | // can be set. The unwrapped value is not allowed to wrap. |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 86 | template <typename T, T M = 0> |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 87 | class SeqNumUnwrapper { |
brucedawson | 452ea0d | 2017-08-09 10:00:11 -0700 | [diff] [blame] | 88 | // Use '<' instead of rtc::SafeLt to avoid crbug.com/753488 |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 89 | static_assert( |
| 90 | std::is_unsigned<T>::value && |
brucedawson | 452ea0d | 2017-08-09 10:00:11 -0700 | [diff] [blame] | 91 | std::numeric_limits<T>::max() < std::numeric_limits<uint64_t>::max(), |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 92 | "Type unwrapped must be an unsigned integer smaller than uint64_t."); |
| 93 | |
| 94 | public: |
philipel | 3b3c9c4 | 2017-09-11 09:38:36 -0700 | [diff] [blame] | 95 | // We want a default value that is close to 2^62 for a two reasons. Firstly, |
| 96 | // we can unwrap wrapping numbers in either direction, and secondly, the |
| 97 | // unwrapped numbers can be stored in either int64_t or uint64_t. We also want |
| 98 | // the default value to be human readable, which makes a power of 10 suitable. |
| 99 | static constexpr uint64_t kDefaultStartValue = 1000000000000000000UL; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 100 | |
| 101 | SeqNumUnwrapper() : last_unwrapped_(kDefaultStartValue) {} |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 102 | explicit SeqNumUnwrapper(uint64_t start_at) : last_unwrapped_(start_at) {} |
| 103 | |
| 104 | uint64_t Unwrap(T value) { |
| 105 | if (!last_value_) |
| 106 | last_value_.emplace(value); |
| 107 | |
| 108 | uint64_t unwrapped = 0; |
| 109 | if (AheadOrAt<T, M>(value, *last_value_)) { |
| 110 | unwrapped = last_unwrapped_ + ForwardDiff<T, M>(*last_value_, value); |
| 111 | RTC_CHECK_GE(unwrapped, last_unwrapped_); |
| 112 | } else { |
| 113 | unwrapped = last_unwrapped_ - ReverseDiff<T, M>(*last_value_, value); |
| 114 | RTC_CHECK_LT(unwrapped, last_unwrapped_); |
| 115 | } |
| 116 | |
| 117 | *last_value_ = value; |
| 118 | last_unwrapped_ = unwrapped; |
| 119 | return last_unwrapped_; |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 120 | } |
philipel | 7956c0f | 2017-07-26 07:48:15 -0700 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | uint64_t last_unwrapped_; |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 124 | absl::optional<T> last_value_; |
philipel | 2cb7341 | 2016-03-22 10:03:43 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace webrtc |
| 128 | |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 129 | #endif // RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_ |