This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Question about c6400 dsplib source code

I am viewing some source code of c6400 dsplib,here is part of DSP_autocor

void DSP_autocor(short *restrict r, const short *restrict x, int nx, int nr)

   int i, k;
   int sum;
...

     for (k = nr; k < nx + nr; k++)
      {
          sum += x[k] * x[k-i];
      }

...

}

My question is will 'sum' overflow when accumulation?

Should it be modified as 'sum+=x[k]*x[k-i]>>15'?

  • Janes,

    The sum in the accumulation will not overflow. The reason for this is the input x is expected to be in Q15 format. The product of two numbers in Q15 format results in a number in Q30 format, hence the accumulate is always going to reult in a number in Q30 format which can be stored in an 32 bit integer variable(sum). The code then converts the result back to the Q15 format and returns the result.

    You can convert the intermediate accumulate results to Q15 format as you have suggested but this will affect the perfomance of the code.

    Regards,

    Rahul

  • Rahul,

    Thank  you very much. Your answer solved part of my question,but I still doubt that in the loop the accumulaiton of 'nr' Q30 format  results will probably overflow a 32 bit interger(sum) at last.one Q30 formate result can be stored in a 32 bit integer, sum of two Q30 is also ok,but what about the sum of 'nr' Q30?Will the accumulation of a lot of small Q30 interger eventually overflow 32 bit ?

    Regards,

    Janes

  • Janes,

    I think you are right about the over flow of the sum variable when nr multiplies of Q30 numbers are being added. I think this overflow case needs to be documented better for this function. But in this case,the overflow will also affect the outcome even if each multiply is converted to Q15 format as ultimately the result is returned as a vector of type short. The only way this can be fixed is to have the output array to be of type int and convert each multiply to Q15 format as you had recommended. I will log this as a bug so that it gets fixed in the future release. If you wish to see an immediate update we can give you an update that you can use in your application.

    Thanks and Regards,

    Rahul