118 lines
4.1 KiB
Swift
118 lines
4.1 KiB
Swift
// BinaryOpVectorHelpers.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Accelerate
|
|
|
|
/// The four arithmetic operators `BinaryOpGenerator` can dispatch to vDSP. A
|
|
/// generator built from an arbitrary closure (not one of these four) carries no
|
|
/// kind and falls back to the per-sample closure loop.
|
|
enum BinaryOpKind {
|
|
case add
|
|
case subtract
|
|
case multiply
|
|
case divide
|
|
}
|
|
|
|
// Vectorised `BinaryOpGenerator` inner loops. Each dispatches one output row.
|
|
// All four operators map to correctly-rounded vDSP routines, so results are
|
|
// bit-identical to the scalar `lhs op rhs` loop — divide uses true division
|
|
// (vsdivD / svdivD / vdivD), never reciprocal-multiply, to preserve that.
|
|
//
|
|
// vDSP subtract/divide take operands in `C = A - B` / `C = A / B` order with the
|
|
// "B" (subtrahend / divisor) pointer FIRST — the calls below pass them reversed
|
|
// accordingly.
|
|
|
|
/// `out[i] = ls[i] <op> scalar`. Caller guarantees `ls.count >= count`.
|
|
@inline(__always)
|
|
func binaryOpVectorScalarRHS(
|
|
_ ls: [Double],
|
|
_ scalar: Double,
|
|
_ kind: BinaryOpKind,
|
|
into out: inout [Double],
|
|
count: Int
|
|
) {
|
|
var s: Double = scalar
|
|
ls.withUnsafeBufferPointer { lp in
|
|
out.withUnsafeMutableBufferPointer { op in
|
|
guard let a: UnsafePointer<Double> = lp.baseAddress,
|
|
let c: UnsafeMutablePointer<Double> = op.baseAddress else { return }
|
|
let n: vDSP_Length = vDSP_Length(count)
|
|
switch kind {
|
|
case .add:
|
|
vDSP_vsaddD(a, 1, &s, c, 1, n)
|
|
case .multiply:
|
|
vDSP_vsmulD(a, 1, &s, c, 1, n)
|
|
case .subtract:
|
|
var neg: Double = -scalar // ls - s == ls + (-s), bit-exact
|
|
vDSP_vsaddD(a, 1, &neg, c, 1, n)
|
|
case .divide:
|
|
vDSP_vsdivD(a, 1, &s, c, 1, n) // C[i] = ls[i] / s (true division)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `out[i] = scalar <op> rs[i]`. Caller guarantees `rs.count >= count`.
|
|
@inline(__always)
|
|
func binaryOpScalarLHSVector(
|
|
_ scalar: Double,
|
|
_ rs: [Double],
|
|
_ kind: BinaryOpKind,
|
|
into out: inout [Double],
|
|
count: Int
|
|
) {
|
|
var s: Double = scalar
|
|
rs.withUnsafeBufferPointer { rp in
|
|
out.withUnsafeMutableBufferPointer { op in
|
|
guard let b: UnsafePointer<Double> = rp.baseAddress,
|
|
let c: UnsafeMutablePointer<Double> = op.baseAddress else { return }
|
|
let n: vDSP_Length = vDSP_Length(count)
|
|
switch kind {
|
|
case .add:
|
|
vDSP_vsaddD(b, 1, &s, c, 1, n) // s + rs == rs + s
|
|
case .multiply:
|
|
vDSP_vsmulD(b, 1, &s, c, 1, n) // commutative
|
|
case .subtract:
|
|
var negOne: Double = -1.0 // rs*(-1) + s == s - rs, bit-exact
|
|
vDSP_vsmsaD(b, 1, &negOne, &s, c, 1, n)
|
|
case .divide:
|
|
vDSP_svdivD(&s, b, 1, c, 1, n) // C[i] = s / rs[i] (true division)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `out[i] = ls[i] <op> rs[i]`. Caller guarantees both counts `>= count`.
|
|
@inline(__always)
|
|
func binaryOpVectorVector(
|
|
_ ls: [Double],
|
|
_ rs: [Double],
|
|
_ kind: BinaryOpKind,
|
|
into out: inout [Double],
|
|
count: Int
|
|
) {
|
|
ls.withUnsafeBufferPointer { lp in
|
|
rs.withUnsafeBufferPointer { rp in
|
|
out.withUnsafeMutableBufferPointer { op in
|
|
guard let a: UnsafePointer<Double> = lp.baseAddress,
|
|
let b: UnsafePointer<Double> = rp.baseAddress,
|
|
let c: UnsafeMutablePointer<Double> = op.baseAddress else { return }
|
|
let n: vDSP_Length = vDSP_Length(count)
|
|
switch kind {
|
|
case .add:
|
|
vDSP_vaddD(a, 1, b, 1, c, 1, n)
|
|
case .multiply:
|
|
vDSP_vmulD(a, 1, b, 1, c, 1, n)
|
|
case .subtract:
|
|
vDSP_vsubD(b, 1, a, 1, c, 1, n) // C = A - B = ls - rs (B first)
|
|
case .divide:
|
|
vDSP_vdivD(b, 1, a, 1, c, 1, n) // C = A / B = ls / rs (B first)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|