|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/*! |
| 7 | + @file byteswap.hpp |
| 8 | + @brief std::byteswap for less than C++23 |
| 9 | +*/ |
| 10 | +#ifndef M5_UTILITY_STL_BYTESWAP_HPP |
| 11 | +#define M5_UTILITY_STL_BYTESWAP_HPP |
| 12 | + |
| 13 | +#include <stdint.h> |
| 14 | +#include <type_traits> |
| 15 | +#include <cstring> // memcpy |
| 16 | + |
| 17 | +namespace m5 { |
| 18 | +namespace stl { |
| 19 | + |
| 20 | +///@cond |
| 21 | +template <typename U, |
| 22 | + typename std::enable_if<sizeof(U) == 2 && std::is_unsigned<U>::value, std::nullptr_t>::type = nullptr> |
| 23 | +inline constexpr U bswap_fixed(U x) noexcept |
| 24 | +{ |
| 25 | + return static_cast<U>((static_cast<uint16_t>(x >> 8) & 0x00FFu) | (static_cast<uint16_t>(x << 8) & 0xFF00u)); |
| 26 | +} |
| 27 | + |
| 28 | +template <typename U, |
| 29 | + typename std::enable_if<sizeof(U) == 4 && std::is_unsigned<U>::value, std::nullptr_t>::type = nullptr> |
| 30 | +inline constexpr U bswap_fixed(U x) noexcept |
| 31 | +{ |
| 32 | + return static_cast<U>(((static_cast<uint32_t>(x) >> 24)) | ((static_cast<uint32_t>(x) >> 8) & 0x0000FF00u) | |
| 33 | + ((static_cast<uint32_t>(x) << 8) & 0x00FF0000u) | ((static_cast<uint32_t>(x) << 24))); |
| 34 | +} |
| 35 | + |
| 36 | +template <typename U, |
| 37 | + typename std::enable_if<sizeof(U) == 8 && std::is_unsigned<U>::value, std::nullptr_t>::type = nullptr> |
| 38 | +inline constexpr U bswap_fixed(U x) noexcept |
| 39 | +{ |
| 40 | + return static_cast<U>( |
| 41 | + ((static_cast<uint64_t>(x) >> 56)) | ((static_cast<uint64_t>(x) >> 40) & 0x000000000000FF00ull) | |
| 42 | + ((static_cast<uint64_t>(x) >> 24) & 0x0000000000FF0000ull) | |
| 43 | + ((static_cast<uint64_t>(x) >> 8) & 0x00000000FF000000ull) | |
| 44 | + ((static_cast<uint64_t>(x) << 8) & 0x000000FF00000000ull) | |
| 45 | + ((static_cast<uint64_t>(x) << 24) & 0x0000FF0000000000ull) | |
| 46 | + ((static_cast<uint64_t>(x) << 40) & 0x00FF000000000000ull) | ((static_cast<uint64_t>(x) << 56))); |
| 47 | +} |
| 48 | + |
| 49 | +#if defined(__SIZEOF_INT128__) |
| 50 | +template <typename U, |
| 51 | + typename std::enable_if<sizeof(U) == 16 && std::is_unsigned<U>::value, std::nullptr_t>::type = nullptr> |
| 52 | +inline constexpr U bswap_fixed(U x) noexcept |
| 53 | +{ |
| 54 | + return ((static_cast<U>(bswap_fixed<uint64_t>(static_cast<uint64_t>(x))) << 64) | |
| 55 | + static_cast<U>(bswap_fixed<uint64_t>(static_cast<uint64_t>(x >> 64)))); |
| 56 | +} |
| 57 | +#endif |
| 58 | + |
| 59 | +template <typename U, typename std::enable_if<sizeof(U) == 1, std::nullptr_t>::type = nullptr> |
| 60 | +inline constexpr U bswap_fixed(U x) noexcept |
| 61 | +{ |
| 62 | + return x; |
| 63 | +} |
| 64 | + |
| 65 | +template <typename T, typename std::enable_if<(std::is_integral<T>::value || std::is_enum<T>::value), |
| 66 | + std::nullptr_t>::type = nullptr> |
| 67 | +inline constexpr T byteswap_constexpr(T v) noexcept |
| 68 | +{ |
| 69 | + using U = typename std::make_unsigned<T>::type; |
| 70 | + return static_cast<T>(bswap_fixed<U>(static_cast<U>(v))); |
| 71 | +} |
| 72 | + |
| 73 | +template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr> |
| 74 | +T byteswap_any(T v) |
| 75 | +{ |
| 76 | + if (sizeof(T) == 1) { |
| 77 | + return v; |
| 78 | + } |
| 79 | + if (sizeof(T) == 2) { |
| 80 | + uint16_t u; |
| 81 | + ::memcpy(&u, &v, 2); |
| 82 | + u = byteswap_constexpr(u); |
| 83 | + ::memcpy(&v, &u, 2); |
| 84 | + return v; |
| 85 | + } |
| 86 | + if (sizeof(T) == 4) { |
| 87 | + uint32_t u; |
| 88 | + ::memcpy(&u, &v, 4); |
| 89 | + u = byteswap_constexpr(u); |
| 90 | + ::memcpy(&v, &u, 4); |
| 91 | + return v; |
| 92 | + } |
| 93 | + if (sizeof(T) == 8) { |
| 94 | + uint64_t u; |
| 95 | + ::memcpy(&u, &v, 8); |
| 96 | + u = byteswap_constexpr(u); |
| 97 | + ::memcpy(&v, &u, 8); |
| 98 | + return v; |
| 99 | + } |
| 100 | +#if defined(__SIZEOF_INT128__) |
| 101 | + if (sizeof(T) == 16) { |
| 102 | + unsigned __int128 u = 0; |
| 103 | + ::memcpy(&u, &v, 16); |
| 104 | + u = bswap_fixed(u); |
| 105 | + ::memcpy(&v, &u, 16); |
| 106 | + return v; |
| 107 | + } |
| 108 | +#endif |
| 109 | + return v; |
| 110 | +} |
| 111 | +///@endcond |
| 112 | + |
| 113 | +//! @brief byteswap for integral type |
| 114 | +template <typename T, typename std::enable_if<(std::is_integral<T>::value || std::is_enum<T>::value), |
| 115 | + std::nullptr_t>::type = nullptr> |
| 116 | +inline constexpr T byteswap(T v) noexcept |
| 117 | +{ |
| 118 | + return byteswap_constexpr(v); |
| 119 | +} |
| 120 | + |
| 121 | +//! @brief byteswap for floating point type |
| 122 | +template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr> |
| 123 | +inline T byteswap(T v) |
| 124 | +{ |
| 125 | + return byteswap_any(v); |
| 126 | +} |
| 127 | + |
| 128 | +// If the type is not a number, it is prohibited |
| 129 | +template <typename T, typename std::enable_if<!(std::is_floating_point<T>::value) && |
| 130 | + !(std::is_integral<T>::value || std::is_enum<T>::value), |
| 131 | + std::nullptr_t>::type = nullptr> |
| 132 | +T byteswap(T) = delete; |
| 133 | + |
| 134 | +} // namespace stl |
| 135 | +} // namespace m5 |
| 136 | + |
| 137 | +#endif |
0 commit comments