After Fract division was written, it was discovered by chance that a function of the Fract16 division operation was provided in a header file in Vdsp, which is VISUALDSP 5.0\blackfin\include\fract_math.h:
/* Produces a result which is the fractional division of f1 by f2. Not a builtin
* as written in C code. */
#pragma inline
#pragma always_inline
static fract16 div_s(fract16 _a, fract16 _b) {
int x = (int)_a;
int y = (int)_b;
fract16 rtn;
int i;
int aq;
if (x==0) {
rtn = 0;
}
else if (x>=y) {
rtn = 0x7fff;
}
else {
x <<= 16;
x = divs(x, y, &aq);
for (i=0; i<15; i++) {
x = divq(x, y, &aq);
}
rtn = (fract16) x;
}
return rtn;
}
This inline function is exactly the same as the sample function provided in the VDSP document. That is, it has a flaw that is mentioned in floating-point arithmetic in VDSP (bf561): Fract16 division, and if you use it to compute a positive decimal and a negative decimal division, it will return 1 without any courtesy.
For example, use it to calculate 0.2/-0.4, this is not supposed to appear saturated!
The problem arises because
int x = (int)_a;
int y = (int)_b;
Then the x and Y comparisons are made, and because of the comparison of integers, positive numbers are always larger than negative numbers, so this problem is caused.
Take 0.2/-0.4 for example:
0.2 converts to hexadecimal in the number of 0x1999, and-0.4 is 0xCCCC, when the two values assigned to x and y respectively into the 0x00001999 and 0XFFFFCCCC, that is, 6553 and-13108, haha, positive number is greater than negative, primary school students know!
Expect the next update to be fixed.