Misc: Use std::bit_ceil()/std::has_single_bit()

This commit is contained in:
Stenzek 2023-07-22 14:19:30 +10:00 committed by Connor McLaughlin
parent 2b4c7d12b6
commit 9191e8ce91
5 changed files with 12 additions and 45 deletions

View File

@ -75,46 +75,9 @@ namespace Common
return value & static_cast<T>(~static_cast<T>(alignment - 1));
}
template <typename T>
static constexpr __fi bool IsPow2(T value)
{
return (value & (value - 1)) == 0;
}
template <typename T>
static constexpr __fi T PreviousPow2(T value)
{
if (value == static_cast<T>(0))
return 0;
value |= (value >> 1);
value |= (value >> 2);
value |= (value >> 4);
value |= (value >> 8);
value |= (value >> 16);
return value - (value >> 1);
}
template<typename T>
static constexpr __fi T NextPow2(T value)
{
if (value == static_cast<T>(0))
return 0;
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value++;
return value;
}
template <typename T>
static constexpr T PageAlign(T size)
{
static_assert(Common::IsPow2(__pagesize), "Page size is a power of 2");
return Common::AlignUpPow2(size, __pagesize);
}

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -19,6 +19,7 @@
#include "GS/GSPng.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include <bit>
#include <bitset>
GSTexture::GSTexture() = default;
@ -164,7 +165,7 @@ u32 GSDownloadTexture::GetBufferSize(u32 width, u32 height, GSTexture::Format fo
const u32 bw = (width + (block_size - 1)) / block_size;
const u32 bh = (height + (block_size - 1)) / block_size;
pxAssert(Common::IsPow2(pitch_align));
pxAssert(std::has_single_bit(pitch_align));
const u32 pitch = Common::AlignUpPow2(bw * bytes_per_block, pitch_align);
return (pitch * bh);
}
@ -175,7 +176,7 @@ u32 GSDownloadTexture::GetTransferPitch(u32 width, u32 pitch_align) const
const u32 bytes_per_block = GSTexture::GetCompressedBytesPerBlock(m_format);
const u32 bw = (width + (block_size - 1)) / block_size;
pxAssert(Common::IsPow2(pitch_align));
pxAssert(std::has_single_bit(pitch_align));
return Common::AlignUpPow2(bw * bytes_per_block, pitch_align);
}

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -28,6 +28,7 @@
#include "imgui.h"
#include <bit>
#include <fstream>
#include <sstream>
#include <VersionHelpers.h>
@ -1724,7 +1725,7 @@ void GSDevice11::RenderImGui()
// This mess is because the vertex size isn't the same...
u32 vertex_offset;
{
static_assert(Common::IsPow2(sizeof(GSVertexPT1)));
static_assert(std::has_single_bit(sizeof(GSVertexPT1)));
D3D11_MAP type = D3D11_MAP_WRITE_NO_OVERWRITE;

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@ -22,6 +22,7 @@
#include "Host.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include <bit>
GSRendererHW::GSRendererHW()
: GSRenderer()
@ -4455,7 +4456,7 @@ bool GSRendererHW::CanUseTexIsFB(const GSTextureCache::Target* rt, const GSTextu
}
else if (clamp == CLAMP_REGION_REPEAT)
{
const u32 req_tbits = (tmax > 1) ? static_cast<u32>(Common::NextPow2(tmax - 1) - 1) : 0x1;
const u32 req_tbits = (tmax > 1) ? std::bit_ceil(static_cast<u32>(tmax - 1) - 1) : 0x1;
if ((min & req_tbits) != req_tbits)
{
GL_CACHE("Can't use tex-is-fb because of REGION_REPEAT [%d, %d] with TMM of [%d, %d] and tbits of %d",

View File

@ -32,6 +32,7 @@
#include "imgui.h"
#include <bit>
#include <mutex>
#include <sstream>
#include <limits>
@ -2548,7 +2549,7 @@ bool GSDeviceVK::CreateDeviceAndSwapChain()
std::max(m_device_properties.limits.minTexelBufferOffsetAlignment, static_cast<VkDeviceSize>(32));
m_device_properties.limits.optimalBufferCopyOffsetAlignment =
std::max(m_device_properties.limits.optimalBufferCopyOffsetAlignment, static_cast<VkDeviceSize>(32));
m_device_properties.limits.optimalBufferCopyRowPitchAlignment = Common::NextPow2(
m_device_properties.limits.optimalBufferCopyRowPitchAlignment = std::bit_ceil(
std::max(m_device_properties.limits.optimalBufferCopyRowPitchAlignment, static_cast<VkDeviceSize>(32)));
m_device_properties.limits.bufferImageGranularity =
std::max(m_device_properties.limits.bufferImageGranularity, static_cast<VkDeviceSize>(32));