Files
Lyrebird/Tests/LyrebirdDSPTests/ScalarDSPParityTests.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

375 lines
17 KiB
Swift
Raw Permalink 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.
// ScalarDSPParityTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable import LyrebirdDSP
// The portable scalar cores in `ScalarDSP.swift` are the non-Apple fallback for
// Accelerate's vDSP. A wrong operand order (vDSP's `vsub`/`vdiv` take the
// second operand first) would be a silent, Linux-only DSP bug. These tests run
// each scalar core against the *real* vDSP on Apple and require agreement, so
// the fallback is proven against the thing it replaces. Off Apple there is no
// reference to compare to, so the suite is Apple-only.
#if canImport(Accelerate)
import Accelerate
@Suite("ScalarDSP vDSP parity")
struct ScalarDSPParityTests {
private static let n: Int = 37 // deliberately not a power of two
private static let tolerance: Double = 1e-12
private func makeInput(_ seed: Double) -> [Double] {
var out: [Double] = [Double](repeating: 0, count: Self.n)
for i: Int in 0..<Self.n {
out[i] = sin(Double(i) * 0.31 + seed) * 3.0 - 0.5
}
return out
}
private func expectClose(_ a: [Double], _ b: [Double], _ label: String, _ tol: Double = tolerance) {
#expect(a.count == b.count)
for i: Int in 0..<a.count {
let diff: Double = abs(a[i] - b[i])
#expect(diff <= tol, "\(label): index \(i) scalar=\(a[i]) vDSP=\(b[i])")
}
}
/// Runs `body` with the base pointers of one read-only and one write buffer,
/// recording a test failure (rather than force-unwrapping) if either is empty.
private func withBases(
_ input: [Double],
_ output: inout [Double],
_ body: (UnsafePointer<Double>, UnsafeMutablePointer<Double>) -> Void
) {
input.withUnsafeBufferPointer { ip in
output.withUnsafeMutableBufferPointer { op in
guard let iBase: UnsafePointer<Double> = ip.baseAddress,
let oBase: UnsafeMutablePointer<Double> = op.baseAddress else {
Issue.record("empty buffer")
return
}
body(iBase, oBase)
}
}
}
@Test("c = a * scalar (vsmulD)")
func vScalarMul() {
let a: [Double] = makeInput(0.1)
var scalar: Double = 1.75
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
vDSP_vsmulD(a, 1, &scalar, &ref, 1, vDSP_Length(Self.n))
withBases(a, &got) { aBase, gBase in
_lbVScalarMul(aBase, 1, scalar, gBase, 1, Self.n)
}
expectClose(got, ref, "vsmul")
}
@Test("c = a + b (vaddD)")
func vAdd() {
let a: [Double] = makeInput(0.1)
let b: [Double] = makeInput(2.3)
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
vDSP_vaddD(a, 1, b, 1, &ref, 1, vDSP_Length(Self.n))
b.withUnsafeBufferPointer { bp in
guard let bBase: UnsafePointer<Double> = bp.baseAddress else { Issue.record("empty"); return }
withBases(a, &got) { aBase, gBase in
_lbVAdd(aBase, 1, bBase, 1, gBase, 1, Self.n)
}
}
expectClose(got, ref, "vadd")
}
@Test("c = a - b, operand order (vsubD)")
func vSub() {
let a: [Double] = makeInput(0.1)
let b: [Double] = makeInput(2.3)
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
// vDSP_vsubD(B, IB, A, IA, C, IC, N) computes C = A - B.
vDSP_vsubD(b, 1, a, 1, &ref, 1, vDSP_Length(Self.n))
b.withUnsafeBufferPointer { bp in
guard let bBase: UnsafePointer<Double> = bp.baseAddress else { Issue.record("empty"); return }
withBases(a, &got) { aBase, gBase in
_lbVSub(aBase, 1, bBase, 1, gBase, 1, Self.n)
}
}
expectClose(got, ref, "vsub")
}
@Test("c = a / b, operand order (vdivD)")
func vDiv() {
let a: [Double] = makeInput(0.1)
let b: [Double] = makeInput(2.3).map { $0 + 5.0 } // keep divisor away from 0
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
// vDSP_vdivD(B, IB, A, IA, C, IC, N) computes C = A / B.
vDSP_vdivD(b, 1, a, 1, &ref, 1, vDSP_Length(Self.n))
b.withUnsafeBufferPointer { bp in
guard let bBase: UnsafePointer<Double> = bp.baseAddress else { Issue.record("empty"); return }
withBases(a, &got) { aBase, gBase in
_lbVDiv(aBase, 1, bBase, 1, gBase, 1, Self.n)
}
}
expectClose(got, ref, "vdiv")
}
@Test("d = a*mul + add (vsmsaD)")
func vScalarMulScalarAdd() {
let a: [Double] = makeInput(0.1)
var mul: Double = 2.5
var add: Double = -1.25
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
vDSP_vsmsaD(a, 1, &mul, &add, &ref, 1, vDSP_Length(Self.n))
withBases(a, &got) { aBase, gBase in
_lbVScalarMulScalarAdd(aBase, 1, mul, add, gBase, 1, Self.n)
}
expectClose(got, ref, "vsmsa")
}
@Test("d = a*mul + c(vector) (vsmaD)")
func vScalarMulVAdd() {
let a: [Double] = makeInput(0.1)
let c: [Double] = makeInput(1.7)
var mul: Double = 0.8
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
vDSP_vsmaD(a, 1, &mul, c, 1, &ref, 1, vDSP_Length(Self.n))
c.withUnsafeBufferPointer { cp in
guard let cBase: UnsafePointer<Double> = cp.baseAddress else { Issue.record("empty"); return }
withBases(a, &got) { aBase, gBase in
_lbVScalarMulVAdd(aBase, 1, mul, cBase, 1, gBase, 1, Self.n)
}
}
expectClose(got, ref, "vsma")
}
@Test("scalar reductions (maxmgvD, sveD)")
func reductions() {
let a: [Double] = makeInput(0.1)
var refMax: Double = 0
var refSum: Double = 0
vDSP_maxmgvD(a, 1, &refMax, vDSP_Length(Self.n))
vDSP_sveD(a, 1, &refSum, vDSP_Length(Self.n))
var gotMax: Double = 0
var gotSum: Double = 0
a.withUnsafeBufferPointer { ap in
guard let aBase: UnsafePointer<Double> = ap.baseAddress else { Issue.record("empty"); return }
gotMax = _lbMaxMagnitude(aBase, 1, Self.n)
gotSum = _lbSum(aBase, 1, Self.n)
}
#expect(abs(gotMax - refMax) <= Self.tolerance)
#expect(abs(gotSum - refSum) <= 1e-10)
}
@Test("double→float conversion (vdpsp)")
func convertDoubleToFloat() {
let a: [Double] = makeInput(0.1)
var ref: [Float] = [Float](repeating: 0, count: Self.n)
var got: [Float] = [Float](repeating: 0, count: Self.n)
vDSP_vdpsp(a, 1, &ref, 1, vDSP_Length(Self.n))
a.withUnsafeBufferPointer { ap in
got.withUnsafeMutableBufferPointer { gp in
guard let aBase: UnsafePointer<Double> = ap.baseAddress,
let gBase: UnsafeMutablePointer<Float> = gp.baseAddress else { Issue.record("empty"); return }
_lbConvertDoubleToFloat(aBase, 1, gBase, 1, Self.n)
}
}
for i: Int in 0..<Self.n { #expect(got[i] == ref[i]) }
}
@Test("row-major matrix multiply (mmulD)")
func matMul() {
// C[3×2] = A[3×4] · B[4×2]
let m: Int = 3, n: Int = 2, p: Int = 4
var a: [Double] = (0..<(m * p)).map { sin(Double($0) * 0.7) }
var b: [Double] = (0..<(p * n)).map { cos(Double($0) * 0.4) - 0.3 }
var ref: [Double] = [Double](repeating: 0, count: m * n)
var got: [Double] = [Double](repeating: 0, count: m * n)
vDSP_mmulD(&a, 1, &b, 1, &ref, 1, vDSP_Length(m), vDSP_Length(n), vDSP_Length(p))
a.withUnsafeBufferPointer { ap in b.withUnsafeBufferPointer { bp in
got.withUnsafeMutableBufferPointer { gp in
guard let aB = ap.baseAddress, let bB = bp.baseAddress, let gB = gp.baseAddress else { Issue.record("empty"); return }
_lbMatMul(aB, 1, bB, 1, gB, 1, m, n, p)
}
} }
for i: Int in 0..<(m * n) { #expect(abs(got[i] - ref[i]) <= Self.tolerance) }
}
@Test("biquad cascade matches vDSP (2 sections)")
func biquad() {
let m: Int = 2
// Two stable sections: [b0, b1, b2, a1, a2] each.
var coeffs: [Double] = [0.5, 0.3, 0.1, -0.2, 0.05,
0.7, -0.1, 0.02, 0.15, -0.30]
let input: [Double] = makeInput(0.2)
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
var refDelay: [Double] = [Double](repeating: 0, count: 2 * m + 2)
var gotDelay: [Double] = [Double](repeating: 0, count: 2 * m + 2)
guard let setup: vDSP_biquad_SetupD = vDSP_biquad_CreateSetupD(&coeffs, vDSP_Length(m)) else {
Issue.record("vDSP_biquad_CreateSetupD returned nil"); return
}
defer { vDSP_biquad_DestroySetupD(setup) }
input.withUnsafeBufferPointer { ip in
guard let iBase: UnsafePointer<Double> = ip.baseAddress else { Issue.record("empty"); return }
vDSP_biquadD(setup, &refDelay, iBase, 1, &ref, 1, vDSP_Length(Self.n))
}
input.withUnsafeBufferPointer { ip in
got.withUnsafeMutableBufferPointer { gp in
coeffs.withUnsafeBufferPointer { cp in
guard let iBase = ip.baseAddress, let gBase = gp.baseAddress, let cBase = cp.baseAddress else { Issue.record("empty"); return }
_lbRunBiquad(cBase, m, &gotDelay, iBase, 1, gBase, 1, Self.n)
}
}
}
for i: Int in 0..<Self.n {
#expect(abs(got[i] - ref[i]) <= 1e-9, "biquad index \(i): got \(got[i]) vDSP \(ref[i])")
}
}
@Test("real FFT forward matches vDSP_fft_zrip (n=16)")
func fftForward() {
let log2n: vDSP_Length = 4
let n: Int = 16
let half: Int = n / 2
let r: [Float] = (0..<n).map { Float(sin(Double($0) * 0.9) + 0.3 * cos(Double($0) * 0.35)) }
var rpV: [Float] = [Float](repeating: 0, count: half)
var ipV: [Float] = [Float](repeating: 0, count: half)
var rpM: [Float] = [Float](repeating: 0, count: half)
var ipM: [Float] = [Float](repeating: 0, count: half)
for j: Int in 0..<half {
rpV[j] = r[2 * j]; ipV[j] = r[2 * j + 1]
rpM[j] = r[2 * j]; ipM[j] = r[2 * j + 1]
}
var workRe: [Float] = [Float](repeating: 0, count: n)
var workIm: [Float] = [Float](repeating: 0, count: n)
guard let setup: FFTSetup = vDSP_create_fftsetup(log2n, FFTRadix(kFFTRadix2)) else {
Issue.record("create_fftsetup nil"); return
}
defer { vDSP_destroy_fftsetup(setup) }
rpV.withUnsafeMutableBufferPointer { rp in ipV.withUnsafeMutableBufferPointer { ip in
guard let a = rp.baseAddress, let b = ip.baseAddress else { return }
var split: DSPSplitComplex = DSPSplitComplex(realp: a, imagp: b)
vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(kFFTDirection_Forward))
} }
rpM.withUnsafeMutableBufferPointer { rp in ipM.withUnsafeMutableBufferPointer { ip in
workRe.withUnsafeMutableBufferPointer { wr in workIm.withUnsafeMutableBufferPointer { wi in
guard let a = rp.baseAddress, let b = ip.baseAddress, let c = wr.baseAddress, let d = wi.baseAddress else { return }
_lbRealFFTForward(a, b, half, c, d)
} }
} }
let tol: Float = 1e-2
for k: Int in 0..<half {
#expect(abs(rpM[k] - rpV[k]) <= tol, "fft realp[\(k)]: mine \(rpM[k]) vDSP \(rpV[k])")
#expect(abs(ipM[k] - ipV[k]) <= tol, "fft imagp[\(k)]: mine \(ipM[k]) vDSP \(ipV[k])")
}
}
@Test("real FFT forward→inverse matches vDSP (n=16)")
func fftRoundTrip() {
let log2n: vDSP_Length = 4
let n: Int = 16
let half: Int = n / 2
let r: [Float] = (0..<n).map { Float(cos(Double($0) * 0.5) - 0.2 * Double($0 % 3)) }
var rpV: [Float] = [Float](repeating: 0, count: half)
var ipV: [Float] = [Float](repeating: 0, count: half)
var rpM: [Float] = [Float](repeating: 0, count: half)
var ipM: [Float] = [Float](repeating: 0, count: half)
for j: Int in 0..<half {
rpV[j] = r[2 * j]; ipV[j] = r[2 * j + 1]
rpM[j] = r[2 * j]; ipM[j] = r[2 * j + 1]
}
var workRe: [Float] = [Float](repeating: 0, count: n)
var workIm: [Float] = [Float](repeating: 0, count: n)
guard let setup: FFTSetup = vDSP_create_fftsetup(log2n, FFTRadix(kFFTRadix2)) else {
Issue.record("create_fftsetup nil"); return
}
defer { vDSP_destroy_fftsetup(setup) }
rpV.withUnsafeMutableBufferPointer { rp in ipV.withUnsafeMutableBufferPointer { ip in
guard let a = rp.baseAddress, let b = ip.baseAddress else { return }
var split: DSPSplitComplex = DSPSplitComplex(realp: a, imagp: b)
vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(kFFTDirection_Forward))
vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(kFFTDirection_Inverse))
} }
rpM.withUnsafeMutableBufferPointer { rp in ipM.withUnsafeMutableBufferPointer { ip in
workRe.withUnsafeMutableBufferPointer { wr in workIm.withUnsafeMutableBufferPointer { wi in
guard let a = rp.baseAddress, let b = ip.baseAddress, let c = wr.baseAddress, let d = wi.baseAddress else { return }
_lbRealFFTForward(a, b, half, c, d)
_lbRealFFTInverse(a, b, half, c, d)
} }
} }
// Both should equal 2n × original (packed even/odd); compare to each other.
let tol: Float = 1e-1
for k: Int in 0..<half {
#expect(abs(rpM[k] - rpV[k]) <= tol, "roundtrip realp[\(k)]: mine \(rpM[k]) vDSP \(rpV[k])")
#expect(abs(ipM[k] - ipV[k]) <= tol, "roundtrip imagp[\(k)]: mine \(ipM[k]) vDSP \(ipV[k])")
}
}
@Test("input × ramp, with start mutation (vrampmul)")
func rampMul() {
let input: [Float] = (0..<Self.n).map { Float(sin(Double($0) * 0.3)) }
var startRef: Float = 0.25, startGot: Float = 0.25
var stepRef: Float = 0.03
let step: Float = 0.03
var ref: [Float] = [Float](repeating: 0, count: Self.n)
var got: [Float] = [Float](repeating: 0, count: Self.n)
input.withUnsafeBufferPointer { ip in
guard let iB = ip.baseAddress else { Issue.record("empty"); return }
ref.withUnsafeMutableBufferPointer { rp in
guard let rB = rp.baseAddress else { return }
vDSP_vrampmul(iB, 1, &startRef, &stepRef, rB, 1, vDSP_Length(Self.n))
}
got.withUnsafeMutableBufferPointer { gp in
guard let gB = gp.baseAddress else { return }
_lbRampMul(iB, 1, &startGot, step, gB, 1, Self.n)
}
}
for i: Int in 0..<Self.n { #expect(abs(got[i] - ref[i]) <= 1e-5) }
#expect(abs(startGot - startRef) <= 1e-5, "start mutation: mine \(startGot) vDSP \(startRef)")
}
@Test("Hann window matches vDSP (n=32)")
func hannWindow() {
let n: Int = 32
var ref: [Float] = [Float](repeating: 0, count: n)
var got: [Float] = [Float](repeating: 0, count: n)
ref.withUnsafeMutableBufferPointer { rp in
guard let rB = rp.baseAddress else { return }
vDSP_hann_window(rB, vDSP_Length(n), Int32(vDSP_HANN_DENORM))
}
got.withUnsafeMutableBufferPointer { gp in
guard let gB = gp.baseAddress else { return }
_lbHannWindow(gB, n)
}
for i: Int in 0..<n { #expect(abs(got[i] - ref[i]) <= 1e-6, "hann[\(i)]: mine \(got[i]) vDSP \(ref[i])") }
}
@Test("c[i] = start + i*step (vrampD)")
func vRamp() {
var start: Double = 0.3
var step: Double = 0.07
var ref: [Double] = [Double](repeating: 0, count: Self.n)
var got: [Double] = [Double](repeating: 0, count: Self.n)
vDSP_vrampD(&start, &step, &ref, 1, vDSP_Length(Self.n))
got.withUnsafeMutableBufferPointer { g in
guard let gBase: UnsafeMutablePointer<Double> = g.baseAddress else { Issue.record("empty"); return }
_lbVRamp(0.3, 0.07, gBase, 1, Self.n)
}
expectClose(got, ref, "vramp")
}
}
#endif