]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_conv_partial_q31.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_q31.c
10 * Description: Partial convolution of Q31 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 Q31 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].
63 * See <code>arm_conv_partial_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
66 arm_status
arm_conv_partial_q31 (
77 #ifndef ARM_MATH_CM0_FAMILY
79 /* Run the below code for Cortex-M4 and Cortex-M3 */
81 q31_t
* pIn1
; /* inputA pointer */
82 q31_t
* pIn2
; /* inputB pointer */
83 q31_t
* pOut
= pDst
; /* output pointer */
84 q31_t
* px
; /* Intermediate inputA pointer */
85 q31_t
* py
; /* Intermediate inputB pointer */
86 q31_t
* pSrc1
, * pSrc2
; /* Intermediate pointers */
87 q63_t sum
, acc0
, acc1
, acc2
; /* Accumulator */
89 uint32_t j
, k
, count
, check
, blkCnt
;
90 int32_t blockSize1
, blockSize2
, blockSize3
; /* loop counter */
91 arm_status status
; /* status of Partial convolution */
94 /* Check for range of output samples to be calculated */
95 if (( firstIndex
+ numPoints
) > (( srcALen
+ ( srcBLen
- 1u ))))
97 /* Set status as ARM_MATH_ARGUMENT_ERROR */
98 status
= ARM_MATH_ARGUMENT_ERROR
;
103 /* The algorithm implementation is based on the lengths of the inputs. */
104 /* srcB is always made to slide across srcA. */
105 /* So srcBLen is always considered as shorter or equal to srcALen */
106 if ( srcALen
>= srcBLen
)
108 /* Initialization of inputA pointer */
111 /* Initialization of inputB pointer */
116 /* Initialization of inputA pointer */
119 /* Initialization of inputB pointer */
122 /* srcBLen is always considered as shorter or equal to srcALen */
128 /* Conditions to check which loopCounter holds
129 * the first and last indices of the output samples to be calculated. */
130 check
= firstIndex
+ numPoints
;
131 blockSize3
= (( int32_t ) check
- ( int32_t ) srcALen
);
132 blockSize3
= ( blockSize3
> 0 ) ? blockSize3
: 0 ;
133 blockSize1
= ((( int32_t ) srcBLen
- 1 ) - ( int32_t ) firstIndex
);
134 blockSize1
= ( blockSize1
> 0 ) ? (( check
> ( srcBLen
- 1u )) ? blockSize1
:
135 ( int32_t ) numPoints
) : 0 ;
136 blockSize2
= ( int32_t ) check
- (( blockSize3
+ blockSize1
) +
137 ( int32_t ) firstIndex
);
138 blockSize2
= ( blockSize2
> 0 ) ? blockSize2
: 0 ;
140 /* 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] */
141 /* The function is internally
142 * divided into three stages according to the number of multiplications that has to be
143 * taken place between inputA samples and inputB samples. In the first stage of the
144 * algorithm, the multiplications increase by one for every iteration.
145 * In the second stage of the algorithm, srcBLen number of multiplications are done.
146 * In the third stage of the algorithm, the multiplications decrease by one
147 * for every iteration. */
149 /* Set the output pointer to point to the firstIndex
150 * of the output sample to be calculated. */
151 pOut
= pDst
+ firstIndex
;
153 /* --------------------------
154 * Initializations of stage1
155 * -------------------------*/
158 * sum = x[0] * y[1] + x[1] * y[0]
160 * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
163 /* In this stage the MAC operations are increased by 1 for every iteration.
164 The count variable holds the number of MAC operations performed.
165 Since the partial convolution starts from firstIndex
166 Number of Macs to be performed is firstIndex + 1 */
167 count
= 1u + firstIndex
;
169 /* Working pointer of inputA */
172 /* Working pointer of inputB */
173 pSrc2
= pIn2
+ firstIndex
;
176 /* ------------------------
178 * ----------------------*/
180 /* The first loop starts here */
181 while ( blockSize1
> 0 )
183 /* Accumulator is made zero for every iteration */
186 /* Apply loop unrolling and compute 4 MACs simultaneously. */
189 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
190 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
193 /* x[0] * y[srcBLen - 1] */
194 sum
+= ( q63_t
) * px
++ * (* py
--);
195 /* x[1] * y[srcBLen - 2] */
196 sum
+= ( q63_t
) * px
++ * (* py
--);
197 /* x[2] * y[srcBLen - 3] */
198 sum
+= ( q63_t
) * px
++ * (* py
--);
199 /* x[3] * y[srcBLen - 4] */
200 sum
+= ( q63_t
) * px
++ * (* py
--);
202 /* Decrement the loop counter */
206 /* If the count is not a multiple of 4, compute any remaining MACs here.
207 ** No loop unrolling is used. */
212 /* Perform the multiply-accumulate */
213 sum
+= ( q63_t
) * px
++ * (* py
--);
215 /* Decrement the loop counter */
219 /* Store the result in the accumulator in the destination buffer. */
220 * pOut
++ = ( q31_t
) ( sum
>> 31 );
222 /* Update the inputA and inputB pointers for next MAC calculation */
226 /* Increment the MAC count */
229 /* Decrement the loop counter */
233 /* --------------------------
234 * Initializations of stage2
235 * ------------------------*/
237 /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
238 * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
240 * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
243 /* Working pointer of inputA */
246 /* Working pointer of inputB */
247 pSrc2
= pIn2
+ ( srcBLen
- 1u );
250 /* count is index by which the pointer pIn1 to be incremented */
253 /* -------------------
255 * ------------------*/
257 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
258 * So, to loop unroll over blockSize2,
259 * srcBLen should be greater than or equal to 4 */
262 /* Loop unroll over blkCnt */
264 blkCnt
= blockSize2
/ 3 ;
267 /* Set all accumulators to zero */
272 /* read x[0], x[1] samples */
276 /* Apply loop unrolling and compute 3 MACs simultaneously. */
279 /* First part of the processing with loop unrolling. Compute 3 MACs at a time.
280 ** a second loop below computes MACs for the remaining 1 to 2 samples. */
283 /* Read y[srcBLen - 1] sample */
286 /* Read x[2] sample */
289 /* Perform the multiply-accumulates */
290 /* acc0 += x[0] * y[srcBLen - 1] */
291 acc0
+= ( q63_t
) x0
* c0
;
292 /* acc1 += x[1] * y[srcBLen - 1] */
293 acc1
+= ( q63_t
) x1
* c0
;
294 /* acc2 += x[2] * y[srcBLen - 1] */
295 acc2
+= ( q63_t
) x2
* c0
;
297 /* Read y[srcBLen - 2] sample */
300 /* Read x[3] sample */
303 /* Perform the multiply-accumulate */
304 /* acc0 += x[1] * y[srcBLen - 2] */
305 acc0
+= ( q63_t
) x1
* c0
;
306 /* acc1 += x[2] * y[srcBLen - 2] */
307 acc1
+= ( q63_t
) x2
* c0
;
308 /* acc2 += x[3] * y[srcBLen - 2] */
309 acc2
+= ( q63_t
) x0
* c0
;
311 /* Read y[srcBLen - 3] sample */
314 /* Read x[4] sample */
317 /* Perform the multiply-accumulates */
318 /* acc0 += x[2] * y[srcBLen - 3] */
319 acc0
+= ( q63_t
) x2
* c0
;
320 /* acc1 += x[3] * y[srcBLen - 2] */
321 acc1
+= ( q63_t
) x0
* c0
;
322 /* acc2 += x[4] * y[srcBLen - 2] */
323 acc2
+= ( q63_t
) x1
* c0
;
332 /* If the srcBLen is not a multiple of 3, compute any remaining MACs here.
333 ** No loop unrolling is used. */
334 k
= srcBLen
- ( 3 * ( srcBLen
/ 3 ));
338 /* Read y[srcBLen - 5] sample */
341 /* Read x[7] sample */
344 /* Perform the multiply-accumulates */
345 /* acc0 += x[4] * y[srcBLen - 5] */
346 acc0
+= ( q63_t
) x0
* c0
;
347 /* acc1 += x[5] * y[srcBLen - 5] */
348 acc1
+= ( q63_t
) x1
* c0
;
349 /* acc2 += x[6] * y[srcBLen - 5] */
350 acc2
+= ( q63_t
) x2
* c0
;
352 /* Reuse the present samples for the next MAC */
356 /* Decrement the loop counter */
360 /* Store the result in the accumulator in the destination buffer. */
361 * pOut
++ = ( q31_t
) ( acc0
>> 31 );
362 * pOut
++ = ( q31_t
) ( acc1
>> 31 );
363 * pOut
++ = ( q31_t
) ( acc2
>> 31 );
365 /* Increment the pointer pIn1 index, count by 3 */
368 /* Update the inputA and inputB pointers for next MAC calculation */
372 /* Decrement the loop counter */
376 /* If the blockSize2 is not a multiple of 3, compute any remaining output samples here.
377 ** No loop unrolling is used. */
378 blkCnt
= blockSize2
- 3 * ( blockSize2
/ 3 );
382 /* Accumulator is made zero for every iteration */
385 /* Apply loop unrolling and compute 4 MACs simultaneously. */
388 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
389 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
392 /* Perform the multiply-accumulates */
393 sum
+= ( q63_t
) * px
++ * (* py
--);
394 sum
+= ( q63_t
) * px
++ * (* py
--);
395 sum
+= ( q63_t
) * px
++ * (* py
--);
396 sum
+= ( q63_t
) * px
++ * (* py
--);
398 /* Decrement the loop counter */
402 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
403 ** No loop unrolling is used. */
408 /* Perform the multiply-accumulate */
409 sum
+= ( q63_t
) * px
++ * (* py
--);
411 /* Decrement the loop counter */
415 /* Store the result in the accumulator in the destination buffer. */
416 * pOut
++ = ( q31_t
) ( sum
>> 31 );
418 /* Increment the MAC count */
421 /* Update the inputA and inputB pointers for next MAC calculation */
425 /* Decrement the loop counter */
431 /* If the srcBLen is not a multiple of 4,
432 * the blockSize2 loop cannot be unrolled by 4 */
433 blkCnt
= ( uint32_t ) blockSize2
;
437 /* Accumulator is made zero for every iteration */
440 /* srcBLen number of MACS should be performed */
445 /* Perform the multiply-accumulate */
446 sum
+= ( q63_t
) * px
++ * (* py
--);
448 /* Decrement the loop counter */
452 /* Store the result in the accumulator in the destination buffer. */
453 * pOut
++ = ( q31_t
) ( sum
>> 31 );
455 /* Increment the MAC count */
458 /* Update the inputA and inputB pointers for next MAC calculation */
462 /* Decrement the loop counter */
468 /* --------------------------
469 * Initializations of stage3
470 * -------------------------*/
472 /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
473 * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
475 * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
476 * sum += x[srcALen-1] * y[srcBLen-1]
479 /* In this stage the MAC operations are decreased by 1 for every iteration.
480 The blockSize3 variable holds the number of MAC operations performed */
481 count
= srcBLen
- 1u ;
483 /* Working pointer of inputA */
484 pSrc1
= ( pIn1
+ srcALen
) - ( srcBLen
- 1u );
487 /* Working pointer of inputB */
488 pSrc2
= pIn2
+ ( srcBLen
- 1u );
491 /* -------------------
493 * ------------------*/
495 while ( blockSize3
> 0 )
497 /* Accumulator is made zero for every iteration */
500 /* Apply loop unrolling and compute 4 MACs simultaneously. */
503 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
504 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
507 sum
+= ( q63_t
) * px
++ * (* py
--);
508 sum
+= ( q63_t
) * px
++ * (* py
--);
509 sum
+= ( q63_t
) * px
++ * (* py
--);
510 sum
+= ( q63_t
) * px
++ * (* py
--);
512 /* Decrement the loop counter */
516 /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
517 ** No loop unrolling is used. */
522 /* Perform the multiply-accumulate */
523 sum
+= ( q63_t
) * px
++ * (* py
--);
525 /* Decrement the loop counter */
529 /* Store the result in the accumulator in the destination buffer. */
530 * pOut
++ = ( q31_t
) ( sum
>> 31 );
532 /* Update the inputA and inputB pointers for next MAC calculation */
536 /* Decrement the MAC count */
539 /* Decrement the loop counter */
544 /* set status as ARM_MATH_SUCCESS */
545 status
= ARM_MATH_SUCCESS
;
548 /* Return to application */
553 /* Run the below code for Cortex-M0 */
555 q31_t
* pIn1
= pSrcA
; /* inputA pointer */
556 q31_t
* pIn2
= pSrcB
; /* inputB pointer */
557 q63_t sum
; /* Accumulator */
558 uint32_t i
, j
; /* loop counters */
559 arm_status status
; /* status of Partial convolution */
561 /* Check for range of output samples to be calculated */
562 if (( firstIndex
+ numPoints
) > (( srcALen
+ ( srcBLen
- 1u ))))
564 /* Set status as ARM_ARGUMENT_ERROR */
565 status
= ARM_MATH_ARGUMENT_ERROR
;
569 /* Loop to calculate convolution for output length number of values */
570 for ( i
= firstIndex
; i
<= ( firstIndex
+ numPoints
- 1 ); i
++)
572 /* Initialize sum with zero to carry on MAC operations */
575 /* Loop to perform MAC operations according to convolution equation */
576 for ( j
= 0 ; j
<= i
; j
++)
578 /* Check the array limitations */
579 if ((( i
- j
) < srcBLen
) && ( j
< srcALen
))
581 /* z[i] += x[i-j] * y[j] */
582 sum
+= (( q63_t
) pIn1
[ j
] * ( pIn2
[ i
- j
]));
586 /* Store the output in the destination buffer */
587 pDst
[ i
] = ( q31_t
) ( sum
>> 31u );
589 /* set status as ARM_SUCCESS as there are no argument errors */
590 status
= ARM_MATH_SUCCESS
;
594 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
599 * @} end of PartialConv group