2016-04-23 00:10:33 +00:00
|
|
|
#ifndef PROTOZERO_BYTESWAP_HPP
|
|
|
|
#define PROTOZERO_BYTESWAP_HPP
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
|
|
|
protozero - Minimalistic protocol buffer decoder and encoder in C++.
|
|
|
|
|
|
|
|
This file is from https://github.com/mapbox/protozero where you can find more
|
|
|
|
documentation.
|
|
|
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file byteswap.hpp
|
|
|
|
*
|
|
|
|
* @brief Contains functions to swap bytes in values (for different endianness).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
2017-06-30 18:20:26 +00:00
|
|
|
#include <cstdint>
|
2016-04-23 00:10:33 +00:00
|
|
|
|
|
|
|
#include <protozero/config.hpp>
|
|
|
|
|
|
|
|
namespace protozero {
|
2017-06-30 18:20:26 +00:00
|
|
|
namespace detail {
|
2016-04-23 00:10:33 +00:00
|
|
|
|
2017-06-30 18:20:26 +00:00
|
|
|
inline uint32_t byteswap_impl(uint32_t value) noexcept {
|
2016-04-23 00:10:33 +00:00
|
|
|
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
|
2017-06-30 18:20:26 +00:00
|
|
|
return __builtin_bswap32(value);
|
2016-04-23 00:10:33 +00:00
|
|
|
#else
|
2017-06-30 18:20:26 +00:00
|
|
|
return ((value & 0xff000000) >> 24) |
|
|
|
|
((value & 0x00ff0000) >> 8) |
|
|
|
|
((value & 0x0000ff00) << 8) |
|
|
|
|
((value & 0x000000ff) << 24);
|
2016-04-23 00:10:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:20:26 +00:00
|
|
|
inline uint64_t byteswap_impl(uint64_t value) noexcept {
|
2016-04-23 00:10:33 +00:00
|
|
|
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
|
2017-06-30 18:20:26 +00:00
|
|
|
return __builtin_bswap64(value);
|
2016-04-23 00:10:33 +00:00
|
|
|
#else
|
2017-06-30 18:20:26 +00:00
|
|
|
return ((value & 0xff00000000000000ULL) >> 56) |
|
|
|
|
((value & 0x00ff000000000000ULL) >> 40) |
|
|
|
|
((value & 0x0000ff0000000000ULL) >> 24) |
|
|
|
|
((value & 0x000000ff00000000ULL) >> 8) |
|
|
|
|
((value & 0x00000000ff000000ULL) << 8) |
|
|
|
|
((value & 0x0000000000ff0000ULL) << 24) |
|
|
|
|
((value & 0x000000000000ff00ULL) << 40) |
|
|
|
|
((value & 0x00000000000000ffULL) << 56);
|
2016-04-23 00:10:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:20:26 +00:00
|
|
|
inline void byteswap_inplace(uint32_t* ptr) noexcept {
|
|
|
|
*ptr = byteswap_impl(*ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void byteswap_inplace(uint64_t* ptr) noexcept {
|
|
|
|
*ptr = byteswap_impl(*ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void byteswap_inplace(int32_t* ptr) noexcept {
|
|
|
|
auto bptr = reinterpret_cast<uint32_t*>(ptr);
|
|
|
|
*bptr = byteswap_impl(*bptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void byteswap_inplace(int64_t* ptr) noexcept {
|
|
|
|
auto bptr = reinterpret_cast<uint64_t*>(ptr);
|
|
|
|
*bptr = byteswap_impl(*bptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void byteswap_inplace(float* ptr) noexcept {
|
|
|
|
auto bptr = reinterpret_cast<uint32_t*>(ptr);
|
|
|
|
*bptr = byteswap_impl(*bptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void byteswap_inplace(double* ptr) noexcept {
|
|
|
|
auto bptr = reinterpret_cast<uint64_t*>(ptr);
|
|
|
|
*bptr = byteswap_impl(*bptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace detail
|
2016-04-23 00:10:33 +00:00
|
|
|
} // end namespace protozero
|
|
|
|
|
|
|
|
#endif // PROTOZERO_BYTESWAP_HPP
|