10#ifndef M5_UTILITY_STL_BYTESWAP_HPP
11#define M5_UTILITY_STL_BYTESWAP_HPP
25#if defined(__SIZEOF_INT128__)
27struct is_int128<__int128> : std::true_type {};
30struct is_int128<unsigned __int128> : std::true_type {};
34struct is_integer_or_enum : std::integral_constant<bool, std::is_integral<typename std::remove_cv<T>::type>::value ||
35 std::is_enum<typename std::remove_cv<T>::type>::value ||
36 is_int128<typename std::remove_cv<T>::type>::value> {};
39struct is_unsigned_ex : std::is_unsigned<typename std::remove_cv<T>::type> {};
41#if defined(__SIZEOF_INT128__)
48 using type =
typename std::make_unsigned<T>::type;
56#if defined(__SIZEOF_INT128__)
59 using type =
unsigned __int128;
63struct make_unsigned_impl<unsigned __int128> {
64 using type =
unsigned __int128;
72 typename std::enable_if<
sizeof(U) == 2 && detail::is_unsigned_ex<U>::value, std::nullptr_t>::type =
nullptr>
73inline constexpr U bswap_fixed(U x)
noexcept
75 return static_cast<U
>((
static_cast<uint16_t
>(x >> 8) & 0x00FFu) | (
static_cast<uint16_t
>(x << 8) & 0xFF00u));
79 typename std::enable_if<
sizeof(U) == 4 && detail::is_unsigned_ex<U>::value, std::nullptr_t>::type =
nullptr>
80inline constexpr U bswap_fixed(U x)
noexcept
82 return static_cast<U
>(((
static_cast<uint32_t
>(x) >> 24)) | ((
static_cast<uint32_t
>(x) >> 8) & 0x0000FF00u) |
83 ((
static_cast<uint32_t
>(x) << 8) & 0x00FF0000u) | ((
static_cast<uint32_t
>(x) << 24)));
87 typename std::enable_if<
sizeof(U) == 8 && detail::is_unsigned_ex<U>::value, std::nullptr_t>::type =
nullptr>
88inline constexpr U bswap_fixed(U x)
noexcept
90 return static_cast<U
>(
91 ((
static_cast<uint64_t
>(x) >> 56)) | ((
static_cast<uint64_t
>(x) >> 40) & 0x000000000000FF00ull) |
92 ((
static_cast<uint64_t
>(x) >> 24) & 0x0000000000FF0000ull) |
93 ((
static_cast<uint64_t
>(x) >> 8) & 0x00000000FF000000ull) |
94 ((
static_cast<uint64_t
>(x) << 8) & 0x000000FF00000000ull) |
95 ((
static_cast<uint64_t
>(x) << 24) & 0x0000FF0000000000ull) |
96 ((
static_cast<uint64_t
>(x) << 40) & 0x00FF000000000000ull) | ((
static_cast<uint64_t
>(x) << 56)));
99#if defined(__SIZEOF_INT128__)
101 typename std::enable_if<
sizeof(U) == 16 && detail::is_unsigned_ex<U>::value, std::nullptr_t>::type =
nullptr>
102inline constexpr U bswap_fixed(U x)
noexcept
104 return ((
static_cast<U
>(bswap_fixed<uint64_t>(
static_cast<uint64_t
>(x))) << 64) |
105 static_cast<U
>(bswap_fixed<uint64_t>(
static_cast<uint64_t
>(x >> 64))));
110 typename std::enable_if<
sizeof(U) == 1 && detail::is_unsigned_ex<U>::value, std::nullptr_t>::type =
nullptr>
111inline constexpr U bswap_fixed(U x)
noexcept
116template <typename T, typename std::enable_if<detail::is_integer_or_enum<T>::value, std::nullptr_t>::type =
nullptr>
117inline constexpr T byteswap_constexpr(T v)
noexcept
119 using U =
typename detail::make_unsigned_ex<T>::type;
120 return static_cast<T
>(bswap_fixed<U>(
static_cast<U
>(v)));
125template <typename T, typename std::enable_if<detail::is_integer_or_enum<T>::value, std::nullptr_t>::type =
nullptr>
128 return byteswap_constexpr(v);
132template <typename T, typename std::enable_if<!detail::is_integer_or_enum<T>::value, std::nullptr_t>::type =
nullptr>
133T byteswap(T) =
delete;
constexpr T byteswap(T v) noexcept
byteswap for integral type
Definition byteswap.hpp:126
Top level namespace of M5.
Definition base64.cpp:39
STL compatibility functions and classes.
Definition byteswap.hpp:23
Definition byteswap.hpp:36
Definition byteswap.hpp:39
Definition byteswap.hpp:52
Definition byteswap.hpp:47