55 #ifndef PSTORE_SUPPORT_VARINT_HPP 56 #define PSTORE_SUPPORT_VARINT_HPP 66 constexpr
unsigned encoded_size (std::uint64_t
const x) noexcept {
69 constexpr
auto nine_byte_threshold = (UINT64_C (1) << (7U * 8U)) - 1U;
70 if (x > nine_byte_threshold) {
76 unsigned const bits = 64U - bit_count::clz (x | 1U);
77 return (bits - 1U) / 7U + 1U;
80 template <
typename OutputIterator>
81 OutputIterator encode (std::uint64_t x, OutputIterator out) {
82 unsigned const bits = 64U - bit_count::clz (x | 1U);
88 bytes = (bits - 1U) / 7U + 1U;
90 x = (2U * x + 1U) << (bytes - 1U);
92 PSTORE_ASSERT (bytes < 9);
96 case 8: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
97 case 7: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
98 case 6: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
99 case 5: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
100 case 4: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
101 case 3: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
102 case 2: *(out++) = x & 0xFFU; x >>= 8U; PSTORE_FALLTHROUGH;
104 *(out++) = x & 0xFFU;
111 template <
typename InputIterator>
112 inline unsigned decode_size (InputIterator in) {
115 return bit_count::ctz (*in | 0x100U) + 1U;
120 template <
typename InputIterator>
121 std::uint64_t decode9 (InputIterator in) {
123 auto result = std::uint64_t{0};
124 for (
auto shift = 0U; shift < 64U; shift += 8U) {
125 result |= std::uint64_t{*(in++)} << shift;
132 template <
typename InputIterator>
133 std::uint64_t decode (InputIterator in,
unsigned size) {
134 PSTORE_ASSERT (size > 0 && size == decode_size (in));
136 return details::decode9 (in);
139 auto result = std::uint64_t{0};
144 case 8: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
145 case 7: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
146 case 6: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
147 case 5: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
148 case 4: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
149 case 3: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
150 case 2: result |= std::uint64_t{*(in++)} << shift; shift += 8; PSTORE_FALLTHROUGH;
152 result |= std::uint64_t{*(in++)} << shift;
155 return result >> size;
158 template <
typename InputIterator>
159 std::uint64_t decode (InputIterator in) {
160 return decode (in, decode_size (in));
166 #endif // PSTORE_SUPPORT_VARINT_HPP constexpr std::size_t const max_output_length
The maximum number of bytes that encode() will produce.
Definition: varint.hpp:64
Definition: nonpod2.cpp:40
Implements portable functions for bit twiddling operations including counting leading and trailing ze...