diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 8062e9e85e..171895b94b 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2406,7 +2406,7 @@ void thread_ctrl::set_thread_affinity_mask(u64 mask) SetThreadAffinityMask(_this_thread, mask); #elif __APPLE__ // Supports only one core - thread_affinity_policy_data_t policy = { static_cast(utils::cnttz64(mask)) }; + thread_affinity_policy_data_t policy = { static_cast(std::countr_zero(mask)) }; thread_port_t mach_thread = pthread_mach_thread_np(pthread_self()); thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, reinterpret_cast(&policy), 1); #elif defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) diff --git a/Utilities/asm.h b/Utilities/asm.h index a8daa32694..6a0148681d 100644 --- a/Utilities/asm.h +++ b/Utilities/asm.h @@ -28,30 +28,6 @@ namespace utils #endif } - inline u32 cnttz32(u32 arg, bool nonzero = false) - { -#ifdef _MSC_VER - ulong res; - return _BitScanForward(&res, arg) || nonzero ? res : 32; -#elif __BMI__ - return _tzcnt_u32(arg); -#else - return arg || nonzero ? __builtin_ctz(arg) : 32; -#endif - } - - inline u64 cnttz64(u64 arg, bool nonzero = false) - { -#ifdef _MSC_VER - ulong res; - return _BitScanForward64(&res, arg) || nonzero ? res : 64; -#elif __BMI__ - return _tzcnt_u64(arg); -#else - return arg || nonzero ? __builtin_ctzll(arg) : 64; -#endif - } - inline u8 popcnt32(u32 arg) { #ifdef _MSC_VER diff --git a/Utilities/types.h b/Utilities/types.h index 36ccc607a9..0c75b6e9d8 100644 --- a/Utilities/types.h +++ b/Utilities/types.h @@ -17,9 +17,12 @@ #include #include -#if __has_include() -#include +#ifdef _MSC_VER +#ifndef __cpp_lib_bitops +#define __cpp_lib_bitops #endif +#endif +#include #ifndef __has_builtin #define __has_builtin(x) 0 diff --git a/rpcs3/Emu/CPU/CPUThread.cpp b/rpcs3/Emu/CPU/CPUThread.cpp index b2871425e5..87173289a9 100644 --- a/rpcs3/Emu/CPU/CPUThread.cpp +++ b/rpcs3/Emu/CPU/CPUThread.cpp @@ -280,7 +280,7 @@ struct cpu_counter if (ok) [[likely]] { // Get actual slot number - array_slot = i * 64 + utils::cnttz64(~bits, false); + array_slot = i * 64 + std::countr_one(bits); break; } } @@ -314,7 +314,7 @@ void for_all_cpu(F&& func) noexcept { for (u64 bits = ctr->cpu_array_bits[i]; bits; bits &= bits - 1) { - const u64 index = i * 64 + utils::cnttz64(bits, true); + const u64 index = i * 64 + std::countr_zero(bits); if (cpu_thread* cpu = ctr->cpu_array[index].load()) { diff --git a/rpcs3/Emu/Cell/PPUAnalyser.cpp b/rpcs3/Emu/Cell/PPUAnalyser.cpp index 5c57f380ae..259d3aa723 100644 --- a/rpcs3/Emu/Cell/PPUAnalyser.cpp +++ b/rpcs3/Emu/Cell/PPUAnalyser.cpp @@ -2144,7 +2144,7 @@ void ppu_acontext::MULLI(ppu_opcode_t op) } } - gpr[op.rd] = spec_gpr::range(min, max, gpr[op.ra].tz() + utils::cnttz64(op.simm16)); + gpr[op.rd] = spec_gpr::range(min, max, gpr[op.ra].tz() + std::countr_zero(op.simm16)); } void ppu_acontext::SUBFIC(ppu_opcode_t op) diff --git a/rpcs3/Emu/Cell/PPUAnalyser.h b/rpcs3/Emu/Cell/PPUAnalyser.h index b83a1e0b79..006b46f50e 100644 --- a/rpcs3/Emu/Cell/PPUAnalyser.h +++ b/rpcs3/Emu/Cell/PPUAnalyser.h @@ -989,7 +989,7 @@ struct ppu_acontext // Return number of trailing zero bits u64 tz() const { - return utils::cnttz64(mask()); + return std::countr_zero(mask()); } // Range NOT diff --git a/rpcs3/Emu/RSX/Overlays/overlays.cpp b/rpcs3/Emu/RSX/Overlays/overlays.cpp index 66dcb6ca9c..96b7978973 100644 --- a/rpcs3/Emu/RSX/Overlays/overlays.cpp +++ b/rpcs3/Emu/RSX/Overlays/overlays.cpp @@ -32,7 +32,7 @@ namespace rsx return 0; } - const u64 r = u64{1} << utils::cnttz64(~_old, false); + const u64 r = u64{1} << std::countr_one(_old); ::overlays.trace("Bit allocated (%u)", r); return r; } diff --git a/rpcs3/Emu/RSX/VK/VKGSRender.cpp b/rpcs3/Emu/RSX/VK/VKGSRender.cpp index e7d8aa31e9..1bcb88f5e9 100644 --- a/rpcs3/Emu/RSX/VK/VKGSRender.cpp +++ b/rpcs3/Emu/RSX/VK/VKGSRender.cpp @@ -819,7 +819,7 @@ void VKGSRender::check_heap_status(u32 flags) else if (flags) { heap_critical = false; - u32 test = 1 << utils::cnttz32(flags, true); + u32 test = 1u << std::countr_zero(flags); do { diff --git a/rpcs3/util/atomic.cpp b/rpcs3/util/atomic.cpp index f0f8fdee74..0498911811 100644 --- a/rpcs3/util/atomic.cpp +++ b/rpcs3/util/atomic.cpp @@ -107,7 +107,7 @@ static u64 slot_alloc() if (ok) { // Find lowest clear bit - return group * 64 + utils::cnttz64(~bits, false); + return group * 64 + std::countr_one(bits); } } @@ -269,7 +269,7 @@ static u32 sema_alloc() if (ok) { // Find lowest clear bit - const u32 id = group * 64 + static_cast(utils::cnttz64(~bits, false)); + const u32 id = group * 64 + static_cast(std::countr_one(bits)); #ifdef USE_POSIX // Initialize semaphore (should be very fast) diff --git a/rpcs3/util/atomic2.cpp b/rpcs3/util/atomic2.cpp index af3a8297dd..fdec8df1df 100644 --- a/rpcs3/util/atomic2.cpp +++ b/rpcs3/util/atomic2.cpp @@ -1,6 +1,5 @@ #include "atomic2.hpp" #include "Utilities/JIT.h" -#include "Utilities/asm.h" #include "Utilities/sysinfo.h" // @@ -265,7 +264,7 @@ static u64 rec_alloc() if (ok) { // Find lowest clear bit - return group * 64 + utils::cnttz64(~bits, false); + return group * 64 + std::countr_one(bits); } }