Skip to content

Commit 2b46fa9

Browse files
committed
Add vDSP_vabsD
1 parent 9b036a2 commit 2b46fa9

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The package is structured as follows:
5656
- [x] `vvpow` - Raise a vector to a power
5757
- [x] `vDSP_vclrD` - Clear a vector
5858
- [x] `vDSP_vfillD` - Fill a vector with a value
59-
- [ ] `vDSP_vabsD` - Absolute value of a vector
59+
- [x] `vDSP_vabsD` - Absolute value of a vector
6060
- [ ] `vDSP_vnegD` - Negate a vector
6161
- [ ] `vDSP_vsqD` - Square a vector
6262
- [ ] `vDSP_dotprD` - Dot product of two vectors

Sources/AccelerateLinux/VectorOps/VectorBasicOps.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#if canImport(Accelerate)
22
@_exported import Accelerate
33
#else
4-
import FoundationEssentials
54

65
/// Calculates the double-precision maximum value of a vector.
76
/// - Parameters:
@@ -179,4 +178,25 @@ public func vDSP_vfillD(
179178
i += 1
180179
}
181180
}
181+
182+
/// Calculates the absolute value of each element in the supplied double-precision vector using the specified stride.
183+
/// - Parameters:
184+
/// - __A: The input vector A.
185+
/// - __IA: The distance between the elements in the input vector A.
186+
/// - __C: On output, the absolute values of the elements in the input vector.
187+
/// - __IC: The distance between the elements in the output vector C.
188+
/// - __N: The number of elements that the function processes.
189+
public func vDSP_vabsD(
190+
_ __A: UnsafePointer<Double>,
191+
_ __IA: vDSP_Stride,
192+
_ __C: UnsafeMutablePointer<Double>,
193+
_ __IC: vDSP_Stride,
194+
_ __N: vDSP_Length
195+
) {
196+
var i = 0
197+
while i < __N {
198+
__C[i * __IC] = abs(__A[i * __IC])
199+
i += 1
200+
}
201+
}
182202
#endif

Tests/AccelerateLinuxTests/VectorTests/VectorBasicOpsTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,24 @@ struct VectorBasicOpsTests {
171171
3.14,
172172
])
173173
}
174+
175+
// https://developer.apple.com/documentation/accelerate/1449982-vdsp_vabsd
176+
@Test("vDSP_vabsD")
177+
func vDSP_vabsDTest() {
178+
let stride = 1
179+
180+
let values: [Double] = [-1, 2, -3, 4, -5, 6, -7, 8]
181+
182+
let absoluteValues = [Double](unsafeUninitializedCapacity: values.count) { buffer, initializedCount in
183+
vDSP_vabsD(
184+
values, stride,
185+
buffer.baseAddress!, stride,
186+
vDSP_Length(values.count)
187+
)
188+
189+
initializedCount = values.count
190+
}
191+
192+
#expect(absoluteValues == [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
193+
}
174194
}

0 commit comments

Comments
 (0)