1 #ifndef PROTOZERO_BYTESWAP_HPP
2 #define PROTOZERO_BYTESWAP_HPP
27 inline uint32_t byteswap_impl(uint32_t value) noexcept {
28 #ifdef PROTOZERO_USE_BUILTIN_BSWAP
29 return __builtin_bswap32(value);
31 return ((value & 0xff000000U) >> 24U) |
32 ((value & 0x00ff0000U) >> 8U) |
33 ((value & 0x0000ff00U) << 8U) |
34 ((value & 0x000000ffU) << 24U);
38 inline uint64_t byteswap_impl(uint64_t value) noexcept {
39 #ifdef PROTOZERO_USE_BUILTIN_BSWAP
40 return __builtin_bswap64(value);
42 return ((value & 0xff00000000000000ULL) >> 56U) |
43 ((value & 0x00ff000000000000ULL) >> 40U) |
44 ((value & 0x0000ff0000000000ULL) >> 24U) |
45 ((value & 0x000000ff00000000ULL) >> 8U) |
46 ((value & 0x00000000ff000000ULL) << 8U) |
47 ((value & 0x0000000000ff0000ULL) << 24U) |
48 ((value & 0x000000000000ff00ULL) << 40U) |
49 ((value & 0x00000000000000ffULL) << 56U);
57 *ptr = detail::byteswap_impl(*ptr);
62 *ptr = detail::byteswap_impl(*ptr);
67 auto* bptr =
reinterpret_cast<uint32_t*
>(ptr);
68 *bptr = detail::byteswap_impl(*bptr);
73 auto* bptr =
reinterpret_cast<uint64_t*
>(ptr);
74 *bptr = detail::byteswap_impl(*bptr);
79 static_assert(
sizeof(
float) == 4,
"Expecting four byte float");
82 std::memcpy(&tmp, ptr, 4);
83 tmp = detail::byteswap_impl(tmp);
84 std::memcpy(ptr, &tmp, 4);
89 static_assert(
sizeof(
double) == 8,
"Expecting eight byte double");
92 std::memcpy(&tmp, ptr, 8);
93 tmp = detail::byteswap_impl(tmp);
94 std::memcpy(ptr, &tmp, 8);
Contains macro checks for different configurations.
All parts of the protozero header-only library are in this namespace.
Definition: basic_pbf_builder.hpp:24
void byteswap_inplace(uint32_t *ptr) noexcept
byteswap the data pointed to by ptr in-place.
Definition: byteswap.hpp:56
void byteswap_inplace(double *ptr) noexcept
byteswap the data pointed to by ptr in-place.
Definition: byteswap.hpp:88