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_opt_q7.c
10 * Description: Partial convolution of Q7 sequences.
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 Q7 sequences.
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 type q15_t) of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
62 * @param[in] *pScratch2 points to scratch buffer (of type q15_t) 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].
66 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
67 * In this case input, output, scratch1 and scratch2 buffers should be aligned by 32-bit
74 #ifndef UNALIGNED_SUPPORT_DISABLE
76 arm_status arm_conv_partial_opt_q7(
88 q15_t *pScr2, *pScr1; /* Intermediate pointers for scratch pointers */
89 q15_t x4; /* Temporary input variable */
90 q7_t *pIn1, *pIn2; /* inputA and inputB pointer */
91 uint32_t j, k, blkCnt, tapCnt; /* loop counter */
92 q7_t *px; /* Temporary input1 pointer */
93 q15_t *py; /* Temporary input2 pointer */
94 q31_t acc0, acc1, acc2, acc3; /* Accumulator */
95 q31_t x1, x2, x3, y1; /* Temporary input variables */
97 q7_t *pOut = pDst; /* output pointer */
98 q7_t out0, out1, out2, out3; /* temporary variables */
100 /* Check for range of output samples to be calculated */
101 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
103 /* Set status as ARM_MATH_ARGUMENT_ERROR */
104 status = ARM_MATH_ARGUMENT_ERROR;
109 /* The algorithm implementation is based on the lengths of the inputs. */
110 /* srcB is always made to slide across srcA. */
111 /* So srcBLen is always considered as shorter or equal to srcALen */
112 if(srcALen >= srcBLen)
114 /* Initialization of inputA pointer */
117 /* Initialization of inputB pointer */
122 /* Initialization of inputA pointer */
125 /* Initialization of inputB pointer */
128 /* srcBLen is always considered as shorter or equal to srcALen */
134 /* pointer to take end of scratch2 buffer */
137 /* points to smaller length sequence */
138 px = pIn2 + srcBLen - 1;
140 /* Apply loop unrolling and do 4 Copies simultaneously. */
143 /* First part of the processing with loop unrolling copies 4 data points at a time.
144 ** a second loop below copies for the remaining 1 to 3 samples. */
147 /* copy second buffer in reversal manner */
157 /* Decrement the loop counter */
161 /* If the count is not a multiple of 4, copy remaining samples here.
162 ** No loop unrolling is used. */
167 /* copy second buffer in reversal manner for remaining samples */
171 /* Decrement the loop counter */
175 /* Initialze temporary scratch pointer */
178 /* Fill (srcBLen - 1u) zeros in scratch buffer */
179 arm_fill_q15(0, pScr1, (srcBLen - 1u));
181 /* Update temporary scratch pointer */
182 pScr1 += (srcBLen - 1u);
184 /* Copy (srcALen) samples in scratch buffer */
185 /* Apply loop unrolling and do 4 Copies simultaneously. */
188 /* First part of the processing with loop unrolling copies 4 data points at a time.
189 ** a second loop below copies for the remaining 1 to 3 samples. */
192 /* copy second buffer in reversal manner */
193 x4 = (q15_t) * pIn1++;
195 x4 = (q15_t) * pIn1++;
197 x4 = (q15_t) * pIn1++;
199 x4 = (q15_t) * pIn1++;
202 /* Decrement the loop counter */
206 /* If the count is not a multiple of 4, copy remaining samples here.
207 ** No loop unrolling is used. */
212 /* copy second buffer in reversal manner for remaining samples */
213 x4 = (q15_t) * pIn1++;
216 /* Decrement the loop counter */
220 /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
221 arm_fill_q15(0, pScr1, (srcBLen - 1u));
224 pScr1 += (srcBLen - 1u);
227 /* Temporary pointer for scratch2 */
230 /* Initialization of pIn2 pointer */
235 pOut = pDst + firstIndex;
237 pScratch1 += firstIndex;
239 /* Actual convolution process starts here */
240 blkCnt = (numPoints) >> 2;
245 /* Initialze temporary scratch pointer as scratch1 */
248 /* Clear Accumlators */
254 /* Read two samples from scratch1 buffer */
255 x1 = *__SIMD32(pScr1)++;
257 /* Read next two samples from scratch1 buffer */
258 x2 = *__SIMD32(pScr1)++;
260 tapCnt = (srcBLen) >> 2u;
265 /* Read four samples from smaller buffer */
266 y1 = _SIMD32_OFFSET(pScr2);
268 /* multiply and accumlate */
269 acc0 = __SMLAD(x1, y1, acc0);
270 acc2 = __SMLAD(x2, y1, acc2);
272 /* pack input data */
273 #ifndef ARM_MATH_BIG_ENDIAN
274 x3 = __PKHBT(x2, x1, 0);
276 x3 = __PKHBT(x1, x2, 0);
279 /* multiply and accumlate */
280 acc1 = __SMLADX(x3, y1, acc1);
282 /* Read next two samples from scratch1 buffer */
283 x1 = *__SIMD32(pScr1)++;
285 /* pack input data */
286 #ifndef ARM_MATH_BIG_ENDIAN
287 x3 = __PKHBT(x1, x2, 0);
289 x3 = __PKHBT(x2, x1, 0);
292 acc3 = __SMLADX(x3, y1, acc3);
294 /* Read four samples from smaller buffer */
295 y1 = _SIMD32_OFFSET(pScr2 + 2u);
297 acc0 = __SMLAD(x2, y1, acc0);
299 acc2 = __SMLAD(x1, y1, acc2);
301 acc1 = __SMLADX(x3, y1, acc1);
303 x2 = *__SIMD32(pScr1)++;
305 #ifndef ARM_MATH_BIG_ENDIAN
306 x3 = __PKHBT(x2, x1, 0);
308 x3 = __PKHBT(x1, x2, 0);
311 acc3 = __SMLADX(x3, y1, acc3);
316 /* Decrement the loop counter */
322 /* Update scratch pointer for remaining samples of smaller length sequence */
326 /* apply same above for remaining samples of smaller length sequence */
327 tapCnt = (srcBLen) & 3u;
332 /* accumlate the results */
333 acc0 += (*pScr1++ * *pScr2);
334 acc1 += (*pScr1++ * *pScr2);
335 acc2 += (*pScr1++ * *pScr2);
336 acc3 += (*pScr1++ * *pScr2++);
340 /* Decrement the loop counter */
346 /* Store the result in the accumulator in the destination buffer. */
347 out0 = (q7_t) (__SSAT(acc0 >> 7u, 8));
348 out1 = (q7_t) (__SSAT(acc1 >> 7u, 8));
349 out2 = (q7_t) (__SSAT(acc2 >> 7u, 8));
350 out3 = (q7_t) (__SSAT(acc3 >> 7u, 8));
352 *__SIMD32(pOut)++ = __PACKq7(out0, out1, out2, out3);
354 /* Initialization of inputB pointer */
361 blkCnt = (numPoints) & 0x3;
363 /* Calculate convolution for remaining samples of Bigger length sequence */
366 /* Initialze temporary scratch pointer as scratch1 */
369 /* Clear Accumlators */
372 tapCnt = (srcBLen) >> 1u;
377 /* Read next two samples from scratch1 buffer */
378 x1 = *__SIMD32(pScr1)++;
380 /* Read two samples from smaller buffer */
381 y1 = *__SIMD32(pScr2)++;
383 acc0 = __SMLAD(x1, y1, acc0);
385 /* Decrement the loop counter */
389 tapCnt = (srcBLen) & 1u;
391 /* apply same above for remaining samples of smaller length sequence */
395 /* accumlate the results */
396 acc0 += (*pScr1++ * *pScr2++);
398 /* Decrement the loop counter */
404 /* Store the result in the accumulator in the destination buffer. */
405 *pOut++ = (q7_t) (__SSAT(acc0 >> 7u, 8));
407 /* Initialization of inputB pointer */
414 /* set status as ARM_MATH_SUCCESS */
415 status = ARM_MATH_SUCCESS;
426 arm_status arm_conv_partial_opt_q7(
438 q15_t *pScr2, *pScr1; /* Intermediate pointers for scratch pointers */
439 q15_t x4; /* Temporary input variable */
440 q7_t *pIn1, *pIn2; /* inputA and inputB pointer */
441 uint32_t j, k, blkCnt, tapCnt; /* loop counter */
442 q7_t *px; /* Temporary input1 pointer */
443 q15_t *py; /* Temporary input2 pointer */
444 q31_t acc0, acc1, acc2, acc3; /* Accumulator */
446 q7_t *pOut = pDst; /* output pointer */
447 q15_t x10, x11, x20, x21; /* Temporary input variables */
448 q15_t y10, y11; /* Temporary input variables */
449 q7_t out0, out1, out2, out3; /* temporary variables */
451 /* Check for range of output samples to be calculated */
452 if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
454 /* Set status as ARM_MATH_ARGUMENT_ERROR */
455 status = ARM_MATH_ARGUMENT_ERROR;
460 /* The algorithm implementation is based on the lengths of the inputs. */
461 /* srcB is always made to slide across srcA. */
462 /* So srcBLen is always considered as shorter or equal to srcALen */
463 if(srcALen >= srcBLen)
465 /* Initialization of inputA pointer */
468 /* Initialization of inputB pointer */
473 /* Initialization of inputA pointer */
476 /* Initialization of inputB pointer */
479 /* srcBLen is always considered as shorter or equal to srcALen */
485 /* pointer to take end of scratch2 buffer */
488 /* points to smaller length sequence */
489 px = pIn2 + srcBLen - 1;
491 /* Apply loop unrolling and do 4 Copies simultaneously. */
494 /* First part of the processing with loop unrolling copies 4 data points at a time.
495 ** a second loop below copies for the remaining 1 to 3 samples. */
498 /* copy second buffer in reversal manner */
508 /* Decrement the loop counter */
512 /* If the count is not a multiple of 4, copy remaining samples here.
513 ** No loop unrolling is used. */
518 /* copy second buffer in reversal manner for remaining samples */
522 /* Decrement the loop counter */
526 /* Initialze temporary scratch pointer */
529 /* Fill (srcBLen - 1u) zeros in scratch buffer */
530 arm_fill_q15(0, pScr1, (srcBLen - 1u));
532 /* Update temporary scratch pointer */
533 pScr1 += (srcBLen - 1u);
535 /* Copy (srcALen) samples in scratch buffer */
536 /* Apply loop unrolling and do 4 Copies simultaneously. */
539 /* First part of the processing with loop unrolling copies 4 data points at a time.
540 ** a second loop below copies for the remaining 1 to 3 samples. */
543 /* copy second buffer in reversal manner */
544 x4 = (q15_t) * pIn1++;
546 x4 = (q15_t) * pIn1++;
548 x4 = (q15_t) * pIn1++;
550 x4 = (q15_t) * pIn1++;
553 /* Decrement the loop counter */
557 /* If the count is not a multiple of 4, copy remaining samples here.
558 ** No loop unrolling is used. */
563 /* copy second buffer in reversal manner for remaining samples */
564 x4 = (q15_t) * pIn1++;
567 /* Decrement the loop counter */
571 /* Apply loop unrolling and do 4 Copies simultaneously. */
572 k = (srcBLen - 1u) >> 2u;
574 /* First part of the processing with loop unrolling copies 4 data points at a time.
575 ** a second loop below copies for the remaining 1 to 3 samples. */
578 /* copy second buffer in reversal manner */
584 /* Decrement the loop counter */
588 /* If the count is not a multiple of 4, copy remaining samples here.
589 ** No loop unrolling is used. */
590 k = (srcBLen - 1u) % 0x4u;
594 /* copy second buffer in reversal manner for remaining samples */
597 /* Decrement the loop counter */
602 /* Temporary pointer for scratch2 */
605 /* Initialization of pIn2 pointer */
610 pOut = pDst + firstIndex;
612 pScratch1 += firstIndex;
614 /* Actual convolution process starts here */
615 blkCnt = (numPoints) >> 2;
620 /* Initialze temporary scratch pointer as scratch1 */
623 /* Clear Accumlators */
629 /* Read two samples from scratch1 buffer */
633 /* Read next two samples from scratch1 buffer */
637 tapCnt = (srcBLen) >> 2u;
642 /* Read four samples from smaller buffer */
646 /* multiply and accumlate */
647 acc0 += (q31_t) x10 *y10;
648 acc0 += (q31_t) x11 *y11;
649 acc2 += (q31_t) x20 *y10;
650 acc2 += (q31_t) x21 *y11;
653 acc1 += (q31_t) x11 *y10;
654 acc1 += (q31_t) x20 *y11;
656 /* Read next two samples from scratch1 buffer */
660 /* multiply and accumlate */
661 acc3 += (q31_t) x21 *y10;
662 acc3 += (q31_t) x10 *y11;
664 /* Read next two samples from scratch2 buffer */
668 /* multiply and accumlate */
669 acc0 += (q31_t) x20 *y10;
670 acc0 += (q31_t) x21 *y11;
671 acc2 += (q31_t) x10 *y10;
672 acc2 += (q31_t) x11 *y11;
673 acc1 += (q31_t) x21 *y10;
674 acc1 += (q31_t) x10 *y11;
676 /* Read next two samples from scratch1 buffer */
680 /* multiply and accumlate */
681 acc3 += (q31_t) x11 *y10;
682 acc3 += (q31_t) x20 *y11;
684 /* update scratch pointers */
689 /* Decrement the loop counter */
695 /* Update scratch pointer for remaining samples of smaller length sequence */
699 /* apply same above for remaining samples of smaller length sequence */
700 tapCnt = (srcBLen) & 3u;
705 /* accumlate the results */
706 acc0 += (*pScr1++ * *pScr2);
707 acc1 += (*pScr1++ * *pScr2);
708 acc2 += (*pScr1++ * *pScr2);
709 acc3 += (*pScr1++ * *pScr2++);
713 /* Decrement the loop counter */
719 /* Store the result in the accumulator in the destination buffer. */
720 out0 = (q7_t) (__SSAT(acc0 >> 7u, 8));
721 out1 = (q7_t) (__SSAT(acc1 >> 7u, 8));
722 out2 = (q7_t) (__SSAT(acc2 >> 7u, 8));
723 out3 = (q7_t) (__SSAT(acc3 >> 7u, 8));
726 *__SIMD32(pOut)++ = __PACKq7(out0, out1, out2, out3);
728 /* Initialization of inputB pointer */
735 blkCnt = (numPoints) & 0x3;
737 /* Calculate convolution for remaining samples of Bigger length sequence */
740 /* Initialze temporary scratch pointer as scratch1 */
743 /* Clear Accumlators */
746 tapCnt = (srcBLen) >> 1u;
751 /* Read next two samples from scratch1 buffer */
755 /* Read two samples from smaller buffer */
759 /* multiply and accumlate */
760 acc0 += (q31_t) x10 *y10;
761 acc0 += (q31_t) x11 *y11;
763 /* Decrement the loop counter */
767 tapCnt = (srcBLen) & 1u;
769 /* apply same above for remaining samples of smaller length sequence */
773 /* accumlate the results */
774 acc0 += (*pScr1++ * *pScr2++);
776 /* Decrement the loop counter */
782 /* Store the result in the accumulator in the destination buffer. */
783 *pOut++ = (q7_t) (__SSAT(acc0 >> 7u, 8));
785 /* Initialization of inputB pointer */
792 /* set status as ARM_MATH_SUCCESS */
793 status = ARM_MATH_SUCCESS;
801 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
806 * @} end of PartialConv group