快速业务通道

Vdsp(bf561)中的浮点运算(11):fract16与float的转换

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-29
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

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号