tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
byteswap.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BIT_BYTESWAP_HPP
4#define TETL_BIT_BYTESWAP_HPP
5
7#include <etl/_config/all.hpp>
11
12namespace etl {
13
14namespace detail {
15
16inline constexpr struct byteswap_fallback {
17 [[nodiscard]] constexpr auto operator()(etl::uint16_t val) const noexcept -> etl::uint16_t
18 {
19 return static_cast<etl::uint16_t>((val << 8) | (val >> 8));
20 }
21
22 [[nodiscard]] constexpr auto operator()(etl::uint32_t val) const noexcept -> etl::uint32_t
23 {
24 return (val << 24) | ((val << 8) & 0x00FF'0000) | ((val >> 8) & 0x0000'FF00) | (val >> 24);
25 }
26
27 [[nodiscard]] constexpr auto operator()(etl::uint64_t val) const noexcept -> etl::uint64_t
28 {
29 // clang-format off
30 return (val << 56)
31 | ((val << 40) & 0x00FF'0000'0000'0000)
32 | ((val << 24) & 0x0000'FF00'0000'0000)
33 | ((val << 8) & 0x0000'00FF'0000'0000)
34 | ((val >> 8) & 0x0000'0000'FF00'0000)
35 | ((val >> 24) & 0x0000'0000'00FF'0000)
36 | ((val >> 40) & 0x0000'0000'0000'FF00)
37 | (val >> 56);
38 // clang-format on
39 }
40
41} byteswap_fallback;
42
43inline constexpr struct byteswap {
44 [[nodiscard]] constexpr auto operator()(etl::uint16_t val) const noexcept -> etl::uint16_t
45 {
46#if __has_builtin(__builtin_bswap16)
47 return __builtin_bswap16(val);
48#else
49 return etl::detail::byteswap_fallback(val);
50#endif
51 }
52
53 [[nodiscard]] constexpr auto operator()(etl::uint32_t val) const noexcept -> etl::uint32_t
54 {
55#if __has_builtin(__builtin_bswap32)
56 return __builtin_bswap32(val);
57#else
58 return etl::detail::byteswap_fallback(val);
59#endif
60 }
61
62 [[nodiscard]] constexpr auto operator()(etl::uint64_t val) const noexcept -> etl::uint64_t
63 {
64#if __has_builtin(__builtin_bswap64)
65 return __builtin_bswap64(val);
66#else
67 return etl::detail::byteswap_fallback(val);
68#endif
69 }
70
71} byteswap;
72
73} // namespace detail
74
84template <integral Int>
85[[nodiscard]] constexpr auto byteswap(Int val) noexcept -> Int
86{
87 if constexpr (sizeof(Int) == 1) {
88 return val;
89 } else if constexpr (sizeof(Int) == 2) {
90 return static_cast<Int>(detail::byteswap(static_cast<etl::uint16_t>(val)));
91 } else if constexpr (sizeof(Int) == 4) {
92 return static_cast<Int>(detail::byteswap(static_cast<etl::uint32_t>(val)));
93 } else if constexpr (sizeof(Int) == 8) {
94 return static_cast<Int>(detail::byteswap(static_cast<etl::uint64_t>(val)));
95 } else {
96 static_assert(always_false<Int>, "byteswap requires sizeof(Int) <= 8");
97 }
98}
99
100} // namespace etl
101
102#endif // TETL_BIT_BYTESWAP_HPP
constexpr auto byteswap(Int val) noexcept -> Int
Reverses the bytes in the given integer value n.
Definition byteswap.hpp:85
auto val(pin_number pin) -> etl::uint16_t
Definition gpio.hpp:37
Definition adjacent_find.hpp:8
TETL_BUILTIN_UINT16 uint16_t
Unsigned integer type with width of exactly 16 bits.
Definition uint_t.hpp:14
TETL_BUILTIN_UINT64 uint64_t
Unsigned integer type with width of exactly 64 bits.
Definition uint_t.hpp:20
constexpr bool always_false
Definition always_false.hpp:9
TETL_BUILTIN_UINT32 uint32_t
Unsigned integer type with width of exactly 32 bits.
Definition uint_t.hpp:17