STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
arm_fir_fast_q31.c
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date: 19. March 2015
5 * $Revision: V.1.4.5
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_fast_q31.c
9 *
10 * Description: Processing function for the Q31 Fast FIR filter.
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 
41 #include "arm_math.h"
42 
74 IAR_ONLY_LOW_OPTIMIZATION_ENTER
76  const arm_fir_instance_q31 * S,
77  q31_t * pSrc,
78  q31_t * pDst,
79  uint32_t blockSize)
80 {
81  q31_t *pState = S->pState; /* State pointer */
82  q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
83  q31_t *pStateCurnt; /* Points to the current sample of the state */
84  q31_t x0, x1, x2, x3; /* Temporary variables to hold state */
85  q31_t c0; /* Temporary variable to hold coefficient value */
86  q31_t *px; /* Temporary pointer for state */
87  q31_t *pb; /* Temporary pointer for coefficient buffer */
88  q31_t acc0, acc1, acc2, acc3; /* Accumulators */
89  uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
90  uint32_t i, tapCnt, blkCnt; /* Loop counters */
91 
92  /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
93  /* pStateCurnt points to the location where the new input data should be written */
94  pStateCurnt = &(S->pState[(numTaps - 1u)]);
95 
96  /* Apply loop unrolling and compute 4 output values simultaneously.
97  * The variables acc0 ... acc3 hold output values that are being computed:
98  *
99  * 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]
100  * 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]
101  * 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]
102  * 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]
103  */
104  blkCnt = blockSize >> 2;
105 
106  /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
107  ** a second loop below computes the remaining 1 to 3 samples. */
108  while(blkCnt > 0u)
109  {
110  /* Copy four new input samples into the state buffer */
111  *pStateCurnt++ = *pSrc++;
112  *pStateCurnt++ = *pSrc++;
113  *pStateCurnt++ = *pSrc++;
114  *pStateCurnt++ = *pSrc++;
115 
116  /* Set all accumulators to zero */
117  acc0 = 0;
118  acc1 = 0;
119  acc2 = 0;
120  acc3 = 0;
121 
122  /* Initialize state pointer */
123  px = pState;
124 
125  /* Initialize coefficient pointer */
126  pb = pCoeffs;
127 
128  /* Read the first three samples from the state buffer:
129  * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */
130  x0 = *(px++);
131  x1 = *(px++);
132  x2 = *(px++);
133 
134  /* Loop unrolling. Process 4 taps at a time. */
135  tapCnt = numTaps >> 2;
136  i = tapCnt;
137 
138  while(i > 0u)
139  {
140  /* Read the b[numTaps] coefficient */
141  c0 = *pb;
142 
143  /* Read x[n-numTaps-3] sample */
144  x3 = *px;
145 
146  /* acc0 += b[numTaps] * x[n-numTaps] */
147  multAcc_32x32_keep32_R(acc0, x0, c0);
148 
149  /* acc1 += b[numTaps] * x[n-numTaps-1] */
150  multAcc_32x32_keep32_R(acc1, x1, c0);
151 
152  /* acc2 += b[numTaps] * x[n-numTaps-2] */
153  multAcc_32x32_keep32_R(acc2, x2, c0);
154 
155  /* acc3 += b[numTaps] * x[n-numTaps-3] */
156  multAcc_32x32_keep32_R(acc3, x3, c0);
157 
158  /* Read the b[numTaps-1] coefficient */
159  c0 = *(pb + 1u);
160 
161  /* Read x[n-numTaps-4] sample */
162  x0 = *(px + 1u);
163 
164  /* Perform the multiply-accumulates */
165  multAcc_32x32_keep32_R(acc0, x1, c0);
166  multAcc_32x32_keep32_R(acc1, x2, c0);
167  multAcc_32x32_keep32_R(acc2, x3, c0);
168  multAcc_32x32_keep32_R(acc3, x0, c0);
169 
170  /* Read the b[numTaps-2] coefficient */
171  c0 = *(pb + 2u);
172 
173  /* Read x[n-numTaps-5] sample */
174  x1 = *(px + 2u);
175 
176  /* Perform the multiply-accumulates */
177  multAcc_32x32_keep32_R(acc0, x2, c0);
178  multAcc_32x32_keep32_R(acc1, x3, c0);
179  multAcc_32x32_keep32_R(acc2, x0, c0);
180  multAcc_32x32_keep32_R(acc3, x1, c0);
181 
182  /* Read the b[numTaps-3] coefficients */
183  c0 = *(pb + 3u);
184 
185  /* Read x[n-numTaps-6] sample */
186  x2 = *(px + 3u);
187 
188  /* Perform the multiply-accumulates */
189  multAcc_32x32_keep32_R(acc0, x3, c0);
190  multAcc_32x32_keep32_R(acc1, x0, c0);
191  multAcc_32x32_keep32_R(acc2, x1, c0);
192  multAcc_32x32_keep32_R(acc3, x2, c0);
193 
194  /* update coefficient pointer */
195  pb += 4u;
196  px += 4u;
197 
198  /* Decrement the loop counter */
199  i--;
200  }
201 
202  /* If the filter length is not a multiple of 4, compute the remaining filter taps */
203 
204  i = numTaps - (tapCnt * 4u);
205  while(i > 0u)
206  {
207  /* Read coefficients */
208  c0 = *(pb++);
209 
210  /* Fetch 1 state variable */
211  x3 = *(px++);
212 
213  /* Perform the multiply-accumulates */
214  multAcc_32x32_keep32_R(acc0, x0, c0);
215  multAcc_32x32_keep32_R(acc1, x1, c0);
216  multAcc_32x32_keep32_R(acc2, x2, c0);
217  multAcc_32x32_keep32_R(acc3, x3, c0);
218 
219  /* Reuse the present sample states for next sample */
220  x0 = x1;
221  x1 = x2;
222  x2 = x3;
223 
224  /* Decrement the loop counter */
225  i--;
226  }
227 
228  /* Advance the state pointer by 4 to process the next group of 4 samples */
229  pState = pState + 4;
230 
231  /* The results in the 4 accumulators are in 2.30 format. Convert to 1.31
232  ** Then store the 4 outputs in the destination buffer. */
233  *pDst++ = (q31_t) (acc0 << 1);
234  *pDst++ = (q31_t) (acc1 << 1);
235  *pDst++ = (q31_t) (acc2 << 1);
236  *pDst++ = (q31_t) (acc3 << 1);
237 
238  /* Decrement the samples loop counter */
239  blkCnt--;
240  }
241 
242 
243  /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
244  ** No loop unrolling is used. */
245  blkCnt = blockSize % 4u;
246 
247  while(blkCnt > 0u)
248  {
249  /* Copy one sample at a time into state buffer */
250  *pStateCurnt++ = *pSrc++;
251 
252  /* Set the accumulator to zero */
253  acc0 = 0;
254 
255  /* Initialize state pointer */
256  px = pState;
257 
258  /* Initialize Coefficient pointer */
259  pb = (pCoeffs);
260 
261  i = numTaps;
262 
263  /* Perform the multiply-accumulates */
264  do
265  {
266  multAcc_32x32_keep32_R(acc0, (*px++), (*(pb++)));
267  i--;
268  } while(i > 0u);
269 
270  /* The result is in 2.30 format. Convert to 1.31
271  ** Then store the output in the destination buffer. */
272  *pDst++ = (q31_t) (acc0 << 1);
273 
274  /* Advance state pointer by 1 for the next sample */
275  pState = pState + 1;
276 
277  /* Decrement the samples loop counter */
278  blkCnt--;
279  }
280 
281  /* Processing is complete.
282  ** Now copy the last numTaps - 1 samples to the start of the state buffer.
283  ** This prepares the state buffer for the next function call. */
284 
285  /* Points to the start of the state buffer */
286  pStateCurnt = S->pState;
287 
288  /* Calculate remaining number of copies */
289  tapCnt = (numTaps - 1u);
290 
291  /* Copy the remaining q31_t data */
292  while(tapCnt > 0u)
293  {
294  *pStateCurnt++ = *pState++;
295 
296  /* Decrement the loop counter */
297  tapCnt--;
298  }
299 
300 
301 }
302 IAR_ONLY_LOW_OPTIMIZATION_EXIT
IAR_ONLY_LOW_OPTIMIZATION_ENTER void arm_fir_fast_q31(const arm_fir_instance_q31 *S, q31_t *pSrc, q31_t *pDst, uint32_t blockSize)
Processing function for the fast Q31 FIR filter for Cortex-M3 and Cortex-M4.
Instance structure for the Q31 FIR filter.
Definition: arm_math.h:1049
#define multAcc_32x32_keep32_R(a, x, y)
Definition: arm_math.h:7042
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition: arm_math.h:397