这个函数fract_exp_two有什么作用?

时间:2015-07-13 14:15:40

标签: c

/* Does linear interpolation between powers of two */
static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
{
        unsigned fract = x & ~(~0 << fract_bits);

        x >>= fract_bits;
        x   = 1 << x;
        x  += (x * fract) >> fract_bits;

        return x;
}

我遇到了一个功能,但我无法弄清楚它是做什么的。它返回什么结果?我尝试了一些论点,但几乎找不到任何线索。是否有任何解释?

1 个答案:

答案 0 :(得分:0)

来自@ doynax的提示并引用此链接https://en.wikipedia.org/wiki/Half-precision_floating-point_format

我弄清楚它是如何运作的。

A由2个部分组成,一个(此函数中的高部分x >>= fract_bits)是指数(pow的2),并且一个(此函数中的低部分fract = x & ~(~0 << fract_bits))是分数部分(也是2)的力量。除非高部分全部为零,否则它隐式表示1 (x = 1<<x)。因此,最终部分x += (x * fract) >> fract_bits表示1 +尾数*指数部分。 这是一种用2字节数据类型编码浮点数的方法。