Skip to content

Commit 2c1af7e

Browse files
committed
Implement WASM compatibility for the areas of the library where we can replace the usage of accelerate, core graphics and implement pure swift/c++ fallbacks or use CLAPACK. There are more changes we have to review, this commit is just to see if we need to iterate and check the CI work
1 parent 0c40e94 commit 2c1af7e

28 files changed

Lines changed: 4044 additions & 5 deletions

Sources/Matft/core/object/mfarray.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
13+
#if canImport(CoreML)
1114
import CoreML
15+
#endif
1216

1317
open class MfArray: MfArrayProtocol{
1418
public typealias MFDATA = MfData
@@ -111,6 +115,7 @@ open class MfArray: MfArrayProtocol{
111115
self.mfstructure = mfstructure//mfstructure will be copied because mfstructure is struct
112116
}
113117

118+
#if canImport(CoreML)
114119
/// Create a VIEW or Copy mfarray from MLShapedArray
115120
/// - Parameters:
116121
/// - base: A base MLShapedArray
@@ -126,6 +131,7 @@ open class MfArray: MfArrayProtocol{
126131
self.mfdata = mfdata
127132
self.mfstructure = MfStructure(shape: base.shape.map{ Int(truncating: $0) }, strides: base.strides.map{ Int(truncating: $0) })
128133
}
134+
#endif
129135

130136
deinit {
131137
self.base = nil

Sources/Matft/core/object/mftype.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
13+
#if canImport(CoreML)
1114
import CoreML
15+
#endif
1216

1317
public enum MfType: Int{
1418
case None
@@ -65,6 +69,7 @@ public enum MfType: Int{
6569
return MfType.mftype(value: value as Any)
6670
}
6771

72+
#if canImport(CoreML)
6873
@available(macOS 10.13, *)
6974
@available(iOS 14.0, *)
7075
static internal func mftype(value: MLMultiArrayDataType) -> MfType{
@@ -77,6 +82,7 @@ public enum MfType: Int{
7782
return .Object // Not supported
7883
}
7984
}
85+
#endif
8086

8187
static public func priority(_ a: MfType, _ b: MfType) -> MfType{
8288
if a.rawValue < b.rawValue{

Sources/Matft/core/protocol/mfdataProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
//
77

88
import Foundation
9+
#if canImport(CoreML)
910
import CoreML
11+
#endif
1012

1113
public protocol MfDataBasable {}
1214

1315
extension MfData: MfDataBasable{}
1416

17+
#if canImport(CoreML)
1518
@available(macOS 10.13, *)
1619
@available(iOS 14.0, *)
1720
extension MLMultiArray: MfDataBasable{}
21+
#endif

Sources/Matft/core/protocol/mftypeProtocol.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,53 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#else
13+
/// Fallback complex type for non-Apple platforms (split complex format for Float)
14+
public struct DSPSplitComplex {
15+
public var realp: UnsafeMutablePointer<Float>
16+
public var imagp: UnsafeMutablePointer<Float>
17+
18+
public init(realp: UnsafeMutablePointer<Float>, imagp: UnsafeMutablePointer<Float>) {
19+
self.realp = realp
20+
self.imagp = imagp
21+
}
22+
}
23+
24+
/// Fallback complex type for non-Apple platforms (split complex format for Double)
25+
public struct DSPDoubleSplitComplex {
26+
public var realp: UnsafeMutablePointer<Double>
27+
public var imagp: UnsafeMutablePointer<Double>
28+
29+
public init(realp: UnsafeMutablePointer<Double>, imagp: UnsafeMutablePointer<Double>) {
30+
self.realp = realp
31+
self.imagp = imagp
32+
}
33+
}
34+
35+
/// Fallback complex type for non-Apple platforms (interleaved complex format for Float)
36+
public struct DSPComplex {
37+
public var real: Float
38+
public var imag: Float
39+
40+
public init(real: Float, imag: Float) {
41+
self.real = real
42+
self.imag = imag
43+
}
44+
}
45+
46+
/// Fallback complex type for non-Apple platforms (interleaved complex format for Double)
47+
public struct DSPDoubleComplex {
48+
public var real: Double
49+
public var imag: Double
50+
51+
public init(real: Double, imag: Double) {
52+
self.real = real
53+
self.imag = imag
54+
}
55+
}
56+
#endif
1157
/*
1258
public protocol MfTypable: Numeric{}
1359

Sources/Matft/core/util/common/type.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,47 @@
66
//
77

88
import Foundation
9+
#if canImport(Accelerate)
910
import Accelerate
11+
#endif
12+
13+
#if !canImport(Accelerate)
14+
/// Pure Swift fallback for toBool_by_vDSP
15+
internal func toBool_by_vDSP(_ mfarray: MfArray) -> MfArray {
16+
let mfarray = check_contiguous(mfarray)
17+
let size = mfarray.storedSize
18+
let newdata = MfData(size: size, mftype: .Bool)
19+
20+
newdata.withUnsafeMutableStartPointer(datatype: Float.self) { dstptr in
21+
mfarray.withUnsafeMutableStartPointer(datatype: Float.self) { srcptr in
22+
for i in 0..<size {
23+
dstptr[i] = srcptr[i] != 0 ? Float(1) : Float(0)
24+
}
25+
}
26+
}
27+
28+
let newstructure = MfStructure(shape: mfarray.shape, strides: mfarray.strides)
29+
return MfArray(mfdata: newdata, mfstructure: newstructure)
30+
}
31+
32+
/// Pure Swift fallback for toIBool_by_vDSP
33+
internal func toIBool_by_vDSP(_ mfarray: MfArray) -> MfArray {
34+
let mfarray = check_contiguous(mfarray)
35+
let size = mfarray.storedSize
36+
let newdata = MfData(size: size, mftype: .Bool)
37+
38+
newdata.withUnsafeMutableStartPointer(datatype: Float.self) { dstptr in
39+
mfarray.withUnsafeMutableStartPointer(datatype: Float.self) { srcptr in
40+
for i in 0..<size {
41+
dstptr[i] = srcptr[i] == 0 ? Float(1) : Float(0)
42+
}
43+
}
44+
}
45+
46+
let newstructure = MfStructure(shape: mfarray.shape, strides: mfarray.strides)
47+
return MfArray(mfdata: newdata, mfstructure: newstructure)
48+
}
49+
#endif
1050

1151
internal func to_Bool(_ mfarray: MfArray, thresholdF: Float = 1e-5, thresholdD: Double = 1e-10) -> MfArray{
1252
//convert float and contiguous

Sources/Matft/core/util/pointer/ptr2array.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
1113

1214
//convert rawpointer to flattenarray via float or Double array
1315
//All kinds of int and uint has been handled as float

Sources/Matft/core/util/pointer/withptr.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//
77

88
import Foundation
9+
#if canImport(Accelerate)
910
import Accelerate
11+
#endif
1012

1113
extension MfArray{
1214

Sources/Matft/core/util/typeconversion.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
13+
// Note: WASI fallback implementations for vDSP functions are defined in vDSP.swift
1114

1215
/// Get mftype from a flatten array
1316
/// - Parameter flattenArray: Flatten array.

Sources/Matft/function/method/creation+method.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
1113

1214
extension MfArray{
1315
/**

Sources/Matft/function/method/subscript.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//
88

99
import Foundation
10+
#if canImport(Accelerate)
1011
import Accelerate
12+
#endif
1113

1214
extension MfArray: MfSubscriptable{
1315
public subscript(indices: Int...) -> Any{

0 commit comments

Comments
 (0)