Common: Merge MathUtils.h into BitUtils.h

This commit is contained in:
Stenzek 2023-07-22 14:12:59 +10:00 committed by Connor McLaughlin
parent 7dd1f7321a
commit 2b4c7d12b6
39 changed files with 85 additions and 148 deletions

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-
@ -14,8 +14,29 @@
*/
#pragma once
#include "common/Pcsx2Defs.h"
#include <bit>
#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
namespace Common
{
template <typename T>
@ -96,4 +117,18 @@ namespace Common
static_assert(Common::IsPow2(__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<u32>(n));
}
} // namespace Common

View File

@ -61,10 +61,10 @@ target_sources(common PRIVATE
# x86emitter headers
target_sources(common PRIVATE
Align.h
AlignedMalloc.h
Assertions.h
boost_spsc_queue.hpp
BitUtils.h
ByteSwap.h
Console.h
CrashHandler.h

View File

@ -25,7 +25,7 @@
#include "fmt/core.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Assertions.h"
#include "common/Console.h"
#include "common/General.h"

View File

@ -1,41 +0,0 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2014- 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-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 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.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// Hopefully this file will be used for cross-source math utilities.
// Currently these are strewn across the code base. Please collect them all!
#include "common/Pcsx2Defs.h"
// On GCC >= 4.7, this is equivalent to __builtin_clrsb(n);
inline u32 count_leading_sign_bits(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.
#ifdef _MSC_VER
unsigned long ret;
_BitScanReverse(&ret, n);
return 31 - (u32)ret;
#else
return __builtin_clz(n);
#endif
}

View File

@ -15,7 +15,7 @@
#if defined(_WIN32)
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/RedtapeWindows.h"
#include "common/Console.h"
#include "common/General.h"

View File

@ -103,7 +103,7 @@
<MASM Include="FastJmp.asm" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Align.h" />
<ClInclude Include="BitUtils.h" />
<ClInclude Include="AlignedMalloc.h" />
<ClInclude Include="ByteSwap.h" />
<ClInclude Include="CrashHandler.h" />
@ -134,7 +134,6 @@
<ClInclude Include="Assertions.h" />
<ClInclude Include="Console.h" />
<ClInclude Include="General.h" />
<ClInclude Include="MathUtils.h" />
<ClInclude Include="Path.h" />
<ClInclude Include="PrecompiledHeader.h" />
<ClInclude Include="ReadbackSpinManager.h" />

View File

@ -201,9 +201,6 @@
<ClInclude Include="emitter\legacy_types.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MathUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Path.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -270,7 +267,7 @@
<ClInclude Include="SettingsWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Align.h">
<ClInclude Include="BitUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WindowInfo.h">

View File

@ -27,8 +27,7 @@
#include "fmt/format.h"
// _BitScanForward()
#include "pcsx2/GS/GSIntrin.h"
#include <bit>
InputBindingDialog::InputBindingDialog(SettingsInterface* sif, InputBindingInfo::Type bind_type, std::string section_name,
std::string key_name, std::vector<std::string> bindings, QWidget* parent)

View File

@ -549,7 +549,6 @@ set(pcsx2GSHeaders
GS/GSDump.h
GS/GSExtra.h
GS/GSGL.h
GS/GSIntrin.h
GS/GSRegs.h
GS/GS.h
GS/GSLocalMemory.h

View File

@ -25,7 +25,7 @@
#include "Host.h"
#include "IconsFontAwesome5.h"
#include "common/Assertions.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/DynamicLibrary.h"
#include "common/Path.h"
#include "common/StringUtil.h"

View File

@ -17,7 +17,7 @@
#include "GS/GSVector.h"
#include "pcsx2/Config.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include <utility>

View File

@ -1,55 +0,0 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 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-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 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.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <xmmintrin.h>
#include <emmintrin.h>
#include <tmmintrin.h>
#include <smmintrin.h>
#if _M_SSE >= 0x500
#include <immintrin.h>
#endif
#if !defined(_MSC_VER)
// http://svn.reactos.org/svn/reactos/trunk/reactos/include/crt/mingw32/intrin_x86.h?view=markup
static inline int _BitScanForward(unsigned long* const Index, const unsigned long Mask)
{
if (Mask == 0)
return 0;
#if __has_builtin(__builtin_ctz)
*Index = __builtin_ctz(Mask);
#else
__asm__("bsfl %k[Mask], %k[Index]" : [Index] "=r" (*Index) : [Mask] "mr" (Mask) : "cc");
#endif
return 1;
}
static inline int _BitScanReverse(unsigned long* const Index, const unsigned long Mask)
{
if (Mask == 0)
return 0;
#if __has_builtin(__builtin_clz)
*Index = 31 - __builtin_clz(Mask);
#else
__asm__("bsrl %k[Mask], %k[Index]" : [Index] "=r" (*Index) : [Mask] "mr" (Mask) : "cc");
#endif
return 1;
}
#endif

View File

@ -19,6 +19,7 @@
#include "GS/GSGL.h"
#include "GS/GSPerfMon.h"
#include "GS/GSUtil.h"
#include "common/BitUtils.h"
#include "common/Path.h"
#include "common/StringUtil.h"

View File

@ -13,12 +13,20 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "GSIntrin.h"
#include <cstring>
#pragma once
#include "PrecompiledHeader.h"
#include <xmmintrin.h>
#include <emmintrin.h>
#include <tmmintrin.h>
#include <smmintrin.h>
#if _M_SSE >= 0x500
#include <immintrin.h>
#endif
#include <cstring>
#ifdef _WIN32
#define gsforceinline __forceinline
#else

View File

@ -20,7 +20,7 @@
#include "GS/GS.h"
#include "Host.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include "imgui.h"

View File

@ -17,7 +17,7 @@
#include "GS/Renderers/Common/GSTexture.h"
#include "GS/Renderers/Common/GSDevice.h"
#include "GS/GSPng.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include <bitset>

View File

@ -22,7 +22,7 @@
#include "GS/GSUtil.h"
#include "Host.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Path.h"
#include "common/StringUtil.h"

View File

@ -18,7 +18,7 @@
#include "GSTexture11.h"
#include "GS/GSPng.h"
#include "GS/GSPerfMon.h"
#include "common/Align.h"
#include "common/BitUtils.h"
GSTexture11::GSTexture11(wil::com_ptr_nothrow<ID3D11Texture2D> texture, const D3D11_TEXTURE2D_DESC& desc,
GSTexture::Type type, GSTexture::Format format)

View File

@ -18,7 +18,7 @@
#include "GS/Renderers/DX12/D3D12StreamBuffer.h"
#include "GS/Renderers/DX12/GSDevice12.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Assertions.h"
#include "common/Console.h"

View File

@ -26,7 +26,7 @@
#include "Host.h"
#include "ShaderCacheVersion.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/ScopedGuard.h"
#include "common/StringUtil.h"

View File

@ -22,7 +22,7 @@
#include "GS/GSGL.h"
#include "common/Assertions.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include "D3D12MemAlloc.h"

View File

@ -20,7 +20,7 @@
#include "GS/GSPerfMon.h"
#include "GS/GSUtil.h"
#include "Host.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
GSRendererHW::GSRendererHW()

View File

@ -19,11 +19,10 @@
#include "GSRendererHW.h"
#include "GS/GSState.h"
#include "GS/GSGL.h"
#include "GS/GSIntrin.h"
#include "GS/GSPerfMon.h"
#include "GS/GSUtil.h"
#include "GS/GSXXH.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/HashCombine.h"
#ifdef __APPLE__

View File

@ -15,7 +15,7 @@
#include "PrecompiledHeader.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/FileSystem.h"
#include "common/Path.h"
#include "common/StringUtil.h"

View File

@ -17,7 +17,7 @@
#include "GS/Renderers/Metal/GSTextureMTL.h"
#include "GS/Renderers/Metal/GSDeviceMTL.h"
#include "GS/GSPerfMon.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Console.h"
#ifdef __APPLE__

View File

@ -17,7 +17,7 @@
#include "GS/Renderers/OpenGL/GLStreamBuffer.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/AlignedMalloc.h"
#include "common/Assertions.h"

View File

@ -22,7 +22,7 @@
#include "GS/GSPerfMon.h"
#include "GS/GSPng.h"
#include "GS/GSGL.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/AlignedMalloc.h"
#include "common/StringUtil.h"

View File

@ -26,7 +26,7 @@
#include "Host.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Path.h"
#include "common/ScopedGuard.h"

View File

@ -21,7 +21,7 @@
#include "GS/GSPerfMon.h"
#include "GS/GSGL.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Assertions.h"
static constexpr const VkComponentMapping s_identity_swizzle{VK_COMPONENT_SWIZZLE_IDENTITY,

View File

@ -19,7 +19,7 @@
#include "GS/Renderers/Vulkan/VKStreamBuffer.h"
#include "GS/Renderers/Vulkan/VKBuilders.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Assertions.h"
#include "common/Console.h"

View File

@ -35,7 +35,7 @@
#include "VMManager.h"
#include "pcsx2/Recording/InputRecording.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/StringUtil.h"
#include "common/Timer.h"

View File

@ -21,7 +21,7 @@
#include "R3000A.h"
#include "IopMem.h"
#include "common/MathUtils.h"
#include "common/BitUtils.h"
#ifdef GTE_DUMP
#define G_OP(name,delay) fprintf(gteLog, "* : %08X : %02d : %s\n", psxRegs.code, delay, name);
#define G_SD(reg) fprintf(gteLog, "+D%02d : %08X\n", reg, psxRegs.CP2D.r[reg]);
@ -208,7 +208,7 @@ __inline void MTC2(unsigned long value, int reg) {
case 30:
psxRegs.CP2D.r[30] = value;
psxRegs.CP2D.r[31] = count_leading_sign_bits(value);
psxRegs.CP2D.r[31] = Common::CountLeadingSignBits(value);
break;
default:

View File

@ -16,7 +16,7 @@
#include "PrecompiledHeader.h"
#include "Common.h"
#include "common/MathUtils.h"
#include "common/BitUtils.h"
namespace R5900 {
namespace Interpreter {
@ -151,8 +151,8 @@ void PLZCW() {
return;
// Return the leading sign bits, excluding the original bit
cpuRegs.GPR.r[_Rd_].UL[0] = count_leading_sign_bits(cpuRegs.GPR.r[_Rs_].SL[0]) - 1;
cpuRegs.GPR.r[_Rd_].UL[1] = count_leading_sign_bits(cpuRegs.GPR.r[_Rs_].SL[1]) - 1;
cpuRegs.GPR.r[_Rd_].UL[0] = Common::CountLeadingSignBits(cpuRegs.GPR.r[_Rs_].SL[0]) - 1;
cpuRegs.GPR.r[_Rd_].UL[1] = Common::CountLeadingSignBits(cpuRegs.GPR.r[_Rs_].SL[1]) - 1;
}
__fi void PMFHL_CLAMP(u16& dst, s32 src)

View File

@ -28,7 +28,7 @@
#include "SysForwardDefs.h"
#include "x86/newVif.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Perf.h"
#include "common/StringUtil.h"

View File

@ -17,7 +17,7 @@
#include "VirtualMemory.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "common/Console.h"
#include "common/Perf.h"

View File

@ -610,7 +610,6 @@
<ClInclude Include="GS\GS.h" />
<ClInclude Include="GS\GSExtra.h" />
<ClInclude Include="GS\GSGL.h" />
<ClInclude Include="GS\GSIntrin.h" />
<ClInclude Include="GS\GSRegs.h" />
<ClInclude Include="GS\GSAlignedClass.h" />
<ClInclude Include="GS\GSBlock.h" />

View File

@ -1880,9 +1880,6 @@
<ClInclude Include="GS\GSExtra.h">
<Filter>System\Ps2\GS</Filter>
</ClInclude>
<ClInclude Include="GS\GSIntrin.h">
<Filter>System\Ps2\GS</Filter>
</ClInclude>
<ClInclude Include="GS\GSGL.h">
<Filter>System\Ps2\GS</Filter>
</ClInclude>

View File

@ -38,7 +38,7 @@
#include "Host.h"
#include "VMManager.h"
#include "common/Align.h"
#include "common/BitUtils.h"
#include "fmt/core.h"

View File

@ -25,7 +25,7 @@
#include "R5900OpcodeTables.h"
#include "iR5900.h"
#include "iMMI.h"
#include "common/MathUtils.h"
#include "common/BitUtils.h"
using namespace x86Emitter;
@ -73,8 +73,8 @@ void recPLZCW()
GPR_SET_CONST(_Rd_);
// Return the leading sign bits, excluding the original bit
g_cpuConstRegs[_Rd_].UL[0] = count_leading_sign_bits(g_cpuConstRegs[_Rs_].SL[0]) - 1;
g_cpuConstRegs[_Rd_].UL[1] = count_leading_sign_bits(g_cpuConstRegs[_Rs_].SL[1]) - 1;
g_cpuConstRegs[_Rd_].UL[0] = Common::CountLeadingSignBits(g_cpuConstRegs[_Rs_].SL[0]) - 1;
g_cpuConstRegs[_Rd_].UL[1] = Common::CountLeadingSignBits(g_cpuConstRegs[_Rs_].SL[1]) - 1;
return;
}