protozero: include/protozero/byteswap.hpp Source File

protozero 1.8.0 Minimalistic protocol buffer decoder and encoder in C++.

Loading...
Searching...
No Matches
byteswap.hpp
Go to the documentation of this file.
1#ifndef PROTOZERO_BYTESWAP_HPP
2#define PROTOZERO_BYTESWAP_HPP
3
4/*****************************************************************************
5
6protozero - Minimalistic protocol buffer decoder and encoder in C++.
7
8This file is from https://github.com/mapbox/protozero where you can find more
9documentation.
10
11*****************************************************************************/
12
18
19#include "config.hpp"
20
21#include <cstdint>
22#include <cstring>
23
24namespace protozero {
25namespace detail {
26
27inline uint32_t byteswap_impl(uint32_t value) noexcept {
28#ifdef PROTOZERO_USE_BUILTIN_BSWAP
29 return __builtin_bswap32(value);
30#else
31 return ((value & 0xff000000U) >> 24U) |
32 ((value & 0x00ff0000U) >> 8U) |
33 ((value & 0x0000ff00U) << 8U) |
34 ((value & 0x000000ffU) << 24U);
35#endif
36}
37
38inline uint64_t byteswap_impl(uint64_t value) noexcept {
39#ifdef PROTOZERO_USE_BUILTIN_BSWAP
40 return __builtin_bswap64(value);
41#else
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);
50#endif
51}
52
53} // end namespace detail
54
56 inline void byteswap_inplace(uint32_t* ptr) noexcept {
57 *ptr = detail::byteswap_impl(*ptr);
58}
59
61 inline void byteswap_inplace(uint64_t* ptr) noexcept {
62 *ptr = detail::byteswap_impl(*ptr);
63}
64
66 inline void byteswap_inplace(int32_t* ptr) noexcept {
67 auto* bptr = reinterpret_cast<uint32_t*>(ptr);
68 *bptr = detail::byteswap_impl(*bptr);
69}
70
72 inline void byteswap_inplace(int64_t* ptr) noexcept {
73 auto* bptr = reinterpret_cast<uint64_t*>(ptr);
74 *bptr = detail::byteswap_impl(*bptr);
75}
76
78 inline void byteswap_inplace(float* ptr) noexcept {
79 static_assert(sizeof(float) == 4, "Expecting four byte float");
80
81 uint32_t tmp = 0;
82 std::memcpy(&tmp, ptr, 4);
83 tmp = detail::byteswap_impl(tmp); // uint32 overload
84 std::memcpy(ptr, &tmp, 4);
85}
86
88 inline void byteswap_inplace(double* ptr) noexcept {
89 static_assert(sizeof(double) == 8, "Expecting eight byte double");
90
91 uint64_t tmp = 0;
92 std::memcpy(&tmp, ptr, 8);
93 tmp = detail::byteswap_impl(tmp); // uint64 overload
94 std::memcpy(ptr, &tmp, 8);
95}
96
97namespace detail {
98
99 // Added for backwards compatibility with any code that might use this
100 // function (even if it shouldn't have). Will be removed in a later
101 // version of protozero.
102 using ::protozero::byteswap_inplace;
103
104} // end namespace detail
105
106} // end namespace protozero
107
108#endif // PROTOZERO_BYTESWAP_HPP
config.hpp
Contains macro checks for different configurations.
protozero
All parts of the protozero header-only library are in this namespace.
Definition basic_pbf_builder.hpp:24
protozero::byteswap_inplace
void byteswap_inplace(uint32_t *ptr) noexcept
byteswap the data pointed to by ptr in-place.
Definition byteswap.hpp:56