1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_partial_q31.c
10 * Description: Partial convolution of Q31 sequences.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
32 * -------------------------------------------------------------------- */
37 * @ingroup groupFilters
41 * @addtogroup PartialConv
46 * @brief Partial convolution of Q31 sequences.
47 * @param[in] *pSrcA points to the first input sequence.
48 * @param[in] srcALen length of the first input sequence.
49 * @param[in] *pSrcB points to the second input sequence.
50 * @param[in] srcBLen length of the second input sequence.
51 * @param[out] *pDst points to the location where the output result is written.
52 * @param[in] firstIndex is the first output sample to start with.
53 * @param[in] numPoints is the number of output points to be computed.
54 * @return Returns either ARM_MATH_SUCCESS if the function completed correctly or ARM_MATH_ARGUMENT_ERROR if the requested subset is not in the range [0 srcALen+srcBLen-2].
56 * See <code>arm_conv_partial_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
59 arm_status arm_conv_partial_q31(
72 /* Run the below code for Cortex-M4 and Cortex-M3 */
74 q31_t *pIn1; /* inputA pointer */
75 q31_t *pIn2; /* inputB pointer */
76 q31_t *pOut = pDst; /* output pointer */
77 q31_t *px; /* Intermediate inputA pointer */
78 q31_t *py; /* Intermediate inputB pointer */
79 q31_t *pSrc1, *pSrc2; /* Intermediate pointers */
80 q63_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
81 q31_t x0, x1, x2, x3, c0;
82 uint32_t j, k, count, check, blkCnt;
83 int32_t blockSize1, blockSize2, blockSize3; /* loop counter */
84 arm_status status; /* status of Partial convolution */
87 /* Check for range of output samples to be calculated */
88 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
90 /* Set status as ARM_MATH_ARGUMENT_ERROR */
91 status = ARM_MATH_ARGUMENT_ERROR;
96 /* The algorithm implementation is based on the lengths of the inputs. */
97 /* srcB is always made to slide across srcA. */
98 /* So srcBLen is always considered as shorter or equal to srcALen */
99 if(srcALen >= srcBLen)
101 /* Initialization of inputA pointer */
104 /* Initialization of inputB pointer */
109 /* Initialization of inputA pointer */
112 /* Initialization of inputB pointer */
115 /* srcBLen is always considered as shorter or equal to srcALen */
121 /* Conditions to check which loopCounter holds
122 * the first and last indices of the output samples to be calculated. */
123 check = firstIndex + numPoints;
124 blockSize3 = ((int32_t) check - (int32_t) srcALen);
125 blockSize3 = (blockSize3 > 0) ? blockSize3 : 0;
126 blockSize1 = (((int32_t) srcBLen - 1) - (int32_t) firstIndex);
127 blockSize1 = (blockSize1 > 0) ? ((check > (srcBLen - 1u)) ? blockSize1 :
128 (int32_t) numPoints) : 0;
129 blockSize2 = (int32_t) check - ((blockSize3 + blockSize1) +
130 (int32_t) firstIndex);
131 blockSize2 = (blockSize2 > 0) ? blockSize2 : 0;
133 /* 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] */
134 /* The function is internally
135 * divided into three stages according to the number of multiplications that has to be
136 * taken place between inputA samples and inputB samples. In the first stage of the
137 * algorithm, the multiplications increase by one for every iteration.
138 * In the second stage of the algorithm, srcBLen number of multiplications are done.
139 * In the third stage of the algorithm, the multiplications decrease by one
140 * for every iteration. */
142 /* Set the output pointer to point to the firstIndex
143 * of the output sample to be calculated. */
144 pOut = pDst + firstIndex;
146 /* --------------------------
147 * Initializations of stage1
148 * -------------------------*/
151 * sum = x[0] * y[1] + x[1] * y[0]
153 * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
156 /* In this stage the MAC operations are increased by 1 for every iteration.
157 The count variable holds the number of MAC operations performed.
158 Since the partial convolution starts from firstIndex
159 Number of Macs to be performed is firstIndex + 1 */
160 count = 1u + firstIndex;
162 /* Working pointer of inputA */
165 /* Working pointer of inputB */
166 pSrc2 = pIn2 + firstIndex;
169 /* ------------------------
171 * ----------------------*/
173 /* The first loop starts here */
174 while(blockSize1 > 0)
176 /* Accumulator is made zero for every iteration */
179 /* Apply loop unrolling and compute 4 MACs simultaneously. */
182 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
183 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
186 /* x[0] * y[srcBLen - 1] */
187 sum += (q63_t) * px++ * (*py--);
188 /* x[1] * y[srcBLen - 2] */
189 sum += (q63_t) * px++ * (*py--);
190 /* x[2] * y[srcBLen - 3] */
191 sum += (q63_t) * px++ * (*py--);
192 /* x[3] * y[srcBLen - 4] */
193 sum += (q63_t) * px++ * (*py--);
195 /* Decrement the loop counter */
199 /* If the count is not a multiple of 4, compute any remaining MACs here.
200 ** No loop unrolling is used. */
205 /* Perform the multiply-accumulate */
206 sum += (q63_t) * px++ * (*py--);
208 /* Decrement the loop counter */
212 /* Store the result in the accumulator in the destination buffer. */
213 *pOut++ = (q31_t) (sum >> 31);
215 /* Update the inputA and inputB pointers for next MAC calculation */
219 /* Increment the MAC count */
222 /* Decrement the loop counter */
226 /* --------------------------
227 * Initializations of stage2
228 * ------------------------*/
230 /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
231 * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
233 * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
236 /* Working pointer of inputA */
239 /* Working pointer of inputB */
240 pSrc2 = pIn2 + (srcBLen - 1u);
243 /* count is index by which the pointer pIn1 to be incremented */
246 /* -------------------
248 * ------------------*/
250 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
251 * So, to loop unroll over blockSize2,
252 * srcBLen should be greater than or equal to 4 */
255 /* Loop unroll over blockSize2 */
256 blkCnt = ((uint32_t) blockSize2 >> 2u);
260 /* Set all accumulators to zero */
266 /* read x[0], x[1], x[2] samples */
271 /* Apply loop unrolling and compute 4 MACs simultaneously. */
274 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
275 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
278 /* Read y[srcBLen - 1] sample */
281 /* Read x[3] sample */
284 /* Perform the multiply-accumulates */
285 /* acc0 += x[0] * y[srcBLen - 1] */
286 acc0 += (q63_t) x0 *c0;
287 /* acc1 += x[1] * y[srcBLen - 1] */
288 acc1 += (q63_t) x1 *c0;
289 /* acc2 += x[2] * y[srcBLen - 1] */
290 acc2 += (q63_t) x2 *c0;
291 /* acc3 += x[3] * y[srcBLen - 1] */
292 acc3 += (q63_t) x3 *c0;
294 /* Read y[srcBLen - 2] sample */
297 /* Read x[4] sample */
300 /* Perform the multiply-accumulate */
301 /* acc0 += x[1] * y[srcBLen - 2] */
302 acc0 += (q63_t) x1 *c0;
303 /* acc1 += x[2] * y[srcBLen - 2] */
304 acc1 += (q63_t) x2 *c0;
305 /* acc2 += x[3] * y[srcBLen - 2] */
306 acc2 += (q63_t) x3 *c0;
307 /* acc3 += x[4] * y[srcBLen - 2] */
308 acc3 += (q63_t) x0 *c0;
310 /* Read y[srcBLen - 3] sample */
313 /* Read x[5] sample */
316 /* Perform the multiply-accumulates */
317 /* acc0 += x[2] * y[srcBLen - 3] */
318 acc0 += (q63_t) x2 *c0;
319 /* acc1 += x[3] * y[srcBLen - 2] */
320 acc1 += (q63_t) x3 *c0;
321 /* acc2 += x[4] * y[srcBLen - 2] */
322 acc2 += (q63_t) x0 *c0;
323 /* acc3 += x[5] * y[srcBLen - 2] */
324 acc3 += (q63_t) x1 *c0;
326 /* Read y[srcBLen - 4] sample */
329 /* Read x[6] sample */
332 /* Perform the multiply-accumulates */
333 /* acc0 += x[3] * y[srcBLen - 4] */
334 acc0 += (q63_t) x3 *c0;
335 /* acc1 += x[4] * y[srcBLen - 4] */
336 acc1 += (q63_t) x0 *c0;
337 /* acc2 += x[5] * y[srcBLen - 4] */
338 acc2 += (q63_t) x1 *c0;
339 /* acc3 += x[6] * y[srcBLen - 4] */
340 acc3 += (q63_t) x2 *c0;
344 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
345 ** No loop unrolling is used. */
350 /* Read y[srcBLen - 5] sample */
353 /* Read x[7] sample */
356 /* Perform the multiply-accumulates */
357 /* acc0 += x[4] * y[srcBLen - 5] */
358 acc0 += (q63_t) x0 *c0;
359 /* acc1 += x[5] * y[srcBLen - 5] */
360 acc1 += (q63_t) x1 *c0;
361 /* acc2 += x[6] * y[srcBLen - 5] */
362 acc2 += (q63_t) x2 *c0;
363 /* acc3 += x[7] * y[srcBLen - 5] */
364 acc3 += (q63_t) x3 *c0;
366 /* Reuse the present samples for the next MAC */
371 /* Decrement the loop counter */
375 /* Store the result in the accumulator in the destination buffer. */
376 *pOut++ = (q31_t) (acc0 >> 31);
377 *pOut++ = (q31_t) (acc1 >> 31);
378 *pOut++ = (q31_t) (acc2 >> 31);
379 *pOut++ = (q31_t) (acc3 >> 31);
381 /* Update the inputA and inputB pointers for next MAC calculation */
382 px = pIn1 + (count * 4u);
385 /* Increment the pointer pIn1 index, count by 1 */
388 /* Decrement the loop counter */
392 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
393 ** No loop unrolling is used. */
394 blkCnt = (uint32_t) blockSize2 % 0x4u;
398 /* Accumulator is made zero for every iteration */
401 /* Apply loop unrolling and compute 4 MACs simultaneously. */
404 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
405 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
408 /* Perform the multiply-accumulates */
409 sum += (q63_t) * px++ * (*py--);
410 sum += (q63_t) * px++ * (*py--);
411 sum += (q63_t) * px++ * (*py--);
412 sum += (q63_t) * px++ * (*py--);
414 /* Decrement the loop counter */
418 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
419 ** No loop unrolling is used. */
424 /* Perform the multiply-accumulate */
425 sum += (q63_t) * px++ * (*py--);
427 /* Decrement the loop counter */
431 /* Store the result in the accumulator in the destination buffer. */
432 *pOut++ = (q31_t) (sum >> 31);
434 /* Update the inputA and inputB pointers for next MAC calculation */
438 /* Increment the MAC count */
441 /* Decrement the loop counter */
447 /* If the srcBLen is not a multiple of 4,
448 * the blockSize2 loop cannot be unrolled by 4 */
449 blkCnt = (uint32_t) blockSize2;
453 /* Accumulator is made zero for every iteration */
456 /* srcBLen number of MACS should be performed */
461 /* Perform the multiply-accumulate */
462 sum += (q63_t) * px++ * (*py--);
464 /* Decrement the loop counter */
468 /* Store the result in the accumulator in the destination buffer. */
469 *pOut++ = (q31_t) (sum >> 31);
471 /* Update the inputA and inputB pointers for next MAC calculation */
475 /* Increment the MAC count */
478 /* Decrement the loop counter */
484 /* --------------------------
485 * Initializations of stage3
486 * -------------------------*/
488 /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
489 * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
491 * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
492 * sum += x[srcALen-1] * y[srcBLen-1]
495 /* In this stage the MAC operations are decreased by 1 for every iteration.
496 The blockSize3 variable holds the number of MAC operations performed */
497 count = srcBLen - 1u;
499 /* Working pointer of inputA */
500 pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
503 /* Working pointer of inputB */
504 pSrc2 = pIn2 + (srcBLen - 1u);
507 /* -------------------
509 * ------------------*/
511 while(blockSize3 > 0)
513 /* Accumulator is made zero for every iteration */
516 /* Apply loop unrolling and compute 4 MACs simultaneously. */
519 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
520 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
523 sum += (q63_t) * px++ * (*py--);
524 sum += (q63_t) * px++ * (*py--);
525 sum += (q63_t) * px++ * (*py--);
526 sum += (q63_t) * px++ * (*py--);
528 /* Decrement the loop counter */
532 /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
533 ** No loop unrolling is used. */
538 /* Perform the multiply-accumulate */
539 sum += (q63_t) * px++ * (*py--);
541 /* Decrement the loop counter */
545 /* Store the result in the accumulator in the destination buffer. */
546 *pOut++ = (q31_t) (sum >> 31);
548 /* Update the inputA and inputB pointers for next MAC calculation */
552 /* Decrement the MAC count */
555 /* Decrement the loop counter */
560 /* set status as ARM_MATH_SUCCESS */
561 status = ARM_MATH_SUCCESS;
564 /* Return to application */
569 /* Run the below code for Cortex-M0 */
571 q31_t *pIn1 = pSrcA; /* inputA pointer */
572 q31_t *pIn2 = pSrcB; /* inputB pointer */
573 q63_t sum; /* Accumulator */
574 uint32_t i, j; /* loop counters */
575 arm_status status; /* status of Partial convolution */
577 /* Check for range of output samples to be calculated */
578 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
580 /* Set status as ARM_ARGUMENT_ERROR */
581 status = ARM_MATH_ARGUMENT_ERROR;
585 /* Loop to calculate convolution for output length number of values */
586 for (i = firstIndex; i <= (firstIndex + numPoints - 1); i++)
588 /* Initialize sum with zero to carry on MAC operations */
591 /* Loop to perform MAC operations according to convolution equation */
592 for (j = 0; j <= i; j++)
594 /* Check the array limitations */
595 if(((i - j) < srcBLen) && (j < srcALen))
597 /* z[i] += x[i-j] * y[j] */
598 sum += ((q63_t) pIn1[j] * (pIn2[i - j]));
602 /* Store the output in the destination buffer */
603 pDst[i] = (q31_t) (sum >> 31u);
605 /* set status as ARM_SUCCESS as there are no argument errors */
606 status = ARM_MATH_SUCCESS;
610 #endif /* #ifndef ARM_MATH_CM0 */
615 * @} end of PartialConv group