Vdsp(bf561)中的浮点运算(8):float除法运算
副标题#e#
1.1 Vdsp对float除法运算的处理惩罚
在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
********************************************************************************/
#p#副标题#e#
1.2 当x为nan可能inf
看代码:
// 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。
1.3 当y为nan可能inf
看代码:
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 |
1.4 当y为0
看代码:
// 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
1.5 当x为0
看代码:
#p#分页标题#e#
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。
1.6 当x == y
看代码
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。
1.7 向上溢出
当一个大数除以一个小数,将产生向上溢出,这个时候的返回值是inf,而不是说明中的nan,想来应该是写前面那段注释时的失误。
好比
1e30 / 1e-30 = inf
此时的CYCLE值为59。
1.8 正常计较
在正常计较一个浮点除法时,所用的CYCLE值为240,这是最慢的一个计较!
1.9 向下溢出
在代码说明里给出的下溢条件是
(EXP(X) – EXP(Y) + 126) > 254
此时返回0,所用的CYCYLE为243。