使用舍入到偶数将整数转换为半精度浮点格式

Convert integer to half-precision floating point format using round-to-even

我已经知道如何使用截断实现到半精度浮点数的转换(感谢this answer)。但是我怎样才能使用最接近的可表示的舍入来进行相同的转换呢?例如,我希望 65519 舍入为 0x7bff(即 65504),而不是无穷大。再举一个例子:在链接的解决方案中,8199 将由 8192 表示,但最接近 8199 的表示是 8200

UPD: 更多示例案例:我想将 32768 和 65519 之间的整数舍入为 32 的倍数,将 16384 和 32768 之间的整数舍入为 16 的倍数等等在。在此解决方案中,8199 将由 8192 表示,但最接近 8199 的表示是 8200

你需要两件才能达到你想要的效果。

1.在进行转换之前添加舍入

添加:

  // round the number if necessary before we do the conversion
  if (manbits > 13)
    absx += (2<<(manbits-13));

  manbits = 0;
  tmp = absx;
  while (tmp)
  {
    tmp >>= 1;
    manbits++;
  }

在你做转换之前。

2。将无限剪裁更改为 > 16

通过改变

  if (exp + truncated > 15)

到:

  if (exp + truncated > 16)

我更新了原来的代码https://ideone.com/mWqgSP