Skip to content

Commit 3b43179

Browse files
committed
Change bit_reader to construct with num_bits
Change flush and checksum to use number of bits instead of bytes
1 parent 75dd369 commit 3b43179

13 files changed

+157
-154
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ uint32_t value = 27; // We can choose any value below 2^5. Otherwise we need mor
254254
writer.serialize_bits(value, 5);
255255
256256
// Flush the writer's remaining state into the buffer
257-
uint32_t num_bytes = writer.flush();
257+
uint32_t num_bits = writer.flush();
258258
259259
// Create a reader, referencing the buffer and bytes written
260-
bit_reader reader(buffer, num_bytes);
260+
bit_reader reader(buffer, num_bits);
261261
262262
// Read the value back
263263
uint32_t out_value; // We don't have to initialize it yet
@@ -275,10 +275,10 @@ int32_t value = -45; // We can choose any value within the range below
275275
writer.serialize<int32_t>(value, -90, 40); // A lower and upper bound which the value will be quantized between
276276

277277
// Flush the writer's remaining state into the buffer
278-
uint32_t num_bytes = writer.flush();
278+
uint32_t num_bits = writer.flush();
279279

280280
// Create a reader by moving and invalidating the writer
281-
bit_reader reader(buffer, num_bytes);
281+
bit_reader reader(buffer, num_bits);
282282

283283
// Read the value back
284284
int32_t out_value; // We don't have to initialize it yet
@@ -296,10 +296,10 @@ const char* value = "Hello world!";
296296
writer.serialize<const char*>(value, 32U); // The second argument is the maximum size we expect the string to be
297297
298298
// Flush the writer's remaining state into the buffer
299-
uint32_t num_bytes = writer.flush();
299+
uint32_t num_bits = writer.flush();
300300
301301
// Create a reader by moving and invalidating the writer
302-
bit_reader reader(buffer, num_bytes);
302+
bit_reader reader(buffer, num_bits);
303303
304304
// Read the value back
305305
char out_value[32]; // Set the size to the max size
@@ -317,10 +317,10 @@ std::string value = "Hello world!";
317317
writer.serialize<std::string>(value, 32U); // The second argument is the maximum size we expect the string to be
318318

319319
// Flush the writer's remaining state into the buffer
320-
uint32_t num_bytes = writer.flush();
320+
uint32_t num_bits = writer.flush();
321321

322322
// Create a reader by moving and invalidating the writer
323-
bit_reader reader(buffer, num_bytes);
323+
bit_reader reader(buffer, num_bits);
324324

325325
// Read the value back
326326
std::string out_value; // The string will be resized if the output doesn't fit
@@ -339,10 +339,10 @@ float value = 1.2345678f;
339339
writer.serialize<bounded_range>(range, value);
340340
341341
// Flush the writer's remaining state into the buffer
342-
uint32_t num_bytes = writer.flush();
342+
uint32_t num_bits = writer.flush();
343343
344344
// Create a reader by moving and invalidating the writer
345-
bit_reader reader(buffer, num_bytes);
345+
bit_reader reader(buffer, num_bits);
346346
347347
// Read the value back
348348
float out_value;

include/bitstream/stream/bit_reader.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@ namespace bitstream
3636
m_WordIndex(0) {}
3737

3838
/**
39-
* @brief Construct a reader pointing to the given byte array with @p num_bytes size
39+
* @brief Construct a reader pointing to the given byte array with @p num_bits
4040
* @param bytes The byte array to read from. Should be 4-byte aligned if possible. The size of the array must be a multiple of 4
41-
* @param num_bytes The maximum number of bytes that we can read
41+
* @param num_bits The maximum number of bits that we can read
4242
*/
43-
explicit bit_reader(const void* bytes, uint32_t num_bytes) noexcept :
43+
explicit bit_reader(const void* bytes, uint32_t num_bits) noexcept :
4444
m_Buffer(static_cast<const uint32_t*>(bytes)),
4545
m_NumBitsRead(0),
46-
m_TotalBits(num_bytes * 8),
46+
m_TotalBits(num_bits),
4747
m_Scratch(0),
4848
m_ScratchBits(0),
4949
m_WordIndex(0) {}
5050

5151
/**
5252
* @brief Construct a reader pointing to the given @p buffer
5353
* @param buffer The buffer to read from
54-
* @param num_bytes The maximum number of bytes that we can read
54+
* @param num_bits The maximum number of bits that we can read
5555
*/
5656
template<size_t Size>
57-
explicit bit_reader(byte_buffer<Size>& buffer, uint32_t num_bytes) noexcept :
57+
explicit bit_reader(byte_buffer<Size>& buffer, uint32_t num_bits) noexcept :
5858
m_Buffer(reinterpret_cast<uint32_t*>(buffer.Bytes)),
5959
m_NumBitsRead(0),
60-
m_TotalBits(num_bytes * 8),
60+
m_TotalBits(num_bits),
6161
m_Scratch(0),
6262
m_ScratchBits(0),
6363
m_WordIndex(0) {}
@@ -113,6 +113,12 @@ namespace bitstream
113113
*/
114114
uint32_t get_num_bits_serialized() const noexcept { return m_NumBitsRead; }
115115

116+
/**
117+
* @brief Returns the number of bytes which have been read from the buffer
118+
* @return The number of bytes which have been read
119+
*/
120+
uint32_t get_num_bytes_serialized() const noexcept { return m_NumBitsRead > 0U ? ((m_NumBitsRead - 1U) / 8U + 1U) : 0U; }
121+
116122
/**
117123
* @brief Returns whether the @p num_bits be read from the buffer
118124
* @param num_bits The number of bits to test

include/bitstream/stream/bit_writer.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,16 @@ namespace bitstream
4242

4343
/**
4444
* @brief Construct a writer pointing to the given byte array with @p num_bytes size
45-
* @param bytes The byte array to write to. Should be 4-byte aligned if possible
46-
* @param num_bytes The number of bytes in the array. Must be a multiple of 4
45+
* @param bytes The byte array to write to. Must be 4-byte aligned and the size must be a multiple of 4
46+
* @param num_bytes The number of bytes in the array
4747
*/
4848
explicit bit_writer(void* bytes, uint32_t num_bytes) noexcept :
4949
m_Buffer(static_cast<uint32_t*>(bytes)),
5050
m_NumBitsWritten(0),
5151
m_TotalBits(num_bytes * 8),
5252
m_Scratch(0),
5353
m_ScratchBits(0),
54-
m_WordIndex(0)
55-
{
56-
#ifdef BS_DEBUG_BREAK
57-
if (num_bytes % 4 != 0)
58-
BS_BREAKPOINT();
59-
#endif
60-
}
54+
m_WordIndex(0) {}
6155

6256
/**
6357
* @brief Construct a writer pointing to the given @p buffer
@@ -123,6 +117,12 @@ namespace bitstream
123117
*/
124118
uint32_t get_num_bits_serialized() const noexcept { return m_NumBitsWritten; }
125119

120+
/**
121+
* @brief Returns the number of bytes which have been written to the buffer
122+
* @return The number of bytes which have been written
123+
*/
124+
uint32_t get_num_bytes_serialized() const noexcept { return m_NumBitsWritten > 0U ? ((m_NumBitsWritten - 1U) / 8U + 1U) : 0U; }
125+
126126
/**
127127
* @brief Returns whether the @p num_bits can fit in the buffer
128128
* @param num_bits The number of bits to test
@@ -160,10 +160,7 @@ namespace bitstream
160160
m_WordIndex++;
161161
}
162162

163-
if (m_NumBitsWritten > 0U)
164-
return (m_NumBitsWritten - 1U) / 8U + 1U;
165-
else
166-
return 0U;
163+
return m_NumBitsWritten;
167164
}
168165

169166
/**
@@ -190,18 +187,18 @@ namespace bitstream
190187
*/
191188
uint32_t serialize_checksum(uint32_t protocol_version) noexcept
192189
{
193-
uint32_t num_bytes = flush();
190+
uint32_t num_bits = flush();
194191

195192
// Copy protocol version to buffer
196193
*m_Buffer = protocol_version;
197194

198195
// Generate checksum of version + data
199-
uint32_t checksum = utility::crc_uint32(reinterpret_cast<uint8_t*>(m_Buffer), num_bytes);
196+
uint32_t checksum = utility::crc_uint32(reinterpret_cast<uint8_t*>(m_Buffer), get_num_bytes_serialized());
200197

201198
// Put checksum at beginning
202199
*m_Buffer = checksum;
203200

204-
return num_bytes;
201+
return num_bits;
205202
}
206203

207204
/**

src/test/examples_test.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ namespace bitstream::test::example
2323
BS_TEST_ASSERT(writer.serialize_bits(value, 5));
2424

2525
// Flush the writer's remaining state into the buffer
26-
uint32_t num_bytes = writer.flush();
26+
uint32_t num_bits = writer.flush();
2727

28-
BS_TEST_ASSERT_OPERATION(num_bytes, == , 1);
28+
BS_TEST_ASSERT_OPERATION(num_bits, == , 5);
2929

3030
// Create a reader, referencing the buffer and bytes written
31-
bit_reader reader(buffer, num_bytes);
31+
bit_reader reader(buffer, num_bits);
3232

3333
// Read the value back
3434
uint32_t out_value; // We don't have to initialize it yet
@@ -49,12 +49,12 @@ namespace bitstream::test::example
4949
BS_TEST_ASSERT(writer.serialize<int32_t>(value, -90, 40)); // A lower and upper bound which the value will be quantized between
5050

5151
// Flush the writer's remaining state into the buffer
52-
uint32_t num_bytes = writer.flush();
52+
uint32_t num_bits = writer.flush();
5353

54-
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
54+
BS_TEST_ASSERT_OPERATION(num_bits, <= , 32);
5555

5656
// Create a reader by moving and invalidating the writer
57-
bit_reader reader(buffer, num_bytes);
57+
bit_reader reader(buffer, num_bits);
5858

5959
// Read the value back
6060
int32_t out_value; // We don't have to initialize it yet
@@ -75,10 +75,10 @@ namespace bitstream::test::example
7575
BS_TEST_ASSERT(writer.serialize<const char*>(value, 32U)); // The second argument is the maximum size we expect the string to be
7676

7777
// Flush the writer's remaining state into the buffer
78-
uint32_t num_bytes = writer.flush();
78+
uint32_t num_bits = writer.flush();
7979

8080
// Create a reader by moving and invalidating the writer
81-
bit_reader reader(buffer, num_bytes);
81+
bit_reader reader(buffer, num_bits);
8282

8383
// Read the value back
8484
char out_value[32]; // Set the size to the max size
@@ -99,10 +99,10 @@ namespace bitstream::test::example
9999
BS_TEST_ASSERT(writer.serialize<std::string>(value, 32U)); // The second argument is the maximum size we expect the string to be
100100

101101
// Flush the writer's remaining state into the buffer
102-
uint32_t num_bytes = writer.flush();
102+
uint32_t num_bits = writer.flush();
103103

104104
// Create a reader by moving and invalidating the writer
105-
bit_reader reader(buffer, num_bytes);
105+
bit_reader reader(buffer, num_bits);
106106

107107
// Read the value back
108108
std::string out_value; // The string will be resized if the output doesn't fit
@@ -124,12 +124,12 @@ namespace bitstream::test::example
124124
writer.serialize<bounded_range>(range, value);
125125

126126
// Flush the writer's remaining state into the buffer
127-
uint32_t num_bytes = writer.flush();
127+
uint32_t num_bits = writer.flush();
128128

129-
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
129+
BS_TEST_ASSERT_OPERATION(num_bits, <= , 32);
130130

131131
// Create a reader by moving and invalidating the writer
132-
bit_reader reader(buffer, num_bytes);
132+
bit_reader reader(buffer, num_bits);
133133

134134
// Read the value back
135135
float out_value;

src/test/serialize_array_traits.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ namespace bitstream::test::traits
3131
bit_writer writer(buffer);
3232

3333
BS_TEST_ASSERT(writer.serialize<trait>(values_in, 6, compare)); // Use bounded_int for writing
34-
uint32_t num_bytes = writer.flush();
34+
uint32_t num_bits = writer.flush();
3535

36-
BS_TEST_ASSERT_OPERATION(num_bytes, == , 6);
36+
BS_TEST_ASSERT_OPERATION(writer.get_num_bytes_serialized(), == , 6);
3737

3838

3939
uint32_t values_out[6];
40-
bit_reader reader(buffer, num_bytes);
40+
bit_reader reader(buffer, num_bits);
4141

4242
BS_TEST_ASSERT(reader.serialize<array_subset<uint32_t>>(values_out, 6, compare, 0U, 2048U)); // Use min, max arguments for reading
4343

src/test/serialize_bool_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ namespace bitstream::test::traits
1818
bit_writer writer(buffer);
1919

2020
BS_TEST_ASSERT(writer.serialize<bool>(value));
21-
uint32_t num_bytes = writer.flush();
21+
uint32_t num_bits = writer.flush();
2222

23-
BS_TEST_ASSERT_OPERATION(num_bytes, == , 1);
23+
BS_TEST_ASSERT_OPERATION(num_bits, == , 1);
2424

2525
// Read the bool back and validate
2626
bool out_value;
27-
bit_reader reader(buffer, num_bytes);
27+
bit_reader reader(buffer, num_bits);
2828

2929
BS_TEST_ASSERT(reader.serialize<bool>(out_value));
3030

@@ -43,14 +43,14 @@ namespace bitstream::test::traits
4343

4444
BS_TEST_ASSERT(writer.serialize_bits(padding, 5U));
4545
BS_TEST_ASSERT(writer.serialize<bool>(value));
46-
uint32_t num_bytes = writer.flush();
46+
uint32_t num_bits = writer.flush();
4747

48-
BS_TEST_ASSERT_OPERATION(num_bytes, == , 1);
48+
BS_TEST_ASSERT_OPERATION(num_bits, == , 6);
4949

5050
// Read the bool back and validate
5151
uint32_t out_padding;
5252
bool out_value;
53-
bit_reader reader(buffer, num_bytes);
53+
bit_reader reader(buffer, num_bits);
5454

5555
BS_TEST_ASSERT(reader.serialize_bits(out_padding, 5U));
5656
BS_TEST_ASSERT(reader.serialize<bool>(out_value));

src/test/serialize_deduction_test.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace bitstream::test::deduction
2222
BS_TEST_ASSERT(writer.serialize(value, -90, 40)); // A lower and upper bound which the value will be quantized between
2323

2424
// Flush the writer's remaining state into the buffer
25-
uint32_t num_bytes = writer.flush();
25+
uint32_t num_bits = writer.flush();
2626

27-
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
27+
BS_TEST_ASSERT_OPERATION(num_bits, <= , 32);
2828

2929
// Create a reader by moving and invalidating the writer
30-
bit_reader reader(buffer, num_bytes);
30+
bit_reader reader(buffer, num_bits);
3131

3232
// Read the value back
3333
int32_t out_value; // We don't have to initialize it yet
@@ -47,10 +47,10 @@ namespace bitstream::test::deduction
4747
BS_TEST_ASSERT(writer.serialize(value, 32U)); // The second argument is the maximum size we expect the string to be
4848

4949
// Flush the writer's remaining state into the buffer
50-
uint32_t num_bytes = writer.flush();
50+
uint32_t num_bits = writer.flush();
5151

5252
// Create a reader by moving and invalidating the writer
53-
bit_reader reader(buffer, num_bytes);
53+
bit_reader reader(buffer, num_bits);
5454

5555
// Read the value back
5656
char out_value[32]; // Set the size to the max size
@@ -70,10 +70,10 @@ namespace bitstream::test::deduction
7070
BS_TEST_ASSERT(writer.serialize(value, 32U)); // The second argument is the maximum size we expect the string to be
7171

7272
// Flush the writer's remaining state into the buffer
73-
uint32_t num_bytes = writer.flush();
73+
uint32_t num_bits = writer.flush();
7474

7575
// Create a reader by moving and invalidating the writer
76-
bit_reader reader(buffer, num_bytes);
76+
bit_reader reader(buffer, num_bits);
7777

7878
// Read the value back
7979
std::string out_value; // The string will be resized if the output doesn't fit
@@ -94,12 +94,12 @@ namespace bitstream::test::deduction
9494
writer.serialize(range, value);
9595

9696
// Flush the writer's remaining state into the buffer
97-
uint32_t num_bytes = writer.flush();
97+
uint32_t num_bits = writer.flush();
9898

99-
BS_TEST_ASSERT_OPERATION(num_bytes, <= , 4);
99+
BS_TEST_ASSERT_OPERATION(num_bits, <= , 32);
100100

101101
// Create a reader by moving and invalidating the writer
102-
bit_reader reader(buffer, num_bytes);
102+
bit_reader reader(buffer, num_bits);
103103

104104
// Read the value back
105105
float out_value;

src/test/serialize_enum_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ namespace bitstream::test::traits
1919
bit_writer writer(buffer);
2020

2121
BS_TEST_ASSERT(writer.serialize<test_enum>(value, 1, 3));
22-
uint32_t num_bytes = writer.flush();
22+
uint32_t num_bits = writer.flush();
2323

24-
BS_TEST_ASSERT_OPERATION(num_bytes, == , 1);
24+
BS_TEST_ASSERT_OPERATION(num_bits, == , 2);
2525

2626
// Read the enum back and validate
2727
test_enum out_value;
28-
bit_reader reader(buffer, num_bytes);
28+
bit_reader reader(buffer, num_bits);
2929

3030
BS_TEST_ASSERT(reader.serialize<test_enum>(out_value, 1, 3));
3131

0 commit comments

Comments
 (0)