Skip to content

Commit 9b036a2

Browse files
committed
Add vDSP_vclrD, vDSP_vfillD
1 parent 8371d67 commit 9b036a2

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ The package is structured as follows:
5454
- [x] `vDSP_vsubD` - Subtract two vectors
5555
- [x] `vDSP_vmulD` - Multiply two vectors
5656
- [x] `vvpow` - Raise a vector to a power
57-
- [ ] `vDSP_vclrD` - Clear a vector
58-
- [ ] `vDSP_vfillD` - Fill a vector with a value
57+
- [x] `vDSP_vclrD` - Clear a vector
58+
- [x] `vDSP_vfillD` - Fill a vector with a value
5959
- [ ] `vDSP_vabsD` - Absolute value of a vector
6060
- [ ] `vDSP_vnegD` - Negate a vector
6161
- [ ] `vDSP_vsqD` - Square a vector

Sources/AccelerateLinux/VectorOps/VectorBasicOps.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,32 @@ public func vvpow(
151151
i += 1
152152
}
153153
}
154+
155+
/// Populates a double-precision vector with zeros.
156+
/// - Parameters:
157+
/// - __C: Double-precision real output vector.
158+
/// - __IC: Address stride for C.
159+
/// - __N: The number of elements to process.
160+
public func vDSP_vclrD(
161+
_ __C: UnsafeMutablePointer<Double>,
162+
_ __IC: vDSP_Stride,
163+
_ __N: vDSP_Length
164+
) {
165+
var zero: Double = 0
166+
vDSP_vfillD(&zero, __C, __IC, __N)
167+
}
168+
169+
/// Populates a double-precision vector with a specified scalar value.
170+
public func vDSP_vfillD(
171+
_ __A: UnsafePointer<Double>,
172+
_ __C: UnsafeMutablePointer<Double>,
173+
_ __IC: vDSP_Stride,
174+
_ __N: vDSP_Length
175+
) {
176+
var i = 0
177+
while i < __N {
178+
__C[i * __IC] = __A.pointee
179+
i += 1
180+
}
181+
}
154182
#endif

Tests/AccelerateLinuxTests/VectorTests/VectorBasicOpsTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AccelerateLinux
2+
import FoundationEssentials
23
import Testing
34

45
@Suite("Vector Basic Ops Tests")
@@ -130,4 +131,44 @@ struct VectorBasicOpsTests {
130131
vvpow(&z, &y, &x, &n)
131132
#expect(z == [9.0, 16.0, 1000.0, 36.0])
132133
}
134+
135+
// https://developer.apple.com/documentation/accelerate/1450639-vdsp_vclrd
136+
@Test("vDSP_vclrD")
137+
func vDSP_vclrDTest() {
138+
let n = vDSP_Length(10)
139+
let stride = vDSP_Stride(1)
140+
141+
var c = [Double](
142+
repeating: .nan,
143+
count: Int(n))
144+
145+
vDSP_vclrD(&c, stride, n)
146+
147+
#expect(c == [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
148+
}
149+
150+
// https://developer.apple.com/documentation/accelerate/1450171-vdsp_vfilld
151+
@Test("vDSP_vfillD")
152+
func vDSP_vfillDTest() {
153+
let n = vDSP_Length(10)
154+
let stride = vDSP_Stride(1)
155+
156+
var a = Double.pi
157+
158+
var c = [Double](
159+
repeating: .nan,
160+
count: Int(n))
161+
162+
vDSP_vfillD(&a, &c, stride, n)
163+
164+
#expect(
165+
c.map {
166+
($0 * 100).rounded() / 100
167+
} == [
168+
3.14, 3.14, 3.14,
169+
3.14, 3.14, 3.14,
170+
3.14, 3.14, 3.14,
171+
3.14,
172+
])
173+
}
133174
}

0 commit comments

Comments
 (0)