当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:54:00  来源:本站整理

Vdsp(bf561)中的浮点运算(11):fract16与float的转换[VC/C++编程]

赞助商链接



  本文“Vdsp(bf561)中的浮点运算(11):fract16与float的转换[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

vdsp供应了两个函数用以实现fract16与float之间的彼此转换:

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

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

1.1 float_to_fr16

这个函数的原始代码在Blackfinlibsrclibcruntimefl2fr.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(bf561)中的浮点运算(11):fract16与float的转换[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • Vdsp(bf561)中的浮点运算(7):float乘法运算
  • Vdsp(bf561)中的浮点运算(8):float除法运算
  • Vdsp(bf561)中的浮点运算(9):long double和float的对比
  • <b>Vdsp(bf561)中的浮点运算(10):fract16范例表示</b>
  • Vdsp(bf561)中的浮点运算(11):fract16与float的转换
  • Vdsp(bf561)中的浮点运算(12):fract16加减运算
  • <b>Vdsp(bf561)中的浮点运算(13):fract16乘法运算</b>
  • <b>Vdsp(bf561)中的浮点运算(14):fract16除法</b>
  • Vdsp(bf561)中的浮点运算(15):vdsp库的一个BUG
  • Vdsp(bf561)中的浮点运算(16):fract2x16范例
  • Vdsp(bf561)中的浮点运算(3):FLT_MIN
  • <b>Vdsp(bf561)中的浮点运算(4):FLT_MAX</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .