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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_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. * @param[in] *pScratch1 points to scratch buffer(of type q15_t) of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
  56. * @param[in] *pScratch2 points to scratch buffer (of type q15_t) 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. * @details
  64. * <b>Scaling and Overflow Behavior:</b>
  65. *
  66. * \par
  67. * The function is implemented using a 32-bit internal accumulator.
  68. * Both the inputs are represented in 1.7 format and multiplications yield a 2.14 result.
  69. * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format.
  70. * This approach provides 17 guard bits and there is no risk of overflow as long as <code>max(srcALen, srcBLen)<131072</code>.
  71. * The 18.14 result is then truncated to 18.7 format by discarding the low 7 bits and then saturated to 1.7 format.
  72. *
  73. */
  74. void arm_conv_opt_q7(
  75. q7_t * pSrcA,
  76. uint32_t srcALen,
  77. q7_t * pSrcB,
  78. uint32_t srcBLen,
  79. q7_t * pDst,
  80. q15_t * pScratch1,
  81. q15_t * pScratch2)
  82. {
  83. q15_t *pScr2, *pScr1; /* Intermediate pointers for scratch pointers */
  84. q15_t x4; /* Temporary input variable */
  85. q7_t *pIn1, *pIn2; /* inputA and inputB pointer */
  86. uint32_t j, k, blkCnt, tapCnt; /* loop counter */
  87. q7_t *px; /* Temporary input1 pointer */
  88. q15_t *py; /* Temporary input2 pointer */
  89. q31_t acc0, acc1, acc2, acc3; /* Accumulator */
  90. q31_t x1, x2, x3, y1; /* Temporary input variables */
  91. q7_t *pOut = pDst; /* output pointer */
  92. q7_t out0, out1, out2, out3; /* temporary variables */
  93. /* The algorithm implementation is based on the lengths of the inputs. */
  94. /* srcB is always made to slide across srcA. */
  95. /* So srcBLen is always considered as shorter or equal to srcALen */
  96. if(srcALen >= srcBLen)
  97. {
  98. /* Initialization of inputA pointer */
  99. pIn1 = pSrcA;
  100. /* Initialization of inputB pointer */
  101. pIn2 = pSrcB;
  102. }
  103. else
  104. {
  105. /* Initialization of inputA pointer */
  106. pIn1 = pSrcB;
  107. /* Initialization of inputB pointer */
  108. pIn2 = pSrcA;
  109. /* srcBLen is always considered as shorter or equal to srcALen */
  110. j = srcBLen;
  111. srcBLen = srcALen;
  112. srcALen = j;
  113. }
  114. /* pointer to take end of scratch2 buffer */
  115. pScr2 = pScratch2;
  116. /* points to smaller length sequence */
  117. px = pIn2 + srcBLen - 1;
  118. /* Apply loop unrolling and do 4 Copies simultaneously. */
  119. k = srcBLen >> 2u;
  120. /* First part of the processing with loop unrolling copies 4 data points at a time.
  121. ** a second loop below copies for the remaining 1 to 3 samples. */
  122. while(k > 0u)
  123. {
  124. /* copy second buffer in reversal manner */
  125. x4 = (q15_t) * px--;
  126. *pScr2++ = x4;
  127. x4 = (q15_t) * px--;
  128. *pScr2++ = x4;
  129. x4 = (q15_t) * px--;
  130. *pScr2++ = x4;
  131. x4 = (q15_t) * px--;
  132. *pScr2++ = x4;
  133. /* Decrement the loop counter */
  134. k--;
  135. }
  136. /* If the count is not a multiple of 4, copy remaining samples here.
  137. ** No loop unrolling is used. */
  138. k = srcBLen % 0x4u;
  139. while(k > 0u)
  140. {
  141. /* copy second buffer in reversal manner for remaining samples */
  142. x4 = (q15_t) * px--;
  143. *pScr2++ = x4;
  144. /* Decrement the loop counter */
  145. k--;
  146. }
  147. /* Initialze temporary scratch pointer */
  148. pScr1 = pScratch1;
  149. /* Fill (srcBLen - 1u) zeros in scratch buffer */
  150. arm_fill_q15(0, pScr1, (srcBLen - 1u));
  151. /* Update temporary scratch pointer */
  152. pScr1 += (srcBLen - 1u);
  153. /* Copy (srcALen) samples in scratch buffer */
  154. /* Apply loop unrolling and do 4 Copies simultaneously. */
  155. k = srcALen >> 2u;
  156. /* First part of the processing with loop unrolling copies 4 data points at a time.
  157. ** a second loop below copies for the remaining 1 to 3 samples. */
  158. while(k > 0u)
  159. {
  160. /* copy second buffer in reversal manner */
  161. x4 = (q15_t) * pIn1++;
  162. *pScr1++ = x4;
  163. x4 = (q15_t) * pIn1++;
  164. *pScr1++ = x4;
  165. x4 = (q15_t) * pIn1++;
  166. *pScr1++ = x4;
  167. x4 = (q15_t) * pIn1++;
  168. *pScr1++ = x4;
  169. /* Decrement the loop counter */
  170. k--;
  171. }
  172. /* If the count is not a multiple of 4, copy remaining samples here.
  173. ** No loop unrolling is used. */
  174. k = srcALen % 0x4u;
  175. while(k > 0u)
  176. {
  177. /* copy second buffer in reversal manner for remaining samples */
  178. x4 = (q15_t) * pIn1++;
  179. *pScr1++ = x4;
  180. /* Decrement the loop counter */
  181. k--;
  182. }
  183. #ifndef UNALIGNED_SUPPORT_DISABLE
  184. /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
  185. arm_fill_q15(0, pScr1, (srcBLen - 1u));
  186. /* Update pointer */
  187. pScr1 += (srcBLen - 1u);
  188. #else
  189. /* Apply loop unrolling and do 4 Copies simultaneously. */
  190. k = (srcBLen - 1u) >> 2u;
  191. /* First part of the processing with loop unrolling copies 4 data points at a time.
  192. ** a second loop below copies for the remaining 1 to 3 samples. */
  193. while(k > 0u)
  194. {
  195. /* copy second buffer in reversal manner */
  196. *pScr1++ = 0;
  197. *pScr1++ = 0;
  198. *pScr1++ = 0;
  199. *pScr1++ = 0;
  200. /* Decrement the loop counter */
  201. k--;
  202. }
  203. /* If the count is not a multiple of 4, copy remaining samples here.
  204. ** No loop unrolling is used. */
  205. k = (srcBLen - 1u) % 0x4u;
  206. while(k > 0u)
  207. {
  208. /* copy second buffer in reversal manner for remaining samples */
  209. *pScr1++ = 0;
  210. /* Decrement the loop counter */
  211. k--;
  212. }
  213. #endif
  214. /* Temporary pointer for scratch2 */
  215. py = pScratch2;
  216. /* Initialization of pIn2 pointer */
  217. pIn2 = (q7_t *) py;
  218. pScr2 = py;
  219. /* Actual convolution process starts here */
  220. blkCnt = (srcALen + srcBLen - 1u) >> 2;
  221. while(blkCnt > 0)
  222. {
  223. /* Initialze temporary scratch pointer as scratch1 */
  224. pScr1 = pScratch1;
  225. /* Clear Accumlators */
  226. acc0 = 0;
  227. acc1 = 0;
  228. acc2 = 0;
  229. acc3 = 0;
  230. /* Read two samples from scratch1 buffer */
  231. x1 = *__SIMD32(pScr1)++;
  232. /* Read next two samples from scratch1 buffer */
  233. x2 = *__SIMD32(pScr1)++;
  234. tapCnt = (srcBLen) >> 2u;
  235. while(tapCnt > 0u)
  236. {
  237. /* Read four samples from smaller buffer */
  238. y1 = _SIMD32_OFFSET(pScr2);
  239. /* multiply and accumlate */
  240. acc0 = __SMLAD(x1, y1, acc0);
  241. acc2 = __SMLAD(x2, y1, acc2);
  242. /* pack input data */
  243. #ifndef ARM_MATH_BIG_ENDIAN
  244. x3 = __PKHBT(x2, x1, 0);
  245. #else
  246. x3 = __PKHBT(x1, x2, 0);
  247. #endif
  248. /* multiply and accumlate */
  249. acc1 = __SMLADX(x3, y1, acc1);
  250. /* Read next two samples from scratch1 buffer */
  251. x1 = *__SIMD32(pScr1)++;
  252. /* pack input data */
  253. #ifndef ARM_MATH_BIG_ENDIAN
  254. x3 = __PKHBT(x1, x2, 0);
  255. #else
  256. x3 = __PKHBT(x2, x1, 0);
  257. #endif
  258. acc3 = __SMLADX(x3, y1, acc3);
  259. /* Read four samples from smaller buffer */
  260. y1 = _SIMD32_OFFSET(pScr2 + 2u);
  261. acc0 = __SMLAD(x2, y1, acc0);
  262. acc2 = __SMLAD(x1, y1, acc2);
  263. acc1 = __SMLADX(x3, y1, acc1);
  264. x2 = *__SIMD32(pScr1)++;
  265. #ifndef ARM_MATH_BIG_ENDIAN
  266. x3 = __PKHBT(x2, x1, 0);
  267. #else
  268. x3 = __PKHBT(x1, x2, 0);
  269. #endif
  270. acc3 = __SMLADX(x3, y1, acc3);
  271. pScr2 += 4u;
  272. /* Decrement the loop counter */
  273. tapCnt--;
  274. }
  275. /* Update scratch pointer for remaining samples of smaller length sequence */
  276. pScr1 -= 4u;
  277. /* apply same above for remaining samples of smaller length sequence */
  278. tapCnt = (srcBLen) & 3u;
  279. while(tapCnt > 0u)
  280. {
  281. /* accumlate the results */
  282. acc0 += (*pScr1++ * *pScr2);
  283. acc1 += (*pScr1++ * *pScr2);
  284. acc2 += (*pScr1++ * *pScr2);
  285. acc3 += (*pScr1++ * *pScr2++);
  286. pScr1 -= 3u;
  287. /* Decrement the loop counter */
  288. tapCnt--;
  289. }
  290. blkCnt--;
  291. /* Store the result in the accumulator in the destination buffer. */
  292. out0 = (q7_t) (__SSAT(acc0 >> 7u, 8));
  293. out1 = (q7_t) (__SSAT(acc1 >> 7u, 8));
  294. out2 = (q7_t) (__SSAT(acc2 >> 7u, 8));
  295. out3 = (q7_t) (__SSAT(acc3 >> 7u, 8));
  296. *__SIMD32(pOut)++ = __PACKq7(out0, out1, out2, out3);
  297. /* Initialization of inputB pointer */
  298. pScr2 = py;
  299. pScratch1 += 4u;
  300. }
  301. blkCnt = (srcALen + srcBLen - 1u) & 0x3;
  302. /* Calculate convolution for remaining samples of Bigger length sequence */
  303. while(blkCnt > 0)
  304. {
  305. /* Initialze temporary scratch pointer as scratch1 */
  306. pScr1 = pScratch1;
  307. /* Clear Accumlators */
  308. acc0 = 0;
  309. tapCnt = (srcBLen) >> 1u;
  310. while(tapCnt > 0u)
  311. {
  312. acc0 += (*pScr1++ * *pScr2++);
  313. acc0 += (*pScr1++ * *pScr2++);
  314. /* Decrement the loop counter */
  315. tapCnt--;
  316. }
  317. tapCnt = (srcBLen) & 1u;
  318. /* apply same above for remaining samples of smaller length sequence */
  319. while(tapCnt > 0u)
  320. {
  321. /* accumlate the results */
  322. acc0 += (*pScr1++ * *pScr2++);
  323. /* Decrement the loop counter */
  324. tapCnt--;
  325. }
  326. blkCnt--;
  327. /* Store the result in the accumulator in the destination buffer. */
  328. *pOut++ = (q7_t) (__SSAT(acc0 >> 7u, 8));
  329. /* Initialization of inputB pointer */
  330. pScr2 = py;
  331. pScratch1 += 1u;
  332. }
  333. }
  334. /**
  335. * @} end of Conv group
  336. */