@@ -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}
0 commit comments