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_opt_q15.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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_opt_q15.c
  9. *
  10. * Description: 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 Conv
  46. * @{
  47. */
  48. /**
  49. * @brief 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. Length srcALen+srcBLen-1.
  55. * @param[in] *pScratch1 points to scratch buffer of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
  56. * @param[in] *pScratch2 points to scratch buffer of size min(srcALen, srcBLen).
  57. * @return none.
  58. *
  59. * \par Restrictions
  60. * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
  61. * In this case input, output, scratch1 and scratch2 buffers should be aligned by 32-bit
  62. *
  63. *
  64. * @details
  65. * <b>Scaling and Overflow Behavior:</b>
  66. *
  67. * \par
  68. * The function is implemented using a 64-bit internal accumulator.
  69. * Both inputs are in 1.15 format and multiplications yield a 2.30 result.
  70. * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  71. * This approach provides 33 guard bits and there is no risk of overflow.
  72. * The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format.
  73. *
  74. *
  75. * \par
  76. * Refer to <code>arm_conv_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
  77. *
  78. *
  79. */
  80. void arm_conv_opt_q15(
  81. q15_t * pSrcA,
  82. uint32_t srcALen,
  83. q15_t * pSrcB,
  84. uint32_t srcBLen,
  85. q15_t * pDst,
  86. q15_t * pScratch1,
  87. q15_t * pScratch2)
  88. {
  89. q63_t acc0, acc1, acc2, acc3; /* Accumulator */
  90. q31_t x1, x2, x3; /* Temporary variables to hold state and coefficient values */
  91. q31_t y1, y2; /* State variables */
  92. q15_t *pOut = pDst; /* output pointer */
  93. q15_t *pScr1 = pScratch1; /* Temporary pointer for scratch1 */
  94. q15_t *pScr2 = pScratch2; /* Temporary pointer for scratch1 */
  95. q15_t *pIn1; /* inputA pointer */
  96. q15_t *pIn2; /* inputB pointer */
  97. q15_t *px; /* Intermediate inputA pointer */
  98. q15_t *py; /* Intermediate inputB pointer */
  99. uint32_t j, k, blkCnt; /* loop counter */
  100. uint32_t tapCnt; /* loop count */
  101. #ifdef UNALIGNED_SUPPORT_DISABLE
  102. q15_t a, b;
  103. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  104. /* The algorithm implementation is based on the lengths of the inputs. */
  105. /* srcB is always made to slide across srcA. */
  106. /* So srcBLen is always considered as shorter or equal to srcALen */
  107. if(srcALen >= srcBLen)
  108. {
  109. /* Initialization of inputA pointer */
  110. pIn1 = pSrcA;
  111. /* Initialization of inputB pointer */
  112. pIn2 = pSrcB;
  113. }
  114. else
  115. {
  116. /* Initialization of inputA pointer */
  117. pIn1 = pSrcB;
  118. /* Initialization of inputB pointer */
  119. pIn2 = pSrcA;
  120. /* srcBLen is always considered as shorter or equal to srcALen */
  121. j = srcBLen;
  122. srcBLen = srcALen;
  123. srcALen = j;
  124. }
  125. /* pointer to take end of scratch2 buffer */
  126. pScr2 = pScratch2 + srcBLen - 1;
  127. /* points to smaller length sequence */
  128. px = pIn2;
  129. /* Apply loop unrolling and do 4 Copies simultaneously. */
  130. k = srcBLen >> 2u;
  131. /* First part of the processing with loop unrolling copies 4 data points at a time.
  132. ** a second loop below copies for the remaining 1 to 3 samples. */
  133. /* Copy smaller length input sequence in reverse order into second scratch buffer */
  134. while(k > 0u)
  135. {
  136. /* copy second buffer in reversal manner */
  137. *pScr2-- = *px++;
  138. *pScr2-- = *px++;
  139. *pScr2-- = *px++;
  140. *pScr2-- = *px++;
  141. /* Decrement the loop counter */
  142. k--;
  143. }
  144. /* If the count is not a multiple of 4, copy remaining samples here.
  145. ** No loop unrolling is used. */
  146. k = srcBLen % 0x4u;
  147. while(k > 0u)
  148. {
  149. /* copy second buffer in reversal manner for remaining samples */
  150. *pScr2-- = *px++;
  151. /* Decrement the loop counter */
  152. k--;
  153. }
  154. /* Initialze temporary scratch pointer */
  155. pScr1 = pScratch1;
  156. /* Assuming scratch1 buffer is aligned by 32-bit */
  157. /* Fill (srcBLen - 1u) zeros in scratch buffer */
  158. arm_fill_q15(0, pScr1, (srcBLen - 1u));
  159. /* Update temporary scratch pointer */
  160. pScr1 += (srcBLen - 1u);
  161. /* Copy bigger length sequence(srcALen) samples in scratch1 buffer */
  162. #ifndef UNALIGNED_SUPPORT_DISABLE
  163. /* Copy (srcALen) samples in scratch buffer */
  164. arm_copy_q15(pIn1, pScr1, srcALen);
  165. /* Update pointers */
  166. pScr1 += srcALen;
  167. #else
  168. /* Apply loop unrolling and do 4 Copies simultaneously. */
  169. k = srcALen >> 2u;
  170. /* First part of the processing with loop unrolling copies 4 data points at a time.
  171. ** a second loop below copies for the remaining 1 to 3 samples. */
  172. while(k > 0u)
  173. {
  174. /* copy second buffer in reversal manner */
  175. *pScr1++ = *pIn1++;
  176. *pScr1++ = *pIn1++;
  177. *pScr1++ = *pIn1++;
  178. *pScr1++ = *pIn1++;
  179. /* Decrement the loop counter */
  180. k--;
  181. }
  182. /* If the count is not a multiple of 4, copy remaining samples here.
  183. ** No loop unrolling is used. */
  184. k = srcALen % 0x4u;
  185. while(k > 0u)
  186. {
  187. /* copy second buffer in reversal manner for remaining samples */
  188. *pScr1++ = *pIn1++;
  189. /* Decrement the loop counter */
  190. k--;
  191. }
  192. #endif
  193. #ifndef UNALIGNED_SUPPORT_DISABLE
  194. /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
  195. arm_fill_q15(0, pScr1, (srcBLen - 1u));
  196. /* Update pointer */
  197. pScr1 += (srcBLen - 1u);
  198. #else
  199. /* Apply loop unrolling and do 4 Copies simultaneously. */
  200. k = (srcBLen - 1u) >> 2u;
  201. /* First part of the processing with loop unrolling copies 4 data points at a time.
  202. ** a second loop below copies for the remaining 1 to 3 samples. */
  203. while(k > 0u)
  204. {
  205. /* copy second buffer in reversal manner */
  206. *pScr1++ = 0;
  207. *pScr1++ = 0;
  208. *pScr1++ = 0;
  209. *pScr1++ = 0;
  210. /* Decrement the loop counter */
  211. k--;
  212. }
  213. /* If the count is not a multiple of 4, copy remaining samples here.
  214. ** No loop unrolling is used. */
  215. k = (srcBLen - 1u) % 0x4u;
  216. while(k > 0u)
  217. {
  218. /* copy second buffer in reversal manner for remaining samples */
  219. *pScr1++ = 0;
  220. /* Decrement the loop counter */
  221. k--;
  222. }
  223. #endif
  224. /* Temporary pointer for scratch2 */
  225. py = pScratch2;
  226. /* Initialization of pIn2 pointer */
  227. pIn2 = py;
  228. /* First part of the processing with loop unrolling process 4 data points at a time.
  229. ** a second loop below process for the remaining 1 to 3 samples. */
  230. /* Actual convolution process starts here */
  231. blkCnt = (srcALen + srcBLen - 1u) >> 2;
  232. while(blkCnt > 0)
  233. {
  234. /* Initialze temporary scratch pointer as scratch1 */
  235. pScr1 = pScratch1;
  236. /* Clear Accumlators */
  237. acc0 = 0;
  238. acc1 = 0;
  239. acc2 = 0;
  240. acc3 = 0;
  241. /* Read two samples from scratch1 buffer */
  242. x1 = *__SIMD32(pScr1)++;
  243. /* Read next two samples from scratch1 buffer */
  244. x2 = *__SIMD32(pScr1)++;
  245. tapCnt = (srcBLen) >> 2u;
  246. while(tapCnt > 0u)
  247. {
  248. #ifndef UNALIGNED_SUPPORT_DISABLE
  249. /* Read four samples from smaller buffer */
  250. y1 = _SIMD32_OFFSET(pIn2);
  251. y2 = _SIMD32_OFFSET(pIn2 + 2u);
  252. /* multiply and accumlate */
  253. acc0 = __SMLALD(x1, y1, acc0);
  254. acc2 = __SMLALD(x2, y1, acc2);
  255. /* pack input data */
  256. #ifndef ARM_MATH_BIG_ENDIAN
  257. x3 = __PKHBT(x2, x1, 0);
  258. #else
  259. x3 = __PKHBT(x1, x2, 0);
  260. #endif
  261. /* multiply and accumlate */
  262. acc1 = __SMLALDX(x3, y1, acc1);
  263. /* Read next two samples from scratch1 buffer */
  264. x1 = _SIMD32_OFFSET(pScr1);
  265. /* multiply and accumlate */
  266. acc0 = __SMLALD(x2, y2, acc0);
  267. acc2 = __SMLALD(x1, y2, acc2);
  268. /* pack input data */
  269. #ifndef ARM_MATH_BIG_ENDIAN
  270. x3 = __PKHBT(x1, x2, 0);
  271. #else
  272. x3 = __PKHBT(x2, x1, 0);
  273. #endif
  274. acc3 = __SMLALDX(x3, y1, acc3);
  275. acc1 = __SMLALDX(x3, y2, acc1);
  276. x2 = _SIMD32_OFFSET(pScr1 + 2u);
  277. #ifndef ARM_MATH_BIG_ENDIAN
  278. x3 = __PKHBT(x2, x1, 0);
  279. #else
  280. x3 = __PKHBT(x1, x2, 0);
  281. #endif
  282. acc3 = __SMLALDX(x3, y2, acc3);
  283. #else
  284. /* Read four samples from smaller buffer */
  285. a = *pIn2;
  286. b = *(pIn2 + 1);
  287. #ifndef ARM_MATH_BIG_ENDIAN
  288. y1 = __PKHBT(a, b, 16);
  289. #else
  290. y1 = __PKHBT(b, a, 16);
  291. #endif
  292. a = *(pIn2 + 2);
  293. b = *(pIn2 + 3);
  294. #ifndef ARM_MATH_BIG_ENDIAN
  295. y2 = __PKHBT(a, b, 16);
  296. #else
  297. y2 = __PKHBT(b, a, 16);
  298. #endif
  299. acc0 = __SMLALD(x1, y1, acc0);
  300. acc2 = __SMLALD(x2, y1, acc2);
  301. #ifndef ARM_MATH_BIG_ENDIAN
  302. x3 = __PKHBT(x2, x1, 0);
  303. #else
  304. x3 = __PKHBT(x1, x2, 0);
  305. #endif
  306. acc1 = __SMLALDX(x3, y1, acc1);
  307. a = *pScr1;
  308. b = *(pScr1 + 1);
  309. #ifndef ARM_MATH_BIG_ENDIAN
  310. x1 = __PKHBT(a, b, 16);
  311. #else
  312. x1 = __PKHBT(b, a, 16);
  313. #endif
  314. acc0 = __SMLALD(x2, y2, acc0);
  315. acc2 = __SMLALD(x1, y2, acc2);
  316. #ifndef ARM_MATH_BIG_ENDIAN
  317. x3 = __PKHBT(x1, x2, 0);
  318. #else
  319. x3 = __PKHBT(x2, x1, 0);
  320. #endif
  321. acc3 = __SMLALDX(x3, y1, acc3);
  322. acc1 = __SMLALDX(x3, y2, acc1);
  323. a = *(pScr1 + 2);
  324. b = *(pScr1 + 3);
  325. #ifndef ARM_MATH_BIG_ENDIAN
  326. x2 = __PKHBT(a, b, 16);
  327. #else
  328. x2 = __PKHBT(b, a, 16);
  329. #endif
  330. #ifndef ARM_MATH_BIG_ENDIAN
  331. x3 = __PKHBT(x2, x1, 0);
  332. #else
  333. x3 = __PKHBT(x1, x2, 0);
  334. #endif
  335. acc3 = __SMLALDX(x3, y2, acc3);
  336. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  337. pIn2 += 4u;
  338. pScr1 += 4u;
  339. /* Decrement the loop counter */
  340. tapCnt--;
  341. }
  342. /* Update scratch pointer for remaining samples of smaller length sequence */
  343. pScr1 -= 4u;
  344. /* apply same above for remaining samples of smaller length sequence */
  345. tapCnt = (srcBLen) & 3u;
  346. while(tapCnt > 0u)
  347. {
  348. /* accumlate the results */
  349. acc0 += (*pScr1++ * *pIn2);
  350. acc1 += (*pScr1++ * *pIn2);
  351. acc2 += (*pScr1++ * *pIn2);
  352. acc3 += (*pScr1++ * *pIn2++);
  353. pScr1 -= 3u;
  354. /* Decrement the loop counter */
  355. tapCnt--;
  356. }
  357. blkCnt--;
  358. /* Store the results in the accumulators in the destination buffer. */
  359. #ifndef ARM_MATH_BIG_ENDIAN
  360. *__SIMD32(pOut)++ =
  361. __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
  362. *__SIMD32(pOut)++ =
  363. __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
  364. #else
  365. *__SIMD32(pOut)++ =
  366. __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
  367. *__SIMD32(pOut)++ =
  368. __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
  369. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  370. /* Initialization of inputB pointer */
  371. pIn2 = py;
  372. pScratch1 += 4u;
  373. }
  374. blkCnt = (srcALen + srcBLen - 1u) & 0x3;
  375. /* Calculate convolution for remaining samples of Bigger length sequence */
  376. while(blkCnt > 0)
  377. {
  378. /* Initialze temporary scratch pointer as scratch1 */
  379. pScr1 = pScratch1;
  380. /* Clear Accumlators */
  381. acc0 = 0;
  382. tapCnt = (srcBLen) >> 1u;
  383. while(tapCnt > 0u)
  384. {
  385. /* Read next two samples from scratch1 buffer */
  386. acc0 += (*pScr1++ * *pIn2++);
  387. acc0 += (*pScr1++ * *pIn2++);
  388. /* Decrement the loop counter */
  389. tapCnt--;
  390. }
  391. tapCnt = (srcBLen) & 1u;
  392. /* apply same above for remaining samples of smaller length sequence */
  393. while(tapCnt > 0u)
  394. {
  395. /* accumlate the results */
  396. acc0 += (*pScr1++ * *pIn2++);
  397. /* Decrement the loop counter */
  398. tapCnt--;
  399. }
  400. blkCnt--;
  401. /* The result is in 2.30 format. Convert to 1.15 with saturation.
  402. ** Then store the output in the destination buffer. */
  403. *pOut++ = (q15_t) (__SSAT((acc0 >> 15), 16));
  404. /* Initialization of inputB pointer */
  405. pIn2 = py;
  406. pScratch1 += 1u;
  407. }
  408. }
  409. /**
  410. * @} end of Conv group
  411. */