// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later // SPDX-FileCopyrightText: 2012 PPSSPP Project // SPDX-FileCopyrightText: 2012 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #pragma once #if defined(_MSC_VER) #include #endif #include #include #include #include "common/common_types.h" namespace Common { #if defined(__Bitrig__) || defined(__OpenBSD__) // We'll redefine swap16, swap32, swap64 as inline functions // but OpenBSD is like "wow I bring my own stuff" // It would be nice if we could use them without C++ namespace shenanigans // But alas :) #undef swap16 #undef swap32 #undef swap64 #endif #if defined(_MSC_VER) [[nodiscard]] inline u16 swap16(u16 data) noexcept { return _byteswap_ushort(data); } [[nodiscard]] inline u32 swap32(u32 data) noexcept { return _byteswap_ulong(data); } [[nodiscard]] inline u64 swap64(u64 data) noexcept { return _byteswap_uint64(data); } #elif defined(__clang__) || defined(__GNUC__) [[nodiscard]] inline u16 swap16(u16 data) noexcept { return __builtin_bswap16(data); } [[nodiscard]] inline u32 swap32(u32 data) noexcept { return __builtin_bswap32(data); } [[nodiscard]] inline u64 swap64(u64 data) noexcept { return __builtin_bswap64(data); } #else // Generic implementation - compiler will optimise these into their respective // __builtin_byteswapXX() and such; if not, the compiler is stupid and we probably // have bigger problems to worry about :) [[nodiscard]] inline u16 swap16(u16 data) noexcept { return (data >> 8) | (data << 8); } [[nodiscard]] inline u32 swap32(u32 data) noexcept { return ((data & 0xFF000000U) >> 24) | ((data & 0x00FF0000U) >> 8) | ((data & 0x0000FF00U) << 8) | ((data & 0x000000FFU) << 24); } [[nodiscard]] inline u64 swap64(u64 data) noexcept { return ((data & 0xFF00000000000000ULL) >> 56) | ((data & 0x00FF000000000000ULL) >> 40) | ((data & 0x0000FF0000000000ULL) >> 24) | ((data & 0x000000FF00000000ULL) >> 8) | ((data & 0x00000000FF000000ULL) << 8) | ((data & 0x0000000000FF0000ULL) << 24) | ((data & 0x000000000000FF00ULL) << 40) | ((data & 0x00000000000000FFULL) << 56); } #endif [[nodiscard]] inline float swapf(float f) noexcept { static_assert(sizeof(u32) == sizeof(float), "float must be the same size as uint32_t."); u32 value; std::memcpy(&value, &f, sizeof(u32)); value = swap32(value); std::memcpy(&f, &value, sizeof(u32)); return f; } [[nodiscard]] inline double swapd(double f) noexcept { static_assert(sizeof(u64) == sizeof(double), "double must be the same size as uint64_t."); u64 value; std::memcpy(&value, &f, sizeof(u64)); value = swap64(value); std::memcpy(&f, &value, sizeof(u64)); return f; } } // Namespace Common template struct SwapStructT { using SwappedT = SwapStructT; protected: T value; static T swap(T v) { return F::swap(v); } public: T swap() const { return swap(value); } SwapStructT() = default; SwapStructT(const T& v) : value(swap(v)) {} template SwappedT& operator=(const S& source) { value = swap(T(source)); return *this; } operator s8() const { return s8(swap()); } operator u8() const { return u8(swap()); } operator s16() const { return s16(swap()); } operator u16() const { return u16(swap()); } operator s32() const { return s32(swap()); } operator u32() const { return u32(swap()); } operator s64() const { return s64(swap()); } operator u64() const { return u64(swap()); } operator float() const { return float(swap()); } operator double() const { return double(swap()); } // +v SwappedT operator+() const { return +swap(); } // -v SwappedT operator-() const { return -swap(); } // v / 5 SwappedT operator/(const SwappedT& i) const { return swap() / i.swap(); } template SwappedT operator/(const S& i) const { return swap() / i; } // v * 5 SwappedT operator*(const SwappedT& i) const { return swap() * i.swap(); } template SwappedT operator*(const S& i) const { return swap() * i; } // v + 5 SwappedT operator+(const SwappedT& i) const { return swap() + i.swap(); } template SwappedT operator+(const S& i) const { return swap() + T(i); } // v - 5 SwappedT operator-(const SwappedT& i) const { return swap() - i.swap(); } template SwappedT operator-(const S& i) const { return swap() - T(i); } // v += 5 SwappedT& operator+=(const SwappedT& i) { value = swap(swap() + i.swap()); return *this; } template SwappedT& operator+=(const S& i) { value = swap(swap() + T(i)); return *this; } // v -= 5 SwappedT& operator-=(const SwappedT& i) { value = swap(swap() - i.swap()); return *this; } template SwappedT& operator-=(const S& i) { value = swap(swap() - T(i)); return *this; } // ++v SwappedT& operator++() { value = swap(swap() + 1); return *this; } // --v SwappedT& operator--() { value = swap(swap() - 1); return *this; } // v++ SwappedT operator++(int) { SwappedT old = *this; value = swap(swap() + 1); return old; } // v-- SwappedT operator--(int) { SwappedT old = *this; value = swap(swap() - 1); return old; } // Comparison // v == i bool operator==(const SwappedT& i) const { return swap() == i.swap(); } template bool operator==(const S& i) const { return swap() == i; } // v != i bool operator!=(const SwappedT& i) const { return swap() != i.swap(); } template bool operator!=(const S& i) const { return swap() != i; } // v > i bool operator>(const SwappedT& i) const { return swap() > i.swap(); } template bool operator>(const S& i) const { return swap() > i; } // v < i bool operator<(const SwappedT& i) const { return swap() < i.swap(); } template bool operator<(const S& i) const { return swap() < i; } // v >= i bool operator>=(const SwappedT& i) const { return swap() >= i.swap(); } template bool operator>=(const S& i) const { return swap() >= i; } // v <= i bool operator<=(const SwappedT& i) const { return swap() <= i.swap(); } template bool operator<=(const S& i) const { return swap() <= i; } // logical SwappedT operator!() const { return !swap(); } // bitmath SwappedT operator~() const { return ~swap(); } SwappedT operator&(const SwappedT& b) const { return swap() & b.swap(); } template SwappedT operator&(const S& b) const { return swap() & b; } SwappedT& operator&=(const SwappedT& b) { value = swap(swap() & b.swap()); return *this; } template SwappedT& operator&=(const S b) { value = swap(swap() & b); return *this; } SwappedT operator|(const SwappedT& b) const { return swap() | b.swap(); } template SwappedT operator|(const S& b) const { return swap() | b; } SwappedT& operator|=(const SwappedT& b) { value = swap(swap() | b.swap()); return *this; } template SwappedT& operator|=(const S& b) { value = swap(swap() | b); return *this; } SwappedT operator^(const SwappedT& b) const { return swap() ^ b.swap(); } template SwappedT operator^(const S& b) const { return swap() ^ b; } SwappedT& operator^=(const SwappedT& b) { value = swap(swap() ^ b.swap()); return *this; } template SwappedT& operator^=(const S& b) { value = swap(swap() ^ b); return *this; } template SwappedT operator<<(const S& b) const { return swap() << b; } template SwappedT& operator<<=(const S& b) const { value = swap(swap() << b); return *this; } template SwappedT operator>>(const S& b) const { return swap() >> b; } template SwappedT& operator>>=(const S& b) const { value = swap(swap() >> b); return *this; } // Member /** todo **/ // Arithmetic template friend S operator+(const S& p, const SwappedT v); template friend S operator-(const S& p, const SwappedT v); template friend S operator/(const S& p, const SwappedT v); template friend S operator*(const S& p, const SwappedT v); template friend S operator%(const S& p, const SwappedT v); // Arithmetic + assignments template friend S operator+=(const S& p, const SwappedT v); template friend S operator-=(const S& p, const SwappedT v); // Bitmath template friend S operator&(const S& p, const SwappedT v); // Comparison template friend bool operator<(const S& p, const SwappedT v); template friend bool operator>(const S& p, const SwappedT v); template friend bool operator<=(const S& p, const SwappedT v); template friend bool operator>=(const S& p, const SwappedT v); template friend bool operator!=(const S& p, const SwappedT v); template friend bool operator==(const S& p, const SwappedT v); }; // Arithmetic template S operator+(const S& i, const SwapStructT v) { return i + v.swap(); } template S operator-(const S& i, const SwapStructT v) { return i - v.swap(); } template S operator/(const S& i, const SwapStructT v) { return i / v.swap(); } template S operator*(const S& i, const SwapStructT v) { return i * v.swap(); } template S operator%(const S& i, const SwapStructT v) { return i % v.swap(); } // Arithmetic + assignments template S& operator+=(S& i, const SwapStructT v) { i += v.swap(); return i; } template S& operator-=(S& i, const SwapStructT v) { i -= v.swap(); return i; } // Logical template S operator&(const S& i, const SwapStructT v) { return i & v.swap(); } // Comparison template bool operator<(const S& p, const SwapStructT v) { return p < v.swap(); } template bool operator>(const S& p, const SwapStructT v) { return p > v.swap(); } template bool operator<=(const S& p, const SwapStructT v) { return p <= v.swap(); } template bool operator>=(const S& p, const SwapStructT v) { return p >= v.swap(); } template bool operator!=(const S& p, const SwapStructT v) { return p != v.swap(); } template bool operator==(const S& p, const SwapStructT v) { return p == v.swap(); } template struct Swap64T { static T swap(T x) { return T(Common::swap64(x)); } }; template struct Swap32T { static T swap(T x) { return T(Common::swap32(x)); } }; template struct Swap16T { static T swap(T x) { return T(Common::swap16(x)); } }; template struct SwapFloatT { static T swap(T x) { return T(Common::swapf(x)); } }; template struct SwapDoubleT { static T swap(T x) { return T(Common::swapd(x)); } }; template struct SwapEnumT { static_assert(std::is_enum_v); using base = std::underlying_type_t; public: SwapEnumT() = default; SwapEnumT(const T& v) : value(swap(v)) {} SwapEnumT& operator=(const T& v) { value = swap(v); return *this; } operator T() const { return swap(value); } explicit operator base() const { return base(swap(value)); } protected: T value{}; using swap_t = std::conditional_t< std::is_same_v, Swap16T, std::conditional_t< std::is_same_v, Swap16T, std::conditional_t< std::is_same_v, Swap32T, std::conditional_t< std::is_same_v, Swap32T, std::conditional_t< std::is_same_v, Swap64T, std::conditional_t< std::is_same_v, Swap64T, void>>>>>>; static T swap(T x) { return T(swap_t::swap(base(x))); } }; struct SwapTag {}; // Use the different endianness from the system struct KeepTag {}; // Use the same endianness as the system template struct AddEndian; // KeepTag specializations template struct AddEndian { using type = T; }; // SwapTag specializations template <> struct AddEndian { using type = u8; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = s8; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template <> struct AddEndian { using type = SwapStructT>; }; template struct AddEndian { static_assert(std::is_enum_v); using type = SwapEnumT; }; // Alias LETag/BETag as KeepTag/SwapTag depending on the system using LETag = std::conditional_t; using BETag = std::conditional_t; // Aliases for LE types using u16_le = AddEndian::type; using u32_le = AddEndian::type; using u64_le = AddEndian::type; using s16_le = AddEndian::type; using s32_le = AddEndian::type; using s64_le = AddEndian::type; template using enum_le = std::enable_if_t, typename AddEndian::type>; using float_le = AddEndian::type; using double_le = AddEndian::type; // Aliases for BE types using u16_be = AddEndian::type; using u32_be = AddEndian::type; using u64_be = AddEndian::type; using s16_be = AddEndian::type; using s32_be = AddEndian::type; using s64_be = AddEndian::type; template using enum_be = std::enable_if_t, typename AddEndian::type>; using float_be = AddEndian::type; using double_be = AddEndian::type;