Float32与Float16转换
Posted jebediahkerman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Float32与Float16转换相关的知识,希望对你有一定的参考价值。
参考https://stackoverflow.com/questions/3026441/float32-to-float16
#include <cstdio> #include <cstdint> using namespace std; using float16_t = uint16_t; struct Float16 { float16_t _value; Float16(float16_t value) { _value = value; } Float16(float value) { uint32_t m = *(uint32_t *)&value; _value = ((m & 0x7fffffff) >> 13) - (0x38000000 >> 13); _value |= ((m & 0x80000000) >> 16); } operator float16_t () { return _value; } operator float () { uint32_t m = _value; m = ((m & 0x7fff) << 13) + 0x38000000; m |= ((_value & 0x8000) << 16); return *(float *)&m; } };
以上是关于Float32与Float16转换的主要内容,如果未能解决你的问题,请参考以下文章