Files
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

56 lines
2.5 KiB
C

#include "LyrebirdC.h"
// Header selection is platform/arch dependent:
// - Apple: <fenv.h> exposes the arch-specific "flush denormals" environments.
// - non-Apple x86: SSE control via the Intel intrinsics headers.
#if defined(__APPLE__)
#include <fenv.h>
#elif defined(__x86_64__) || defined(__i386__)
#include <xmmintrin.h> // _MM_SET_FLUSH_ZERO_MODE (FTZ)
#include <pmmintrin.h> // _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 <fenv.h> 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
}