Skip to content

Commit 3d8429d

Browse files
committed
Add vDSP_vrsumD
1 parent 79c303d commit 3d8429d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The package is structured as follows:
6262
- [x] `vDSP_dotprD` - Dot product of two vectors
6363
- [x] `vDSP_vlimD` - Limit a vector to a range
6464
- [c] `vDSP_vclipcD` - Clip a vector to a range
65-
- [ ] `vDSP_vrsumD` - Recursive sum of a vector
65+
- [x] `vDSP_vrsumD` - Recursive sum of a vector
6666
- [ ] `vDSP_vsortD` - Sort a vector
6767
- [ ] `vDSP_vrampD` - Ramp a vector
6868
- [ ] `vDSP.sum` - Sum of a vector

Sources/AccelerateLinux/VectorOps/VectorBasicOps.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,40 @@ public func vDSP_vclipcD(
313313
i += 1
314314
}
315315
}
316+
317+
/// Performs running sum integration over a double-precision vector.
318+
/// - Parameters:
319+
/// - __A: Double-precision real input vector.
320+
/// - __IA: Address stride for A.
321+
/// - __S: Points to double-precision real input scalar: weighting factor.
322+
/// - __C: Double-precision real output vector.
323+
/// - __IC: Stride for C
324+
/// - __N: The number of elements to process.
325+
/// Integrates vector A using a running sum from vector C.
326+
/// Vector A is weighted by scalar *S and added to the previous output point.
327+
/// The first element from vector A is not used in the sum.
328+
public func vDSP_vrsumD(
329+
_ __A: UnsafePointer<Double>,
330+
_ __IA: vDSP_Stride,
331+
_ __S: UnsafePointer<Double>,
332+
_ __C: UnsafeMutablePointer<Double>,
333+
_ __IC: vDSP_Stride,
334+
_ __N: vDSP_Length
335+
) {
336+
@discardableResult
337+
func aux(
338+
_ __A: UnsafePointer<Double>,
339+
_ __IA: vDSP_Stride,
340+
_ __S: UnsafePointer<Double>,
341+
_ __C: UnsafeMutablePointer<Double>,
342+
_ __IC: vDSP_Stride,
343+
_ __N: vDSP_Length
344+
) -> Double {
345+
if __N == 0 { return 0 }
346+
__C[Int(__N) * __IC] += (__S.pointee * __A[Int(__N) * __IA]) + aux(__A, __IA, __S, __C, __IC, __N - 1)
347+
return __C[Int(__N) * __IC]
348+
}
349+
350+
aux(__A, __IA, __S, __C, __IC, __N)
351+
}
316352
#endif

Tests/AccelerateLinuxTests/VectorTests/VectorBasicOpsTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,22 @@ struct VectorBasicOpsTests {
267267
#expect(nLow == 2)
268268
#expect(nHigh == 1)
269269
}
270+
271+
@Test("vDSP_vrsum")
272+
func vDSP_vrsumTest() {
273+
let stride = 1
274+
let n = 4
275+
276+
let a: [Double] = [1.0, 2.0, 3.0, 4.0]
277+
var startValue: Double = 10.0
278+
var c = [Double](repeating: 0.0, count: 4)
279+
280+
vDSP_vrsumD(
281+
a, stride,
282+
&startValue,
283+
&c, stride,
284+
vDSP_Length(n))
285+
286+
#expect(c == [0.0, 20.0, 50.0, 90.0])
287+
}
270288
}

0 commit comments

Comments
 (0)