快速业务通道

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

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-29

vdsp提供了两个函数用以实现fract16与float之间的相互转换:

fract16 float_to_fr16 (float _x);
float fr16_to_float (fract16 _x);

看看这两个转换函数到底做了什么。

1.1 float_to_fr16

这个函数的原始代码在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 cyc

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站: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号