瀏覽單個文章
拿破崙波拿巴
*停權中*
 
拿破崙波拿巴的大頭照
 

加入日期: Dec 2018
文章: 53
引用:
作者冬之炎陽
都告訴你去看 spec 了, 裡面就有答案了
還是不會嗎?
可以再問, 我會告訴你怎麼做的, 我可是很少教人這種junior的問題

對了 你的水土散熱器進化到哪一版了啊?


有答案

意思就是說 你知道就對了

那測試一下 看你是真知道 還是假知道


這段你看得懂嗎
代碼:
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));
}


半精度浮點是幾 byte

你知道不知道 我想問的是什麼東西
舊 2019-04-13, 07:17 PM #5
回應時引用此文章
拿破崙波拿巴離線中