// AccelerateShim.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // `LyrebirdDSP` is the seam that lets the synthesis core build off Apple // platforms. Every Lyrebird file that previously wrote `import Accelerate` // writes `import LyrebirdDSP` instead: // // * On Apple, this module re-exports Accelerate wholesale // (`@_exported import Accelerate`), so `vDSP_*` / `vv*` / FFT / biquad all // resolve to the real, hardware-accelerated implementations. Call sites are // byte-identical to before — there is no behavioural or performance change. // // * On non-Apple platforms, Accelerate does not exist. This file (plus // `ScalarDSP.swift`, `PortableBiquad.swift`, `PortableFFT.swift`, // `ComplexFFTCore.swift`) supplies API-compatible replacements for every // vDSP/vForce primitive Lyrebird uses — vector ops and vForce here, the // biquad-setup family in `PortableBiquad.swift`, and the FFT family // (`vDSP_fft_zrip`, `vDSP_create_fftsetup`, `vDSP_ctoz`, …) in // `PortableFFT.swift`. All are parity-tested against real vDSP on Apple in // `Tests/LyrebirdDSPTests`. // Re-export the platform C library so every consumer gets scalar math // (`sin`, `cos`, `pow`, `tanh`, `exp`, `log`, …) — and libc — through the single // `import LyrebirdDSP`, with no per-file platform `#if`. This is the scalar-math // counterpart to the vDSP re-export below. #if canImport(Darwin) @_exported import Darwin #elseif canImport(Glibc) @_exported import Glibc #elseif canImport(Musl) @_exported import Musl #elseif canImport(ucrt) @_exported import ucrt #endif #if canImport(Accelerate) @_exported import Accelerate #else // (The platform C library — sin/cos/sqrt/… — is @_exported at the top of this // file, so the scalar cores below and every consumer already have it in scope.) // MARK: - vDSP type shims public typealias vDSP_Length = UInt public typealias vDSP_Stride = Int // MARK: - Double vector ops @inline(__always) public func vDSP_vclrD(_ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbClear(c, ic, Int(n)) } @inline(__always) public func vDSP_vfillD(_ a: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbFill(a.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_vsmulD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarMul(a, ia, b.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_vaddD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVAdd(a, ia, b, ib, c, ic, Int(n)) } @inline(__always) public func vDSP_vmulD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVMul(a, ia, b, ib, c, ic, Int(n)) } /// `vDSP_vsubD` takes the subtrahend `b` first, then the minuend `a`; result is `a - b`. @inline(__always) public func vDSP_vsubD(_ b: UnsafePointer, _ ib: vDSP_Stride, _ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVSub(a, ia, b, ib, c, ic, Int(n)) } @inline(__always) public func vDSP_vsaddD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarAdd(a, ia, b.pointee, c, ic, Int(n)) } /// `d = a * b + c` where `b` and `c` are scalars. @inline(__always) public func vDSP_vsmsaD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafePointer, _ d: UnsafeMutablePointer, _ id: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarMulScalarAdd(a, ia, b.pointee, c.pointee, d, id, Int(n)) } /// `d = a * b + c` where `b` is a scalar and `c` is a vector. @inline(__always) public func vDSP_vsmaD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafePointer, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer, _ id: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarMulVAdd(a, ia, b.pointee, c, ic, d, id, Int(n)) } @inline(__always) public func vDSP_vabsD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVAbs(a, ia, c, ic, Int(n)) } @inline(__always) public func vDSP_vsdivD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarDiv(a, ia, b.pointee, c, ic, Int(n)) } /// `vDSP_vdivD` takes the divisor `b` first, then the dividend `a`; result is `a / b`. @inline(__always) public func vDSP_vdivD(_ b: UnsafePointer, _ ib: vDSP_Stride, _ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVDiv(a, ia, b, ib, c, ic, Int(n)) } /// `c = a / b` where `a` is a scalar. @inline(__always) public func vDSP_svdivD(_ a: UnsafePointer, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbScalarVDiv(a.pointee, b, ib, c, ic, Int(n)) } /// `c[i] = a + i * b` where `a` (start) and `b` (step) are scalars. @inline(__always) public func vDSP_vrampD(_ a: UnsafePointer, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVRamp(a.pointee, b.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_maxmgvD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ n: vDSP_Length) { c.pointee = _lbMaxMagnitude(a, ia, Int(n)) } @inline(__always) public func vDSP_sveD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ n: vDSP_Length) { c.pointee = _lbSum(a, ia, Int(n)) } // MARK: - Float vector ops @inline(__always) public func vDSP_vclr(_ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbClear(c, ic, Int(n)) } @inline(__always) public func vDSP_vsmul(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarMul(a, ia, b.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_vadd(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVAdd(a, ia, b, ib, c, ic, Int(n)) } @inline(__always) public func vDSP_vmul(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVMul(a, ia, b, ib, c, ic, Int(n)) } @inline(__always) public func vDSP_vsadd(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarAdd(a, ia, b.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_vsma(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafePointer, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer, _ id: vDSP_Stride, _ n: vDSP_Length) { _lbVScalarMulVAdd(a, ia, b.pointee, c, ic, d, id, Int(n)) } @inline(__always) public func vDSP_vramp(_ a: UnsafePointer, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVRamp(a.pointee, b.pointee, c, ic, Int(n)) } /// `d[i] = a[i] + c * (b[i] - a[i])` where `c` is a scalar interpolation factor. @inline(__always) public func vDSP_vintb(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafePointer, _ d: UnsafeMutablePointer, _ id: vDSP_Stride, _ n: vDSP_Length) { _lbVInterp(a, ia, b, ib, c.pointee, d, id, Int(n)) } /// `c[i] = max(a[i], b)` where `b` is a scalar threshold (low-clamp). @inline(__always) public func vDSP_vthr(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbVThreshold(a, ia, b.pointee, c, ic, Int(n)) } @inline(__always) public func vDSP_dotpr(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ n: vDSP_Length) { c.pointee = _lbDot(a, ia, b, ib, Int(n)) } /// `output = input · (start + i·step)`; `start` is advanced past the block. @inline(__always) public func vDSP_vrampmul(_ input: UnsafePointer, _ iStride: vDSP_Stride, _ start: UnsafeMutablePointer, _ step: UnsafePointer, _ output: UnsafeMutablePointer, _ oStride: vDSP_Stride, _ n: vDSP_Length) { _lbRampMul(input, iStride, start, step.pointee, output, oStride, Int(n)) } /// Hann-window flags (only the standard denormalized window is used). public let vDSP_HANN_DENORM: Int = 0 public let vDSP_HANN_NORM: Int = 1 /// Fills `c` with a symmetric Hann window of length `n` (flag ignored; the /// codebase only requests `vDSP_HANN_DENORM`). @inline(__always) public func vDSP_hann_window(_ c: UnsafeMutablePointer, _ n: vDSP_Length, _ flag: Int32) { _lbHannWindow(c, Int(n)) } // MARK: - Conversion & matrix /// Double→Float conversion. @inline(__always) public func vDSP_vdpsp(_ a: UnsafePointer, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ n: vDSP_Length) { _lbConvertDoubleToFloat(a, ia, c, ic, Int(n)) } /// Row-major matrix multiply `C[M×N] = A[M×P] · B[P×N]`. @inline(__always) public func vDSP_mmulD(_ a: UnsafePointer, _ ia: vDSP_Stride, _ b: UnsafePointer, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer, _ ic: vDSP_Stride, _ m: vDSP_Length, _ n: vDSP_Length, _ p: vDSP_Length) { _lbMatMul(a, ia, b, ib, c, ic, Int(m), Int(n), Int(p)) } // MARK: - vForce transcendentals (n is a pointer to the Int32 element count) @inline(__always) public func vvsin(_ y: UnsafeMutablePointer, _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ cosOut: UnsafeMutablePointer, _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0.., _ y: UnsafePointer, _ x: UnsafePointer, _ n: UnsafePointer) { let count: Int = Int(n.pointee) for i: Int in 0..