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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_BIT_BYTESWAP_HPP
5#define TETL_BIT_BYTESWAP_HPP
6
7#include <etl/_concepts/unsigned_integral.hpp>
8#include <etl/_config/all.hpp>
9#include <etl/_cstdint/uint_t.hpp>
10#include <etl/_type_traits/always_false.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 return (val << 56)
30 | ((val << 40) & 0x00FF'0000'0000'0000)
31 | ((val << 24) & 0x0000'FF00'0000'0000)
32 | ((val << 8) & 0x0000'00FF'0000'0000)
33 | ((val >> 8) & 0x0000'0000'FF00'0000)
34 | ((val >> 24) & 0x0000'0000'00FF'0000)
35 | ((val >> 40) & 0x0000'0000'0000'FF00)
36 | (val >> 56);
37 }
38
39} byteswap_fallback;
40
41inline constexpr struct byteswap {
42 [[nodiscard]] constexpr auto operator()(etl::uint16_t val) const noexcept -> etl::uint16_t
43 {
44#if __has_builtin(__builtin_bswap16)
45 return __builtin_bswap16(val);
46#else
47 return etl::detail::byteswap_fallback(val);
48#endif
49 }
50
51 [[nodiscard]] constexpr auto operator()(etl::uint32_t val) const noexcept -> etl::uint32_t
52 {
53#if __has_builtin(__builtin_bswap32)
54 return __builtin_bswap32(val);
55#else
56 return etl::detail::byteswap_fallback(val);
57#endif
58 }
59
60 [[nodiscard]] constexpr auto operator()(etl::uint64_t val) const noexcept -> etl::uint64_t
61 {
62#if __has_builtin(__builtin_bswap64)
63 return __builtin_bswap64(val);
64#else
65 return etl::detail::byteswap_fallback(val);
66#endif
67 }
68
69} byteswap;
70
71} // namespace detail
72
73/// \brief Reverses the bytes in the given integer value n.
74///
75/// \details etl::byteswap participates in overload resolution only if Int
76/// satisfies integral, i.e., Int is an integer type. The program is ill-formed if
77/// Int has padding bits.
78///
79/// https://en.cppreference.com/w/cpp/numeric/byteswap
80///
81/// \ingroup bit
82template <integral Int>
83[[nodiscard]] constexpr auto byteswap(Int val) noexcept -> Int
84{
85 if constexpr (sizeof(Int) == 1) {
86 return val;
87 } else if constexpr (sizeof(Int) == 2) {
88 return static_cast<Int>(detail::byteswap(static_cast<etl::uint16_t>(val)));
89 } else if constexpr (sizeof(Int) == 4) {
90 return static_cast<Int>(detail::byteswap(static_cast<etl::uint32_t>(val)));
91 } else if constexpr (sizeof(Int) == 8) {
92 return static_cast<Int>(detail::byteswap(static_cast<etl::uint64_t>(val)));
93 } else {
94 static_assert(always_false<Int>, "byteswap requires sizeof(Int) <= 8");
95 }
96}
97
98} // namespace etl
99
100#endif // TETL_BIT_BYTESWAP_HPP
constexpr auto byteswap(Int val) noexcept -> Int
Reverses the bytes in the given integer value n.
Definition byteswap.hpp:83
Definition adjacent_find.hpp:9