Coverage Report

Created: 2026-10-19 14:15

src/zserio/BitStreamWriter.cpp
Line
Count
Source
1
#include <algorithm>
2
#include <array>
3
#include <cstring>
4
#include <fstream>
5
#include <string>
6
7
#include "zserio/BitSizeOfCalculator.h"
8
#include "zserio/BitStreamWriter.h"
9
#include "zserio/CppRuntimeException.h"
10
#include "zserio/FloatUtil.h"
11
12
namespace zserio
13
{
14
15
static const std::array<uint32_t, 33> MAX_U32_VALUES = {
16
        0x00U,
17
        0x0001U,
18
        0x0003U,
19
        0x0007U,
20
        0x000fU,
21
        0x001fU,
22
        0x003fU,
23
        0x007fU,
24
        0x00ffU,
25
        0x01ffU,
26
        0x03ffU,
27
        0x07ffU,
28
        0x0fffU,
29
        0x1fffU,
30
        0x3fffU,
31
        0x7fffU,
32
        0xffffU,
33
        0x0001ffffU,
34
        0x0003ffffU,
35
        0x0007ffffU,
36
        0x000fffffU,
37
        0x001fffffU,
38
        0x003fffffU,
39
        0x007fffffU,
40
        0x00ffffffU,
41
        0x01ffffffU,
42
        0x03ffffffU,
43
        0x07ffffffU,
44
        0x0fffffffU,
45
        0x1fffffffU,
46
        0x3fffffffU,
47
        0x7fffffffU,
48
        0xffffffffU,
49
};
50
51
static const std::array<int32_t, 33> MIN_I32_VALUES = {
52
        0,
53
        -0x0001,
54
        -0x0002,
55
        -0x0004,
56
        -0x0008,
57
        -0x0010,
58
        -0x0020,
59
        -0x0040,
60
        -0x0080,
61
        -0x0100,
62
        -0x0200,
63
        -0x0400,
64
        -0x0800,
65
        -0x1000,
66
        -0x2000,
67
        -0x4000,
68
        -0x8000,
69
        -0x00010000,
70
        -0x00020000,
71
        -0x00040000,
72
        -0x00080000,
73
        -0x00100000,
74
        -0x00200000,
75
        -0x00400000,
76
        -0x00800000,
77
        -0x01000000,
78
        -0x02000000,
79
        -0x04000000,
80
        -0x08000000,
81
        -0x10000000,
82
        -0x20000000,
83
        -0x40000000,
84
        INT32_MIN,
85
};
86
87
static const std::array<int32_t, 33> MAX_I32_VALUES = {
88
        0x00,
89
        0x0000,
90
        0x0001,
91
        0x0003,
92
        0x0007,
93
        0x000f,
94
        0x001f,
95
        0x003f,
96
        0x007f,
97
        0x00ff,
98
        0x01ff,
99
        0x03ff,
100
        0x07ff,
101
        0x0fff,
102
        0x1fff,
103
        0x3fff,
104
        0x7fff,
105
        0x0000ffff,
106
        0x0001ffff,
107
        0x0003ffff,
108
        0x0007ffff,
109
        0x000fffff,
110
        0x001fffff,
111
        0x003fffff,
112
        0x007fffff,
113
        0x00ffffff,
114
        0x01ffffff,
115
        0x03ffffff,
116
        0x07ffffff,
117
        0x0fffffff,
118
        0x1fffffff,
119
        0x3fffffff,
120
        0x7fffffff,
121
};
122
123
static const std::array<uint64_t, 65> MAX_U64_VALUES = {
124
        0x00ULL,
125
        0x0001ULL,
126
        0x0003ULL,
127
        0x0007ULL,
128
        0x000fULL,
129
        0x001fULL,
130
        0x003fULL,
131
        0x007fULL,
132
        0x00ffULL,
133
        0x01ffULL,
134
        0x03ffULL,
135
        0x07ffULL,
136
        0x0fffULL,
137
        0x1fffULL,
138
        0x3fffULL,
139
        0x7fffULL,
140
        0xffffULL,
141
        0x0001ffffULL,
142
        0x0003ffffULL,
143
        0x0007ffffULL,
144
        0x000fffffULL,
145
        0x001fffffULL,
146
        0x003fffffULL,
147
        0x007fffffULL,
148
        0x00ffffffULL,
149
        0x01ffffffULL,
150
        0x03ffffffULL,
151
        0x07ffffffULL,
152
        0x0fffffffULL,
153
        0x1fffffffULL,
154
        0x3fffffffULL,
155
        0x7fffffffULL,
156
        0xffffffffULL,
157
        0x0001ffffffffULL,
158
        0x0003ffffffffULL,
159
        0x0007ffffffffULL,
160
        0x000fffffffffULL,
161
        0x001fffffffffULL,
162
        0x003fffffffffULL,
163
        0x007fffffffffULL,
164
        0x00ffffffffffULL,
165
        0x01ffffffffffULL,
166
        0x03ffffffffffULL,
167
        0x07ffffffffffULL,
168
        0x0fffffffffffULL,
169
        0x1fffffffffffULL,
170
        0x3fffffffffffULL,
171
        0x7fffffffffffULL,
172
        0xffffffffffffULL,
173
        0x0001ffffffffffffULL,
174
        0x0003ffffffffffffULL,
175
        0x0007ffffffffffffULL,
176
        0x000fffffffffffffULL,
177
        0x001fffffffffffffULL,
178
        0x003fffffffffffffULL,
179
        0x007fffffffffffffULL,
180
        0x00ffffffffffffffULL,
181
        0x01ffffffffffffffULL,
182
        0x03ffffffffffffffULL,
183
        0x07ffffffffffffffULL,
184
        0x0fffffffffffffffULL,
185
        0x1fffffffffffffffULL,
186
        0x3fffffffffffffffULL,
187
        0x7fffffffffffffffULL,
188
        0xffffffffffffffffULL,
189
};
190
191
static const std::array<int64_t, 65> MIN_I64_VALUES = {
192
        0LL,
193
        -0x0001LL,
194
        -0x0002LL,
195
        -0x0004LL,
196
        -0x0008LL,
197
        -0x0010LL,
198
        -0x0020LL,
199
        -0x0040LL,
200
        -0x0080LL,
201
        -0x0100LL,
202
        -0x0200LL,
203
        -0x0400LL,
204
        -0x0800LL,
205
        -0x1000LL,
206
        -0x2000LL,
207
        -0x4000LL,
208
        -0x8000LL,
209
        -0x00010000LL,
210
        -0x00020000LL,
211
        -0x00040000LL,
212
        -0x00080000LL,
213
        -0x00100000LL,
214
        -0x00200000LL,
215
        -0x00400000LL,
216
        -0x00800000LL,
217
        -0x01000000LL,
218
        -0x02000000LL,
219
        -0x04000000LL,
220
        -0x08000000LL,
221
        -0x10000000LL,
222
        -0x20000000LL,
223
        -0x40000000LL,
224
        -0x80000000LL,
225
        -0x000100000000LL,
226
        -0x000200000000LL,
227
        -0x000400000000LL,
228
        -0x000800000000LL,
229
        -0x001000000000LL,
230
        -0x002000000000LL,
231
        -0x004000000000LL,
232
        -0x008000000000LL,
233
        -0x010000000000LL,
234
        -0x020000000000LL,
235
        -0x040000000000LL,
236
        -0x080000000000LL,
237
        -0x100000000000LL,
238
        -0x200000000000LL,
239
        -0x400000000000LL,
240
        -0x800000000000LL,
241
        -0x0001000000000000LL,
242
        -0x0002000000000000LL,
243
        -0x0004000000000000LL,
244
        -0x0008000000000000LL,
245
        -0x0010000000000000LL,
246
        -0x0020000000000000LL,
247
        -0x0040000000000000LL,
248
        -0x0080000000000000LL,
249
        -0x0100000000000000LL,
250
        -0x0200000000000000LL,
251
        -0x0400000000000000LL,
252
        -0x0800000000000000LL,
253
        -0x1000000000000000LL,
254
        -0x2000000000000000LL,
255
        -0x4000000000000000LL,
256
        INT64_MIN,
257
};
258
259
static const std::array<int64_t, 65> MAX_I64_VALUES = {
260
        0x00LL,
261
        0x0000LL,
262
        0x0001LL,
263
        0x0003LL,
264
        0x0007LL,
265
        0x000fLL,
266
        0x001fLL,
267
        0x003fLL,
268
        0x007fLL,
269
        0x00ffLL,
270
        0x01ffLL,
271
        0x03ffLL,
272
        0x07ffLL,
273
        0x0fffLL,
274
        0x1fffLL,
275
        0x3fffLL,
276
        0x7fffLL,
277
        0x0000ffffLL,
278
        0x0001ffffLL,
279
        0x0003ffffLL,
280
        0x0007ffffLL,
281
        0x000fffffLL,
282
        0x001fffffLL,
283
        0x003fffffLL,
284
        0x007fffffLL,
285
        0x00ffffffLL,
286
        0x01ffffffLL,
287
        0x03ffffffLL,
288
        0x07ffffffLL,
289
        0x0fffffffLL,
290
        0x1fffffffLL,
291
        0x3fffffffLL,
292
        0x7fffffffLL,
293
        0x0000ffffffffLL,
294
        0x0001ffffffffLL,
295
        0x0003ffffffffLL,
296
        0x0007ffffffffLL,
297
        0x000fffffffffLL,
298
        0x001fffffffffLL,
299
        0x003fffffffffLL,
300
        0x007fffffffffLL,
301
        0x00ffffffffffLL,
302
        0x01ffffffffffLL,
303
        0x03ffffffffffLL,
304
        0x07ffffffffffLL,
305
        0x0fffffffffffLL,
306
        0x1fffffffffffLL,
307
        0x3fffffffffffLL,
308
        0x7fffffffffffLL,
309
        0x0000ffffffffffffLL,
310
        0x0001ffffffffffffLL,
311
        0x0003ffffffffffffLL,
312
        0x0007ffffffffffffLL,
313
        0x000fffffffffffffLL,
314
        0x001fffffffffffffLL,
315
        0x003fffffffffffffLL,
316
        0x007fffffffffffffLL,
317
        0x00ffffffffffffffLL,
318
        0x01ffffffffffffffLL,
319
        0x03ffffffffffffffLL,
320
        0x07ffffffffffffffLL,
321
        0x0fffffffffffffffLL,
322
        0x1fffffffffffffffLL,
323
        0x3fffffffffffffffLL,
324
        0x7fffffffffffffffLL,
325
};
326
327
BitStreamWriter::BitStreamWriter(uint8_t* buffer, size_t bufferBitSize, BitsTag) :
328
        m_buffer(buffer, (bufferBitSize + 7) / 8),
329
        m_bitIndex(0),
330
        m_bufferBitSize(bufferBitSize)
331
1
{}
332
333
BitStreamWriter::BitStreamWriter(uint8_t* buffer, size_t bufferByteSize) :
334
        BitStreamWriter(Span<uint8_t>(buffer, bufferByteSize))
335
3.40k
{}
336
337
BitStreamWriter::BitStreamWriter(Span<uint8_t> buffer) :
338
        m_buffer(buffer),
339
        m_bitIndex(0),
340
        m_bufferBitSize(buffer.size() * 8)
341
3.41k
{}
342
343
BitStreamWriter::BitStreamWriter(Span<uint8_t> buffer, size_t bufferBitSize) :
344
        m_buffer(buffer),
345
        m_bitIndex(0),
346
        m_bufferBitSize(bufferBitSize)
347
471
{
348
471
    if (buffer.size() < (bufferBitSize + 7) / 8)
349
1
    {
350
1
        throw CppRuntimeException("BitStreamWriter: Wrong buffer bit size ('")
351
1
                << buffer.size() << "' < '" << (bufferBitSize + 7) / 8 << "')!";
352
1
    }
353
471
}
354
355
void BitStreamWriter::writeBits(uint32_t data, uint8_t numBits)
356
8.44k
{
357
8.44k
    if (numBits == 0 || 
numBits > sizeof(uint32_t) * 88.43k
||
data > MAX_U32_VALUES[numBits]8.43k
)
358
37
    {
359
37
        throw CppRuntimeException("BitStreamWriter: Writing of ")
360
37
                << numBits << "-bits value '" << data << "' failed!";
361
37
    }
362
363
8.40k
    writeUnsignedBits(data, numBits);
364
8.40k
}
365
366
void BitStreamWriter::writeBits64(uint64_t data, uint8_t numBits)
367
4.35k
{
368
4.35k
    if (numBits == 0 || 
numBits > sizeof(uint64_t) * 84.35k
||
data > MAX_U64_VALUES[numBits]4.35k
)
369
69
    {
370
69
        throw CppRuntimeException("BitStreamWriter: Writing of ")
371
69
                << numBits << "-bits value '" << data << "' failed!";
372
69
    }
373
374
4.28k
    writeUnsignedBits64(data, numBits);
375
4.28k
}
376
377
void BitStreamWriter::writeSignedBits(int32_t data, uint8_t numBits)
378
1.63k
{
379
1.63k
    if (numBits == 0 || 
numBits > sizeof(int32_t) * 81.63k
||
data < MIN_I32_VALUES[numBits]1.62k
||
380
1.63k
            
data > MAX_I32_VALUES[numBits]1.59k
)
381
68
    {
382
68
        throw CppRuntimeException("BitStreamWriter: Writing of ")
383
68
                << numBits << "-bits value '" << data << "' failed!";
384
68
    }
385
386
1.56k
    writeUnsignedBits(static_cast<uint32_t>(data) & MAX_U32_VALUES[numBits], numBits);
387
1.56k
}
388
389
void BitStreamWriter::writeSignedBits64(int64_t data, uint8_t numBits)
390
2.18k
{
391
2.18k
    if (numBits == 0 || 
numBits > sizeof(int64_t) * 82.18k
||
data < MIN_I64_VALUES[numBits]2.18k
||
392
2.18k
            
data > MAX_I64_VALUES[numBits]2.12k
)
393
132
    {
394
132
        throw CppRuntimeException("BitStreamWriter: Writing of ")
395
132
                << numBits << "-bits value '" << data << "' failed!";
396
132
    }
397
398
2.05k
    writeUnsignedBits64(static_cast<uint64_t>(data) & MAX_U64_VALUES[numBits], numBits);
399
2.05k
}
400
401
void BitStreamWriter::writeVarInt64(int64_t data)
402
4.42k
{
403
4.42k
    writeSignedVarNum(data, 8, zserio::bitSizeOfVarInt64(data) / 8);
404
4.42k
}
405
406
void BitStreamWriter::writeVarInt32(int32_t data)
407
1.19k
{
408
1.19k
    writeSignedVarNum(data, 4, zserio::bitSizeOfVarInt32(data) / 8);
409
1.19k
}
410
411
void BitStreamWriter::writeVarInt16(int16_t data)
412
344
{
413
344
    writeSignedVarNum(data, 2, zserio::bitSizeOfVarInt16(data) / 8);
414
344
}
415
416
void BitStreamWriter::writeVarUInt64(uint64_t data)
417
3.14k
{
418
3.14k
    writeUnsignedVarNum(data, 8, zserio::bitSizeOfVarUInt64(data) / 8);
419
3.14k
}
420
421
void BitStreamWriter::writeVarUInt32(uint32_t data)
422
820
{
423
820
    writeUnsignedVarNum(data, 4, zserio::bitSizeOfVarUInt32(data) / 8);
424
820
}
425
426
void BitStreamWriter::writeVarUInt16(uint16_t data)
427
284
{
428
284
    writeUnsignedVarNum(data, 2, zserio::bitSizeOfVarUInt16(data) / 8);
429
284
}
430
431
void BitStreamWriter::writeVarInt(int64_t data)
432
5.50k
{
433
5.50k
    if (data == INT64_MIN)
434
159
    {
435
159
        writeBits(0x80, 8); // INT64_MIN is encoded as -0
436
159
    }
437
5.34k
    else
438
5.34k
    {
439
5.34k
        writeSignedVarNum(data, 9, zserio::bitSizeOfVarInt(data) / 8);
440
5.34k
    }
441
5.50k
}
442
443
void BitStreamWriter::writeVarUInt(uint64_t data)
444
2.75k
{
445
2.75k
    writeUnsignedVarNum(data, 9, zserio::bitSizeOfVarUInt(data) / 8);
446
2.75k
}
447
448
void BitStreamWriter::writeVarSize(uint32_t data)
449
3.11k
{
450
3.11k
    writeUnsignedVarNum(data, 5, zserio::bitSizeOfVarSize(data) / 8);
451
3.11k
}
452
453
void BitStreamWriter::writeFloat16(float data)
454
305
{
455
305
    const uint16_t halfPrecisionFloat = convertFloatToUInt16(data);
456
305
    writeUnsignedBits(halfPrecisionFloat, 16);
457
305
}
458
459
void BitStreamWriter::writeFloat32(float data)
460
499
{
461
499
    const uint32_t singlePrecisionFloat = convertFloatToUInt32(data);
462
499
    writeUnsignedBits(singlePrecisionFloat, 32);
463
499
}
464
465
void BitStreamWriter::writeFloat64(double data)
466
859
{
467
859
    const uint64_t doublePrecisionFloat = convertDoubleToUInt64(data);
468
859
    writeUnsignedBits64(doublePrecisionFloat, 64);
469
859
}
470
471
void BitStreamWriter::writeBytes(Span<const uint8_t> data)
472
99
{
473
99
    const size_t len = data.size();
474
99
    writeVarSize(convertSizeToUInt32(len));
475
476
99
    const BitPosType beginBitPosition = getBitPosition();
477
99
    if ((beginBitPosition & 0x07U) != 0)
478
52
    {
479
        // we are not aligned to byte
480
180
        for (size_t i = 0; i < len; 
++i128
)
481
128
        {
482
128
            writeBits(data[i], 8);
483
128
        }
484
52
    }
485
47
    else
486
47
    {
487
        // we are aligned to bytes
488
47
        setBitPosition(beginBitPosition + len * 8);
489
47
        if (hasWriteBuffer())
490
45
        {
491
45
            (void)std::copy(data.begin(), data.end(), m_buffer.begin() + beginBitPosition / 8);
492
45
        }
493
47
    }
494
99
}
495
496
void BitStreamWriter::writeString(StringView data)
497
161
{
498
161
    const size_t len = data.size();
499
161
    writeVarSize(convertSizeToUInt32(len));
500
501
161
    const BitPosType beginBitPosition = getBitPosition();
502
161
    if ((beginBitPosition & 0x07U) != 0)
503
78
    {
504
        // we are not aligned to byte
505
1.54k
        for (size_t i = 0; i < len; 
++i1.47k
)
506
1.47k
        {
507
1.47k
            writeBits(static_cast<uint32_t>(std::char_traits<char>::to_int_type(data[i])), 8);
508
1.47k
        }
509
78
    }
510
83
    else
511
83
    {
512
        // we are aligned to bytes
513
83
        setBitPosition(beginBitPosition + len * 8);
514
83
        if (hasWriteBuffer())
515
80
        {
516
80
            (void)std::copy(data.begin(), data.begin() + len, m_buffer.data() + beginBitPosition / 8);
517
80
        }
518
83
    }
519
161
}
520
521
void BitStreamWriter::writeBool(bool data)
522
909
{
523
909
    writeBits((data ? 
1391
:
0518
), 1);
524
909
}
525
526
void BitStreamWriter::setBitPosition(BitPosType position)
527
704
{
528
704
    if (hasWriteBuffer())
529
695
    {
530
695
        checkCapacity(position);
531
695
    }
532
533
704
    m_bitIndex = position;
534
704
}
535
536
void BitStreamWriter::alignTo(size_t alignment)
537
4.73k
{
538
4.73k
    const BitPosType offset = getBitPosition() % alignment;
539
4.73k
    if (offset != 0)
540
2.46k
    {
541
2.46k
        const uint8_t skip = static_cast<uint8_t>(alignment - offset);
542
2.46k
        writeBits64(0, skip);
543
2.46k
    }
544
4.73k
}
545
546
const uint8_t* BitStreamWriter::getWriteBuffer() const
547
515
{
548
515
    return m_buffer.data();
549
515
}
550
551
Span<const uint8_t> BitStreamWriter::getBuffer() const
552
18
{
553
18
    return m_buffer;
554
18
}
555
556
void BitStreamWriter::writeUnsignedBits(uint32_t data, uint8_t numBits)
557
103k
{
558
103k
    if (!hasWriteBuffer())
559
37.3k
    {
560
37.3k
        m_bitIndex += numBits;
561
37.3k
        return;
562
37.3k
    }
563
564
65.9k
    checkCapacity(m_bitIndex + numBits);
565
566
65.9k
    uint8_t restNumBits = numBits;
567
65.9k
    const uint8_t bitsUsed = m_bitIndex & 0x07U;
568
65.9k
    uint8_t bitsFree = static_cast<uint8_t>(8 - bitsUsed);
569
65.9k
    size_t byteIndex = m_bitIndex / 8;
570
571
65.9k
    if (restNumBits > bitsFree)
572
46.3k
    {
573
        // first part
574
46.3k
        const uint8_t shiftNum = static_cast<uint8_t>(restNumBits - bitsFree);
575
46.3k
        const uint8_t maskedByte = static_cast<uint8_t>(m_buffer[byteIndex] & ~(0xFFU >> bitsUsed));
576
46.3k
        m_buffer[byteIndex++] = static_cast<uint8_t>(maskedByte | (data >> shiftNum));
577
46.3k
        restNumBits = static_cast<uint8_t>(restNumBits - bitsFree);
578
579
        // middle parts
580
64.3k
        while (restNumBits >= 8)
581
18.0k
        {
582
18.0k
            restNumBits = static_cast<uint8_t>(restNumBits - 8);
583
18.0k
            m_buffer[byteIndex++] = static_cast<uint8_t>((data >> restNumBits) & MAX_U32_VALUES[8]);
584
18.0k
        }
585
586
        // reset bits free
587
46.3k
        bitsFree = 8;
588
46.3k
    }
589
590
    // last part
591
65.9k
    if (restNumBits > 0)
592
64.2k
    {
593
64.2k
        const uint8_t shiftNum = static_cast<uint8_t>(bitsFree - restNumBits);
594
64.2k
        const uint32_t mask = MAX_U32_VALUES[restNumBits];
595
64.2k
        const uint8_t maskedByte =
596
64.2k
                m_buffer[byteIndex] & static_cast<uint8_t>(~static_cast<uint8_t>(mask << shiftNum));
597
64.2k
        m_buffer[byteIndex] = static_cast<uint8_t>(maskedByte | ((data & mask) << shiftNum));
598
64.2k
    }
599
600
65.9k
    m_bitIndex += numBits;
601
65.9k
}
602
603
inline void BitStreamWriter::writeUnsignedBits64(uint64_t data, uint8_t numBits)
604
7.20k
{
605
7.20k
    if (numBits <= 32)
606
4.04k
    {
607
4.04k
        writeUnsignedBits(static_cast<uint32_t>(data), numBits);
608
4.04k
    }
609
3.16k
    else
610
3.16k
    {
611
3.16k
        writeUnsignedBits(static_cast<uint32_t>(data >> 32U), static_cast<uint8_t>(numBits - 32));
612
3.16k
        writeUnsignedBits(static_cast<uint32_t>(data), 32);
613
3.16k
    }
614
7.20k
}
615
616
inline void BitStreamWriter::writeSignedVarNum(int64_t value, size_t maxVarBytes, size_t numVarBytes)
617
11.3k
{
618
11.3k
    const uint64_t absValue = static_cast<uint64_t>(value < 0 ? 
-value3.92k
:
value7.37k
);
619
11.3k
    writeVarNum(absValue, true, value < 0, maxVarBytes, numVarBytes);
620
11.3k
}
621
622
inline void BitStreamWriter::writeUnsignedVarNum(uint64_t value, size_t maxVarBytes, size_t numVarBytes)
623
10.1k
{
624
10.1k
    writeVarNum(value, false, false, maxVarBytes, numVarBytes);
625
10.1k
}
626
627
inline void BitStreamWriter::writeVarNum(
628
        uint64_t value, bool hasSign, bool isNegative, size_t maxVarBytes, size_t numVarBytes)
629
21.4k
{
630
21.4k
    static const std::array<uint64_t, 8> bitMasks = {0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
631
21.4k
    const bool hasMaxByteRange = (numVarBytes == maxVarBytes);
632
633
103k
    for (size_t i = 0; i < numVarBytes; 
i++81.9k
)
634
81.9k
    {
635
81.9k
        uint8_t byte = 0x00;
636
81.9k
        uint8_t numBits = 8;
637
81.9k
        const bool hasNextByte = (i < numVarBytes - 1);
638
81.9k
        const bool hasSignBit = (hasSign && 
i == 048.6k
);
639
81.9k
        if (hasSignBit)
640
11.3k
        {
641
11.3k
            if (isNegative)
642
3.92k
            {
643
3.92k
                byte |= 0x80U;
644
3.92k
            }
645
11.3k
            numBits--;
646
11.3k
        }
647
81.9k
        if (hasNextByte)
648
60.5k
        {
649
60.5k
            numBits--;
650
60.5k
            const uint8_t add = static_cast<uint8_t>(0x01U << numBits);
651
60.5k
            byte = static_cast<uint8_t>(byte | add); // use bit 6 if signed bit is present, use bit 7 otherwise
652
60.5k
        }
653
21.4k
        else // this is the last byte
654
21.4k
        {
655
21.4k
            if (!hasMaxByteRange) // next byte indicator is not used in last byte in case of max byte range
656
18.8k
            {
657
18.8k
                numBits--;
658
18.8k
            }
659
21.4k
        }
660
661
81.9k
        const size_t shiftBits = (numVarBytes - (i + 1)) * 7 + ((hasMaxByteRange && 
hasNextByte17.7k
) ?
115.1k
:
066.8k
);
662
81.9k
        const uint8_t add = static_cast<uint8_t>((value >> shiftBits) & bitMasks[numBits - 1U]);
663
81.9k
        byte = static_cast<uint8_t>(byte | add);
664
81.9k
        writeUnsignedBits(byte, 8);
665
81.9k
    }
666
21.4k
}
667
668
inline void BitStreamWriter::throwInsufficientCapacityException() const
669
71
{
670
71
    throw InsufficientCapacityException("BitStreamWriter: Reached end of bit buffer!");
671
71
}
672
673
inline void BitStreamWriter::checkCapacity(size_t bitSize) const
674
66.6k
{
675
66.6k
    if (bitSize > m_bufferBitSize)
676
71
    {
677
71
        throwInsufficientCapacityException();
678
71
    }
679
66.6k
}
680
681
} // namespace zserio