Floating-point arithmetic in VDSP (bf561): A bug in the VDSP library

Source: Internet
Author: User
Tags arithmetic

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.