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_q31.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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_q31.c
  9. *
  10. * Description: Convolution of Q31 sequences.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupFilters
  43. */
  44. /**
  45. * @addtogroup Conv
  46. * @{
  47. */
  48. /**
  49. * @brief Convolution of Q31 sequences.
  50. * @param[in] *pSrcA points to the first input sequence.
  51. * @param[in] srcALen length of the first input sequence.
  52. * @param[in] *pSrcB points to the second input sequence.
  53. * @param[in] srcBLen length of the second input sequence.
  54. * @param[out] *pDst points to the location where the output result is written. Length srcALen+srcBLen-1.
  55. * @return none.
  56. *
  57. * @details
  58. * <b>Scaling and Overflow Behavior:</b>
  59. *
  60. * \par
  61. * The function is implemented using an internal 64-bit accumulator.
  62. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  63. * There is no saturation on intermediate additions.
  64. * Thus, if the accumulator overflows it wraps around and distorts the result.
  65. * The input signals should be scaled down to avoid intermediate overflows.
  66. * Scale down the inputs by log2(min(srcALen, srcBLen)) (log2 is read as log to the base 2) times to avoid overflows,
  67. * as maximum of min(srcALen, srcBLen) number of additions are carried internally.
  68. * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  69. *
  70. * \par
  71. * See <code>arm_conv_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  72. */
  73. void arm_conv_q31(
  74. q31_t * pSrcA,
  75. uint32_t srcALen,
  76. q31_t * pSrcB,
  77. uint32_t srcBLen,
  78. q31_t * pDst)
  79. {
  80. #ifndef ARM_MATH_CM0_FAMILY
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. q31_t *pIn1; /* inputA pointer */
  83. q31_t *pIn2; /* inputB pointer */
  84. q31_t *pOut = pDst; /* output pointer */
  85. q31_t *px; /* Intermediate inputA pointer */
  86. q31_t *py; /* Intermediate inputB pointer */
  87. q31_t *pSrc1, *pSrc2; /* Intermediate pointers */
  88. q63_t sum; /* Accumulator */
  89. q63_t acc0, acc1, acc2; /* Accumulator */
  90. q31_t x0, x1, x2, c0; /* Temporary variables to hold state and coefficient values */
  91. uint32_t j, k, count, blkCnt, blockSize1, blockSize2, blockSize3; /* loop counter */
  92. /* The algorithm implementation is based on the lengths of the inputs. */
  93. /* srcB is always made to slide across srcA. */
  94. /* So srcBLen is always considered as shorter or equal to srcALen */
  95. if(srcALen >= srcBLen)
  96. {
  97. /* Initialization of inputA pointer */
  98. pIn1 = pSrcA;
  99. /* Initialization of inputB pointer */
  100. pIn2 = pSrcB;
  101. }
  102. else
  103. {
  104. /* Initialization of inputA pointer */
  105. pIn1 = (q31_t *) pSrcB;
  106. /* Initialization of inputB pointer */
  107. pIn2 = (q31_t *) pSrcA;
  108. /* srcBLen is always considered as shorter or equal to srcALen */
  109. j = srcBLen;
  110. srcBLen = srcALen;
  111. srcALen = j;
  112. }
  113. /* 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] */
  114. /* The function is internally
  115. * divided into three stages according to the number of multiplications that has to be
  116. * taken place between inputA samples and inputB samples. In the first stage of the
  117. * algorithm, the multiplications increase by one for every iteration.
  118. * In the second stage of the algorithm, srcBLen number of multiplications are done.
  119. * In the third stage of the algorithm, the multiplications decrease by one
  120. * for every iteration. */
  121. /* The algorithm is implemented in three stages.
  122. The loop counters of each stage is initiated here. */
  123. blockSize1 = srcBLen - 1u;
  124. blockSize2 = srcALen - (srcBLen - 1u);
  125. blockSize3 = blockSize1;
  126. /* --------------------------
  127. * Initializations of stage1
  128. * -------------------------*/
  129. /* sum = x[0] * y[0]
  130. * sum = x[0] * y[1] + x[1] * y[0]
  131. * ....
  132. * sum = x[0] * y[srcBlen - 1] + x[1] * y[srcBlen - 2] +...+ x[srcBLen - 1] * y[0]
  133. */
  134. /* In this stage the MAC operations are increased by 1 for every iteration.
  135. The count variable holds the number of MAC operations performed */
  136. count = 1u;
  137. /* Working pointer of inputA */
  138. px = pIn1;
  139. /* Working pointer of inputB */
  140. py = pIn2;
  141. /* ------------------------
  142. * Stage1 process
  143. * ----------------------*/
  144. /* The first stage starts here */
  145. while(blockSize1 > 0u)
  146. {
  147. /* Accumulator is made zero for every iteration */
  148. sum = 0;
  149. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  150. k = count >> 2u;
  151. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  152. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  153. while(k > 0u)
  154. {
  155. /* x[0] * y[srcBLen - 1] */
  156. sum += (q63_t) * px++ * (*py--);
  157. /* x[1] * y[srcBLen - 2] */
  158. sum += (q63_t) * px++ * (*py--);
  159. /* x[2] * y[srcBLen - 3] */
  160. sum += (q63_t) * px++ * (*py--);
  161. /* x[3] * y[srcBLen - 4] */
  162. sum += (q63_t) * px++ * (*py--);
  163. /* Decrement the loop counter */
  164. k--;
  165. }
  166. /* If the count is not a multiple of 4, compute any remaining MACs here.
  167. ** No loop unrolling is used. */
  168. k = count % 0x4u;
  169. while(k > 0u)
  170. {
  171. /* Perform the multiply-accumulate */
  172. sum += (q63_t) * px++ * (*py--);
  173. /* Decrement the loop counter */
  174. k--;
  175. }
  176. /* Store the result in the accumulator in the destination buffer. */
  177. *pOut++ = (q31_t) (sum >> 31);
  178. /* Update the inputA and inputB pointers for next MAC calculation */
  179. py = pIn2 + count;
  180. px = pIn1;
  181. /* Increment the MAC count */
  182. count++;
  183. /* Decrement the loop counter */
  184. blockSize1--;
  185. }
  186. /* --------------------------
  187. * Initializations of stage2
  188. * ------------------------*/
  189. /* sum = x[0] * y[srcBLen-1] + x[1] * y[srcBLen-2] +...+ x[srcBLen-1] * y[0]
  190. * sum = x[1] * y[srcBLen-1] + x[2] * y[srcBLen-2] +...+ x[srcBLen] * y[0]
  191. * ....
  192. * sum = x[srcALen-srcBLen-2] * y[srcBLen-1] + x[srcALen] * y[srcBLen-2] +...+ x[srcALen-1] * y[0]
  193. */
  194. /* Working pointer of inputA */
  195. px = pIn1;
  196. /* Working pointer of inputB */
  197. pSrc2 = pIn2 + (srcBLen - 1u);
  198. py = pSrc2;
  199. /* count is index by which the pointer pIn1 to be incremented */
  200. count = 0u;
  201. /* -------------------
  202. * Stage2 process
  203. * ------------------*/
  204. /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed.
  205. * So, to loop unroll over blockSize2,
  206. * srcBLen should be greater than or equal to 4 */
  207. if(srcBLen >= 4u)
  208. {
  209. /* Loop unroll by 3 */
  210. blkCnt = blockSize2 / 3;
  211. while(blkCnt > 0u)
  212. {
  213. /* Set all accumulators to zero */
  214. acc0 = 0;
  215. acc1 = 0;
  216. acc2 = 0;
  217. /* read x[0], x[1], x[2] samples */
  218. x0 = *(px++);
  219. x1 = *(px++);
  220. /* Apply loop unrolling and compute 3 MACs simultaneously. */
  221. k = srcBLen / 3;
  222. /* First part of the processing with loop unrolling. Compute 3 MACs at a time.
  223. ** a second loop below computes MACs for the remaining 1 to 2 samples. */
  224. do
  225. {
  226. /* Read y[srcBLen - 1] sample */
  227. c0 = *(py);
  228. /* Read x[3] sample */
  229. x2 = *(px);
  230. /* Perform the multiply-accumulates */
  231. /* acc0 += x[0] * y[srcBLen - 1] */
  232. acc0 += ((q63_t) x0 * c0);
  233. /* acc1 += x[1] * y[srcBLen - 1] */
  234. acc1 += ((q63_t) x1 * c0);
  235. /* acc2 += x[2] * y[srcBLen - 1] */
  236. acc2 += ((q63_t) x2 * c0);
  237. /* Read y[srcBLen - 2] sample */
  238. c0 = *(py - 1u);
  239. /* Read x[4] sample */
  240. x0 = *(px + 1u);
  241. /* Perform the multiply-accumulate */
  242. /* acc0 += x[1] * y[srcBLen - 2] */
  243. acc0 += ((q63_t) x1 * c0);
  244. /* acc1 += x[2] * y[srcBLen - 2] */
  245. acc1 += ((q63_t) x2 * c0);
  246. /* acc2 += x[3] * y[srcBLen - 2] */
  247. acc2 += ((q63_t) x0 * c0);
  248. /* Read y[srcBLen - 3] sample */
  249. c0 = *(py - 2u);
  250. /* Read x[5] sample */
  251. x1 = *(px + 2u);
  252. /* Perform the multiply-accumulates */
  253. /* acc0 += x[2] * y[srcBLen - 3] */
  254. acc0 += ((q63_t) x2 * c0);
  255. /* acc1 += x[3] * y[srcBLen - 2] */
  256. acc1 += ((q63_t) x0 * c0);
  257. /* acc2 += x[4] * y[srcBLen - 2] */
  258. acc2 += ((q63_t) x1 * c0);
  259. /* update scratch pointers */
  260. px += 3u;
  261. py -= 3u;
  262. } while(--k);
  263. /* If the srcBLen is not a multiple of 3, compute any remaining MACs here.
  264. ** No loop unrolling is used. */
  265. k = srcBLen - (3 * (srcBLen / 3));
  266. while(k > 0u)
  267. {
  268. /* Read y[srcBLen - 5] sample */
  269. c0 = *(py--);
  270. /* Read x[7] sample */
  271. x2 = *(px++);
  272. /* Perform the multiply-accumulates */
  273. /* acc0 += x[4] * y[srcBLen - 5] */
  274. acc0 += ((q63_t) x0 * c0);
  275. /* acc1 += x[5] * y[srcBLen - 5] */
  276. acc1 += ((q63_t) x1 * c0);
  277. /* acc2 += x[6] * y[srcBLen - 5] */
  278. acc2 += ((q63_t) x2 * c0);
  279. /* Reuse the present samples for the next MAC */
  280. x0 = x1;
  281. x1 = x2;
  282. /* Decrement the loop counter */
  283. k--;
  284. }
  285. /* Store the results in the accumulators in the destination buffer. */
  286. *pOut++ = (q31_t) (acc0 >> 31);
  287. *pOut++ = (q31_t) (acc1 >> 31);
  288. *pOut++ = (q31_t) (acc2 >> 31);
  289. /* Increment the pointer pIn1 index, count by 3 */
  290. count += 3u;
  291. /* Update the inputA and inputB pointers for next MAC calculation */
  292. px = pIn1 + count;
  293. py = pSrc2;
  294. /* Decrement the loop counter */
  295. blkCnt--;
  296. }
  297. /* If the blockSize2 is not a multiple of 3, compute any remaining output samples here.
  298. ** No loop unrolling is used. */
  299. blkCnt = blockSize2 - 3 * (blockSize2 / 3);
  300. while(blkCnt > 0u)
  301. {
  302. /* Accumulator is made zero for every iteration */
  303. sum = 0;
  304. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  305. k = srcBLen >> 2u;
  306. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  307. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  308. while(k > 0u)
  309. {
  310. /* Perform the multiply-accumulates */
  311. sum += (q63_t) * px++ * (*py--);
  312. sum += (q63_t) * px++ * (*py--);
  313. sum += (q63_t) * px++ * (*py--);
  314. sum += (q63_t) * px++ * (*py--);
  315. /* Decrement the loop counter */
  316. k--;
  317. }
  318. /* If the srcBLen is not a multiple of 4, compute any remaining MACs here.
  319. ** No loop unrolling is used. */
  320. k = srcBLen % 0x4u;
  321. while(k > 0u)
  322. {
  323. /* Perform the multiply-accumulate */
  324. sum += (q63_t) * px++ * (*py--);
  325. /* Decrement the loop counter */
  326. k--;
  327. }
  328. /* Store the result in the accumulator in the destination buffer. */
  329. *pOut++ = (q31_t) (sum >> 31);
  330. /* Increment the MAC count */
  331. count++;
  332. /* Update the inputA and inputB pointers for next MAC calculation */
  333. px = pIn1 + count;
  334. py = pSrc2;
  335. /* Decrement the loop counter */
  336. blkCnt--;
  337. }
  338. }
  339. else
  340. {
  341. /* If the srcBLen is not a multiple of 4,
  342. * the blockSize2 loop cannot be unrolled by 4 */
  343. blkCnt = blockSize2;
  344. while(blkCnt > 0u)
  345. {
  346. /* Accumulator is made zero for every iteration */
  347. sum = 0;
  348. /* srcBLen number of MACS should be performed */
  349. k = srcBLen;
  350. while(k > 0u)
  351. {
  352. /* Perform the multiply-accumulate */
  353. sum += (q63_t) * px++ * (*py--);
  354. /* Decrement the loop counter */
  355. k--;
  356. }
  357. /* Store the result in the accumulator in the destination buffer. */
  358. *pOut++ = (q31_t) (sum >> 31);
  359. /* Increment the MAC count */
  360. count++;
  361. /* Update the inputA and inputB pointers for next MAC calculation */
  362. px = pIn1 + count;
  363. py = pSrc2;
  364. /* Decrement the loop counter */
  365. blkCnt--;
  366. }
  367. }
  368. /* --------------------------
  369. * Initializations of stage3
  370. * -------------------------*/
  371. /* sum += x[srcALen-srcBLen+1] * y[srcBLen-1] + x[srcALen-srcBLen+2] * y[srcBLen-2] +...+ x[srcALen-1] * y[1]
  372. * sum += x[srcALen-srcBLen+2] * y[srcBLen-1] + x[srcALen-srcBLen+3] * y[srcBLen-2] +...+ x[srcALen-1] * y[2]
  373. * ....
  374. * sum += x[srcALen-2] * y[srcBLen-1] + x[srcALen-1] * y[srcBLen-2]
  375. * sum += x[srcALen-1] * y[srcBLen-1]
  376. */
  377. /* In this stage the MAC operations are decreased by 1 for every iteration.
  378. The blockSize3 variable holds the number of MAC operations performed */
  379. /* Working pointer of inputA */
  380. pSrc1 = (pIn1 + srcALen) - (srcBLen - 1u);
  381. px = pSrc1;
  382. /* Working pointer of inputB */
  383. pSrc2 = pIn2 + (srcBLen - 1u);
  384. py = pSrc2;
  385. /* -------------------
  386. * Stage3 process
  387. * ------------------*/
  388. while(blockSize3 > 0u)
  389. {
  390. /* Accumulator is made zero for every iteration */
  391. sum = 0;
  392. /* Apply loop unrolling and compute 4 MACs simultaneously. */
  393. k = blockSize3 >> 2u;
  394. /* First part of the processing with loop unrolling. Compute 4 MACs at a time.
  395. ** a second loop below computes MACs for the remaining 1 to 3 samples. */
  396. while(k > 0u)
  397. {
  398. /* sum += x[srcALen - srcBLen + 1] * y[srcBLen - 1] */
  399. sum += (q63_t) * px++ * (*py--);
  400. /* sum += x[srcALen - srcBLen + 2] * y[srcBLen - 2] */
  401. sum += (q63_t) * px++ * (*py--);
  402. /* sum += x[srcALen - srcBLen + 3] * y[srcBLen - 3] */
  403. sum += (q63_t) * px++ * (*py--);
  404. /* sum += x[srcALen - srcBLen + 4] * y[srcBLen - 4] */
  405. sum += (q63_t) * px++ * (*py--);
  406. /* Decrement the loop counter */
  407. k--;
  408. }
  409. /* If the blockSize3 is not a multiple of 4, compute any remaining MACs here.
  410. ** No loop unrolling is used. */
  411. k = blockSize3 % 0x4u;
  412. while(k > 0u)
  413. {
  414. /* Perform the multiply-accumulate */
  415. sum += (q63_t) * px++ * (*py--);
  416. /* Decrement the loop counter */
  417. k--;
  418. }
  419. /* Store the result in the accumulator in the destination buffer. */
  420. *pOut++ = (q31_t) (sum >> 31);
  421. /* Update the inputA and inputB pointers for next MAC calculation */
  422. px = ++pSrc1;
  423. py = pSrc2;
  424. /* Decrement the loop counter */
  425. blockSize3--;
  426. }
  427. #else
  428. /* Run the below code for Cortex-M0 */
  429. q31_t *pIn1 = pSrcA; /* input pointer */
  430. q31_t *pIn2 = pSrcB; /* coefficient pointer */
  431. q63_t sum; /* Accumulator */
  432. uint32_t i, j; /* loop counter */
  433. /* Loop to calculate output of convolution for output length number of times */
  434. for (i = 0; i < (srcALen + srcBLen - 1); i++)
  435. {
  436. /* Initialize sum with zero to carry on MAC operations */
  437. sum = 0;
  438. /* Loop to perform MAC operations according to convolution equation */
  439. for (j = 0; j <= i; j++)
  440. {
  441. /* Check the array limitations */
  442. if(((i - j) < srcBLen) && (j < srcALen))
  443. {
  444. /* z[i] += x[i-j] * y[j] */
  445. sum += ((q63_t) pIn1[j] * (pIn2[i - j]));
  446. }
  447. }
  448. /* Store the output in the destination buffer */
  449. pDst[i] = (q31_t) (sum >> 31u);
  450. }
  451. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  452. }
  453. /**
  454. * @} end of Conv group
  455. */