1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
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 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
32 * -------------------------------------------------------------------- */
37 * @ingroup groupFilters
46 * @brief Correlation of Q15 sequences.
47 * @param[in] *pSrcA points to the first input sequence.
48 * @param[in] srcALen length of the first input sequence.
49 * @param[in] *pSrcB points to the second input sequence.
50 * @param[in] srcBLen length of the second input sequence.
51 * @param[out] *pDst points to the location where the output result is written. Length 2 * max(srcALen, srcBLen) - 1.
55 * <b>Scaling and Overflow Behavior:</b>
58 * The function is implemented using a 64-bit internal accumulator.
59 * Both inputs are in 1.15 format and multiplications yield a 2.30 result.
60 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
61 * This approach provides 33 guard bits and there is no risk of overflow.
62 * The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
65 * Refer to <code>arm_correlate_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
68 void arm_correlate_q15(
78 /* Run the below code for Cortex-M4 and Cortex-M3 */
80 q15_t *pIn1; /* inputA pointer */
81 q15_t *pIn2; /* inputB pointer */
82 q15_t *pOut = pDst; /* output pointer */
83 q63_t sum, acc0, acc1, acc2, acc3; /* Accumulators */
84 q15_t *px; /* Intermediate inputA pointer */
85 q15_t *py; /* Intermediate inputB pointer */
86 q15_t *pSrc1; /* Intermediate pointers */
87 q31_t x0, x1, x2, x3, c0; /* temporary variables for holding input and coefficient values */
88 uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counter */
89 int32_t inc = 1; /* Destination address modifier */
90 q31_t *pb; /* 32 bit pointer for inputB buffer */
93 /* The algorithm implementation is based on the lengths of the inputs. */
94 /* srcB is always made to slide across srcA. */
95 /* So srcBLen is always considered as shorter or equal to srcALen */
96 /* But CORR(x, y) is reverse of CORR(y, x) */
97 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
98 /* and the destination pointer modifier, inc is set to -1 */
99 /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */
100 /* But to improve the performance,
101 * we include zeroes in the output instead of zero padding either of the the inputs*/
102 /* If srcALen > srcBLen,
103 * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */
104 /* If srcALen < srcBLen,
105 * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */
106 if(srcALen >= srcBLen)
108 /* Initialization of inputA pointer */
111 /* Initialization of inputB pointer */
114 /* Number of output samples is calculated */
115 outBlockSize = (2u * srcALen) - 1u;
117 /* When srcALen > srcBLen, zero padding is done to srcB
118 * to make their lengths equal.
119 * Instead, (outBlockSize - (srcALen + srcBLen - 1))
120 * number of output samples are made zero */
121 j = outBlockSize - (srcALen + (srcBLen - 1u));
123 /* Updating the pointer position to non zero value */
129 /* Initialization of inputA pointer */
132 /* Initialization of inputB pointer */
135 /* srcBLen is always considered as shorter or equal to srcALen */
140 /* CORR(x, y) = Reverse order(CORR(y, x)) */
141 /* Hence set the destination pointer to point to the last output sample */
142 pOut = pDst + ((srcALen + srcBLen) - 2u);
144 /* Destination address modifier is set to -1 */
149 /* The function is internally
150 * divided into three parts according to the number of multiplications that has to be
151 * taken place between inputA samples and inputB samples. In the first part of the
152 * algorithm, the multiplications increase by one for every iteration.
153 * In the second part of the algorithm, srcBLen number of multiplications are done.
154 * In the third part of the algorithm, the multiplications decrease by one
155 * for every iteration.*/
156 /* The algorithm is implemented in three stages.
157 * The loop counters of each stage is initiated here. */
158 blockSize1 = srcBLen - 1u;
159 blockSize2 = srcALen - (srcBLen - 1u);
160 blockSize3 = blockSize1;
162 /* --------------------------
163 * Initializations of stage1
164 * -------------------------*/
166 /* sum = x[0] * y[srcBlen - 1]
167 * sum = x[0] * y[srcBlen - 2] + x[1] * y[srcBlen - 1]
169 * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1]
172 /* In this stage the MAC operations are increased by 1 for every iteration.
173 The count variable holds the number of MAC operations performed */
176 /* Working pointer of inputA */
179 /* Working pointer of inputB */
180 pSrc1 = pIn2 + (srcBLen - 1u);
183 /* ------------------------
185 * ----------------------*/
187 /* The first loop starts here */
188 while(blockSize1 > 0u)
190 /* Accumulator is made zero for every iteration */
193 /* Apply loop unrolling and compute 4 MACs simultaneously. */
196 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
197 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
200 /* x[0] * y[srcBLen - 4] , x[1] * y[srcBLen - 3] */
201 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
202 /* x[3] * y[srcBLen - 1] , x[2] * y[srcBLen - 2] */
203 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
205 /* Decrement the loop counter */
209 /* If the count is not a multiple of 4, compute any remaining MACs here.
210 ** No loop unrolling is used. */
215 /* Perform the multiply-accumulates */
216 /* x[0] * y[srcBLen - 1] */
217 sum = __SMLALD(*px++, *py++, sum);
219 /* Decrement the loop counter */
223 /* Store the result in the accumulator in the destination buffer. */
224 *pOut = (q15_t) (__SSAT((sum >> 15), 16));
225 /* Destination pointer is updated according to the address modifier, inc */
228 /* Update the inputA and inputB pointers for next MAC calculation */
232 /* Increment the MAC count */
235 /* Decrement the loop counter */
239 /* --------------------------
240 * Initializations of stage2
241 * ------------------------*/
243 /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1]
244 * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1]
246 * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
249 /* Working pointer of inputA */
252 /* Working pointer of inputB */
255 /* Initialize inputB pointer of type q31 */
258 /* count is index by which the pointer pIn1 to be incremented */
261 /* -------------------
263 * ------------------*/
265 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
266 * So, to loop unroll over blockSize2,
267 * srcBLen should be greater than or equal to 4, to loop unroll the srcBLen loop */
270 /* Loop unroll over blockSize2, by 4 */
271 blkCnt = blockSize2 >> 2u;
275 /* Set all accumulators to zero */
281 /* read x[0], x[1] samples */
282 x0 = *(q31_t *) (px++);
283 /* read x[1], x[2] samples */
284 x1 = *(q31_t *) (px++);
286 /* Apply loop unrolling and compute 4 MACs simultaneously. */
289 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
290 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
293 /* Read the first two inputB samples using SIMD:
297 /* acc0 += x[0] * y[0] + x[1] * y[1] */
298 acc0 = __SMLALD(x0, c0, acc0);
300 /* acc1 += x[1] * y[0] + x[2] * y[1] */
301 acc1 = __SMLALD(x1, c0, acc1);
303 /* Read x[2], x[3] */
304 x2 = *(q31_t *) (px++);
306 /* Read x[3], x[4] */
307 x3 = *(q31_t *) (px++);
309 /* acc2 += x[2] * y[0] + x[3] * y[1] */
310 acc2 = __SMLALD(x2, c0, acc2);
312 /* acc3 += x[3] * y[0] + x[4] * y[1] */
313 acc3 = __SMLALD(x3, c0, acc3);
315 /* Read y[2] and y[3] */
318 /* acc0 += x[2] * y[2] + x[3] * y[3] */
319 acc0 = __SMLALD(x2, c0, acc0);
321 /* acc1 += x[3] * y[2] + x[4] * y[3] */
322 acc1 = __SMLALD(x3, c0, acc1);
324 /* Read x[4], x[5] */
325 x0 = *(q31_t *) (px++);
327 /* Read x[5], x[6] */
328 x1 = *(q31_t *) (px++);
330 /* acc2 += x[4] * y[2] + x[5] * y[3] */
331 acc2 = __SMLALD(x0, c0, acc2);
333 /* acc3 += x[5] * y[2] + x[6] * y[3] */
334 acc3 = __SMLALD(x1, c0, acc3);
338 /* For the next MAC operations, SIMD is not used
339 * So, the 16 bit pointer if inputB, py is updated */
342 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
343 ** No loop unrolling is used. */
350 #ifdef ARM_MATH_BIG_ENDIAN
356 c0 = c0 & 0x0000FFFF;
358 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
360 x3 = *(q31_t *) px++;
362 /* Perform the multiply-accumulates */
363 acc0 = __SMLALD(x0, c0, acc0);
364 acc1 = __SMLALD(x1, c0, acc1);
365 acc2 = __SMLALDX(x1, c0, acc2);
366 acc3 = __SMLALDX(x3, c0, acc3);
371 /* Read y[4], y[5] */
374 /* Read x[7], x[8] */
375 x3 = *(q31_t *) px++;
378 x2 = *(q31_t *) px++;
380 /* Perform the multiply-accumulates */
381 acc0 = __SMLALD(x0, c0, acc0);
382 acc1 = __SMLALD(x1, c0, acc1);
383 acc2 = __SMLALD(x3, c0, acc2);
384 acc3 = __SMLALD(x2, c0, acc3);
389 /* Read y[4], y[5] */
392 /* Read x[7], x[8] */
393 x3 = *(q31_t *) px++;
396 x2 = *(q31_t *) px++;
398 /* Perform the multiply-accumulates */
399 acc0 = __SMLALD(x0, c0, acc0);
400 acc1 = __SMLALD(x1, c0, acc1);
401 acc2 = __SMLALD(x3, c0, acc2);
402 acc3 = __SMLALD(x2, c0, acc3);
405 #ifdef ARM_MATH_BIG_ENDIAN
408 c0 = c0 & 0xFFFF0000;
413 c0 = c0 & 0x0000FFFF;
415 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
417 x3 = *(q31_t *) px++;
419 /* Perform the multiply-accumulates */
420 acc0 = __SMLALDX(x1, c0, acc0);
421 acc1 = __SMLALD(x2, c0, acc1);
422 acc2 = __SMLALDX(x2, c0, acc2);
423 acc3 = __SMLALDX(x3, c0, acc3);
426 /* Store the result in the accumulator in the destination buffer. */
427 *pOut = (q15_t) (__SSAT(acc0 >> 15, 16));
428 /* Destination pointer is updated according to the address modifier, inc */
431 *pOut = (q15_t) (__SSAT(acc1 >> 15, 16));
434 *pOut = (q15_t) (__SSAT(acc2 >> 15, 16));
437 *pOut = (q15_t) (__SSAT(acc3 >> 15, 16));
440 /* Increment the count by 4 as 4 output values are computed */
443 /* Update the inputA and inputB pointers for next MAC calculation */
449 /* Decrement the loop counter */
453 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
454 ** No loop unrolling is used. */
455 blkCnt = blockSize2 % 0x4u;
459 /* Accumulator is made zero for every iteration */
462 /* Apply loop unrolling and compute 4 MACs simultaneously. */
465 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
466 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
469 /* Perform the multiply-accumulates */
470 sum += ((q63_t) * px++ * *py++);
471 sum += ((q63_t) * px++ * *py++);
472 sum += ((q63_t) * px++ * *py++);
473 sum += ((q63_t) * px++ * *py++);
475 /* Decrement the loop counter */
479 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
480 ** No loop unrolling is used. */
485 /* Perform the multiply-accumulates */
486 sum += ((q63_t) * px++ * *py++);
488 /* Decrement the loop counter */
492 /* Store the result in the accumulator in the destination buffer. */
493 *pOut = (q15_t) (__SSAT(sum >> 15, 16));
494 /* Destination pointer is updated according to the address modifier, inc */
497 /* Increment count by 1, as one output value is computed */
500 /* Update the inputA and inputB pointers for next MAC calculation */
504 /* Decrement the loop counter */
510 /* If the srcBLen is not a multiple of 4,
511 * the blockSize2 loop cannot be unrolled by 4 */
516 /* Accumulator is made zero for every iteration */
519 /* Loop over srcBLen */
524 /* Perform the multiply-accumulate */
525 sum += ((q63_t) * px++ * *py++);
527 /* Decrement the loop counter */
531 /* Store the result in the accumulator in the destination buffer. */
532 *pOut = (q15_t) (__SSAT(sum >> 15, 16));
533 /* Destination pointer is updated according to the address modifier, inc */
536 /* Increment the MAC count */
539 /* Update the inputA and inputB pointers for next MAC calculation */
543 /* Decrement the loop counter */
548 /* --------------------------
549 * Initializations of stage3
550 * -------------------------*/
552 /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
553 * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
555 * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1]
556 * sum += x[srcALen-1] * y[0]
559 /* In this stage the MAC operations are decreased by 1 for every iteration.
560 The count variable holds the number of MAC operations performed */
561 count = srcBLen - 1u;
563 /* Working pointer of inputA */
564 pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
567 /* Working pointer of inputB */
570 /* -------------------
572 * ------------------*/
574 while(blockSize3 > 0u)
576 /* Accumulator is made zero for every iteration */
579 /* Apply loop unrolling and compute 4 MACs simultaneously. */
582 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
583 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
586 /* Perform the multiply-accumulates */
587 /* sum += x[srcALen - srcBLen + 4] * y[3] , sum += x[srcALen - srcBLen + 3] * y[2] */
588 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
589 /* sum += x[srcALen - srcBLen + 2] * y[1] , sum += x[srcALen - srcBLen + 1] * y[0] */
590 sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
592 /* Decrement the loop counter */
596 /* If the count is not a multiple of 4, compute any remaining MACs here.
597 ** No loop unrolling is used. */
602 /* Perform the multiply-accumulates */
603 sum = __SMLALD(*px++, *py++, sum);
605 /* Decrement the loop counter */
609 /* Store the result in the accumulator in the destination buffer. */
610 *pOut = (q15_t) (__SSAT((sum >> 15), 16));
611 /* Destination pointer is updated according to the address modifier, inc */
614 /* Update the inputA and inputB pointers for next MAC calculation */
618 /* Decrement the MAC count */
621 /* Decrement the loop counter */
627 /* Run the below code for Cortex-M0 */
629 q15_t *pIn1 = pSrcA; /* inputA pointer */
630 q15_t *pIn2 = pSrcB + (srcBLen - 1u); /* inputB pointer */
631 q63_t sum; /* Accumulators */
632 uint32_t i = 0u, j; /* loop counters */
633 uint32_t inv = 0u; /* Reverse order flag */
634 uint32_t tot = 0u; /* Length */
636 /* The algorithm implementation is based on the lengths of the inputs. */
637 /* srcB is always made to slide across srcA. */
638 /* So srcBLen is always considered as shorter or equal to srcALen */
639 /* But CORR(x, y) is reverse of CORR(y, x) */
640 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
641 /* and a varaible, inv is set to 1 */
642 /* If lengths are not equal then zero pad has to be done to make the two
643 * inputs of same length. But to improve the performance, we include zeroes
644 * in the output instead of zero padding either of the the inputs*/
645 /* If srcALen > srcBLen, (srcALen - srcBLen) zeroes has to included in the
646 * starting of the output buffer */
647 /* If srcALen < srcBLen, (srcALen - srcBLen) zeroes has to included in the
648 * ending of the output buffer */
649 /* Once the zero padding is done the remaining of the output is calcualted
650 * using convolution but with the shorter signal time shifted. */
652 /* Calculate the length of the remaining sequence */
653 tot = ((srcALen + srcBLen) - 2u);
655 if(srcALen > srcBLen)
657 /* Calculating the number of zeros to be padded to the output */
658 j = srcALen - srcBLen;
660 /* Initialise the pointer after zero padding */
664 else if(srcALen < srcBLen)
666 /* Initialization to inputB pointer */
669 /* Initialization to the end of inputA pointer */
670 pIn2 = pSrcA + (srcALen - 1u);
672 /* Initialisation of the pointer after zero padding */
675 /* Swapping the lengths */
680 /* Setting the reverse flag */
685 /* Loop to calculate convolution for output length number of times */
686 for (i = 0u; i <= tot; i++)
688 /* Initialize sum with zero to carry on MAC operations */
691 /* Loop to perform MAC operations according to convolution equation */
692 for (j = 0u; j <= i; j++)
694 /* Check the array limitations */
695 if((((i - j) < srcBLen) && (j < srcALen)))
697 /* z[i] += x[i-j] * y[j] */
698 sum += ((q31_t) pIn1[j] * pIn2[-((int32_t) i - j)]);
701 /* Store the output in the destination buffer */
703 *pDst-- = (q15_t) __SSAT((sum >> 15u), 16u);
705 *pDst++ = (q15_t) __SSAT((sum >> 15u), 16u);
708 #endif /* #ifndef ARM_MATH_CM0 */
713 * @} end of Corr group