Skip to content

Instantly share code, notes, and snippets.

@danielctull
Created April 20, 2018 08:58
Show Gist options
  • Save danielctull/b8adc590b7980b00ac9f6a66504cfbfc to your computer and use it in GitHub Desktop.
Save danielctull/b8adc590b7980b00ac9f6a66504cfbfc to your computer and use it in GitHub Desktop.
import Foundation
import Accelerate
struct Float16 {
fileprivate var value: UInt16
init(_ float: Float) {
var input = [float]
var output: [UInt16] = [0]
var source = vImage_Buffer(data: &input, height: 1, width: 1, rowBytes: MemoryLayout<Float>.size)
var destination = vImage_Buffer(data: &output, height: 1, width: 1, rowBytes: MemoryLayout<UInt16>.size)
vImageConvert_PlanarFtoPlanar16F(&source, &destination, 0)
value = output[0]
}
init(_ uint16: UInt16) {
value = uint16
}
}
extension Float {
fileprivate init(_ float16: Float16) {
var input = [float16.value]
var output: [Float] = [0]
var sourceBuffer = vImage_Buffer(data: &input, height: 1, width: 1, rowBytes: MemoryLayout<UInt16>.size)
var destinationBuffer = vImage_Buffer(data: &output, height: 1, width: 1, rowBytes: MemoryLayout<Float>.size)
vImageConvert_Planar16FtoPlanarF(&sourceBuffer, &destinationBuffer, 0)
self = output[0]
}
}
extension Float16: CustomStringConvertible {
var description: String {
return Float(self).description
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment