Skip to content

Commit 19d4987

Browse files
committed
Start adding vDSP namespace methods
1 parent f4d913f commit 19d4987

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if canImport(Accelerate)
2+
@_exported import Accelerate
3+
#else
4+
5+
extension vDSP {
6+
/// Returns the double-precision element-wise sum of a vector and a scalar value.
7+
/// - Parameters:
8+
/// - scalar: The input scalar value, B.
9+
/// - vector: The input vector, A.
10+
/// - Returns: The output vector, C.
11+
public static func add<U>(
12+
_ scalar: Double,
13+
_ vector: U
14+
) -> [Double] where U: AccelerateBuffer, U.Element == Double {
15+
.init(unsafeUninitializedCapacity: vector.count) { buffer, initializedCount in
16+
vector.withUnsafeBufferPointer { vecPtr in
17+
var i = 0
18+
while i < vector.count {
19+
buffer[i] = scalar * vecPtr[i] + vecPtr[i]
20+
i += 1
21+
}
22+
initializedCount = vector.count
23+
}
24+
}
25+
}
26+
}
27+
28+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#if canImport(Accelerate)
2+
@_exported import Accelerate
3+
#else
4+
/// An enumeration that acts as a namespace for Swift overlays to vDSP.
5+
public enum vDSP {}
6+
#endif

Sources/AccelerateLinux/vDSPTypes.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,54 @@
33
public typealias vDSP_Length = UInt
44
/// An integer value that represents the differences between indices of elements, including the lengths of strides.
55
public typealias vDSP_Stride = Int
6+
7+
/// A type that represents an immutable buffer.
8+
///
9+
/// If you implement your own type that conforms to ``AccelerateBuffer`` and uses the default implementation of ``withUnsafeBufferPointer(_:)``,
10+
/// your type needs to return a nonnil result from `withContiguousStorageIfAvailable(_:)`.
11+
// https://developer.apple.com/documentation/accelerate/acceleratebuffer
12+
public protocol AccelerateBuffer<Element> {
13+
/// The buffer’s element type.
14+
associatedtype Element
15+
/// The number of elements in the buffer.
16+
var count: Int { get }
17+
/// Calls a closure with a pointer to the object’s contiguous storage.
18+
/// - Parameter body: A closure that receives an UnsafeBufferPointer to the sequence’s contiguous storage.
19+
func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Self.Element>) throws -> R) rethrows -> R
20+
}
21+
22+
// https://developer.apple.com/documentation/accelerate/acceleratebuffer/3240656-withunsafebufferpointer
23+
extension AccelerateBuffer where Self: Collection {
24+
public func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Self.Element>) throws -> R) rethrows -> R {
25+
try self.withContiguousStorageIfAvailable(body)!
26+
}
27+
}
28+
29+
extension Slice: AccelerateBuffer where Base: AccelerateBuffer {}
30+
31+
/// A type that represents a mutable buffer.
32+
///
33+
/// If you implement your own type that conforms to `AccelerateMutableBuffer` and uses the default implementation of ``withUnsafeMutableBufferPointer(_:)``,
34+
/// your type needs to return a nonnil result from `withContiguousMutableStorageIfAvailable(_:)``.
35+
// https://developer.apple.com/documentation/accelerate/acceleratemutablebuffer
36+
public protocol AccelerateMutableBuffer<Element>: AccelerateBuffer {
37+
/// Calls the given closure with a pointer to the object’s mutable contiguous storage.
38+
/// - Parameter body: A closure that receives an UnsafeMutableBufferPointer to the sequence’s contiguous storage.
39+
mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Self.Element>) throws -> R) rethrows -> R
40+
}
41+
42+
// https://developer.apple.com/documentation/accelerate/acceleratemutablebuffer/3240659-withunsafemutablebufferpointer
43+
extension AccelerateMutableBuffer where Self: MutableCollection {
44+
public mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Self.Element>) throws -> R) rethrows
45+
-> R
46+
{
47+
try self.withContiguousMutableStorageIfAvailable(body)!
48+
}
49+
}
50+
51+
extension Array: AccelerateMutableBuffer {}
52+
extension ArraySlice: AccelerateMutableBuffer {}
53+
extension ContiguousArray: AccelerateMutableBuffer {}
54+
extension Slice: AccelerateMutableBuffer where Base: AccelerateMutableBuffer & MutableCollection {}
55+
extension UnsafeMutableBufferPointer: AccelerateMutableBuffer {}
656
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import AccelerateLinux
2+
import Testing
3+
4+
@Suite("vDSP+add Test")
5+
struct vDSPaddTests {
6+
@Test("vDSP.add scalar + vector")
7+
func vDSPaddScalarVector() {
8+
let a: [Double] = [1, 2, 3, 4, 5]
9+
let b: Double = 10
10+
11+
let c = vDSP.add(b, a)
12+
13+
#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
14+
}
15+
}

0 commit comments

Comments
 (0)