diff --git a/src/oscillator/adaa.rs b/src/oscillator/adaa.rs index aa85a91..4fc58bb 100644 --- a/src/oscillator/adaa.rs +++ b/src/oscillator/adaa.rs @@ -4,6 +4,22 @@ //! Periodic primitive tables and cancellation-safe divided differences for phase modulation. //! All phases in a difference use the same current band and unwrapped cycle coordinates. +#[inline(always)] +fn floor_phase(value: f64) -> f64 { + #[cfg(not(miri))] + if std::is_x86_feature_detected!("sse4.1") { + let result: f64; + // SAFETY: SSE4.1の実行可否を確認済み。下位f64だけを入出力し、メモリに触れない。 + unsafe { + std::arch::asm!("roundsd {result}, {value}, 9", + result = lateout(xmm_reg) result, value = in(xmm_reg) value, + options(pure, nomem, nostack, preserves_flags)); + } + return result; + } + value.floor() +} + #[derive(Clone, Copy)] struct Table { /// Intervals per cycle: 4096 stores a full cycle, larger tables a half cycle. @@ -102,7 +118,7 @@ impl Table { fn loc(self, q: f64) -> (usize, f64, f64) { // Large tables store the positive half cycle: F2 is odd and F1 is even. // Small tables retain a full cycle to avoid reflection in the common path. - let phase = q - (q + if FOLDED { 0.5 } else { 0.0 }).floor(); + let phase = q - floor_phase(q + if FOLDED { 0.5 } else { 0.0 }); let x = if FOLDED { phase.abs() } else { phase } * self.n; // SAFETY: Oscillator supplies finite, bounded modulated phases. Folding // gives |phase| <= 1/2 for large tables (n <= 131072), or phase <= 1 diff --git a/src/oscillator.rs b/src/oscillator.rs index 17ae160..93bd120 100644 --- a/src/oscillator.rs +++ b/src/oscillator.rs @@ -204,6 +204,9 @@ pub struct Oscillator { impl Oscillator { pub fn new() -> Self { + // CPUIDキャッシュの初期化を音声処理の開始前に済ませる。 + #[cfg(not(miri))] + let _ = std::is_x86_feature_detected!("sse4.1"); Self { state: OscillatorState { phases: [0.0; 7],