Skip to content

Commit cc7178f

Browse files
committed
Add pad() to bit reader and writer
Remove wrong noexcept from string serialize read Add more endian swap functions
1 parent 3916af0 commit cc7178f

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

include/bitstream/stream/bit_reader.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ namespace bitstream
200200
return true;
201201
}
202202

203+
/**
204+
* @brief Pads the buffer up with the given number of bytes
205+
* @param num_bytes The amount of bytes to pad
206+
* @return Returns false if the current size of the buffer is bigger than @p num_bytes or if the padded bits are not zeros.
207+
*/
208+
[[nodiscard]] bool pad(uint32_t num_bytes) noexcept
209+
{
210+
return pad_to_size(get_num_bytes_serialized() + num_bytes);
211+
}
212+
203213
/**
204214
* @brief Pads the buffer with up to 8 zeros, so that the next read is byte-aligned
205215
* @notes Return false if the padded bits are not zeros
@@ -235,7 +245,7 @@ namespace bitstream
235245
{
236246
const uint32_t* ptr = m_Buffer + m_WordIndex;
237247

238-
uint64_t ptr_value = static_cast<uint64_t>(utility::endian_swap_32(*ptr)) << (32U - m_ScratchBits);
248+
uint64_t ptr_value = static_cast<uint64_t>(utility::to_big_endian32(*ptr)) << (32U - m_ScratchBits);
239249
m_Scratch |= ptr_value;
240250
m_ScratchBits += 32U;
241251
m_WordIndex++;
@@ -285,7 +295,7 @@ namespace bitstream
285295

286296
// Casting a byte-array to an int is wrong on little-endian systems
287297
// We have to swap the bytes around
288-
word_buffer[i] = utility::endian_swap_32(value);
298+
word_buffer[i] = utility::to_big_endian32(value);
289299
}
290300
}
291301

include/bitstream/stream/bit_writer.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace bitstream
153153
{
154154
uint32_t* ptr = m_Buffer + m_WordIndex;
155155
uint32_t ptr_value = static_cast<uint32_t>(m_Scratch >> 32U);
156-
*ptr = utility::endian_swap_32(ptr_value);
156+
*ptr = utility::to_big_endian32(ptr_value);
157157

158158
m_Scratch = 0U;
159159
m_ScratchBits = 0U;
@@ -241,6 +241,16 @@ namespace bitstream
241241
return true;
242242
}
243243

244+
/**
245+
* @brief Pads the buffer up with the given number of bytes
246+
* @param num_bytes The amount of bytes to pad
247+
* @return Returns false if the current size of the buffer is bigger than @p num_bytes or if the padded bits are not zeros.
248+
*/
249+
[[nodiscard]] bool pad(uint32_t num_bytes) noexcept
250+
{
251+
return pad_to_size(get_num_bytes_serialized() + num_bytes);
252+
}
253+
244254
/**
245255
* @brief Pads the buffer with up to 8 zeros, so that the next write is byte-aligned
246256
* @return Success
@@ -281,7 +291,7 @@ namespace bitstream
281291
{
282292
uint32_t* ptr = m_Buffer + m_WordIndex;
283293
uint32_t ptr_value = static_cast<uint32_t>(m_Scratch >> 32U);
284-
*ptr = utility::endian_swap_32(ptr_value);
294+
*ptr = utility::to_big_endian32(ptr_value);
285295
m_Scratch <<= 32ULL;
286296
m_ScratchBits -= 32U;
287297
m_WordIndex++;
@@ -321,7 +331,7 @@ namespace bitstream
321331
{
322332
// Casting a byte-array to an int is wrong on little-endian systems
323333
// We have to swap the bytes around
324-
uint32_t value = utility::endian_swap_32(word_buffer[i]);
334+
uint32_t value = utility::to_big_endian32(word_buffer[i]);
325335
BS_ASSERT(serialize_bits(value, 32U));
326336
}
327337
}

include/bitstream/traits/string_traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ namespace bitstream
253253
*/
254254
template<typename Stream>
255255
typename utility::is_reading_t<Stream>
256-
static serialize(Stream& reader, out<std::basic_string<T, Traits, Alloc>> value, uint32_t max_size) noexcept
256+
static serialize(Stream& reader, out<std::basic_string<T, Traits, Alloc>> value, uint32_t max_size)
257257
{
258258
uint32_t num_bits = utility::bits_to_represent(max_size);
259259

@@ -318,7 +318,7 @@ namespace bitstream
318318
*/
319319
template<typename Stream>
320320
typename utility::is_reading_t<Stream>
321-
static serialize(Stream& reader, out<std::basic_string<T, Traits, Alloc>> value) noexcept
321+
static serialize(Stream& reader, out<std::basic_string<T, Traits, Alloc>> value)
322322
{
323323
constexpr uint32_t num_bits = utility::bits_to_represent(MaxSize);
324324

include/bitstream/utility/endian.h

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,50 @@ namespace bitstream::utility
6363
#endif // defined(BS_LITTLE_ENDIAN)
6464
}
6565

66-
inline uint32_t endian_swap_32(uint32_t value)
67-
{
68-
if constexpr (little_endian())
69-
{
66+
inline uint32_t endian_swap32(uint32_t value)
67+
{
7068
#if defined(_WIN32)
71-
return _byteswap_ulong(value);
69+
return _byteswap_ulong(value);
7270
#elif defined(__linux__)
73-
return __builtin_bswap32(value);
71+
return __builtin_bswap32(value);
7472
#else
75-
const uint32_t first = (value << 24) & 0xFF000000;
76-
const uint32_t second = (value << 8) & 0x00FF0000;
77-
const uint32_t third = (value >> 8) & 0x0000FF00;
78-
const uint32_t fourth = (value >> 24) & 0x000000FF;
73+
const uint32_t first = (value << 24) & 0xFF000000;
74+
const uint32_t second = (value << 8) & 0x00FF0000;
75+
const uint32_t third = (value >> 8) & 0x0000FF00;
76+
const uint32_t fourth = (value >> 24) & 0x000000FF;
7977

80-
return first | second | third | fourth;
78+
return first | second | third | fourth;
8179
#endif // _WIN32 || __linux__
82-
}
80+
}
81+
82+
constexpr inline uint32_t endian_swap24(uint32_t value)
83+
{
84+
const uint32_t first = (value << 16) & 0x00FF0000;
85+
const uint32_t second = (value << 8) & 0x0000FF00;
86+
const uint32_t third = (value >> 16) & 0x000000FF;
87+
88+
return first | second | third;
89+
}
90+
91+
inline uint32_t endian_swap16(uint32_t value)
92+
{
93+
#if defined(_WIN32)
94+
return _byteswap_ushort(value);
95+
#elif defined(__linux__)
96+
return __builtin_bswap16(value);
97+
#else
98+
const uint32_t first = (value << 8) & 0x0000FF00;
99+
const uint32_t second = (value >> 8) & 0x000000FF;
100+
101+
return first | second;
102+
#endif // _WIN32 || __linux__
103+
}
104+
105+
inline uint32_t to_big_endian32(uint32_t value)
106+
{
107+
if constexpr (little_endian())
108+
return endian_swap32(value);
83109
else
84-
{
85110
return value;
86-
}
87-
}
111+
}
88112
}

include/bitstream/utility/parameter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace bitstream
9191
* @brief Passes by const or const reference depending on size
9292
*/
9393
template<typename T>
94-
using in = std::conditional_t<(sizeof(T) < 16), std::add_const_t<T>, std::add_lvalue_reference_t<std::add_const_t<T>>>;
94+
using in = std::conditional_t<(sizeof(T) <= 16), std::add_const_t<T>, std::add_lvalue_reference_t<std::add_const_t<T>>>;
9595

9696
/**
9797
* @brief Passes by reference

0 commit comments

Comments
 (0)