]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_correlate_opt_q15.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_correlate_opt_q15.c
10 * Description: Correlation of Q15 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
53 * @brief Correlation of Q15 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. Length 2 * max(srcALen, srcBLen) - 1.
59 * @param[in] *pScratch points to scratch buffer of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
63 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
64 * In this case input, output, scratch buffers should be aligned by 32-bit
67 * <b>Scaling and Overflow Behavior:</b>
70 * The function is implemented using a 64-bit internal accumulator.
71 * Both inputs are in 1.15 format and multiplications yield a 2.30 result.
72 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
73 * This approach provides 33 guard bits and there is no risk of overflow.
74 * The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
77 * Refer to <code>arm_correlate_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
83 void arm_correlate_opt_q15(
91 q15_t
*pIn1
; /* inputA pointer */
92 q15_t
*pIn2
; /* inputB pointer */
93 q63_t acc0
, acc1
, acc2
, acc3
; /* Accumulators */
94 q15_t
*py
; /* Intermediate inputB pointer */
95 q31_t x1
, x2
, x3
; /* temporary variables for holding input1 and input2 values */
96 uint32_t j
, blkCnt
, outBlockSize
; /* loop counter */
97 int32_t inc
= 1; /* output pointer increment */
100 q15_t
*pScr
; /* Intermediate pointers */
101 q15_t
*pOut
= pDst
; /* output pointer */
102 #ifdef UNALIGNED_SUPPORT_DISABLE
106 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
108 /* The algorithm implementation is based on the lengths of the inputs. */
109 /* srcB is always made to slide across srcA. */
110 /* So srcBLen is always considered as shorter or equal to srcALen */
111 /* But CORR(x, y) is reverse of CORR(y, x) */
112 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
113 /* and the destination pointer modifier, inc is set to -1 */
114 /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */
115 /* But to improve the performance,
116 * we include zeroes in the output instead of zero padding either of the the inputs*/
117 /* If srcALen > srcBLen,
118 * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */
119 /* If srcALen < srcBLen,
120 * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */
121 if(srcALen
>= srcBLen
)
123 /* Initialization of inputA pointer */
126 /* Initialization of inputB pointer */
129 /* Number of output samples is calculated */
130 outBlockSize
= (2u * srcALen
) - 1u;
132 /* When srcALen > srcBLen, zero padding is done to srcB
133 * to make their lengths equal.
134 * Instead, (outBlockSize - (srcALen + srcBLen - 1))
135 * number of output samples are made zero */
136 j
= outBlockSize
- (srcALen
+ (srcBLen
- 1u));
138 /* Updating the pointer position to non zero value */
144 /* Initialization of inputA pointer */
147 /* Initialization of inputB pointer */
150 /* srcBLen is always considered as shorter or equal to srcALen */
155 /* CORR(x, y) = Reverse order(CORR(y, x)) */
156 /* Hence set the destination pointer to point to the last output sample */
157 pOut
= pDst
+ ((srcALen
+ srcBLen
) - 2u);
159 /* Destination address modifier is set to -1 */
166 /* Fill (srcBLen - 1u) zeros in scratch buffer */
167 arm_fill_q15(0, pScr
, (srcBLen
- 1u));
169 /* Update temporary scratch pointer */
170 pScr
+= (srcBLen
- 1u);
172 #ifndef UNALIGNED_SUPPORT_DISABLE
174 /* Copy (srcALen) samples in scratch buffer */
175 arm_copy_q15(pIn1
, pScr
, srcALen
);
177 /* Update pointers */
183 /* Apply loop unrolling and do 4 Copies simultaneously. */
186 /* First part of the processing with loop unrolling copies 4 data points at a time.
187 ** a second loop below copies for the remaining 1 to 3 samples. */
190 /* copy second buffer in reversal manner */
196 /* Decrement the loop counter */
200 /* If the count is not a multiple of 4, copy remaining samples here.
201 ** No loop unrolling is used. */
206 /* copy second buffer in reversal manner for remaining samples */
209 /* Decrement the loop counter */
213 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
215 #ifndef UNALIGNED_SUPPORT_DISABLE
217 /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
218 arm_fill_q15(0, pScr
, (srcBLen
- 1u));
221 pScr
+= (srcBLen
- 1u);
225 /* Apply loop unrolling and do 4 Copies simultaneously. */
226 j
= (srcBLen
- 1u) >> 2u;
228 /* First part of the processing with loop unrolling copies 4 data points at a time.
229 ** a second loop below copies for the remaining 1 to 3 samples. */
232 /* copy second buffer in reversal manner */
238 /* Decrement the loop counter */
242 /* If the count is not a multiple of 4, copy remaining samples here.
243 ** No loop unrolling is used. */
244 j
= (srcBLen
- 1u) % 0x4u
;
248 /* copy second buffer in reversal manner for remaining samples */
251 /* Decrement the loop counter */
255 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
257 /* Temporary pointer for scratch2 */
261 /* Actual correlation process starts here */
262 blkCnt
= (srcALen
+ srcBLen
- 1u) >> 2;
266 /* Initialze temporary scratch pointer as scratch1 */
269 /* Clear Accumlators */
275 /* Read four samples from scratch1 buffer */
276 x1
= *__SIMD32(pScr
)++;
278 /* Read next four samples from scratch1 buffer */
279 x2
= *__SIMD32(pScr
)++;
281 tapCnt
= (srcBLen
) >> 2u;
286 #ifndef UNALIGNED_SUPPORT_DISABLE
288 /* Read four samples from smaller buffer */
289 y1
= _SIMD32_OFFSET(pIn2
);
290 y2
= _SIMD32_OFFSET(pIn2
+ 2u);
292 acc0
= __SMLALD(x1
, y1
, acc0
);
294 acc2
= __SMLALD(x2
, y1
, acc2
);
296 #ifndef ARM_MATH_BIG_ENDIAN
297 x3
= __PKHBT(x2
, x1
, 0);
299 x3
= __PKHBT(x1
, x2
, 0);
302 acc1
= __SMLALDX(x3
, y1
, acc1
);
304 x1
= _SIMD32_OFFSET(pScr
);
306 acc0
= __SMLALD(x2
, y2
, acc0
);
308 acc2
= __SMLALD(x1
, y2
, acc2
);
310 #ifndef ARM_MATH_BIG_ENDIAN
311 x3
= __PKHBT(x1
, x2
, 0);
313 x3
= __PKHBT(x2
, x1
, 0);
316 acc3
= __SMLALDX(x3
, y1
, acc3
);
318 acc1
= __SMLALDX(x3
, y2
, acc1
);
320 x2
= _SIMD32_OFFSET(pScr
+ 2u);
322 #ifndef ARM_MATH_BIG_ENDIAN
323 x3
= __PKHBT(x2
, x1
, 0);
325 x3
= __PKHBT(x1
, x2
, 0);
328 acc3
= __SMLALDX(x3
, y2
, acc3
);
332 /* Read four samples from smaller buffer */
336 #ifndef ARM_MATH_BIG_ENDIAN
337 y1
= __PKHBT(a
, b
, 16);
339 y1
= __PKHBT(b
, a
, 16);
344 #ifndef ARM_MATH_BIG_ENDIAN
345 y2
= __PKHBT(a
, b
, 16);
347 y2
= __PKHBT(b
, a
, 16);
350 acc0
= __SMLALD(x1
, y1
, acc0
);
352 acc2
= __SMLALD(x2
, y1
, acc2
);
354 #ifndef ARM_MATH_BIG_ENDIAN
355 x3
= __PKHBT(x2
, x1
, 0);
357 x3
= __PKHBT(x1
, x2
, 0);
360 acc1
= __SMLALDX(x3
, y1
, acc1
);
365 #ifndef ARM_MATH_BIG_ENDIAN
366 x1
= __PKHBT(a
, b
, 16);
368 x1
= __PKHBT(b
, a
, 16);
371 acc0
= __SMLALD(x2
, y2
, acc0
);
373 acc2
= __SMLALD(x1
, y2
, acc2
);
375 #ifndef ARM_MATH_BIG_ENDIAN
376 x3
= __PKHBT(x1
, x2
, 0);
378 x3
= __PKHBT(x2
, x1
, 0);
381 acc3
= __SMLALDX(x3
, y1
, acc3
);
383 acc1
= __SMLALDX(x3
, y2
, acc1
);
388 #ifndef ARM_MATH_BIG_ENDIAN
389 x2
= __PKHBT(a
, b
, 16);
391 x2
= __PKHBT(b
, a
, 16);
394 #ifndef ARM_MATH_BIG_ENDIAN
395 x3
= __PKHBT(x2
, x1
, 0);
397 x3
= __PKHBT(x1
, x2
, 0);
400 acc3
= __SMLALDX(x3
, y2
, acc3
);
402 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
409 /* Decrement the loop counter */
415 /* Update scratch pointer for remaining samples of smaller length sequence */
419 /* apply same above for remaining samples of smaller length sequence */
420 tapCnt
= (srcBLen
) & 3u;
425 /* accumlate the results */
426 acc0
+= (*pScr
++ * *pIn2
);
427 acc1
+= (*pScr
++ * *pIn2
);
428 acc2
+= (*pScr
++ * *pIn2
);
429 acc3
+= (*pScr
++ * *pIn2
++);
433 /* Decrement the loop counter */
440 /* Store the results in the accumulators in the destination buffer. */
441 *pOut
= (__SSAT(acc0
>> 15u, 16));
443 *pOut
= (__SSAT(acc1
>> 15u, 16));
445 *pOut
= (__SSAT(acc2
>> 15u, 16));
447 *pOut
= (__SSAT(acc3
>> 15u, 16));
450 /* Initialization of inputB pointer */
458 blkCnt
= (srcALen
+ srcBLen
- 1u) & 0x3;
460 /* Calculate correlation for remaining samples of Bigger length sequence */
463 /* Initialze temporary scratch pointer as scratch1 */
466 /* Clear Accumlators */
469 tapCnt
= (srcBLen
) >> 1u;
474 acc0
+= (*pScr
++ * *pIn2
++);
475 acc0
+= (*pScr
++ * *pIn2
++);
477 /* Decrement the loop counter */
481 tapCnt
= (srcBLen
) & 1u;
483 /* apply same above for remaining samples of smaller length sequence */
487 /* accumlate the results */
488 acc0
+= (*pScr
++ * *pIn2
++);
490 /* Decrement the loop counter */
496 /* Store the result in the accumulator in the destination buffer. */
497 *pOut
= (q15_t
) (__SSAT((acc0
>> 15), 16));
501 /* Initialization of inputB pointer */
512 * @} end of Corr group