16 const size_t MAX_BUFFER_SIZE = std::numeric_limits<size_t>::max() / 8 - 4;
19 using ReaderContext = BitStreamReader::ReaderContext;
21 #ifdef ZSERIO_RUNTIME_64BIT
22 using BaseType = uint64_t;
23 using BaseSignedType = int64_t;
25 using BaseType = uint32_t;
26 using BaseSignedType = int32_t;
29 #ifdef ZSERIO_RUNTIME_64BIT
30 const std::array<BaseType, 65> MASK_TABLE = {
65 UINT64_C(0x00000001ffffffff),
66 UINT64_C(0x00000003ffffffff),
67 UINT64_C(0x00000007ffffffff),
68 UINT64_C(0x0000000fffffffff),
69 UINT64_C(0x0000001fffffffff),
70 UINT64_C(0x0000003fffffffff),
71 UINT64_C(0x0000007fffffffff),
72 UINT64_C(0x000000ffffffffff),
73 UINT64_C(0x000001ffffffffff),
74 UINT64_C(0x000003ffffffffff),
75 UINT64_C(0x000007ffffffffff),
76 UINT64_C(0x00000fffffffffff),
77 UINT64_C(0x00001fffffffffff),
78 UINT64_C(0x00003fffffffffff),
79 UINT64_C(0x00007fffffffffff),
80 UINT64_C(0x0000ffffffffffff),
81 UINT64_C(0x0001ffffffffffff),
82 UINT64_C(0x0003ffffffffffff),
83 UINT64_C(0x0007ffffffffffff),
84 UINT64_C(0x000fffffffffffff),
85 UINT64_C(0x001fffffffffffff),
86 UINT64_C(0x003fffffffffffff),
87 UINT64_C(0x007fffffffffffff),
88 UINT64_C(0x00ffffffffffffff),
89 UINT64_C(0x01ffffffffffffff),
90 UINT64_C(0x03ffffffffffffff),
91 UINT64_C(0x07ffffffffffffff),
92 UINT64_C(0x0fffffffffffffff),
93 UINT64_C(0x1fffffffffffffff),
94 UINT64_C(0x3fffffffffffffff),
95 UINT64_C(0x7fffffffffffffff),
96 UINT64_C(0xffffffffffffffff),
99 const std::array<BaseType, 33> MASK_TABLE = {
117 UINT32_C(0x0001ffff),
118 UINT32_C(0x0003ffff),
119 UINT32_C(0x0007ffff),
120 UINT32_C(0x000fffff),
121 UINT32_C(0x001fffff),
122 UINT32_C(0x003fffff),
123 UINT32_C(0x007fffff),
124 UINT32_C(0x00ffffff),
125 UINT32_C(0x01ffffff),
126 UINT32_C(0x03ffffff),
127 UINT32_C(0x07ffffff),
128 UINT32_C(0x0fffffff),
129 UINT32_C(0x1fffffff),
130 UINT32_C(0x3fffffff),
131 UINT32_C(0x7fffffff),
132 UINT32_C(0xffffffff),
136 const uint8_t VARINT_SIGN_1 = UINT8_C(0x80);
137 const uint8_t VARINT_BYTE_1 = UINT8_C(0x3f);
138 const uint8_t VARINT_BYTE_N = UINT8_C(0x7f);
139 const uint8_t VARINT_HAS_NEXT_1 = UINT8_C(0x40);
140 const uint8_t VARINT_HAS_NEXT_N = UINT8_C(0x80);
142 const uint8_t VARUINT_BYTE = UINT8_C(0x7f);
143 const uint8_t VARUINT_HAS_NEXT = UINT8_C(0x80);
145 const uint32_t VARSIZE_MAX_VALUE = (UINT32_C(1) << 31U) - 1;
147 #ifdef ZSERIO_RUNTIME_64BIT
150 return static_cast<BaseType
>(*bufferIt) << 56U |
static_cast<BaseType
>(*(bufferIt + 1)) << 48U |
151 static_cast<BaseType
>(*(bufferIt + 2)) << 40U |
static_cast<BaseType
>(*(bufferIt + 3)) << 32U |
152 static_cast<BaseType
>(*(bufferIt + 4)) << 24U |
static_cast<BaseType
>(*(bufferIt + 5)) << 16U |
153 static_cast<BaseType
>(*(bufferIt + 6)) << 8U |
static_cast<BaseType
>(*(bufferIt + 7));
158 return static_cast<BaseType
>(*bufferIt) << 48U |
static_cast<BaseType
>(*(bufferIt + 1)) << 40U |
159 static_cast<BaseType
>(*(bufferIt + 2)) << 32U |
static_cast<BaseType
>(*(bufferIt + 3)) << 24U |
160 static_cast<BaseType
>(*(bufferIt + 4)) << 16U |
static_cast<BaseType
>(*(bufferIt + 5)) << 8U |
161 static_cast<BaseType
>(*(bufferIt + 6));
166 return static_cast<BaseType
>(*bufferIt) << 40U |
static_cast<BaseType
>(*(bufferIt + 1)) << 32U |
167 static_cast<BaseType
>(*(bufferIt + 2)) << 24U |
static_cast<BaseType
>(*(bufferIt + 3)) << 16U |
168 static_cast<BaseType
>(*(bufferIt + 4)) << 8U |
static_cast<BaseType
>(*(bufferIt + 5));
173 return static_cast<BaseType
>(*bufferIt) << 32U |
static_cast<BaseType
>(*(bufferIt + 1)) << 24U |
174 static_cast<BaseType
>(*(bufferIt + 2)) << 16U |
static_cast<BaseType
>(*(bufferIt + 3)) << 8U |
175 static_cast<BaseType
>(*(bufferIt + 4));
180 return static_cast<BaseType
>(*bufferIt) << 24U |
static_cast<BaseType
>(*(bufferIt + 1)) << 16U |
181 static_cast<BaseType
>(*(bufferIt + 2)) << 8U |
static_cast<BaseType
>(*(bufferIt + 3));
186 return static_cast<BaseType
>(*bufferIt) << 16U |
static_cast<BaseType
>(*(bufferIt + 1)) << 8U |
187 static_cast<BaseType
>(*(bufferIt + 2));
192 return static_cast<BaseType
>(*bufferIt) << 8U |
static_cast<BaseType
>(*(bufferIt + 1));
197 return static_cast<BaseType
>(*bufferIt);
201 inline void throwNumBitsIsNotValid(uint8_t
numBits)
203 throw CppRuntimeException(
"BitStreamReader: ReadBits #")
204 <<
numBits <<
" is not valid, reading from stream failed!";
208 inline void checkNumBits(uint8_t
numBits)
212 throwNumBitsIsNotValid(
numBits);
217 inline void checkNumBits64(uint8_t
numBits)
221 throwNumBitsIsNotValid(
numBits);
226 inline void throwEof()
228 throw CppRuntimeException(
"BitStreamReader: Reached eof(), reading from stream failed!");
232 inline void loadCacheNext(ReaderContext& ctx, uint8_t
numBits)
234 static const uint8_t cacheBitSize =
sizeof(BaseType) * 8;
237 const size_t byteIndex = ctx.bitIndex >> 3U;
238 if (ctx.bufferBitSize >= ctx.bitIndex + cacheBitSize)
241 #ifdef ZSERIO_RUNTIME_64BIT
242 parse64(ctx.buffer.begin() + byteIndex);
244 parse32(ctx.buffer.begin() + byteIndex);
246 ctx.cacheNumBits = cacheBitSize;
250 if (ctx.bitIndex +
numBits > ctx.bufferBitSize)
255 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.bufferBitSize - ctx.bitIndex);
258 const uint8_t alignedNumBits =
static_cast<uint8_t
>((ctx.cacheNumBits + 7U) & ~0x7U);
260 switch (alignedNumBits)
262 #ifdef ZSERIO_RUNTIME_64BIT
264 ctx.cache = parse64(ctx.buffer.begin() + byteIndex);
267 ctx.cache = parse56(ctx.buffer.begin() + byteIndex);
270 ctx.cache = parse48(ctx.buffer.begin() + byteIndex);
273 ctx.cache = parse40(ctx.buffer.begin() + byteIndex);
277 ctx.cache = parse32(ctx.buffer.begin() + byteIndex);
280 ctx.cache = parse24(ctx.buffer.begin() + byteIndex);
283 ctx.cache = parse16(ctx.buffer.begin() + byteIndex);
286 ctx.cache = parse8(ctx.buffer.begin() + byteIndex);
290 ctx.cache >>=
static_cast<uint8_t
>(alignedNumBits - ctx.cacheNumBits);
295 inline BaseType readBitsImpl(ReaderContext& ctx, uint8_t
numBits)
298 if (ctx.cacheNumBits <
numBits)
301 value = ctx.cache & MASK_TABLE[ctx.cacheNumBits];
302 ctx.bitIndex += ctx.cacheNumBits;
310 if (
numBits <
sizeof(BaseType) * 8)
315 value |= ((ctx.cache >>
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits)) & MASK_TABLE[
numBits]);
316 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits);
323 inline BaseSignedType readSignedBitsImpl(ReaderContext& ctx, uint8_t
numBits)
325 static const uint8_t typeSize =
sizeof(BaseSignedType) * 8;
326 BaseType value = readBitsImpl(ctx,
numBits);
332 (value >= (
static_cast<BaseType
>(1) <<
static_cast<uint8_t
>(
numBits - 1))))
334 value -=
static_cast<BaseType
>(1) <<
numBits;
337 return static_cast<BaseSignedType
>(value);
340 #ifndef ZSERIO_RUNTIME_64BIT
342 inline uint64_t readBits64Impl(ReaderContext& ctx, uint8_t
numBits)
346 uint64_t value = readBitsImpl(ctx, 32);
350 value |= readBitsImpl(ctx,
numBits);
360 bufferBitSize(readBufferBitSize),
368 << MAX_BUFFER_SIZE <<
"' bytes!";
377 m_context(buffer, buffer.size() * 8)
381 m_context(buffer, bufferBitSize)
383 if (buffer.
size() < (bufferBitSize + 7) / 8)
386 << buffer.
size() <<
"' < '" << (bufferBitSize + 7) / 8 <<
"')!";
391 m_context(
Span<const uint8_t>(buffer, (bufferBitSize + 7) / 8), bufferBitSize)
398 return static_cast<uint32_t
>(readBitsImpl(m_context,
numBits));
405 #ifdef ZSERIO_RUNTIME_64BIT
406 return readBitsImpl(m_context,
numBits);
410 return readBitsImpl(m_context,
numBits);
413 return readBits64Impl(m_context,
numBits);
421 #ifdef ZSERIO_RUNTIME_64BIT
422 return readSignedBitsImpl(m_context,
numBits);
426 return readSignedBitsImpl(m_context,
numBits);
429 int64_t value =
static_cast<int64_t
>(readBits64Impl(m_context,
numBits));
434 const bool needsSignExtension =
435 numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (
numBits - 1)));
436 if (needsSignExtension)
438 value =
static_cast<int64_t
>(
static_cast<uint64_t
>(value) - (UINT64_C(1) <<
numBits));
449 return static_cast<int32_t
>(readSignedBitsImpl(m_context,
numBits));
454 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
455 const bool sign = (
byte & VARINT_SIGN_1) != 0;
456 uint64_t result =
byte & VARINT_BYTE_1;
457 if ((
byte & VARINT_HAS_NEXT_1) == 0)
459 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
462 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
463 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
464 if ((
byte & VARINT_HAS_NEXT_N) == 0)
466 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
469 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
470 result =
static_cast<uint64_t
>(result) << 7U |
static_cast<uint8_t
>(
byte & VARINT_BYTE_N);
471 if ((
byte & VARINT_HAS_NEXT_N) == 0)
473 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
476 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
477 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
478 if ((
byte & VARINT_HAS_NEXT_N) == 0)
480 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
483 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
484 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
485 if ((
byte & VARINT_HAS_NEXT_N) == 0)
487 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
490 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
491 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
492 if ((
byte & VARINT_HAS_NEXT_N) == 0)
494 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
497 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
498 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
499 if ((
byte & VARINT_HAS_NEXT_N) == 0)
501 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
504 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
505 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
510 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
511 const bool sign = (
byte & VARINT_SIGN_1) != 0;
512 uint32_t result =
byte & VARINT_BYTE_1;
513 if ((
byte & VARINT_HAS_NEXT_1) == 0)
515 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
518 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
519 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
520 if ((
byte & VARINT_HAS_NEXT_N) == 0)
522 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
525 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
526 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
527 if ((
byte & VARINT_HAS_NEXT_N) == 0)
529 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
532 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
533 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
538 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
539 const bool sign = (
byte & VARINT_SIGN_1) != 0;
540 uint16_t result =
byte & VARINT_BYTE_1;
541 if ((
byte & VARINT_HAS_NEXT_1) == 0)
543 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
546 result =
static_cast<uint16_t
>(result << 8U);
547 result =
static_cast<uint16_t
>(result | readBitsImpl(m_context, 8));
548 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
553 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
554 uint64_t result =
byte & VARUINT_BYTE;
555 if ((
byte & VARUINT_HAS_NEXT) == 0)
560 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
561 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
562 if ((
byte & VARUINT_HAS_NEXT) == 0)
567 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
568 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
569 if ((
byte & VARUINT_HAS_NEXT) == 0)
574 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
575 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
576 if ((
byte & VARUINT_HAS_NEXT) == 0)
581 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
582 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
583 if ((
byte & VARUINT_HAS_NEXT) == 0)
588 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
589 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
590 if ((
byte & VARUINT_HAS_NEXT) == 0)
595 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
596 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
597 if ((
byte & VARUINT_HAS_NEXT) == 0)
602 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
608 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
609 uint32_t result =
byte & VARUINT_BYTE;
610 if ((
byte & VARUINT_HAS_NEXT) == 0)
615 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
616 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
617 if ((
byte & VARUINT_HAS_NEXT) == 0)
622 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
623 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
624 if ((
byte & VARUINT_HAS_NEXT) == 0)
629 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
635 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
636 uint16_t result =
byte & VARUINT_BYTE;
637 if ((
byte & VARUINT_HAS_NEXT) == 0)
642 result =
static_cast<uint16_t
>(result << 8U);
643 result =
static_cast<uint16_t
>(result | readBitsImpl(m_context, 8));
649 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
650 const bool sign = (
byte & VARINT_SIGN_1) != 0;
651 uint64_t result =
byte & VARINT_BYTE_1;
652 if ((
byte & VARINT_HAS_NEXT_1) == 0)
654 return sign ? (result == 0 ? INT64_MIN : -
static_cast<int64_t
>(result)) :
static_cast<int64_t
>(result);
657 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
658 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
659 if ((
byte & VARINT_HAS_NEXT_N) == 0)
661 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
664 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
665 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
666 if ((
byte & VARINT_HAS_NEXT_N) == 0)
668 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
671 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
672 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
673 if ((
byte & VARINT_HAS_NEXT_N) == 0)
675 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
678 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
679 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
680 if ((
byte & VARINT_HAS_NEXT_N) == 0)
682 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
685 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
686 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
687 if ((
byte & VARINT_HAS_NEXT_N) == 0)
689 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
692 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
693 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
694 if ((
byte & VARINT_HAS_NEXT_N) == 0)
696 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
699 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
700 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
701 if ((
byte & VARINT_HAS_NEXT_N) == 0)
703 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
706 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
707 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
712 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
713 uint64_t result =
byte & VARUINT_BYTE;
714 if ((
byte & VARUINT_HAS_NEXT) == 0)
719 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
720 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
721 if ((
byte & VARUINT_HAS_NEXT) == 0)
726 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
727 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
728 if ((
byte & VARUINT_HAS_NEXT) == 0)
733 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
734 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
735 if ((
byte & VARUINT_HAS_NEXT) == 0)
740 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
741 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
742 if ((
byte & VARUINT_HAS_NEXT) == 0)
747 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
748 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
749 if ((
byte & VARUINT_HAS_NEXT) == 0)
754 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
755 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
756 if ((
byte & VARUINT_HAS_NEXT) == 0)
761 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
762 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
763 if ((
byte & VARUINT_HAS_NEXT) == 0)
768 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
774 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
775 uint32_t result =
byte & VARUINT_BYTE;
776 if ((
byte & VARUINT_HAS_NEXT) == 0)
781 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
782 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
783 if ((
byte & VARUINT_HAS_NEXT) == 0)
788 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
789 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
790 if ((
byte & VARUINT_HAS_NEXT) == 0)
795 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
796 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
797 if ((
byte & VARUINT_HAS_NEXT) == 0)
802 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
803 if (result > VARSIZE_MAX_VALUE)
806 << result <<
"' is out of range for varsize type!";
814 const uint16_t halfPrecisionFloatValue =
static_cast<uint16_t
>(readBitsImpl(m_context, 16));
821 const uint32_t singlePrecisionFloatValue =
static_cast<uint32_t
>(readBitsImpl(m_context, 32));
828 #ifdef ZSERIO_RUNTIME_64BIT
829 const uint64_t doublePrecisionFloatValue =
static_cast<uint64_t
>(readBitsImpl(m_context, 64));
831 const uint64_t doublePrecisionFloatValue = readBits64Impl(m_context, 64);
839 return readBitsImpl(m_context, 1) != 0;
846 throw CppRuntimeException(
"BitStreamReader: Reached eof(), setting of bit position failed!");
849 m_context.
bitIndex = (position / 8) * 8;
851 const uint8_t skip =
static_cast<uint8_t
>(position - m_context.
bitIndex);
863 const uint8_t skip =
static_cast<uint8_t
>(alignment - offset);
868 uint8_t BitStreamReader::readByte()
870 return static_cast<uint8_t
>(readBitsImpl(m_context, 8));
void setBitPosition(BitPosType position)
void alignTo(size_t alignment)
uint64_t readBits64(uint8_t numBits=64)
BitStreamReader(const uint8_t *buffer, size_t bufferByteSize)
uint32_t readBits(uint8_t numBits=32)
int32_t readSignedBits(uint8_t numBits=32)
int64_t readSignedBits64(uint8_t numBits=64)
BitPosType getBitPosition() const
constexpr size_type size() const noexcept
const_pointer const_iterator
uint8_t numBits(uint64_t numValues)
float convertUInt32ToFloat(uint32_t float32Value)
double convertUInt64ToDouble(uint64_t float64Value)
float convertUInt16ToFloat(uint16_t float16Value)
ReaderContext(Span< const uint8_t > readBuffer, size_t readBufferBitSize)
Span< const uint8_t > buffer
const BitPosType bufferBitSize