STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
arm_conv_partial_q31.c
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date: 19. March 2015
5 * $Revision: V.1.4.5
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_partial_q31.c
9 *
10 * Description: Partial convolution of Q31 sequences.
11 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 
41 #include "arm_math.h"
42 
67  q31_t * pSrcA,
68  uint32_t srcALen,
69  q31_t * pSrcB,
70  uint32_t srcBLen,
71  q31_t * pDst,
72  uint32_t firstIndex,
73  uint32_t numPoints)
74 {
75 
76 
77 #ifndef ARM_MATH_CM0_FAMILY
78 
79  /* Run the below code for Cortex-M4 and Cortex-M3 */
80 
81  q31_t *pIn1; /* inputA pointer */
82  q31_t *pIn2; /* inputB pointer */
83  q31_t *pOut = pDst; /* output pointer */
84  q31_t *px; /* Intermediate inputA pointer */
85  q31_t *py; /* Intermediate inputB pointer */
86  q31_t *pSrc1, *pSrc2; /* Intermediate pointers */
87  q63_t sum, acc0, acc1, acc2; /* Accumulator */
88  q31_t x0, x1, x2, c0;
89  uint32_t j, k, count, check, blkCnt;
90  int32_t blockSize1, blockSize2, blockSize3; /* loop counter */
91  arm_status status; /* status of Partial convolution */
92 
93 
94  /* Check for range of output samples to be calculated */
95  if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
96  {
97  /* Set status as ARM_MATH_ARGUMENT_ERROR */
98  status = ARM_MATH_ARGUMENT_ERROR;
99  }
100  else
101  {
102 
103  /* The algorithm implementation is based on the lengths of the inputs. */
104  /* srcB is always made to slide across srcA. */
105  /* So srcBLen is always considered as shorter or equal to srcALen */
106  if(srcALen >= srcBLen)
107  {
108  /* Initialization of inputA pointer */
109  pIn1 = pSrcA;
110 
111  /* Initialization of inputB pointer */
112  pIn2 = pSrcB;
113  }
114  else
115  {
116  /* Initialization of inputA pointer */
117  pIn1 = pSrcB;
118 
119  /* Initialization of inputB pointer */
120  pIn2 = pSrcA;
121 
122  /* srcBLen is always considered as shorter or equal to srcALen */
123  j = srcBLen;
124  srcBLen = srcALen;
125  srcALen = j;
126  }
127 
128  /* Conditions to check which loopCounter holds
129  * the first and last indices of the output samples to be calculated. */
130  check = firstIndex + numPoints;
131  blockSize3 = ((int32_t)check > (int32_t)srcALen) ? (int32_t)check - (int32_t)srcALen : 0;
132  blockSize3 = ((int32_t)firstIndex > (int32_t)srcALen - 1) ? blockSize3 - (int32_t)firstIndex + (int32_t)srcALen : blockSize3;
133  blockSize1 = (((int32_t) srcBLen - 1) - (int32_t) firstIndex);
134  blockSize1 = (blockSize1 > 0) ? ((check > (srcBLen - 1u)) ? blockSize1 :
135  (int32_t) numPoints) : 0;
136  blockSize2 = (int32_t) check - ((blockSize3 + blockSize1) +
137  (int32_t) firstIndex);
138  blockSize2 = (blockSize2 > 0) ? blockSize2 : 0;
139 
140  /* conv(x,y) at n = x[n] * y[0] + x[n-1] * y[1] + x[n-2] * y[2] + ...+ x[n-N+1] * y[N -1] */
141  /* The function is internally
142  * divided into three stages according to the number of multiplications that has to be
143  * taken place between inputA samples and inputB samples. In the first stage of the
144  * algorithm, the multiplications increase by one for every iteration.
145  * In the second stage of the algorithm, srcBLen number of multiplications are done.
146  * In the third stage of the algorithm, the multiplications decrease by one
147  * for every iteration. */
148 
149  /* Set the output pointer to point to the firstIndex
150  * of the output sample to be calculated. */
151  pOut = pDst + firstIndex;
152 
153  /* --------------------------
154  * Initializations of stage1
155  * -------------------------*/
156 
157  /* sum = x[0] * y[0]
158  * sum = x[0] * y[1] + x[1] * y[0]
159  * ....
160  * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
161  */
162 
163  /* In this stage the MAC operations are increased by 1 for every iteration.
164  The count variable holds the number of MAC operations performed.
165  Since the partial convolution starts from firstIndex
166  Number of Macs to be performed is firstIndex + 1 */
167  count = 1u + firstIndex;
168 
169  /* Working pointer of inputA */
170  px = pIn1;
171 
172  /* Working pointer of inputB */
173  pSrc2 = pIn2 + firstIndex;
174  py = pSrc2;
175 
176  /* ------------------------
177  * Stage1 process
178  * ----------------------*/
179 
180  /* The first loop starts here */
181  while(blockSize1 > 0)
182  {
183  /* Accumulator is made zero for every iteration */
184  sum = 0;
185 
186  /* Apply loop unrolling and compute 4 MACs simultaneously. */
187  k = count >> 2u;
188 
189  /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
190  ** a second loop below computes MACs for the remaining 1 to 3 samples. */
191  while(k > 0u)
192  {
193  /* x[0] * y[srcBLen - 1] */
194  sum += (q63_t) * px++ * (*py--);
195  /* x[1] * y[srcBLen - 2] */
196  sum += (q63_t) * px++ * (*py--);
197  /* x[2] * y[srcBLen - 3] */
198  sum += (q63_t) * px++ * (*py--);
199  /* x[3] * y[srcBLen - 4] */
200  sum += (q63_t) * px++ * (*py--);
201 
202  /* Decrement the loop counter */
203  k--;
204  }
205 
206  /* If the count is not a multiple of 4, compute any remaining MACs here.
207  ** No loop unrolling is used. */
208  k = count % 0x4u;
209 
210  while(k > 0u)
211  {
212  /* Perform the multiply-accumulate */
213  sum += (q63_t) * px++ * (*py--);
214 
215  /* Decrement the loop counter */
216  k--;
217  }
218 
219  /* Store the result in the accumulator in the destination buffer. */
220  *pOut++ = (q31_t) (sum >> 31);
221 
222  /* Update the inputA and inputB pointers for next MAC calculation */
223  py = ++pSrc2;
224  px = pIn1;
225 
226  /* Increment the MAC count */
227  count++;
228 
229  /* Decrement the loop counter */
230  blockSize1--;
231  }
232 
233  /* --------------------------
234  * Initializations of stage2
235  * ------------------------*/
236 
237  /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
238  * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
239  * ....
240  * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
241  */
242 
243  /* Working pointer of inputA */
244  if((int32_t)firstIndex - (int32_t)srcBLen + 1 > 0)
245  {
246  px = pIn1 + firstIndex - srcBLen + 1;
247  }
248  else
249  {
250  px = pIn1;
251  }
252 
253  /* Working pointer of inputB */
254  pSrc2 = pIn2 + (srcBLen - 1u);
255  py = pSrc2;
256 
257  /* count is index by which the pointer pIn1 to be incremented */
258  count = 0u;
259 
260  /* -------------------
261  * Stage2 process
262  * ------------------*/
263 
264  /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
265  * So, to loop unroll over blockSize2,
266  * srcBLen should be greater than or equal to 4 */
267  if(srcBLen >= 4u)
268  {
269  /* Loop unroll over blkCnt */
270 
271  blkCnt = blockSize2 / 3;
272  while(blkCnt > 0u)
273  {
274  /* Set all accumulators to zero */
275  acc0 = 0;
276  acc1 = 0;
277  acc2 = 0;
278 
279  /* read x[0], x[1] samples */
280  x0 = *(px++);
281  x1 = *(px++);
282 
283  /* Apply loop unrolling and compute 3 MACs simultaneously. */
284  k = srcBLen / 3;
285 
286  /* First part of the processing with loop unrolling. Compute 3 MACs at a time.
287  ** a second loop below computes MACs for the remaining 1 to 2 samples. */
288  do
289  {
290  /* Read y[srcBLen - 1] sample */
291  c0 = *(py);
292 
293  /* Read x[2] sample */
294  x2 = *(px);
295 
296  /* Perform the multiply-accumulates */
297  /* acc0 += x[0] * y[srcBLen - 1] */
298  acc0 += (q63_t) x0 *c0;
299  /* acc1 += x[1] * y[srcBLen - 1] */
300  acc1 += (q63_t) x1 *c0;
301  /* acc2 += x[2] * y[srcBLen - 1] */
302  acc2 += (q63_t) x2 *c0;
303 
304  /* Read y[srcBLen - 2] sample */
305  c0 = *(py - 1u);
306 
307  /* Read x[3] sample */
308  x0 = *(px + 1u);
309 
310  /* Perform the multiply-accumulate */
311  /* acc0 += x[1] * y[srcBLen - 2] */
312  acc0 += (q63_t) x1 *c0;
313  /* acc1 += x[2] * y[srcBLen - 2] */
314  acc1 += (q63_t) x2 *c0;
315  /* acc2 += x[3] * y[srcBLen - 2] */
316  acc2 += (q63_t) x0 *c0;
317 
318  /* Read y[srcBLen - 3] sample */
319  c0 = *(py - 2u);
320 
321  /* Read x[4] sample */
322  x1 = *(px + 2u);
323 
324  /* Perform the multiply-accumulates */
325  /* acc0 += x[2] * y[srcBLen - 3] */
326  acc0 += (q63_t) x2 *c0;
327  /* acc1 += x[3] * y[srcBLen - 2] */
328  acc1 += (q63_t) x0 *c0;
329  /* acc2 += x[4] * y[srcBLen - 2] */
330  acc2 += (q63_t) x1 *c0;
331 
332 
333  px += 3u;
334 
335  py -= 3u;
336 
337  } while(--k);
338 
339  /* If the srcBLen is not a multiple of 3, compute any remaining MACs here.
340  ** No loop unrolling is used. */
341  k = srcBLen - (3 * (srcBLen / 3));
342 
343  while(k > 0u)
344  {
345  /* Read y[srcBLen - 5] sample */
346  c0 = *(py--);
347 
348  /* Read x[7] sample */
349  x2 = *(px++);
350 
351  /* Perform the multiply-accumulates */
352  /* acc0 += x[4] * y[srcBLen - 5] */
353  acc0 += (q63_t) x0 *c0;
354  /* acc1 += x[5] * y[srcBLen - 5] */
355  acc1 += (q63_t) x1 *c0;
356  /* acc2 += x[6] * y[srcBLen - 5] */
357  acc2 += (q63_t) x2 *c0;
358 
359  /* Reuse the present samples for the next MAC */
360  x0 = x1;
361  x1 = x2;
362 
363  /* Decrement the loop counter */
364  k--;
365  }
366 
367  /* Store the result in the accumulator in the destination buffer. */
368  *pOut++ = (q31_t) (acc0 >> 31);
369  *pOut++ = (q31_t) (acc1 >> 31);
370  *pOut++ = (q31_t) (acc2 >> 31);
371 
372  /* Increment the pointer pIn1 index, count by 3 */
373  count += 3u;
374 
375  /* Update the inputA and inputB pointers for next MAC calculation */
376  px = pIn1 + count;
377  py = pSrc2;
378 
379  /* Decrement the loop counter */
380  blkCnt--;
381  }
382 
383  /* If the blockSize2 is not a multiple of 3, compute any remaining output samples here.
384  ** No loop unrolling is used. */
385  blkCnt = blockSize2 - 3 * (blockSize2 / 3);
386 
387  while(blkCnt > 0u)
388  {
389  /* Accumulator is made zero for every iteration */
390  sum = 0;
391 
392  /* Apply loop unrolling and compute 4 MACs simultaneously. */
393  k = srcBLen >> 2u;
394 
395  /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
396  ** a second loop below computes MACs for the remaining 1 to 3 samples. */
397  while(k > 0u)
398  {
399  /* Perform the multiply-accumulates */
400  sum += (q63_t) * px++ * (*py--);
401  sum += (q63_t) * px++ * (*py--);
402  sum += (q63_t) * px++ * (*py--);
403  sum += (q63_t) * px++ * (*py--);
404 
405  /* Decrement the loop counter */
406  k--;
407  }
408 
409  /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
410  ** No loop unrolling is used. */
411  k = srcBLen % 0x4u;
412 
413  while(k > 0u)
414  {
415  /* Perform the multiply-accumulate */
416  sum += (q63_t) * px++ * (*py--);
417 
418  /* Decrement the loop counter */
419  k--;
420  }
421 
422  /* Store the result in the accumulator in the destination buffer. */
423  *pOut++ = (q31_t) (sum >> 31);
424 
425  /* Increment the MAC count */
426  count++;
427 
428  /* Update the inputA and inputB pointers for next MAC calculation */
429  px = pIn1 + count;
430  py = pSrc2;
431 
432  /* Decrement the loop counter */
433  blkCnt--;
434  }
435  }
436  else
437  {
438  /* If the srcBLen is not a multiple of 4,
439  * the blockSize2 loop cannot be unrolled by 4 */
440  blkCnt = (uint32_t) blockSize2;
441 
442  while(blkCnt > 0u)
443  {
444  /* Accumulator is made zero for every iteration */
445  sum = 0;
446 
447  /* srcBLen number of MACS should be performed */
448  k = srcBLen;
449 
450  while(k > 0u)
451  {
452  /* Perform the multiply-accumulate */
453  sum += (q63_t) * px++ * (*py--);
454 
455  /* Decrement the loop counter */
456  k--;
457  }
458 
459  /* Store the result in the accumulator in the destination buffer. */
460  *pOut++ = (q31_t) (sum >> 31);
461 
462  /* Increment the MAC count */
463  count++;
464 
465  /* Update the inputA and inputB pointers for next MAC calculation */
466  px = pIn1 + count;
467  py = pSrc2;
468 
469  /* Decrement the loop counter */
470  blkCnt--;
471  }
472  }
473 
474 
475  /* --------------------------
476  * Initializations of stage3
477  * -------------------------*/
478 
479  /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
480  * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
481  * ....
482  * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
483  * sum += x[srcALen-1] * y[srcBLen-1]
484  */
485 
486  /* In this stage the MAC operations are decreased by 1 for every iteration.
487  The blockSize3 variable holds the number of MAC operations performed */
488  count = srcBLen - 1u;
489 
490  /* Working pointer of inputA */
491  pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
492  px = pSrc1;
493 
494  /* Working pointer of inputB */
495  pSrc2 = pIn2 + (srcBLen - 1u);
496  py = pSrc2;
497 
498  /* -------------------
499  * Stage3 process
500  * ------------------*/
501 
502  while(blockSize3 > 0)
503  {
504  /* Accumulator is made zero for every iteration */
505  sum = 0;
506 
507  /* Apply loop unrolling and compute 4 MACs simultaneously. */
508  k = count >> 2u;
509 
510  /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
511  ** a second loop below computes MACs for the remaining 1 to 3 samples. */
512  while(k > 0u)
513  {
514  sum += (q63_t) * px++ * (*py--);
515  sum += (q63_t) * px++ * (*py--);
516  sum += (q63_t) * px++ * (*py--);
517  sum += (q63_t) * px++ * (*py--);
518 
519  /* Decrement the loop counter */
520  k--;
521  }
522 
523  /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
524  ** No loop unrolling is used. */
525  k = count % 0x4u;
526 
527  while(k > 0u)
528  {
529  /* Perform the multiply-accumulate */
530  sum += (q63_t) * px++ * (*py--);
531 
532  /* Decrement the loop counter */
533  k--;
534  }
535 
536  /* Store the result in the accumulator in the destination buffer. */
537  *pOut++ = (q31_t) (sum >> 31);
538 
539  /* Update the inputA and inputB pointers for next MAC calculation */
540  px = ++pSrc1;
541  py = pSrc2;
542 
543  /* Decrement the MAC count */
544  count--;
545 
546  /* Decrement the loop counter */
547  blockSize3--;
548 
549  }
550 
551  /* set status as ARM_MATH_SUCCESS */
552  status = ARM_MATH_SUCCESS;
553  }
554 
555  /* Return to application */
556  return (status);
557 
558 #else
559 
560  /* Run the below code for Cortex-M0 */
561 
562  q31_t *pIn1 = pSrcA; /* inputA pointer */
563  q31_t *pIn2 = pSrcB; /* inputB pointer */
564  q63_t sum; /* Accumulator */
565  uint32_t i, j; /* loop counters */
566  arm_status status; /* status of Partial convolution */
567 
568  /* Check for range of output samples to be calculated */
569  if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
570  {
571  /* Set status as ARM_ARGUMENT_ERROR */
572  status = ARM_MATH_ARGUMENT_ERROR;
573  }
574  else
575  {
576  /* Loop to calculate convolution for output length number of values */
577  for (i = firstIndex; i <= (firstIndex + numPoints - 1); i++)
578  {
579  /* Initialize sum with zero to carry on MAC operations */
580  sum = 0;
581 
582  /* Loop to perform MAC operations according to convolution equation */
583  for (j = 0; j <= i; j++)
584  {
585  /* Check the array limitations */
586  if(((i - j) < srcBLen) && (j < srcALen))
587  {
588  /* z[i] += x[i-j] * y[j] */
589  sum += ((q63_t) pIn1[j] * (pIn2[i - j]));
590  }
591  }
592 
593  /* Store the output in the destination buffer */
594  pDst[i] = (q31_t) (sum >> 31u);
595  }
596  /* set status as ARM_SUCCESS as there are no argument errors */
597  status = ARM_MATH_SUCCESS;
598  }
599  return (status);
600 
601 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
602 
603 }
604 
int64_t q63_t
64-bit fractional data type in 1.63 format.
Definition: arm_math.h:402
arm_status arm_conv_partial_q31(q31_t *pSrcA, uint32_t srcALen, q31_t *pSrcB, uint32_t srcBLen, q31_t *pDst, uint32_t firstIndex, uint32_t numPoints)
Partial convolution of Q31 sequences.
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition: arm_math.h:397
arm_status
Error status returned by some functions in the library.
Definition: arm_math.h:373