// 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.., UnsafeMutablePointer) -> Void ) { input.withUnsafeBufferPointer { ip in output.withUnsafeMutableBufferPointer { op in guard let iBase: UnsafePointer = ip.baseAddress, let oBase: UnsafeMutablePointer = 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 = 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 = 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 = 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 = 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 = 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 = ap.baseAddress, let gBase: UnsafeMutablePointer = gp.baseAddress else { Issue.record("empty"); return } _lbConvertDoubleToFloat(aBase, 1, gBase, 1, Self.n) } } for i: Int in 0.. = 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.. = g.baseAddress else { Issue.record("empty"); return } _lbVRamp(0.3, 0.07, gBase, 1, Self.n) } expectClose(got, ref, "vramp") } } #endif