1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
4 * $Date: 19. March 2015
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_partial_q7.c
10 * Description: Partial convolution of Q7 sequences.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
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 * @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].
64 * Refer the function <code>arm_conv_partial_opt_q7()</code> for a faster implementation of this function.
68 arm_status
arm_conv_partial_q7(
79 #ifndef ARM_MATH_CM0_FAMILY
81 /* Run the below code for Cortex-M4 and Cortex-M3 */
83 q7_t
*pIn1
; /* inputA pointer */
84 q7_t
*pIn2
; /* inputB pointer */
85 q7_t
*pOut
= pDst
; /* output pointer */
86 q7_t
*px
; /* Intermediate inputA pointer */
87 q7_t
*py
; /* Intermediate inputB pointer */
88 q7_t
*pSrc1
, *pSrc2
; /* Intermediate pointers */
89 q31_t sum
, acc0
, acc1
, acc2
, acc3
; /* Accumulator */
92 q7_t x0
, x1
, x2
, x3
, c0
, c1
;
93 uint32_t j
, k
, count
, check
, blkCnt
;
94 int32_t blockSize1
, blockSize2
, blockSize3
; /* loop counter */
98 /* Check for range of output samples to be calculated */
99 if((firstIndex
+ numPoints
) > ((srcALen
+ (srcBLen
- 1u))))
101 /* Set status as ARM_MATH_ARGUMENT_ERROR */
102 status
= ARM_MATH_ARGUMENT_ERROR
;
107 /* The algorithm implementation is based on the lengths of the inputs. */
108 /* srcB is always made to slide across srcA. */
109 /* So srcBLen is always considered as shorter or equal to srcALen */
110 if(srcALen
>= srcBLen
)
112 /* Initialization of inputA pointer */
115 /* Initialization of inputB pointer */
120 /* Initialization of inputA pointer */
123 /* Initialization of inputB pointer */
126 /* srcBLen is always considered as shorter or equal to srcALen */
132 /* Conditions to check which loopCounter holds
133 * the first and last indices of the output samples to be calculated. */
134 check
= firstIndex
+ numPoints
;
135 blockSize3
= ((int32_t)check
> (int32_t)srcALen
) ? (int32_t)check
- (int32_t)srcALen
: 0;
136 blockSize3
= ((int32_t)firstIndex
> (int32_t)srcALen
- 1) ? blockSize3
- (int32_t)firstIndex
+ (int32_t)srcALen
: blockSize3
;
137 blockSize1
= (((int32_t) srcBLen
- 1) - (int32_t) firstIndex
);
138 blockSize1
= (blockSize1
> 0) ? ((check
> (srcBLen
- 1u)) ? blockSize1
:
139 (int32_t) numPoints
) : 0;
140 blockSize2
= (int32_t) check
- ((blockSize3
+ blockSize1
) +
141 (int32_t) firstIndex
);
142 blockSize2
= (blockSize2
> 0) ? blockSize2
: 0;
144 /* 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] */
145 /* The function is internally
146 * divided into three stages according to the number of multiplications that has to be
147 * taken place between inputA samples and inputB samples. In the first stage of the
148 * algorithm, the multiplications increase by one for every iteration.
149 * In the second stage of the algorithm, srcBLen number of multiplications are done.
150 * In the third stage of the algorithm, the multiplications decrease by one
151 * for every iteration. */
153 /* Set the output pointer to point to the firstIndex
154 * of the output sample to be calculated. */
155 pOut
= pDst
+ firstIndex
;
157 /* --------------------------
158 * Initializations of stage1
159 * -------------------------*/
162 * sum = x[0] * y[1] + x[1] * y[0]
164 * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
167 /* In this stage the MAC operations are increased by 1 for every iteration.
168 The count variable holds the number of MAC operations performed.
169 Since the partial convolution starts from from firstIndex
170 Number of Macs to be performed is firstIndex + 1 */
171 count
= 1u + firstIndex
;
173 /* Working pointer of inputA */
176 /* Working pointer of inputB */
177 pSrc2
= pIn2
+ firstIndex
;
180 /* ------------------------
182 * ----------------------*/
184 /* The first stage starts here */
185 while(blockSize1
> 0)
187 /* Accumulator is made zero for every iteration */
190 /* Apply loop unrolling and compute 4 MACs simultaneously. */
193 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
194 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
198 in1
= (q15_t
) * px
++;
199 in2
= (q15_t
) * px
++;
200 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
202 /* y[srcBLen - 1] , y[srcBLen - 2] */
203 in1
= (q15_t
) * py
--;
204 in2
= (q15_t
) * py
--;
205 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
207 /* x[0] * y[srcBLen - 1] */
208 /* x[1] * y[srcBLen - 2] */
209 sum
= __SMLAD(input1
, input2
, sum
);
212 in1
= (q15_t
) * px
++;
213 in2
= (q15_t
) * px
++;
214 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
216 /* y[srcBLen - 3] , y[srcBLen - 4] */
217 in1
= (q15_t
) * py
--;
218 in2
= (q15_t
) * py
--;
219 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
221 /* x[2] * y[srcBLen - 3] */
222 /* x[3] * y[srcBLen - 4] */
223 sum
= __SMLAD(input1
, input2
, sum
);
225 /* Decrement the loop counter */
229 /* If the count is not a multiple of 4, compute any remaining MACs here.
230 ** No loop unrolling is used. */
235 /* Perform the multiply-accumulates */
236 sum
+= ((q31_t
) * px
++ * *py
--);
238 /* Decrement the loop counter */
242 /* Store the result in the accumulator in the destination buffer. */
243 *pOut
++ = (q7_t
) (__SSAT(sum
>> 7, 8));
245 /* Update the inputA and inputB pointers for next MAC calculation */
249 /* Increment the MAC count */
252 /* Decrement the loop counter */
256 /* --------------------------
257 * Initializations of stage2
258 * ------------------------*/
260 /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
261 * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
263 * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
266 /* Working pointer of inputA */
267 if((int32_t)firstIndex
- (int32_t)srcBLen
+ 1 > 0)
269 px
= pIn1
+ firstIndex
- srcBLen
+ 1;
276 /* Working pointer of inputB */
277 pSrc2
= pIn2
+ (srcBLen
- 1u);
280 /* count is index by which the pointer pIn1 to be incremented */
283 /* -------------------
285 * ------------------*/
287 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
288 * So, to loop unroll over blockSize2,
289 * srcBLen should be greater than or equal to 4 */
292 /* Loop unroll over blockSize2, by 4 */
293 blkCnt
= ((uint32_t) blockSize2
>> 2u);
297 /* Set all accumulators to zero */
303 /* read x[0], x[1], x[2] samples */
308 /* Apply loop unrolling and compute 4 MACs simultaneously. */
311 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
312 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
315 /* Read y[srcBLen - 1] sample */
317 /* Read y[srcBLen - 2] sample */
320 /* Read x[3] sample */
323 /* x[0] and x[1] are packed */
327 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
329 /* y[srcBLen - 1] and y[srcBLen - 2] are packed */
333 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
335 /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
336 acc0
= __SMLAD(input1
, input2
, acc0
);
338 /* x[1] and x[2] are packed */
342 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
344 /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
345 acc1
= __SMLAD(input1
, input2
, acc1
);
347 /* x[2] and x[3] are packed */
351 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
353 /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
354 acc2
= __SMLAD(input1
, input2
, acc2
);
356 /* Read x[4] sample */
359 /* x[3] and x[4] are packed */
363 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
365 /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
366 acc3
= __SMLAD(input1
, input2
, acc3
);
368 /* Read y[srcBLen - 3] sample */
370 /* Read y[srcBLen - 4] sample */
373 /* Read x[5] sample */
376 /* x[2] and x[3] are packed */
380 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
382 /* y[srcBLen - 3] and y[srcBLen - 4] are packed */
386 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
388 /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
389 acc0
= __SMLAD(input1
, input2
, acc0
);
391 /* x[3] and x[4] are packed */
395 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
397 /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
398 acc1
= __SMLAD(input1
, input2
, acc1
);
400 /* x[4] and x[5] are packed */
404 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
406 /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
407 acc2
= __SMLAD(input1
, input2
, acc2
);
409 /* Read x[6] sample */
412 /* x[5] and x[6] are packed */
416 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
418 /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
419 acc3
= __SMLAD(input1
, input2
, acc3
);
423 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
424 ** No loop unrolling is used. */
429 /* Read y[srcBLen - 5] sample */
432 /* Read x[7] sample */
435 /* Perform the multiply-accumulates */
436 /* acc0 += x[4] * y[srcBLen - 5] */
437 acc0
+= ((q31_t
) x0
* c0
);
438 /* acc1 += x[5] * y[srcBLen - 5] */
439 acc1
+= ((q31_t
) x1
* c0
);
440 /* acc2 += x[6] * y[srcBLen - 5] */
441 acc2
+= ((q31_t
) x2
* c0
);
442 /* acc3 += x[7] * y[srcBLen - 5] */
443 acc3
+= ((q31_t
) x3
* c0
);
445 /* Reuse the present samples for the next MAC */
450 /* Decrement the loop counter */
454 /* Store the result in the accumulator in the destination buffer. */
455 *pOut
++ = (q7_t
) (__SSAT(acc0
>> 7, 8));
456 *pOut
++ = (q7_t
) (__SSAT(acc1
>> 7, 8));
457 *pOut
++ = (q7_t
) (__SSAT(acc2
>> 7, 8));
458 *pOut
++ = (q7_t
) (__SSAT(acc3
>> 7, 8));
460 /* Increment the pointer pIn1 index, count by 4 */
463 /* Update the inputA and inputB pointers for next MAC calculation */
468 /* Decrement the loop counter */
472 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
473 ** No loop unrolling is used. */
474 blkCnt
= (uint32_t) blockSize2
% 0x4u
;
478 /* Accumulator is made zero for every iteration */
481 /* Apply loop unrolling and compute 4 MACs simultaneously. */
484 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
485 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
489 /* Reading two inputs of SrcA buffer and packing */
490 in1
= (q15_t
) * px
++;
491 in2
= (q15_t
) * px
++;
492 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
494 /* Reading two inputs of SrcB buffer and packing */
495 in1
= (q15_t
) * py
--;
496 in2
= (q15_t
) * py
--;
497 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
499 /* Perform the multiply-accumulates */
500 sum
= __SMLAD(input1
, input2
, sum
);
502 /* Reading two inputs of SrcA buffer and packing */
503 in1
= (q15_t
) * px
++;
504 in2
= (q15_t
) * px
++;
505 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
507 /* Reading two inputs of SrcB buffer and packing */
508 in1
= (q15_t
) * py
--;
509 in2
= (q15_t
) * py
--;
510 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
512 /* Perform the multiply-accumulates */
513 sum
= __SMLAD(input1
, input2
, sum
);
515 /* Decrement the loop counter */
519 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
520 ** No loop unrolling is used. */
525 /* Perform the multiply-accumulates */
526 sum
+= ((q31_t
) * px
++ * *py
--);
528 /* Decrement the loop counter */
532 /* Store the result in the accumulator in the destination buffer. */
533 *pOut
++ = (q7_t
) (__SSAT(sum
>> 7, 8));
535 /* Increment the pointer pIn1 index, count by 1 */
538 /* Update the inputA and inputB pointers for next MAC calculation */
542 /* Decrement the loop counter */
548 /* If the srcBLen is not a multiple of 4,
549 * the blockSize2 loop cannot be unrolled by 4 */
550 blkCnt
= (uint32_t) blockSize2
;
554 /* Accumulator is made zero for every iteration */
557 /* srcBLen number of MACS should be performed */
562 /* Perform the multiply-accumulate */
563 sum
+= ((q31_t
) * px
++ * *py
--);
565 /* Decrement the loop counter */
569 /* Store the result in the accumulator in the destination buffer. */
570 *pOut
++ = (q7_t
) (__SSAT(sum
>> 7, 8));
572 /* Increment the MAC count */
575 /* Update the inputA and inputB pointers for next MAC calculation */
579 /* Decrement the loop counter */
585 /* --------------------------
586 * Initializations of stage3
587 * -------------------------*/
589 /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
590 * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
592 * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
593 * sum += x[srcALen-1] * y[srcBLen-1]
596 /* In this stage the MAC operations are decreased by 1 for every iteration.
597 The count variable holds the number of MAC operations performed */
598 count
= srcBLen
- 1u;
600 /* Working pointer of inputA */
601 pSrc1
= (pIn1
+ srcALen
) - (srcBLen
- 1u);
604 /* Working pointer of inputB */
605 pSrc2
= pIn2
+ (srcBLen
- 1u);
608 /* -------------------
610 * ------------------*/
612 while(blockSize3
> 0)
614 /* Accumulator is made zero for every iteration */
617 /* Apply loop unrolling and compute 4 MACs simultaneously. */
620 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
621 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
624 /* Reading two inputs, x[srcALen - srcBLen + 1] and x[srcALen - srcBLen + 2] of SrcA buffer and packing */
625 in1
= (q15_t
) * px
++;
626 in2
= (q15_t
) * px
++;
627 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
629 /* Reading two inputs, y[srcBLen - 1] and y[srcBLen - 2] of SrcB buffer and packing */
630 in1
= (q15_t
) * py
--;
631 in2
= (q15_t
) * py
--;
632 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
634 /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
635 /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
636 sum
= __SMLAD(input1
, input2
, sum
);
638 /* Reading two inputs, x[srcALen - srcBLen + 3] and x[srcALen - srcBLen + 4] of SrcA buffer and packing */
639 in1
= (q15_t
) * px
++;
640 in2
= (q15_t
) * px
++;
641 input1
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
643 /* Reading two inputs, y[srcBLen - 3] and y[srcBLen - 4] of SrcB buffer and packing */
644 in1
= (q15_t
) * py
--;
645 in2
= (q15_t
) * py
--;
646 input2
= ((q31_t
) in1
& 0x0000FFFF) | ((q31_t
) in2
<< 16);
648 /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
649 /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
650 sum
= __SMLAD(input1
, input2
, sum
);
652 /* Decrement the loop counter */
656 /* If the count is not a multiple of 4, compute any remaining MACs here.
657 ** No loop unrolling is used. */
662 /* Perform the multiply-accumulates */
663 /* sum += x[srcALen-1] * y[srcBLen-1] */
664 sum
+= ((q31_t
) * px
++ * *py
--);
666 /* Decrement the loop counter */
670 /* Store the result in the accumulator in the destination buffer. */
671 *pOut
++ = (q7_t
) (__SSAT(sum
>> 7, 8));
673 /* Update the inputA and inputB pointers for next MAC calculation */
677 /* Decrement the MAC count */
680 /* Decrement the loop counter */
685 /* set status as ARM_MATH_SUCCESS */
686 status
= ARM_MATH_SUCCESS
;
689 /* Return to application */
694 /* Run the below code for Cortex-M0 */
696 q7_t
*pIn1
= pSrcA
; /* inputA pointer */
697 q7_t
*pIn2
= pSrcB
; /* inputB pointer */
698 q31_t sum
; /* Accumulator */
699 uint32_t i
, j
; /* loop counters */
700 arm_status status
; /* status of Partial convolution */
702 /* Check for range of output samples to be calculated */
703 if((firstIndex
+ numPoints
) > ((srcALen
+ (srcBLen
- 1u))))
705 /* Set status as ARM_ARGUMENT_ERROR */
706 status
= ARM_MATH_ARGUMENT_ERROR
;
710 /* Loop to calculate convolution for output length number of values */
711 for (i
= firstIndex
; i
<= (firstIndex
+ numPoints
- 1); i
++)
713 /* Initialize sum with zero to carry on MAC operations */
716 /* Loop to perform MAC operations according to convolution equation */
717 for (j
= 0; j
<= i
; j
++)
719 /* Check the array limitations */
720 if(((i
- j
) < srcBLen
) && (j
< srcALen
))
722 /* z[i] += x[i-j] * y[j] */
723 sum
+= ((q15_t
) pIn1
[j
] * (pIn2
[i
- j
]));
727 /* Store the output in the destination buffer */
728 pDst
[i
] = (q7_t
) __SSAT((sum
>> 7u), 8u);
730 /* set status as ARM_SUCCESS as there are no argument errors */
731 status
= ARM_MATH_SUCCESS
;
735 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
740 * @} end of PartialConv group