]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_conv_partial_q7.c
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_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
);
136 blockSize3
= ( blockSize3
> 0 ) ? blockSize3
: 0 ;
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 */
269 /* Working pointer of inputB */
270 pSrc2
= pIn2
+ ( srcBLen
- 1u );
273 /* count is index by which the pointer pIn1 to be incremented */
276 /* -------------------
278 * ------------------*/
280 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
281 * So, to loop unroll over blockSize2,
282 * srcBLen should be greater than or equal to 4 */
285 /* Loop unroll over blockSize2, by 4 */
286 blkCnt
= (( uint32_t ) blockSize2
>> 2u );
290 /* Set all accumulators to zero */
296 /* read x[0], x[1], x[2] samples */
301 /* Apply loop unrolling and compute 4 MACs simultaneously. */
304 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
305 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
308 /* Read y[srcBLen - 1] sample */
310 /* Read y[srcBLen - 2] sample */
313 /* Read x[3] sample */
316 /* x[0] and x[1] are packed */
320 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
322 /* y[srcBLen - 1] and y[srcBLen - 2] are packed */
326 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
328 /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
329 acc0
= __SMLAD ( input1
, input2
, acc0
);
331 /* x[1] and x[2] are packed */
335 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
337 /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
338 acc1
= __SMLAD ( input1
, input2
, acc1
);
340 /* x[2] and x[3] are packed */
344 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
346 /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
347 acc2
= __SMLAD ( input1
, input2
, acc2
);
349 /* Read x[4] sample */
352 /* x[3] and x[4] are packed */
356 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
358 /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
359 acc3
= __SMLAD ( input1
, input2
, acc3
);
361 /* Read y[srcBLen - 3] sample */
363 /* Read y[srcBLen - 4] sample */
366 /* Read x[5] sample */
369 /* x[2] and x[3] are packed */
373 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
375 /* y[srcBLen - 3] and y[srcBLen - 4] are packed */
379 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
381 /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
382 acc0
= __SMLAD ( input1
, input2
, acc0
);
384 /* x[3] and x[4] are packed */
388 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
390 /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
391 acc1
= __SMLAD ( input1
, input2
, acc1
);
393 /* x[4] and x[5] are packed */
397 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
399 /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
400 acc2
= __SMLAD ( input1
, input2
, acc2
);
402 /* Read x[6] sample */
405 /* x[5] and x[6] are packed */
409 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
411 /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
412 acc3
= __SMLAD ( input1
, input2
, acc3
);
416 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
417 ** No loop unrolling is used. */
422 /* Read y[srcBLen - 5] sample */
425 /* Read x[7] sample */
428 /* Perform the multiply-accumulates */
429 /* acc0 += x[4] * y[srcBLen - 5] */
430 acc0
+= (( q31_t
) x0
* c0
);
431 /* acc1 += x[5] * y[srcBLen - 5] */
432 acc1
+= (( q31_t
) x1
* c0
);
433 /* acc2 += x[6] * y[srcBLen - 5] */
434 acc2
+= (( q31_t
) x2
* c0
);
435 /* acc3 += x[7] * y[srcBLen - 5] */
436 acc3
+= (( q31_t
) x3
* c0
);
438 /* Reuse the present samples for the next MAC */
443 /* Decrement the loop counter */
447 /* Store the result in the accumulator in the destination buffer. */
448 * pOut
++ = ( q7_t
) ( __SSAT ( acc0
>> 7 , 8 ));
449 * pOut
++ = ( q7_t
) ( __SSAT ( acc1
>> 7 , 8 ));
450 * pOut
++ = ( q7_t
) ( __SSAT ( acc2
>> 7 , 8 ));
451 * pOut
++ = ( q7_t
) ( __SSAT ( acc3
>> 7 , 8 ));
453 /* Increment the pointer pIn1 index, count by 4 */
456 /* Update the inputA and inputB pointers for next MAC calculation */
461 /* Decrement the loop counter */
465 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
466 ** No loop unrolling is used. */
467 blkCnt
= ( uint32_t ) blockSize2
% 0x4 u
;
471 /* Accumulator is made zero for every iteration */
474 /* Apply loop unrolling and compute 4 MACs simultaneously. */
477 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
478 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
482 /* Reading two inputs of SrcA buffer and packing */
483 in1
= ( q15_t
) * px
++;
484 in2
= ( q15_t
) * px
++;
485 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
487 /* Reading two inputs of SrcB buffer and packing */
488 in1
= ( q15_t
) * py
--;
489 in2
= ( q15_t
) * py
--;
490 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
492 /* Perform the multiply-accumulates */
493 sum
= __SMLAD ( input1
, input2
, sum
);
495 /* Reading two inputs of SrcA buffer and packing */
496 in1
= ( q15_t
) * px
++;
497 in2
= ( q15_t
) * px
++;
498 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
500 /* Reading two inputs of SrcB buffer and packing */
501 in1
= ( q15_t
) * py
--;
502 in2
= ( q15_t
) * py
--;
503 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
505 /* Perform the multiply-accumulates */
506 sum
= __SMLAD ( input1
, input2
, sum
);
508 /* Decrement the loop counter */
512 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
513 ** No loop unrolling is used. */
518 /* Perform the multiply-accumulates */
519 sum
+= (( q31_t
) * px
++ * * py
--);
521 /* Decrement the loop counter */
525 /* Store the result in the accumulator in the destination buffer. */
526 * pOut
++ = ( q7_t
) ( __SSAT ( sum
>> 7 , 8 ));
528 /* Increment the pointer pIn1 index, count by 1 */
531 /* Update the inputA and inputB pointers for next MAC calculation */
535 /* Decrement the loop counter */
541 /* If the srcBLen is not a multiple of 4,
542 * the blockSize2 loop cannot be unrolled by 4 */
543 blkCnt
= ( uint32_t ) blockSize2
;
547 /* Accumulator is made zero for every iteration */
550 /* srcBLen number of MACS should be performed */
555 /* Perform the multiply-accumulate */
556 sum
+= (( q31_t
) * px
++ * * py
--);
558 /* Decrement the loop counter */
562 /* Store the result in the accumulator in the destination buffer. */
563 * pOut
++ = ( q7_t
) ( __SSAT ( sum
>> 7 , 8 ));
565 /* Increment the MAC count */
568 /* Update the inputA and inputB pointers for next MAC calculation */
572 /* Decrement the loop counter */
578 /* --------------------------
579 * Initializations of stage3
580 * -------------------------*/
582 /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
583 * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
585 * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
586 * sum += x[srcALen-1] * y[srcBLen-1]
589 /* In this stage the MAC operations are decreased by 1 for every iteration.
590 The count variable holds the number of MAC operations performed */
591 count
= srcBLen
- 1u ;
593 /* Working pointer of inputA */
594 pSrc1
= ( pIn1
+ srcALen
) - ( srcBLen
- 1u );
597 /* Working pointer of inputB */
598 pSrc2
= pIn2
+ ( srcBLen
- 1u );
601 /* -------------------
603 * ------------------*/
605 while ( blockSize3
> 0 )
607 /* Accumulator is made zero for every iteration */
610 /* Apply loop unrolling and compute 4 MACs simultaneously. */
613 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
614 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
617 /* Reading two inputs, x[srcALen - srcBLen + 1] and x[srcALen - srcBLen + 2] of SrcA buffer and packing */
618 in1
= ( q15_t
) * px
++;
619 in2
= ( q15_t
) * px
++;
620 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
622 /* Reading two inputs, y[srcBLen - 1] and y[srcBLen - 2] of SrcB buffer and packing */
623 in1
= ( q15_t
) * py
--;
624 in2
= ( q15_t
) * py
--;
625 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
627 /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
628 /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
629 sum
= __SMLAD ( input1
, input2
, sum
);
631 /* Reading two inputs, x[srcALen - srcBLen + 3] and x[srcALen - srcBLen + 4] of SrcA buffer and packing */
632 in1
= ( q15_t
) * px
++;
633 in2
= ( q15_t
) * px
++;
634 input1
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
636 /* Reading two inputs, y[srcBLen - 3] and y[srcBLen - 4] of SrcB buffer and packing */
637 in1
= ( q15_t
) * py
--;
638 in2
= ( q15_t
) * py
--;
639 input2
= (( q31_t
) in1
& 0x0000FFFF ) | (( q31_t
) in2
<< 16 );
641 /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
642 /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
643 sum
= __SMLAD ( input1
, input2
, sum
);
645 /* Decrement the loop counter */
649 /* If the count is not a multiple of 4, compute any remaining MACs here.
650 ** No loop unrolling is used. */
655 /* Perform the multiply-accumulates */
656 /* sum += x[srcALen-1] * y[srcBLen-1] */
657 sum
+= (( q31_t
) * px
++ * * py
--);
659 /* Decrement the loop counter */
663 /* Store the result in the accumulator in the destination buffer. */
664 * pOut
++ = ( q7_t
) ( __SSAT ( sum
>> 7 , 8 ));
666 /* Update the inputA and inputB pointers for next MAC calculation */
670 /* Decrement the MAC count */
673 /* Decrement the loop counter */
678 /* set status as ARM_MATH_SUCCESS */
679 status
= ARM_MATH_SUCCESS
;
682 /* Return to application */
687 /* Run the below code for Cortex-M0 */
689 q7_t
* pIn1
= pSrcA
; /* inputA pointer */
690 q7_t
* pIn2
= pSrcB
; /* inputB pointer */
691 q31_t sum
; /* Accumulator */
692 uint32_t i
, j
; /* loop counters */
693 arm_status status
; /* status of Partial convolution */
695 /* Check for range of output samples to be calculated */
696 if (( firstIndex
+ numPoints
) > (( srcALen
+ ( srcBLen
- 1u ))))
698 /* Set status as ARM_ARGUMENT_ERROR */
699 status
= ARM_MATH_ARGUMENT_ERROR
;
703 /* Loop to calculate convolution for output length number of values */
704 for ( i
= firstIndex
; i
<= ( firstIndex
+ numPoints
- 1 ); i
++)
706 /* Initialize sum with zero to carry on MAC operations */
709 /* Loop to perform MAC operations according to convolution equation */
710 for ( j
= 0 ; j
<= i
; j
++)
712 /* Check the array limitations */
713 if ((( i
- j
) < srcBLen
) && ( j
< srcALen
))
715 /* z[i] += x[i-j] * y[j] */
716 sum
+= (( q15_t
) pIn1
[ j
] * ( pIn2
[ i
- j
]));
720 /* Store the output in the destination buffer */
721 pDst
[ i
] = ( q7_t
) __SSAT (( sum
>> 7u ), 8u );
723 /* set status as ARM_SUCCESS as there are no argument errors */
724 status
= ARM_MATH_SUCCESS
;
728 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
733 * @} end of PartialConv group