byteswap.hpp
Go to the documentation of this file.
#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.
*****************************************************************************/
#include <cstdint>
#include <cassert>
#include <protozero/config.hpp>
namespace protozero {
template <int N>
inline void byteswap(const char* /*data*/, char* /*result*/) {
static_assert(N == 1, "Can only swap 4 or 8 byte values");
}
template <>
inline void byteswap<4>(const char* data, char* result) {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
*reinterpret_cast<uint32_t*>(result) = __builtin_bswap32(*reinterpret_cast<const uint32_t*>(data));
#else
result[3] = data[0];
result[2] = data[1];
result[1] = data[2];
result[0] = data[3];
#endif
}
template <>
inline void byteswap<8>(const char* data, char* result) {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
*reinterpret_cast<uint64_t*>(result) = __builtin_bswap64(*reinterpret_cast<const uint64_t*>(data));
#else
result[7] = data[0];
result[6] = data[1];
result[5] = data[2];
result[4] = data[3];
result[3] = data[4];
result[2] = data[5];
result[1] = data[6];
result[0] = data[7];
#endif
}
} // end namespace protozero
#endif // PROTOZERO_BYTESWAP_HPP
protozero::byteswap< 4 >
void byteswap< 4 >(const char *data, char *result)
Definition: byteswap.hpp:39
protozero::byteswap< 8 >
void byteswap< 8 >(const char *data, char *result)
Definition: byteswap.hpp:54
config.hpp
Contains macro checks for different configurations.
protozero::byteswap
void byteswap(const char *, char *)
Definition: byteswap.hpp:31
protozero
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24