|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import Foundation |
| 9 | +#if canImport(Accelerate) |
9 | 10 | 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 |
10 | 50 |
|
11 | 51 | internal func to_Bool(_ mfarray: MfArray, thresholdF: Float = 1e-5, thresholdD: Double = 1e-10) -> MfArray{ |
12 | 52 | //convert float and contiguous |
|
0 commit comments