Skip to content

Commit acf669a

Browse files
committed
Remove unused endian operations
Add fast path to serialize_bits
1 parent 48cb8f4 commit acf669a

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

include/bitstream/stream/bit_reader.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ namespace bitstream
241241

242242
BS_ASSERT(can_serialize_bits(num_bits));
243243

244+
// Fast path
245+
if (num_bits == 32U && m_ScratchBits == 0U)
246+
{
247+
const uint32_t* ptr = m_Buffer + m_WordIndex;
248+
249+
value = utility::to_big_endian32(*ptr);
250+
251+
m_NumBitsRead += num_bits;
252+
m_WordIndex++;
253+
254+
return true;
255+
}
256+
244257
if (m_ScratchBits < num_bits)
245258
{
246259
const uint32_t* ptr = m_Buffer + m_WordIndex;

include/bitstream/stream/bit_writer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ namespace bitstream
275275

276276
BS_ASSERT(can_serialize_bits(num_bits));
277277

278+
// Fast path
279+
if (num_bits == 32U && m_ScratchBits == 0U)
280+
{
281+
uint32_t* ptr = m_Buffer + m_WordIndex;
282+
283+
*ptr = utility::to_big_endian32(value);
284+
285+
m_NumBitsWritten += num_bits;
286+
m_WordIndex++;
287+
288+
return true;
289+
}
290+
278291
uint32_t offset = 64U - num_bits - m_ScratchBits;
279292
uint64_t ls_value = static_cast<uint64_t>(value) << offset;
280293

include/bitstream/utility/endian.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,6 @@ namespace bitstream::utility
7979
#endif // _WIN32 || __linux__
8080
}
8181

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(static_cast<uint16_t>(value));
95-
#elif defined(__linux__)
96-
return __builtin_bswap16(static_cast<uint16_t>(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-
10582
inline uint32_t to_big_endian32(uint32_t value)
10683
{
10784
if constexpr (little_endian())

src/shared/test_types.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include <bitstream/stream/serialize_traits.h>
4+
35
#include <bitstream/utility/assert.h>
6+
#include <bitstream/utility/parameter.h>
47

58
#include <cstddef>
69

@@ -19,11 +22,33 @@ namespace bitstream::test
1922
return values[index];
2023
}
2124
};
25+
26+
struct custom_type
27+
{
28+
bool enabled = true;
29+
int count = 42;
30+
};
2231

2332
enum class test_enum
2433
{
2534
FirstValue = 3,
2635
SecondValue = 1,
2736
ThirdValue
2837
};
38+
}
39+
40+
namespace bitstream
41+
{
42+
template<>
43+
struct serialize_traits<bitstream::test::custom_type>
44+
{
45+
template<typename Stream>
46+
static bool serialize(Stream& stream, inout<Stream, bitstream::test::custom_type> value) noexcept
47+
{
48+
if (!stream.template serialize<bool>(value.enabled))
49+
return false;
50+
51+
return stream.template serialize<int>(value.count);
52+
}
53+
};
2954
}

0 commit comments

Comments
 (0)