1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_partial_fast_opt_q15.c
10 * Description: Fast Q15 Partial convolution.
12 * Target Processor: Cortex-M4/Cortex-M3
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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
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.
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 * -------------------------------------------------------------------- */
44 * @ingroup groupFilters
48 * @addtogroup PartialConv
53 * @brief Partial convolution of Q15 sequences (fast version) for Cortex-M3 and Cortex-M4.
54 * @param[in] *pSrcA points to the first input sequence.
55 * @param[in] srcALen length of the first input sequence.
56 * @param[in] *pSrcB points to the second input sequence.
57 * @param[in] srcBLen length of the second input sequence.
58 * @param[out] *pDst points to the location where the output result is written.
59 * @param[in] firstIndex is the first output sample to start with.
60 * @param[in] numPoints is the number of output points to be computed.
61 * @param[in] *pScratch1 points to scratch buffer of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
62 * @param[in] *pScratch2 points to scratch buffer of size min(srcALen, srcBLen).
63 * @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].
65 * See <code>arm_conv_partial_q15()</code> for a slower implementation of this function which uses a 64-bit accumulator to avoid wrap around distortion.
68 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
69 * In this case input, output, scratch1 and scratch2 buffers should be aligned by 32-bit
73 #ifndef UNALIGNED_SUPPORT_DISABLE
75 arm_status arm_conv_partial_fast_opt_q15(
87 q15_t *pOut = pDst; /* output pointer */
88 q15_t *pScr1 = pScratch1; /* Temporary pointer for scratch1 */
89 q15_t *pScr2 = pScratch2; /* Temporary pointer for scratch1 */
90 q31_t acc0, acc1, acc2, acc3; /* Accumulator */
91 q31_t x1, x2, x3; /* Temporary variables to hold state and coefficient values */
92 q31_t y1, y2; /* State variables */
93 q15_t *pIn1; /* inputA pointer */
94 q15_t *pIn2; /* inputB pointer */
95 q15_t *px; /* Intermediate inputA pointer */
96 q15_t *py; /* Intermediate inputB pointer */
97 uint32_t j, k, blkCnt; /* loop counter */
100 uint32_t tapCnt; /* loop count */
102 /* Check for range of output samples to be calculated */
103 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
105 /* Set status as ARM_MATH_ARGUMENT_ERROR */
106 status = ARM_MATH_ARGUMENT_ERROR;
111 /* The algorithm implementation is based on the lengths of the inputs. */
112 /* srcB is always made to slide across srcA. */
113 /* So srcBLen is always considered as shorter or equal to srcALen */
114 if(srcALen >= srcBLen)
116 /* Initialization of inputA pointer */
119 /* Initialization of inputB pointer */
124 /* Initialization of inputA pointer */
127 /* Initialization of inputB pointer */
130 /* srcBLen is always considered as shorter or equal to srcALen */
136 /* Temporary pointer for scratch2 */
139 /* pointer to take end of scratch2 buffer */
140 pScr2 = pScratch2 + srcBLen - 1;
142 /* points to smaller length sequence */
145 /* Apply loop unrolling and do 4 Copies simultaneously. */
148 /* First part of the processing with loop unrolling copies 4 data points at a time.
149 ** a second loop below copies for the remaining 1 to 3 samples. */
151 /* Copy smaller length input sequence in reverse order into second scratch buffer */
154 /* copy second buffer in reversal manner */
160 /* Decrement the loop counter */
164 /* If the count is not a multiple of 4, copy remaining samples here.
165 ** No loop unrolling is used. */
170 /* copy second buffer in reversal manner for remaining samples */
173 /* Decrement the loop counter */
177 /* Initialze temporary scratch pointer */
180 /* Assuming scratch1 buffer is aligned by 32-bit */
181 /* Fill (srcBLen - 1u) zeros in scratch buffer */
182 arm_fill_q15(0, pScr1, (srcBLen - 1u));
184 /* Update temporary scratch pointer */
185 pScr1 += (srcBLen - 1u);
187 /* Copy bigger length sequence(srcALen) samples in scratch1 buffer */
189 /* Copy (srcALen) samples in scratch buffer */
190 arm_copy_q15(pIn1, pScr1, srcALen);
192 /* Update pointers */
195 /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
196 arm_fill_q15(0, pScr1, (srcBLen - 1u));
199 pScr1 += (srcBLen - 1u);
201 /* Initialization of pIn2 pointer */
204 pScratch1 += firstIndex;
206 pOut = pDst + firstIndex;
208 /* First part of the processing with loop unrolling process 4 data points at a time.
209 ** a second loop below process for the remaining 1 to 3 samples. */
211 /* Actual convolution process starts here */
212 blkCnt = (numPoints) >> 2;
216 /* Initialze temporary scratch pointer as scratch1 */
219 /* Clear Accumlators */
225 /* Read two samples from scratch1 buffer */
226 x1 = *__SIMD32(pScr1)++;
228 /* Read next two samples from scratch1 buffer */
229 x2 = *__SIMD32(pScr1)++;
231 tapCnt = (srcBLen) >> 2u;
236 /* Read four samples from smaller buffer */
237 y1 = _SIMD32_OFFSET(pIn2);
238 y2 = _SIMD32_OFFSET(pIn2 + 2u);
240 /* multiply and accumlate */
241 acc0 = __SMLAD(x1, y1, acc0);
242 acc2 = __SMLAD(x2, y1, acc2);
244 /* pack input data */
245 #ifndef ARM_MATH_BIG_ENDIAN
246 x3 = __PKHBT(x2, x1, 0);
248 x3 = __PKHBT(x1, x2, 0);
251 /* multiply and accumlate */
252 acc1 = __SMLADX(x3, y1, acc1);
254 /* Read next two samples from scratch1 buffer */
255 x1 = _SIMD32_OFFSET(pScr1);
257 /* multiply and accumlate */
258 acc0 = __SMLAD(x2, y2, acc0);
260 acc2 = __SMLAD(x1, y2, acc2);
262 /* pack input data */
263 #ifndef ARM_MATH_BIG_ENDIAN
264 x3 = __PKHBT(x1, x2, 0);
266 x3 = __PKHBT(x2, x1, 0);
269 acc3 = __SMLADX(x3, y1, acc3);
270 acc1 = __SMLADX(x3, y2, acc1);
272 x2 = _SIMD32_OFFSET(pScr1 + 2u);
274 #ifndef ARM_MATH_BIG_ENDIAN
275 x3 = __PKHBT(x2, x1, 0);
277 x3 = __PKHBT(x1, x2, 0);
280 acc3 = __SMLADX(x3, y2, acc3);
282 /* update scratch pointers */
287 /* Decrement the loop counter */
291 /* Update scratch pointer for remaining samples of smaller length sequence */
294 /* apply same above for remaining samples of smaller length sequence */
295 tapCnt = (srcBLen) & 3u;
300 /* accumlate the results */
301 acc0 += (*pScr1++ * *pIn2);
302 acc1 += (*pScr1++ * *pIn2);
303 acc2 += (*pScr1++ * *pIn2);
304 acc3 += (*pScr1++ * *pIn2++);
308 /* Decrement the loop counter */
315 /* Store the results in the accumulators in the destination buffer. */
317 #ifndef ARM_MATH_BIG_ENDIAN
320 __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
322 __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
327 __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
329 __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
331 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
333 /* Initialization of inputB pointer */
341 blkCnt = numPoints & 0x3;
343 /* Calculate convolution for remaining samples of Bigger length sequence */
346 /* Initialze temporary scratch pointer as scratch1 */
349 /* Clear Accumlators */
352 tapCnt = (srcBLen) >> 1u;
357 /* Read next two samples from scratch1 buffer */
358 x1 = *__SIMD32(pScr1)++;
360 /* Read two samples from smaller buffer */
361 y1 = *__SIMD32(pIn2)++;
363 acc0 = __SMLAD(x1, y1, acc0);
365 /* Decrement the loop counter */
369 tapCnt = (srcBLen) & 1u;
371 /* apply same above for remaining samples of smaller length sequence */
375 /* accumlate the results */
376 acc0 += (*pScr1++ * *pIn2++);
378 /* Decrement the loop counter */
384 /* The result is in 2.30 format. Convert to 1.15 with saturation.
385 ** Then store the output in the destination buffer. */
386 *pOut++ = (q15_t) (__SSAT((acc0 >> 15), 16));
388 /* Initialization of inputB pointer */
394 /* set status as ARM_MATH_SUCCESS */
395 status = ARM_MATH_SUCCESS;
397 /* Return to application */
403 arm_status arm_conv_partial_fast_opt_q15(
415 q15_t *pOut = pDst; /* output pointer */
416 q15_t *pScr1 = pScratch1; /* Temporary pointer for scratch1 */
417 q15_t *pScr2 = pScratch2; /* Temporary pointer for scratch1 */
418 q31_t acc0, acc1, acc2, acc3; /* Accumulator */
419 q15_t *pIn1; /* inputA pointer */
420 q15_t *pIn2; /* inputB pointer */
421 q15_t *px; /* Intermediate inputA pointer */
422 q15_t *py; /* Intermediate inputB pointer */
423 uint32_t j, k, blkCnt; /* loop counter */
424 arm_status status; /* Status variable */
425 uint32_t tapCnt; /* loop count */
426 q15_t x10, x11, x20, x21; /* Temporary variables to hold srcA buffer */
427 q15_t y10, y11; /* Temporary variables to hold srcB buffer */
430 /* Check for range of output samples to be calculated */
431 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
433 /* Set status as ARM_MATH_ARGUMENT_ERROR */
434 status = ARM_MATH_ARGUMENT_ERROR;
439 /* The algorithm implementation is based on the lengths of the inputs. */
440 /* srcB is always made to slide across srcA. */
441 /* So srcBLen is always considered as shorter or equal to srcALen */
442 if(srcALen >= srcBLen)
444 /* Initialization of inputA pointer */
447 /* Initialization of inputB pointer */
452 /* Initialization of inputA pointer */
455 /* Initialization of inputB pointer */
458 /* srcBLen is always considered as shorter or equal to srcALen */
464 /* Temporary pointer for scratch2 */
467 /* pointer to take end of scratch2 buffer */
468 pScr2 = pScratch2 + srcBLen - 1;
470 /* points to smaller length sequence */
473 /* Apply loop unrolling and do 4 Copies simultaneously. */
476 /* First part of the processing with loop unrolling copies 4 data points at a time.
477 ** a second loop below copies for the remaining 1 to 3 samples. */
480 /* copy second buffer in reversal manner */
486 /* Decrement the loop counter */
490 /* If the count is not a multiple of 4, copy remaining samples here.
491 ** No loop unrolling is used. */
496 /* copy second buffer in reversal manner for remaining samples */
499 /* Decrement the loop counter */
503 /* Initialze temporary scratch pointer */
506 /* Fill (srcBLen - 1u) zeros in scratch buffer */
507 arm_fill_q15(0, pScr1, (srcBLen - 1u));
509 /* Update temporary scratch pointer */
510 pScr1 += (srcBLen - 1u);
512 /* Copy bigger length sequence(srcALen) samples in scratch1 buffer */
515 /* Apply loop unrolling and do 4 Copies simultaneously. */
518 /* First part of the processing with loop unrolling copies 4 data points at a time.
519 ** a second loop below copies for the remaining 1 to 3 samples. */
522 /* copy second buffer in reversal manner */
528 /* Decrement the loop counter */
532 /* If the count is not a multiple of 4, copy remaining samples here.
533 ** No loop unrolling is used. */
538 /* copy second buffer in reversal manner for remaining samples */
541 /* Decrement the loop counter */
546 /* Apply loop unrolling and do 4 Copies simultaneously. */
547 k = (srcBLen - 1u) >> 2u;
549 /* First part of the processing with loop unrolling copies 4 data points at a time.
550 ** a second loop below copies for the remaining 1 to 3 samples. */
553 /* copy second buffer in reversal manner */
559 /* Decrement the loop counter */
563 /* If the count is not a multiple of 4, copy remaining samples here.
564 ** No loop unrolling is used. */
565 k = (srcBLen - 1u) % 0x4u;
569 /* copy second buffer in reversal manner for remaining samples */
572 /* Decrement the loop counter */
577 /* Initialization of pIn2 pointer */
580 pScratch1 += firstIndex;
582 pOut = pDst + firstIndex;
584 /* Actual convolution process starts here */
585 blkCnt = (numPoints) >> 2;
589 /* Initialze temporary scratch pointer as scratch1 */
592 /* Clear Accumlators */
598 /* Read two samples from scratch1 buffer */
602 /* Read next two samples from scratch1 buffer */
606 tapCnt = (srcBLen) >> 2u;
611 /* Read two samples from smaller buffer */
615 /* multiply and accumlate */
616 acc0 += (q31_t) x10 *y10;
617 acc0 += (q31_t) x11 *y11;
618 acc2 += (q31_t) x20 *y10;
619 acc2 += (q31_t) x21 *y11;
621 /* multiply and accumlate */
622 acc1 += (q31_t) x11 *y10;
623 acc1 += (q31_t) x20 *y11;
625 /* Read next two samples from scratch1 buffer */
629 /* multiply and accumlate */
630 acc3 += (q31_t) x21 *y10;
631 acc3 += (q31_t) x10 *y11;
633 /* Read next two samples from scratch2 buffer */
637 /* multiply and accumlate */
638 acc0 += (q31_t) x20 *y10;
639 acc0 += (q31_t) x21 *y11;
640 acc2 += (q31_t) x10 *y10;
641 acc2 += (q31_t) x11 *y11;
642 acc1 += (q31_t) x21 *y10;
643 acc1 += (q31_t) x10 *y11;
645 /* Read next two samples from scratch1 buffer */
649 /* multiply and accumlate */
650 acc3 += (q31_t) x11 *y10;
651 acc3 += (q31_t) x20 *y11;
653 /* update scratch pointers */
657 /* Decrement the loop counter */
661 /* Update scratch pointer for remaining samples of smaller length sequence */
664 /* apply same above for remaining samples of smaller length sequence */
665 tapCnt = (srcBLen) & 3u;
669 /* accumlate the results */
670 acc0 += (*pScr1++ * *pIn2);
671 acc1 += (*pScr1++ * *pIn2);
672 acc2 += (*pScr1++ * *pIn2);
673 acc3 += (*pScr1++ * *pIn2++);
677 /* Decrement the loop counter */
684 /* Store the results in the accumulators in the destination buffer. */
685 *pOut++ = __SSAT((acc0 >> 15), 16);
686 *pOut++ = __SSAT((acc1 >> 15), 16);
687 *pOut++ = __SSAT((acc2 >> 15), 16);
688 *pOut++ = __SSAT((acc3 >> 15), 16);
690 /* Initialization of inputB pointer */
698 blkCnt = numPoints & 0x3;
700 /* Calculate convolution for remaining samples of Bigger length sequence */
703 /* Initialze temporary scratch pointer as scratch1 */
706 /* Clear Accumlators */
709 tapCnt = (srcBLen) >> 1u;
714 /* Read next two samples from scratch1 buffer */
718 /* Read two samples from smaller buffer */
722 /* multiply and accumlate */
723 acc0 += (q31_t) x10 *y10;
724 acc0 += (q31_t) x11 *y11;
726 /* Decrement the loop counter */
730 tapCnt = (srcBLen) & 1u;
732 /* apply same above for remaining samples of smaller length sequence */
736 /* accumlate the results */
737 acc0 += (*pScr1++ * *pIn2++);
739 /* Decrement the loop counter */
745 /* Store the result in the accumulator in the destination buffer. */
746 *pOut++ = (q15_t) (__SSAT((acc0 >> 15), 16));
748 /* Initialization of inputB pointer */
755 /* set status as ARM_MATH_SUCCESS */
756 status = ARM_MATH_SUCCESS;
760 /* Return to application */
764 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
767 * @} end of PartialConv group