From d54fb0ba78ce2ffac3caa399414af19ec1016f05 Mon Sep 17 00:00:00 2001 From: SamoZ256 <96914946+SamoZ256@users.noreply.github.com> Date: Sat, 27 Sep 2025 00:41:17 +0200 Subject: [PATCH] arm: flush denormals to zero (#1696) --- src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp b/src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp index 2f89000b..19391bbd 100644 --- a/src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp +++ b/src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp @@ -15,6 +15,33 @@ #include "util/helpers/helpers.h" +#ifdef __arm64__ +#if defined(__clang__) +#include +#elif defined(_MSC_VER) +#include +#endif +#endif + +namespace { + +void enableFlushDenormalsToZero() +{ +#if defined(ARCH_X86_64) + _mm_setcsr(_mm_getcsr() | 0x8000); +#elif defined(__arm64__) +#if defined(__clang__) + __arm_wsr64("fpcr", __arm_rsr64("fpcr") | (1 << 24)); +#elif defined(__GNUC__) + __builtin_aarch64_set_fpcr(__builtin_aarch64_get_fpcr() | (1 << 24)); +#elif defined(_MSC_VER) + _WriteStatusReg(ARM64_FPCR, _ReadStatusReg(ARM64_FPCR) | (1 << 24)); +#endif +#endif +} + +} + SlimRWLock srwlock_activeThreadList; // public list of active threads @@ -1321,9 +1348,7 @@ namespace coreinit #endif OSHostThread* hostThread = (OSHostThread*)_thread; - #if defined(ARCH_X86_64) - _mm_setcsr(_mm_getcsr() | 0x8000); // flush denormals to zero - #endif + enableFlushDenormalsToZero(); PPCInterpreter_t* hCPU = &hostThread->ppcInstance; __OSLoadThread(hostThread->m_thread, hCPU, hostThread->selectedCore); @@ -1369,9 +1394,8 @@ namespace coreinit { SetThreadName(fmt::format("OSSched[core={}]", (uintptr_t)_assignedCoreIndex).c_str()); t_assignedCoreIndex = (sint32)(uintptr_t)_assignedCoreIndex; - #if defined(ARCH_X86_64) - _mm_setcsr(_mm_getcsr() | 0x8000); // flush denormals to zero - #endif + + enableFlushDenormalsToZero(); #if BOOST_OS_LINUX if (g_gdbstub)