#include "LyrebirdC.h" // Header selection is platform/arch dependent: // - Apple: exposes the arch-specific "flush denormals" environments. // - non-Apple x86: SSE control via the Intel intrinsics headers. #if defined(__APPLE__) #include #elif defined(__x86_64__) || defined(__i386__) #include // _MM_SET_FLUSH_ZERO_MODE (FTZ) #include // _MM_SET_DENORMALS_ZERO_MODE (DAZ) #endif // Enable flush-to-zero / denormals-are-zero for float SIMD math on the RT path. // Denormal handling is catastrophically slow on most FPUs, so audio DSP disables // it. The mechanism differs by platform and arch; the end effect is the same. // // This is thread-local state, so every thread that runs DSP has to set it: // engine start does so for the setup thread, and each driver's render callback // (CoreAudioDriver, iOSAudioDriver, ALSAAudioDriver) calls it on entry. It is a // few register writes — cheap enough per callback, never per-sample. void lyrebird_enable_flush_to_zero(void) { #if defined(__APPLE__) // Apple's exposes two arch-specific environments: // - arm64: FE_DFL_DISABLE_DENORMS_ENV (sets FPCR.FZ) // - x86_64: FE_DFL_DISABLE_SSE_DENORMS_ENV (sets MXCSR.FZ + DAZ for SSE) // Universal-binary builds compile this TU for both archs; each uses the // macro that exists on its target arch. These macros are Darwin-only. #if defined(__arm64__) fesetenv(FE_DFL_DISABLE_DENORMS_ENV); #elif defined(__i386__) || defined(__x86_64__) fesetenv(FE_DFL_DISABLE_SSE_DENORMS_ENV); #else (void)0; #endif #elif defined(__x86_64__) || defined(__i386__) // Non-Apple x86 (Linux/Windows): set SSE flush-to-zero + denormals-are-zero // directly in MXCSR. FZ is SSE1, so it is safe on any x86 that gets here; // DAZ needs SSE3, which is baseline on x86_64 but NOT on 32-bit x86 — a // pre-SSE3 i386 CPU faults on the instruction, so it is gated. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); #if defined(__x86_64__) || defined(__SSE3__) _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); #endif #elif defined(__aarch64__) // Non-Apple AArch64 (Linux): set FPCR.FZ (bit 24) to flush denormals to zero. unsigned long fpcr; __asm__ __volatile__("mrs %0, fpcr" : "=r"(fpcr)); fpcr |= (1UL << 24); __asm__ __volatile__("msr fpcr, %0" : : "r"(fpcr)); #else // Unsupported arch — leave the FPU in default IEEE-754 mode. Denormals are // still processed correctly, just without the performance win. (void)0; #endif }