Björn Terelius | fe903d5 | 2021-09-27 16:30:44 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ |
| 12 | #define LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ |
| 13 | |
Björn Terelius | ac4e0b6 | 2024-05-28 12:17:46 | [diff] [blame] | 14 | #include <cstdint> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
Björn Terelius | ac4e0b6 | 2024-05-28 12:17:46 | [diff] [blame] | 16 | |
Björn Terelius | fe903d5 | 2021-09-27 16:30:44 | [diff] [blame] | 17 | #include "api/array_view.h" |
| 18 | #include "logging/rtc_event_log/events/rtc_event_field_extraction.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Parameters for fixed-size delta-encoding/decoding. |
| 23 | // These are tailored for the sequence which will be encoded (e.g. widths). |
| 24 | class FixedLengthEncodingParametersV3 final { |
| 25 | public: |
| 26 | static bool ValidParameters(uint64_t delta_bit_width, |
| 27 | bool signed_deltas, |
| 28 | bool values_optional, |
| 29 | uint64_t value_bit_width) { |
| 30 | return (1 <= delta_bit_width && delta_bit_width <= 64 && |
| 31 | 1 <= value_bit_width && value_bit_width <= 64 && |
| 32 | (delta_bit_width <= value_bit_width || |
| 33 | (signed_deltas && delta_bit_width == 64))); |
| 34 | } |
| 35 | |
| 36 | static FixedLengthEncodingParametersV3 CalculateParameters( |
| 37 | uint64_t base, |
Ali Tofigh | 1e157a9 | 2022-01-31 10:08:24 | [diff] [blame] | 38 | rtc::ArrayView<const uint64_t> values, |
Björn Terelius | fe903d5 | 2021-09-27 16:30:44 | [diff] [blame] | 39 | uint64_t value_bit_width, |
| 40 | bool values_optional); |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 41 | static std::optional<FixedLengthEncodingParametersV3> ParseDeltaHeader( |
Björn Terelius | fe903d5 | 2021-09-27 16:30:44 | [diff] [blame] | 42 | uint64_t header, |
| 43 | uint64_t value_bit_width); |
| 44 | |
| 45 | uint64_t DeltaHeaderAsInt() const; |
| 46 | |
| 47 | // Number of bits necessary to hold the widest(*) of the deltas between the |
| 48 | // values in the sequence. |
| 49 | // (*) - Widest might not be the largest, if signed deltas are used. |
| 50 | uint64_t delta_bit_width() const { return delta_bit_width_; } |
| 51 | |
| 52 | // Whether deltas are signed. |
| 53 | bool signed_deltas() const { return signed_deltas_; } |
| 54 | |
| 55 | // Whether the values of the sequence are optional. That is, it may be |
| 56 | // that some of them do not have a value (not even a sentinel value indicating |
| 57 | // invalidity). |
| 58 | bool values_optional() const { return values_optional_; } |
| 59 | |
| 60 | // Whether all values are equal. 64-bit signed deltas are assumed to not |
| 61 | // occur, since those could equally well be represented using 64 bit unsigned |
| 62 | // deltas. |
| 63 | bool values_equal() const { |
| 64 | return delta_bit_width() == 64 && signed_deltas(); |
| 65 | } |
| 66 | |
| 67 | // Number of bits necessary to hold the largest value in the sequence. |
| 68 | uint64_t value_bit_width() const { return value_bit_width_; } |
| 69 | |
| 70 | // Masks where only the bits relevant to the deltas/values are turned on. |
| 71 | uint64_t delta_mask() const { return delta_mask_; } |
| 72 | uint64_t value_mask() const { return value_mask_; } |
| 73 | |
| 74 | private: |
| 75 | FixedLengthEncodingParametersV3(uint64_t delta_bit_width, |
| 76 | bool signed_deltas, |
| 77 | bool values_optional, |
| 78 | uint64_t value_bit_width) |
| 79 | : delta_bit_width_(delta_bit_width), |
| 80 | signed_deltas_(signed_deltas), |
| 81 | values_optional_(values_optional), |
| 82 | value_bit_width_(value_bit_width), |
| 83 | delta_mask_( |
| 84 | webrtc_event_logging::MaxUnsignedValueOfBitWidth(delta_bit_width_)), |
| 85 | value_mask_(webrtc_event_logging::MaxUnsignedValueOfBitWidth( |
| 86 | value_bit_width_)) {} |
| 87 | |
| 88 | uint64_t delta_bit_width_; |
| 89 | bool signed_deltas_; |
| 90 | bool values_optional_; |
| 91 | uint64_t value_bit_width_; |
| 92 | |
| 93 | uint64_t delta_mask_; |
| 94 | uint64_t value_mask_; |
| 95 | }; |
| 96 | |
| 97 | } // namespace webrtc |
| 98 | #endif // LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ |