diff --git a/adapters/standalone.cpp b/adapters/standalone.cpp index 0e24be00..7aa0ba95 100644 --- a/adapters/standalone.cpp +++ b/adapters/standalone.cpp @@ -129,6 +129,7 @@ int main(int argc, char* argv[]) { // Initialize environment system::init(); + system::initCpuFlags(); asset::init(); if (!settings::devMode) { logger::logPath = asset::user("log.txt"); diff --git a/include/system.hpp b/include/system.hpp index 47a6231e..8afa5d2e 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -196,6 +196,9 @@ The launched process will continue running if the current process is closed. void runProcessDetached(const std::string& path); PRIVATE void init(); +/** Sets Rack-recommended CPU flags for the current thread. +*/ +void initCpuFlags(); } // namespace system diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 31593e25..6b2e332d 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -21,24 +21,6 @@ namespace rack { namespace engine { -static void initCpu() { - // Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode - // https://software.intel.com/en-us/node/682949 - // On ARM64, this is a SIMDe function. - _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); - // ARM64 always uses DAZ -#if defined ARCH_X64 - _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); -#endif - - // Reset rounding mode -#if !defined _MM_ROUND_NEAREST - #define _MM_ROUND_NEAREST 0x0000 -#endif - _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST); -} - - /** Barrier based on mutexes. Not finished or tested, do not use. */ @@ -495,7 +477,7 @@ void Engine::stepBlock(int frames) { SharedLock lock(internal->mutex); // Configure thread uint32_t csr = _mm_getcsr(); - initCpu(); + system::initCpuFlags(); random::init(); internal->blockFrame = internal->frame; @@ -1289,7 +1271,7 @@ void EngineWorker::run() { // Configure thread contextSet(engine->internal->context); system::setThreadName(string::f("Worker %d", id)); - initCpu(); + system::initCpuFlags(); random::init(); while (true) { diff --git a/src/system.cpp b/src/system.cpp index 671130a6..0411580f 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,6 +1,7 @@ #include #include #include +#include // for std::fesetround #include #include @@ -38,6 +39,7 @@ #include #include +#include /* @@ -955,5 +957,20 @@ void init() { } +void initCpuFlags() { + // Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode + // https://software.intel.com/en-us/node/682949 + // On ARM64, this is a SIMDe function. + _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); + // ARM64 always uses DAZ +#if defined ARCH_X64 + _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); +#endif + + // Reset rounding mode to default (nearest) + std::fesetround(FE_TONEAREST); +} + + } // namespace system } // namespace rack