-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
less allocations, more tests, new api
- Loading branch information
1 parent
2b9be21
commit f3f52ce
Showing
13 changed files
with
437 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Accelerate | ||
import Foundation | ||
|
||
#if arch(x86_64) | ||
typealias Float16 = UInt16 | ||
#endif | ||
|
||
/// fast floating point conversion | ||
enum FPC { | ||
// MARK: Internal | ||
|
||
static func _Float32_Float16<T: AccelerateBuffer>(_ input: T) -> [Float16] where T.Element == Float { | ||
convert(input, vImageConvert_PlanarFtoPlanar16F) | ||
} | ||
|
||
static func _Float16_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == Float16 { | ||
convert(input, vImageConvert_Planar16FtoPlanarF) | ||
} | ||
|
||
static func _Int8_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == Int8 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
static func _Int16_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == Int16 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
static func _Int32_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == Int32 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
static func _UInt8_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == UInt8 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
static func _UInt16_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == UInt16 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
static func _UInt32_Float32<T: AccelerateBuffer>(_ input: T) -> [Float] where T.Element == UInt32 { | ||
convert(input, vDSP.convertElements) | ||
} | ||
|
||
// MARK: Private | ||
|
||
@_transparent | ||
private static func convert<U: AccelerateBuffer, V: Numeric>(_ input: U, _ body: (U, inout UnsafeMutableBufferPointer<V>) -> Void) -> [V] { | ||
.init(unsafeUninitializedCapacity: input.count) { buffer, initializedCount in | ||
body(input, &buffer) | ||
initializedCount = input.count | ||
} | ||
} | ||
|
||
private static func convert<U: AccelerateBuffer, V: Numeric>( | ||
_ input: U, | ||
_ body: (UnsafePointer<vImage_Buffer>, UnsafePointer<vImage_Buffer>, vImage_Flags) -> vImage_Error | ||
) -> [V] where U.Element: Numeric { | ||
var output = [V](repeating: 0, count: input.count) | ||
|
||
@_transparent | ||
func buffer<T>(of _: T.Type, pointer: UnsafeMutableRawPointer, count: Int) -> vImage_Buffer { | ||
.init(data: pointer, height: 1, width: UInt(count), rowBytes: count * MemoryLayout<T>.stride) | ||
} | ||
|
||
input.withUnsafeBufferPointer { inputPointer in | ||
output.withUnsafeMutableBufferPointer { outputPointer in | ||
var inputBuffer = buffer(of: U.self, pointer: .init(mutating: inputPointer.baseAddress!), count: inputPointer.count) | ||
var outputBuffer = buffer(of: V.self, pointer: .init(mutating: outputPointer.baseAddress!), count: outputPointer.count) | ||
|
||
_ = body(&inputBuffer, &outputBuffer, 0) | ||
} | ||
} | ||
|
||
return output | ||
} | ||
} |
Oops, something went wrong.