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_q7.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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_q7.c
  9. *
  10. * Description: Partial 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 PartialConv
  46. * @{
  47. */
  48. /**
  49. * @brief Partial 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.
  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. * \par
  60. * Refer the function <code>arm_conv_partial_opt_q7()</code> for a faster implementation of this function.
  61. *
  62. */
  63. arm_status arm_conv_partial_q7(
  64. q7_t * pSrcA,
  65. uint32_t srcALen,
  66. q7_t * pSrcB,
  67. uint32_t srcBLen,
  68. q7_t * pDst,
  69. uint32_t firstIndex,
  70. uint32_t numPoints)
  71. {
  72. #ifndef ARM_MATH_CM0_FAMILY
  73. /* Run the below code for Cortex-M4 and Cortex-M3 */
  74. q7_t *pIn1; /* inputA pointer */
  75. q7_t *pIn2; /* inputB pointer */
  76. q7_t *pOut = pDst; /* output pointer */
  77. q7_t *px; /* Intermediate inputA pointer */
  78. q7_t *py; /* Intermediate inputB pointer */
  79. q7_t *pSrc1, *pSrc2; /* Intermediate pointers */
  80. q31_t sum, acc0, acc1, acc2, acc3; /* Accumulator */
  81. q31_t input1, input2;
  82. q15_t in1, in2;
  83. q7_t x0, x1, x2, x3, c0, c1;
  84. uint32_t j, k, count, check, blkCnt;
  85. int32_t blockSize1, blockSize2, blockSize3; /* loop counter */
  86. arm_status status;
  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 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. /* The first stage starts here */
  160. while(blockSize1 > 0)
  161. {
  162. /* Accumulator is made zero for every iteration */
  163. sum = 0;
  164. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  165. k = count >> 2u;
  166. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  167. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  168. while(k > 0u)
  169. {
  170. /* x[0] , x[1] */
  171. in1 = (q15_t) * px++;
  172. in2 = (q15_t) * px++;
  173. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  174. /* y[srcBLen - 1] , y[srcBLen - 2] */
  175. in1 = (q15_t) * py--;
  176. in2 = (q15_t) * py--;
  177. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  178. /* x[0] * y[srcBLen - 1] */
  179. /* x[1] * y[srcBLen - 2] */
  180. sum = __SMLAD(input1, input2, sum);
  181. /* x[2] , x[3] */
  182. in1 = (q15_t) * px++;
  183. in2 = (q15_t) * px++;
  184. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  185. /* y[srcBLen - 3] , y[srcBLen - 4] */
  186. in1 = (q15_t) * py--;
  187. in2 = (q15_t) * py--;
  188. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  189. /* x[2] * y[srcBLen - 3] */
  190. /* x[3] * y[srcBLen - 4] */
  191. sum = __SMLAD(input1, input2, sum);
  192. /* Decrement the loop counter */
  193. k--;
  194. }
  195. /* If the count is not a multiple of 4, compute any remaining MACs here.
  196. ** No loop unrolling is used. */
  197. k = count % 0x4u;
  198. while(k > 0u)
  199. {
  200. /* Perform the multiply-accumulates */
  201. sum += ((q31_t) * px++ * *py--);
  202. /* Decrement the loop counter */
  203. k--;
  204. }
  205. /* Store the result in the accumulator in the destination buffer. */
  206. *pOut++ = (q7_t) (__SSAT(sum >> 7, 8));
  207. /* Update the inputA and inputB pointers for next MAC calculation */
  208. py = ++pSrc2;
  209. px = pIn1;
  210. /* Increment the MAC count */
  211. count++;
  212. /* Decrement the loop counter */
  213. blockSize1--;
  214. }
  215. /* --------------------------
  216. * Initializations of stage2
  217. * ------------------------*/
  218. /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
  219. * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
  220. * ....
  221. * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
  222. */
  223. /* Working pointer of inputA */
  224. px = pIn1;
  225. /* Working pointer of inputB */
  226. pSrc2 = pIn2 + (srcBLen - 1u);
  227. py = pSrc2;
  228. /* count is index by which the pointer pIn1 to be incremented */
  229. count = 0u;
  230. /* -------------------
  231. * Stage2 process
  232. * ------------------*/
  233. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  234. * So, to loop unroll over blockSize2,
  235. * srcBLen should be greater than or equal to 4 */
  236. if(srcBLen >= 4u)
  237. {
  238. /* Loop unroll over blockSize2, by 4 */
  239. blkCnt = ((uint32_t) blockSize2 >> 2u);
  240. while(blkCnt > 0u)
  241. {
  242. /* Set all accumulators to zero */
  243. acc0 = 0;
  244. acc1 = 0;
  245. acc2 = 0;
  246. acc3 = 0;
  247. /* read x[0], x[1], x[2] samples */
  248. x0 = *(px++);
  249. x1 = *(px++);
  250. x2 = *(px++);
  251. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  252. k = srcBLen >> 2u;
  253. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  254. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  255. do
  256. {
  257. /* Read y[srcBLen - 1] sample */
  258. c0 = *(py--);
  259. /* Read y[srcBLen - 2] sample */
  260. c1 = *(py--);
  261. /* Read x[3] sample */
  262. x3 = *(px++);
  263. /* x[0] and x[1] are packed */
  264. in1 = (q15_t) x0;
  265. in2 = (q15_t) x1;
  266. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  267. /* y[srcBLen - 1] and y[srcBLen - 2] are packed */
  268. in1 = (q15_t) c0;
  269. in2 = (q15_t) c1;
  270. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  271. /* acc0 += x[0] * y[srcBLen - 1] + x[1] * y[srcBLen - 2] */
  272. acc0 = __SMLAD(input1, input2, acc0);
  273. /* x[1] and x[2] are packed */
  274. in1 = (q15_t) x1;
  275. in2 = (q15_t) x2;
  276. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  277. /* acc1 += x[1] * y[srcBLen - 1] + x[2] * y[srcBLen - 2] */
  278. acc1 = __SMLAD(input1, input2, acc1);
  279. /* x[2] and x[3] are packed */
  280. in1 = (q15_t) x2;
  281. in2 = (q15_t) x3;
  282. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  283. /* acc2 += x[2] * y[srcBLen - 1] + x[3] * y[srcBLen - 2] */
  284. acc2 = __SMLAD(input1, input2, acc2);
  285. /* Read x[4] sample */
  286. x0 = *(px++);
  287. /* x[3] and x[4] are packed */
  288. in1 = (q15_t) x3;
  289. in2 = (q15_t) x0;
  290. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  291. /* acc3 += x[3] * y[srcBLen - 1] + x[4] * y[srcBLen - 2] */
  292. acc3 = __SMLAD(input1, input2, acc3);
  293. /* Read y[srcBLen - 3] sample */
  294. c0 = *(py--);
  295. /* Read y[srcBLen - 4] sample */
  296. c1 = *(py--);
  297. /* Read x[5] sample */
  298. x1 = *(px++);
  299. /* x[2] and x[3] are packed */
  300. in1 = (q15_t) x2;
  301. in2 = (q15_t) x3;
  302. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  303. /* y[srcBLen - 3] and y[srcBLen - 4] are packed */
  304. in1 = (q15_t) c0;
  305. in2 = (q15_t) c1;
  306. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  307. /* acc0 += x[2] * y[srcBLen - 3] + x[3] * y[srcBLen - 4] */
  308. acc0 = __SMLAD(input1, input2, acc0);
  309. /* x[3] and x[4] are packed */
  310. in1 = (q15_t) x3;
  311. in2 = (q15_t) x0;
  312. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  313. /* acc1 += x[3] * y[srcBLen - 3] + x[4] * y[srcBLen - 4] */
  314. acc1 = __SMLAD(input1, input2, acc1);
  315. /* x[4] and x[5] are packed */
  316. in1 = (q15_t) x0;
  317. in2 = (q15_t) x1;
  318. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  319. /* acc2 += x[4] * y[srcBLen - 3] + x[5] * y[srcBLen - 4] */
  320. acc2 = __SMLAD(input1, input2, acc2);
  321. /* Read x[6] sample */
  322. x2 = *(px++);
  323. /* x[5] and x[6] are packed */
  324. in1 = (q15_t) x1;
  325. in2 = (q15_t) x2;
  326. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  327. /* acc3 += x[5] * y[srcBLen - 3] + x[6] * y[srcBLen - 4] */
  328. acc3 = __SMLAD(input1, input2, acc3);
  329. } while(--k);
  330. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  331. ** No loop unrolling is used. */
  332. k = srcBLen % 0x4u;
  333. while(k > 0u)
  334. {
  335. /* Read y[srcBLen - 5] sample */
  336. c0 = *(py--);
  337. /* Read x[7] sample */
  338. x3 = *(px++);
  339. /* Perform the multiply-accumulates */
  340. /* acc0 += x[4] * y[srcBLen - 5] */
  341. acc0 += ((q31_t) x0 * c0);
  342. /* acc1 += x[5] * y[srcBLen - 5] */
  343. acc1 += ((q31_t) x1 * c0);
  344. /* acc2 += x[6] * y[srcBLen - 5] */
  345. acc2 += ((q31_t) x2 * c0);
  346. /* acc3 += x[7] * y[srcBLen - 5] */
  347. acc3 += ((q31_t) x3 * c0);
  348. /* Reuse the present samples for the next MAC */
  349. x0 = x1;
  350. x1 = x2;
  351. x2 = x3;
  352. /* Decrement the loop counter */
  353. k--;
  354. }
  355. /* Store the result in the accumulator in the destination buffer. */
  356. *pOut++ = (q7_t) (__SSAT(acc0 >> 7, 8));
  357. *pOut++ = (q7_t) (__SSAT(acc1 >> 7, 8));
  358. *pOut++ = (q7_t) (__SSAT(acc2 >> 7, 8));
  359. *pOut++ = (q7_t) (__SSAT(acc3 >> 7, 8));
  360. /* Increment the pointer pIn1 index, count by 4 */
  361. count += 4u;
  362. /* Update the inputA and inputB pointers for next MAC calculation */
  363. px = pIn1 + count;
  364. py = pSrc2;
  365. /* Decrement the loop counter */
  366. blkCnt--;
  367. }
  368. /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here.
  369. ** No loop unrolling is used. */
  370. blkCnt = (uint32_t) blockSize2 % 0x4u;
  371. while(blkCnt > 0u)
  372. {
  373. /* Accumulator is made zero for every iteration */
  374. sum = 0;
  375. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  376. k = srcBLen >> 2u;
  377. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  378. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  379. while(k > 0u)
  380. {
  381. /* Reading two inputs of SrcA buffer and packing */
  382. in1 = (q15_t) * px++;
  383. in2 = (q15_t) * px++;
  384. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  385. /* Reading two inputs of SrcB buffer and packing */
  386. in1 = (q15_t) * py--;
  387. in2 = (q15_t) * py--;
  388. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  389. /* Perform the multiply-accumulates */
  390. sum = __SMLAD(input1, input2, sum);
  391. /* Reading two inputs of SrcA buffer and packing */
  392. in1 = (q15_t) * px++;
  393. in2 = (q15_t) * px++;
  394. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  395. /* Reading two inputs of SrcB buffer and packing */
  396. in1 = (q15_t) * py--;
  397. in2 = (q15_t) * py--;
  398. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  399. /* Perform the multiply-accumulates */
  400. sum = __SMLAD(input1, input2, sum);
  401. /* Decrement the loop counter */
  402. k--;
  403. }
  404. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  405. ** No loop unrolling is used. */
  406. k = srcBLen % 0x4u;
  407. while(k > 0u)
  408. {
  409. /* Perform the multiply-accumulates */
  410. sum += ((q31_t) * px++ * *py--);
  411. /* Decrement the loop counter */
  412. k--;
  413. }
  414. /* Store the result in the accumulator in the destination buffer. */
  415. *pOut++ = (q7_t) (__SSAT(sum >> 7, 8));
  416. /* Increment the pointer pIn1 index, count by 1 */
  417. count++;
  418. /* Update the inputA and inputB pointers for next MAC calculation */
  419. px = pIn1 + count;
  420. py = pSrc2;
  421. /* Decrement the loop counter */
  422. blkCnt--;
  423. }
  424. }
  425. else
  426. {
  427. /* If the srcBLen is not a multiple of 4,
  428. * the blockSize2 loop cannot be unrolled by 4 */
  429. blkCnt = (uint32_t) blockSize2;
  430. while(blkCnt > 0u)
  431. {
  432. /* Accumulator is made zero for every iteration */
  433. sum = 0;
  434. /* srcBLen number of MACS should be performed */
  435. k = srcBLen;
  436. while(k > 0u)
  437. {
  438. /* Perform the multiply-accumulate */
  439. sum += ((q31_t) * px++ * *py--);
  440. /* Decrement the loop counter */
  441. k--;
  442. }
  443. /* Store the result in the accumulator in the destination buffer. */
  444. *pOut++ = (q7_t) (__SSAT(sum >> 7, 8));
  445. /* Increment the MAC count */
  446. count++;
  447. /* Update the inputA and inputB pointers for next MAC calculation */
  448. px = pIn1 + count;
  449. py = pSrc2;
  450. /* Decrement the loop counter */
  451. blkCnt--;
  452. }
  453. }
  454. /* --------------------------
  455. * Initializations of stage3
  456. * -------------------------*/
  457. /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
  458. * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
  459. * ....
  460. * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
  461. * sum += x[srcALen-1] * y[srcBLen-1]
  462. */
  463. /* In this stage the MAC operations are decreased by 1 for every iteration.
  464. The count variable holds the number of MAC operations performed */
  465. count = srcBLen - 1u;
  466. /* Working pointer of inputA */
  467. pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
  468. px = pSrc1;
  469. /* Working pointer of inputB */
  470. pSrc2 = pIn2 + (srcBLen - 1u);
  471. py = pSrc2;
  472. /* -------------------
  473. * Stage3 process
  474. * ------------------*/
  475. while(blockSize3 > 0)
  476. {
  477. /* Accumulator is made zero for every iteration */
  478. sum = 0;
  479. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  480. k = count >> 2u;
  481. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  482. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  483. while(k > 0u)
  484. {
  485. /* Reading two inputs, x[srcALen - srcBLen + 1] and x[srcALen - srcBLen + 2] of SrcA buffer and packing */
  486. in1 = (q15_t) * px++;
  487. in2 = (q15_t) * px++;
  488. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  489. /* Reading two inputs, y[srcBLen - 1] and y[srcBLen - 2] of SrcB buffer and packing */
  490. in1 = (q15_t) * py--;
  491. in2 = (q15_t) * py--;
  492. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  493. /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
  494. /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
  495. sum = __SMLAD(input1, input2, sum);
  496. /* Reading two inputs, x[srcALen - srcBLen + 3] and x[srcALen - srcBLen + 4] of SrcA buffer and packing */
  497. in1 = (q15_t) * px++;
  498. in2 = (q15_t) * px++;
  499. input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  500. /* Reading two inputs, y[srcBLen - 3] and y[srcBLen - 4] of SrcB buffer and packing */
  501. in1 = (q15_t) * py--;
  502. in2 = (q15_t) * py--;
  503. input2 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
  504. /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
  505. /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
  506. sum = __SMLAD(input1, input2, sum);
  507. /* Decrement the loop counter */
  508. k--;
  509. }
  510. /* If the count is not a multiple of 4, compute any remaining MACs here.
  511. ** No loop unrolling is used. */
  512. k = count % 0x4u;
  513. while(k > 0u)
  514. {
  515. /* Perform the multiply-accumulates */
  516. /* sum += x[srcALen-1] * y[srcBLen-1] */
  517. sum += ((q31_t) * px++ * *py--);
  518. /* Decrement the loop counter */
  519. k--;
  520. }
  521. /* Store the result in the accumulator in the destination buffer. */
  522. *pOut++ = (q7_t) (__SSAT(sum >> 7, 8));
  523. /* Update the inputA and inputB pointers for next MAC calculation */
  524. px = ++pSrc1;
  525. py = pSrc2;
  526. /* Decrement the MAC count */
  527. count--;
  528. /* Decrement the loop counter */
  529. blockSize3--;
  530. }
  531. /* set status as ARM_MATH_SUCCESS */
  532. status = ARM_MATH_SUCCESS;
  533. }
  534. /* Return to application */
  535. return (status);
  536. #else
  537. /* Run the below code for Cortex-M0 */
  538. q7_t *pIn1 = pSrcA; /* inputA pointer */
  539. q7_t *pIn2 = pSrcB; /* inputB pointer */
  540. q31_t sum; /* Accumulator */
  541. uint32_t i, j; /* loop counters */
  542. arm_status status; /* status of Partial convolution */
  543. /* Check for range of output samples to be calculated */
  544. if((firstIndex + numPoints) > ((srcALen + (srcBLen - 1u))))
  545. {
  546. /* Set status as ARM_ARGUMENT_ERROR */
  547. status = ARM_MATH_ARGUMENT_ERROR;
  548. }
  549. else
  550. {
  551. /* Loop to calculate convolution for output length number of values */
  552. for (i = firstIndex; i <= (firstIndex + numPoints - 1); i++)
  553. {
  554. /* Initialize sum with zero to carry on MAC operations */
  555. sum = 0;
  556. /* Loop to perform MAC operations according to convolution equation */
  557. for (j = 0; j <= i; j++)
  558. {
  559. /* Check the array limitations */
  560. if(((i - j) < srcBLen) && (j < srcALen))
  561. {
  562. /* z[i] += x[i-j] * y[j] */
  563. sum += ((q15_t) pIn1[j] * (pIn2[i - j]));
  564. }
  565. }
  566. /* Store the output in the destination buffer */
  567. pDst[i] = (q7_t) __SSAT((sum >> 7u), 8u);
  568. }
  569. /* set status as ARM_SUCCESS as there are no argument errors */
  570. status = ARM_MATH_SUCCESS;
  571. }
  572. return (status);
  573. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  574. }
  575. /**
  576. * @} end of PartialConv group
  577. */