1 #ifndef PROTOZERO_VARINT_HPP
2 #define PROTOZERO_VARINT_HPP
34 inline uint64_t decode_varint_impl(
const char** data,
const char* end) {
35 const auto* begin =
reinterpret_cast<const int8_t*
>(*data);
36 const auto* iend =
reinterpret_cast<const int8_t*
>(end);
37 const int8_t* p = begin;
43 val = ((uint64_t(b) & 0x7fU) );
if (b >= 0) {
break; }
44 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 7U);
if (b >= 0) {
break; }
45 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 14U);
if (b >= 0) {
break; }
46 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 21U);
if (b >= 0) {
break; }
47 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 28U);
if (b >= 0) {
break; }
48 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 35U);
if (b >= 0) {
break; }
49 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 42U);
if (b >= 0) {
break; }
50 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 49U);
if (b >= 0) {
break; }
51 b = *p++; val |= ((uint64_t(b) & 0x7fU) << 56U);
if (b >= 0) {
break; }
52 b = *p++; val |= ((uint64_t(b) & 0x01U) << 63U);
if (b >= 0) {
break; }
56 unsigned int shift = 0;
57 while (p != iend && *p < 0) {
58 val |= (uint64_t(*p++) & 0x7fU) << shift;
64 val |= uint64_t(*p++) << shift;
67 *data =
reinterpret_cast<const char*
>(p);
92 if (end != *data && ((
static_cast<uint64_t
>(**data) & 0x80U) == 0)) {
93 const auto val =
static_cast<uint64_t
>(**data);
98 return detail::decode_varint_impl(data, end);
114 const auto* begin =
reinterpret_cast<const int8_t*
>(*data);
115 const auto* iend =
reinterpret_cast<const int8_t*
>(end);
116 const int8_t* p = begin;
118 while (p != iend && *p < 0) {
132 *data =
reinterpret_cast<const char*
>(p);
146 template <
typename T>
150 while (value >= 0x80U) {
151 *data++ = char((value & 0x7fU) | 0x80U);
169 template <
typename TBuffer>
171 while (value >= 0x80U) {
172 buffer_customization<TBuffer>::push_back(buffer,
char((value & 0x7fU) | 0x80U));
175 buffer_customization<TBuffer>::push_back(buffer,
char(value));
188 while (value >= 0x80U) {
189 *data++ = char((value & 0x7fU) | 0x80U);
207 while (value >= 0x80U) {
219 return (
static_cast<uint32_t
>(value) << 1U) ^
static_cast<uint32_t
>(-
static_cast<int32_t
>(
static_cast<uint32_t
>(value) >> 31U));
226 return (
static_cast<uint64_t
>(value) << 1U) ^
static_cast<uint64_t
>(-
static_cast<int64_t
>(
static_cast<uint64_t
>(value) >> 63U));
233 return static_cast<int32_t
>((value >> 1U) ^
static_cast<uint32_t
>(-
static_cast<int32_t
>(value & 1U)));
240 return static_cast<int64_t
>((value >> 1U) ^
static_cast<uint64_t
>(-
static_cast<int64_t
>(value & 1U)));
245 #endif // PROTOZERO_VARINT_HPP