Files

101 lines
2.7 KiB
C++
Raw Permalink Normal View History

// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
2024-07-30 13:42:36 +02:00
// SPDX-License-Identifier: GPL-3.0+
2021-10-16 10:42:31 +10:00
#pragma once
2023-07-22 14:12:59 +10:00
2021-10-16 10:42:31 +10:00
#include "common/Pcsx2Defs.h"
2023-07-22 14:12:59 +10:00
#include <bit>
2023-12-26 21:25:10 +10:00
#include <cstring>
2023-07-22 14:12:59 +10:00
#ifdef _MSC_VER
#include <intrin.h>
#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
2021-10-16 10:42:31 +10:00
namespace Common
{
template <typename T>
static constexpr __fi bool IsAligned(T value, unsigned int alignment)
{
return (value % static_cast<T>(alignment)) == 0;
}
template <typename T>
static constexpr __fi T AlignUp(T value, unsigned int alignment)
{
return (value + static_cast<T>(alignment - 1)) / static_cast<T>(alignment) * static_cast<T>(alignment);
}
template <typename T>
static constexpr __fi T AlignDown(T value, unsigned int alignment)
{
return value / static_cast<T>(alignment) * static_cast<T>(alignment);
}
template <typename T>
static constexpr __fi bool IsAlignedPow2(T value, unsigned int alignment)
{
return (value & static_cast<T>(alignment - 1)) == 0;
}
template <typename T>
static constexpr __fi T AlignUpPow2(T value, unsigned int alignment)
{
return (value + static_cast<T>(alignment - 1)) & static_cast<T>(~static_cast<T>(alignment - 1));
}
template <typename T>
static constexpr __fi T AlignDownPow2(T value, unsigned int alignment)
{
return value & static_cast<T>(~static_cast<T>(alignment - 1));
}
2022-10-12 23:57:53 +10:00
template <typename T>
static constexpr T PageAlign(T size)
{
2023-07-22 14:22:06 +10:00
static_assert(std::has_single_bit(__pagesize), "Page size is a power of 2");
2022-10-12 23:57:53 +10:00
return Common::AlignUpPow2(size, __pagesize);
}
2023-07-22 14:12:59 +10:00
__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<u32>(n));
}
2021-10-16 10:42:31 +10:00
} // namespace Common
2023-12-26 21:25:10 +10:00
template <typename T>
[[maybe_unused]] __fi static T GetBufferT(const u8* buffer, u32 offset)
2023-12-26 21:25:10 +10:00
{
T value;
std::memcpy(&value, buffer + offset, sizeof(value));
return value;
}
[[maybe_unused]] __fi static u8 GetBufferU8(const u8* buffer, u32 offset) { return GetBufferT<u8>(buffer, offset); }
[[maybe_unused]] __fi static u16 GetBufferU16(const u8* buffer, u32 offset) { return GetBufferT<u16>(buffer, offset); }
[[maybe_unused]] __fi static u32 GetBufferU32(const u8* buffer, u32 offset) { return GetBufferT<u32>(buffer, offset); }
[[maybe_unused]] __fi static u64 GetBufferU64(const u8* buffer, u32 offset) { return GetBufferT<u64>(buffer, offset); }