Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

arm_fir_fast_q15.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_fir_fast_q15.c
  9. *
  10. * Description: Q15 Fast FIR filter processing function.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3
  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 FIR
  46. * @{
  47. */
  48. /**
  49. * @param[in] *S points to an instance of the Q15 FIR filter structure.
  50. * @param[in] *pSrc points to the block of input data.
  51. * @param[out] *pDst points to the block of output data.
  52. * @param[in] blockSize number of samples to process per call.
  53. * @return none.
  54. *
  55. * <b>Scaling and Overflow Behavior:</b>
  56. * \par
  57. * This fast version uses a 32-bit accumulator with 2.30 format.
  58. * The accumulator maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  59. * Thus, if the accumulator result overflows it wraps around and distorts the result.
  60. * In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits.
  61. * The 2.30 accumulator is then truncated to 2.15 format and saturated to yield the 1.15 result.
  62. *
  63. * \par
  64. * Refer to the function <code>arm_fir_q15()</code> for a slower implementation of this function which uses 64-bit accumulation to avoid wrap around distortion. Both the slow and the fast versions use the same instance structure.
  65. * Use the function <code>arm_fir_init_q15()</code> to initialize the filter structure.
  66. */
  67. void arm_fir_fast_q15(
  68. const arm_fir_instance_q15 * S,
  69. q15_t * pSrc,
  70. q15_t * pDst,
  71. uint32_t blockSize)
  72. {
  73. q15_t *pState = S->pState; /* State pointer */
  74. q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
  75. q15_t *pStateCurnt; /* Points to the current sample of the state */
  76. q31_t acc0, acc1, acc2, acc3; /* Accumulators */
  77. q15_t *pb; /* Temporary pointer for coefficient buffer */
  78. q15_t *px; /* Temporary q31 pointer for SIMD state buffer accesses */
  79. q31_t x0, x1, x2, c0; /* Temporary variables to hold SIMD state and coefficient values */
  80. uint32_t numTaps = S->numTaps; /* Number of taps in the filter */
  81. uint32_t tapCnt, blkCnt; /* Loop counters */
  82. /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  83. /* pStateCurnt points to the location where the new input data should be written */
  84. pStateCurnt = &(S->pState[(numTaps - 1u)]);
  85. /* Apply loop unrolling and compute 4 output values simultaneously.
  86. * The variables acc0 ... acc3 hold output values that are being computed:
  87. *
  88. * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0]
  89. * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1]
  90. * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2]
  91. * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3]
  92. */
  93. blkCnt = blockSize >> 2;
  94. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  95. ** a second loop below computes the remaining 1 to 3 samples. */
  96. while(blkCnt > 0u)
  97. {
  98. /* Copy four new input samples into the state buffer.
  99. ** Use 32-bit SIMD to move the 16-bit data. Only requires two copies. */
  100. *pStateCurnt++ = *pSrc++;
  101. *pStateCurnt++ = *pSrc++;
  102. *pStateCurnt++ = *pSrc++;
  103. *pStateCurnt++ = *pSrc++;
  104. /* Set all accumulators to zero */
  105. acc0 = 0;
  106. acc1 = 0;
  107. acc2 = 0;
  108. acc3 = 0;
  109. /* Typecast q15_t pointer to q31_t pointer for state reading in q31_t */
  110. px = pState;
  111. /* Typecast q15_t pointer to q31_t pointer for coefficient reading in q31_t */
  112. pb = pCoeffs;
  113. /* Read the first two samples from the state buffer: x[n-N], x[n-N-1] */
  114. x0 = *__SIMD32(px)++;
  115. /* Read the third and forth samples from the state buffer: x[n-N-2], x[n-N-3] */
  116. x2 = *__SIMD32(px)++;
  117. /* Loop over the number of taps. Unroll by a factor of 4.
  118. ** Repeat until we've computed numTaps-(numTaps%4) coefficients. */
  119. tapCnt = numTaps >> 2;
  120. while(tapCnt > 0)
  121. {
  122. /* Read the first two coefficients using SIMD: b[N] and b[N-1] coefficients */
  123. c0 = *__SIMD32(pb)++;
  124. /* acc0 += b[N] * x[n-N] + b[N-1] * x[n-N-1] */
  125. acc0 = __SMLAD(x0, c0, acc0);
  126. /* acc2 += b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
  127. acc2 = __SMLAD(x2, c0, acc2);
  128. /* pack x[n-N-1] and x[n-N-2] */
  129. #ifndef ARM_MATH_BIG_ENDIAN
  130. x1 = __PKHBT(x2, x0, 0);
  131. #else
  132. x1 = __PKHBT(x0, x2, 0);
  133. #endif
  134. /* Read state x[n-N-4], x[n-N-5] */
  135. x0 = _SIMD32_OFFSET(px);
  136. /* acc1 += b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
  137. acc1 = __SMLADX(x1, c0, acc1);
  138. /* pack x[n-N-3] and x[n-N-4] */
  139. #ifndef ARM_MATH_BIG_ENDIAN
  140. x1 = __PKHBT(x0, x2, 0);
  141. #else
  142. x1 = __PKHBT(x2, x0, 0);
  143. #endif
  144. /* acc3 += b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
  145. acc3 = __SMLADX(x1, c0, acc3);
  146. /* Read coefficients b[N-2], b[N-3] */
  147. c0 = *__SIMD32(pb)++;
  148. /* acc0 += b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
  149. acc0 = __SMLAD(x2, c0, acc0);
  150. /* Read state x[n-N-6], x[n-N-7] with offset */
  151. x2 = _SIMD32_OFFSET(px + 2u);
  152. /* acc2 += b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
  153. acc2 = __SMLAD(x0, c0, acc2);
  154. /* acc1 += b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
  155. acc1 = __SMLADX(x1, c0, acc1);
  156. /* pack x[n-N-5] and x[n-N-6] */
  157. #ifndef ARM_MATH_BIG_ENDIAN
  158. x1 = __PKHBT(x2, x0, 0);
  159. #else
  160. x1 = __PKHBT(x0, x2, 0);
  161. #endif
  162. /* acc3 += b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
  163. acc3 = __SMLADX(x1, c0, acc3);
  164. /* Update state pointer for next state reading */
  165. px += 4u;
  166. /* Decrement tap count */
  167. tapCnt--;
  168. }
  169. /* If the filter length is not a multiple of 4, compute the remaining filter taps.
  170. ** This is always be 2 taps since the filter length is even. */
  171. if((numTaps & 0x3u) != 0u)
  172. {
  173. /* Read last two coefficients */
  174. c0 = *__SIMD32(pb)++;
  175. /* Perform the multiply-accumulates */
  176. acc0 = __SMLAD(x0, c0, acc0);
  177. acc2 = __SMLAD(x2, c0, acc2);
  178. /* pack state variables */
  179. #ifndef ARM_MATH_BIG_ENDIAN
  180. x1 = __PKHBT(x2, x0, 0);
  181. #else
  182. x1 = __PKHBT(x0, x2, 0);
  183. #endif
  184. /* Read last state variables */
  185. x0 = *__SIMD32(px);
  186. /* Perform the multiply-accumulates */
  187. acc1 = __SMLADX(x1, c0, acc1);
  188. /* pack state variables */
  189. #ifndef ARM_MATH_BIG_ENDIAN
  190. x1 = __PKHBT(x0, x2, 0);
  191. #else
  192. x1 = __PKHBT(x2, x0, 0);
  193. #endif
  194. /* Perform the multiply-accumulates */
  195. acc3 = __SMLADX(x1, c0, acc3);
  196. }
  197. /* The results in the 4 accumulators are in 2.30 format. Convert to 1.15 with saturation.
  198. ** Then store the 4 outputs in the destination buffer. */
  199. #ifndef ARM_MATH_BIG_ENDIAN
  200. *__SIMD32(pDst)++ =
  201. __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
  202. *__SIMD32(pDst)++ =
  203. __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
  204. #else
  205. *__SIMD32(pDst)++ =
  206. __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
  207. *__SIMD32(pDst)++ =
  208. __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
  209. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  210. /* Advance the state pointer by 4 to process the next group of 4 samples */
  211. pState = pState + 4u;
  212. /* Decrement the loop counter */
  213. blkCnt--;
  214. }
  215. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  216. ** No loop unrolling is used. */
  217. blkCnt = blockSize % 0x4u;
  218. while(blkCnt > 0u)
  219. {
  220. /* Copy two samples into state buffer */
  221. *pStateCurnt++ = *pSrc++;
  222. /* Set the accumulator to zero */
  223. acc0 = 0;
  224. /* Use SIMD to hold states and coefficients */
  225. px = pState;
  226. pb = pCoeffs;
  227. tapCnt = numTaps >> 1u;
  228. do
  229. {
  230. acc0 += (q31_t) * px++ * *pb++;
  231. acc0 += (q31_t) * px++ * *pb++;
  232. tapCnt--;
  233. }
  234. while(tapCnt > 0u);
  235. /* The result is in 2.30 format. Convert to 1.15 with saturation.
  236. ** Then store the output in the destination buffer. */
  237. *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16));
  238. /* Advance state pointer by 1 for the next sample */
  239. pState = pState + 1u;
  240. /* Decrement the loop counter */
  241. blkCnt--;
  242. }
  243. /* Processing is complete.
  244. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  245. ** This prepares the state buffer for the next function call. */
  246. /* Points to the start of the state buffer */
  247. pStateCurnt = S->pState;
  248. /* Calculation of count for copying integer writes */
  249. tapCnt = (numTaps - 1u) >> 2;
  250. while(tapCnt > 0u)
  251. {
  252. *pStateCurnt++ = *pState++;
  253. *pStateCurnt++ = *pState++;
  254. *pStateCurnt++ = *pState++;
  255. *pStateCurnt++ = *pState++;
  256. tapCnt--;
  257. }
  258. /* Calculation of count for remaining q15_t data */
  259. tapCnt = (numTaps - 1u) % 0x4u;
  260. /* copy remaining data */
  261. while(tapCnt > 0u)
  262. {
  263. *pStateCurnt++ = *pState++;
  264. /* Decrement the loop counter */
  265. tapCnt--;
  266. }
  267. }
  268. /**
  269. * @} end of FIR group
  270. */