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_partial_q15.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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_partial_q15.c
  9. *
  10. * Description: Partial convolution 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 PartialConv
  46. * @{
  47. */
  48. /**
  49. * @brief Partial convolution 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.
  55. * @param[in] firstIndex is the first output sample to start with.
  56. * @param[in] numPoints is the number of output points to be computed.
  57. * @return Returns either ARM_MATH_SUCCESS if the function completed correctly or ARM_MATH_ARGUMENT_ERROR if the requested subset is not in the range [0 srcALen+srcBLen-2].
  58. *
  59. * Refer to <code>arm_conv_partial_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
  60. *
  61. * \par
  62. * Refer the function <code>arm_conv_partial_opt_q15()</code> for a faster implementation of this function using scratch buffers.
  63. *
  64. */
  65. arm_status arm_conv_partial_q15(
  66. q15_t * pSrcA,
  67. uint32_t srcALen,
  68. q15_t * pSrcB,
  69. uint32_t srcBLen,
  70. q15_t * pDst,
  71. uint32_t firstIndex,
  72. uint32_t numPoints)
  73. {
  74. #if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE)
  75. /* Run the below code for Cortex-M4 and Cortex-M3 */
  76. q15_t *pIn1; /* inputA pointer */
  77. q15_t *pIn2; /* inputB pointer */
  78. q15_t *pOut = pDst; /* output pointer */
  79. q63_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
  80. q15_t *px; /* Intermediate inputA pointer */
  81. q15_t *py; /* Intermediate inputB pointer */
  82. q15_t *pSrc1, *pSrc2; /* Intermediate pointers */
  83. q31_t x0, x1, x2, x3, c0; /* Temporary input variables */
  84. uint32_t j, k, count, check, blkCnt;
  85. int32_t blockSize1, blockSize2, blockSize3; /* loop counter */
  86. arm_status status; /* status of Partial convolution */
  87. /* Check for range of output samples to be calculated */
  88. if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
  89. {
  90. /* Set status as ARM_MATH_ARGUMENT_ERROR */
  91. status = ARM_MATH_ARGUMENT_ERROR;
  92. }
  93. else
  94. {
  95. /* The algorithm implementation is based on the lengths of the inputs. */
  96. /* srcB is always made to slide across srcA. */
  97. /* So srcBLen is always considered as shorter or equal to srcALen */
  98. if(srcALen >= srcBLen)
  99. {
  100. /* Initialization of inputA pointer */
  101. pIn1 = pSrcA;
  102. /* Initialization of inputB pointer */
  103. pIn2 = pSrcB;
  104. }
  105. else
  106. {
  107. /* Initialization of inputA pointer */
  108. pIn1 = pSrcB;
  109. /* Initialization of inputB pointer */
  110. pIn2 = pSrcA;
  111. /* srcBLen is always considered as shorter or equal to srcALen */
  112. j = srcBLen;
  113. srcBLen = srcALen;
  114. srcALen = j;
  115. }
  116. /* Conditions to check which loopCounter holds
  117. * the first and last indices of the output samples to be calculated. */
  118. check = firstIndex + numPoints;
  119. blockSize3 = ((int32_t) check - (int32_t) srcALen);
  120. blockSize3 = (blockSize3 > 0) ? blockSize3 : 0;
  121. blockSize1 = (((int32_t) srcBLen - 1) - (int32_t) firstIndex);
  122. blockSize1 = (blockSize1 > 0) ? ((check > (srcBLen - 1u)) ? blockSize1 :
  123. (int32_t) numPoints) : 0;
  124. blockSize2 = (int32_t) check - ((blockSize3 + blockSize1) +
  125. (int32_t) firstIndex);
  126. blockSize2 = (blockSize2 > 0) ? blockSize2 : 0;
  127. /* 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] */
  128. /* The function is internally
  129. * divided into three stages according to the number of multiplications that has to be
  130. * taken place between inputA samples and inputB samples. In the first stage of the
  131. * algorithm, the multiplications increase by one for every iteration.
  132. * In the second stage of the algorithm, srcBLen number of multiplications are done.
  133. * In the third stage of the algorithm, the multiplications decrease by one
  134. * for every iteration. */
  135. /* Set the output pointer to point to the firstIndex
  136. * of the output sample to be calculated. */
  137. pOut = pDst + firstIndex;
  138. /* --------------------------
  139. * Initializations of stage1
  140. * -------------------------*/
  141. /* sum = x[0] * y[0]
  142. * sum = x[0] * y[1] + x[1] * y[0]
  143. * ....
  144. * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
  145. */
  146. /* In this stage the MAC operations are increased by 1 for every iteration.
  147. The count variable holds the number of MAC operations performed.
  148. Since the partial convolution starts from firstIndex
  149. Number of Macs to be performed is firstIndex + 1 */
  150. count = 1u + firstIndex;
  151. /* Working pointer of inputA */
  152. px = pIn1;
  153. /* Working pointer of inputB */
  154. pSrc2 = pIn2 + firstIndex;
  155. py = pSrc2;
  156. /* ------------------------
  157. * Stage1 process
  158. * ----------------------*/
  159. /* For loop unrolling by 4, this stage is divided into two. */
  160. /* First part of this stage computes the MAC operations less than 4 */
  161. /* Second part of this stage computes the MAC operations greater than or equal to 4 */
  162. /* The first part of the stage starts here */
  163. while((count < 4u) && (blockSize1 > 0))
  164. {
  165. /* Accumulator is made zero for every iteration */
  166. sum = 0;
  167. /* Loop over number of MAC operations between
  168. * inputA samples and inputB samples */
  169. k = count;
  170. while(k > 0u)
  171. {
  172. /* Perform the multiply-accumulates */
  173. sum = __SMLALD(*px++, *py--, sum);
  174. /* Decrement the loop counter */
  175. k--;
  176. }
  177. /* Store the result in the accumulator in the destination buffer. */
  178. *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
  179. /* Update the inputA and inputB pointers for next MAC calculation */
  180. py = ++pSrc2;
  181. px = pIn1;
  182. /* Increment the MAC count */
  183. count++;
  184. /* Decrement the loop counter */
  185. blockSize1--;
  186. }
  187. /* The second part of the stage starts here */
  188. /* The internal loop, over count, is unrolled by 4 */
  189. /* To, read the last two inputB samples using SIMD:
  190. * y[srcBLen] and y[srcBLen-1] coefficients, py is decremented by 1 */
  191. py = py - 1;
  192. while(blockSize1 > 0)
  193. {
  194. /* Accumulator is made zero for every iteration */
  195. sum = 0;
  196. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  197. k = count >> 2u;
  198. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  199. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  200. while(k > 0u)
  201. {
  202. /* Perform the multiply-accumulates */
  203. /* x[0], x[1] are multiplied with y[srcBLen - 1], y[srcBLen - 2] respectively */
  204. sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
  205. /* x[2], x[3] are multiplied with y[srcBLen - 3], y[srcBLen - 4] respectively */
  206. sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
  207. /* Decrement the loop counter */
  208. k--;
  209. }
  210. /* For the next MAC operations, the pointer py is used without SIMD
  211. * So, py is incremented by 1 */
  212. py = py + 1u;
  213. /* If the count is not a multiple of 4, compute any remaining MACs here.
  214. ** No loop unrolling is used. */
  215. k = count % 0x4u;
  216. while(k > 0u)
  217. {
  218. /* Perform the multiply-accumulates */
  219. sum = __SMLALD(*px++, *py--, sum);
  220. /* Decrement the loop counter */
  221. k--;
  222. }
  223. /* Store the result in the accumulator in the destination buffer. */
  224. *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
  225. /* Update the inputA and inputB pointers for next MAC calculation */
  226. py = ++pSrc2 - 1u;
  227. px = pIn1;
  228. /* Increment the MAC count */
  229. count++;
  230. /* Decrement the loop counter */
  231. blockSize1--;
  232. }
  233. /* --------------------------
  234. * Initializations of stage2
  235. * ------------------------*/
  236. /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
  237. * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
  238. * ....
  239. * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
  240. */
  241. /* Working pointer of inputA */
  242. px = pIn1;
  243. /* Working pointer of inputB */
  244. pSrc2 = pIn2 + (srcBLen - 1u);
  245. py = pSrc2;
  246. /* count is the index by which the pointer pIn1 to be incremented */
  247. count = 0u;
  248. /* --------------------
  249. * Stage2 process
  250. * -------------------*/
  251. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  252. * So, to loop unroll over blockSize2,
  253. * srcBLen should be greater than or equal to 4 */
  254. if(srcBLen >= 4u)
  255. {
  256. /* Loop unroll over blockSize2, by 4 */
  257. blkCnt = blockSize2 >> 2u;
  258. while(blkCnt > 0u)
  259. {
  260. py = py - 1u;
  261. /* Set all accumulators to zero */
  262. acc0 = 0;
  263. acc1 = 0;
  264. acc2 = 0;
  265. acc3 = 0;
  266. /* read x[0], x[1] samples */
  267. x0 = *__SIMD32(px);
  268. /* read x[1], x[2] samples */
  269. x1 = _SIMD32_OFFSET(px+1);
  270. px+= 2u;
  271. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  272. k = srcBLen >> 2u;
  273. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  274. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  275. do
  276. {
  277. /* Read the last two inputB samples using SIMD:
  278. * y[srcBLen - 1] and y[srcBLen - 2] */
  279. c0 = *__SIMD32(py)--;
  280. /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
  281. acc0 = __SMLALDX(x0, c0, acc0);
  282. /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
  283. acc1 = __SMLALDX(x1, c0, acc1);
  284. /* Read x[2], x[3] */
  285. x2 = *__SIMD32(px);
  286. /* Read x[3], x[4] */
  287. x3 = _SIMD32_OFFSET(px+1);
  288. /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
  289. acc2 = __SMLALDX(x2, c0, acc2);
  290. /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
  291. acc3 = __SMLALDX(x3, c0, acc3);
  292. /* Read y[srcBLen - 3] and y[srcBLen - 4] */
  293. c0 = *__SIMD32(py)--;
  294. /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
  295. acc0 = __SMLALDX(x2, c0, acc0);
  296. /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
  297. acc1 = __SMLALDX(x3, c0, acc1);
  298. /* Read x[4], x[5] */
  299. x0 = _SIMD32_OFFSET(px+2);
  300. /* Read x[5], x[6] */
  301. x1 = _SIMD32_OFFSET(px+3);
  302. px += 4u;
  303. /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
  304. acc2 = __SMLALDX(x0, c0, acc2);
  305. /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
  306. acc3 = __SMLALDX(x1, c0, acc3);
  307. } while(--k);
  308. /* For the next MAC operations, SIMD is not used
  309. * So, the 16 bit pointer if inputB, py is updated */
  310. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  311. ** No loop unrolling is used. */
  312. k = srcBLen % 0x4u;
  313. if(k == 1u)
  314. {
  315. /* Read y[srcBLen - 5] */
  316. c0 = *(py+1);
  317. #ifdef ARM_MATH_BIG_ENDIAN
  318. c0 = c0 << 16u;
  319. #else
  320. c0 = c0 & 0x0000FFFF;
  321. #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
  322. /* Read x[7] */
  323. x3 = *__SIMD32(px);
  324. px++;
  325. /* Perform the multiply-accumulates */
  326. acc0 = __SMLALD(x0, c0, acc0);
  327. acc1 = __SMLALD(x1, c0, acc1);
  328. acc2 = __SMLALDX(x1, c0, acc2);
  329. acc3 = __SMLALDX(x3, c0, acc3);
  330. }
  331. if(k == 2u)
  332. {
  333. /* Read y[srcBLen - 5], y[srcBLen - 6] */
  334. c0 = _SIMD32_OFFSET(py);
  335. /* Read x[7], x[8] */
  336. x3 = *__SIMD32(px);
  337. /* Read x[9] */
  338. x2 = _SIMD32_OFFSET(px+1);
  339. px += 2u;
  340. /* Perform the multiply-accumulates */
  341. acc0 = __SMLALDX(x0, c0, acc0);
  342. acc1 = __SMLALDX(x1, c0, acc1);
  343. acc2 = __SMLALDX(x3, c0, acc2);
  344. acc3 = __SMLALDX(x2, c0, acc3);
  345. }
  346. if(k == 3u)
  347. {
  348. /* Read y[srcBLen - 5], y[srcBLen - 6] */
  349. c0 = _SIMD32_OFFSET(py);
  350. /* Read x[7], x[8] */
  351. x3 = *__SIMD32(px);
  352. /* Read x[9] */
  353. x2 = _SIMD32_OFFSET(px+1);
  354. /* Perform the multiply-accumulates */
  355. acc0 = __SMLALDX(x0, c0, acc0);
  356. acc1 = __SMLALDX(x1, c0, acc1);
  357. acc2 = __SMLALDX(x3, c0, acc2);
  358. acc3 = __SMLALDX(x2, c0, acc3);
  359. c0 = *(py-1);
  360. #ifdef ARM_MATH_BIG_ENDIAN
  361. c0 = c0 << 16u;
  362. #else
  363. c0 = c0 & 0x0000FFFF;
  364. #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
  365. /* Read x[10] */
  366. x3 = _SIMD32_OFFSET(px+2);
  367. px += 3u;
  368. /* Perform the multiply-accumulates */
  369. acc0 = __SMLALDX(x1, c0, acc0);
  370. acc1 = __SMLALD(x2, c0, acc1);
  371. acc2 = __SMLALDX(x2, c0, acc2);
  372. acc3 = __SMLALDX(x3, c0, acc3);
  373. }
  374. /* Store the results in the accumulators in the destination buffer. */
  375. #ifndef ARM_MATH_BIG_ENDIAN
  376. *__SIMD32(pOut)++ =
  377. __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
  378. *__SIMD32(pOut)++ =
  379. __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
  380. #else
  381. *__SIMD32(pOut)++ =
  382. __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
  383. *__SIMD32(pOut)++ =
  384. __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
  385. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  386. /* Increment the pointer pIn1 index, count by 4 */
  387. count += 4u;
  388. /* Update the inputA and inputB pointers for next MAC calculation */
  389. px = pIn1 + count;
  390. py = pSrc2;
  391. /* Decrement the loop counter */
  392. blkCnt--;
  393. }
  394. /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
  395. ** No loop unrolling is used. */
  396. blkCnt = (uint32_t) blockSize2 % 0x4u;
  397. while(blkCnt > 0u)
  398. {
  399. /* Accumulator is made zero for every iteration */
  400. sum = 0;
  401. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  402. k = srcBLen >> 2u;
  403. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  404. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  405. while(k > 0u)
  406. {
  407. /* Perform the multiply-accumulates */
  408. sum += (q63_t) ((q31_t) * px++ * *py--);
  409. sum += (q63_t) ((q31_t) * px++ * *py--);
  410. sum += (q63_t) ((q31_t) * px++ * *py--);
  411. sum += (q63_t) ((q31_t) * px++ * *py--);
  412. /* Decrement the loop counter */
  413. k--;
  414. }
  415. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  416. ** No loop unrolling is used. */
  417. k = srcBLen % 0x4u;
  418. while(k > 0u)
  419. {
  420. /* Perform the multiply-accumulates */
  421. sum += (q63_t) ((q31_t) * px++ * *py--);
  422. /* Decrement the loop counter */
  423. k--;
  424. }
  425. /* Store the result in the accumulator in the destination buffer. */
  426. *pOut++ = (q15_t) (__SSAT(sum >> 15, 16));
  427. /* Increment the pointer pIn1 index, count by 1 */
  428. count++;
  429. /* Update the inputA and inputB pointers for next MAC calculation */
  430. px = pIn1 + count;
  431. py = pSrc2;
  432. /* Decrement the loop counter */
  433. blkCnt--;
  434. }
  435. }
  436. else
  437. {
  438. /* If the srcBLen is not a multiple of 4,
  439. * the blockSize2 loop cannot be unrolled by 4 */
  440. blkCnt = (uint32_t) blockSize2;
  441. while(blkCnt > 0u)
  442. {
  443. /* Accumulator is made zero for every iteration */
  444. sum = 0;
  445. /* srcBLen number of MACS should be performed */
  446. k = srcBLen;
  447. while(k > 0u)
  448. {
  449. /* Perform the multiply-accumulate */
  450. sum += (q63_t) ((q31_t) * px++ * *py--);
  451. /* Decrement the loop counter */
  452. k--;
  453. }
  454. /* Store the result in the accumulator in the destination buffer. */
  455. *pOut++ = (q15_t) (__SSAT(sum >> 15, 16));
  456. /* Increment the MAC count */
  457. count++;
  458. /* Update the inputA and inputB pointers for next MAC calculation */
  459. px = pIn1 + count;
  460. py = pSrc2;
  461. /* Decrement the loop counter */
  462. blkCnt--;
  463. }
  464. }
  465. /* --------------------------
  466. * Initializations of stage3
  467. * -------------------------*/
  468. /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
  469. * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
  470. * ....
  471. * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
  472. * sum += x[srcALen-1] * y[srcBLen-1]
  473. */
  474. /* In this stage the MAC operations are decreased by 1 for every iteration.
  475. The count variable holds the number of MAC operations performed */
  476. count = srcBLen - 1u;
  477. /* Working pointer of inputA */
  478. pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
  479. px = pSrc1;
  480. /* Working pointer of inputB */
  481. pSrc2 = pIn2 + (srcBLen - 1u);
  482. pIn2 = pSrc2 - 1u;
  483. py = pIn2;
  484. /* -------------------
  485. * Stage3 process
  486. * ------------------*/
  487. /* For loop unrolling by 4, this stage is divided into two. */
  488. /* First part of this stage computes the MAC operations greater than 4 */
  489. /* Second part of this stage computes the MAC operations less than or equal to 4 */
  490. /* The first part of the stage starts here */
  491. j = count >> 2u;
  492. while((j > 0u) && (blockSize3 > 0))
  493. {
  494. /* Accumulator is made zero for every iteration */
  495. sum = 0;
  496. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  497. k = count >> 2u;
  498. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  499. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  500. while(k > 0u)
  501. {
  502. /* x[srcALen - srcBLen + 1], x[srcALen - srcBLen + 2] are multiplied
  503. * with y[srcBLen - 1], y[srcBLen - 2] respectively */
  504. sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
  505. /* x[srcALen - srcBLen + 3], x[srcALen - srcBLen + 4] are multiplied
  506. * with y[srcBLen - 3], y[srcBLen - 4] respectively */
  507. sum = __SMLALDX(*__SIMD32(px)++, *__SIMD32(py)--, sum);
  508. /* Decrement the loop counter */
  509. k--;
  510. }
  511. /* For the next MAC operations, the pointer py is used without SIMD
  512. * So, py is incremented by 1 */
  513. py = py + 1u;
  514. /* If the count is not a multiple of 4, compute any remaining MACs here.
  515. ** No loop unrolling is used. */
  516. k = count % 0x4u;
  517. while(k > 0u)
  518. {
  519. /* sum += x[srcALen - srcBLen + 5] * y[srcBLen - 5] */
  520. sum = __SMLALD(*px++, *py--, sum);
  521. /* Decrement the loop counter */
  522. k--;
  523. }
  524. /* Store the result in the accumulator in the destination buffer. */
  525. *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
  526. /* Update the inputA and inputB pointers for next MAC calculation */
  527. px = ++pSrc1;
  528. py = pIn2;
  529. /* Decrement the MAC count */
  530. count--;
  531. /* Decrement the loop counter */
  532. blockSize3--;
  533. j--;
  534. }
  535. /* The second part of the stage starts here */
  536. /* SIMD is not used for the next MAC operations,
  537. * so pointer py is updated to read only one sample at a time */
  538. py = py + 1u;
  539. while(blockSize3 > 0)
  540. {
  541. /* Accumulator is made zero for every iteration */
  542. sum = 0;
  543. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  544. k = count;
  545. while(k > 0u)
  546. {
  547. /* Perform the multiply-accumulates */
  548. /* sum += x[srcALen-1] * y[srcBLen-1] */
  549. sum = __SMLALD(*px++, *py--, sum);
  550. /* Decrement the loop counter */
  551. k--;
  552. }
  553. /* Store the result in the accumulator in the destination buffer. */
  554. *pOut++ = (q15_t) (__SSAT((sum >> 15), 16));
  555. /* Update the inputA and inputB pointers for next MAC calculation */
  556. px = ++pSrc1;
  557. py = pSrc2;
  558. /* Decrement the MAC count */
  559. count--;
  560. /* Decrement the loop counter */
  561. blockSize3--;
  562. }
  563. /* set status as ARM_MATH_SUCCESS */
  564. status = ARM_MATH_SUCCESS;
  565. }
  566. /* Return to application */
  567. return (status);
  568. #else
  569. /* Run the below code for Cortex-M0 */
  570. q15_t *pIn1 = pSrcA; /* inputA pointer */
  571. q15_t *pIn2 = pSrcB; /* inputB pointer */
  572. q63_t sum; /* Accumulator */
  573. uint32_t i, j; /* loop counters */
  574. arm_status status; /* status of Partial convolution */
  575. /* Check for range of output samples to be calculated */
  576. if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
  577. {
  578. /* Set status as ARM_ARGUMENT_ERROR */
  579. status = ARM_MATH_ARGUMENT_ERROR;
  580. }
  581. else
  582. {
  583. /* Loop to calculate convolution for output length number of values */
  584. for (i = firstIndex; i <= (firstIndex + numPoints - 1); i++)
  585. {
  586. /* Initialize sum with zero to carry on MAC operations */
  587. sum = 0;
  588. /* Loop to perform MAC operations according to convolution equation */
  589. for (j = 0; j <= i; j++)
  590. {
  591. /* Check the array limitations */
  592. if(((i - j) < srcBLen) && (j < srcALen))
  593. {
  594. /* z[i] += x[i-j] * y[j] */
  595. sum += ((q31_t) pIn1[j] * (pIn2[i - j]));
  596. }
  597. }
  598. /* Store the output in the destination buffer */
  599. pDst[i] = (q15_t) __SSAT((sum >> 15u), 16u);
  600. }
  601. /* set status as ARM_SUCCESS as there are no argument errors */
  602. status = ARM_MATH_SUCCESS;
  603. }
  604. return (status);
  605. #endif /* #if (defined(ARM_MATH_CM4) || defined(ARM_MATH_CM3)) && !defined(UNALIGNED_SUPPORT_DISABLE) */
  606. }
  607. /**
  608. * @} end of PartialConv group
  609. */