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)
211 throwNumBitsIsNotValid(
numBits);
215 inline void checkNumBits64(uint8_t
numBits)
218 throwNumBitsIsNotValid(
numBits);
222 inline void throwEof()
224 throw CppRuntimeException(
"BitStreamReader: Reached eof(), reading from stream failed!");
228 inline void loadCacheNext(ReaderContext& ctx, uint8_t
numBits)
230 static const uint8_t cacheBitSize =
sizeof(BaseType) * 8;
233 const size_t byteIndex = ctx.bitIndex >> 3U;
234 if (ctx.bufferBitSize >= ctx.bitIndex + cacheBitSize)
237 #ifdef ZSERIO_RUNTIME_64BIT
238 parse64(ctx.buffer.begin() + byteIndex);
240 parse32(ctx.buffer.begin() + byteIndex);
242 ctx.cacheNumBits = cacheBitSize;
246 if (ctx.bitIndex +
numBits > ctx.bufferBitSize)
249 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.bufferBitSize - ctx.bitIndex);
252 const uint8_t alignedNumBits =
static_cast<uint8_t
>((ctx.cacheNumBits + 7U) & ~0x7U);
254 switch (alignedNumBits)
256 #ifdef ZSERIO_RUNTIME_64BIT
258 ctx.cache = parse64(ctx.buffer.begin() + byteIndex);
261 ctx.cache = parse56(ctx.buffer.begin() + byteIndex);
264 ctx.cache = parse48(ctx.buffer.begin() + byteIndex);
267 ctx.cache = parse40(ctx.buffer.begin() + byteIndex);
271 ctx.cache = parse32(ctx.buffer.begin() + byteIndex);
274 ctx.cache = parse24(ctx.buffer.begin() + byteIndex);
277 ctx.cache = parse16(ctx.buffer.begin() + byteIndex);
280 ctx.cache = parse8(ctx.buffer.begin() + byteIndex);
284 ctx.cache >>=
static_cast<uint8_t
>(alignedNumBits - ctx.cacheNumBits);
289 inline BaseType readBitsImpl(ReaderContext& ctx, uint8_t
numBits)
292 if (ctx.cacheNumBits <
numBits)
295 value = ctx.cache & MASK_TABLE[ctx.cacheNumBits];
296 ctx.bitIndex += ctx.cacheNumBits;
304 if (
numBits <
sizeof(BaseType) * 8)
307 value |= ((ctx.cache >>
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits)) & MASK_TABLE[
numBits]);
308 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits);
315 inline BaseSignedType readSignedBitsImpl(ReaderContext& ctx, uint8_t
numBits)
317 static const uint8_t typeSize =
sizeof(BaseSignedType) * 8;
318 BaseType value = readBitsImpl(ctx,
numBits);
324 (value >= (
static_cast<BaseType
>(1) <<
static_cast<uint8_t
>(
numBits - 1))))
326 value -=
static_cast<BaseType
>(1) <<
numBits;
329 return static_cast<BaseSignedType
>(value);
332 #ifndef ZSERIO_RUNTIME_64BIT
334 inline uint64_t readBits64Impl(ReaderContext& ctx, uint8_t
numBits)
338 uint64_t value = readBitsImpl(ctx, 32);
342 value |= readBitsImpl(ctx,
numBits);
352 bufferBitSize(readBufferBitSize),
360 << MAX_BUFFER_SIZE <<
"' bytes!";
369 m_context(buffer, buffer.size() * 8)
373 m_context(buffer, bufferBitSize)
375 if (buffer.
size() < (bufferBitSize + 7) / 8)
378 << buffer.
size() <<
"' < '" << (bufferBitSize + 7) / 8 <<
"')!";
383 m_context(
Span<const uint8_t>(buffer, (bufferBitSize + 7) / 8), bufferBitSize)
390 return static_cast<uint32_t
>(readBitsImpl(m_context,
numBits));
397 #ifdef ZSERIO_RUNTIME_64BIT
398 return readBitsImpl(m_context,
numBits);
401 return readBitsImpl(m_context,
numBits);
403 return readBits64Impl(m_context,
numBits);
411 #ifdef ZSERIO_RUNTIME_64BIT
412 return readSignedBitsImpl(m_context,
numBits);
415 return readSignedBitsImpl(m_context,
numBits);
417 int64_t value =
static_cast<int64_t
>(readBits64Impl(m_context,
numBits));
422 const bool needsSignExtension =
423 numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (
numBits - 1)));
424 if (needsSignExtension)
425 value -= UINT64_C(1) <<
numBits;
435 return static_cast<int32_t
>(readSignedBitsImpl(m_context,
numBits));
440 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
441 const bool sign = (
byte & VARINT_SIGN_1) != 0;
442 uint64_t result =
byte & VARINT_BYTE_1;
443 if ((
byte & VARINT_HAS_NEXT_1) == 0)
444 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
446 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
447 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
448 if ((
byte & VARINT_HAS_NEXT_N) == 0)
449 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
451 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
452 result =
static_cast<uint64_t
>(result) << 7U |
static_cast<uint8_t
>(
byte & VARINT_BYTE_N);
453 if ((
byte & VARINT_HAS_NEXT_N) == 0)
454 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
456 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
457 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
458 if ((
byte & VARINT_HAS_NEXT_N) == 0)
459 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
461 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
462 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
463 if ((
byte & VARINT_HAS_NEXT_N) == 0)
464 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
466 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
467 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
468 if ((
byte & VARINT_HAS_NEXT_N) == 0)
469 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
471 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
472 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
473 if ((
byte & VARINT_HAS_NEXT_N) == 0)
474 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
476 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
477 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
482 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
483 const bool sign = (
byte & VARINT_SIGN_1) != 0;
484 uint32_t result =
byte & VARINT_BYTE_1;
485 if ((
byte & VARINT_HAS_NEXT_1) == 0)
486 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
488 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
489 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
490 if ((
byte & VARINT_HAS_NEXT_N) == 0)
491 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
493 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
494 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
495 if ((
byte & VARINT_HAS_NEXT_N) == 0)
496 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
498 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
499 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
504 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
505 const bool sign = (
byte & VARINT_SIGN_1) != 0;
506 uint16_t result =
byte & VARINT_BYTE_1;
507 if ((
byte & VARINT_HAS_NEXT_1) == 0)
508 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
510 result =
static_cast<uint16_t
>(result << 8U);
511 result =
static_cast<uint16_t
>(result | readBitsImpl(m_context, 8));
512 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
517 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
518 uint64_t result =
byte & VARUINT_BYTE;
519 if ((
byte & VARUINT_HAS_NEXT) == 0)
522 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
523 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
524 if ((
byte & VARUINT_HAS_NEXT) == 0)
527 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
528 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
529 if ((
byte & VARUINT_HAS_NEXT) == 0)
532 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
533 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
534 if ((
byte & VARUINT_HAS_NEXT) == 0)
537 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
538 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
539 if ((
byte & VARUINT_HAS_NEXT) == 0)
542 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
543 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
544 if ((
byte & VARUINT_HAS_NEXT) == 0)
547 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
548 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
549 if ((
byte & VARUINT_HAS_NEXT) == 0)
552 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
558 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
559 uint32_t result =
byte & VARUINT_BYTE;
560 if ((
byte & VARUINT_HAS_NEXT) == 0)
563 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
564 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
565 if ((
byte & VARUINT_HAS_NEXT) == 0)
568 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
569 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
570 if ((
byte & VARUINT_HAS_NEXT) == 0)
573 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
579 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
580 uint16_t result =
byte & VARUINT_BYTE;
581 if ((
byte & VARUINT_HAS_NEXT) == 0)
584 result =
static_cast<uint16_t
>(result << 8U);
585 result =
static_cast<uint16_t
>(result | readBitsImpl(m_context, 8));
591 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
592 const bool sign = (
byte & VARINT_SIGN_1) != 0;
593 uint64_t result =
byte & VARINT_BYTE_1;
594 if ((
byte & VARINT_HAS_NEXT_1) == 0)
595 return sign ? (result == 0 ? INT64_MIN : -
static_cast<int64_t
>(result)) :
static_cast<int64_t
>(result);
597 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
598 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
599 if ((
byte & VARINT_HAS_NEXT_N) == 0)
600 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
602 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
603 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
604 if ((
byte & VARINT_HAS_NEXT_N) == 0)
605 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
607 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
608 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
609 if ((
byte & VARINT_HAS_NEXT_N) == 0)
610 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
612 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
613 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
614 if ((
byte & VARINT_HAS_NEXT_N) == 0)
615 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
617 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
618 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
619 if ((
byte & VARINT_HAS_NEXT_N) == 0)
620 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
622 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
623 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
624 if ((
byte & VARINT_HAS_NEXT_N) == 0)
625 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
627 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
628 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
629 if ((
byte & VARINT_HAS_NEXT_N) == 0)
630 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
632 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
633 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
638 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
639 uint64_t result =
byte & VARUINT_BYTE;
640 if ((
byte & VARUINT_HAS_NEXT) == 0)
643 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
644 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
645 if ((
byte & VARUINT_HAS_NEXT) == 0)
648 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
649 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
650 if ((
byte & VARUINT_HAS_NEXT) == 0)
653 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
654 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
655 if ((
byte & VARUINT_HAS_NEXT) == 0)
658 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
659 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
660 if ((
byte & VARUINT_HAS_NEXT) == 0)
663 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
664 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
665 if ((
byte & VARUINT_HAS_NEXT) == 0)
668 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
669 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
670 if ((
byte & VARUINT_HAS_NEXT) == 0)
673 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
674 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
675 if ((
byte & VARUINT_HAS_NEXT) == 0)
678 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
684 uint8_t
byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
685 uint32_t result =
byte & VARUINT_BYTE;
686 if ((
byte & VARUINT_HAS_NEXT) == 0)
689 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
690 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
691 if ((
byte & VARUINT_HAS_NEXT) == 0)
694 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
695 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
696 if ((
byte & VARUINT_HAS_NEXT) == 0)
699 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
700 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
701 if ((
byte & VARUINT_HAS_NEXT) == 0)
704 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
705 if (result > VARSIZE_MAX_VALUE)
707 << result <<
"' is out of range for varsize type!";
714 const uint16_t halfPrecisionFloatValue =
static_cast<uint16_t
>(readBitsImpl(m_context, 16));
721 const uint32_t singlePrecisionFloatValue =
static_cast<uint32_t
>(readBitsImpl(m_context, 32));
728 #ifdef ZSERIO_RUNTIME_64BIT
729 const uint64_t doublePrecisionFloatValue =
static_cast<uint64_t
>(readBitsImpl(m_context, 64));
731 const uint64_t doublePrecisionFloatValue = readBits64Impl(m_context, 64);
739 return readBitsImpl(m_context, 1) != 0;
745 throw CppRuntimeException(
"BitStreamReader: Reached eof(), setting of bit position failed!");
747 m_context.
bitIndex = (position / 8) * 8;
749 const uint8_t skip =
static_cast<uint8_t
>(position - m_context.
bitIndex);
759 const uint8_t skip =
static_cast<uint8_t
>(alignment - offset);
764 uint8_t BitStreamReader::readByte()
766 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