1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-29 21:56:04 +00:00
Atmosphere/libraries/libvapours/include/vapours/util/util_endian.hpp

156 lines
5.3 KiB
C++
Raw Normal View History

2020-02-24 01:34:30 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-02-24 01:34:30 +00:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {
consteval bool IsLittleEndian() {
return std::endian::native == std::endian::little;
2020-02-24 01:34:30 +00:00
}
consteval bool IsBigEndian() {
return std::endian::native == std::endian::big;
2020-02-24 01:34:30 +00:00
}
static_assert(IsLittleEndian() ^ IsBigEndian());
2022-04-03 18:51:46 +01:00
template<std::unsigned_integral U>
constexpr ALWAYS_INLINE U SwapEndian(const U u) {
2020-02-24 01:34:30 +00:00
static_assert(BITSIZEOF(u8) == 8);
2022-04-03 18:51:46 +01:00
if constexpr (sizeof(U) * BITSIZEOF(u8) == 64) {
static_assert(__builtin_bswap64(UINT64_C(0x0123456789ABCDEF)) == UINT64_C(0xEFCDAB8967452301));
return __builtin_bswap64(u);
} else if constexpr (sizeof(U) * BITSIZEOF(u8) == 32) {
static_assert(__builtin_bswap32(0x01234567u) == 0x67452301u);
return __builtin_bswap32(u);
} else if constexpr (sizeof(U) * BITSIZEOF(u8) == 16) {
static_assert(__builtin_bswap16(0x0123u) == 0x2301u);
return __builtin_bswap16(u);
} else if constexpr (sizeof(U) * BITSIZEOF(u8) == 8) {
2020-02-24 01:34:30 +00:00
return u;
} else {
static_assert(!std::is_same<U, U>::value);
}
}
2022-04-03 18:51:46 +01:00
constexpr ALWAYS_INLINE u64 SwapEndian48(const u64 u) {
2020-02-25 03:29:00 +00:00
using U = u64;
static_assert(BITSIZEOF(u8) == 8);
constexpr U ByteMask = 0xFFu;
AMS_ASSERT((u & UINT64_C(0xFFFF000000000000)) == 0);
return ((u & (ByteMask << 40)) >> 40) |
((u & (ByteMask << 32)) >> 24) |
((u & (ByteMask << 24)) >> 8) |
((u & (ByteMask << 16)) << 8) |
((u & (ByteMask << 8)) << 24) |
((u & (ByteMask << 0)) << 40);
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
constexpr ALWAYS_INLINE void SwapEndian(T *ptr) {
2020-02-24 01:34:30 +00:00
using U = typename std::make_unsigned<T>::type;
2022-04-03 18:51:46 +01:00
*ptr = static_cast<T>(SwapEndian<U>(static_cast<U>(*ptr)));
2020-02-24 01:34:30 +00:00
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
2020-02-24 01:34:30 +00:00
constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) {
2020-02-25 03:29:00 +00:00
using U = typename std::make_unsigned<T>::type;
2020-02-24 01:34:30 +00:00
if constexpr (IsBigEndian()) {
2020-02-25 03:29:00 +00:00
return static_cast<T>(static_cast<U>(val));
2020-02-24 01:34:30 +00:00
} else {
static_assert(IsLittleEndian());
2022-04-03 18:51:46 +01:00
return static_cast<T>(SwapEndian<U>(static_cast<U>(val)));
2020-02-24 01:34:30 +00:00
}
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
2020-02-24 01:34:30 +00:00
constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) {
2020-02-25 03:29:00 +00:00
using U = typename std::make_unsigned<T>::type;
if constexpr (IsBigEndian()) {
2022-04-03 18:51:46 +01:00
return static_cast<T>(SwapEndian<U>(static_cast<U>(val)));
2020-02-25 03:29:00 +00:00
} else {
static_assert(IsLittleEndian());
return static_cast<T>(static_cast<U>(val));
}
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
constexpr ALWAYS_INLINE T ConvertFromBigEndian(const T val) {
return ConvertToBigEndian(val);
}
template<std::integral T>
constexpr ALWAYS_INLINE T ConvertFromLittleEndian(const T val) {
return ConvertToLittleEndian(val);
}
template<std::integral T>
2020-02-25 03:29:00 +00:00
constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) {
using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64));
if constexpr (IsBigEndian()) {
AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0);
return static_cast<T>(static_cast<U>(val));
} else {
static_assert(IsLittleEndian());
2022-04-03 18:51:46 +01:00
return static_cast<T>(SwapEndian48(static_cast<U>(val)));
2020-02-25 03:29:00 +00:00
}
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
2020-02-25 03:29:00 +00:00
constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) {
using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64));
2020-02-24 01:34:30 +00:00
if constexpr (IsBigEndian()) {
2022-04-03 18:51:46 +01:00
return static_cast<T>(SwapEndian48(static_cast<U>(val)));
2020-02-24 01:34:30 +00:00
} else {
static_assert(IsLittleEndian());
2020-02-25 03:29:00 +00:00
AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0);
return static_cast<T>(static_cast<U>(val));
2020-02-24 01:34:30 +00:00
}
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
constexpr ALWAYS_INLINE T LoadBigEndian(const T *ptr) {
return ConvertToBigEndian<T>(*ptr);
2020-02-24 01:34:30 +00:00
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
constexpr ALWAYS_INLINE T LoadLittleEndian(const T *ptr) {
return ConvertToLittleEndian<T>(*ptr);
2020-02-24 01:34:30 +00:00
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
2020-02-24 01:34:30 +00:00
constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) {
*static_cast<volatile T *>(ptr) = ConvertToBigEndian<T>(val);
2020-02-24 01:34:30 +00:00
}
2022-04-03 18:51:46 +01:00
template<std::integral T>
2020-02-24 01:34:30 +00:00
constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) {
*static_cast<volatile T *>(ptr) = ConvertToLittleEndian<T>(val);
2020-02-24 01:34:30 +00:00
}
}