1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_correlate_fast_q15.c
10 * Description: Fast Q15 Correlation.
12 * Target Processor: Cortex-M4/Cortex-M3
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.
28 * -------------------------------------------------------------------- */
33 * @ingroup groupFilters
42 * @brief Correlation of Q15 sequences (fast version) for Cortex-M3 and Cortex-M4.
43 * @param[in] *pSrcA points to the first input sequence.
44 * @param[in] srcALen length of the first input sequence.
45 * @param[in] *pSrcB points to the second input sequence.
46 * @param[in] srcBLen length of the second input sequence.
47 * @param[out] *pDst points to the location where the output result is written. Length 2 * max(srcALen, srcBLen) - 1.
50 * <b>Scaling and Overflow Behavior:</b>
53 * This fast version uses a 32-bit accumulator with 2.30 format.
54 * The accumulator maintains full precision of the intermediate multiplication results but provides only a single guard bit.
55 * There is no saturation on intermediate additions.
56 * Thus, if the accumulator overflows it wraps around and distorts the result.
57 * The input signals should be scaled down to avoid intermediate overflows.
58 * Scale down one of the inputs by 1/min(srcALen, srcBLen) to avoid overflow since a
59 * maximum of min(srcALen, srcBLen) number of additions is carried internally.
60 * The 2.30 accumulator is right shifted by 15 bits and then saturated to 1.15 format to yield the final result.
63 * See <code>arm_correlate_q15()</code> for a slower implementation of this function which uses a 64-bit accumulator to avoid wrap around distortion.
66 void arm_correlate_fast_q15(
73 q15_t *pIn1; /* inputA pointer */
74 q15_t *pIn2; /* inputB pointer */
75 q15_t *pOut = pDst; /* output pointer */
76 q31_t sum, acc0, acc1, acc2, acc3; /* Accumulators */
77 q15_t *px; /* Intermediate inputA pointer */
78 q15_t *py; /* Intermediate inputB pointer */
79 q15_t *pSrc1; /* Intermediate pointers */
80 q31_t x0, x1, x2, x3, c0; /* temporary variables for holding input and coefficient values */
81 uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counter */
82 int32_t inc = 1; /* Destination address modifier */
83 q31_t *pb; /* 32 bit pointer for inputB buffer */
86 /* The algorithm implementation is based on the lengths of the inputs. */
87 /* srcB is always made to slide across srcA. */
88 /* So srcBLen is always considered as shorter or equal to srcALen */
89 /* But CORR(x, y) is reverse of CORR(y, x) */
90 /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
91 /* and the destination pointer modifier, inc is set to -1 */
92 /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */
93 /* But to improve the performance,
94 * we include zeroes in the output instead of zero padding either of the the inputs*/
95 /* If srcALen > srcBLen,
96 * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */
97 /* If srcALen < srcBLen,
98 * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */
99 if(srcALen >= srcBLen)
101 /* Initialization of inputA pointer */
104 /* Initialization of inputB pointer */
107 /* Number of output samples is calculated */
108 outBlockSize = (2u * srcALen) - 1u;
110 /* When srcALen > srcBLen, zero padding is done to srcB
111 * to make their lengths equal.
112 * Instead, (outBlockSize - (srcALen + srcBLen - 1))
113 * number of output samples are made zero */
114 j = outBlockSize - (srcALen + (srcBLen - 1u));
116 /* Updating the pointer position to non zero value */
122 /* Initialization of inputA pointer */
125 /* Initialization of inputB pointer */
128 /* srcBLen is always considered as shorter or equal to srcALen */
133 /* CORR(x, y) = Reverse order(CORR(y, x)) */
134 /* Hence set the destination pointer to point to the last output sample */
135 pOut = pDst + ((srcALen + srcBLen) - 2u);
137 /* Destination address modifier is set to -1 */
142 /* The function is internally
143 * divided into three parts according to the number of multiplications that has to be
144 * taken place between inputA samples and inputB samples. In the first part of the
145 * algorithm, the multiplications increase by one for every iteration.
146 * In the second part of the algorithm, srcBLen number of multiplications are done.
147 * In the third part of the algorithm, the multiplications decrease by one
148 * for every iteration.*/
149 /* The algorithm is implemented in three stages.
150 * The loop counters of each stage is initiated here. */
151 blockSize1 = srcBLen - 1u;
152 blockSize2 = srcALen - (srcBLen - 1u);
153 blockSize3 = blockSize1;
155 /* --------------------------
156 * Initializations of stage1
157 * -------------------------*/
159 /* sum = x[0] * y[srcBlen - 1]
160 * sum = x[0] * y[srcBlen - 2] + x[1] * y[srcBlen - 1]
162 * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1]
165 /* In this stage the MAC operations are increased by 1 for every iteration.
166 The count variable holds the number of MAC operations performed */
169 /* Working pointer of inputA */
172 /* Working pointer of inputB */
173 pSrc1 = pIn2 + (srcBLen - 1u);
176 /* ------------------------
178 * ----------------------*/
180 /* The first loop starts here */
181 while(blockSize1 > 0u)
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 - 4] , x[1] * y[srcBLen - 3] */
194 sum = __SMLAD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
195 /* x[3] * y[srcBLen - 1] , x[2] * y[srcBLen - 2] */
196 sum = __SMLAD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
198 /* Decrement the loop counter */
202 /* If the count is not a multiple of 4, compute any remaining MACs here.
203 ** No loop unrolling is used. */
208 /* Perform the multiply-accumulates */
209 /* x[0] * y[srcBLen - 1] */
210 sum = __SMLAD(*px++, *py++, sum);
212 /* Decrement the loop counter */
216 /* Store the result in the accumulator in the destination buffer. */
217 *pOut = (q15_t) (sum >> 15);
218 /* Destination pointer is updated according to the address modifier, inc */
221 /* Update the inputA and inputB pointers for next MAC calculation */
225 /* Increment the MAC count */
228 /* Decrement the loop counter */
232 /* --------------------------
233 * Initializations of stage2
234 * ------------------------*/
236 /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1]
237 * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1]
239 * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
242 /* Working pointer of inputA */
245 /* Working pointer of inputB */
248 /* Initialize inputB pointer of type q31 */
251 /* count is index by which the pointer pIn1 to be incremented */
254 /* -------------------
256 * ------------------*/
258 /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
259 * So, to loop unroll over blockSize2,
260 * srcBLen should be greater than or equal to 4, to loop unroll the srcBLen loop */
263 /* Loop unroll over blockSize2, by 4 */
264 blkCnt = blockSize2 >> 2u;
268 /* Set all accumulators to zero */
274 /* read x[0], x[1] samples */
275 x0 = *(q31_t *) (px++);
276 /* read x[1], x[2] samples */
277 x1 = *(q31_t *) (px++);
279 /* Apply loop unrolling and compute 4 MACs simultaneously. */
282 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
283 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
286 /* Read the first two inputB samples using SIMD:
290 /* acc0 += x[0] * y[0] + x[1] * y[1] */
291 acc0 = __SMLAD(x0, c0, acc0);
293 /* acc1 += x[1] * y[0] + x[2] * y[1] */
294 acc1 = __SMLAD(x1, c0, acc1);
296 /* Read x[2], x[3] */
297 x2 = *(q31_t *) (px++);
299 /* Read x[3], x[4] */
300 x3 = *(q31_t *) (px++);
302 /* acc2 += x[2] * y[0] + x[3] * y[1] */
303 acc2 = __SMLAD(x2, c0, acc2);
305 /* acc3 += x[3] * y[0] + x[4] * y[1] */
306 acc3 = __SMLAD(x3, c0, acc3);
308 /* Read y[2] and y[3] */
311 /* acc0 += x[2] * y[2] + x[3] * y[3] */
312 acc0 = __SMLAD(x2, c0, acc0);
314 /* acc1 += x[3] * y[2] + x[4] * y[3] */
315 acc1 = __SMLAD(x3, c0, acc1);
317 /* Read x[4], x[5] */
318 x0 = *(q31_t *) (px++);
320 /* Read x[5], x[6] */
321 x1 = *(q31_t *) (px++);
323 /* acc2 += x[4] * y[2] + x[5] * y[3] */
324 acc2 = __SMLAD(x0, c0, acc2);
326 /* acc3 += x[5] * y[2] + x[6] * y[3] */
327 acc3 = __SMLAD(x1, c0, acc3);
331 /* For the next MAC operations, SIMD is not used
332 * So, the 16 bit pointer if inputB, py is updated */
335 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
336 ** No loop unrolling is used. */
343 #ifdef ARM_MATH_BIG_ENDIAN
349 c0 = c0 & 0x0000FFFF;
351 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
354 x3 = *(q31_t *) px++;
356 /* Perform the multiply-accumulates */
357 acc0 = __SMLAD(x0, c0, acc0);
358 acc1 = __SMLAD(x1, c0, acc1);
359 acc2 = __SMLADX(x1, c0, acc2);
360 acc3 = __SMLADX(x3, c0, acc3);
365 /* Read y[4], y[5] */
368 /* Read x[7], x[8] */
369 x3 = *(q31_t *) px++;
372 x2 = *(q31_t *) px++;
374 /* Perform the multiply-accumulates */
375 acc0 = __SMLAD(x0, c0, acc0);
376 acc1 = __SMLAD(x1, c0, acc1);
377 acc2 = __SMLAD(x3, c0, acc2);
378 acc3 = __SMLAD(x2, c0, acc3);
383 /* Read y[4], y[5] */
386 /* Read x[7], x[8] */
387 x3 = *(q31_t *) px++;
390 x2 = *(q31_t *) px++;
392 /* Perform the multiply-accumulates */
393 acc0 = __SMLAD(x0, c0, acc0);
394 acc1 = __SMLAD(x1, c0, acc1);
395 acc2 = __SMLAD(x3, c0, acc2);
396 acc3 = __SMLAD(x2, c0, acc3);
399 #ifdef ARM_MATH_BIG_ENDIAN
401 c0 = c0 & 0xFFFF0000;
405 c0 = c0 & 0x0000FFFF;
407 #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
410 x3 = *(q31_t *) px++;
412 /* Perform the multiply-accumulates */
413 acc0 = __SMLADX(x1, c0, acc0);
414 acc1 = __SMLAD(x2, c0, acc1);
415 acc2 = __SMLADX(x2, c0, acc2);
416 acc3 = __SMLADX(x3, c0, acc3);
419 /* Store the result in the accumulator in the destination buffer. */
420 *pOut = (q15_t) (acc0 >> 15);
421 /* Destination pointer is updated according to the address modifier, inc */
424 *pOut = (q15_t) (acc1 >> 15);
427 *pOut = (q15_t) (acc2 >> 15);
430 *pOut = (q15_t) (acc3 >> 15);
433 /* Increment the pointer pIn1 index, count by 1 */
436 /* Update the inputA and inputB pointers for next MAC calculation */
442 /* Decrement the loop counter */
446 /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
447 ** No loop unrolling is used. */
448 blkCnt = blockSize2 % 0x4u;
452 /* Accumulator is made zero for every iteration */
455 /* Apply loop unrolling and compute 4 MACs simultaneously. */
458 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
459 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
462 /* Perform the multiply-accumulates */
463 sum += ((q31_t) * px++ * *py++);
464 sum += ((q31_t) * px++ * *py++);
465 sum += ((q31_t) * px++ * *py++);
466 sum += ((q31_t) * px++ * *py++);
468 /* Decrement the loop counter */
472 /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
473 ** No loop unrolling is used. */
478 /* Perform the multiply-accumulates */
479 sum += ((q31_t) * px++ * *py++);
481 /* Decrement the loop counter */
485 /* Store the result in the accumulator in the destination buffer. */
486 *pOut = (q15_t) (sum >> 15);
487 /* Destination pointer is updated according to the address modifier, inc */
490 /* Increment the pointer pIn1 index, count by 1 */
493 /* Update the inputA and inputB pointers for next MAC calculation */
497 /* Decrement the loop counter */
503 /* If the srcBLen is not a multiple of 4,
504 * the blockSize2 loop cannot be unrolled by 4 */
509 /* Accumulator is made zero for every iteration */
512 /* Loop over srcBLen */
517 /* Perform the multiply-accumulate */
518 sum += ((q31_t) * px++ * *py++);
520 /* Decrement the loop counter */
524 /* Store the result in the accumulator in the destination buffer. */
525 *pOut = (q15_t) (sum >> 15);
526 /* Destination pointer is updated according to the address modifier, inc */
529 /* Increment the MAC count */
532 /* Update the inputA and inputB pointers for next MAC calculation */
536 /* Decrement the loop counter */
541 /* --------------------------
542 * Initializations of stage3
543 * -------------------------*/
545 /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
546 * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
548 * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1]
549 * sum += x[srcALen-1] * y[0]
552 /* In this stage the MAC operations are decreased by 1 for every iteration.
553 The count variable holds the number of MAC operations performed */
554 count = srcBLen - 1u;
556 /* Working pointer of inputA */
557 pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
560 /* Working pointer of inputB */
563 /* -------------------
565 * ------------------*/
567 while(blockSize3 > 0u)
569 /* Accumulator is made zero for every iteration */
572 /* Apply loop unrolling and compute 4 MACs simultaneously. */
575 /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
576 ** a second loop below computes MACs for the remaining 1 to 3 samples. */
579 /* Perform the multiply-accumulates */
580 /* sum += x[srcALen - srcBLen + 4] * y[3] , sum += x[srcALen - srcBLen + 3] * y[2] */
581 sum = __SMLAD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
582 /* sum += x[srcALen - srcBLen + 2] * y[1] , sum += x[srcALen - srcBLen + 1] * y[0] */
583 sum = __SMLAD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
585 /* Decrement the loop counter */
589 /* If the count is not a multiple of 4, compute any remaining MACs here.
590 ** No loop unrolling is used. */
595 /* Perform the multiply-accumulates */
596 sum = __SMLAD(*px++, *py++, sum);
598 /* Decrement the loop counter */
602 /* Store the result in the accumulator in the destination buffer. */
603 *pOut = (q15_t) (sum >> 15);
604 /* Destination pointer is updated according to the address modifier, inc */
607 /* Update the inputA and inputB pointers for next MAC calculation */
611 /* Decrement the MAC count */
614 /* Decrement the loop counter */
621 * @} end of Corr group