程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Vdsp(bf561)中的浮點運算(13):fract16乘法運算

Vdsp(bf561)中的浮點運算(13):fract16乘法運算

編輯:關於C++

vdsp提供了三種不同的方式進行fract16的乘法運算。

1.1 mult_fr1x16函數

這個函數定義為:

#pragma inline
#pragma always_inline
static fract16  mult_fr1x16(fract16  __a, fract16  __b) {
fract16  __rval = __builtin_mult_fr1x16(__a, __b);
return __rval;
}

從這裡可以看出我們實際可以使用__builtin_mult_fr1x16這一函數調用。

寫一個很簡單的程序:

typedef fract16 ft;

ft calc(ft x, ft y)
{
ft r;
r = __builtin_mult_fr1x16(x, y);
return r;
}

__builtin_mult_fr1x16展開後的匯編代碼為:

// line 29
R0.L = R0.L * R1.L (T);
R0 = R0.L (X);
W[FP + 16] = R0;

因而完成這樣一個運算將只需要一個cycle的時間。

這裡的乘法運算使用了(T)尾綴,文檔裡這樣解釋:

Signed fraction with truncation. Truncate Accumulator 9.31 format value at bit 16. (Perform no rounding.) Saturate the result to 1.15 precision in destination register half. Result is between minimum -1 and maximum 1-2-15 (or, expressed in hex, between minimum 0x8000 and maximum 0x7FFF).

這種計算方式直接將累加器裡的數進行截斷而不進行任何捨入的處理。

1.2 multr_fr1x16

這個函數定義為:

/* Performs a 16-bit fractional multiplication of the two input
** parameters. The result is rounded to 16 bits. Whether the
** rounding is biased or unbiased depends what the RND_MOD bit
** in the ASTAT register is set to.
*/

#pragma inline
#pragma always_inline
static fract16  multr_fr1x16(fract16  __a, fract16  __b) {
fract16  __rval = __builtin_multr_fr1x16(__a, __b);
return __rval;
}

它實際使用__builtin_multr_fr1x16完成計算,展開後的匯編代碼就是:

// line 29
R0.L = R0.L * R1.L ;
R0 = R0.L (X);
W[FP + 16] = R0;

它不使用尾綴進行乘法計算,關於這種計算方式,文檔這樣描述:

Signed fraction. Multiply 1.15 * 1.15 to produce 1.31 results after left-shift correction. Round 1.31 format value at bit 16. (RND_MOD bit in the ASTAT register controls the rounding.) Saturate the result to 1.15 precision in destination register half. Result is between minimum -1 and maximum 1-2-15 (or, expressed in hex, between minimum 0x8000 and maximum 0x7FFF).

也就是說它將對結果進行捨入操作,當然和截斷相比,它們之間的差值最大也就是2-15。

1.3 multu_fr1x16

這個函數在文檔裡面沒有記載,其定義為:

/* 16-bit unsigned fractional multiplication using the FU option
*/

#pragma inline
#pragma always_inline
static fract16  multu_fr1x16(fract16  __a, fract16  __b) {
fract16  __rval = __builtin_multu_fr1x16(__a, __b);
return __rval;
}

展開__builtin_multu_fr1x16的匯編代碼為:

// line 29
R0.L = R0.L * R1.L (FU);
R0 = R0.L (X);
W[FP + 16] = R0;

它使用了FU尾綴進行乘法運算,文檔這樣描述:

Unsigned fraction. Multiply 0.16 * 0.16 to produce 0.32 results. No shift correction. Round 0.32 format value at bit 16. (RND_MOD bit in the ASTAT register controls the rounding.) Saturate the result to 0.16 precision in destination register half. Result is between minimum 0 and maximum 1-2-16 (or, expressed in hex, between minimum 0x0000 and maximum 0xFFFF).

它采用的是0.16的格式,而不是fract16的1.15格式,難怪在文檔裡面沒有記載,嘿嘿。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved