Skip to content

Commit 1211b7e

Browse files
authored
Merge pull request #2 from KredeGC/dev
More tests and comments
2 parents 2461264 + bc45866 commit 1211b7e

File tree

7 files changed

+224
-147
lines changed

7 files changed

+224
-147
lines changed

include/bitstream/stream/bit_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace bitstream
174174
/**
175175
* @brief Pads the buffer with up to 8 zeros, so that the next read is byte-aligned
176176
* @notes Return false if the padded bits are not zeros
177-
* @return Success
177+
* @return Returns false if the padded bits are not zeros
178178
*/
179179
bool align() noexcept
180180
{

include/bitstream/traits/integral_traits.h

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ namespace bitstream
4040
if constexpr (sizeof(T) > 4)
4141
{
4242
// If the given range is bigger than a word (32 bits)
43-
int max_words = (num_bits - 1) / 32 + 1;
44-
for (int i = 0; i < max_words; i++)
45-
{
46-
int shift = i * 32;
47-
int bit_count = (std::min)(num_bits - shift, 32);
48-
uint32_t unsigned_value = static_cast<uint32_t>((value - min) >> shift);
49-
BS_ASSERT(writer.serialize_bits(unsigned_value, bit_count));
50-
}
43+
uint32_t unsigned_value = static_cast<uint32_t>(value - min);
44+
BS_ASSERT(writer.serialize_bits(unsigned_value, 32));
45+
46+
unsigned_value = static_cast<uint32_t>((value - min) >> 32);
47+
BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits - 32));
5148
}
5249
else
5350
{
@@ -69,19 +66,15 @@ namespace bitstream
6966

7067
if constexpr (sizeof(T) > 4)
7168
{
69+
// If the given range is bigger than a word (32 bits)
7270
value = 0;
71+
uint32_t unsigned_value;
7372

74-
// If the given range is bigger than a word (32 bits)
75-
int max_words = (num_bits - 1) / 32 + 1;
76-
for (int i = 0; i < max_words; i++)
77-
{
78-
uint32_t unsigned_value;
79-
int shift = i * 32;
80-
int bit_count = (std::min)(num_bits - shift, 32);
81-
BS_ASSERT(reader.serialize_bits(unsigned_value, bit_count));
73+
BS_ASSERT(reader.serialize_bits(unsigned_value, 32));
74+
value |= static_cast<T>(unsigned_value);
8275

83-
value |= static_cast<T>(unsigned_value) << shift;
84-
}
76+
BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits - 32));
77+
value |= static_cast<T>(unsigned_value) << 32;
8578

8679
value += min;
8780
}
@@ -124,14 +117,11 @@ namespace bitstream
124117
if constexpr (sizeof(T) > 4 && num_bits > 32)
125118
{
126119
// If the given range is bigger than a word (32 bits)
127-
int max_words = (num_bits - 1) / 32 + 1;
128-
for (int i = 0; i < max_words; i++)
129-
{
130-
int shift = i * 32;
131-
int bit_count = (std::min)(num_bits - shift, 32);
132-
uint32_t unsigned_value = static_cast<uint32_t>((value - Min) >> shift);
133-
BS_ASSERT(writer.serialize_bits(unsigned_value, bit_count));
134-
}
120+
uint32_t unsigned_value = static_cast<uint32_t>(value - Min);
121+
BS_ASSERT(writer.serialize_bits(unsigned_value, 32));
122+
123+
unsigned_value = static_cast<uint32_t>((value - Min) >> 32);
124+
BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits - 32));
135125
}
136126
else
137127
{
@@ -153,19 +143,15 @@ namespace bitstream
153143

154144
if constexpr (sizeof(T) > 4 && num_bits > 32)
155145
{
146+
// If the given range is bigger than a word (32 bits)
156147
value = 0;
148+
uint32_t unsigned_value;
157149

158-
// If the given range is bigger than a word (32 bits)
159-
int max_words = (num_bits - 1) / 32 + 1;
160-
for (int i = 0; i < max_words; i++)
161-
{
162-
uint32_t unsigned_value;
163-
int shift = i * 32;
164-
int bit_count = (std::min)(num_bits - shift, 32);
165-
BS_ASSERT(reader.serialize_bits(unsigned_value, bit_count));
166-
167-
value |= static_cast<T>(unsigned_value) << shift;
168-
}
150+
BS_ASSERT(reader.serialize_bits(unsigned_value, 32));
151+
value |= static_cast<T>(unsigned_value);
152+
153+
BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits - 32));
154+
value |= static_cast<T>(unsigned_value) << 32;
169155

170156
value += Min;
171157
}

include/bitstream/traits/string_traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ namespace bitstream
103103
}
104104

105105
/**
106-
* @brief Reads a string from the @p writer into @p value
106+
* @brief Reads a string from the @p reader into @p value
107107
* @param reader The stream to read from
108108
* @param value The string to read into. It will be resized if the read string won't fit
109109
* @param max_size The maximum expected length of the string

test/include/test_types.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
5+
namespace bitstream::test
6+
{
7+
struct quaternion
8+
{
9+
// smallest_three supports any combination of w, x, y and z, as long as it's consistent
10+
float w;
11+
float x;
12+
float y;
13+
float z;
14+
15+
quaternion() = default;
16+
17+
// The constructor order must be the same as the operator[]
18+
quaternion(float w, float x, float y, float z)
19+
: w(w), x(x), y(y), z(z) {}
20+
21+
// smallest_three uses this operator
22+
float operator[](size_t index) const
23+
{
24+
return reinterpret_cast<const float*>(this)[index];
25+
}
26+
};
27+
}

test/src/test/quantization_test.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "test_assert.h"
22
#include "test.h"
3+
#include "test_types.h"
34

45
#include <bitstream/quantization/bounded_range.h>
56
#include <bitstream/quantization/half_precision.h>
@@ -10,27 +11,6 @@
1011

1112
namespace bitstream::test::quantization
1213
{
13-
struct quaternion
14-
{
15-
// smallest_three supports any combination of w, x, y and z, as long as it's consistent
16-
float w;
17-
float x;
18-
float y;
19-
float z;
20-
21-
quaternion() = default;
22-
23-
// The constructor order must be the same as the operator[]
24-
quaternion(float w, float x, float y, float z)
25-
: w(w), x(x), y(y), z(z) {}
26-
27-
// smallest_three uses this operator
28-
float operator[](size_t index) const
29-
{
30-
return reinterpret_cast<const float*>(this)[index];
31-
}
32-
};
33-
3414
BS_ADD_TEST(test_half_precision)
3515
{
3616
float value_in = 3.141592f;

0 commit comments

Comments
 (0)