Skip to content

Commit b7e0792

Browse files
committed
Add vDSP_vaddD, vDSP_vsubD
1 parent fe6878f commit b7e0792

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ The package is structured as follows:
5050
- `BasicOps` - Basic vector operations:
5151
- [x] `vDSP_maxvD` - Find the maximum value in a vector
5252
- [x] `vDSP_minvD` - Find the minimum value in a vector
53-
- [ ] `vDSP_vaddD` - Add two vectors
54-
- [ ] `vDSP_vsubD` - Subtract two vectors
53+
- [x] `vDSP_vaddD` - Add two vectors
54+
- [x] `vDSP_vsubD` - Subtract two vectors
5555
- [ ] `vDSP_vmulD` - Multiply two vectors
5656
- [ ] `vvpow` - Raise a vector to a power
5757
- [ ] `vDSP_vclrD` - Clear a vector

Sources/AccelerateLinux/VectorOps/VectorBasicOps.swift

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ public func vDSP_maxvD(
1616
) {
1717
__C.pointee = -Double.infinity
1818
guard __N > 0 else { return }
19-
for i in 1..<__N {
19+
var i = 1
20+
while i < __N {
2021
__C.pointee = max(__C.pointee, __A[Int(i) * __I])
22+
i += 1
2123
}
2224
}
2325

@@ -35,8 +37,60 @@ public func vDSP_minvD(
3537
) {
3638
__C.pointee = Double.infinity
3739
guard __N > 0 else { return }
38-
for i in 1..<__N {
40+
var i = 1
41+
while i < __N {
3942
__C.pointee = min(__C.pointee, __A[Int(i) * __I])
43+
i += 1
44+
}
45+
}
46+
47+
/// Calculates the double-precision element-wise sum of two vectors, using the specified stride.
48+
/// - Parameters:
49+
/// - __A: The first input vector, A.
50+
/// - __IA: The distance between the elements in the first input vector.
51+
/// - __B: The second input vector, B.
52+
/// - __IB: The distance between the elements in the second input vector.
53+
/// - __C: The output vector, C.
54+
/// - __IC: The distance between the elements in the output vector.
55+
/// - __N: The number of elements that the function processes.
56+
public func vDSP_vaddD(
57+
_ __A: UnsafePointer<Double>,
58+
_ __IA: vDSP_Stride,
59+
_ __B: UnsafePointer<Double>,
60+
_ __IB: vDSP_Stride,
61+
_ __C: UnsafeMutablePointer<Double>,
62+
_ __IC: vDSP_Stride,
63+
_ __N: vDSP_Length
64+
) {
65+
var i = 0
66+
while i < __N {
67+
__C[Int(i) * __IC] = __A[Int(i) * __IA] + __B[Int(i) * __IB]
68+
i += 1
69+
}
70+
}
71+
72+
/// Calculates the double-precision element-wise subtraction of two vectors, using the specified stride.
73+
/// - Parameters:
74+
/// - __B: The first input vector, B.
75+
/// - __IB: The distance between the elements in the first input vector.
76+
/// - __A: The second input vector, A.
77+
/// - __IA: The distance between the elements in the second input vector.
78+
/// - __C: The output vector, C.
79+
/// - __IC: The distance between the elements in the output vector.
80+
/// - __N: The number of elements that the function processes.
81+
public func vDSP_vsubD(
82+
_ __B: UnsafePointer<Double>,
83+
_ __IB: vDSP_Stride,
84+
_ __A: UnsafePointer<Double>,
85+
_ __IA: vDSP_Stride,
86+
_ __C: UnsafeMutablePointer<Double>,
87+
_ __IC: vDSP_Stride,
88+
_ __N: vDSP_Length
89+
) {
90+
var i = 0
91+
while i < __N {
92+
__C[Int(i) * __IC] = __A[Int(i) * __IA] - __B[Int(i) * __IB]
93+
i += 1
4094
}
4195
}
4296
#endif

Tests/AccelerateLinuxTests/VectorTests/VectorBasicOpsTests.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,57 @@ struct VectorBasicOpsTests {
4747
vDSP_minvD(a, stride, &c, n)
4848
#expect(c == Double.infinity)
4949
}
50+
51+
// https://developer.apple.com/documentation/accelerate/1449910-vdsp_vaddd
52+
@Test("vaddD")
53+
func vDSP_vaddDTest() {
54+
let stride = 1
55+
let count = 5
56+
57+
let a: [Double] = [1, 2, 3, 4, 5]
58+
let b: [Double] = [10, 20, 30, 40, 50]
59+
60+
let c = [Double](unsafeUninitializedCapacity: count) { buffer, initializedCount in
61+
vDSP_vaddD(
62+
a,
63+
stride,
64+
b,
65+
stride,
66+
buffer.baseAddress!,
67+
stride,
68+
vDSP_Length(count)
69+
)
70+
initializedCount = count
71+
}
72+
73+
#expect(c == [11.0, 22.0, 33.0, 44.0, 55.0])
74+
}
75+
76+
// https://developer.apple.com/documentation/accelerate/1449743-vdsp_vsubd
77+
@Test("vsubD")
78+
func vDSP_vsubDTest() {
79+
let stride = 1
80+
let count = 5
81+
82+
let a: [Double] = [10, 20, 30, 40, 50]
83+
let b: [Double] = [1, 2, 3, 4, 5]
84+
85+
let c = [Double](unsafeUninitializedCapacity: count) {
86+
buffer, initializedCount in
87+
88+
vDSP_vsubD(
89+
b,
90+
stride,
91+
a,
92+
stride,
93+
buffer.baseAddress!,
94+
stride,
95+
vDSP_Length(count)
96+
)
97+
98+
initializedCount = count
99+
}
100+
101+
#expect(c == [9.0, 18.0, 27.0, 36.0, 45.0])
102+
}
50103
}

0 commit comments

Comments
 (0)