G Alexander G Alexander - 16 days ago 6
Swift Question

How to call a DSP function with OpaquePointer?

From Vector-Scalar Linear Algebra Functions (from vectorOps.h)
https://developer.apple.com/reference/accelerate/1546030-visamax


func vIsamax(_ count: Int32,
_ x: OpaquePointer) -> Int32)


count
Number of elements in the vector x; must be a multiple of 4.

x A vector array of float values.

i've tried solutions from
https://swift.org/migration-guide/se-0107-migrate.html
UnsafeRawPointer
,
withUnsafePointer
,
MemoryLayout
, etc
as of yet no success

cannot invoke initializer for type 'OpaquePointer' with ... CFData,

cannot invoke " " with... yada, yada, yada

How to call a DSP function with OpaquePointer?

Answer

vIsamax takes a vector of vFloat aka float4 elements as argument. (Each float4 holds 4 floating point numbers.) The C declaration is

int32_t vIsamax(int32_t count, const vFloat *x);

which should be mapped to Swift as

public func vIsamax(_ count: Int32, _ x: UnsafePointer<vFloat>) -> Int32

instead of

public func vIsamax(_ count: Int32, _ x: OpaquePointer) -> Int32

But you can simply pass an vFloat/float4 array, as shown in this small example:

import Swift
import Accelerate
import simd

let values = [ float4(-1.0, -2.0, -3.0, -4.0), float4(4.0, 3.0, 2.0, 1.0) ]
let idx = vIsamax(Int32(4 * values.count), values)
print(idx) // 3