Skip to content

Commit 6b1dbab

Browse files
committed
Make some changes to make code compatible with WASM
1 parent 4ff93ea commit 6b1dbab

48 files changed

Lines changed: 4238 additions & 46 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/swift.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
- uses: actions/checkout@v4
1515
- uses: swift-actions/setup-swift@v2
1616
with:
17-
swift-version: "6.0.3"
17+
swift-version: "6.1"
1818
- name: Build and Test
1919
run: ./scripts/build-and-test-ios.sh

.github/workflows/wasm.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ jobs:
1414
- name: Install Swift
1515
uses: swift-actions/setup-swift@v2
1616
with:
17-
swift-version: "6.0.3"
17+
swift-version: "6.1"
1818

1919
# Wasmtime is required because `swift test` doesn't work for WebAssembly targets.
2020
# For WASM, we must build tests separately and run them with a WASM runtime.
2121
# See: https://book.swiftwasm.org/getting-started/testing.html
2222
- name: Install Wasmtime
23-
run: |
24-
curl https://wasmtime.dev/install.sh -sSf | bash
25-
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH
23+
uses: bytecodealliance/actions/wasmtime/setup@v1
24+
with:
25+
version: "29.0.1"
26+
github_token: ${{ github.token }}
2627

2728
- name: Build and Test
2829
run: ./scripts/build-and-test-wasm.sh

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,10 @@ This script will:
893893

894894
**Note:** The WASM script automatically handles SDK and runtime installation, so you can run it on a fresh machine without any prior setup!
895895

896-
### 📋 Requirements
896+
### Requirements
897897

898-
- **iOS/macOS:** Swift 6.0.3 or later
899-
- **WebAssembly:** Swift 6.0.3 or later (SDK will be automatically installed)
898+
- **iOS/macOS:** Swift 6.1 or later
899+
- **WebAssembly:** Swift 6.1 or later (SDK will be automatically installed)
900900

901901
## Contact
902902

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/mfdata.swift

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

99
import Foundation
10-
import Accelerate
1110

1211
internal enum MfDataSource{
1312
case mfdata
@@ -106,18 +105,18 @@ public class MfData: MfDataProtocol{
106105

107106
if let data_imag_ptr = data_imag_ptr{
108107
self.data_imag = allocate_unsafeMRPtr(type: Float.self, count: storedSize)
109-
memcpy(self.data_imag, data_imag_ptr, self.storedByteSize)
108+
memcpy(self.data_imag!, data_imag_ptr, self.storedByteSize)
110109
}
111110
else{
112111
self.data_imag = nil
113112
}
114113
case .Double:
115114
self.data_real = allocate_unsafeMRPtr(type: Double.self, count: storedSize)
116115
memcpy(self.data_real, data_real_ptr, self.storedByteSize)
117-
116+
118117
if let data_imag_ptr = data_imag_ptr{
119118
self.data_imag = allocate_unsafeMRPtr(type: Double.self, count: storedSize)
120-
memcpy(self.data_imag, data_imag_ptr, self.storedByteSize)
119+
memcpy(self.data_imag!, data_imag_ptr, self.storedByteSize)
121120
}
122121
else{
123122
self.data_imag = 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/order.swift

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

99
import Foundation
10-
import Accelerate
1110

1211

1312
/// Copy mfarray including structure
@@ -36,7 +35,7 @@ internal func copy_all_mfarray(_ src_mfarray: MfArray) -> MfArray{
3635
srcptr in
3736
dst_mfarray.withUnsafeMutableStartImagPointer(datatype: Float.self){
3837
dstptr in
39-
memcpy(dstptr, srcptr, MemoryLayout<Float>.size*newsize)
38+
memcpy(dstptr!, srcptr, MemoryLayout<Float>.size*newsize)
4039
}
4140
}
4241
}
@@ -53,7 +52,7 @@ internal func copy_all_mfarray(_ src_mfarray: MfArray) -> MfArray{
5352
srcptr in
5453
dst_mfarray.withUnsafeMutableStartImagPointer(datatype: Double.self){
5554
dstptr in
56-
memcpy(dstptr, srcptr, MemoryLayout<Double>.size*newsize)
55+
memcpy(dstptr!, srcptr, MemoryLayout<Double>.size*newsize)
5756
}
5857
}
5958
}

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

0 commit comments

Comments
 (0)