// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: GPL-3.0+ #pragma once #include "common/Pcsx2Defs.h" #include #include #ifdef _MSC_VER #include #else static inline int _BitScanReverse(unsigned long* const Index, const unsigned long Mask) { if (Mask == 0) return 0; // For some reason, clang won't emit bsr if we use std::countl_zeros()... *Index = 31 - __builtin_clz(Mask); return 1; } #endif namespace Common { template static constexpr __fi bool IsAligned(T value, unsigned int alignment) { return (value % static_cast(alignment)) == 0; } template static constexpr __fi T AlignUp(T value, unsigned int alignment) { return (value + static_cast(alignment - 1)) / static_cast(alignment) * static_cast(alignment); } template static constexpr __fi T AlignDown(T value, unsigned int alignment) { return value / static_cast(alignment) * static_cast(alignment); } template static constexpr __fi bool IsAlignedPow2(T value, unsigned int alignment) { return (value & static_cast(alignment - 1)) == 0; } template static constexpr __fi T AlignUpPow2(T value, unsigned int alignment) { return (value + static_cast(alignment - 1)) & static_cast(~static_cast(alignment - 1)); } template static constexpr __fi T AlignDownPow2(T value, unsigned int alignment) { return value & static_cast(~static_cast(alignment - 1)); } template static constexpr T PageAlign(T size) { static_assert(std::has_single_bit(__pagesize), "Page size is a power of 2"); return Common::AlignUpPow2(size, __pagesize); } __fi static u32 CountLeadingSignBits(s32 n) { // If the sign bit is 1, we invert the bits to 0 for count-leading-zero. if (n < 0) n = ~n; // If BSR is used directly, it would have an undefined value for 0. if (n == 0) return 32; // Perform our count leading zero. return std::countl_zero(static_cast(n)); } } // namespace Common template [[maybe_unused]] __fi static T GetBufferT(const u8* buffer, u32 offset) { T value; std::memcpy(&value, buffer + offset, sizeof(value)); return value; } [[maybe_unused]] __fi static u8 GetBufferU8(const u8* buffer, u32 offset) { return GetBufferT(buffer, offset); } [[maybe_unused]] __fi static u16 GetBufferU16(const u8* buffer, u32 offset) { return GetBufferT(buffer, offset); } [[maybe_unused]] __fi static u32 GetBufferU32(const u8* buffer, u32 offset) { return GetBufferT(buffer, offset); } [[maybe_unused]] __fi static u64 GetBufferU64(const u8* buffer, u32 offset) { return GetBufferT(buffer, offset); }