From c8b051628f68ca1bed1ec56d2988376fc80aea61 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 22 Dec 2023 19:27:44 +1000 Subject: [PATCH] Misc: Replace likely/unlikely with C++20 equivalents --- common/Misc.cpp | 2 +- common/Windows/WinThreads.cpp | 2 +- pcsx2/GS/GSLocalMemory.h | 2 +- pcsx2/GS/GSRingHeap.cpp | 6 ++--- pcsx2/GS/Renderers/HW/GSRendererHW.cpp | 6 ++--- pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm | 12 ++++----- pcsx2/GS/Renderers/SW/GSDrawScanline.cpp | 4 +-- pcsx2/GS/Renderers/SW/GSRasterizer.cpp | 4 +-- pcsx2/Host.cpp | 8 +++--- pcsx2/PINE.cpp | 34 ++++++++++++------------ pcsx2/PINE.h | 5 +--- pcsx2/R5900OpcodeImpl.cpp | 16 +++++------ pcsx2/x86/newVif_Dynarec.cpp | 6 ++--- 13 files changed, 51 insertions(+), 56 deletions(-) diff --git a/common/Misc.cpp b/common/Misc.cpp index c8263f7dca..e703d03a7c 100644 --- a/common/Misc.cpp +++ b/common/Misc.cpp @@ -69,7 +69,7 @@ __noinline static void UpdatePauseTime() u32 ShortSpin() { u32 inc = PAUSE_TIME; - if (unlikely(inc == 0)) + if (inc == 0) [[unlikely]] { UpdatePauseTime(); inc = PAUSE_TIME; diff --git a/common/Windows/WinThreads.cpp b/common/Windows/WinThreads.cpp index ccd8d851bc..1cfd4dbf45 100644 --- a/common/Windows/WinThreads.cpp +++ b/common/Windows/WinThreads.cpp @@ -216,7 +216,7 @@ u64 Threading::GetThreadTicksPerSecond() // On x86, despite what the MS documentation says, this basically appears to be rdtsc. // So, the frequency is our base clock speed (and stable regardless of power management). static u64 frequency = 0; - if (unlikely(frequency == 0)) + if (frequency == 0) [[unlikely]] { HKEY key; LSTATUS res = diff --git a/pcsx2/GS/GSLocalMemory.h b/pcsx2/GS/GSLocalMemory.h index 341801f298..5af8897547 100644 --- a/pcsx2/GS/GSLocalMemory.h +++ b/pcsx2/GS/GSLocalMemory.h @@ -312,7 +312,7 @@ public: int endOff = firstRowPgXEnd; int yCnt = this->yCnt; - if (unlikely(slowPath)) + if (slowPath) [[unlikely]] { u32 touched[MAX_PAGES / 32] = {}; for (int y = 0; y < yCnt; y++) diff --git a/pcsx2/GS/GSRingHeap.cpp b/pcsx2/GS/GSRingHeap.cpp index 684ff33dae..fb227775b3 100644 --- a/pcsx2/GS/GSRingHeap.cpp +++ b/pcsx2/GS/GSRingHeap.cpp @@ -118,7 +118,7 @@ struct GSRingHeap::Buffer /// Decrement the main amt_allocated refcount void decref(size_t amt) { - if (unlikely(m_amt_allocated.fetch_sub(amt, std::memory_order_release) == amt)) + if (m_amt_allocated.fetch_sub(amt, std::memory_order_release) == amt) [[unlikely]] { std::atomic_thread_fence(std::memory_order_acquire); _aligned_free(this); @@ -154,7 +154,7 @@ struct GSRingHeap::Buffer do { usage_mask |= 1ull << (cur_quadrant * 16); - if (unlikely(isStillInUse(cur_quadrant))) + if (isStillInUse(cur_quadrant)) [[unlikely]] return nullptr; } while (++cur_quadrant <= new_quadrant); } @@ -202,7 +202,7 @@ void* GSRingHeap::alloc_internal(size_t size, size_t align_mask, size_t prefix_s prefix_size += sizeof(Buffer*); // Add space for a pointer to the buffer size_t total_size = size + prefix_size; - if (likely(total_size <= (m_current_buffer->m_size / 2))) + if (total_size <= (m_current_buffer->m_size / 2)) [[likely]] { if (void* ptr = m_current_buffer->alloc(size, align_mask, prefix_size)) { diff --git a/pcsx2/GS/Renderers/HW/GSRendererHW.cpp b/pcsx2/GS/Renderers/HW/GSRendererHW.cpp index d22d2220cb..01fe65e3e1 100644 --- a/pcsx2/GS/Renderers/HW/GSRendererHW.cpp +++ b/pcsx2/GS/Renderers/HW/GSRendererHW.cpp @@ -2240,7 +2240,7 @@ void GSRendererHW::Draw() g_texture_cache->LookupSource(TEX0, env.TEXA, MIP_CLAMP, tmm.coverage, (GSConfig.HWMipmap >= HWMipmapLevel::Basic || GSConfig.TriFilter == TriFiltering::Forced) ? &hash_lod_range : nullptr, possible_shuffle, m_vt.IsLinear(), m_cached_ctx.FRAME.Block(), req_color, req_alpha); - if (unlikely(!src)) + if (!src) [[unlikely]] { GL_INS("ERROR: Source lookup failed, skipping."); CleanupDraw(true); @@ -2352,7 +2352,7 @@ void GSRendererHW::Draw() rt = g_texture_cache->CreateTarget(FRAME_TEX0, t_size, GetValidSize(src), target_scale, GSTextureCache::RenderTarget, true, fm, false, force_preload, preserve_rt_color, m_r, src); - if (unlikely(!rt)) + if (!rt) [[unlikely]] { GL_INS("ERROR: Failed to create FRAME target, skipping."); CleanupDraw(true); @@ -2376,7 +2376,7 @@ void GSRendererHW::Draw() { ds = g_texture_cache->CreateTarget(ZBUF_TEX0, t_size, GetValidSize(src), target_scale, GSTextureCache::DepthStencil, true, 0, false, force_preload, preserve_depth, m_r, src); - if (unlikely(!ds)) + if (!ds) [[unlikely]] { GL_INS("ERROR: Failed to create ZBUF target, skipping."); CleanupDraw(true); diff --git a/pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm b/pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm index 484b72d4b8..2fbe325e51 100644 --- a/pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm +++ b/pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm @@ -99,7 +99,7 @@ GSDeviceMTL::Map GSDeviceMTL::Allocate(UploadBuffer& buffer, size_t amt) amt = (amt + 31) & ~31ull; u64 last_draw = m_last_finished_draw.load(std::memory_order_acquire); bool needs_new = buffer.usage.PrepareForAllocation(last_draw, amt); - if (unlikely(needs_new)) + if (needs_new) [[unlikely]] { // Orphan buffer size_t newsize = std::max(buffer.usage.Size() * 2, 4096); @@ -140,7 +140,7 @@ GSDeviceMTL::Map GSDeviceMTL::Allocate(BufferPair& buffer, size_t amt) } buffer.last_upload = 0; } - if (unlikely(needs_new)) + if (needs_new) [[unlikely]] { // Orphan buffer size_t newsize = std::max(buffer.usage.Size() * 2, 4096); @@ -669,7 +669,7 @@ MRCOwned> GSDeviceMTL::LoadShader(NSString* name) { NSError* err = nil; MRCOwned> fn = MRCTransfer([m_dev.shaders newFunctionWithName:name constantValues:m_fn_constants error:&err]); - if (unlikely(err)) + if (err) [[unlikely]] { NSString* msg = [NSString stringWithFormat:@"Failed to load shader %@: %@", name, [err localizedDescription]]; Console.Error("%s", [msg UTF8String]); @@ -685,7 +685,7 @@ MRCOwned> GSDeviceMTL::MakePipeline(MTLRenderPipeline [desc setFragmentFunction:fragment]; NSError* err; MRCOwned> res = MRCTransfer([m_dev.dev newRenderPipelineStateWithDescriptor:desc error:&err]); - if (unlikely(err)) + if (err) [[unlikely]] { NSString* msg = [NSString stringWithFormat:@"Failed to create pipeline %@: %@", name, [err localizedDescription]]; Console.Error("%s", [msg UTF8String]); @@ -705,7 +705,7 @@ MRCOwned> GSDeviceMTL::MakeComputePipeline(id& da void GSSingleRasterizer::Draw(GSRasterizerData& data) { - if (!unlikely(m_ds.SetupDraw(data))) + if (!m_ds.SetupDraw(data)) [[unlikely]] { m_ds.ResetCodeCache(); m_ds.SetupDraw(data); @@ -1251,7 +1251,7 @@ void GSRasterizerList::Queue(const GSRingHeap::SharedPtr& data { GSVector4i r = data->bbox.rintersect(data->scissor); - if (unlikely(!m_ds.SetupDraw(*data.get()))) + if (!m_ds.SetupDraw(*data.get())) [[unlikely]] { Sync(); m_ds.ResetCodeCache(); diff --git a/pcsx2/Host.cpp b/pcsx2/Host.cpp index 621a352504..3023bcb873 100644 --- a/pcsx2/Host.cpp +++ b/pcsx2/Host.cpp @@ -62,7 +62,7 @@ std::pair Host::LookupTranslationString(const std::string_view s32 len; // Shouldn't happen, but just in case someone tries to translate an empty string. - if (unlikely(msg.empty())) + if (msg.empty()) [[unlikely]] { ret.first = &s_translation_string_cache[0]; ret.second = 0; @@ -72,11 +72,11 @@ std::pair Host::LookupTranslationString(const std::string_view s_translation_string_mutex.lock_shared(); ctx_it = s_translation_string_map.find(context); - if (unlikely(ctx_it == s_translation_string_map.end())) + if (ctx_it == s_translation_string_map.end()) [[unlikely]] goto add_string; msg_it = ctx_it->second.find(msg); - if (unlikely(msg_it == ctx_it->second.end())) + if (msg_it == ctx_it->second.end()) [[unlikely]] goto add_string; ret.first = &s_translation_string_cache[msg_it->second.first]; @@ -88,7 +88,7 @@ add_string: s_translation_string_mutex.unlock_shared(); s_translation_string_mutex.lock(); - if (unlikely(s_translation_string_cache.empty())) + if (s_translation_string_cache.empty()) [[unlikely]] { // First element is always an empty string. s_translation_string_cache.resize(TRANSLATION_STRING_CACHE_SIZE); diff --git a/pcsx2/PINE.cpp b/pcsx2/PINE.cpp index 4ddfb3bd4b..3f4a3af127 100644 --- a/pcsx2/PINE.cpp +++ b/pcsx2/PINE.cpp @@ -278,7 +278,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector buf, std::vector(buf, buf_cnt); const u8 res = memRead8(a); @@ -311,7 +311,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); const u16 res = memRead16(a); @@ -324,7 +324,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); const u32 res = memRead32(a); @@ -337,7 +337,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); const u64 res = memRead64(a); @@ -350,7 +350,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); memWrite8(a, FromSpan(buf, buf_cnt + 4)); @@ -361,7 +361,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); memWrite16(a, FromSpan(buf, buf_cnt + 4)); @@ -372,7 +372,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); memWrite32(a, FromSpan(buf, buf_cnt + 4)); @@ -383,7 +383,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt); memWrite64(a, FromSpan(buf, buf_cnt + 4)); @@ -406,7 +406,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector buf, std::vector(buf, buf_cnt)); buf_cnt += 1; @@ -428,7 +428,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector(buf, buf_cnt)); buf_cnt += 1; @@ -440,7 +440,7 @@ PINEServer::IPCBuffer PINEServer::ParseCommand(std::span buf, std::vector buf, std::vector buf, std::vector buf, std::vector buf, std::vector buf_size || + return ((command_len + command_size) > buf_size || (reply_len + reply_size) >= MAX_IPC_RETURN_SIZE); - if (unlikely(res)) - return false; - return true; } public: diff --git a/pcsx2/R5900OpcodeImpl.cpp b/pcsx2/R5900OpcodeImpl.cpp index 4dfd0e36d0..5e2eaffb03 100644 --- a/pcsx2/R5900OpcodeImpl.cpp +++ b/pcsx2/R5900OpcodeImpl.cpp @@ -554,7 +554,7 @@ void LH() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 1)) + if (addr & 1) [[unlikely]] RaiseAddressError(addr, false); s16 temp = memRead16(addr); @@ -567,7 +567,7 @@ void LHU() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 1)) + if (addr & 1) [[unlikely]] RaiseAddressError(addr, false); u16 temp = memRead16(addr); @@ -580,7 +580,7 @@ void LW() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 3)) + if (addr & 3) [[unlikely]] RaiseAddressError(addr, false); u32 temp = memRead32(addr); @@ -593,7 +593,7 @@ void LWU() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 3)) + if (addr & 3) [[unlikely]] RaiseAddressError(addr, false); u32 temp = memRead32(addr); @@ -681,7 +681,7 @@ void LD() { s32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 7)) + if (addr & 7) [[unlikely]] RaiseAddressError(addr, false); cpuRegs.GPR.r[_Rt_].UD[0] = memRead64(addr); @@ -743,7 +743,7 @@ void SH() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 1)) + if (addr & 1) [[unlikely]] RaiseAddressError(addr, true); memWrite16(addr, cpuRegs.GPR.r[_Rt_].US[0]); @@ -753,7 +753,7 @@ void SW() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 3)) + if (addr & 3) [[unlikely]] RaiseAddressError(addr, true); memWrite32(addr, cpuRegs.GPR.r[_Rt_].UL[0]); @@ -810,7 +810,7 @@ void SD() { u32 addr = cpuRegs.GPR.r[_Rs_].UL[0] + _Imm_; - if (unlikely(addr & 7)) + if (addr & 7) [[unlikely]] RaiseAddressError(addr, true); memWrite64(addr,cpuRegs.GPR.r[_Rt_].UD[0]); diff --git a/pcsx2/x86/newVif_Dynarec.cpp b/pcsx2/x86/newVif_Dynarec.cpp index 46ec0cef9f..d1f32fe904 100644 --- a/pcsx2/x86/newVif_Dynarec.cpp +++ b/pcsx2/x86/newVif_Dynarec.cpp @@ -407,10 +407,8 @@ _vifT __fi void dVifUnpack(const u8* data, bool isFill) // Seach in cache before trying to compile the block nVifBlock* b = v.vifBlocks.find(block); - if (unlikely(b == nullptr)) - { + if (!b) [[unlikely]] b = dVifCompile(block, isFill); - } { // Execute the block const VURegs& VU = vuRegs[idx]; @@ -419,7 +417,7 @@ _vifT __fi void dVifUnpack(const u8* data, bool isFill) u8* startmem = VU.Mem + (vif.tag.addr & (vuMemLimit - 0x10)); u8* endmem = VU.Mem + vuMemLimit; - if (likely((startmem + b->length) <= endmem)) + if ((startmem + b->length) <= endmem) [[likely]] { // No wrapping, you can run the fast dynarec ((nVifrecCall)b->startPtr)((uptr)startmem, (uptr)data);