Implement constexpr byteswapping

This commit is contained in:
Eladash 2020-06-25 20:48:42 +03:00 committed by Ivan
parent d00f882c23
commit 07a44d0ff9

View File

@ -13,6 +13,20 @@ namespace stx
{
static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big);
template<class T, std::size_t... N>
constexpr T bswap_impl(T i, std::index_sequence<N...>)
{
return static_cast<T>(((((i >> (N * 8)) & T{UINT8_MAX}) <<
((sizeof(T) - 1 - N) * 8)) | ...));
};
template<class T, class U = typename std::make_unsigned<T>::type>
constexpr U bswap(T i)
{
return bswap_impl<U>(i, std::make_index_sequence<sizeof(T)>{});
}
template <typename T, std::size_t Align = alignof(T), std::size_t Size = sizeof(T)>
struct se_storage
{
@ -39,6 +53,11 @@ namespace stx
static constexpr std::uint16_t swap(std::uint16_t src) noexcept
{
if (std::is_constant_evaluated())
{
return stx::bswap(src);
}
#if defined(__GNUG__)
return __builtin_bswap16(src);
#else
@ -54,6 +73,11 @@ namespace stx
static constexpr std::uint32_t swap(std::uint32_t src) noexcept
{
if (std::is_constant_evaluated())
{
return stx::bswap(src);
}
#if defined(__GNUG__)
return __builtin_bswap32(src);
#else
@ -69,6 +93,11 @@ namespace stx
static constexpr std::uint64_t swap(std::uint64_t src) noexcept
{
if (std::is_constant_evaluated())
{
return stx::bswap(src);
}
#if defined(__GNUG__)
return __builtin_bswap64(src);
#else