Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arm_correlate_q31.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 17. January 2013
  5. * $Revision: V1.4.1
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_correlate_q31.c
  9. *
  10. * Description: Correlation of Q31 sequences.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  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
  22. * distribution.
  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.
  26. *
  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. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupFilters
  43. */
  44. /**
  45. * @addtogroup Corr
  46. * @{
  47. */
  48. /**
  49. * @brief Correlation of Q31 sequences.
  50. * @param[in] *pSrcA points to the first input sequence.
  51. * @param[in] srcALen length of the first input sequence.
  52. * @param[in] *pSrcB points to the second input sequence.
  53. * @param[in] srcBLen length of the second input sequence.
  54. * @param[out] *pDst points to the location where the output result is written. Length 2 * max(srcALen, srcBLen) - 1.
  55. * @return none.
  56. *
  57. * @details
  58. * <b>Scaling and Overflow Behavior:</b>
  59. *
  60. * \par
  61. * The function is implemented using an internal 64-bit accumulator.
  62. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  63. * There is no saturation on intermediate additions.
  64. * Thus, if the accumulator overflows it wraps around and distorts the result.
  65. * The input signals should be scaled down to avoid intermediate overflows.
  66. * Scale down one of the inputs by 1/min(srcALen, srcBLen)to avoid overflows since a
  67. * maximum of min(srcALen, srcBLen) number of additions is carried internally.
  68. * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  69. *
  70. * \par
  71. * See <code>arm_correlate_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  72. */
  73. void arm_correlate_q31(
  74. q31_t * pSrcA,
  75. uint32_t srcALen,
  76. q31_t * pSrcB,
  77. uint32_t srcBLen,
  78. q31_t * pDst)
  79. {
  80. #ifndef ARM_MATH_CM0_FAMILY
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. q31_t *pIn1; /* inputA pointer */
  83. q31_t *pIn2; /* inputB pointer */
  84. q31_t *pOut = pDst; /* output pointer */
  85. q31_t *px; /* Intermediate inputA pointer */
  86. q31_t *py; /* Intermediate inputB pointer */
  87. q31_t *pSrc1; /* Intermediate pointers */
  88. q63_t sum, acc0, acc1, acc2; /* Accumulators */
  89. q31_t x0, x1, x2, c0; /* temporary variables for holding input and coefficient values */
  90. uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counter */
  91. int32_t inc = 1; /* Destination address modifier */
  92. /* The algorithm implementation is based on the lengths of the inputs. */
  93. /* srcB is always made to slide across srcA. */
  94. /* So srcBLen is always considered as shorter or equal to srcALen */
  95. /* But CORR(x, y) is reverse of CORR(y, x) */
  96. /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
  97. /* and the destination pointer modifier, inc is set to -1 */
  98. /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */
  99. /* But to improve the performance,
  100. * we include zeroes in the output instead of zero padding either of the the inputs*/
  101. /* If srcALen > srcBLen,
  102. * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */
  103. /* If srcALen < srcBLen,
  104. * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */
  105. if(srcALen >= srcBLen)
  106. {
  107. /* Initialization of inputA pointer */
  108. pIn1 = (pSrcA);
  109. /* Initialization of inputB pointer */
  110. pIn2 = (pSrcB);
  111. /* Number of output samples is calculated */
  112. outBlockSize = (2u * srcALen) - 1u;
  113. /* When srcALen > srcBLen, zero padding is done to srcB
  114. * to make their lengths equal.
  115. * Instead, (outBlockSize - (srcALen + srcBLen - 1))
  116. * number of output samples are made zero */
  117. j = outBlockSize - (srcALen + (srcBLen - 1u));
  118. /* Updating the pointer position to non zero value */
  119. pOut += j;
  120. }
  121. else
  122. {
  123. /* Initialization of inputA pointer */
  124. pIn1 = (pSrcB);
  125. /* Initialization of inputB pointer */
  126. pIn2 = (pSrcA);
  127. /* srcBLen is always considered as shorter or equal to srcALen */
  128. j = srcBLen;
  129. srcBLen = srcALen;
  130. srcALen = j;
  131. /* CORR(x, y) = Reverse order(CORR(y, x)) */
  132. /* Hence set the destination pointer to point to the last output sample */
  133. pOut = pDst + ((srcALen + srcBLen) - 2u);
  134. /* Destination address modifier is set to -1 */
  135. inc = -1;
  136. }
  137. /* The function is internally
  138. * divided into three parts according to the number of multiplications that has to be
  139. * taken place between inputA samples and inputB samples. In the first part of the
  140. * algorithm, the multiplications increase by one for every iteration.
  141. * In the second part of the algorithm, srcBLen number of multiplications are done.
  142. * In the third part of the algorithm, the multiplications decrease by one
  143. * for every iteration.*/
  144. /* The algorithm is implemented in three stages.
  145. * The loop counters of each stage is initiated here. */
  146. blockSize1 = srcBLen - 1u;
  147. blockSize2 = srcALen - (srcBLen - 1u);
  148. blockSize3 = blockSize1;
  149. /* --------------------------
  150. * Initializations of stage1
  151. * -------------------------*/
  152. /* sum = x[0] * y[srcBlen - 1]
  153. * sum = x[0] * y[srcBlen - 2] + x[1] * y[srcBlen - 1]
  154. * ....
  155. * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1]
  156. */
  157. /* In this stage the MAC operations are increased by 1 for every iteration.
  158. The count variable holds the number of MAC operations performed */
  159. count = 1u;
  160. /* Working pointer of inputA */
  161. px = pIn1;
  162. /* Working pointer of inputB */
  163. pSrc1 = pIn2 + (srcBLen - 1u);
  164. py = pSrc1;
  165. /* ------------------------
  166. * Stage1 process
  167. * ----------------------*/
  168. /* The first stage starts here */
  169. while(blockSize1 > 0u)
  170. {
  171. /* Accumulator is made zero for every iteration */
  172. sum = 0;
  173. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  174. k = count >> 2;
  175. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  176. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  177. while(k > 0u)
  178. {
  179. /* x[0] * y[srcBLen - 4] */
  180. sum += (q63_t) * px++ * (*py++);
  181. /* x[1] * y[srcBLen - 3] */
  182. sum += (q63_t) * px++ * (*py++);
  183. /* x[2] * y[srcBLen - 2] */
  184. sum += (q63_t) * px++ * (*py++);
  185. /* x[3] * y[srcBLen - 1] */
  186. sum += (q63_t) * px++ * (*py++);
  187. /* Decrement the loop counter */
  188. k--;
  189. }
  190. /* If the count is not a multiple of 4, compute any remaining MACs here.
  191. ** No loop unrolling is used. */
  192. k = count % 0x4u;
  193. while(k > 0u)
  194. {
  195. /* Perform the multiply-accumulates */
  196. /* x[0] * y[srcBLen - 1] */
  197. sum += (q63_t) * px++ * (*py++);
  198. /* Decrement the loop counter */
  199. k--;
  200. }
  201. /* Store the result in the accumulator in the destination buffer. */
  202. *pOut = (q31_t) (sum >> 31);
  203. /* Destination pointer is updated according to the address modifier, inc */
  204. pOut += inc;
  205. /* Update the inputA and inputB pointers for next MAC calculation */
  206. py = pSrc1 - count;
  207. px = pIn1;
  208. /* Increment the MAC count */
  209. count++;
  210. /* Decrement the loop counter */
  211. blockSize1--;
  212. }
  213. /* --------------------------
  214. * Initializations of stage2
  215. * ------------------------*/
  216. /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1]
  217. * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1]
  218. * ....
  219. * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  220. */
  221. /* Working pointer of inputA */
  222. px = pIn1;
  223. /* Working pointer of inputB */
  224. py = pIn2;
  225. /* count is index by which the pointer pIn1 to be incremented */
  226. count = 0u;
  227. /* -------------------
  228. * Stage2 process
  229. * ------------------*/
  230. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  231. * So, to loop unroll over blockSize2,
  232. * srcBLen should be greater than or equal to 4 */
  233. if(srcBLen >= 4u)
  234. {
  235. /* Loop unroll by 3 */
  236. blkCnt = blockSize2 / 3;
  237. while(blkCnt > 0u)
  238. {
  239. /* Set all accumulators to zero */
  240. acc0 = 0;
  241. acc1 = 0;
  242. acc2 = 0;
  243. /* read x[0], x[1] samples */
  244. x0 = *(px++);
  245. x1 = *(px++);
  246. /* Apply loop unrolling and compute 3 MACs simultaneously. */
  247. k = srcBLen / 3;
  248. /* First part of the processing with loop unrolling. Compute 3 MACs at a time.
  249. ** a second loop below computes MACs for the remaining 1 to 2 samples. */
  250. do
  251. {
  252. /* Read y[0] sample */
  253. c0 = *(py);
  254. /* Read x[2] sample */
  255. x2 = *(px);
  256. /* Perform the multiply-accumulate */
  257. /* acc0 += x[0] * y[0] */
  258. acc0 += ((q63_t) x0 * c0);
  259. /* acc1 += x[1] * y[0] */
  260. acc1 += ((q63_t) x1 * c0);
  261. /* acc2 += x[2] * y[0] */
  262. acc2 += ((q63_t) x2 * c0);
  263. /* Read y[1] sample */
  264. c0 = *(py + 1u);
  265. /* Read x[3] sample */
  266. x0 = *(px + 1u);
  267. /* Perform the multiply-accumulates */
  268. /* acc0 += x[1] * y[1] */
  269. acc0 += ((q63_t) x1 * c0);
  270. /* acc1 += x[2] * y[1] */
  271. acc1 += ((q63_t) x2 * c0);
  272. /* acc2 += x[3] * y[1] */
  273. acc2 += ((q63_t) x0 * c0);
  274. /* Read y[2] sample */
  275. c0 = *(py + 2u);
  276. /* Read x[4] sample */
  277. x1 = *(px + 2u);
  278. /* Perform the multiply-accumulates */
  279. /* acc0 += x[2] * y[2] */
  280. acc0 += ((q63_t) x2 * c0);
  281. /* acc1 += x[3] * y[2] */
  282. acc1 += ((q63_t) x0 * c0);
  283. /* acc2 += x[4] * y[2] */
  284. acc2 += ((q63_t) x1 * c0);
  285. /* update scratch pointers */
  286. px += 3u;
  287. py += 3u;
  288. } while(--k);
  289. /* If the srcBLen is not a multiple of 3, compute any remaining MACs here.
  290. ** No loop unrolling is used. */
  291. k = srcBLen - (3 * (srcBLen / 3));
  292. while(k > 0u)
  293. {
  294. /* Read y[4] sample */
  295. c0 = *(py++);
  296. /* Read x[7] sample */
  297. x2 = *(px++);
  298. /* Perform the multiply-accumulates */
  299. /* acc0 += x[4] * y[4] */
  300. acc0 += ((q63_t) x0 * c0);
  301. /* acc1 += x[5] * y[4] */
  302. acc1 += ((q63_t) x1 * c0);
  303. /* acc2 += x[6] * y[4] */
  304. acc2 += ((q63_t) x2 * c0);
  305. /* Reuse the present samples for the next MAC */
  306. x0 = x1;
  307. x1 = x2;
  308. /* Decrement the loop counter */
  309. k--;
  310. }
  311. /* Store the result in the accumulator in the destination buffer. */
  312. *pOut = (q31_t) (acc0 >> 31);
  313. /* Destination pointer is updated according to the address modifier, inc */
  314. pOut += inc;
  315. *pOut = (q31_t) (acc1 >> 31);
  316. pOut += inc;
  317. *pOut = (q31_t) (acc2 >> 31);
  318. pOut += inc;
  319. /* Increment the pointer pIn1 index, count by 3 */
  320. count += 3u;
  321. /* Update the inputA and inputB pointers for next MAC calculation */
  322. px = pIn1 + count;
  323. py = pIn2;
  324. /* Decrement the loop counter */
  325. blkCnt--;
  326. }
  327. /* If the blockSize2 is not a multiple of 3, compute any remaining output samples here.
  328. ** No loop unrolling is used. */
  329. blkCnt = blockSize2 - 3 * (blockSize2 / 3);
  330. while(blkCnt > 0u)
  331. {
  332. /* Accumulator is made zero for every iteration */
  333. sum = 0;
  334. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  335. k = srcBLen >> 2u;
  336. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  337. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  338. while(k > 0u)
  339. {
  340. /* Perform the multiply-accumulates */
  341. sum += (q63_t) * px++ * (*py++);
  342. sum += (q63_t) * px++ * (*py++);
  343. sum += (q63_t) * px++ * (*py++);
  344. sum += (q63_t) * px++ * (*py++);
  345. /* Decrement the loop counter */
  346. k--;
  347. }
  348. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  349. ** No loop unrolling is used. */
  350. k = srcBLen % 0x4u;
  351. while(k > 0u)
  352. {
  353. /* Perform the multiply-accumulate */
  354. sum += (q63_t) * px++ * (*py++);
  355. /* Decrement the loop counter */
  356. k--;
  357. }
  358. /* Store the result in the accumulator in the destination buffer. */
  359. *pOut = (q31_t) (sum >> 31);
  360. /* Destination pointer is updated according to the address modifier, inc */
  361. pOut += inc;
  362. /* Increment the MAC count */
  363. count++;
  364. /* Update the inputA and inputB pointers for next MAC calculation */
  365. px = pIn1 + count;
  366. py = pIn2;
  367. /* Decrement the loop counter */
  368. blkCnt--;
  369. }
  370. }
  371. else
  372. {
  373. /* If the srcBLen is not a multiple of 4,
  374. * the blockSize2 loop cannot be unrolled by 4 */
  375. blkCnt = blockSize2;
  376. while(blkCnt > 0u)
  377. {
  378. /* Accumulator is made zero for every iteration */
  379. sum = 0;
  380. /* Loop over srcBLen */
  381. k = srcBLen;
  382. while(k > 0u)
  383. {
  384. /* Perform the multiply-accumulate */
  385. sum += (q63_t) * px++ * (*py++);
  386. /* Decrement the loop counter */
  387. k--;
  388. }
  389. /* Store the result in the accumulator in the destination buffer. */
  390. *pOut = (q31_t) (sum >> 31);
  391. /* Destination pointer is updated according to the address modifier, inc */
  392. pOut += inc;
  393. /* Increment the MAC count */
  394. count++;
  395. /* Update the inputA and inputB pointers for next MAC calculation */
  396. px = pIn1 + count;
  397. py = pIn2;
  398. /* Decrement the loop counter */
  399. blkCnt--;
  400. }
  401. }
  402. /* --------------------------
  403. * Initializations of stage3
  404. * -------------------------*/
  405. /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  406. * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  407. * ....
  408. * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1]
  409. * sum += x[srcALen-1] * y[0]
  410. */
  411. /* In this stage the MAC operations are decreased by 1 for every iteration.
  412. The count variable holds the number of MAC operations performed */
  413. count = srcBLen - 1u;
  414. /* Working pointer of inputA */
  415. pSrc1 = pIn1 + (srcALen - (srcBLen - 1u));
  416. px = pSrc1;
  417. /* Working pointer of inputB */
  418. py = pIn2;
  419. /* -------------------
  420. * Stage3 process
  421. * ------------------*/
  422. while(blockSize3 > 0u)
  423. {
  424. /* Accumulator is made zero for every iteration */
  425. sum = 0;
  426. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  427. k = count >> 2u;
  428. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  429. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  430. while(k > 0u)
  431. {
  432. /* Perform the multiply-accumulates */
  433. /* sum += x[srcALen - srcBLen + 4] * y[3] */
  434. sum += (q63_t) * px++ * (*py++);
  435. /* sum += x[srcALen - srcBLen + 3] * y[2] */
  436. sum += (q63_t) * px++ * (*py++);
  437. /* sum += x[srcALen - srcBLen + 2] * y[1] */
  438. sum += (q63_t) * px++ * (*py++);
  439. /* sum += x[srcALen - srcBLen + 1] * y[0] */
  440. sum += (q63_t) * px++ * (*py++);
  441. /* Decrement the loop counter */
  442. k--;
  443. }
  444. /* If the count is not a multiple of 4, compute any remaining MACs here.
  445. ** No loop unrolling is used. */
  446. k = count % 0x4u;
  447. while(k > 0u)
  448. {
  449. /* Perform the multiply-accumulates */
  450. sum += (q63_t) * px++ * (*py++);
  451. /* Decrement the loop counter */
  452. k--;
  453. }
  454. /* Store the result in the accumulator in the destination buffer. */
  455. *pOut = (q31_t) (sum >> 31);
  456. /* Destination pointer is updated according to the address modifier, inc */
  457. pOut += inc;
  458. /* Update the inputA and inputB pointers for next MAC calculation */
  459. px = ++pSrc1;
  460. py = pIn2;
  461. /* Decrement the MAC count */
  462. count--;
  463. /* Decrement the loop counter */
  464. blockSize3--;
  465. }
  466. #else
  467. /* Run the below code for Cortex-M0 */
  468. q31_t *pIn1 = pSrcA; /* inputA pointer */
  469. q31_t *pIn2 = pSrcB + (srcBLen - 1u); /* inputB pointer */
  470. q63_t sum; /* Accumulators */
  471. uint32_t i = 0u, j; /* loop counters */
  472. uint32_t inv = 0u; /* Reverse order flag */
  473. uint32_t tot = 0u; /* Length */
  474. /* The algorithm implementation is based on the lengths of the inputs. */
  475. /* srcB is always made to slide across srcA. */
  476. /* So srcBLen is always considered as shorter or equal to srcALen */
  477. /* But CORR(x, y) is reverse of CORR(y, x) */
  478. /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
  479. /* and a varaible, inv is set to 1 */
  480. /* If lengths are not equal then zero pad has to be done to make the two
  481. * inputs of same length. But to improve the performance, we include zeroes
  482. * in the output instead of zero padding either of the the inputs*/
  483. /* If srcALen > srcBLen, (srcALen - srcBLen) zeroes has to included in the
  484. * starting of the output buffer */
  485. /* If srcALen < srcBLen, (srcALen - srcBLen) zeroes has to included in the
  486. * ending of the output buffer */
  487. /* Once the zero padding is done the remaining of the output is calcualted
  488. * using correlation but with the shorter signal time shifted. */
  489. /* Calculate the length of the remaining sequence */
  490. tot = ((srcALen + srcBLen) - 2u);
  491. if(srcALen > srcBLen)
  492. {
  493. /* Calculating the number of zeros to be padded to the output */
  494. j = srcALen - srcBLen;
  495. /* Initialise the pointer after zero padding */
  496. pDst += j;
  497. }
  498. else if(srcALen < srcBLen)
  499. {
  500. /* Initialization to inputB pointer */
  501. pIn1 = pSrcB;
  502. /* Initialization to the end of inputA pointer */
  503. pIn2 = pSrcA + (srcALen - 1u);
  504. /* Initialisation of the pointer after zero padding */
  505. pDst = pDst + tot;
  506. /* Swapping the lengths */
  507. j = srcALen;
  508. srcALen = srcBLen;
  509. srcBLen = j;
  510. /* Setting the reverse flag */
  511. inv = 1;
  512. }
  513. /* Loop to calculate correlation for output length number of times */
  514. for (i = 0u; i <= tot; i++)
  515. {
  516. /* Initialize sum with zero to carry on MAC operations */
  517. sum = 0;
  518. /* Loop to perform MAC operations according to correlation equation */
  519. for (j = 0u; j <= i; j++)
  520. {
  521. /* Check the array limitations */
  522. if((((i - j) < srcBLen) && (j < srcALen)))
  523. {
  524. /* z[i] += x[i-j] * y[j] */
  525. sum += ((q63_t) pIn1[j] * pIn2[-((int32_t) i - j)]);
  526. }
  527. }
  528. /* Store the output in the destination buffer */
  529. if(inv == 1)
  530. *pDst-- = (q31_t) (sum >> 31u);
  531. else
  532. *pDst++ = (q31_t) (sum >> 31u);
  533. }
  534. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  535. }
  536. /**
  537. * @} end of Corr group
  538. */