From bcb6f98cbd6fe2bbf079be874165dbe748a82d62 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 17 Aug 2025 13:15:56 +1000 Subject: [PATCH] Common: Eliminate more static functions in headers --- src/common/align.h | 22 ++++++------- src/common/bitutils.h | 52 +++++++++++++++---------------- src/common/easing.h | 60 ++++++++++++++++++------------------ src/common/minizip_helpers.h | 8 ++--- src/common/zip_helpers.h | 39 +++++++++++------------ 5 files changed, 89 insertions(+), 92 deletions(-) diff --git a/src/common/align.h b/src/common/align.h index 95f3e98a2..a88874cd6 100644 --- a/src/common/align.h +++ b/src/common/align.h @@ -15,42 +15,42 @@ namespace Common { template -constexpr bool IsAligned(T value, unsigned int alignment) +ALWAYS_INLINE constexpr bool IsAligned(T value, unsigned int alignment) { return (value % static_cast(alignment)) == 0; } template -constexpr T AlignUp(T value, unsigned int alignment) +ALWAYS_INLINE constexpr T AlignUp(T value, unsigned int alignment) { return (value + static_cast(alignment - 1)) / static_cast(alignment) * static_cast(alignment); } template -constexpr T AlignDown(T value, unsigned int alignment) +ALWAYS_INLINE constexpr T AlignDown(T value, unsigned int alignment) { return value / static_cast(alignment) * static_cast(alignment); } template -constexpr bool IsAlignedPow2(T value, unsigned int alignment) +ALWAYS_INLINE constexpr bool IsAlignedPow2(T value, unsigned int alignment) { return (value & static_cast(alignment - 1)) == 0; } template -constexpr T AlignUpPow2(T value, unsigned int alignment) +ALWAYS_INLINE constexpr T AlignUpPow2(T value, unsigned int alignment) { return (value + static_cast(alignment - 1)) & static_cast(~static_cast(alignment - 1)); } template -constexpr T AlignDownPow2(T value, unsigned int alignment) +ALWAYS_INLINE constexpr T AlignDownPow2(T value, unsigned int alignment) { return value & static_cast(~static_cast(alignment - 1)); } template -constexpr bool IsPow2(T value) +ALWAYS_INLINE constexpr bool IsPow2(T value) { return (value & (value - 1)) == 0; } template -constexpr T PreviousPow2(T value) +ALWAYS_INLINE constexpr T PreviousPow2(T value) { if (value == static_cast(0)) return 0; @@ -70,7 +70,7 @@ constexpr T PreviousPow2(T value) /// NOTE: Undefined for values greater than (1 << BITS-1), i.e. 0x80000000 for 32-bit. template -constexpr T NextPow2(T value) +ALWAYS_INLINE constexpr T NextPow2(T value) { // https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 if (value == static_cast(0)) @@ -90,7 +90,7 @@ constexpr T NextPow2(T value) return value; } -ALWAYS_INLINE static void* AlignedMalloc(size_t size, size_t alignment) +ALWAYS_INLINE void* AlignedMalloc(size_t size, size_t alignment) { #ifdef _MSC_VER return _aligned_malloc(size, alignment); @@ -105,7 +105,7 @@ ALWAYS_INLINE static void* AlignedMalloc(size_t size, size_t alignment) #endif } -ALWAYS_INLINE static void AlignedFree(void* ptr) +ALWAYS_INLINE void AlignedFree(void* ptr) { #ifdef _MSC_VER _aligned_free(ptr); diff --git a/src/common/bitutils.h b/src/common/bitutils.h index c21e7acaf..fb92adbda 100644 --- a/src/common/bitutils.h +++ b/src/common/bitutils.h @@ -14,14 +14,14 @@ // Zero-extending helper template -ALWAYS_INLINE static constexpr TReturn ZeroExtend(TValue value) +ALWAYS_INLINE constexpr TReturn ZeroExtend(TValue value) { return static_cast(static_cast::type>( static_cast::type>(value))); } // Sign-extending helper template -ALWAYS_INLINE static constexpr TReturn SignExtend(TValue value) +ALWAYS_INLINE constexpr TReturn SignExtend(TValue value) { return static_cast( static_cast::type>(static_cast::type>(value))); @@ -29,101 +29,101 @@ ALWAYS_INLINE static constexpr TReturn SignExtend(TValue value) // Type-specific helpers template -ALWAYS_INLINE static constexpr u16 ZeroExtend16(TValue value) +ALWAYS_INLINE constexpr u16 ZeroExtend16(TValue value) { return ZeroExtend(value); } template -ALWAYS_INLINE static constexpr u32 ZeroExtend32(TValue value) +ALWAYS_INLINE constexpr u32 ZeroExtend32(TValue value) { return ZeroExtend(value); } template -ALWAYS_INLINE static constexpr u64 ZeroExtend64(TValue value) +ALWAYS_INLINE constexpr u64 ZeroExtend64(TValue value) { return ZeroExtend(value); } template -ALWAYS_INLINE static constexpr u16 SignExtend16(TValue value) +ALWAYS_INLINE constexpr u16 SignExtend16(TValue value) { return SignExtend(value); } template -ALWAYS_INLINE static constexpr u32 SignExtend32(TValue value) +ALWAYS_INLINE constexpr u32 SignExtend32(TValue value) { return SignExtend(value); } template -ALWAYS_INLINE static constexpr u64 SignExtend64(TValue value) +ALWAYS_INLINE constexpr u64 SignExtend64(TValue value) { return SignExtend(value); } template -ALWAYS_INLINE static constexpr u8 Truncate8(TValue value) +ALWAYS_INLINE constexpr u8 Truncate8(TValue value) { return static_cast(static_cast::type>(value)); } template -ALWAYS_INLINE static constexpr u16 Truncate16(TValue value) +ALWAYS_INLINE constexpr u16 Truncate16(TValue value) { return static_cast(static_cast::type>(value)); } template -ALWAYS_INLINE static constexpr u32 Truncate32(TValue value) +ALWAYS_INLINE constexpr u32 Truncate32(TValue value) { return static_cast(static_cast::type>(value)); } // BCD helpers -ALWAYS_INLINE static constexpr u8 BinaryToBCD(u8 value) +ALWAYS_INLINE constexpr u8 BinaryToBCD(u8 value) { return ((value / 10) << 4) + (value % 10); } -ALWAYS_INLINE static constexpr u8 PackedBCDToBinary(u8 value) +ALWAYS_INLINE constexpr u8 PackedBCDToBinary(u8 value) { return ((value >> 4) * 10) + (value % 16); } -ALWAYS_INLINE static constexpr u8 IsValidBCDDigit(u8 digit) +ALWAYS_INLINE constexpr u8 IsValidBCDDigit(u8 digit) { return (digit <= 9); } -ALWAYS_INLINE static constexpr u8 IsValidPackedBCD(u8 value) +ALWAYS_INLINE constexpr u8 IsValidPackedBCD(u8 value) { return IsValidBCDDigit(value & 0x0F) && IsValidBCDDigit(value >> 4); } // Boolean to integer -ALWAYS_INLINE static constexpr u8 BoolToUInt8(bool value) +ALWAYS_INLINE constexpr u8 BoolToUInt8(bool value) { return static_cast(value); } -ALWAYS_INLINE static constexpr u16 BoolToUInt16(bool value) +ALWAYS_INLINE constexpr u16 BoolToUInt16(bool value) { return static_cast(value); } -ALWAYS_INLINE static constexpr u32 BoolToUInt32(bool value) +ALWAYS_INLINE constexpr u32 BoolToUInt32(bool value) { return static_cast(value); } -ALWAYS_INLINE static constexpr u64 BoolToUInt64(bool value) +ALWAYS_INLINE constexpr u64 BoolToUInt64(bool value) { return static_cast(value); } -ALWAYS_INLINE static constexpr float BoolToFloat(bool value) +ALWAYS_INLINE constexpr float BoolToFloat(bool value) { return static_cast(value); } // Integer to boolean template -ALWAYS_INLINE static constexpr bool ConvertToBool(TValue value) +ALWAYS_INLINE constexpr bool ConvertToBool(TValue value) { return static_cast(value); } // Unsafe integer to boolean template -ALWAYS_INLINE static bool ConvertToBoolUnchecked(TValue value) +ALWAYS_INLINE bool ConvertToBoolUnchecked(TValue value) { // static_assert(sizeof(uint8) == sizeof(bool)); bool ret; @@ -133,7 +133,7 @@ ALWAYS_INLINE static bool ConvertToBoolUnchecked(TValue value) // Generic sign extension template -ALWAYS_INLINE static constexpr T SignExtendN(T value) +ALWAYS_INLINE constexpr T SignExtendN(T value) { // http://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend constexpr int shift = 8 * sizeof(T) - NBITS; @@ -142,7 +142,7 @@ ALWAYS_INLINE static constexpr T SignExtendN(T value) /// Returns the number of zero bits before the first set bit, going MSB->LSB. template -ALWAYS_INLINE static unsigned CountLeadingZeros(T value) +ALWAYS_INLINE unsigned CountLeadingZeros(T value) { #ifdef _MSC_VER if constexpr (sizeof(value) >= sizeof(u64)) @@ -169,7 +169,7 @@ ALWAYS_INLINE static unsigned CountLeadingZeros(T value) /// Returns the number of zero bits before the first set bit, going LSB->MSB. template -ALWAYS_INLINE static unsigned CountTrailingZeros(T value) +ALWAYS_INLINE unsigned CountTrailingZeros(T value) { #ifdef _MSC_VER if constexpr (sizeof(value) >= sizeof(u64)) @@ -194,7 +194,7 @@ ALWAYS_INLINE static unsigned CountTrailingZeros(T value) // C++23-like std::byteswap() template -ALWAYS_INLINE static T ByteSwap(T value) +ALWAYS_INLINE T ByteSwap(T value) { if constexpr (std::is_signed_v) { diff --git a/src/common/easing.h b/src/common/easing.h index 051fed469..8aa738ebc 100644 --- a/src/common/easing.h +++ b/src/common/easing.h @@ -11,75 +11,75 @@ namespace Easing { inline constexpr float pi = 3.1415926545f; template -ALWAYS_INLINE_RELEASE static T InSine(T t) +ALWAYS_INLINE_RELEASE T InSine(T t) { return std::sin(1.5707963f * t); } template -ALWAYS_INLINE_RELEASE static T OutSine(T t) +ALWAYS_INLINE_RELEASE T OutSine(T t) { return 1 + std::sin(1.5707963f * (--t)); } template -ALWAYS_INLINE_RELEASE static T InOutSine(T t) +ALWAYS_INLINE_RELEASE T InOutSine(T t) { return 0.5f * (1 + std::sin(3.1415926f * (t - 0.5f))); } template -ALWAYS_INLINE_RELEASE static T InQuad(T t) +ALWAYS_INLINE_RELEASE T InQuad(T t) { return t * t; } template -ALWAYS_INLINE_RELEASE static T OutQuad(T t) +ALWAYS_INLINE_RELEASE T OutQuad(T t) { return t * (2 - t); } template -ALWAYS_INLINE_RELEASE static T InOutQuad(T t) +ALWAYS_INLINE_RELEASE T InOutQuad(T t) { return t < 0.5f ? 2 * t * t : t * (4 - 2 * t) - 1; } template -ALWAYS_INLINE_RELEASE static T InCubic(T t) +ALWAYS_INLINE_RELEASE T InCubic(T t) { return t * t * t; } template -ALWAYS_INLINE_RELEASE static T OutCubic(T t) +ALWAYS_INLINE_RELEASE T OutCubic(T t) { return 1 + (--t) * t * t; } template -ALWAYS_INLINE_RELEASE static T InOutCubic(T t) +ALWAYS_INLINE_RELEASE T InOutCubic(T t) { return t < 0.5f ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); } template -ALWAYS_INLINE_RELEASE static T InQuart(T t) +ALWAYS_INLINE_RELEASE T InQuart(T t) { t *= t; return t * t; } template -ALWAYS_INLINE_RELEASE static T OutQuart(T t) +ALWAYS_INLINE_RELEASE T OutQuart(T t) { t = (--t) * t; return 1 - t * t; } template -ALWAYS_INLINE_RELEASE static T InOutQuart(T t) +ALWAYS_INLINE_RELEASE T InOutQuart(T t) { if (t < 0.5) { @@ -94,21 +94,21 @@ ALWAYS_INLINE_RELEASE static T InOutQuart(T t) } template -ALWAYS_INLINE_RELEASE static T InQuint(T t) +ALWAYS_INLINE_RELEASE T InQuint(T t) { T t2 = t * t; return t * t2 * t2; } template -ALWAYS_INLINE_RELEASE static T OutQuint(T t) +ALWAYS_INLINE_RELEASE T OutQuint(T t) { T t2 = (--t) * t; return 1 + t * t2 * t2; } template -ALWAYS_INLINE_RELEASE static T InOutQuint(T t) +ALWAYS_INLINE_RELEASE T InOutQuint(T t) { T t2; if (t < 0.5) @@ -124,19 +124,19 @@ ALWAYS_INLINE_RELEASE static T InOutQuint(T t) } template -ALWAYS_INLINE_RELEASE static T InExpo(T t) +ALWAYS_INLINE_RELEASE T InExpo(T t) { return (std::pow(static_cast(2), static_cast(8) * t) - static_cast(1)) / static_cast(255); } template -ALWAYS_INLINE_RELEASE static T OutExpo(T t) +ALWAYS_INLINE_RELEASE T OutExpo(T t) { return static_cast(1) - std::pow(static_cast(2), static_cast(-8) * t); } template -ALWAYS_INLINE_RELEASE static T InOutExpo(T t) +ALWAYS_INLINE_RELEASE T InOutExpo(T t) { if (t < 0.5f) { @@ -149,19 +149,19 @@ ALWAYS_INLINE_RELEASE static T InOutExpo(T t) } template -ALWAYS_INLINE_RELEASE static T InCirc(T t) +ALWAYS_INLINE_RELEASE T InCirc(T t) { return 1 - std::sqrt(1 - t); } template -ALWAYS_INLINE_RELEASE static T OutCirc(T t) +ALWAYS_INLINE_RELEASE T OutCirc(T t) { return std::sqrt(t); } template -ALWAYS_INLINE_RELEASE static T InOutCirc(T t) +ALWAYS_INLINE_RELEASE T InOutCirc(T t) { if (t < 0.5f) { @@ -174,20 +174,20 @@ ALWAYS_INLINE_RELEASE static T InOutCirc(T t) } template -ALWAYS_INLINE_RELEASE static T InBack(T t) +ALWAYS_INLINE_RELEASE T InBack(T t) { return t * t * (2.70158f * t - 1.70158f); } template -static T OutBack(T t) +ALWAYS_INLINE_RELEASE T OutBack(T t) { t -= 1; return 1 + t * t * (2.70158f * t + 1.70158f); } template -ALWAYS_INLINE_RELEASE static T InOutBack(T t) +ALWAYS_INLINE_RELEASE T InOutBack(T t) { if (t < 0.5f) { @@ -200,21 +200,21 @@ ALWAYS_INLINE_RELEASE static T InOutBack(T t) } template -ALWAYS_INLINE_RELEASE static T InElastic(T t) +ALWAYS_INLINE_RELEASE T InElastic(T t) { T t2 = t * t; return t2 * t2 * std::sin(t * pi * 4.5f); } template -ALWAYS_INLINE_RELEASE static T OutElastic(T t) +ALWAYS_INLINE_RELEASE T OutElastic(T t) { T t2 = (t - 1) * (t - 1); return 1 - t2 * t2 * std::cos(t * pi * 4.5f); } template -ALWAYS_INLINE_RELEASE static T InOutElastic(T t) +ALWAYS_INLINE_RELEASE T InOutElastic(T t) { T t2; if (t < 0.45f) @@ -234,19 +234,19 @@ ALWAYS_INLINE_RELEASE static T InOutElastic(T t) } template -ALWAYS_INLINE_RELEASE static T InBounce(T t) +ALWAYS_INLINE_RELEASE T InBounce(T t) { return std::pow(2, 6 * (t - 1)) * std::abs(sin(t * pi * 3.5f)); } template -ALWAYS_INLINE_RELEASE static T OutBounce(T t) +ALWAYS_INLINE_RELEASE T OutBounce(T t) { return 1 - std::pow(2, -6 * t) * std::abs(std::cos(t * pi * 3.5f)); } template -ALWAYS_INLINE_RELEASE static T InOutBounce(T t) +ALWAYS_INLINE_RELEASE T InOutBounce(T t) { if (t < 0.5f) { diff --git a/src/common/minizip_helpers.h b/src/common/minizip_helpers.h index c608c9c5d..3cdc4b474 100644 --- a/src/common/minizip_helpers.h +++ b/src/common/minizip_helpers.h @@ -13,7 +13,7 @@ namespace MinizipHelpers { -[[maybe_unused]] static unzFile OpenUnzMemoryFile(const void* memory, size_t memory_size) +inline unzFile OpenUnzMemoryFile(const void* memory, size_t memory_size) { struct MemoryFileInfo { @@ -70,7 +70,7 @@ namespace MinizipHelpers { return unzOpen2_64("", &funcs); } -[[maybe_unused]] static unzFile OpenUnzFile(const char* filename) +inline unzFile OpenUnzFile(const char* filename) { zlib_filefunc64_def funcs; fill_fopen64_filefunc(&funcs); @@ -90,8 +90,8 @@ namespace MinizipHelpers { return unzOpen2_64(filename, &funcs); } -[[maybe_unused]] static std::optional ReadZipFileToString(unzFile zf, const char* filename, - bool case_sensitivity, Error* error) +inline std::optional ReadZipFileToString(unzFile zf, const char* filename, bool case_sensitivity, + Error* error) { std::optional ret; diff --git a/src/common/zip_helpers.h b/src/common/zip_helpers.h index da6b5c50e..b6fa96e0f 100644 --- a/src/common/zip_helpers.h +++ b/src/common/zip_helpers.h @@ -16,7 +16,7 @@ namespace ZipHelpers { -[[maybe_unused]] static void SetErrorObject(Error* error, std::string_view msg, zip_error_t* ze) +inline void SetErrorObject(Error* error, std::string_view msg, zip_error_t* ze) { Error::SetStringFmt(error, "{}{}", msg, ze ? zip_error_strerror(ze) : "UNKNOWN"); if (ze) @@ -53,7 +53,7 @@ struct ZipFileDeleter using ManagedZipT = std::unique_ptr; using ManagedZipFileT = std::unique_ptr; -[[maybe_unused]] static inline ManagedZipT OpenManagedZipFile(const char* filename, int flags, Error* error = nullptr) +inline ManagedZipT OpenManagedZipFile(const char* filename, int flags, Error* error = nullptr) { zip_error_t ze; zip_source_t* zs = zip_source_file_create(filename, 0, 0, &ze); @@ -76,7 +76,7 @@ using ManagedZipFileT = std::unique_ptr; return ManagedZipT(zip); } -[[maybe_unused]] static inline ManagedZipT OpenManagedZipCFile(std::FILE* fp, int flags, Error* error = nullptr) +inline ManagedZipT OpenManagedZipCFile(std::FILE* fp, int flags, Error* error = nullptr) { zip_error_t ze; zip_source_t* zs = zip_source_filep_create(fp, 0, 0, &ze); @@ -100,8 +100,8 @@ using ManagedZipFileT = std::unique_ptr; return ManagedZipT(zip); } -[[maybe_unused]] static inline ManagedZipT OpenManagedZipBuffer(const void* buffer, size_t size, int flags, - bool free_buffer, Error* error = nullptr) +inline ManagedZipT OpenManagedZipBuffer(const void* buffer, size_t size, int flags, bool free_buffer, + Error* error = nullptr) { zip_error_t ze; zip_source_t* zs = zip_source_buffer_create(buffer, size, free_buffer, &ze); @@ -126,8 +126,7 @@ using ManagedZipFileT = std::unique_ptr; return ManagedZipT(zip); } -[[maybe_unused]] static inline ManagedZipFileT OpenManagedFileInZip(zip_t* zip, const char* filename, zip_flags_t flags, - Error* error = nullptr) +inline ManagedZipFileT OpenManagedFileInZip(zip_t* zip, const char* filename, zip_flags_t flags, Error* error = nullptr) { zip_file_t* zf = zip_fopen(zip, filename, flags); if (!zf) @@ -135,8 +134,8 @@ using ManagedZipFileT = std::unique_ptr; return ManagedZipFileT(zf); } -[[maybe_unused]] static inline ManagedZipFileT OpenManagedFileIndexInZip(zip_t* zip, zip_uint64_t index, - zip_flags_t flags, Error* error = nullptr) +inline ManagedZipFileT OpenManagedFileIndexInZip(zip_t* zip, zip_uint64_t index, zip_flags_t flags, + Error* error = nullptr) { zip_file_t* zf = zip_fopen_index(zip, index, flags); if (!zf) @@ -145,8 +144,8 @@ using ManagedZipFileT = std::unique_ptr; } template -[[maybe_unused]] static inline std::optional -ReadFileInZipToContainer(zip_t* zip, const char* name, bool case_sensitive = true, Error* error = nullptr) +inline std::optional ReadFileInZipToContainer(zip_t* zip, const char* name, bool case_sensitive = true, + Error* error = nullptr) { const int flags = case_sensitive ? 0 : ZIP_FL_NOCASE; @@ -185,8 +184,7 @@ ReadFileInZipToContainer(zip_t* zip, const char* name, bool case_sensitive = tru } template -[[maybe_unused]] static inline std::optional ReadFileInZipToContainer(zip_file_t* file, u32 chunk_size = 4096, - Error* error = nullptr) +inline std::optional ReadFileInZipToContainer(zip_file_t* file, u32 chunk_size = 4096, Error* error = nullptr) { std::optional ret = T(); for (;;) @@ -212,26 +210,25 @@ template return ret; } -[[maybe_unused]] static inline std::optional -ReadFileInZipToString(zip_t* zip, const char* name, bool case_sensitive = true, Error* error = nullptr) +inline std::optional ReadFileInZipToString(zip_t* zip, const char* name, bool case_sensitive = true, + Error* error = nullptr) { return ReadFileInZipToContainer(zip, name, case_sensitive, error); } -[[maybe_unused]] static inline std::optional ReadFileInZipToString(zip_file_t* file, u32 chunk_size = 4096, - Error* error = nullptr) +inline std::optional ReadFileInZipToString(zip_file_t* file, u32 chunk_size = 4096, Error* error = nullptr) { return ReadFileInZipToContainer(file, chunk_size, error); } -[[maybe_unused]] static inline std::optional> -ReadBinaryFileInZip(zip_t* zip, const char* name, bool case_sensitive = true, Error* error = nullptr) +inline std::optional> ReadBinaryFileInZip(zip_t* zip, const char* name, bool case_sensitive = true, + Error* error = nullptr) { return ReadFileInZipToContainer>(zip, name, case_sensitive, error); } -[[maybe_unused]] static inline std::optional> -ReadBinaryFileInZip(zip_file_t* file, u32 chunk_size = 4096, Error* error = nullptr) +inline std::optional> ReadBinaryFileInZip(zip_file_t* file, u32 chunk_size = 4096, + Error* error = nullptr) { return ReadFileInZipToContainer>(file, chunk_size, error); }