快乐虾
http://blog.csdn.net/lights_joy/
本文适用于
ADSP-BF561
Visual DSP++ 5.0 (update 6)
Vdsp dual processor simulate
欢迎转载,但请保留作者信息
在vdsp下,可以很简单地用:
float fdiv(float x, float y)
{
float r = x / y;
return r;
}
来完成浮点除法运算,编译器自动将里面的乘法操作转换为___float32_div的函数调用,这个函数的调用实现在libdsp/fpdiv.asm中,在这个文件的开头说明了这个函数的用法:
/******************************************************************************
Copyright(c) 2000-2008 Analog Devices Inc. IPDC BANGALORE, India.
All rights reserved
******************************************************************************
File Name : fpdiv32.asm
Module Name : floating point division
Label name : __float32_div
Description : This function computes single precision signed floating point
division. Implemention is based on the algorithm mentioned in
the reference. Some more conditions are added in the present
algorithm to take care of various testcases.
Registers used:
Operands in R0 & R1
R0- Numerator(X),R1- Denominator(Y)
R2 - R7 and P1
******************************************************************************
Special cases :
1) If(X == 0) Return 0.0 or -0.0 depending on sign of X,Y
2) If(Y == 0) Return INF or -INF depending on sign of X,Y
3) If(X == Y) Return 1.0 or -1.0 depending on sign of X,Y
4) If(Y == 1) Return X or -X depending on sign of X,Y
5) Underflow : If(EXP(X) - EXP(Y) < -149),return 0,
6) Overflow : If((EXP(X) - EXP(Y) + 126) > 254), return NAN or -NAN
depending on sign of X,Y
Reference : Computer Architecture a Quantitative Approach
second edition
by John L Hennessy and David Patterson
!!NOTE- Uses non-standard clobber set in compiler:
DefaultClobMinusPABIMandLoop1Regs
Remember to change the #pragma regs_clobbered in fpdiv.c in softfloat if you
change this clobber set
********************************************************************************/
看代码:
// First check if either operand is a NaN or inf, and handle certain
// identities with these values
R7 = (MAXBIASEXP+1) (Z);
CC = R6 == R7;
IF CC JUMP .HANDLE_NAN_INF_X;
……………
.HANDLE_NAN_INF_X:
// Return a NaN unless X is inf and Y is a valid number including 0, in
// which case return inf (signed appropriately)
CC = R5 < R7; // if y is a valid number
R6 = R0 << 9; // and if x is inf zero significand means X=inf
CC &= AZ;
IF CC JUMP .RET_INF (BP); // Return inf
// (predict branch to avoid 05-00-0428)
.RET_NAN:
R0 = -1; // Otherwise return a NaN
(R7:4)=[SP++]; // Pop R7-R4 from stack
RTS;
l 当x为inf且y为一个合法的浮点数
返回inf
fdiv(inf, 0) = inf,CYCLE为48。
l 其它
返回nan
CYCLE为50。
看代码:
CC = R5 == R7;
IF CC JUMP .HANDLE_NAN_INF_Y;
…………….
.HANDLE_NAN_INF_Y:
// Return NaN for all cases apart from 0/inf=0
CC = R0 == 0; // x == 0
R7 = R1 << 9; // and y == inf
CC &= AZ;
IF !CC JUMP .RET_NAN;
R0 = 0;
JUMP .SIGN_AND_RETURN;
.DIV_BY_ZERO:
// Return inf unless 0/0 in which case we return NaN.
CC = R0 == 0;
if CC JUMP .RET_NAN;
//else fallthrough - return inf
.RET_INF:
R0 = 0x7F8 (Z); // Infinity.
R0 <<= 20;
……………….
下面是计算结果:
表达式 |
结果 |
CYCLE |
0 / inf |
0 |
50 |
0 / nan |
nan |
52 |
inf / inf |
nan |
50 |
看代码:
// Handle identities where neither operand is a NaN or inf
CC = R1 == 0; // If X/0, return inf (or NaN if X=0)
IF CC JUMP .DIV_BY_ZERO;
……………
.DIV_BY_ZERO:
// Return inf unless 0/0 in which case we return NaN.
CC = R0 == 0;
if CC JUMP .RET_NAN;
//else fallthrough - return inf
.RET_INF:
R0 = 0x7F8 (Z); // Infinity.
R0 <<= 20;
.SIGN_AND_RETURN:
R0 = R0 | R3; // R3.31 contains the sign bit needed.
(R7:4)=[SP++]; // Pop R7-R4 from stack
RTS;
l 当x为零
0 / 0 = NAN
CYCLE = 52
l 当x不为零
任意合法浮点数 / 0 = inf
CYCLE = 46
看代码:
CC = R0 == 0; // IF 0/Y, return 0
IF CC JUMP .SIGN_AND_RETURN;
………
.SIGN_AND_RETURN:
R0 = R0 | R3; // R3.31 contains the sign bit needed.
(R7:4)=[SP++]; // Pop R7-R4 from stack
RTS;
直接返回0,所用的CYCLE为44。
看代码
CC = R0 == R1; // If X/X, return +/- 1.
IF CC R0 = R2;
IF CC JUMP .SIGN_AND_RETURN;
…….
.SIGN_AND_RETURN:
R0 = R0 | R3; // R3.31 contains the sign bit needed.
(R7:4)=[SP++]; // Pop R7-R4 from stack
RTS;
注意,这里的等是除了符号位之外,其十六进制表示的值完全相等!
直接返回1或者-1,所用的CYCLE为48。
当一个大数除以一个小数,将发生向上溢出,这个时候的返回值是inf,而不是说明中的nan,想来应该是写前面那段注释时的失误。
比如
1e30 / 1e-30 = inf
此时的CYCLE值为59。
在正常计算一个浮点除法时,所用的CYCLE值为240,这是最慢的一个计算!
在代码说明里给出的下溢条件是
(EXP(X) - EXP(Y) + 126) > 254
此时返回0,所用的CYCYLE为243。
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)