Files
Lyrebird/Sources/LyrebirdDSP/AccelerateShim.swift
T
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

318 lines
13 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbClear(c, ic, Int(n))
}
@inline(__always)
public func vDSP_vfillD(_ a: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbFill(a.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsmulD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVScalarMul(a, ia, b.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vaddD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVAdd(a, ia, b, ib, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vmulD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ ib: vDSP_Stride, _ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVSub(a, ia, b, ib, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsaddD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafePointer<Double>, _ d: UnsafeMutablePointer<Double>, _ 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<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafePointer<Double>, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer<Double>, _ 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<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVAbs(a, ia, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsdivD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ ib: vDSP_Stride, _ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVRamp(a.pointee, b.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_maxmgvD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ n: vDSP_Length) {
c.pointee = _lbMaxMagnitude(a, ia, Int(n))
}
@inline(__always)
public func vDSP_sveD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ n: vDSP_Length) {
c.pointee = _lbSum(a, ia, Int(n))
}
// MARK: - Float vector ops
@inline(__always)
public func vDSP_vclr(_ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbClear(c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsmul(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVScalarMul(a, ia, b.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vadd(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVAdd(a, ia, b, ib, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vmul(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVMul(a, ia, b, ib, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsadd(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVScalarAdd(a, ia, b.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_vsma(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafePointer<Float>, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer<Float>, _ 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<Float>, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ 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<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafePointer<Float>, _ d: UnsafeMutablePointer<Float>, _ 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<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
_lbVThreshold(a, ia, b.pointee, c, ic, Int(n))
}
@inline(__always)
public func vDSP_dotpr(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ 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<Float>, _ iStride: vDSP_Stride, _ start: UnsafeMutablePointer<Float>, _ step: UnsafePointer<Float>, _ output: UnsafeMutablePointer<Float>, _ 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<Float>, _ n: vDSP_Length, _ flag: Int32) {
_lbHannWindow(c, Int(n))
}
// MARK: - Conversion & matrix
/// Double→Float conversion.
@inline(__always)
public func vDSP_vdpsp(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ 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<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ 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<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = sin(x[i]) }
}
@inline(__always)
public func vvcos(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = cos(x[i]) }
}
@inline(__always)
public func vvsqrt(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = sqrt(x[i]) }
}
@inline(__always)
public func vvexp(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = exp(x[i]) }
}
@inline(__always)
public func vvlog(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = log(x[i]) }
}
@inline(__always)
public func vvfloor(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = floor(x[i]) }
}
@inline(__always)
public func vvceil(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = ceil(x[i]) }
}
@inline(__always)
public func vvsinf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = sin(x[i]) }
}
@inline(__always)
public func vvcosf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = cos(x[i]) }
}
@inline(__always)
public func vvsqrtf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = sqrt(x[i]) }
}
@inline(__always)
public func vvexpf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = exp(x[i]) }
}
@inline(__always)
public func vvlogf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { y[i] = log(x[i]) }
}
/// `sinOut[i] = sin(x[i])`, `cosOut[i] = cos(x[i])`.
@inline(__always)
public func vvsincosf(_ sinOut: UnsafeMutablePointer<Float>, _ cosOut: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count {
sinOut[i] = sin(x[i])
cosOut[i] = cos(x[i])
}
}
/// `z[i] = atan2(y[i], x[i])`.
@inline(__always)
public func vvatan2f(_ z: UnsafeMutablePointer<Float>, _ y: UnsafePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
let count: Int = Int(n.pointee)
for i: Int in 0..<count { z[i] = atan2(y[i], x[i]) }
}
#endif