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#include <etl/_type_traits/is_constant_evaluated.hpp>
12
13namespace etl {
14
15namespace detail {
16
17inline constexpr struct byteswap_fallback {
18 [[nodiscard]] constexpr auto operator()(etl::uint16_t val) const noexcept -> etl::uint16_t
19 {
20 return static_cast<etl::uint16_t>((val << 8) | (val >> 8));
21 }
22
23 [[nodiscard]] constexpr auto operator()(etl::uint32_t val) const noexcept -> etl::uint32_t
24 {
25 return (val << 24) | ((val << 8) & 0x00FF'0000) | ((val >> 8) & 0x0000'FF00) | (val >> 24);
26 }
27
28 [[nodiscard]] constexpr auto operator()(etl::uint64_t val) const noexcept -> etl::uint64_t
29 {
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 }
39
40} byteswap_fallback;
41
42inline constexpr struct byteswap {
43 [[nodiscard]] constexpr auto operator()(etl::uint16_t val) const noexcept -> etl::uint16_t
44 {
45#if __has_builtin(__builtin_bswap16)
46 return __builtin_bswap16(val);
47#else
48 return etl::detail::byteswap_fallback(val);
49#endif
50 }
51
52 [[nodiscard]] constexpr auto operator()(etl::uint32_t val) const noexcept -> etl::uint32_t
53 {
54#if __has_builtin(__builtin_bswap32)
55 return __builtin_bswap32(val);
56#else
57 return etl::detail::byteswap_fallback(val);
58#endif
59 }
60
61 [[nodiscard]] constexpr auto operator()(etl::uint64_t val) const noexcept -> etl::uint64_t
62 {
63#if __has_builtin(__builtin_bswap64)
64 return __builtin_bswap64(val);
65#else
66 return etl::detail::byteswap_fallback(val);
67#endif
68 }
69
70} byteswap;
71
72} // namespace detail
73
74/// \brief Reverses the bytes in the given integer value n.
75///
76/// \details etl::byteswap participates in overload resolution only if Int
77/// satisfies integral, i.e., Int is an integer type. The program is ill-formed if
78/// Int has padding bits.
79///
80/// https://en.cppreference.com/w/cpp/numeric/byteswap
81///
82/// \ingroup bit
83template <integral Int>
84[[nodiscard]] constexpr auto byteswap(Int val) noexcept -> Int
85{
86 if constexpr (sizeof(Int) == 1) {
87 return val;
88 } else if constexpr (sizeof(Int) == 2) {
89 return static_cast<Int>(detail::byteswap(static_cast<etl::uint16_t>(val)));
90 } else if constexpr (sizeof(Int) == 4) {
91 return static_cast<Int>(detail::byteswap(static_cast<etl::uint32_t>(val)));
92 } else if constexpr (sizeof(Int) == 8) {
93 return static_cast<Int>(detail::byteswap(static_cast<etl::uint64_t>(val)));
94 } else {
95 static_assert(always_false<Int>, "byteswap requires sizeof(Int) <= 8");
96 }
97}
98
99} // namespace etl
100
101#endif // TETL_BIT_BYTESWAP_HPP
constexpr auto byteswap(Int val) noexcept -> Int
Reverses the bytes in the given integer value n.
Definition byteswap.hpp:84
Definition adjacent_find.hpp:9