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_q15.c
10 * Description: Correlation of Q15 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
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.
62 * <b>Scaling and Overflow Behavior:</b>
65 * The function is implemented using a 64-bit internal accumulator.
66 * Both inputs are in 1.15 format and multiplications yield a 2.30 result.
67 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
68 * This approach provides 33 guard bits and there is no risk of overflow.
69 * The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
72 * Refer to <code>arm_correlate_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
75 * Refer the function <code>arm_correlate_opt_q15()</code> for a faster implementation of this function using scratch buffers.
79 void arm_correlate_q15(
87 #if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE)
89 /* Run the below code for Cortex-M4 and Cortex-M3 */
91 q15_t *pIn1; /* inputA pointer */
92 q15_t *pIn2; /* inputB pointer */
93 q15_t *pOut = pDst; /* output pointer */
94 q63_t sum, acc0, acc1, acc2, acc3; /* Accumulators */
95 q15_t *px; /* Intermediate inputA pointer */
96 q15_t *py; /* Intermediate inputB pointer */
97 q15_t *pSrc1; /* Intermediate pointers */
98 q31_t x0, x1, x2, x3, c0; /* temporary variables for holding input and coefficient values */
99 uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counter */
100 int32_t inc = 1; /* Destination address modifier */
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 /* But CORR(x, y) is reverse of CORR(y, x) */
107 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
108 /* and the destination pointer modifier, inc is set to -1 */
109 /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */
110 /* But to improve the performance,
111 * we include zeroes in the output instead of zero padding either of the the inputs*/
112 /* If srcALen > srcBLen,
113 * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */
114 /* If srcALen < srcBLen,
115 * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */
116 if(srcALen >= srcBLen)
118 /* Initialization of inputA pointer */
121 /* Initialization of inputB pointer */
124 /* Number of output samples is calculated */
125 outBlockSize = (2u * srcALen) - 1u;
127 /* When srcALen > srcBLen, zero padding is done to srcB
128 * to make their lengths equal.
129 * Instead, (outBlockSize - (srcALen + srcBLen - 1))
130 * number of output samples are made zero */
131 j = outBlockSize - (srcALen + (srcBLen - 1u));
133 /* Updating the pointer position to non zero value */
139 /* Initialization of inputA pointer */
142 /* Initialization of inputB pointer */
145 /* srcBLen is always considered as shorter or equal to srcALen */
150 /* CORR(x, y) = Reverse order(CORR(y, x)) */
151 /* Hence set the destination pointer to point to the last output sample */
152 pOut = pDst + ((srcALen + srcBLen) - 2u);
154 /* Destination address modifier is set to -1 */
159 /* The function is internally
160 * divided into three parts according to the number of multiplications that has to be
161 * taken place between inputA samples and inputB samples. In the first part of the
162 * algorithm, the multiplications increase by one for every iteration.
163 * In the second part of the algorithm, srcBLen number of multiplications are done.
164 * In the third part of the algorithm, the multiplications decrease by one
165 * for every iteration.*/
166 /* The algorithm is implemented in three stages.
167 * The loop counters of each stage is initiated here. */
168 blockSize1 = srcBLen - 1u;
169 blockSize2 = srcALen - (srcBLen - 1u);
170 blockSize3 = blockSize1;
172 /* --------------------------
173 * Initializations of stage1
174 * -------------------------*/
176 /* sum = x[0] * y[srcBlen - 1]
177 * sum = x[0] * y[srcBlen - 2] + x[1] * y[srcBlen - 1]
179 * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1]
182 /* In this stage the MAC operations are increased by 1 for every iteration.
183 The count variable holds the number of MAC operations performed */
186 /* Working pointer of inputA */
189 /* Working pointer of inputB */
190 pSrc1 = pIn2 + (srcBLen - 1u);
193 /* ------------------------
195 * ----------------------*/
197 /* The first loop starts here */
198 while(blockSize1 > 0u)
200 /* Accumulator is made zero for every iteration */
203 /* Apply loop unrolling and compute 4 MACs simultaneously. */
206 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
207 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
210 /* x[0] * y[srcBLen - 4] , x[1] * y[srcBLen - 3] */
211 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
212 /* x[3] * y[srcBLen - 1] , x[2] * y[srcBLen - 2] */
213 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
215 /* Decrement the loop counter */
219 /* If the count is not a multiple of 4, compute any remaining MACs here.
220 ** No loop unrolling is used. */
225 /* Perform the multiply-accumulates */
226 /* x[0] * y[srcBLen - 1] */
227 sum = __SMLALD(*px++, *py++, sum);
229 /* Decrement the loop counter */
233 /* Store the result in the accumulator in the destination buffer. */
234 *pOut = (q15_t) (__SSAT((sum >> 15), 16));
235 /* Destination pointer is updated according to the address modifier, inc */
238 /* Update the inputA and inputB pointers for next MAC calculation */
242 /* Increment the MAC count */
245 /* Decrement the loop counter */
249 /* --------------------------
250 * Initializations of stage2
251 * ------------------------*/
253 /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1]
254 * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1]
256 * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
259 /* Working pointer of inputA */
262 /* Working pointer of inputB */
265 /* count is index by which the pointer pIn1 to be incremented */
268 /* -------------------
270 * ------------------*/
272 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
273 * So, to loop unroll over blockSize2,
274 * srcBLen should be greater than or equal to 4, to loop unroll the srcBLen loop */
277 /* Loop unroll over blockSize2, by 4 */
278 blkCnt = blockSize2 >> 2u;
282 /* Set all accumulators to zero */
288 /* read x[0], x[1] samples */
290 /* read x[1], x[2] samples */
291 x1 = _SIMD32_OFFSET(px + 1);
294 /* Apply loop unrolling and compute 4 MACs simultaneously. */
297 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
298 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
301 /* Read the first two inputB samples using SIMD:
303 c0 = *__SIMD32(py)++;
305 /* acc0 += x[0] * y[0] + x[1] * y[1] */
306 acc0 = __SMLALD(x0, c0, acc0);
308 /* acc1 += x[1] * y[0] + x[2] * y[1] */
309 acc1 = __SMLALD(x1, c0, acc1);
311 /* Read x[2], x[3] */
314 /* Read x[3], x[4] */
315 x3 = _SIMD32_OFFSET(px + 1);
317 /* acc2 += x[2] * y[0] + x[3] * y[1] */
318 acc2 = __SMLALD(x2, c0, acc2);
320 /* acc3 += x[3] * y[0] + x[4] * y[1] */
321 acc3 = __SMLALD(x3, c0, acc3);
323 /* Read y[2] and y[3] */
324 c0 = *__SIMD32(py)++;
326 /* acc0 += x[2] * y[2] + x[3] * y[3] */
327 acc0 = __SMLALD(x2, c0, acc0);
329 /* acc1 += x[3] * y[2] + x[4] * y[3] */
330 acc1 = __SMLALD(x3, c0, acc1);
332 /* Read x[4], x[5] */
333 x0 = _SIMD32_OFFSET(px + 2);
335 /* Read x[5], x[6] */
336 x1 = _SIMD32_OFFSET(px + 3);
340 /* acc2 += x[4] * y[2] + x[5] * y[3] */
341 acc2 = __SMLALD(x0, c0, acc2);
343 /* acc3 += x[5] * y[2] + x[6] * y[3] */
344 acc3 = __SMLALD(x1, c0, acc3);
348 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
349 ** No loop unrolling is used. */
356 #ifdef ARM_MATH_BIG_ENDIAN
362 c0 = c0 & 0x0000FFFF;
364 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
369 /* Perform the multiply-accumulates */
370 acc0 = __SMLALD(x0, c0, acc0);
371 acc1 = __SMLALD(x1, c0, acc1);
372 acc2 = __SMLALDX(x1, c0, acc2);
373 acc3 = __SMLALDX(x3, c0, acc3);
378 /* Read y[4], y[5] */
381 /* Read x[7], x[8] */
385 x2 = _SIMD32_OFFSET(px + 1);
388 /* Perform the multiply-accumulates */
389 acc0 = __SMLALD(x0, c0, acc0);
390 acc1 = __SMLALD(x1, c0, acc1);
391 acc2 = __SMLALD(x3, c0, acc2);
392 acc3 = __SMLALD(x2, c0, acc3);
397 /* Read y[4], y[5] */
398 c0 = *__SIMD32(py)++;
400 /* Read x[7], x[8] */
404 x2 = _SIMD32_OFFSET(px + 1);
406 /* Perform the multiply-accumulates */
407 acc0 = __SMLALD(x0, c0, acc0);
408 acc1 = __SMLALD(x1, c0, acc1);
409 acc2 = __SMLALD(x3, c0, acc2);
410 acc3 = __SMLALD(x2, c0, acc3);
415 #ifdef ARM_MATH_BIG_ENDIAN
420 c0 = c0 & 0x0000FFFF;
421 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
423 x3 = _SIMD32_OFFSET(px + 2);
426 /* Perform the multiply-accumulates */
427 acc0 = __SMLALDX(x1, c0, acc0);
428 acc1 = __SMLALD(x2, c0, acc1);
429 acc2 = __SMLALDX(x2, c0, acc2);
430 acc3 = __SMLALDX(x3, c0, acc3);
433 /* Store the result in the accumulator in the destination buffer. */
434 *pOut = (q15_t) (__SSAT(acc0 >> 15, 16));
435 /* Destination pointer is updated according to the address modifier, inc */
438 *pOut = (q15_t) (__SSAT(acc1 >> 15, 16));
441 *pOut = (q15_t) (__SSAT(acc2 >> 15, 16));
444 *pOut = (q15_t) (__SSAT(acc3 >> 15, 16));
447 /* Increment the count by 4 as 4 output values are computed */
450 /* Update the inputA and inputB pointers for next MAC calculation */
454 /* Decrement the loop counter */
458 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
459 ** No loop unrolling is used. */
460 blkCnt = blockSize2 % 0x4u;
464 /* Accumulator is made zero for every iteration */
467 /* Apply loop unrolling and compute 4 MACs simultaneously. */
470 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
471 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
474 /* Perform the multiply-accumulates */
475 sum += ((q63_t) * px++ * *py++);
476 sum += ((q63_t) * px++ * *py++);
477 sum += ((q63_t) * px++ * *py++);
478 sum += ((q63_t) * px++ * *py++);
480 /* Decrement the loop counter */
484 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
485 ** No loop unrolling is used. */
490 /* Perform the multiply-accumulates */
491 sum += ((q63_t) * px++ * *py++);
493 /* Decrement the loop counter */
497 /* Store the result in the accumulator in the destination buffer. */
498 *pOut = (q15_t) (__SSAT(sum >> 15, 16));
499 /* Destination pointer is updated according to the address modifier, inc */
502 /* Increment count by 1, as one output value is computed */
505 /* Update the inputA and inputB pointers for next MAC calculation */
509 /* Decrement the loop counter */
515 /* If the srcBLen is not a multiple of 4,
516 * the blockSize2 loop cannot be unrolled by 4 */
521 /* Accumulator is made zero for every iteration */
524 /* Loop over srcBLen */
529 /* Perform the multiply-accumulate */
530 sum += ((q63_t) * px++ * *py++);
532 /* Decrement the loop counter */
536 /* Store the result in the accumulator in the destination buffer. */
537 *pOut = (q15_t) (__SSAT(sum >> 15, 16));
538 /* Destination pointer is updated according to the address modifier, inc */
541 /* Increment the MAC count */
544 /* Update the inputA and inputB pointers for next MAC calculation */
548 /* Decrement the loop counter */
553 /* --------------------------
554 * Initializations of stage3
555 * -------------------------*/
557 /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
558 * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
560 * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1]
561 * sum += x[srcALen-1] * y[0]
564 /* In this stage the MAC operations are decreased by 1 for every iteration.
565 The count variable holds the number of MAC operations performed */
566 count = srcBLen - 1u;
568 /* Working pointer of inputA */
569 pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
572 /* Working pointer of inputB */
575 /* -------------------
577 * ------------------*/
579 while(blockSize3 > 0u)
581 /* Accumulator is made zero for every iteration */
584 /* Apply loop unrolling and compute 4 MACs simultaneously. */
587 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
588 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
591 /* Perform the multiply-accumulates */
592 /* sum += x[srcALen - srcBLen + 4] * y[3] , sum += x[srcALen - srcBLen + 3] * y[2] */
593 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
594 /* sum += x[srcALen - srcBLen + 2] * y[1] , sum += x[srcALen - srcBLen + 1] * y[0] */
595 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
597 /* Decrement the loop counter */
601 /* If the count is not a multiple of 4, compute any remaining MACs here.
602 ** No loop unrolling is used. */
607 /* Perform the multiply-accumulates */
608 sum = __SMLALD(*px++, *py++, sum);
610 /* Decrement the loop counter */
614 /* Store the result in the accumulator in the destination buffer. */
615 *pOut = (q15_t) (__SSAT((sum >> 15), 16));
616 /* Destination pointer is updated according to the address modifier, inc */
619 /* Update the inputA and inputB pointers for next MAC calculation */
623 /* Decrement the MAC count */
626 /* Decrement the loop counter */
632 /* Run the below code for Cortex-M0 */
634 q15_t *pIn1 = pSrcA; /* inputA pointer */
635 q15_t *pIn2 = pSrcB + (srcBLen - 1u); /* inputB pointer */
636 q63_t sum; /* Accumulators */
637 uint32_t i = 0u, j; /* loop counters */
638 uint32_t inv = 0u; /* Reverse order flag */
639 uint32_t tot = 0u; /* Length */
641 /* The algorithm implementation is based on the lengths of the inputs. */
642 /* srcB is always made to slide across srcA. */
643 /* So srcBLen is always considered as shorter or equal to srcALen */
644 /* But CORR(x, y) is reverse of CORR(y, x) */
645 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
646 /* and a varaible, inv is set to 1 */
647 /* If lengths are not equal then zero pad has to be done to make the two
648 * inputs of same length. But to improve the performance, we include zeroes
649 * in the output instead of zero padding either of the the inputs*/
650 /* If srcALen > srcBLen, (srcALen - srcBLen) zeroes has to included in the
651 * starting of the output buffer */
652 /* If srcALen < srcBLen, (srcALen - srcBLen) zeroes has to included in the
653 * ending of the output buffer */
654 /* Once the zero padding is done the remaining of the output is calcualted
655 * using convolution but with the shorter signal time shifted. */
657 /* Calculate the length of the remaining sequence */
658 tot = ((srcALen + srcBLen) - 2u);
660 if(srcALen > srcBLen)
662 /* Calculating the number of zeros to be padded to the output */
663 j = srcALen - srcBLen;
665 /* Initialise the pointer after zero padding */
669 else if(srcALen < srcBLen)
671 /* Initialization to inputB pointer */
674 /* Initialization to the end of inputA pointer */
675 pIn2 = pSrcA + (srcALen - 1u);
677 /* Initialisation of the pointer after zero padding */
680 /* Swapping the lengths */
685 /* Setting the reverse flag */
690 /* Loop to calculate convolution for output length number of times */
691 for (i = 0u; i <= tot; i++)
693 /* Initialize sum with zero to carry on MAC operations */
696 /* Loop to perform MAC operations according to convolution equation */
697 for (j = 0u; j <= i; j++)
699 /* Check the array limitations */
700 if((((i - j) < srcBLen) && (j < srcALen)))
702 /* z[i] += x[i-j] * y[j] */
703 sum += ((q31_t) pIn1[j] * pIn2[-((int32_t) i - j)]);
706 /* Store the output in the destination buffer */
708 *pDst-- = (q15_t) __SSAT((sum >> 15u), 16u);
710 *pDst++ = (q15_t) __SSAT((sum >> 15u), 16u);
713 #endif /*#if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE) */
718 * @} end of Corr group