在C++中将整数转换为16位浮点数(半精度浮点数)。

7

如何将一个 整数 转换为 半精度浮点数(存储到一个 unsigned char[2] 数组中)。输入的整数范围为1-65535,精度并不重要。

我正在做类似于将 16位整数 转换为 unsigned char[2] 的事情,但我了解到 C++ 没有 半精度浮点数 数据类型。以下是示例:

int16_t position16int = (int16_t)data;
memcpy(&dataArray, &position16int, 2);

2
可能相关:http://gamedev.stackexchange.com/a/17410/9333 - slaphappy
可能有所帮助,来自 Goz 的提示:32 位转 16 位浮点数转换 - Christian Ammer
4个回答

5

这是一个非常简单的事情,你需要的所有信息都在Wikipedia网站上。

参考实现:

#include <stdio.h>

unsigned int2hfloat(int x)
{
  unsigned sign = x < 0;
  unsigned absx = ((unsigned)x ^ -sign) + sign; // safe abs(x)
  unsigned tmp = absx, manbits = 0;
  int exp = 0, truncated = 0;

  // calculate the number of bits needed for the mantissa
  while (tmp)
  {
    tmp >>= 1;
    manbits++;
  }

  // half-precision floats have 11 bits in the mantissa.
  // truncate the excess or insert the lacking 0s until there are 11.
  if (manbits)
  {
    exp = 10; // exp bias because 1.0 is at bit position 10
    while (manbits > 11)
    {
      truncated |= absx & 1;
      absx >>= 1;
      manbits--;
      exp++;
    }
    while (manbits < 11)
    {
      absx <<= 1;
      manbits++;
      exp--;
    }
  }

  if (exp + truncated > 15)
  {
    // absx was too big, force it to +/- infinity
    exp = 31; // special infinity value
    absx = 0;
  }
  else if (manbits)
  {
    // normal case, absx > 0
    exp += 15; // bias the exponent
  }

  return (sign << 15) | ((unsigned)exp << 10) | (absx & ((1u<<10)-1));
}

int main(void)
{
  printf(" 0: 0x%04X\n", int2hfloat(0));
  printf("-1: 0x%04X\n", int2hfloat(-1));
  printf("+1: 0x%04X\n", int2hfloat(+1));
  printf("-2: 0x%04X\n", int2hfloat(-2));
  printf("+2: 0x%04X\n", int2hfloat(+2));
  printf("-3: 0x%04X\n", int2hfloat(-3));
  printf("+3: 0x%04X\n", int2hfloat(+3));
  printf("-2047: 0x%04X\n", int2hfloat(-2047));
  printf("+2047: 0x%04X\n", int2hfloat(+2047));
  printf("-2048: 0x%04X\n", int2hfloat(-2048));
  printf("+2048: 0x%04X\n", int2hfloat(+2048));
  printf("-2049: 0x%04X\n", int2hfloat(-2049)); // first inexact integer
  printf("+2049: 0x%04X\n", int2hfloat(+2049));
  printf("-2050: 0x%04X\n", int2hfloat(-2050));
  printf("+2050: 0x%04X\n", int2hfloat(+2050));
  printf("-32752: 0x%04X\n", int2hfloat(-32752));
  printf("+32752: 0x%04X\n", int2hfloat(+32752));
  printf("-32768: 0x%04X\n", int2hfloat(-32768));
  printf("+32768: 0x%04X\n", int2hfloat(+32768));
  printf("-65504: 0x%04X\n", int2hfloat(-65504)); // legal maximum
  printf("+65504: 0x%04X\n", int2hfloat(+65504));
  printf("-65505: 0x%04X\n", int2hfloat(-65505)); // infinity from here on
  printf("+65505: 0x%04X\n", int2hfloat(+65505));
  printf("-65535: 0x%04X\n", int2hfloat(-65535));
  printf("+65535: 0x%04X\n", int2hfloat(+65535));
  return 0;
}

输出结果 (ideone):

 0: 0x0000
-1: 0xBC00
+1: 0x3C00
-2: 0xC000
+2: 0x4000
-3: 0xC200
+3: 0x4200
-2047: 0xE7FF
+2047: 0x67FF
-2048: 0xE800
+2048: 0x6800
-2049: 0xE800
+2049: 0x6800
-2050: 0xE801
+2050: 0x6801
-32752: 0xF7FF
+32752: 0x77FF
-32768: 0xF800
+32768: 0x7800
-65504: 0xFBFF
+65504: 0x7BFF
-65505: 0xFC00
+65505: 0x7C00
-65535: 0xFC00
+65535: 0x7C00

1
@UmNyobe 如果你掌握足够的学校数学知识,这就很简单了。 - Alexey Frunze
1
需要注意的是,此代码具有异常的舍入行为。大多数情况下,它会截断而不是四舍五入到最近的值(这更常见),但在上限处是异常的。大于可表示有限值的最大输入将转换为无穷大,而不像其他输入一样被截断,即使它们只比最大值略大。例如,0xfff(4095)转换为0x6bff(4094),但0xfff0(65520)或0xffe1(65505)转换为0x7c00(无穷大),而不是0x7bff(65504)。 - Eric Postpischil
3
@EricPostpischil 您说得对。但这是我对“精度真的不重要”这句话的理解。 - Alexey Frunze
@EricPostpischil 我该如何使用银行家舍入法来实现相同的函数? - envy grunt

2
我提出了如何将32位浮点数转换为16位浮点数的问题。 Float32 to Float16 因此,您可以很容易地将int转换为float,然后使用上面的问题创建16位浮点数。我建议这可能比直接从int转换为16位浮点数要容易得多。实际上,通过转换为32位浮点数,您已经完成了大部分的工作,然后只需要移动一些位即可。
编辑:查看Alexey的出色答案后,我认为使用硬件int到float转换,然后将其位移可能比他的方法快得多。值得对两种方法进行剖析和比较。

0

根据@kbok的问题评论,我使用了this answer的第一部分来获取半浮点数,然后获取数组:

uint16_t position16float = float_to_half_branch(data);
memcpy(&dataArray, &position16float, 2);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接