快乐虾
http://blog.csdn.net/lights_joy/
本文适用于
ADSP-BF561
Visual DSP++ 5.0 (update 6)
Vdsp dual processor simulate
欢迎转载,但请保留作者信息
vdsp提供了两个函数用以实现fract16与float之间的相互转换:
fract16 float_to_fr16 (float _x);
float fr16_to_float (fract16 _x);
看看这两个转换函数到底做了什么。
这个函数的原始代码在Blackfin\lib\src\libc\runtime\fl2fr.asm中,先看看它的注释:
/***************************************************************************
*
* Function: FLOAT_TO_FR16 -- Convert a floating-point value to a fract16
*
* Synopsis:
*
* #include <fract2float_conv.h>
* fract16 float_to_fr16(float x);
*
* Description:
*
* The float_to_fr16 function converts a single precision, 32-bit IEEE
* value into a fract16 number in 1.15 notation. Floating-point values
* that cannot be converted to fract15 notation are handled as follows:
*
* Return 0x7fffffff if x >= 1.0 or NaN or +Inf
* Return 0x80000000 if x < -1.0 or -NaN or -Inf
* Return 0 if fabs(x) < 3.0517578125e-5
*
* (Note that the IEEE single precision, 32-bit, representation
* contains 24 bits of precision, made up of a hidden bit and 23
* bits of mantissa, and thus some precision may be lost by converting
* a float to a fract16).
*
* Algorithm:
*
* The traditional algorithm to convert a floating-point value to 1.15
* fractional notation is:
*
* (fract16) (x * 32768.0)
*
* However on Blackfin, floating-point multiplication is relatively
* slow is emulated in software, and this basic algorithm does not
* handle out of range results.
*
* This implementation is based on the support routine that converts
* a float to fract32, and then converts the fract32 into a fract16
* by performing an arithmetic right shift by 16 bits. (It is possible
* to avoid the shift by coding the function to "multiply" the input
* input argument by 2^15 (rather than 2^31) but this approach can lead
* to the loss of 1-bit precision when handling negative inputs).
*
* The following is a C implementation of this function and is about
* a third slower:
#include <fract2float_conv.h>
extern fract16
float_to_fr16(float x)
{
int temp;
fract32 result;
temp = *(int *)(&x);
if ((temp & 0x7f800000) >= 0x3f800000) {
result = 0x7fffffff;
if (temp < 0)
result = 0x80000000;
} else {
temp = temp + 0x0f800000;
result = *(float *)(&temp);
}
return (result >> 16);
}
*
* WARNING: This algorithm assumes that the floating-point number
* representation is conformant with IEEE.
*
* Cycle Counts:
*
* 31 cycles when the result is within range
* 30 cycles when the result is out of range
* 28 cycles when the input is 0.0
*
* These cycle counts were measured using the BF532 cycle accurate
* simulator and include the overheads involved in calling the function
* as well as the costs associated with argument passing.
*
* Code Size:
*
* 76 bytes
*
* Registers Used:
*
* R0 - the input argument
* R1 - various constants
* R2 - the exponent of the input argument or a shift amount
* R3 - the mantissa of the input argument
*
* (c) Copyright 2006 Analog Devices, Inc. All rights reserved.
* $Revision: 1.3 $
*
***************************************************************************/
这段注释和原始代码比VDSP提供的文档清晰多了,从这里可以知道其转换过程是先将其转换为fract32类型,在最后将转换结果右移16位得到fract16类型,且由于float类型有23位的尾数,而fract16则只有16位,因此不可避免地会引起精度丢失。
l 当输入值>=1、为nan或者为inf时
返回0x7fff,也就是fract16能表示的最大值0.999969482421875。
此时需要29个cycle
l 当输入值<-1或者为-inf时
返回0x8000,也就是fract16能表示的最小值-1。
此时需要29个cycle。
l 范围内的值
此时需要30个cycle。
这个转换由于是从小精度的数转换为大精度的数,过程比较简单,也没有精度丢失的问题,其实现代码在Blackfin\lib\src\libc\runtime\fr2fl.asm中,看其注释:
/***************************************************************************
*
* Function: FR16_TO_FLOAT -- Convert a fract16 to a floating-point value
*
* Synopsis:
*
* #include <fract2float_conv.h>
* float fr16_to_float(fract16 x);
*
* Description:
*
* The fr16_to_float converts a fixed-point, 16-bit fractional number
* in 1.15 notation into a single precision, 32-bit IEEE floating-point
* value; no precision is lost during the conversion.
*
* Algorithm:
*
* The traditional algorithm to convert a 1.15 fractional numbers to
* floating-point value is:
*
* (float)(x) / 32768.0
*
* However on Blackfin, floating-point division is relatively slow,
* and one can alternatively adapt the algorithm for converting from
* a short int to a float and then subtracting 15 from the exponent
* to simulate a division by 32768.0.
*
* The following is a slower C implementation of this function:
#include <fract2float_conv.h>
extern float
fr16_to_float(fract16 x)
{
float result = fabsf(x);
int *presult = (int *)(&result);
if (result != 0.0) {
*presult = *ptemp - 0x07800000;
if (x < 0)
result = -result;
}
return result;
}
*
* WARNING: This algorithm assumes that the floating-point number
* representation is conformant with IEEE.
*
* Cycle Counts:
*
* 22 cycles when the input is 0
* 25 cycles for all other input
*
* These cycle counts were measured using the BF532 cycle accurate
* simulator and include the overheads involved in calling the function
* as well as the costs associated with argument passing.
*
* Code Size:
*
* 38 bytes
*
* Registers Used:
*
* R0 - the input argument and result
* R1 - various
* R2 - various
*
* (c) Copyright 2006 Analog Devices, Inc. All rights reserved.
* $Revision: 1.2 $
*
***************************************************************************/
这个转换过程需要24个cycle。
vdsp虽然没有将fract16当成内置类型,但是对于常量,编译器还是网开一面,使用r16或者r32后缀,编译器会自动将这个常量正确转换为fract16类型,如
fract16 r = 0.2r16;
编译器自动计算r的值为0x1999。
由于fract16不是内置类型,编译器将不会自动完成两种类型之间的转换,如果不小心写上:
float r;
fract16 r1 = 0.2r16;
r = r1;
那么很不幸,r的值不是期望的0.2,而是6553!
Fract的其它运算也一样,务必通过函数调用来完成。
Vdsp(bf561)中的浮点运算(10):fract16类型表示(2009-8-17)
Vdsp(bf561)中的浮点运算(9):long double和float的比较(2009-8-14)
Vdsp(bf561)中的浮点运算(8):float除法运算(2009-8-14)
Vdsp(bf561)中的浮点运算(7):float乘法运算(2009-8-13)
Vdsp(bf561)中的浮点运算(6):float加减运算(2009-8-13)
Vdsp(bf561)中的浮点运算(5):float类型表示总结(2009-8-12)
Vdsp(bf561)中的浮点运算(4):FLT_MAX(2009-8-12)
Vdsp(bf561)中的浮点运算(3):FLT_MIN(2008-12-19)
Vdsp(bf561)中的浮点运算(2):float的疑问(2008-12-18)
Vdsp(bf561)中的浮点运算(1):文档的说法(2008-12-16)