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_conv_q7.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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_conv_q7.c
  9. *
  10. * Description: Convolution of Q7 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 Conv
  46. * @{
  47. */
  48. /**
  49. * @brief Convolution of Q7 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 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 32-bit internal accumulator.
  62. * Both the inputs are represented in 1.7 format and multiplications yield a 2.14 result.
  63. * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format.
  64. * This approach provides 17 guard bits and there is no risk of overflow as long as <code>max(srcALen, srcBLen)<131072</code>.
  65. * The 18.14 result is then truncated to 18.7 format by discarding the low 7 bits and then saturated to 1.7 format.
  66. *
  67. * \par
  68. * Refer the function <code>arm_conv_opt_q7()</code> for a faster implementation of this function.
  69. *
  70. */
  71. void arm_conv_q7(
  72. q7_t * pSrcA,
  73. uint32_t srcALen,
  74. q7_t * pSrcB,
  75. uint32_t srcBLen,
  76. q7_t * pDst)
  77. {
  78. #ifndef ARM_MATH_CM0_FAMILY
  79. /* Run the below code for Cortex-M4 and Cortex-M3 */
  80. q7_t *pIn1; /* inputA pointer */
  81. q7_t *pIn2; /* inputB pointer */
  82. q7_t *pOut = pDst; /* output pointer */
  83. q7_t *px; /* Intermediate inputA pointer */
  84. q7_t *py; /* Intermediate inputB pointer */
  85. q7_t *pSrc1, *pSrc2; /* Intermediate pointers */
  86. q7_t x0, x1, x2, x3, c0, c1; /* Temporary variables to hold state and coefficient values */
  87. q31_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
  88. q31_t input1, input2; /* Temporary input variables */
  89. q15_t in1, in2; /* Temporary input variables */
  90. uint32_t j, k, count, blkCnt, blockSize1, blockSize2, blockSize3; /* loop counter */
  91. /* The algorithm implementation is based on the lengths of the inputs. */
  92. /* srcB is always made to slide across srcA. */
  93. /* So srcBLen is always considered as shorter or equal to srcALen */
  94. if(srcALen >= srcBLen)
  95. {
  96. /* Initialization of inputA pointer */
  97. pIn1 = pSrcA;
  98. /* Initialization of inputB pointer */
  99. pIn2 = pSrcB;
  100. }
  101. else
  102. {
  103. /* Initialization of inputA pointer */
  104. pIn1 = pSrcB;
  105. /* Initialization of inputB pointer */
  106. pIn2 = pSrcA;
  107. /* srcBLen is always considered as shorter or equal to srcALen */
  108. j = srcBLen;
  109. srcBLen = srcALen;
  110. srcALen = j;
  111. }
  112. /* conv(x,y) at n = x[n] * y[0] + x[n-1] * y[1] + x[n-2] * y[2] + ...+ x[n-N+1] * y[N -1] */
  113. /* The function is internally
  114. * divided into three stages according to the number of multiplications that has to be
  115. * taken place between inputA samples and inputB samples. In the first stage of the
  116. * algorithm, the multiplications increase by one for every iteration.
  117. * In the second stage of the algorithm, srcBLen number of multiplications are done.
  118. * In the third stage of the algorithm, the multiplications decrease by one
  119. * for every iteration. */
  120. /* The algorithm is implemented in three stages.
  121. The loop counters of each stage is initiated here. */
  122. blockSize1 = srcBLen - 1u;
  123. blockSize2 = (srcALen - srcBLen) + 1u;
  124. blockSize3 = blockSize1;
  125. /* --------------------------
  126. * Initializations of stage1
  127. * -------------------------*/
  128. /* sum = x[0] * y[0]
  129. * sum = x[0] * y[1] + x[1] * y[0]
  130. * ....
  131. * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
  132. */
  133. /* In this stage the MAC operations are increased by 1 for every iteration.
  134. The count variable holds the number of MAC operations performed */
  135. count = 1u;
  136. /* Working pointer of inputA */
  137. px = pIn1;
  138. /* Working pointer of inputB */
  139. py = pIn2;
  140. /* ------------------------
  141. * Stage1 process
  142. * ----------------------*/
  143. /* The first stage starts here */
  144. while(blockSize1 > 0u)
  145. {
  146. /* Accumulator is made zero for every iteration */
  147. sum = 0;
  148. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  149. k = count >> 2u;
  150. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  151. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  152. while(k > 0u)
  153. {
  154. /* x[0] , x[1] */
  155. in1 = (q15_t) * px++;
  156. in2 = (q15_t) * px++;
  157. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  158. /* y[srcBLen - 1] , y[srcBLen - 2] */
  159. in1 = (q15_t) * py--;
  160. in2 = (q15_t) * py--;
  161. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  162. /* x[0] * y[srcBLen - 1] */
  163. /* x[1] * y[srcBLen - 2] */
  164. sum = __SMLAD(input1, input2, sum);
  165. /* x[2] , x[3] */
  166. in1 = (q15_t) * px++;
  167. in2 = (q15_t) * px++;
  168. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  169. /* y[srcBLen - 3] , y[srcBLen - 4] */
  170. in1 = (q15_t) * py--;
  171. in2 = (q15_t) * py--;
  172. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  173. /* x[2] * y[srcBLen - 3] */
  174. /* x[3] * y[srcBLen - 4] */
  175. sum = __SMLAD(input1, input2, sum);
  176. /* Decrement the loop counter */
  177. k--;
  178. }
  179. /* If the count is not a multiple of 4, compute any remaining MACs here.
  180. ** No loop unrolling is used. */
  181. k = count % 0x4u;
  182. while(k > 0u)
  183. {
  184. /* Perform the multiply-accumulates */
  185. sum += ((q15_t) * px++ * *py--);
  186. /* Decrement the loop counter */
  187. k--;
  188. }
  189. /* Store the result in the accumulator in the destination buffer. */
  190. *pOut++ = (q7_t) (__SSAT(sum >> 7u, 8));
  191. /* Update the inputA and inputB pointers for next MAC calculation */
  192. py = pIn2 + count;
  193. px = pIn1;
  194. /* Increment the MAC count */
  195. count++;
  196. /* Decrement the loop counter */
  197. blockSize1--;
  198. }
  199. /* --------------------------
  200. * Initializations of stage2
  201. * ------------------------*/
  202. /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
  203. * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
  204. * ....
  205. * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
  206. */
  207. /* Working pointer of inputA */
  208. px = pIn1;
  209. /* Working pointer of inputB */
  210. pSrc2 = pIn2 + (srcBLen - 1u);
  211. py = pSrc2;
  212. /* count is index by which the pointer pIn1 to be incremented */
  213. count = 0u;
  214. /* -------------------
  215. * Stage2 process
  216. * ------------------*/
  217. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  218. * So, to loop unroll over blockSize2,
  219. * srcBLen should be greater than or equal to 4 */
  220. if(srcBLen >= 4u)
  221. {
  222. /* Loop unroll over blockSize2, by 4 */
  223. blkCnt = blockSize2 >> 2u;
  224. while(blkCnt > 0u)
  225. {
  226. /* Set all accumulators to zero */
  227. acc0 = 0;
  228. acc1 = 0;
  229. acc2 = 0;
  230. acc3 = 0;
  231. /* read x[0], x[1], x[2] samples */
  232. x0 = *(px++);
  233. x1 = *(px++);
  234. x2 = *(px++);
  235. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  236. k = srcBLen >> 2u;
  237. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  238. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  239. do
  240. {
  241. /* Read y[srcBLen - 1] sample */
  242. c0 = *(py--);
  243. /* Read y[srcBLen - 2] sample */
  244. c1 = *(py--);
  245. /* Read x[3] sample */
  246. x3 = *(px++);
  247. /* x[0] and x[1] are packed */
  248. in1 = (q15_t) x0;
  249. in2 = (q15_t) x1;
  250. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  251. /* y[srcBLen - 1] and y[srcBLen - 2] are packed */
  252. in1 = (q15_t) c0;
  253. in2 = (q15_t) c1;
  254. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  255. /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
  256. acc0 = __SMLAD(input1, input2, acc0);
  257. /* x[1] and x[2] are packed */
  258. in1 = (q15_t) x1;
  259. in2 = (q15_t) x2;
  260. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  261. /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
  262. acc1 = __SMLAD(input1, input2, acc1);
  263. /* x[2] and x[3] are packed */
  264. in1 = (q15_t) x2;
  265. in2 = (q15_t) x3;
  266. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  267. /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
  268. acc2 = __SMLAD(input1, input2, acc2);
  269. /* Read x[4] sample */
  270. x0 = *(px++);
  271. /* x[3] and x[4] are packed */
  272. in1 = (q15_t) x3;
  273. in2 = (q15_t) x0;
  274. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  275. /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
  276. acc3 = __SMLAD(input1, input2, acc3);
  277. /* Read y[srcBLen - 3] sample */
  278. c0 = *(py--);
  279. /* Read y[srcBLen - 4] sample */
  280. c1 = *(py--);
  281. /* Read x[5] sample */
  282. x1 = *(px++);
  283. /* x[2] and x[3] are packed */
  284. in1 = (q15_t) x2;
  285. in2 = (q15_t) x3;
  286. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  287. /* y[srcBLen - 3] and y[srcBLen - 4] are packed */
  288. in1 = (q15_t) c0;
  289. in2 = (q15_t) c1;
  290. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  291. /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
  292. acc0 = __SMLAD(input1, input2, acc0);
  293. /* x[3] and x[4] are packed */
  294. in1 = (q15_t) x3;
  295. in2 = (q15_t) x0;
  296. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  297. /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
  298. acc1 = __SMLAD(input1, input2, acc1);
  299. /* x[4] and x[5] are packed */
  300. in1 = (q15_t) x0;
  301. in2 = (q15_t) x1;
  302. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  303. /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
  304. acc2 = __SMLAD(input1, input2, acc2);
  305. /* Read x[6] sample */
  306. x2 = *(px++);
  307. /* x[5] and x[6] are packed */
  308. in1 = (q15_t) x1;
  309. in2 = (q15_t) x2;
  310. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  311. /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
  312. acc3 = __SMLAD(input1, input2, acc3);
  313. } while(--k);
  314. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  315. ** No loop unrolling is used. */
  316. k = srcBLen % 0x4u;
  317. while(k > 0u)
  318. {
  319. /* Read y[srcBLen - 5] sample */
  320. c0 = *(py--);
  321. /* Read x[7] sample */
  322. x3 = *(px++);
  323. /* Perform the multiply-accumulates */
  324. /* acc0 += x[4] * y[srcBLen - 5] */
  325. acc0 += ((q15_t) x0 * c0);
  326. /* acc1 += x[5] * y[srcBLen - 5] */
  327. acc1 += ((q15_t) x1 * c0);
  328. /* acc2 += x[6] * y[srcBLen - 5] */
  329. acc2 += ((q15_t) x2 * c0);
  330. /* acc3 += x[7] * y[srcBLen - 5] */
  331. acc3 += ((q15_t) x3 * c0);
  332. /* Reuse the present samples for the next MAC */
  333. x0 = x1;
  334. x1 = x2;
  335. x2 = x3;
  336. /* Decrement the loop counter */
  337. k--;
  338. }
  339. /* Store the result in the accumulator in the destination buffer. */
  340. *pOut++ = (q7_t) (__SSAT(acc0 >> 7u, 8));
  341. *pOut++ = (q7_t) (__SSAT(acc1 >> 7u, 8));
  342. *pOut++ = (q7_t) (__SSAT(acc2 >> 7u, 8));
  343. *pOut++ = (q7_t) (__SSAT(acc3 >> 7u, 8));
  344. /* Increment the pointer pIn1 index, count by 4 */
  345. count += 4u;
  346. /* Update the inputA and inputB pointers for next MAC calculation */
  347. px = pIn1 + count;
  348. py = pSrc2;
  349. /* Decrement the loop counter */
  350. blkCnt--;
  351. }
  352. /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
  353. ** No loop unrolling is used. */
  354. blkCnt = blockSize2 % 0x4u;
  355. while(blkCnt > 0u)
  356. {
  357. /* Accumulator is made zero for every iteration */
  358. sum = 0;
  359. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  360. k = srcBLen >> 2u;
  361. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  362. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  363. while(k > 0u)
  364. {
  365. /* Reading two inputs of SrcA buffer and packing */
  366. in1 = (q15_t) * px++;
  367. in2 = (q15_t) * px++;
  368. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  369. /* Reading two inputs of SrcB buffer and packing */
  370. in1 = (q15_t) * py--;
  371. in2 = (q15_t) * py--;
  372. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  373. /* Perform the multiply-accumulates */
  374. sum = __SMLAD(input1, input2, sum);
  375. /* Reading two inputs of SrcA buffer and packing */
  376. in1 = (q15_t) * px++;
  377. in2 = (q15_t) * px++;
  378. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  379. /* Reading two inputs of SrcB buffer and packing */
  380. in1 = (q15_t) * py--;
  381. in2 = (q15_t) * py--;
  382. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  383. /* Perform the multiply-accumulates */
  384. sum = __SMLAD(input1, input2, sum);
  385. /* Decrement the loop counter */
  386. k--;
  387. }
  388. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  389. ** No loop unrolling is used. */
  390. k = srcBLen % 0x4u;
  391. while(k > 0u)
  392. {
  393. /* Perform the multiply-accumulates */
  394. sum += ((q15_t) * px++ * *py--);
  395. /* Decrement the loop counter */
  396. k--;
  397. }
  398. /* Store the result in the accumulator in the destination buffer. */
  399. *pOut++ = (q7_t) (__SSAT(sum >> 7u, 8));
  400. /* Increment the pointer pIn1 index, count by 1 */
  401. count++;
  402. /* Update the inputA and inputB pointers for next MAC calculation */
  403. px = pIn1 + count;
  404. py = pSrc2;
  405. /* Decrement the loop counter */
  406. blkCnt--;
  407. }
  408. }
  409. else
  410. {
  411. /* If the srcBLen is not a multiple of 4,
  412. * the blockSize2 loop cannot be unrolled by 4 */
  413. blkCnt = blockSize2;
  414. while(blkCnt > 0u)
  415. {
  416. /* Accumulator is made zero for every iteration */
  417. sum = 0;
  418. /* srcBLen number of MACS should be performed */
  419. k = srcBLen;
  420. while(k > 0u)
  421. {
  422. /* Perform the multiply-accumulate */
  423. sum += ((q15_t) * px++ * *py--);
  424. /* Decrement the loop counter */
  425. k--;
  426. }
  427. /* Store the result in the accumulator in the destination buffer. */
  428. *pOut++ = (q7_t) (__SSAT(sum >> 7u, 8));
  429. /* Increment the MAC count */
  430. count++;
  431. /* Update the inputA and inputB pointers for next MAC calculation */
  432. px = pIn1 + count;
  433. py = pSrc2;
  434. /* Decrement the loop counter */
  435. blkCnt--;
  436. }
  437. }
  438. /* --------------------------
  439. * Initializations of stage3
  440. * -------------------------*/
  441. /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
  442. * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
  443. * ....
  444. * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
  445. * sum += x[srcALen-1] * y[srcBLen-1]
  446. */
  447. /* In this stage the MAC operations are decreased by 1 for every iteration.
  448. The blockSize3 variable holds the number of MAC operations performed */
  449. /* Working pointer of inputA */
  450. pSrc1 = pIn1 + (srcALen - (srcBLen - 1u));
  451. px = pSrc1;
  452. /* Working pointer of inputB */
  453. pSrc2 = pIn2 + (srcBLen - 1u);
  454. py = pSrc2;
  455. /* -------------------
  456. * Stage3 process
  457. * ------------------*/
  458. while(blockSize3 > 0u)
  459. {
  460. /* Accumulator is made zero for every iteration */
  461. sum = 0;
  462. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  463. k = blockSize3 >> 2u;
  464. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  465. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  466. while(k > 0u)
  467. {
  468. /* Reading two inputs, x[srcALen - srcBLen + 1] and x[srcALen - srcBLen + 2] of SrcA buffer and packing */
  469. in1 = (q15_t) * px++;
  470. in2 = (q15_t) * px++;
  471. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  472. /* Reading two inputs, y[srcBLen - 1] and y[srcBLen - 2] of SrcB buffer and packing */
  473. in1 = (q15_t) * py--;
  474. in2 = (q15_t) * py--;
  475. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  476. /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
  477. /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
  478. sum = __SMLAD(input1, input2, sum);
  479. /* Reading two inputs, x[srcALen - srcBLen + 3] and x[srcALen - srcBLen + 4] of SrcA buffer and packing */
  480. in1 = (q15_t) * px++;
  481. in2 = (q15_t) * px++;
  482. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  483. /* Reading two inputs, y[srcBLen - 3] and y[srcBLen - 4] of SrcB buffer and packing */
  484. in1 = (q15_t) * py--;
  485. in2 = (q15_t) * py--;
  486. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16u);
  487. /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
  488. /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
  489. sum = __SMLAD(input1, input2, sum);
  490. /* Decrement the loop counter */
  491. k--;
  492. }
  493. /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
  494. ** No loop unrolling is used. */
  495. k = blockSize3 % 0x4u;
  496. while(k > 0u)
  497. {
  498. /* Perform the multiply-accumulates */
  499. sum += ((q15_t) * px++ * *py--);
  500. /* Decrement the loop counter */
  501. k--;
  502. }
  503. /* Store the result in the accumulator in the destination buffer. */
  504. *pOut++ = (q7_t) (__SSAT(sum >> 7u, 8));
  505. /* Update the inputA and inputB pointers for next MAC calculation */
  506. px = ++pSrc1;
  507. py = pSrc2;
  508. /* Decrement the loop counter */
  509. blockSize3--;
  510. }
  511. #else
  512. /* Run the below code for Cortex-M0 */
  513. q7_t *pIn1 = pSrcA; /* input pointer */
  514. q7_t *pIn2 = pSrcB; /* coefficient pointer */
  515. q31_t sum; /* Accumulator */
  516. uint32_t i, j; /* loop counter */
  517. /* Loop to calculate output of convolution for output length number of times */
  518. for (i = 0; i < (srcALen + srcBLen - 1); i++)
  519. {
  520. /* Initialize sum with zero to carry on MAC operations */
  521. sum = 0;
  522. /* Loop to perform MAC operations according to convolution equation */
  523. for (j = 0; j <= i; j++)
  524. {
  525. /* Check the array limitations */
  526. if(((i - j) < srcBLen) && (j < srcALen))
  527. {
  528. /* z[i] += x[i-j] * y[j] */
  529. sum += (q15_t) pIn1[j] * (pIn2[i - j]);
  530. }
  531. }
  532. /* Store the output in the destination buffer */
  533. pDst[i] = (q7_t) __SSAT((sum >> 7u), 8u);
  534. }
  535. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  536. }
  537. /**
  538. * @} end of Conv group
  539. */