当前位置:天才代写 > tutorial > C语言/C++ 教程 > Vdsp(bf561)中的浮点运算(7):float乘法运算

Vdsp(bf561)中的浮点运算(7):float乘法运算

2017-11-05 08:00 星期日 所属: C语言/C++ 教程 浏览:307

副标题#e#

1.1 Vdsp对float乘法运算的处理惩罚

在vdsp下,可以很简朴地用:

float mul (float x, float y)
{
float r = x * y;
return r;
}

来完成浮点乘法运算,编译器自动将内里的乘法操纵转换为___float32_mul的函数挪用,这个函数的挪用实此刻libdsp/fpmult.asm中,在这个文件的开头说明白这个函数的用法:

/***************************************************************************
Copyright (c) 2000-2008 Analog Devices Inc. All rights reserved.

****************************************************************************
File name :  fpmult.asm

This function performs 32 bit floating point multiplication. 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 - X operand,
R1 - Y operand
R2 - R7

Special case:
1) If (x AND y) == 0, Return 0,
2) Overflow  :  If(Exp(x) + Exp(y) > 254,
Return 0X7F80000 or 0xFF80000
depending upon sign of X and y.

3) Underflow :  If(Exp(x) + Exp(y) <= -149, RETURN 0.

Reference  : Computer Architecture a Quantitative Approach 2nd Ed.
by Jhon l Hennessy and David Patterson

!!NOTE- Uses non-standard clobber set in compiler:
DefaultClobMinusBIMandLoopRegs

Remember to change the #pragma regs_clobbered in fpmult.c in softfloat if you
change this clobber set

**************************************************************/


#p#副标题#e#

1.2 当x和y都为0

看实现代码:

R2 = R0^R1;   /* sign of the result of X * Y */ 
P0 = R2;     /* Store sign  */
R2 = R0 << 1;  /* Remove the sign bit of X */
CC = R2 == 0;
R3 = R1 << 1;   /* Remove the sign bit of Y */
CC |= AZ;
IF CC JUMP .HANDLE_ZERO_OP;
………..
.HANDLE_ZERO_OP:
// One operand is zero. If the other is NaN or inf, return NaN, otherwise
// zero
R2 = R2 | R3;
R2 = R2 >> 24;
R3 = MAXBIASEXP+1;  // Max biased exponent
CC = R2 == R3;
R0 = 0;
IF !CC JUMP .SIGN_RESULT; // Return zero (signed appropriately)
R0 = -1;  // return default NAN
RTS;    // no regs to restore on this path

当x和y都为0时,直接返回0,此时的操纵需要的CYCLE数为12。

这里一个较量有意思的工作是0是可以有正负号的,也就是说假如计较0.0 * (-0.0),那么功效的标记位保持为负!

1.3 当x为nan可能inf

看代码:

[--SP] = (R7:4);  /* Push registers R4-R7 */
R4 = MAXBIASEXP+1; /* Max biased exponent */

/* Get exponents. */
R2 = R2 >> 24;  /* Exponent of X operand */
R3 = R3 >> 24;  /* Exponent of Y operand */
CC = R2 == R4;
IF CC JUMP .HANDLE_NAN_INF_X;
……………..
.HANDLE_NAN_INF_X:
// Return signed inf if X=inf, and Y is either inf or a non-zero number
// Otherwise return NaN
R5 = R1 << 1;  // If Y = 0
CC = AZ;           // Return NaN
IF CC JUMP .RET_DEFAULT_NAN;
CC = R3 < R4;  // if y exp is valid
R5 = R1 << 9;  // or if Y significand is zero
CC |= AZ;
R5 = R0 << 9;  // then Y is either inf or a valid number
CC &= AZ;    // And if X is inf, then return inf, otherwise NaN
IF !CC JUMP .RET_DEFAULT_NAN;
.RET_INF:
R0 = 0x7F8 (Z);
R0 <<= 21;
(R7:4) = [SP++]; /* Pop registers R4-R7 */

.SIGN_RESULT:
CC = P0 < 0;   /* Extract sign, and set into result */
R0 = ROT R0 BY -1;  /* by rotating back with CC */
RTS;

.RET_DEFAULT_NAN:
R0 = -1;
(R7:4) = [SP++];  /* Pop registers R4-R7 */
RTS;

#p#副标题#e#

l 当x为inf且y为inf可能非0值

功效为inf。

mul (inf, inf)的功效为inf。

mul (inf, 1.0)的功效为inf。

此时的标记位仍然按照两个操纵数的标记位来确定。

此时的操纵需要的CYCLE数为53。

l 当x为inf且y为0

功效为NAN。

l 当x为inf且y为-inf

功效为-inf

1.4 当y为nan可能inf

看代码:

CC = R3 == R4;
IF CC JUMP .HANDLE_NAN_INF_Y;
…………..
// Handle certain identities concerning multiplying by NaNs and +/-inf
.HANDLE_NAN_INF_Y:
// Swap operands and exponents
R5 = R0;
R0 = R1;
R1 = R5;
R5 = R2;
R2 = R3;
R3 = R5;

.HANDLE_NAN_INF_X:
………..

简朴互换xy,然后和上一个判定条件沟通。

1.5 向上溢出

当两个操纵数的指数相加大于254时,认为溢出:

#p#分页标题#e#

// Compute result exponent, and check for overflow
R4 = BIASEXP;
R5 = R2 + R3;
R5 = R5 - R4;
R4 <<= 1;    // R4 now 254, max allowed exponent
CC = R4 < R5;
IF CC JUMP .RET_INF;
……………
.RET_INF:
R0 = 0x7F8 (Z);
R0 <<= 21;

(R7:4) = [SP++]; /* Pop registers R4-R7 */

.SIGN_RESULT:
CC = P0 < 0;  /* Extract sign, and set into result */
R0 = ROT R0 BY -1; /* by rotating back with CC */
RTS;

领略这段代码有点费劲,因为float能暗示的最大指数是127,那么应该是大于127就认为溢出,为什么要和254做较量呢?

判定条件应该是在

(e1 – 127) + (e2 – 127) > 127

时溢出,在上面的代码里相当于等价改为e1 + e2 – 127 > 254

为何不爽性改为e1 + e2 > 381呢?

奇怪!

溢出计较时的CYCLE值为52。

1.6 正常计较

在正常计较一个浮点乘法时,所用的CYCLE值为92,居然比做浮点加减所用的CYCLE少,一个出乎料想的功效!

1.7 向下溢出

在代码说明里给出的下溢条件是

Exp(x) + Exp(y) <= -149

为什么为是这样的条件呢?想不通!看来非得去看看Reference提及的书才行。

 

    关键字:

天才代写-代写联系方式