将半精度浮点数(字节)转换为Swift中的浮点数 [英] Convert half precision float (bytes) to float in Swift

查看:362
本文介绍了将半精度浮点数(字节)转换为Swift中的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从二进制文件中读取一半的浮点数,并将其转换为Swift中的浮点数.我看过其他语言(例如Java和C#)的几种转换,但是我无法获得对应于半浮点数的正确值.如果有人可以帮助我实现,我将不胜感激.从Float转换为Half Float也将非常有帮助.这是我尝试从此 Java实现转换而成的实现.

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)
}

推荐答案

如果您具有半精度数据数组,则可以使用Accelerate.framework提供的vImageConvert_Planar16FtoPlanarF将所有数据一次转换为浮点型. :

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.

如果您需要转换隔离的值,则可以在桥接标头中放入类似以下C函数的内容,并从Swift中使用它:

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 <stdint.h>
static inline float loadFromF16(const uint16_t *pointer) { return *(const __fp16 *)pointer; }

当您为具有目标的目标(armv7s,arm64,x86_64h)进行编译时,这将使用硬件转换指令,并在为不具有硬件支持的目标进行编译时调用相当好的软件转换例程.

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.

附录:另辟

您可以使用几乎相同的方式将float转换为半精度:

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; }

或使用功能vImageConvert_PlanarFtoPlanar16F.

这篇关于将半精度浮点数(字节)转换为Swift中的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆