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_q15.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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_q15.c
  9. *
  10. * Description: Correlation of Q15 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 Q15 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 a 64-bit internal accumulator.
  62. * Both inputs are in 1.15 format and multiplications yield a 2.30 result.
  63. * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  64. * This approach provides 33 guard bits and there is no risk of overflow.
  65. * The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
  66. *
  67. * \par
  68. * Refer to <code>arm_correlate_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
  69. *
  70. * \par
  71. * Refer the function <code>arm_correlate_opt_q15()</code> for a faster implementation of this function using scratch buffers.
  72. *
  73. */
  74. void arm_correlate_q15(
  75. q15_t * pSrcA,
  76. uint32_t srcALen,
  77. q15_t * pSrcB,
  78. uint32_t srcBLen,
  79. q15_t * pDst)
  80. {
  81. #if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE)
  82. /* Run the below code for Cortex-M4 and Cortex-M3 */
  83. q15_t *pIn1; /* inputA pointer */
  84. q15_t *pIn2; /* inputB pointer */
  85. q15_t *pOut = pDst; /* output pointer */
  86. q63_t sum, acc0, acc1, acc2, acc3; /* Accumulators */
  87. q15_t *px; /* Intermediate inputA pointer */
  88. q15_t *py; /* Intermediate inputB pointer */
  89. q15_t *pSrc1; /* Intermediate pointers */
  90. q31_t x0, x1, x2, x3, c0; /* temporary variables for holding input and coefficient values */
  91. uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counter */
  92. int32_t inc = 1; /* Destination address modifier */
  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)
  107. {
  108. /* Initialization of inputA pointer */
  109. pIn1 = (pSrcA);
  110. /* Initialization of inputB pointer */
  111. pIn2 = (pSrcB);
  112. /* Number of output samples is calculated */
  113. outBlockSize = (2u * srcALen) - 1u;
  114. /* When srcALen > srcBLen, zero padding is done to srcB
  115. * to make their lengths equal.
  116. * Instead, (outBlockSize - (srcALen + srcBLen - 1))
  117. * number of output samples are made zero */
  118. j = outBlockSize - (srcALen + (srcBLen - 1u));
  119. /* Updating the pointer position to non zero value */
  120. pOut += j;
  121. }
  122. else
  123. {
  124. /* Initialization of inputA pointer */
  125. pIn1 = (pSrcB);
  126. /* Initialization of inputB pointer */
  127. pIn2 = (pSrcA);
  128. /* srcBLen is always considered as shorter or equal to srcALen */
  129. j = srcBLen;
  130. srcBLen = srcALen;
  131. srcALen = j;
  132. /* CORR(x, y) = Reverse order(CORR(y, x)) */
  133. /* Hence set the destination pointer to point to the last output sample */
  134. pOut = pDst + ((srcALen + srcBLen) - 2u);
  135. /* Destination address modifier is set to -1 */
  136. inc = -1;
  137. }
  138. /* The function is internally
  139. * divided into three parts according to the number of multiplications that has to be
  140. * taken place between inputA samples and inputB samples. In the first part of the
  141. * algorithm, the multiplications increase by one for every iteration.
  142. * In the second part of the algorithm, srcBLen number of multiplications are done.
  143. * In the third part of the algorithm, the multiplications decrease by one
  144. * for every iteration.*/
  145. /* The algorithm is implemented in three stages.
  146. * The loop counters of each stage is initiated here. */
  147. blockSize1 = srcBLen - 1u;
  148. blockSize2 = srcALen - (srcBLen - 1u);
  149. blockSize3 = blockSize1;
  150. /* --------------------------
  151. * Initializations of stage1
  152. * -------------------------*/
  153. /* sum = x[0] * y[srcBlen - 1]
  154. * sum = x[0] * y[srcBlen - 2] + x[1] * y[srcBlen - 1]
  155. * ....
  156. * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1]
  157. */
  158. /* In this stage the MAC operations are increased by 1 for every iteration.
  159. The count variable holds the number of MAC operations performed */
  160. count = 1u;
  161. /* Working pointer of inputA */
  162. px = pIn1;
  163. /* Working pointer of inputB */
  164. pSrc1 = pIn2 + (srcBLen - 1u);
  165. py = pSrc1;
  166. /* ------------------------
  167. * Stage1 process
  168. * ----------------------*/
  169. /* The first loop starts here */
  170. while(blockSize1 > 0u)
  171. {
  172. /* Accumulator is made zero for every iteration */
  173. sum = 0;
  174. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  175. k = count >> 2;
  176. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  177. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  178. while(k > 0u)
  179. {
  180. /* x[0] * y[srcBLen - 4] , x[1] * y[srcBLen - 3] */
  181. sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
  182. /* x[3] * y[srcBLen - 1] , x[2] * y[srcBLen - 2] */
  183. sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
  184. /* Decrement the loop counter */
  185. k--;
  186. }
  187. /* If the count is not a multiple of 4, compute any remaining MACs here.
  188. ** No loop unrolling is used. */
  189. k = count % 0x4u;
  190. while(k > 0u)
  191. {
  192. /* Perform the multiply-accumulates */
  193. /* x[0] * y[srcBLen - 1] */
  194. sum = __SMLALD(*px++, *py++, sum);
  195. /* Decrement the loop counter */
  196. k--;
  197. }
  198. /* Store the result in the accumulator in the destination buffer. */
  199. *pOut = (q15_t) (__SSAT((sum >> 15), 16));
  200. /* Destination pointer is updated according to the address modifier, inc */
  201. pOut += inc;
  202. /* Update the inputA and inputB pointers for next MAC calculation */
  203. py = pSrc1 - count;
  204. px = pIn1;
  205. /* Increment the MAC count */
  206. count++;
  207. /* Decrement the loop counter */
  208. blockSize1--;
  209. }
  210. /* --------------------------
  211. * Initializations of stage2
  212. * ------------------------*/
  213. /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1]
  214. * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1]
  215. * ....
  216. * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  217. */
  218. /* Working pointer of inputA */
  219. px = pIn1;
  220. /* Working pointer of inputB */
  221. py = pIn2;
  222. /* count is index by which the pointer pIn1 to be incremented */
  223. count = 0u;
  224. /* -------------------
  225. * Stage2 process
  226. * ------------------*/
  227. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  228. * So, to loop unroll over blockSize2,
  229. * srcBLen should be greater than or equal to 4, to loop unroll the srcBLen loop */
  230. if(srcBLen >= 4u)
  231. {
  232. /* Loop unroll over blockSize2, by 4 */
  233. blkCnt = blockSize2 >> 2u;
  234. while(blkCnt > 0u)
  235. {
  236. /* Set all accumulators to zero */
  237. acc0 = 0;
  238. acc1 = 0;
  239. acc2 = 0;
  240. acc3 = 0;
  241. /* read x[0], x[1] samples */
  242. x0 = *__SIMD32(px);
  243. /* read x[1], x[2] samples */
  244. x1 = _SIMD32_OFFSET(px + 1);
  245. px += 2u;
  246. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  247. k = srcBLen >> 2u;
  248. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  249. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  250. do
  251. {
  252. /* Read the first two inputB samples using SIMD:
  253. * y[0] and y[1] */
  254. c0 = *__SIMD32(py)++;
  255. /* acc0 += x[0] * y[0] + x[1] * y[1] */
  256. acc0 = __SMLALD(x0, c0, acc0);
  257. /* acc1 += x[1] * y[0] + x[2] * y[1] */
  258. acc1 = __SMLALD(x1, c0, acc1);
  259. /* Read x[2], x[3] */
  260. x2 = *__SIMD32(px);
  261. /* Read x[3], x[4] */
  262. x3 = _SIMD32_OFFSET(px + 1);
  263. /* acc2 += x[2] * y[0] + x[3] * y[1] */
  264. acc2 = __SMLALD(x2, c0, acc2);
  265. /* acc3 += x[3] * y[0] + x[4] * y[1] */
  266. acc3 = __SMLALD(x3, c0, acc3);
  267. /* Read y[2] and y[3] */
  268. c0 = *__SIMD32(py)++;
  269. /* acc0 += x[2] * y[2] + x[3] * y[3] */
  270. acc0 = __SMLALD(x2, c0, acc0);
  271. /* acc1 += x[3] * y[2] + x[4] * y[3] */
  272. acc1 = __SMLALD(x3, c0, acc1);
  273. /* Read x[4], x[5] */
  274. x0 = _SIMD32_OFFSET(px + 2);
  275. /* Read x[5], x[6] */
  276. x1 = _SIMD32_OFFSET(px + 3);
  277. px += 4u;
  278. /* acc2 += x[4] * y[2] + x[5] * y[3] */
  279. acc2 = __SMLALD(x0, c0, acc2);
  280. /* acc3 += x[5] * y[2] + x[6] * y[3] */
  281. acc3 = __SMLALD(x1, c0, acc3);
  282. } while(--k);
  283. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  284. ** No loop unrolling is used. */
  285. k = srcBLen % 0x4u;
  286. if(k == 1u)
  287. {
  288. /* Read y[4] */
  289. c0 = *py;
  290. #ifdef ARM_MATH_BIG_ENDIAN
  291. c0 = c0 << 16u;
  292. #else
  293. c0 = c0 & 0x0000FFFF;
  294. #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
  295. /* Read x[7] */
  296. x3 = *__SIMD32(px);
  297. px++;
  298. /* Perform the multiply-accumulates */
  299. acc0 = __SMLALD(x0, c0, acc0);
  300. acc1 = __SMLALD(x1, c0, acc1);
  301. acc2 = __SMLALDX(x1, c0, acc2);
  302. acc3 = __SMLALDX(x3, c0, acc3);
  303. }
  304. if(k == 2u)
  305. {
  306. /* Read y[4], y[5] */
  307. c0 = *__SIMD32(py);
  308. /* Read x[7], x[8] */
  309. x3 = *__SIMD32(px);
  310. /* Read x[9] */
  311. x2 = _SIMD32_OFFSET(px + 1);
  312. px += 2u;
  313. /* Perform the multiply-accumulates */
  314. acc0 = __SMLALD(x0, c0, acc0);
  315. acc1 = __SMLALD(x1, c0, acc1);
  316. acc2 = __SMLALD(x3, c0, acc2);
  317. acc3 = __SMLALD(x2, c0, acc3);
  318. }
  319. if(k == 3u)
  320. {
  321. /* Read y[4], y[5] */
  322. c0 = *__SIMD32(py)++;
  323. /* Read x[7], x[8] */
  324. x3 = *__SIMD32(px);
  325. /* Read x[9] */
  326. x2 = _SIMD32_OFFSET(px + 1);
  327. /* Perform the multiply-accumulates */
  328. acc0 = __SMLALD(x0, c0, acc0);
  329. acc1 = __SMLALD(x1, c0, acc1);
  330. acc2 = __SMLALD(x3, c0, acc2);
  331. acc3 = __SMLALD(x2, c0, acc3);
  332. c0 = (*py);
  333. /* Read y[6] */
  334. #ifdef ARM_MATH_BIG_ENDIAN
  335. c0 = c0 << 16u;
  336. #else
  337. c0 = c0 & 0x0000FFFF;
  338. #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
  339. /* Read x[10] */
  340. x3 = _SIMD32_OFFSET(px + 2);
  341. px += 3u;
  342. /* Perform the multiply-accumulates */
  343. acc0 = __SMLALDX(x1, c0, acc0);
  344. acc1 = __SMLALD(x2, c0, acc1);
  345. acc2 = __SMLALDX(x2, c0, acc2);
  346. acc3 = __SMLALDX(x3, c0, acc3);
  347. }
  348. /* Store the result in the accumulator in the destination buffer. */
  349. *pOut = (q15_t) (__SSAT(acc0 >> 15, 16));
  350. /* Destination pointer is updated according to the address modifier, inc */
  351. pOut += inc;
  352. *pOut = (q15_t) (__SSAT(acc1 >> 15, 16));
  353. pOut += inc;
  354. *pOut = (q15_t) (__SSAT(acc2 >> 15, 16));
  355. pOut += inc;
  356. *pOut = (q15_t) (__SSAT(acc3 >> 15, 16));
  357. pOut += inc;
  358. /* Increment the count by 4 as 4 output values are computed */
  359. count += 4u;
  360. /* Update the inputA and inputB pointers for next MAC calculation */
  361. px = pIn1 + count;
  362. py = pIn2;
  363. /* Decrement the loop counter */
  364. blkCnt--;
  365. }
  366. /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
  367. ** No loop unrolling is used. */
  368. blkCnt = blockSize2 % 0x4u;
  369. while(blkCnt > 0u)
  370. {
  371. /* Accumulator is made zero for every iteration */
  372. sum = 0;
  373. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  374. k = srcBLen >> 2u;
  375. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  376. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  377. while(k > 0u)
  378. {
  379. /* Perform the multiply-accumulates */
  380. sum += ((q63_t) * px++ * *py++);
  381. sum += ((q63_t) * px++ * *py++);
  382. sum += ((q63_t) * px++ * *py++);
  383. sum += ((q63_t) * px++ * *py++);
  384. /* Decrement the loop counter */
  385. k--;
  386. }
  387. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  388. ** No loop unrolling is used. */
  389. k = srcBLen % 0x4u;
  390. while(k > 0u)
  391. {
  392. /* Perform the multiply-accumulates */
  393. sum += ((q63_t) * px++ * *py++);
  394. /* Decrement the loop counter */
  395. k--;
  396. }
  397. /* Store the result in the accumulator in the destination buffer. */
  398. *pOut = (q15_t) (__SSAT(sum >> 15, 16));
  399. /* Destination pointer is updated according to the address modifier, inc */
  400. pOut += inc;
  401. /* Increment count by 1, as one output value is computed */
  402. count++;
  403. /* Update the inputA and inputB pointers for next MAC calculation */
  404. px = pIn1 + count;
  405. py = pIn2;
  406. /* Decrement the loop counter */
  407. blkCnt--;
  408. }
  409. }
  410. else
  411. {
  412. /* If the srcBLen is not a multiple of 4,
  413. * the blockSize2 loop cannot be unrolled by 4 */
  414. blkCnt = blockSize2;
  415. while(blkCnt > 0u)
  416. {
  417. /* Accumulator is made zero for every iteration */
  418. sum = 0;
  419. /* Loop over srcBLen */
  420. k = srcBLen;
  421. while(k > 0u)
  422. {
  423. /* Perform the multiply-accumulate */
  424. sum += ((q63_t) * px++ * *py++);
  425. /* Decrement the loop counter */
  426. k--;
  427. }
  428. /* Store the result in the accumulator in the destination buffer. */
  429. *pOut = (q15_t) (__SSAT(sum >> 15, 16));
  430. /* Destination pointer is updated according to the address modifier, inc */
  431. pOut += inc;
  432. /* Increment the MAC count */
  433. count++;
  434. /* Update the inputA and inputB pointers for next MAC calculation */
  435. px = pIn1 + count;
  436. py = pIn2;
  437. /* Decrement the loop counter */
  438. blkCnt--;
  439. }
  440. }
  441. /* --------------------------
  442. * Initializations of stage3
  443. * -------------------------*/
  444. /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  445. * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1]
  446. * ....
  447. * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1]
  448. * sum += x[srcALen-1] * y[0]
  449. */
  450. /* In this stage the MAC operations are decreased by 1 for every iteration.
  451. The count variable holds the number of MAC operations performed */
  452. count = srcBLen - 1u;
  453. /* Working pointer of inputA */
  454. pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
  455. px = pSrc1;
  456. /* Working pointer of inputB */
  457. py = pIn2;
  458. /* -------------------
  459. * Stage3 process
  460. * ------------------*/
  461. while(blockSize3 > 0u)
  462. {
  463. /* Accumulator is made zero for every iteration */
  464. sum = 0;
  465. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  466. k = count >> 2u;
  467. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  468. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  469. while(k > 0u)
  470. {
  471. /* Perform the multiply-accumulates */
  472. /* sum += x[srcALen - srcBLen + 4] * y[3] , sum += x[srcALen - srcBLen + 3] * y[2] */
  473. sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
  474. /* sum += x[srcALen - srcBLen + 2] * y[1] , sum += x[srcALen - srcBLen + 1] * y[0] */
  475. sum = __SMLALD(*__SIMD32(px)++, *__SIMD32(py)++, sum);
  476. /* Decrement the loop counter */
  477. k--;
  478. }
  479. /* If the count is not a multiple of 4, compute any remaining MACs here.
  480. ** No loop unrolling is used. */
  481. k = count % 0x4u;
  482. while(k > 0u)
  483. {
  484. /* Perform the multiply-accumulates */
  485. sum = __SMLALD(*px++, *py++, sum);
  486. /* Decrement the loop counter */
  487. k--;
  488. }
  489. /* Store the result in the accumulator in the destination buffer. */
  490. *pOut = (q15_t) (__SSAT((sum >> 15), 16));
  491. /* Destination pointer is updated according to the address modifier, inc */
  492. pOut += inc;
  493. /* Update the inputA and inputB pointers for next MAC calculation */
  494. px = ++pSrc1;
  495. py = pIn2;
  496. /* Decrement the MAC count */
  497. count--;
  498. /* Decrement the loop counter */
  499. blockSize3--;
  500. }
  501. #else
  502. /* Run the below code for Cortex-M0 */
  503. q15_t *pIn1 = pSrcA; /* inputA pointer */
  504. q15_t *pIn2 = pSrcB + (srcBLen - 1u); /* inputB pointer */
  505. q63_t sum; /* Accumulators */
  506. uint32_t i = 0u, j; /* loop counters */
  507. uint32_t inv = 0u; /* Reverse order flag */
  508. uint32_t tot = 0u; /* Length */
  509. /* The algorithm implementation is based on the lengths of the inputs. */
  510. /* srcB is always made to slide across srcA. */
  511. /* So srcBLen is always considered as shorter or equal to srcALen */
  512. /* But CORR(x, y) is reverse of CORR(y, x) */
  513. /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */
  514. /* and a varaible, inv is set to 1 */
  515. /* If lengths are not equal then zero pad has to be done to make the two
  516. * inputs of same length. But to improve the performance, we include zeroes
  517. * in the output instead of zero padding either of the the inputs*/
  518. /* If srcALen > srcBLen, (srcALen - srcBLen) zeroes has to included in the
  519. * starting of the output buffer */
  520. /* If srcALen < srcBLen, (srcALen - srcBLen) zeroes has to included in the
  521. * ending of the output buffer */
  522. /* Once the zero padding is done the remaining of the output is calcualted
  523. * using convolution but with the shorter signal time shifted. */
  524. /* Calculate the length of the remaining sequence */
  525. tot = ((srcALen + srcBLen) - 2u);
  526. if(srcALen > srcBLen)
  527. {
  528. /* Calculating the number of zeros to be padded to the output */
  529. j = srcALen - srcBLen;
  530. /* Initialise the pointer after zero padding */
  531. pDst += j;
  532. }
  533. else if(srcALen < srcBLen)
  534. {
  535. /* Initialization to inputB pointer */
  536. pIn1 = pSrcB;
  537. /* Initialization to the end of inputA pointer */
  538. pIn2 = pSrcA + (srcALen - 1u);
  539. /* Initialisation of the pointer after zero padding */
  540. pDst = pDst + tot;
  541. /* Swapping the lengths */
  542. j = srcALen;
  543. srcALen = srcBLen;
  544. srcBLen = j;
  545. /* Setting the reverse flag */
  546. inv = 1;
  547. }
  548. /* Loop to calculate convolution for output length number of times */
  549. for (i = 0u; i <= tot; i++)
  550. {
  551. /* Initialize sum with zero to carry on MAC operations */
  552. sum = 0;
  553. /* Loop to perform MAC operations according to convolution equation */
  554. for (j = 0u; j <= i; j++)
  555. {
  556. /* Check the array limitations */
  557. if((((i - j) < srcBLen) && (j < srcALen)))
  558. {
  559. /* z[i] += x[i-j] * y[j] */
  560. sum += ((q31_t) pIn1[j] * pIn2[-((int32_t) i - j)]);
  561. }
  562. }
  563. /* Store the output in the destination buffer */
  564. if(inv == 1)
  565. *pDst-- = (q15_t) __SSAT((sum >> 15u), 16u);
  566. else
  567. *pDst++ = (q15_t) __SSAT((sum >> 15u), 16u);
  568. }
  569. #endif /*#if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE) */
  570. }
  571. /**
  572. * @} end of Corr group
  573. */