protozero
Minimalistic protocol buffer decoder and encoder in C++.
byteswap.hpp
Go to the documentation of this file.
1 #ifndef PROTOZERO_BYTESWAP_HPP
2 #define PROTOZERO_BYTESWAP_HPP
3 
4 /*****************************************************************************
5 
6 protozero - Minimalistic protocol buffer decoder and encoder in C++.
7 
8 This file is from https://github.com/mapbox/protozero where you can find more
9 documentation.
10 
11 *****************************************************************************/
12 
19 #include <cstdint>
20 #include <cassert>
21 
22 #include <protozero/config.hpp>
23 
24 namespace protozero {
25 
30 template <int N>
31 inline void byteswap(const char* /*data*/, char* /*result*/) noexcept {
32  static_assert(N == 1, "Can only swap 4 or 8 byte values");
33 }
34 
38 template <>
39 inline void byteswap<4>(const char* data, char* result) noexcept {
40 #ifdef PROTOZERO_USE_BUILTIN_BSWAP
41  *reinterpret_cast<uint32_t*>(result) = __builtin_bswap32(*reinterpret_cast<const uint32_t*>(data));
42 #else
43  result[3] = data[0];
44  result[2] = data[1];
45  result[1] = data[2];
46  result[0] = data[3];
47 #endif
48 }
49 
53 template <>
54 inline void byteswap<8>(const char* data, char* result) noexcept {
55 #ifdef PROTOZERO_USE_BUILTIN_BSWAP
56  *reinterpret_cast<uint64_t*>(result) = __builtin_bswap64(*reinterpret_cast<const uint64_t*>(data));
57 #else
58  result[7] = data[0];
59  result[6] = data[1];
60  result[5] = data[2];
61  result[4] = data[3];
62  result[3] = data[4];
63  result[2] = data[5];
64  result[1] = data[6];
65  result[0] = data[7];
66 #endif
67 }
68 
69 } // end namespace protozero
70 
71 #endif // PROTOZERO_BYTESWAP_HPP
void byteswap(const char *, char *) noexcept
Definition: byteswap.hpp:31
Contains macro checks for different configurations.
void byteswap< 8 >(const char *data, char *result) noexcept
Definition: byteswap.hpp:54
void byteswap< 4 >(const char *data, char *result) noexcept
Definition: byteswap.hpp:39
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24