File tree Expand file tree Collapse file tree 4 files changed +51
-23
lines changed Expand file tree Collapse file tree 4 files changed +51
-23
lines changed Original file line number Diff line number Diff line change @@ -241,6 +241,19 @@ namespace bitstream
241241
242242BS_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+
244257if (m_ScratchBits < num_bits)
245258{
246259const uint32_t * ptr = m_Buffer + m_WordIndex;
Original file line number Diff line number Diff line change @@ -275,6 +275,19 @@ namespace bitstream
275275
276276BS_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+
278291uint32_t offset = 64U - num_bits - m_ScratchBits;
279292uint64_t ls_value = static_cast <uint64_t >(value) << offset;
280293
Original file line number Diff line number Diff 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 ())
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments