java 半精度浮点数,将半精度浮点数(字节)转换为Swift中的浮点数

I would like to be able to read in half floats from a binary file and convert them to a float in Swift. I've looked at several conversions from other languages such as Java and C#, however I have not been able to get the correct value corresponding to the half float. If anyone could help me with an implementation I would appreciate it. A conversion from Float to Half Float would also be extremely helpful. Here's an implementation I attempted to convert from this Java implementation.

static func toFloat(value: UInt16) -> Float {

let value = Int32(value)

var mantissa = Int32(value) & 0x03ff

var exp: Int32 = Int32(value) & 0x7c00

if(exp == 0x7c00) {

exp = 0x3fc00

} else if exp != 0 {

exp += 0x1c000

if(mantissa == 0 && exp > 0x1c400) {

return Float((value & 0x8000) << 16 | exp << 13 | 0x3ff)

}

} else if mantissa != 0 {

exp = 0x1c400

repeat {

mantissa << 1

exp -= 0x400

} while ((mantissa & 0x400) == 0)

mantissa &= 0x3ff

}

return Float((value & 0x80000) << 16 | (exp | mantissa) << 13)

}

解决方案

If you have an array of half-precision data, you can convert all of it to float at once using vImageConvert_Planar16FtoPlanarF, which is provided by Accelerate.framework:

import Accelerate

let n = 2

var input: [UInt16] = [ 0x3c00, 0xbc00 ]

var output = [Float](count: n, repeatedValue: 0)

var src = vImage_Buffer(data:&input, height:1, width:UInt(n), rowBytes:2*n)

var dst = vImage_Buffer(data:&output, height:1, width:UInt(n), rowBytes:4*n)

vImageConvert_Planar16FtoPlanarF(&src, &dst, 0)

// output now contains [1.0, -1.0]

You can also use this method to convert individual values, but it's fairly heavyweight if that's all that you're doing; on the other hand it's extremely efficient if you have large buffers of values to convert.

If you need to convert isolated values, you might put something like the following C function in your bridging header and use it from Swift:

#include

static inline float loadFromF16(const uint16_t *pointer) { return *(const __fp16 *)pointer; }

This will use hardware conversion instructions when you're compiling for targets that have them (armv7s, arm64, x86_64h), and call a reasonably good software conversion routine when compiling for targets that don't have hardware support.

addendum: going the other way

You can convert float to half-precision in pretty much the same way:

static inline storeAsF16(float value, uint16_t *pointer) { *(const __fp16 *)pointer = value; }

Or use the function vImageConvert_PlanarFtoPlanar16F.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值