Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting parameters to make Swift compile with vDSP API

I am running into some issues trying to use the Accelerate framework with vDSP API from Swift. Obviously I am doing something wrong although the compiler gives me all sorts of warnings

var srcAsFloat:CConstPointer<CFloat> = CFloat[](count: Int(width*height), repeatedValue: 0)
var dstAsFloat = CFloat[](count: Int(width*height), repeatedValue: 0)

if shouldClip {
    var min:CFloat = 0.0
    var max:CFloat = 255.0
    var l:vDSP_Stride = Int(width*height)
    vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat), vDSP_Stride(1), CConstPointer<CFloat>(&min), CConstPointer<CFloat>(&max), CMutablePointer<CFloat>(&dstAsFloat), vDSP_Stride(1), l)
}

The error:

error: could not find an overload for 'init' that accepts the supplied arguments 
    vDSP_vclip(CConstPointer<CFloat>(&dstAsFloat), 
    vDSP_Stride(1), 
    CConstPointer<CFloat>(&min), 
    CConstPointer<CFloat>(&max), 
    CMutablePointer<CFloat>(&dstAsFloat), 
    vDSP_Stride(1), 
    l) – 

I've tried to cast the heck out of it but so far, no luck.

like image 884
John Difool Avatar asked Nov 01 '22 22:11

John Difool


1 Answers

Okay, I figured out a (probably sub-optimal) solution. I decided to bridge to Objective-C (and used a slightly different function).

main.swift

import Foundation
func abs(x: matrix) -> matrix{
    let N = x.count
    var arg1 = NSArray(array: x)

    var yy = abs_objc(arg1, CInt(N))

    var y = zeros(N)
    for i in 0..N{
        y[i] = Double(yy[i])
    }
    return y

abs_objc.m

#import <Accelerate/Accelerate.h>
double* abs_objc(NSArray * x, int N){
    // converting input to double *
    double * xx = (double *)malloc(sizeof(double) * N);
    for (int i=0; i<[x count]; i++) {
        xx[i] = [[x objectAtIndex:i] doubleValue];
    }

    // init'ing output
    double * y = (double *)malloc(sizeof(double) * N);
    for (int i=0; i<N; i++){
        y[i] = 0;
    }

    vDSP_vabsD(xx,1,y,1,N);

    return y;
}

swix-Bridging-Header.h

#import <Foundation/Foundation.h>
double* abs_objc(NSArray * x, int N);
like image 103
Scott Avatar answered Nov 13 '22 03:11

Scott