From 784a21692763296cbd93f59c4cf8a58c902b81e8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 15 Apr 2023 03:27:35 -0400 Subject: [PATCH] Common/MathUtil: Move IntLog2 into MathUtil namespace Gets this out of the global namespace. --- Source/Core/Common/Arm64Emitter.cpp | 2 +- Source/Core/Common/MathUtil.h | 3 +-- Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp | 4 ++-- .../Core/PowerPC/Jit64/Jit_SystemRegisters.cpp | 4 ++-- .../Core/Core/PowerPC/Jit64Common/EmuCodeBlock.cpp | 2 +- Source/Core/Core/PowerPC/JitArm64/Jit.cpp | 6 +++--- .../Core/PowerPC/JitArm64/JitArm64_Integer.cpp | 12 ++++++------ .../Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp | 4 ++-- .../PowerPC/JitArm64/JitArm64_SystemRegisters.cpp | 4 ++-- Source/Core/UICommon/UICommon.cpp | 2 +- Source/Core/VideoBackends/OGL/OGLStreamBuffer.cpp | 2 +- .../Core/VideoCommon/TextureConversionShader.cpp | 11 ++++++----- Source/Core/VideoCommon/TextureInfo.cpp | 2 +- Source/UnitTests/Common/MathUtilTest.cpp | 14 +++++++------- 14 files changed, 36 insertions(+), 36 deletions(-) diff --git a/Source/Core/Common/Arm64Emitter.cpp b/Source/Core/Common/Arm64Emitter.cpp index d4293971b0..d59fd13300 100644 --- a/Source/Core/Common/Arm64Emitter.cpp +++ b/Source/Core/Common/Arm64Emitter.cpp @@ -3092,7 +3092,7 @@ void ARM64FloatEmitter::FMOV(ARM64Reg Rd, uint8_t imm8) // Vector void ARM64FloatEmitter::ADD(u8 size, ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm) { - EmitThreeSame(0, IntLog2(size) - 3, 0b10000, Rd, Rn, Rm); + EmitThreeSame(0, MathUtil::IntLog2(size) - 3, 0b10000, Rd, Rn, Rm); } void ARM64FloatEmitter::AND(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm) { diff --git a/Source/Core/Common/MathUtil.h b/Source/Core/Common/MathUtil.h index 8656c4313a..1494a894d9 100644 --- a/Source/Core/Common/MathUtil.h +++ b/Source/Core/Common/MathUtil.h @@ -185,10 +185,9 @@ private: T m_variance{}; }; -} // namespace MathUtil - // Rounds down. 0 -> undefined constexpr int IntLog2(u64 val) { return 63 - std::countl_zero(val); } +} // namespace MathUtil diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp index 19c2890c9e..fb8e8fbaef 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp @@ -1223,7 +1223,7 @@ void Jit64::MultiplyImmediate(u32 imm, int a, int d, bool overflow) // power of 2; just a shift if (MathUtil::IsPow2(imm)) { - u32 shift = IntLog2(imm); + u32 shift = MathUtil::IntLog2(imm); // use LEA if it saves an op if (d != a && shift <= 3 && shift >= 1 && Ra.IsSimpleReg()) { @@ -1731,7 +1731,7 @@ void Jit64::divwx(UGeckoInstruction inst) TEST(32, R(dividend), R(dividend)); LEA(32, sum, MDisp(dividend, abs_val - 1)); CMOVcc(32, Rd, R(src), cond); - SAR(32, Rd, Imm8(IntLog2(abs_val))); + SAR(32, Rd, Imm8(MathUtil::IntLog2(abs_val))); if (divisor < 0) NEG(32, Rd); diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_SystemRegisters.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_SystemRegisters.cpp index 84692f0068..8fc44b3041 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_SystemRegisters.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_SystemRegisters.cpp @@ -198,7 +198,7 @@ void Jit64::UpdateFPExceptionSummary(X64Reg fpscr, X64Reg tmp1, X64Reg tmp2) // fpscr.VX = (fpscr & FPSCR_VX_ANY) != 0 TEST(32, R(fpscr), Imm32(FPSCR_VX_ANY)); SETcc(CC_NZ, R(tmp1)); - SHL(32, R(tmp1), Imm8(IntLog2(FPSCR_VX))); + SHL(32, R(tmp1), Imm8(MathUtil::IntLog2(FPSCR_VX))); AND(32, R(fpscr), Imm32(~(FPSCR_VX | FPSCR_FEX))); OR(32, R(fpscr), R(tmp1)); @@ -212,7 +212,7 @@ void Jit64::UpdateFPExceptionSummary(X64Reg fpscr, X64Reg tmp1, X64Reg tmp2) // the TEST, and we can't use XOR right after the TEST since that would overwrite flags. However, // there is no false dependency, since SETcc depends on TEST's flags and TEST depends on tmp1. SETcc(CC_NZ, R(tmp1)); - SHL(32, R(tmp1), Imm8(IntLog2(FPSCR_FEX))); + SHL(32, R(tmp1), Imm8(MathUtil::IntLog2(FPSCR_FEX))); OR(32, R(fpscr), R(tmp1)); } diff --git a/Source/Core/Core/PowerPC/Jit64Common/EmuCodeBlock.cpp b/Source/Core/Core/PowerPC/Jit64Common/EmuCodeBlock.cpp index 006e3d6227..3871235493 100644 --- a/Source/Core/Core/PowerPC/Jit64Common/EmuCodeBlock.cpp +++ b/Source/Core/Core/PowerPC/Jit64Common/EmuCodeBlock.cpp @@ -97,7 +97,7 @@ FixupBranch EmuCodeBlock::BATAddressLookup(X64Reg addr, X64Reg tmp, const void* MOV(64, R(tmp), ImmPtr(bat_table)); SHR(32, R(addr), Imm8(PowerPC::BAT_INDEX_SHIFT)); MOV(32, R(addr), MComplex(tmp, addr, SCALE_4, 0)); - BT(32, R(addr), Imm8(IntLog2(PowerPC::BAT_MAPPED_BIT))); + BT(32, R(addr), Imm8(MathUtil::IntLog2(PowerPC::BAT_MAPPED_BIT))); return J_CC(CC_NC, m_far_code.Enabled()); } diff --git a/Source/Core/Core/PowerPC/JitArm64/Jit.cpp b/Source/Core/Core/PowerPC/JitArm64/Jit.cpp index ef0dd426ab..4f1c81316f 100644 --- a/Source/Core/Core/PowerPC/JitArm64/Jit.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/Jit.cpp @@ -544,7 +544,7 @@ void JitArm64::WriteConditionalExceptionExit(int exception, ARM64Reg temp_gpr, A u64 increment_sp_on_exit) { LDR(IndexType::Unsigned, temp_gpr, PPC_REG, PPCSTATE_OFF(Exceptions)); - FixupBranch no_exception = TBZ(temp_gpr, IntLog2(exception)); + FixupBranch no_exception = TBZ(temp_gpr, MathUtil::IntLog2(exception)); const bool switch_to_far_code = !IsInFarCode(); @@ -938,7 +938,7 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) // Inline exception check LDR(IndexType::Unsigned, ARM64Reg::W30, PPC_REG, PPCSTATE_OFF(Exceptions)); - FixupBranch no_ext_exception = TBZ(ARM64Reg::W30, IntLog2(EXCEPTION_EXTERNAL_INT)); + FixupBranch no_ext_exception = TBZ(ARM64Reg::W30, MathUtil::IntLog2(EXCEPTION_EXTERNAL_INT)); FixupBranch exception = B(); SwitchToFarCode(); const u8* done_here = GetCodePtr(); @@ -974,7 +974,7 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) ARM64Reg XA = EncodeRegTo64(WA); LDR(IndexType::Unsigned, WA, PPC_REG, PPCSTATE_OFF(Exceptions)); - FixupBranch no_ext_exception = TBZ(WA, IntLog2(EXCEPTION_EXTERNAL_INT)); + FixupBranch no_ext_exception = TBZ(WA, MathUtil::IntLog2(EXCEPTION_EXTERNAL_INT)); FixupBranch exception = B(); SwitchToFarCode(); const u8* done_here = GetCodePtr(); diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp index a9a0f540bb..1accdff380 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp @@ -909,7 +909,7 @@ bool JitArm64::MultiplyImmediate(u32 imm, int a, int d, bool rc) else if (MathUtil::IsPow2(imm)) { // Multiplication by a power of two (2^n). - const int shift = IntLog2(imm); + const int shift = MathUtil::IntLog2(imm); gpr.BindToRegister(d, d == a); LSL(gpr.R(d), gpr.R(a), shift); @@ -919,7 +919,7 @@ bool JitArm64::MultiplyImmediate(u32 imm, int a, int d, bool rc) else if (MathUtil::IsPow2(imm - 1)) { // Multiplication by a power of two plus one (2^n + 1). - const int shift = IntLog2(imm - 1); + const int shift = MathUtil::IntLog2(imm - 1); gpr.BindToRegister(d, d == a); ADD(gpr.R(d), gpr.R(a), gpr.R(a), ArithOption(gpr.R(a), ShiftType::LSL, shift)); @@ -929,7 +929,7 @@ bool JitArm64::MultiplyImmediate(u32 imm, int a, int d, bool rc) else if (MathUtil::IsPow2(~imm + 1)) { // Multiplication by a negative power of two (-(2^n)). - const int shift = IntLog2(~imm + 1); + const int shift = MathUtil::IntLog2(~imm + 1); gpr.BindToRegister(d, d == a); NEG(gpr.R(d), gpr.R(a), ArithOption(gpr.R(a), ShiftType::LSL, shift)); @@ -939,7 +939,7 @@ bool JitArm64::MultiplyImmediate(u32 imm, int a, int d, bool rc) else if (MathUtil::IsPow2(~imm + 2)) { // Multiplication by a negative power of two plus one (-(2^n) + 1). - const int shift = IntLog2(~imm + 2); + const int shift = MathUtil::IntLog2(~imm + 2); gpr.BindToRegister(d, d == a); SUB(gpr.R(d), gpr.R(a), gpr.R(a), ArithOption(gpr.R(a), ShiftType::LSL, shift)); @@ -1603,9 +1603,9 @@ void JitArm64::divwx(UGeckoInstruction inst) CSEL(WA, RA, WA, CCFlags::CC_PL); if (divisor < 0) - NEG(RD, WA, ArithOption(WA, ShiftType::ASR, IntLog2(abs_val))); + NEG(RD, WA, ArithOption(WA, ShiftType::ASR, MathUtil::IntLog2(abs_val))); else - ASR(RD, WA, IntLog2(abs_val)); + ASR(RD, WA, MathUtil::IntLog2(abs_val)); if (allocate_reg) gpr.Unlock(WA); diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp index 5c4a1b1d56..e353e87731 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_LoadStore.cpp @@ -347,7 +347,7 @@ FixupBranch JitArm64::BATAddressLookup(ARM64Reg addr_out, ARM64Reg addr_in, ARM6 MOVP2R(tmp, bat_table); LSR(addr_out, addr_in, PowerPC::BAT_INDEX_SHIFT); LDR(addr_out, tmp, ArithOption(addr_out, true)); - FixupBranch pass = TBNZ(addr_out, IntLog2(PowerPC::BAT_MAPPED_BIT)); + FixupBranch pass = TBNZ(addr_out, MathUtil::IntLog2(PowerPC::BAT_MAPPED_BIT)); FixupBranch fail = B(); SetJumpTarget(pass); return fail; @@ -361,7 +361,7 @@ FixupBranch JitArm64::CheckIfSafeAddress(Arm64Gen::ARM64Reg addr, Arm64Gen::ARM6 MOVP2R(tmp2, m_mmu.GetDBATTable().data()); LSR(tmp1, addr, PowerPC::BAT_INDEX_SHIFT); LDR(tmp1, tmp2, ArithOption(tmp1, true)); - FixupBranch pass = TBNZ(tmp1, IntLog2(PowerPC::BAT_PHYSICAL_BIT)); + FixupBranch pass = TBNZ(tmp1, MathUtil::IntLog2(PowerPC::BAT_PHYSICAL_BIT)); FixupBranch fail = B(); SetJumpTarget(pass); return fail; diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_SystemRegisters.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_SystemRegisters.cpp index f01a0f0cf9..6093ae2481 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_SystemRegisters.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_SystemRegisters.cpp @@ -60,13 +60,13 @@ void JitArm64::UpdateFPExceptionSummary(ARM64Reg fpscr) MOVI2R(WA, FPSCR_VX_ANY); TST(WA, fpscr); CSET(WA, CCFlags::CC_NEQ); - BFI(fpscr, WA, IntLog2(FPSCR_VX), 1); + BFI(fpscr, WA, MathUtil::IntLog2(FPSCR_VX), 1); // fpscr.FEX = ((fpscr >> 22) & (fpscr & FPSCR_ANY_E)) != 0 AND(WA, fpscr, LogicalImm(FPSCR_ANY_E, 32)); TST(WA, fpscr, ArithOption(fpscr, ShiftType::LSR, 22)); CSET(WA, CCFlags::CC_NEQ); - BFI(fpscr, WA, IntLog2(FPSCR_FEX), 1); + BFI(fpscr, WA, MathUtil::IntLog2(FPSCR_FEX), 1); gpr.Unlock(WA); } diff --git a/Source/Core/UICommon/UICommon.cpp b/Source/Core/UICommon/UICommon.cpp index 6b345e3b81..464646eb17 100644 --- a/Source/Core/UICommon/UICommon.cpp +++ b/Source/Core/UICommon/UICommon.cpp @@ -547,7 +547,7 @@ std::string FormatSize(u64 bytes, int decimals) // div 10 to get largest named unit less than size // 10 == log2(1024) (number of B in a KiB, KiB in a MiB, etc) // Max value is 63 / 10 = 6 - const int unit = IntLog2(std::max(bytes, 1)) / 10; + const int unit = MathUtil::IntLog2(std::max(bytes, 1)) / 10; // Don't need exact values, only 5 most significant digits const double unit_size = std::pow(2, unit * 10); diff --git a/Source/Core/VideoBackends/OGL/OGLStreamBuffer.cpp b/Source/Core/VideoBackends/OGL/OGLStreamBuffer.cpp index b4f26db453..1d5deadfcf 100644 --- a/Source/Core/VideoBackends/OGL/OGLStreamBuffer.cpp +++ b/Source/Core/VideoBackends/OGL/OGLStreamBuffer.cpp @@ -25,7 +25,7 @@ static u32 GenBuffer() StreamBuffer::StreamBuffer(u32 type, u32 size) : m_buffer(GenBuffer()), m_buffertype(type), m_size(MathUtil::NextPowerOf2(size)), - m_bit_per_slot(IntLog2(MathUtil::NextPowerOf2(size) / SYNC_POINTS)) + m_bit_per_slot(MathUtil::IntLog2(MathUtil::NextPowerOf2(size) / SYNC_POINTS)) { m_iterator = 0; m_used_iterator = 0; diff --git a/Source/Core/VideoCommon/TextureConversionShader.cpp b/Source/Core/VideoCommon/TextureConversionShader.cpp index c9242de6e5..6962daa29b 100644 --- a/Source/Core/VideoCommon/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/TextureConversionShader.cpp @@ -202,9 +202,9 @@ static void WriteSwizzler(ShaderCode& code, const EFBCopyParams& params, APIType const int blkH = TexDecoder_GetEFBCopyBlockHeightInTexels(params.copy_format); int samples = GetEncodedSampleCount(params.copy_format); - code.Write(" int x_block_position = (uv1.x >> {}) << {};\n", IntLog2(blkH * blkW / samples), - IntLog2(blkW)); - code.Write(" int y_block_position = uv1.y << {};\n", IntLog2(blkH)); + code.Write(" int x_block_position = (uv1.x >> {}) << {};\n", + MathUtil::IntLog2(blkH * blkW / samples), MathUtil::IntLog2(blkW)); + code.Write(" int y_block_position = uv1.y << {};\n", MathUtil::IntLog2(blkH)); if (samples == 1) { // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8. @@ -212,9 +212,10 @@ static void WriteSwizzler(ShaderCode& code, const EFBCopyParams& params, APIType samples = 2; } code.Write(" int offset_in_block = uv1.x & {};\n", (blkH * blkW / samples) - 1); - code.Write(" int y_offset_in_block = offset_in_block >> {};\n", IntLog2(blkW / samples)); + code.Write(" int y_offset_in_block = offset_in_block >> {};\n", + MathUtil::IntLog2(blkW / samples)); code.Write(" int x_offset_in_block = (offset_in_block & {}) << {};\n", (blkW / samples) - 1, - IntLog2(samples)); + MathUtil::IntLog2(samples)); code.Write(" sampleUv.x = x_block_position + x_offset_in_block;\n" " sampleUv.y = y_block_position + y_offset_in_block;\n"); diff --git a/Source/Core/VideoCommon/TextureInfo.cpp b/Source/Core/VideoCommon/TextureInfo.cpp index 35441d4d36..b73461ba33 100644 --- a/Source/Core/VideoCommon/TextureInfo.cpp +++ b/Source/Core/VideoCommon/TextureInfo.cpp @@ -83,7 +83,7 @@ TextureInfo::TextureInfo(u32 stage, const u8* ptr, const u8* tlut_ptr, u32 addre // e.g. 64x64 with 7 LODs would have the mipmap chain 64x64,32x32,16x16,8x8,4x4,2x2,1x1,0x0, so // we limit the mipmap count to 6 there const u32 limited_mip_count = - std::min(IntLog2(std::max(width, height)) + 1, raw_mip_count + 1) - 1; + std::min(MathUtil::IntLog2(std::max(width, height)) + 1, raw_mip_count + 1) - 1; // load mips const u8* src_data = m_ptr + GetTextureSize(); diff --git a/Source/UnitTests/Common/MathUtilTest.cpp b/Source/UnitTests/Common/MathUtilTest.cpp index 229acac7fe..78416772a9 100644 --- a/Source/UnitTests/Common/MathUtilTest.cpp +++ b/Source/UnitTests/Common/MathUtilTest.cpp @@ -7,15 +7,15 @@ TEST(MathUtil, IntLog2) { - EXPECT_EQ(0, IntLog2(1)); - EXPECT_EQ(1, IntLog2(2)); - EXPECT_EQ(2, IntLog2(4)); - EXPECT_EQ(3, IntLog2(8)); - EXPECT_EQ(63, IntLog2(0x8000000000000000ull)); + EXPECT_EQ(0, MathUtil::IntLog2(1)); + EXPECT_EQ(1, MathUtil::IntLog2(2)); + EXPECT_EQ(2, MathUtil::IntLog2(4)); + EXPECT_EQ(3, MathUtil::IntLog2(8)); + EXPECT_EQ(63, MathUtil::IntLog2(0x8000000000000000ull)); // Rounding behavior. - EXPECT_EQ(3, IntLog2(15)); - EXPECT_EQ(63, IntLog2(0xFFFFFFFFFFFFFFFFull)); + EXPECT_EQ(3, MathUtil::IntLog2(15)); + EXPECT_EQ(63, MathUtil::IntLog2(0xFFFFFFFFFFFFFFFFull)); } TEST(MathUtil, NextPowerOf2)