最近在搞DSP,所以不可避免地会遇到浮点数,包括半精度浮点(16bit) 和单精度浮点(32bit)。
1、根据wiki百科介绍,IEEE 754规范中规定的32bit float point的格式为:(以下引用wiki百科https://en.wikipedia.org/wiki/Single-precision_floating-point_format)
- Sign bit: 1 bit
- Exponent width: 8 bits
- Significand precision: 24 bits (23 explicitly stored)
The real value assumed by a given 32-bit binary32 data with a given biased sign, exponent e (the 8-bit unsigned integer), and a 23-bit fraction is
which in decimal yields
-
,也可以是
符号位是最高1位,指数位,位宽8bit,这8位可以表示为8bit的有符号数,也可以表示为8bit的无符号数,当表示成8bit的无符号数时,指数的实际值 = E(指数表示的值) - 127,127被称为 exponent - bias。这样做之后指数的实际值的范围为 -126 ~ +127,而-127和+128保留做其他用处。有效位数,也被称为尾数,24bit中最高1bit为隐式存储,且值为1,剩下的23bit显式存储为32bit中的低23bit。上述计算公式也可以表示为,value = (-1)^sign X 2^(e - 127) X (1 + M / 2^ 23 );(M的值为23bit表示的整数值);
2、而16bit半精度浮点的格式为:sign bit: 1bitexponent width: 5bitsignificand: 10bit表示的浮点数值为, alue = (-1)^sign X 2^(e - 16) X (1 + M / 2^ 10 )。
3、单精度浮点的开根号求倒数算法众所周知的算法是:(引用wiki百科:https://en.wikipedia.org/wiki/Fast_inverse_square_root)float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }
这个算法应用很广,这里主要是阐述下magic number 0x5f3759df 值的推导- Sx, the "sign bit", is 0 if x > 0, and 1 if x < 0 (1 bit)
- Ex = ex + B is the "biased exponent", where B = 127 is the "exponent bias"[note 3] (8 bits)
- Mx = mx × L, where L = 223[note 4] (23 bits)
推导过程可参考wiki百科,
最后得出
-
σ ≈ 0.0450466,按照这个公式计算magic number时可以使用科学计算器,计算出来的小数4舍5入后转为整数,该整数就是magic number
而16bit半精度浮点的magic number的计算也使用该公式,这时L = 2^10,B = 16,最后magic number = 0x5fba (或0x5fbb)16bit开根号求倒数的代码为: -
float16 Q_rsqrt16( float16 number ) { short i; float16 x2, y; const float16 threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( short * ) &y; // evil floating point bit level hacking i = 0x5fba - ( i >> 1 ); // what the fuck? y = * ( float16 * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }
本文介绍了在 DSP 中常见的浮点数格式,特别是IEEE 754规范下的32位单精度浮点数和16位半精度浮点数。32bit float 包含1位符号位、8位指数宽度和23位有效数字。通过wiki百科的资料,解析了浮点数的表示方法,并提供了相关计算的推导概述。

454

被折叠的 条评论
为什么被折叠?



