1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_partial_q15.c
10 * Description: Partial convolution of Q15 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 Q15 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 * Refer to <code>arm_conv_partial_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
60 arm_status arm_conv_partial_q15(
73 /* Run the below code for Cortex-M4 and Cortex-M3 */
75 q15_t *pIn1; /* inputA pointer */
76 q15_t *pIn2; /* inputB pointer */
77 q15_t *pOut = pDst; /* output pointer */
78 q63_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
79 q15_t *px; /* Intermediate inputA pointer */
80 q15_t *py; /* Intermediate inputB pointer */
81 q15_t *pSrc1, *pSrc2; /* Intermediate pointers */
82 q31_t x0, x1, x2, x3, c0; /* Temporary input variables */
83 uint32_t j, k, count, check, blkCnt;
84 int32_t blockSize1, blockSize2, blockSize3; /* loop counter */
85 arm_status status; /* status of Partial convolution */
86 q31_t *pb; /* 32 bit pointer for inputB buffer */
88 /* Check for range of output samples to be calculated */
89 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
91 /* Set status as ARM_MATH_ARGUMENT_ERROR */
92 status = ARM_MATH_ARGUMENT_ERROR;
97 /* The algorithm implementation is based on the lengths of the inputs. */
98 /* srcB is always made to slide across srcA. */
99 /* So srcBLen is always considered as shorter or equal to srcALen */
100 if(srcALen >= srcBLen)
102 /* Initialization of inputA pointer */
105 /* Initialization of inputB pointer */
110 /* Initialization of inputA pointer */
113 /* Initialization of inputB pointer */
116 /* srcBLen is always considered as shorter or equal to srcALen */
122 /* Conditions to check which loopCounter holds
123 * the first and last indices of the output samples to be calculated. */
124 check = firstIndex + numPoints;
125 blockSize3 = ((int32_t) check - (int32_t) srcALen);
126 blockSize3 = (blockSize3 > 0) ? blockSize3 : 0;
127 blockSize1 = (((int32_t) srcBLen - 1) - (int32_t) firstIndex);
128 blockSize1 = (blockSize1 > 0) ? ((check > (srcBLen - 1u)) ? blockSize1 :
129 (int32_t) numPoints) : 0;
130 blockSize2 = (int32_t) check - ((blockSize3 + blockSize1) +
131 (int32_t) firstIndex);
132 blockSize2 = (blockSize2 > 0) ? blockSize2 : 0;
134 /* 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] */
135 /* The function is internally
136 * divided into three stages according to the number of multiplications that has to be
137 * taken place between inputA samples and inputB samples. In the first stage of the
138 * algorithm, the multiplications increase by one for every iteration.
139 * In the second stage of the algorithm, srcBLen number of multiplications are done.
140 * In the third stage of the algorithm, the multiplications decrease by one
141 * for every iteration. */
143 /* Set the output pointer to point to the firstIndex
144 * of the output sample to be calculated. */
145 pOut = pDst + firstIndex;
147 /* --------------------------
148 * Initializations of stage1
149 * -------------------------*/
152 * sum = x[0] * y[1] + x[1] * y[0]
154 * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
157 /* In this stage the MAC operations are increased by 1 for every iteration.
158 The count variable holds the number of MAC operations performed.
159 Since the partial convolution starts from firstIndex
160 Number of Macs to be performed is firstIndex + 1 */
161 count = 1u + firstIndex;
163 /* Working pointer of inputA */
166 /* Working pointer of inputB */
167 pSrc2 = pIn2 + firstIndex;
170 /* ------------------------
172 * ----------------------*/
174 /* For loop unrolling by 4, this stage is divided into two. */
175 /* First part of this stage computes the MAC operations less than 4 */
176 /* Second part of this stage computes the MAC operations greater than or equal to 4 */
178 /* The first part of the stage starts here */
179 while((count < 4u) && (blockSize1 > 0))
181 /* Accumulator is made zero for every iteration */
184 /* Loop over number of MAC operations between
185 * inputA samples and inputB samples */
190 /* Perform the multiply-accumulates */
191 sum = __SMLALD(*px++, *py--, sum);
193 /* Decrement the loop counter */
197 /* Store the result in the accumulator in the destination buffer. */
198 *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
200 /* Update the inputA and inputB pointers for next MAC calculation */
204 /* Increment the MAC count */
207 /* Decrement the loop counter */
211 /* The second part of the stage starts here */
212 /* The internal loop, over count, is unrolled by 4 */
213 /* To, read the last two inputB samples using SIMD:
214 * y[srcBLen] and y[srcBLen-1] coefficients, py is decremented by 1 */
217 while(blockSize1 > 0)
219 /* Accumulator is made zero for every iteration */
222 /* Apply loop unrolling and compute 4 MACs simultaneously. */
225 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
226 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
229 /* Perform the multiply-accumulates */
230 /* x[0], x[1] are multiplied with y[srcBLen - 1], y[srcBLen - 2] respectively */
231 sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
232 /* x[2], x[3] are multiplied with y[srcBLen - 3], y[srcBLen - 4] respectively */
233 sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
235 /* Decrement the loop counter */
239 /* For the next MAC operations, the pointer py is used without SIMD
240 * So, py is incremented by 1 */
243 /* If the count is not a multiple of 4, compute any remaining MACs here.
244 ** No loop unrolling is used. */
249 /* Perform the multiply-accumulates */
250 sum = __SMLALD(*px++, *py--, sum);
252 /* Decrement the loop counter */
256 /* Store the result in the accumulator in the destination buffer. */
257 *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
259 /* Update the inputA and inputB pointers for next MAC calculation */
263 /* Increment the MAC count */
266 /* Decrement the loop counter */
270 /* --------------------------
271 * Initializations of stage2
272 * ------------------------*/
274 /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
275 * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
277 * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
280 /* Working pointer of inputA */
283 /* Working pointer of inputB */
284 pSrc2 = pIn2 + (srcBLen - 1u);
287 /* Initialize inputB pointer of type q31 */
288 pb = (q31_t *) (py - 1u);
290 /* count is the index by which the pointer pIn1 to be incremented */
294 /* --------------------
296 * -------------------*/
298 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
299 * So, to loop unroll over blockSize2,
300 * srcBLen should be greater than or equal to 4 */
303 /* Loop unroll over blockSize2, by 4 */
304 blkCnt = ((uint32_t) blockSize2 >> 2u);
308 /* Set all accumulators to zero */
315 /* read x[0], x[1] samples */
316 x0 = *(q31_t *) (px++);
317 /* read x[1], x[2] samples */
318 x1 = *(q31_t *) (px++);
321 /* Apply loop unrolling and compute 4 MACs simultaneously. */
324 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
325 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
328 /* Read the last two inputB samples using SIMD:
329 * y[srcBLen - 1] and y[srcBLen - 2] */
332 /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
333 acc0 = __SMLALDX(x0, c0, acc0);
335 /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
336 acc1 = __SMLALDX(x1, c0, acc1);
338 /* Read x[2], x[3] */
339 x2 = *(q31_t *) (px++);
341 /* Read x[3], x[4] */
342 x3 = *(q31_t *) (px++);
344 /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
345 acc2 = __SMLALDX(x2, c0, acc2);
347 /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
348 acc3 = __SMLALDX(x3, c0, acc3);
350 /* Read y[srcBLen - 3] and y[srcBLen - 4] */
353 /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
354 acc0 = __SMLALDX(x2, c0, acc0);
356 /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
357 acc1 = __SMLALDX(x3, c0, acc1);
359 /* Read x[4], x[5] */
360 x0 = *(q31_t *) (px++);
362 /* Read x[5], x[6] */
363 x1 = *(q31_t *) (px++);
365 /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
366 acc2 = __SMLALDX(x0, c0, acc2);
368 /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
369 acc3 = __SMLALDX(x1, c0, acc3);
373 /* For the next MAC operations, SIMD is not used
374 * So, the 16 bit pointer if inputB, py is updated */
378 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
379 ** No loop unrolling is used. */
384 /* Read y[srcBLen - 5] */
387 #ifdef ARM_MATH_BIG_ENDIAN
391 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
393 x3 = *(q31_t *) px++;
395 /* Perform the multiply-accumulates */
396 acc0 = __SMLALD(x0, c0, acc0);
397 acc1 = __SMLALD(x1, c0, acc1);
398 acc2 = __SMLALDX(x1, c0, acc2);
399 acc3 = __SMLALDX(x3, c0, acc3);
404 /* Read y[srcBLen - 5], y[srcBLen - 6] */
407 /* Read x[7], x[8] */
408 x3 = *(q31_t *) px++;
411 x2 = *(q31_t *) px++;
413 /* Perform the multiply-accumulates */
414 acc0 = __SMLALDX(x0, c0, acc0);
415 acc1 = __SMLALDX(x1, c0, acc1);
416 acc2 = __SMLALDX(x3, c0, acc2);
417 acc3 = __SMLALDX(x2, c0, acc3);
422 /* Read y[srcBLen - 5], y[srcBLen - 6] */
425 /* Read x[7], x[8] */
426 x3 = *(q31_t *) px++;
429 x2 = *(q31_t *) px++;
431 /* Perform the multiply-accumulates */
432 acc0 = __SMLALDX(x0, c0, acc0);
433 acc1 = __SMLALDX(x1, c0, acc1);
434 acc2 = __SMLALDX(x3, c0, acc2);
435 acc3 = __SMLALDX(x2, c0, acc3);
437 #ifdef ARM_MATH_BIG_ENDIAN
439 /* Read y[srcBLen - 7] */
445 /* Read y[srcBLen - 7] */
446 c0 = (q15_t) (*pb >> 16);
448 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
451 x3 = *(q31_t *) px++;
453 /* Perform the multiply-accumulates */
454 acc0 = __SMLALDX(x1, c0, acc0);
455 acc1 = __SMLALD(x2, c0, acc1);
456 acc2 = __SMLALDX(x2, c0, acc2);
457 acc3 = __SMLALDX(x3, c0, acc3);
460 /* Store the results in the accumulators in the destination buffer. */
461 #ifndef ARM_MATH_BIG_ENDIAN
464 __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
466 __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
471 __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
473 __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
475 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
477 /* Update the inputA and inputB pointers for next MAC calculation */
478 px = pIn1 + (count * 4u);
480 pb = (q31_t *) (py - 1);
482 /* Increment the pointer pIn1 index, count by 1 */
485 /* Decrement the loop counter */
489 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
490 ** No loop unrolling is used. */
491 blkCnt = (uint32_t) blockSize2 % 0x4u;
495 /* Accumulator is made zero for every iteration */
498 /* Apply loop unrolling and compute 4 MACs simultaneously. */
501 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
502 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
505 /* Perform the multiply-accumulates */
506 sum += (q63_t) ((q31_t) * px++ * *py--);
507 sum += (q63_t) ((q31_t) * px++ * *py--);
508 sum += (q63_t) ((q31_t) * px++ * *py--);
509 sum += (q63_t) ((q31_t) * px++ * *py--);
511 /* Decrement the loop counter */
515 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
516 ** No loop unrolling is used. */
521 /* Perform the multiply-accumulates */
522 sum += (q63_t) ((q31_t) * px++ * *py--);
524 /* Decrement the loop counter */
528 /* Store the result in the accumulator in the destination buffer. */
529 *pOut++ = (q15_t) (__SSAT(sum >> 15, 16));
531 /* Update the inputA and inputB pointers for next MAC calculation */
535 /* Increment the pointer pIn1 index, count by 1 */
538 /* Decrement the loop counter */
544 /* If the srcBLen is not a multiple of 4,
545 * the blockSize2 loop cannot be unrolled by 4 */
546 blkCnt = (uint32_t) blockSize2;
550 /* Accumulator is made zero for every iteration */
553 /* srcBLen number of MACS should be performed */
558 /* Perform the multiply-accumulate */
559 sum += (q63_t) ((q31_t) * px++ * *py--);
561 /* Decrement the loop counter */
565 /* Store the result in the accumulator in the destination buffer. */
566 *pOut++ = (q15_t) (__SSAT(sum >> 15, 16));
568 /* Update the inputA and inputB pointers for next MAC calculation */
572 /* Increment the MAC count */
575 /* Decrement the loop counter */
581 /* --------------------------
582 * Initializations of stage3
583 * -------------------------*/
585 /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
586 * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
588 * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
589 * sum += x[srcALen-1] * y[srcBLen-1]
592 /* In this stage the MAC operations are decreased by 1 for every iteration.
593 The count variable holds the number of MAC operations performed */
594 count = srcBLen - 1u;
596 /* Working pointer of inputA */
597 pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
600 /* Working pointer of inputB */
601 pSrc2 = pIn2 + (srcBLen - 1u);
605 /* -------------------
607 * ------------------*/
609 /* For loop unrolling by 4, this stage is divided into two. */
610 /* First part of this stage computes the MAC operations greater than 4 */
611 /* Second part of this stage computes the MAC operations less than or equal to 4 */
613 /* The first part of the stage starts here */
616 while((j > 0u) && (blockSize3 > 0))
618 /* Accumulator is made zero for every iteration */
621 /* Apply loop unrolling and compute 4 MACs simultaneously. */
624 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
625 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
628 /* x[srcALen - srcBLen + 1], x[srcALen - srcBLen + 2] are multiplied
629 * with y[srcBLen - 1], y[srcBLen - 2] respectively */
630 sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
631 /* x[srcALen - srcBLen + 3], x[srcALen - srcBLen + 4] are multiplied
632 * with y[srcBLen - 3], y[srcBLen - 4] respectively */
633 sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
635 /* Decrement the loop counter */
639 /* For the next MAC operations, the pointer py is used without SIMD
640 * So, py is incremented by 1 */
643 /* If the count is not a multiple of 4, compute any remaining MACs here.
644 ** No loop unrolling is used. */
649 /* sum += x[srcALen - srcBLen + 5] * y[srcBLen - 5] */
650 sum = __SMLALD(*px++, *py--, sum);
652 /* Decrement the loop counter */
656 /* Store the result in the accumulator in the destination buffer. */
657 *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
659 /* Update the inputA and inputB pointers for next MAC calculation */
663 /* Decrement the MAC count */
666 /* Decrement the loop counter */
672 /* The second part of the stage starts here */
673 /* SIMD is not used for the next MAC operations,
674 * so pointer py is updated to read only one sample at a time */
677 while(blockSize3 > 0)
679 /* Accumulator is made zero for every iteration */
682 /* Apply loop unrolling and compute 4 MACs simultaneously. */
687 /* Perform the multiply-accumulates */
688 /* sum += x[srcALen-1] * y[srcBLen-1] */
689 sum = __SMLALD(*px++, *py--, sum);
691 /* Decrement the loop counter */
695 /* Store the result in the accumulator in the destination buffer. */
696 *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
698 /* Update the inputA and inputB pointers for next MAC calculation */
702 /* Decrement the MAC count */
705 /* Decrement the loop counter */
709 /* set status as ARM_MATH_SUCCESS */
710 status = ARM_MATH_SUCCESS;
713 /* Return to application */
718 /* Run the below code for Cortex-M0 */
720 q15_t *pIn1 = pSrcA; /* inputA pointer */
721 q15_t *pIn2 = pSrcB; /* inputB pointer */
722 q63_t sum; /* Accumulator */
723 uint32_t i, j; /* loop counters */
724 arm_status status; /* status of Partial convolution */
726 /* Check for range of output samples to be calculated */
727 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
729 /* Set status as ARM_ARGUMENT_ERROR */
730 status = ARM_MATH_ARGUMENT_ERROR;
734 /* Loop to calculate convolution for output length number of values */
735 for (i = firstIndex; i <= (firstIndex + numPoints - 1); i++)
737 /* Initialize sum with zero to carry on MAC operations */
740 /* Loop to perform MAC operations according to convolution equation */
741 for (j = 0; j <= i; j++)
743 /* Check the array limitations */
744 if(((i - j) < srcBLen) && (j < srcALen))
746 /* z[i] += x[i-j] * y[j] */
747 sum += ((q31_t) pIn1[j] * (pIn2[i - j]));
751 /* Store the output in the destination buffer */
752 pDst[i] = (q15_t) __SSAT((sum >> 15u), 16u);
754 /* set status as ARM_SUCCESS as there are no argument errors */
755 status = ARM_MATH_SUCCESS;
759 #endif /* #ifndef ARM_MATH_CM0 */
764 * @} end of PartialConv group