le 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。
1.2 fr16_to_float
这个转换由于是从小精度的数转换为大精度的数,过程比较简单,也没有精度丢失的问题,其实现代码在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 argume
|