mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
120
gpu/third_party/suyu/common/alignment.h
vendored
Normal file
120
gpu/third_party/suyu/common/alignment.h
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: 2014 Jannik Vogel <email@jannikvogel.de>
|
||||
// SPDX-License-Identifier: CC0-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <new>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Common {
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T AlignUp(T value_, size_t size) {
|
||||
using U = typename std::make_unsigned_t<T>;
|
||||
auto value{static_cast<U>(value_)};
|
||||
auto mod{static_cast<T>(value % size)};
|
||||
value -= mod;
|
||||
return static_cast<T>(mod == T{0} ? value : value + size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_unsigned_v<T>
|
||||
[[nodiscard]] constexpr T AlignUpLog2(T value, size_t align_log2) {
|
||||
return static_cast<T>((value + ((1ULL << align_log2) - 1)) >> align_log2 << align_log2);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T AlignDown(T value_, size_t size) {
|
||||
using U = typename std::make_unsigned_t<T>;
|
||||
const auto value{static_cast<U>(value_)};
|
||||
return static_cast<T>(value - value % size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_unsigned_v<T>
|
||||
[[nodiscard]] constexpr bool IsWordAligned(T value) {
|
||||
return (value & 0b11) == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr bool IsAligned(T value, size_t alignment) noexcept {
|
||||
using U = typename std::make_unsigned_t<T>;
|
||||
const U mask = U(alignment - 1);
|
||||
return (value & mask) == 0;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T DivideUp(T x, U y) {
|
||||
return (x + (y - 1)) / y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T LeastSignificantOneBit(T x) {
|
||||
return x & ~(x - 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T ResetLeastSignificantOneBit(T x) {
|
||||
return x & (x - 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr bool IsPowerOfTwo(T x) {
|
||||
return x > 0 && ResetLeastSignificantOneBit(x) == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T FloorPowerOfTwo(T x) {
|
||||
return T{1} << (sizeof(T) * 8 - std::countl_zero(x) - 1);
|
||||
}
|
||||
|
||||
template <typename T, size_t Align = 16>
|
||||
class AlignmentAllocator {
|
||||
public:
|
||||
using value_type = T;
|
||||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
|
||||
using propagate_on_container_copy_assignment = std::true_type;
|
||||
using propagate_on_container_move_assignment = std::true_type;
|
||||
using propagate_on_container_swap = std::true_type;
|
||||
using is_always_equal = std::false_type;
|
||||
|
||||
constexpr AlignmentAllocator() noexcept = default;
|
||||
|
||||
template <typename T2>
|
||||
constexpr AlignmentAllocator(const AlignmentAllocator<T2, Align>&) noexcept {}
|
||||
|
||||
[[nodiscard]] T* allocate(size_type n) {
|
||||
return static_cast<T*>(::operator new(n * sizeof(T), std::align_val_t{Align}));
|
||||
}
|
||||
|
||||
void deallocate(T* p, size_type n) {
|
||||
::operator delete(p, n * sizeof(T), std::align_val_t{Align});
|
||||
}
|
||||
|
||||
template <typename T2>
|
||||
struct rebind {
|
||||
using other = AlignmentAllocator<T2, Align>;
|
||||
};
|
||||
|
||||
template <typename T2, size_t Align2>
|
||||
constexpr bool operator==(const AlignmentAllocator<T2, Align2>&) const noexcept {
|
||||
return std::is_same_v<T, T2> && Align == Align2;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Common
|
||||
40
gpu/third_party/suyu/common/assert.h
vendored
Normal file
40
gpu/third_party/suyu/common/assert.h
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// Rexcomp replacement for suyu's common/assert.h: same macros; a failed assertion prints and carries on, as suyu's
|
||||
// soft asserts do in release builds.
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
|
||||
namespace Common {
|
||||
inline void AssertFail(const char* file, int line, const char* what) {
|
||||
std::fprintf(stderr, "[shader] assertion failed at %s:%d: %s\n", file, line, what);
|
||||
}
|
||||
} // namespace Common
|
||||
|
||||
// each assertion reports its first 3 failures
|
||||
#define ASSERT_MSG(_a_, ...) \
|
||||
do { \
|
||||
if (!(_a_)) [[unlikely]] { \
|
||||
static int assert_reports_; \
|
||||
if (assert_reports_++ < 3) ::Common::AssertFail(__FILE__, __LINE__, #_a_); \
|
||||
} \
|
||||
} while (0)
|
||||
#define ASSERT(_a_) ASSERT_MSG(_a_, "")
|
||||
#define UNREACHABLE_MSG(...) \
|
||||
do { \
|
||||
::Common::AssertFail(__FILE__, __LINE__, "unreachable"); \
|
||||
__builtin_unreachable(); \
|
||||
} while (0)
|
||||
#define UNREACHABLE() UNREACHABLE_MSG("")
|
||||
#define DEBUG_ASSERT(_a_) ((void)0)
|
||||
#define DEBUG_ASSERT_MSG(_a_, ...) ((void)0)
|
||||
#define UNIMPLEMENTED() ASSERT(false && "Unimplemented!")
|
||||
#define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__)
|
||||
#define UNIMPLEMENTED_IF(cond) ASSERT((!(cond)) && "Unimplemented!")
|
||||
#define UNIMPLEMENTED_IF_MSG(cond, ...) ASSERT_MSG(!(cond), __VA_ARGS__)
|
||||
#define ASSERT_OR_EXECUTE(_a_, _b_) \
|
||||
do { \
|
||||
ASSERT(_a_); \
|
||||
if (!(_a_)) [[unlikely]] { \
|
||||
_b_ \
|
||||
} \
|
||||
} while (0)
|
||||
#define ASSERT_OR_EXECUTE_MSG(_a_, _b_, ...) ASSERT_OR_EXECUTE(_a_, _b_)
|
||||
175
gpu/third_party/suyu/common/bit_field.h
vendored
Normal file
175
gpu/third_party/suyu/common/bit_field.h
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: 2014 Tony Wasserka
|
||||
// SPDX-FileCopyrightText: 2014 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <climits>
|
||||
#include "common/swap.h"
|
||||
|
||||
/*
|
||||
* Abstract bitfield class
|
||||
*
|
||||
* Allows endianness-independent access to individual bitfields within some raw
|
||||
* integer value. The assembly generated by this class is identical to the
|
||||
* usage of raw bitfields, so it's a perfectly fine replacement.
|
||||
*
|
||||
* For BitField<X,Y,Z>, X is the distance of the bitfield to the LSB of the
|
||||
* raw value, Y is the length in bits of the bitfield. Z is an integer type
|
||||
* which determines the sign of the bitfield. Z must have the same size as the
|
||||
* raw integer.
|
||||
*
|
||||
*
|
||||
* General usage:
|
||||
*
|
||||
* Create a new union with the raw integer value as a member.
|
||||
* Then for each bitfield you want to expose, add a BitField member
|
||||
* in the union. The template parameters are the bit offset and the number
|
||||
* of desired bits.
|
||||
*
|
||||
* Changes in the bitfield members will then get reflected in the raw integer
|
||||
* value and vice-versa.
|
||||
*
|
||||
*
|
||||
* Sample usage:
|
||||
*
|
||||
* union SomeRegister
|
||||
* {
|
||||
* u32 hex;
|
||||
*
|
||||
* BitField<0,7,u32> first_seven_bits; // unsigned
|
||||
* BitField<7,8,u32> next_eight_bits; // unsigned
|
||||
* BitField<3,15,s32> some_signed_fields; // signed
|
||||
* };
|
||||
*
|
||||
* This is equivalent to the little-endian specific code:
|
||||
*
|
||||
* union SomeRegister
|
||||
* {
|
||||
* u32 hex;
|
||||
*
|
||||
* struct
|
||||
* {
|
||||
* u32 first_seven_bits : 7;
|
||||
* u32 next_eight_bits : 8;
|
||||
* };
|
||||
* struct
|
||||
* {
|
||||
* u32 : 3; // padding
|
||||
* s32 some_signed_fields : 15;
|
||||
* };
|
||||
* };
|
||||
*
|
||||
*
|
||||
* Caveats:
|
||||
*
|
||||
* 1)
|
||||
* BitField provides automatic casting from and to the storage type where
|
||||
* appropriate. However, when using non-typesafe functions like printf, an
|
||||
* explicit cast must be performed on the BitField object to make sure it gets
|
||||
* passed correctly, e.g.:
|
||||
* printf("Value: %d", (s32)some_register.some_signed_fields);
|
||||
*
|
||||
* 2)
|
||||
* Not really a caveat, but potentially irritating: This class is used in some
|
||||
* packed structures that do not guarantee proper alignment. Therefore we have
|
||||
* to use #pragma pack here not to pack the members of the class, but instead
|
||||
* to break GCC's assumption that the members of the class are aligned on
|
||||
* sizeof(StorageType).
|
||||
* TODO(neobrain): Confirm that this is a proper fix and not just masking
|
||||
* symptoms.
|
||||
*/
|
||||
#pragma pack(1)
|
||||
template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag>
|
||||
requires std::is_trivially_copyable_v<T>
|
||||
struct BitField {
|
||||
private:
|
||||
// UnderlyingType is T for non-enum types and the underlying type of T if
|
||||
// T is an enumeration. Note that T is wrapped within an enable_if in the
|
||||
// former case to workaround compile errors which arise when using
|
||||
// std::underlying_type<T>::type directly.
|
||||
using UnderlyingType = typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>, std::enable_if<true, T>>::type;
|
||||
// We store the value as the unsigned type to avoid undefined behaviour on value shifting
|
||||
using StorageType = std::make_unsigned_t<UnderlyingType>;
|
||||
using StorageTypeWithEndian = typename AddEndian<StorageType, EndianTag>::type;
|
||||
public:
|
||||
/// Constants to allow limited introspection of fields if needed
|
||||
static constexpr std::size_t position = Position;
|
||||
static constexpr std::size_t bits = Bits;
|
||||
static constexpr StorageType mask = (StorageType(~0) >> (CHAR_BIT * sizeof(T) - bits)) << position;
|
||||
|
||||
/**
|
||||
* Formats a value by masking and shifting it according to the field parameters. A value
|
||||
* containing several bitfields can be assembled by formatting each of their values and ORing
|
||||
* the results together.
|
||||
*/
|
||||
[[nodiscard]] static constexpr StorageType FormatValue(const T& value) {
|
||||
return (StorageType(value) << position) & mask;
|
||||
}
|
||||
|
||||
/// @brief Extracts a value from the passed storage. In most situations prefer use the member functions
|
||||
/// (such as Value() or operator T), but this can be used to extract a value from a bitfield
|
||||
/// union in a constexpr context.
|
||||
[[nodiscard]] static constexpr T ExtractValue(const StorageType& storage) {
|
||||
if constexpr (std::numeric_limits<UnderlyingType>::is_signed) {
|
||||
std::size_t shift = CHAR_BIT * sizeof(T) - bits;
|
||||
return T(UnderlyingType(storage << (shift - position)) >> shift);
|
||||
} else {
|
||||
return T((storage & mask) >> position);
|
||||
}
|
||||
}
|
||||
|
||||
// This constructor and assignment operator might be considered ambiguous:
|
||||
// Would they initialize the storage or just the bitfield?
|
||||
// Hence, delete them. Use the Assign method to set bitfield values!
|
||||
BitField(T val) = delete;
|
||||
BitField& operator=(T val) = delete;
|
||||
|
||||
constexpr BitField() noexcept = default;
|
||||
|
||||
constexpr BitField(const BitField&) noexcept = default;
|
||||
constexpr BitField& operator=(const BitField&) noexcept = default;
|
||||
|
||||
constexpr BitField(BitField&&) noexcept = default;
|
||||
constexpr BitField& operator=(BitField&&) noexcept = default;
|
||||
|
||||
constexpr void Assign(const T value) {
|
||||
storage = StorageType((storage & ~mask) | FormatValue(value));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T Value() const {
|
||||
return ExtractValue(storage);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr operator T() const {
|
||||
return Value();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr explicit operator bool() const {
|
||||
return Value() != 0;
|
||||
}
|
||||
|
||||
private:
|
||||
StorageTypeWithEndian storage;
|
||||
|
||||
static_assert(bits + position <= CHAR_BIT * sizeof(T), "Bitfield out of range");
|
||||
// And, you know, just in case people specify something stupid like bits=position=0x80000000
|
||||
static_assert(position < CHAR_BIT * sizeof(T), "Invalid position");
|
||||
static_assert(bits <= CHAR_BIT * sizeof(T), "Invalid number of bits");
|
||||
static_assert(bits > 0, "Invalid number of bits");
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
template <std::size_t Position, std::size_t Bits, typename T>
|
||||
using BitFieldBE = BitField<Position, Bits, T, BETag>;
|
||||
|
||||
template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag>
|
||||
inline auto format_as(BitField<Position, Bits, T, EndianTag> bitfield) {
|
||||
return bitfield.Value();
|
||||
}
|
||||
57
gpu/third_party/suyu/common/bit_util.h
vendored
Normal file
57
gpu/third_party/suyu/common/bit_util.h
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <bit>
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
/// Gets the size of a specified type T in bits.
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr std::size_t BitSize() {
|
||||
return std::size_t(sizeof(T) * CHAR_BIT);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr u32 MostSignificantBit(const T value) {
|
||||
return u32(sizeof(T) * CHAR_BIT - 1 - std::countl_zero(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T Log2Floor(const T value) {
|
||||
return T(MostSignificantBit<T>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr T Log2Ceil(const T value) {
|
||||
const T log2_f = Log2Floor<T>(value);
|
||||
return T(log2_f + T((value ^ (T(1ULL) << log2_f)) != T(0ULL)));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] T NextPow2(T value) {
|
||||
return T(1ULL << (sizeof(T) * CHAR_BIT - std::countl_zero(value - 1U)));
|
||||
}
|
||||
|
||||
template <size_t bit_index, typename T>
|
||||
requires std::is_integral_v<T>
|
||||
[[nodiscard]] constexpr bool Bit(const T value) {
|
||||
static_assert(bit_index < BitSize<T>(), "bit_index must be smaller than size of T");
|
||||
return ((value >> bit_index) & T(1)) == T(1);
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
111
gpu/third_party/suyu/common/common_funcs.h
vendored
Normal file
111
gpu/third_party/suyu/common/common_funcs.h
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
#include <cstdlib> // for exit
|
||||
#include "common/common_types.h"
|
||||
|
||||
#ifndef __cpp_lib_to_underlying
|
||||
namespace std {
|
||||
// https://en.cppreference.com/cpp/utility/to_underlying
|
||||
template<class T>
|
||||
requires std::is_enum_v<T>
|
||||
constexpr std::underlying_type_t<T> to_underlying(T e) noexcept {
|
||||
return std::underlying_type_t<T>(e);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
|
||||
#define CONCAT2(x, y) DO_CONCAT2(x, y)
|
||||
#define DO_CONCAT2(x, y) x##y
|
||||
|
||||
/// Helper macros to insert unused bytes or words to properly align structs. These values will be
|
||||
/// zero-initialized.
|
||||
#define INSERT_PADDING_BYTES(num_bytes) \
|
||||
[[maybe_unused]] std::array<u8, num_bytes> CONCAT2(pad, __LINE__) {}
|
||||
#define INSERT_PADDING_WORDS(num_words) \
|
||||
[[maybe_unused]] std::array<u32, num_words> CONCAT2(pad, __LINE__) {}
|
||||
|
||||
/// These are similar to the INSERT_PADDING_* macros but do not zero-initialize the contents.
|
||||
/// This keeps the structure trivial to construct.
|
||||
#define INSERT_PADDING_BYTES_NOINIT(num_bytes) \
|
||||
[[maybe_unused]] std::array<u8, num_bytes> CONCAT2(pad, __LINE__)
|
||||
#define INSERT_PADDING_WORDS_NOINIT(num_words) \
|
||||
[[maybe_unused]] std::array<u32, num_words> CONCAT2(pad, __LINE__)
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define locale_t _locale_t // Locale Cross-Compatibility
|
||||
#endif // _MSC_VER
|
||||
|
||||
#define DECLARE_ENUM_FLAG_OPERATORS(type) \
|
||||
[[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
|
||||
return type(std::to_underlying(a) | std::to_underlying(b)); \
|
||||
} \
|
||||
[[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
|
||||
return type(std::to_underlying(a) & std::to_underlying(b)); \
|
||||
} \
|
||||
[[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
|
||||
return type(std::to_underlying(a) ^ std::to_underlying(b)); \
|
||||
} \
|
||||
[[nodiscard]] constexpr type operator<<(type a, type b) noexcept { \
|
||||
return type(std::to_underlying(a) << std::to_underlying(b)); \
|
||||
} \
|
||||
[[nodiscard]] constexpr type operator>>(type a, type b) noexcept { \
|
||||
return type(std::to_underlying(a) >> std::to_underlying(b)); \
|
||||
} \
|
||||
constexpr type operator|=(type& a, type b) noexcept { \
|
||||
return a = a | b; \
|
||||
} \
|
||||
constexpr type operator&=(type& a, type b) noexcept { \
|
||||
return a = a & b; \
|
||||
} \
|
||||
constexpr type operator^=(type& a, type b) noexcept { \
|
||||
return a = a ^ b; \
|
||||
} \
|
||||
constexpr type operator<<=(type& a, type b) noexcept { \
|
||||
return a = a << b; \
|
||||
} \
|
||||
constexpr type operator>>=(type& a, type b) noexcept { \
|
||||
return a = a >> b; \
|
||||
} \
|
||||
[[nodiscard]] constexpr type operator~(type key) noexcept { \
|
||||
return type(~std::to_underlying(key)); \
|
||||
} \
|
||||
[[nodiscard]] constexpr bool True(type key) noexcept { \
|
||||
return std::to_underlying(key) != 0; \
|
||||
} \
|
||||
[[nodiscard]] constexpr bool False(type key) noexcept { \
|
||||
return std::to_underlying(key) == 0; \
|
||||
}
|
||||
|
||||
#define YUZU_NON_COPYABLE(cls) \
|
||||
cls(const cls&) = delete; \
|
||||
cls& operator=(const cls&) = delete
|
||||
|
||||
#define YUZU_NON_MOVEABLE(cls) \
|
||||
cls(cls&&) = delete; \
|
||||
cls& operator=(cls&&) = delete
|
||||
|
||||
// suyu-branded aliases
|
||||
#define SUYU_NON_COPYABLE(cls) YUZU_NON_COPYABLE(cls)
|
||||
#define SUYU_NON_MOVEABLE(cls) YUZU_NON_MOVEABLE(cls)
|
||||
|
||||
namespace Common {
|
||||
|
||||
[[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) {
|
||||
return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr u64 MakeMagic(char a, char b, char c, char d, char e, char f, char g, char h) {
|
||||
return u64(a) << 0 | u64(b) << 8 | u64(c) << 16 | u64(d) << 24 | u64(e) << 32 | u64(f) << 40 | u64(g) << 48 | u64(h) << 56;
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
14
gpu/third_party/suyu/common/common_precompiled_headers.h
vendored
Normal file
14
gpu/third_party/suyu/common/common_precompiled_headers.h
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "common/common_types.h"
|
||||
56
gpu/third_party/suyu/common/common_types.h
vendored
Normal file
56
gpu/third_party/suyu/common/common_types.h
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: 2012 Gekko Emulator
|
||||
// SPDX-FileContributor: ShizZy <shizzy247@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/**
|
||||
* Copyright (C) 2005-2012 Gekko Emulator
|
||||
*
|
||||
* @file common_types.h
|
||||
* @author ShizZy <shizzy247@gmail.com>
|
||||
* @date 2012-02-11
|
||||
* @brief Common types used throughout the project
|
||||
*
|
||||
* @section LICENSE
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that 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 at
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* Official project repository can be found at:
|
||||
* http://code.google.com/p/gekko-gc-emu/
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
using u8 = std::uint8_t; ///< 8-bit unsigned byte
|
||||
using u16 = std::uint16_t; ///< 16-bit unsigned short
|
||||
using u32 = std::uint32_t; ///< 32-bit unsigned word
|
||||
using u64 = std::uint64_t; ///< 64-bit unsigned int
|
||||
|
||||
using s8 = std::int8_t; ///< 8-bit signed byte
|
||||
using s16 = std::int16_t; ///< 16-bit signed short
|
||||
using s32 = std::int32_t; ///< 32-bit signed word
|
||||
using s64 = std::int64_t; ///< 64-bit signed int
|
||||
|
||||
using f32 = float; ///< 32-bit floating point
|
||||
using f64 = double; ///< 64-bit floating point
|
||||
|
||||
using VAddr = u64; ///< Represents a pointer in the userspace virtual address space.
|
||||
using DAddr = u64; ///< Represents a pointer in the device specific virtual address space.
|
||||
using PAddr = u64; ///< Represents a pointer in the ARM11 physical address space.
|
||||
using GPUVAddr = u64; ///< Represents a pointer in the GPU virtual address space.
|
||||
|
||||
using u128 = std::array<std::uint64_t, 2>;
|
||||
static_assert(sizeof(u128) == 16, "u128 must be 128 bits wide");
|
||||
25
gpu/third_party/suyu/common/div_ceil.h
vendored
Normal file
25
gpu/third_party/suyu/common/div_ceil.h
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Common {
|
||||
|
||||
/// Ceiled integer division.
|
||||
template <typename N, typename D>
|
||||
requires std::is_integral_v<N> && std::is_unsigned_v<D>
|
||||
[[nodiscard]] constexpr N DivCeil(N number, D divisor) {
|
||||
return static_cast<N>((static_cast<D>(number) + divisor - 1) / divisor);
|
||||
}
|
||||
|
||||
/// Ceiled integer division with logarithmic divisor in base 2
|
||||
template <typename N, typename D>
|
||||
requires std::is_integral_v<N> && std::is_unsigned_v<D>
|
||||
[[nodiscard]] constexpr N DivCeilLog2(N value, D alignment_log2) {
|
||||
return static_cast<N>((static_cast<D>(value) + (D(1) << alignment_log2) - 1) >> alignment_log2);
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
15
gpu/third_party/suyu/common/logging.h
vendored
Normal file
15
gpu/third_party/suyu/common/logging.h
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
// Rexcomp replacement for suyu's common/logging.h: log lines go nowhere unless GPU_SHADER_LOG is defined.
|
||||
#pragma once
|
||||
#include <fmt/format.h> // suyu code reaches fmt through this header
|
||||
#ifdef GPU_SHADER_LOG
|
||||
#include <cstdio>
|
||||
#define LOG_GENERIC_(level, cls, ...) std::fprintf(stderr, "[shader %s] %s\n", level, fmt::format(__VA_ARGS__).c_str())
|
||||
#else
|
||||
#define LOG_GENERIC_(level, cls, ...) ((void)0)
|
||||
#endif
|
||||
#define LOG_TRACE(cls, ...) ((void)0)
|
||||
#define LOG_DEBUG(cls, ...) ((void)0)
|
||||
#define LOG_INFO(cls, ...) LOG_GENERIC_("info", cls, __VA_ARGS__)
|
||||
#define LOG_WARNING(cls, ...) LOG_GENERIC_("warning", cls, __VA_ARGS__)
|
||||
#define LOG_ERROR(cls, ...) LOG_GENERIC_("error", cls, __VA_ARGS__)
|
||||
#define LOG_CRITICAL(cls, ...) LOG_GENERIC_("critical", cls, __VA_ARGS__)
|
||||
100
gpu/third_party/suyu/common/math_util.h
vendored
Normal file
100
gpu/third_party/suyu/common/math_util.h
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
|
||||
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Common {
|
||||
|
||||
template <class T>
|
||||
struct Rectangle {
|
||||
T left{};
|
||||
T top{};
|
||||
T right{};
|
||||
T bottom{};
|
||||
|
||||
constexpr Rectangle() = default;
|
||||
|
||||
constexpr Rectangle(T width, T height) : right(width), bottom(height) {}
|
||||
|
||||
constexpr Rectangle(T left_, T top_, T right_, T bottom_)
|
||||
: left(left_), top(top_), right(right_), bottom(bottom_) {}
|
||||
|
||||
[[nodiscard]] constexpr T Left() const {
|
||||
return left;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T Top() const {
|
||||
return top;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T Right() const {
|
||||
return right;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T Bottom() const {
|
||||
return bottom;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool IsEmpty() const {
|
||||
return (GetWidth() <= 0) || (GetHeight() <= 0);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T GetWidth() const {
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
return std::abs(right - left);
|
||||
} else {
|
||||
return static_cast<T>(std::abs(static_cast<std::make_signed_t<T>>(right - left)));
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr T GetHeight() const {
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
return std::abs(bottom - top);
|
||||
} else {
|
||||
return static_cast<T>(std::abs(static_cast<std::make_signed_t<T>>(bottom - top)));
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Rectangle<T> TranslateX(const T x) const {
|
||||
return Rectangle{left + x, top, right + x, bottom};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Rectangle<T> TranslateY(const T y) const {
|
||||
return Rectangle{left, top + y, right, bottom + y};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Rectangle<T> Scale(const float s) const {
|
||||
return Rectangle{left, top, static_cast<T>(static_cast<float>(left + GetWidth()) * s),
|
||||
static_cast<T>(static_cast<float>(top + GetHeight()) * s)};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Rectangle<T>& rhs) const {
|
||||
return (left == rhs.left) && (top == rhs.top) && (right == rhs.right) &&
|
||||
(bottom == rhs.bottom);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Rectangle<T>& rhs) const {
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool Intersect(const Rectangle<T>& with, Rectangle<T>* result) const {
|
||||
result->left = (std::max)(left, with.left);
|
||||
result->top = (std::max)(top, with.top);
|
||||
result->right = (std::min)(right, with.right);
|
||||
result->bottom = (std::min)(bottom, with.bottom);
|
||||
return !result->IsEmpty();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Rectangle(T, T, T, T) -> Rectangle<T>;
|
||||
|
||||
} // namespace Common
|
||||
28
gpu/third_party/suyu/common/settings.h
vendored
Normal file
28
gpu/third_party/suyu/common/settings.h
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
// Rexcomp replacement for suyu's common/settings.h: only the values shader_recompiler reads, at suyu's defaults.
|
||||
#pragma once
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Settings {
|
||||
enum class AstcRecompression : u32 { Uncompressed = 0, Bc1 = 1, Bc3 = 2 };
|
||||
template <typename T>
|
||||
struct Setting {
|
||||
T value;
|
||||
T GetValue() const { return value; }
|
||||
operator T() const { return value; }
|
||||
};
|
||||
struct ResolutionScalingInfo {
|
||||
u32 up_scale{1};
|
||||
u32 down_shift{0};
|
||||
f32 up_factor{1.0f};
|
||||
f32 down_factor{1.0f};
|
||||
bool active{false};
|
||||
};
|
||||
struct Values {
|
||||
Setting<bool> disable_shader_loop_safety_checks{false};
|
||||
Setting<bool> renderer_debug{false};
|
||||
Setting<bool> rescale_hack{false};
|
||||
ResolutionScalingInfo resolution_info{};
|
||||
Setting<AstcRecompression> astc_recompression{AstcRecompression::Uncompressed};
|
||||
};
|
||||
inline Values values;
|
||||
} // namespace Settings
|
||||
664
gpu/third_party/suyu/common/swap.h
vendored
Normal file
664
gpu/third_party/suyu/common/swap.h
vendored
Normal file
@@ -0,0 +1,664 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu 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
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <bit>
|
||||
#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 <typename T, typename F>
|
||||
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 <typename S>
|
||||
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 <typename S>
|
||||
SwappedT operator/(const S& i) const {
|
||||
return swap() / i;
|
||||
}
|
||||
|
||||
// v * 5
|
||||
SwappedT operator*(const SwappedT& i) const {
|
||||
return swap() * i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT operator*(const S& i) const {
|
||||
return swap() * i;
|
||||
}
|
||||
|
||||
// v + 5
|
||||
SwappedT operator+(const SwappedT& i) const {
|
||||
return swap() + i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT operator+(const S& i) const {
|
||||
return swap() + T(i);
|
||||
}
|
||||
// v - 5
|
||||
SwappedT operator-(const SwappedT& i) const {
|
||||
return swap() - i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
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 <typename S>
|
||||
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 <typename S>
|
||||
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 <typename S>
|
||||
bool operator==(const S& i) const {
|
||||
return swap() == i;
|
||||
}
|
||||
|
||||
// v != i
|
||||
bool operator!=(const SwappedT& i) const {
|
||||
return swap() != i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
bool operator!=(const S& i) const {
|
||||
return swap() != i;
|
||||
}
|
||||
|
||||
// v > i
|
||||
bool operator>(const SwappedT& i) const {
|
||||
return swap() > i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
bool operator>(const S& i) const {
|
||||
return swap() > i;
|
||||
}
|
||||
|
||||
// v < i
|
||||
bool operator<(const SwappedT& i) const {
|
||||
return swap() < i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
bool operator<(const S& i) const {
|
||||
return swap() < i;
|
||||
}
|
||||
|
||||
// v >= i
|
||||
bool operator>=(const SwappedT& i) const {
|
||||
return swap() >= i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
bool operator>=(const S& i) const {
|
||||
return swap() >= i;
|
||||
}
|
||||
|
||||
// v <= i
|
||||
bool operator<=(const SwappedT& i) const {
|
||||
return swap() <= i.swap();
|
||||
}
|
||||
template <typename S>
|
||||
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 <typename S>
|
||||
SwappedT operator&(const S& b) const {
|
||||
return swap() & b;
|
||||
}
|
||||
SwappedT& operator&=(const SwappedT& b) {
|
||||
value = swap(swap() & b.swap());
|
||||
return *this;
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT& operator&=(const S b) {
|
||||
value = swap(swap() & b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
SwappedT operator|(const SwappedT& b) const {
|
||||
return swap() | b.swap();
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT operator|(const S& b) const {
|
||||
return swap() | b;
|
||||
}
|
||||
SwappedT& operator|=(const SwappedT& b) {
|
||||
value = swap(swap() | b.swap());
|
||||
return *this;
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT& operator|=(const S& b) {
|
||||
value = swap(swap() | b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
SwappedT operator^(const SwappedT& b) const {
|
||||
return swap() ^ b.swap();
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT operator^(const S& b) const {
|
||||
return swap() ^ b;
|
||||
}
|
||||
SwappedT& operator^=(const SwappedT& b) {
|
||||
value = swap(swap() ^ b.swap());
|
||||
return *this;
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT& operator^=(const S& b) {
|
||||
value = swap(swap() ^ b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
SwappedT operator<<(const S& b) const {
|
||||
return swap() << b;
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT& operator<<=(const S& b) const {
|
||||
value = swap(swap() << b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
SwappedT operator>>(const S& b) const {
|
||||
return swap() >> b;
|
||||
}
|
||||
template <typename S>
|
||||
SwappedT& operator>>=(const S& b) const {
|
||||
value = swap(swap() >> b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Member
|
||||
/** todo **/
|
||||
|
||||
// Arithmetic
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator+(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator-(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator/(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator*(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator%(const S& p, const SwappedT v);
|
||||
|
||||
// Arithmetic + assignments
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator+=(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator-=(const S& p, const SwappedT v);
|
||||
|
||||
// Bitmath
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend S operator&(const S& p, const SwappedT v);
|
||||
|
||||
// Comparison
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator<(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator>(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator<=(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator>=(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator!=(const S& p, const SwappedT v);
|
||||
|
||||
template <typename S, typename T2, typename F2>
|
||||
friend bool operator==(const S& p, const SwappedT v);
|
||||
};
|
||||
|
||||
// Arithmetic
|
||||
template <typename S, typename T, typename F>
|
||||
S operator+(const S& i, const SwapStructT<T, F> v) {
|
||||
return i + v.swap();
|
||||
}
|
||||
|
||||
template <typename S, typename T, typename F>
|
||||
S operator-(const S& i, const SwapStructT<T, F> v) {
|
||||
return i - v.swap();
|
||||
}
|
||||
|
||||
template <typename S, typename T, typename F>
|
||||
S operator/(const S& i, const SwapStructT<T, F> v) {
|
||||
return i / v.swap();
|
||||
}
|
||||
|
||||
template <typename S, typename T, typename F>
|
||||
S operator*(const S& i, const SwapStructT<T, F> v) {
|
||||
return i * v.swap();
|
||||
}
|
||||
|
||||
template <typename S, typename T, typename F>
|
||||
S operator%(const S& i, const SwapStructT<T, F> v) {
|
||||
return i % v.swap();
|
||||
}
|
||||
|
||||
// Arithmetic + assignments
|
||||
template <typename S, typename T, typename F>
|
||||
S& operator+=(S& i, const SwapStructT<T, F> v) {
|
||||
i += v.swap();
|
||||
return i;
|
||||
}
|
||||
|
||||
template <typename S, typename T, typename F>
|
||||
S& operator-=(S& i, const SwapStructT<T, F> v) {
|
||||
i -= v.swap();
|
||||
return i;
|
||||
}
|
||||
|
||||
// Logical
|
||||
template <typename S, typename T, typename F>
|
||||
S operator&(const S& i, const SwapStructT<T, F> v) {
|
||||
return i & v.swap();
|
||||
}
|
||||
|
||||
// Comparison
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator<(const S& p, const SwapStructT<T, F> v) {
|
||||
return p < v.swap();
|
||||
}
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator>(const S& p, const SwapStructT<T, F> v) {
|
||||
return p > v.swap();
|
||||
}
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator<=(const S& p, const SwapStructT<T, F> v) {
|
||||
return p <= v.swap();
|
||||
}
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator>=(const S& p, const SwapStructT<T, F> v) {
|
||||
return p >= v.swap();
|
||||
}
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator!=(const S& p, const SwapStructT<T, F> v) {
|
||||
return p != v.swap();
|
||||
}
|
||||
template <typename S, typename T, typename F>
|
||||
bool operator==(const S& p, const SwapStructT<T, F> v) {
|
||||
return p == v.swap();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct Swap64T {
|
||||
static T swap(T x) {
|
||||
return T(Common::swap64(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Swap32T {
|
||||
static T swap(T x) {
|
||||
return T(Common::swap32(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Swap16T {
|
||||
static T swap(T x) {
|
||||
return T(Common::swap16(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct SwapFloatT {
|
||||
static T swap(T x) {
|
||||
return T(Common::swapf(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct SwapDoubleT {
|
||||
static T swap(T x) {
|
||||
return T(Common::swapd(x));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct SwapEnumT {
|
||||
static_assert(std::is_enum_v<T>);
|
||||
using base = std::underlying_type_t<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<base, u16>, Swap16T<u16>, std::conditional_t<
|
||||
std::is_same_v<base, s16>, Swap16T<s16>, std::conditional_t<
|
||||
std::is_same_v<base, u32>, Swap32T<u32>, std::conditional_t<
|
||||
std::is_same_v<base, s32>, Swap32T<s32>, std::conditional_t<
|
||||
std::is_same_v<base, u64>, Swap64T<u64>, std::conditional_t<
|
||||
std::is_same_v<base, s64>, Swap64T<s64>, 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 <typename T, typename Tag>
|
||||
struct AddEndian;
|
||||
|
||||
// KeepTag specializations
|
||||
|
||||
template <typename T>
|
||||
struct AddEndian<T, KeepTag> {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
// SwapTag specializations
|
||||
|
||||
template <>
|
||||
struct AddEndian<u8, SwapTag> {
|
||||
using type = u8;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<u16, SwapTag> {
|
||||
using type = SwapStructT<u16, Swap16T<u16>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<u32, SwapTag> {
|
||||
using type = SwapStructT<u32, Swap32T<u32>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<u64, SwapTag> {
|
||||
using type = SwapStructT<u64, Swap64T<u64>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<s8, SwapTag> {
|
||||
using type = s8;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<s16, SwapTag> {
|
||||
using type = SwapStructT<s16, Swap16T<s16>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<s32, SwapTag> {
|
||||
using type = SwapStructT<s32, Swap32T<s32>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<s64, SwapTag> {
|
||||
using type = SwapStructT<s64, Swap64T<s64>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<float, SwapTag> {
|
||||
using type = SwapStructT<float, SwapFloatT<float>>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct AddEndian<double, SwapTag> {
|
||||
using type = SwapStructT<double, SwapDoubleT<double>>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct AddEndian<T, SwapTag> {
|
||||
static_assert(std::is_enum_v<T>);
|
||||
using type = SwapEnumT<T>;
|
||||
};
|
||||
|
||||
// Alias LETag/BETag as KeepTag/SwapTag depending on the system
|
||||
using LETag = std::conditional_t<std::endian::native == std::endian::little, KeepTag, SwapTag>;
|
||||
using BETag = std::conditional_t<std::endian::native == std::endian::big, KeepTag, SwapTag>;
|
||||
|
||||
// Aliases for LE types
|
||||
using u16_le = AddEndian<u16, LETag>::type;
|
||||
using u32_le = AddEndian<u32, LETag>::type;
|
||||
using u64_le = AddEndian<u64, LETag>::type;
|
||||
|
||||
using s16_le = AddEndian<s16, LETag>::type;
|
||||
using s32_le = AddEndian<s32, LETag>::type;
|
||||
using s64_le = AddEndian<s64, LETag>::type;
|
||||
|
||||
template <typename T>
|
||||
using enum_le = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, LETag>::type>;
|
||||
|
||||
using float_le = AddEndian<float, LETag>::type;
|
||||
using double_le = AddEndian<double, LETag>::type;
|
||||
|
||||
// Aliases for BE types
|
||||
using u16_be = AddEndian<u16, BETag>::type;
|
||||
using u32_be = AddEndian<u32, BETag>::type;
|
||||
using u64_be = AddEndian<u64, BETag>::type;
|
||||
|
||||
using s16_be = AddEndian<s16, BETag>::type;
|
||||
using s32_be = AddEndian<s32, BETag>::type;
|
||||
using s64_be = AddEndian<s64, BETag>::type;
|
||||
|
||||
template <typename T>
|
||||
using enum_be = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, BETag>::type>;
|
||||
|
||||
using float_be = AddEndian<float, BETag>::type;
|
||||
using double_be = AddEndian<double, BETag>::type;
|
||||
Reference in New Issue
Block a user