summaryrefslogtreecommitdiffstats
path: root/ext/protozero/include/protozero/byteswap.hpp
blob: 799d1795d57805e7af86d441fbe1a84e165da72f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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 "config.hpp"

#include <cstdint>

namespace protozero {
namespace detail {

inline uint32_t byteswap_impl(uint32_t value) noexcept {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
    return __builtin_bswap32(value);
#else
    return ((value & 0xff000000U) >> 24U) |
           ((value & 0x00ff0000U) >>  8U) |
           ((value & 0x0000ff00U) <<  8U) |
           ((value & 0x000000ffU) << 24U);
#endif
}

inline uint64_t byteswap_impl(uint64_t value) noexcept {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
    return __builtin_bswap64(value);
#else
    return ((value & 0xff00000000000000ULL) >> 56U) |
           ((value & 0x00ff000000000000ULL) >> 40U) |
           ((value & 0x0000ff0000000000ULL) >> 24U) |
           ((value & 0x000000ff00000000ULL) >>  8U) |
           ((value & 0x00000000ff000000ULL) <<  8U) |
           ((value & 0x0000000000ff0000ULL) << 24U) |
           ((value & 0x000000000000ff00ULL) << 40U) |
           ((value & 0x00000000000000ffULL) << 56U);
#endif
}

} // end namespace detail

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(uint32_t* ptr) noexcept {
    *ptr = detail::byteswap_impl(*ptr);
}

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(uint64_t* ptr) noexcept {
    *ptr = detail::byteswap_impl(*ptr);
}

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(int32_t* ptr) noexcept {
    auto* bptr = reinterpret_cast<uint32_t*>(ptr);
    *bptr = detail::byteswap_impl(*bptr);
}

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(int64_t* ptr) noexcept {
    auto* bptr = reinterpret_cast<uint64_t*>(ptr);
    *bptr = detail::byteswap_impl(*bptr);
}

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(float* ptr) noexcept {
    auto* bptr = reinterpret_cast<uint32_t*>(ptr);
    *bptr = detail::byteswap_impl(*bptr);
}

/// byteswap the data pointed to by ptr in-place.
inline void byteswap_inplace(double* ptr) noexcept {
    auto* bptr = reinterpret_cast<uint64_t*>(ptr);
    *bptr = detail::byteswap_impl(*bptr);
}

namespace detail {

    // Added for backwards compatibility with any code that might use this
    // function (even if it shouldn't have). Will be removed in a later
    // version of protozero.
    using ::protozero::byteswap_inplace;

} // end namespace detail

} // end namespace protozero

#endif // PROTOZERO_BYTESWAP_HPP