mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-07 15:31:26 +12:00
Implement constexpr byteswapping
This commit is contained in:
parent
d00f882c23
commit
07a44d0ff9
1 changed files with 29 additions and 0 deletions
|
@ -13,6 +13,20 @@ namespace stx
|
||||||
{
|
{
|
||||||
static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big);
|
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)>
|
template <typename T, std::size_t Align = alignof(T), std::size_t Size = sizeof(T)>
|
||||||
struct se_storage
|
struct se_storage
|
||||||
{
|
{
|
||||||
|
@ -39,6 +53,11 @@ namespace stx
|
||||||
|
|
||||||
static constexpr std::uint16_t swap(std::uint16_t src) noexcept
|
static constexpr std::uint16_t swap(std::uint16_t src) noexcept
|
||||||
{
|
{
|
||||||
|
if (std::is_constant_evaluated())
|
||||||
|
{
|
||||||
|
return stx::bswap(src);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(__GNUG__)
|
#if defined(__GNUG__)
|
||||||
return __builtin_bswap16(src);
|
return __builtin_bswap16(src);
|
||||||
#else
|
#else
|
||||||
|
@ -54,6 +73,11 @@ namespace stx
|
||||||
|
|
||||||
static constexpr std::uint32_t swap(std::uint32_t src) noexcept
|
static constexpr std::uint32_t swap(std::uint32_t src) noexcept
|
||||||
{
|
{
|
||||||
|
if (std::is_constant_evaluated())
|
||||||
|
{
|
||||||
|
return stx::bswap(src);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(__GNUG__)
|
#if defined(__GNUG__)
|
||||||
return __builtin_bswap32(src);
|
return __builtin_bswap32(src);
|
||||||
#else
|
#else
|
||||||
|
@ -69,6 +93,11 @@ namespace stx
|
||||||
|
|
||||||
static constexpr std::uint64_t swap(std::uint64_t src) noexcept
|
static constexpr std::uint64_t swap(std::uint64_t src) noexcept
|
||||||
{
|
{
|
||||||
|
if (std::is_constant_evaluated())
|
||||||
|
{
|
||||||
|
return stx::bswap(src);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(__GNUG__)
|
#if defined(__GNUG__)
|
||||||
return __builtin_bswap64(src);
|
return __builtin_bswap64(src);
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue