4#ifndef TETL_BIT_BYTESWAP_HPP
5#define TETL_BIT_BYTESWAP_HPP
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>
17inline constexpr struct byteswap_fallback {
18 [[nodiscard]]
constexpr auto operator()(
etl::uint16_t val)
const noexcept ->
etl::uint16_t
20 return static_cast<
etl::uint16_t>((val << 8) | (val >> 8));
23 [[nodiscard]]
constexpr auto operator()(
etl::uint32_t val)
const noexcept ->
etl::uint32_t
25 return (val << 24) | ((val << 8) & 0x00FF'0000) | ((val >> 8) & 0x0000'FF00) | (val >> 24);
28 [[nodiscard]]
constexpr auto operator()(
etl::uint64_t val)
const noexcept ->
etl::uint64_t
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)
42inline constexpr struct byteswap {
43 [[nodiscard]]
constexpr auto operator()(
etl::uint16_t val)
const noexcept ->
etl::uint16_t
45#if __has_builtin(__builtin_bswap16)
46 return __builtin_bswap16(val);
48 return etl::detail::byteswap_fallback(val);
52 [[nodiscard]]
constexpr auto operator()(
etl::uint32_t val)
const noexcept ->
etl::uint32_t
54#if __has_builtin(__builtin_bswap32)
55 return __builtin_bswap32(val);
57 return etl::detail::byteswap_fallback(val);
61 [[nodiscard]]
constexpr auto operator()(
etl::uint64_t val)
const noexcept ->
etl::uint64_t
63#if __has_builtin(__builtin_bswap64)
64 return __builtin_bswap64(val);
66 return etl::detail::byteswap_fallback(val);
83template <integral Int>
84[[nodiscard]]
constexpr auto byteswap(Int val)
noexcept -> Int
86 if constexpr (
sizeof(Int) == 1) {
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)));
95 static_assert(always_false<Int>,
"byteswap requires sizeof(Int) <= 8");
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