blob: 9a7361d353ff58cce432c29aa4000897641f0eb4 [file] [log] [blame]
philipel2cb73412016-03-22 10:03:43 +01001/*
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 Tereliusa194e582017-10-25 13:07:09 +020011#ifndef RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
12#define RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
philipel2cb73412016-03-22 10:03:43 +010013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
philipel2cb73412016-03-22 10:03:43 +010015#include <limits>
16#include <type_traits>
17
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020018#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/checks.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010020#include "rtc_base/numerics/mod_ops.h"
philipel2cb73412016-03-22 10:03:43 +010021
22namespace 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.
29template <typename T, T M>
philipel7956c0f2017-07-26 07:48:15 -070030inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010031 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
philipel7956c0f2017-07-26 07:48:15 -070039template <typename T, T M>
40inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010041 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
philipel7956c0f2017-07-26 07:48:15 -070049template <typename T>
50inline bool AheadOrAt(T a, T b) {
51 return AheadOrAt<T, 0>(a, b);
52}
53
philipel2cb73412016-03-22 10:03:43 +010054// 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.
philipel7956c0f2017-07-26 07:48:15 -070059template <typename T, T M = 0>
philipel2cb73412016-03-22 10:03:43 +010060inline 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
philipel7956c0f2017-07-26 07:48:15 -070066// 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).
70template <typename T, T M = 0>
71struct AscendingSeqNumComp {
philipel2cb73412016-03-22 10:03:43 +010072 bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); }
73};
74
philipel2cb73412016-03-22 10:03:43 +010075// 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).
79template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070080struct DescendingSeqNumComp {
81 bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); }
philipel2cb73412016-03-22 10:03:43 +010082};
83
philipel7956c0f2017-07-26 07:48:15 -070084// A sequencer number unwrapper where the start value of the unwrapped sequence
85// can be set. The unwrapped value is not allowed to wrap.
philipel2cb73412016-03-22 10:03:43 +010086template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070087class SeqNumUnwrapper {
brucedawson452ea0d2017-08-09 10:00:11 -070088 // Use '<' instead of rtc::SafeLt to avoid crbug.com/753488
philipel7956c0f2017-07-26 07:48:15 -070089 static_assert(
90 std::is_unsigned<T>::value &&
brucedawson452ea0d2017-08-09 10:00:11 -070091 std::numeric_limits<T>::max() < std::numeric_limits<uint64_t>::max(),
philipel7956c0f2017-07-26 07:48:15 -070092 "Type unwrapped must be an unsigned integer smaller than uint64_t.");
93
94 public:
philipel3b3c9c42017-09-11 09:38:36 -070095 // 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;
philipeld4fac692017-09-04 07:03:46 -0700100
101 SeqNumUnwrapper() : last_unwrapped_(kDefaultStartValue) {}
philipel7956c0f2017-07-26 07:48:15 -0700102 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_;
philipel2cb73412016-03-22 10:03:43 +0100120 }
philipel7956c0f2017-07-26 07:48:15 -0700121
122 private:
123 uint64_t last_unwrapped_;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200124 absl::optional<T> last_value_;
philipel2cb73412016-03-22 10:03:43 +0100125};
126
127} // namespace webrtc
128
Bjorn Tereliusa194e582017-10-25 13:07:09 +0200129#endif // RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_