1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_lattice_q15.c
10 * Description: Q15 FIR lattice filter processing function.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
31 * -------------------------------------------------------------------- */
36 * @ingroup groupFilters
40 * @addtogroup FIR_Lattice
46 * @brief Processing function for the Q15 FIR lattice filter.
47 * @param[in] *S points to an instance of the Q15 FIR lattice structure.
48 * @param[in] *pSrc points to the block of input data.
49 * @param[out] *pDst points to the block of output data
50 * @param[in] blockSize number of samples to process.
54 void arm_fir_lattice_q15(
55 const arm_fir_lattice_instance_q15 * S,
60 q15_t *pState; /* State pointer */
61 q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
62 q15_t *px; /* temporary state pointer */
63 q15_t *pk; /* temporary coefficient pointer */
68 /* Run the below code for Cortex-M4 and Cortex-M3 */
70 q31_t fcurnt1, fnext1, gcurnt1 = 0, gnext1; /* temporary variables for first sample in loop unrolling */
71 q31_t fcurnt2, fnext2, gnext2; /* temporary variables for second sample in loop unrolling */
72 q31_t fcurnt3, fnext3, gnext3; /* temporary variables for third sample in loop unrolling */
73 q31_t fcurnt4, fnext4, gnext4; /* temporary variables for fourth sample in loop unrolling */
74 uint32_t numStages = S->numStages; /* Number of stages in the filter */
75 uint32_t blkCnt, stageCnt; /* temporary variables for counts */
77 pState = &S->pState[0];
79 blkCnt = blockSize >> 2u;
81 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
82 ** a second loop below computes the remaining 1 to 3 samples. */
86 /* Read two samples from input buffer */
91 /* Initialize coeff pointer */
94 /* Initialize state pointer */
97 /* Read g0(n-1) from state */
100 /* Process first sample for first tap */
101 /* f1(n) = f0(n) + K1 * g0(n-1) */
102 fnext1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fcurnt1;
103 fnext1 = __SSAT(fnext1, 16);
105 /* g1(n) = f0(n) * K1 + g0(n-1) */
106 gnext1 = (q31_t) ((fcurnt1 * (*pk)) >> 15u) + gcurnt1;
107 gnext1 = __SSAT(gnext1, 16);
109 /* Process second sample for first tap */
110 /* for sample 2 processing */
111 fnext2 = (q31_t) ((fcurnt1 * (*pk)) >> 15u) + fcurnt2;
112 fnext2 = __SSAT(fnext2, 16);
114 gnext2 = (q31_t) ((fcurnt2 * (*pk)) >> 15u) + fcurnt1;
115 gnext2 = __SSAT(gnext2, 16);
118 /* Read next two samples from input buffer */
119 /* f0(n+2) = x(n+2) */
123 /* Copy only last input samples into the state buffer
124 which is used for next four samples processing */
125 *px++ = (q15_t) fcurnt4;
127 /* Process third sample for first tap */
128 fnext3 = (q31_t) ((fcurnt2 * (*pk)) >> 15u) + fcurnt3;
129 fnext3 = __SSAT(fnext3, 16);
130 gnext3 = (q31_t) ((fcurnt3 * (*pk)) >> 15u) + fcurnt2;
131 gnext3 = __SSAT(gnext3, 16);
133 /* Process fourth sample for first tap */
134 fnext4 = (q31_t) ((fcurnt3 * (*pk)) >> 15u) + fcurnt4;
135 fnext4 = __SSAT(fnext4, 16);
136 gnext4 = (q31_t) ((fcurnt4 * (*pk++)) >> 15u) + fcurnt3;
137 gnext4 = __SSAT(gnext4, 16);
139 /* Update of f values for next coefficient set processing */
146 /* Loop unrolling. Process 4 taps at a time . */
147 stageCnt = (numStages - 1u) >> 2;
150 /* Loop over the number of taps. Unroll by a factor of 4.
151 ** Repeat until we've computed numStages-3 coefficients. */
153 /* Process 2nd, 3rd, 4th and 5th taps ... here */
156 /* Read g1(n-1), g3(n-1) .... from state */
159 /* save g1(n) in state buffer */
160 *px++ = (q15_t) gnext4;
162 /* Process first sample for 2nd, 6th .. tap */
163 /* Sample processing for K2, K6.... */
164 /* f1(n) = f0(n) + K1 * g0(n-1) */
165 fnext1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fcurnt1;
166 fnext1 = __SSAT(fnext1, 16);
169 /* Process second sample for 2nd, 6th .. tap */
170 /* for sample 2 processing */
171 fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15u) + fcurnt2;
172 fnext2 = __SSAT(fnext2, 16);
173 /* Process third sample for 2nd, 6th .. tap */
174 fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15u) + fcurnt3;
175 fnext3 = __SSAT(fnext3, 16);
176 /* Process fourth sample for 2nd, 6th .. tap */
177 /* fnext4 = fcurnt4 + (*pk) * gnext3; */
178 fnext4 = (q31_t) ((gnext3 * (*pk)) >> 15u) + fcurnt4;
179 fnext4 = __SSAT(fnext4, 16);
181 /* g1(n) = f0(n) * K1 + g0(n-1) */
182 /* Calculation of state values for next stage */
183 gnext4 = (q31_t) ((fcurnt4 * (*pk)) >> 15u) + gnext3;
184 gnext4 = __SSAT(gnext4, 16);
185 gnext3 = (q31_t) ((fcurnt3 * (*pk)) >> 15u) + gnext2;
186 gnext3 = __SSAT(gnext3, 16);
188 gnext2 = (q31_t) ((fcurnt2 * (*pk)) >> 15u) + gnext1;
189 gnext2 = __SSAT(gnext2, 16);
191 gnext1 = (q31_t) ((fcurnt1 * (*pk++)) >> 15u) + gcurnt1;
192 gnext1 = __SSAT(gnext1, 16);
195 /* Read g2(n-1), g4(n-1) .... from state */
198 /* save g1(n) in state buffer */
199 *px++ = (q15_t) gnext4;
201 /* Sample processing for K3, K7.... */
202 /* Process first sample for 3rd, 7th .. tap */
203 /* f3(n) = f2(n) + K3 * g2(n-1) */
204 fcurnt1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fnext1;
205 fcurnt1 = __SSAT(fcurnt1, 16);
207 /* Process second sample for 3rd, 7th .. tap */
208 fcurnt2 = (q31_t) ((gnext1 * (*pk)) >> 15u) + fnext2;
209 fcurnt2 = __SSAT(fcurnt2, 16);
211 /* Process third sample for 3rd, 7th .. tap */
212 fcurnt3 = (q31_t) ((gnext2 * (*pk)) >> 15u) + fnext3;
213 fcurnt3 = __SSAT(fcurnt3, 16);
215 /* Process fourth sample for 3rd, 7th .. tap */
216 fcurnt4 = (q31_t) ((gnext3 * (*pk)) >> 15u) + fnext4;
217 fcurnt4 = __SSAT(fcurnt4, 16);
219 /* Calculation of state values for next stage */
220 /* g3(n) = f2(n) * K3 + g2(n-1) */
221 gnext4 = (q31_t) ((fnext4 * (*pk)) >> 15u) + gnext3;
222 gnext4 = __SSAT(gnext4, 16);
224 gnext3 = (q31_t) ((fnext3 * (*pk)) >> 15u) + gnext2;
225 gnext3 = __SSAT(gnext3, 16);
227 gnext2 = (q31_t) ((fnext2 * (*pk)) >> 15u) + gnext1;
228 gnext2 = __SSAT(gnext2, 16);
230 gnext1 = (q31_t) ((fnext1 * (*pk++)) >> 15u) + gcurnt1;
231 gnext1 = __SSAT(gnext1, 16);
233 /* Read g1(n-1), g3(n-1) .... from state */
236 /* save g1(n) in state buffer */
237 *px++ = (q15_t) gnext4;
239 /* Sample processing for K4, K8.... */
240 /* Process first sample for 4th, 8th .. tap */
241 /* f4(n) = f3(n) + K4 * g3(n-1) */
242 fnext1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fcurnt1;
243 fnext1 = __SSAT(fnext1, 16);
245 /* Process second sample for 4th, 8th .. tap */
246 /* for sample 2 processing */
247 fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15u) + fcurnt2;
248 fnext2 = __SSAT(fnext2, 16);
250 /* Process third sample for 4th, 8th .. tap */
251 fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15u) + fcurnt3;
252 fnext3 = __SSAT(fnext3, 16);
254 /* Process fourth sample for 4th, 8th .. tap */
255 fnext4 = (q31_t) ((gnext3 * (*pk)) >> 15u) + fcurnt4;
256 fnext4 = __SSAT(fnext4, 16);
258 /* g4(n) = f3(n) * K4 + g3(n-1) */
259 /* Calculation of state values for next stage */
260 gnext4 = (q31_t) ((fcurnt4 * (*pk)) >> 15u) + gnext3;
261 gnext4 = __SSAT(gnext4, 16);
263 gnext3 = (q31_t) ((fcurnt3 * (*pk)) >> 15u) + gnext2;
264 gnext3 = __SSAT(gnext3, 16);
266 gnext2 = (q31_t) ((fcurnt2 * (*pk)) >> 15u) + gnext1;
267 gnext2 = __SSAT(gnext2, 16);
268 gnext1 = (q31_t) ((fcurnt1 * (*pk++)) >> 15u) + gcurnt1;
269 gnext1 = __SSAT(gnext1, 16);
272 /* Read g2(n-1), g4(n-1) .... from state */
275 /* save g4(n) in state buffer */
276 *px++ = (q15_t) gnext4;
278 /* Sample processing for K5, K9.... */
279 /* Process first sample for 5th, 9th .. tap */
280 /* f5(n) = f4(n) + K5 * g4(n-1) */
281 fcurnt1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fnext1;
282 fcurnt1 = __SSAT(fcurnt1, 16);
284 /* Process second sample for 5th, 9th .. tap */
285 fcurnt2 = (q31_t) ((gnext1 * (*pk)) >> 15u) + fnext2;
286 fcurnt2 = __SSAT(fcurnt2, 16);
288 /* Process third sample for 5th, 9th .. tap */
289 fcurnt3 = (q31_t) ((gnext2 * (*pk)) >> 15u) + fnext3;
290 fcurnt3 = __SSAT(fcurnt3, 16);
292 /* Process fourth sample for 5th, 9th .. tap */
293 fcurnt4 = (q31_t) ((gnext3 * (*pk)) >> 15u) + fnext4;
294 fcurnt4 = __SSAT(fcurnt4, 16);
296 /* Calculation of state values for next stage */
297 /* g5(n) = f4(n) * K5 + g4(n-1) */
298 gnext4 = (q31_t) ((fnext4 * (*pk)) >> 15u) + gnext3;
299 gnext4 = __SSAT(gnext4, 16);
300 gnext3 = (q31_t) ((fnext3 * (*pk)) >> 15u) + gnext2;
301 gnext3 = __SSAT(gnext3, 16);
302 gnext2 = (q31_t) ((fnext2 * (*pk)) >> 15u) + gnext1;
303 gnext2 = __SSAT(gnext2, 16);
304 gnext1 = (q31_t) ((fnext1 * (*pk++)) >> 15u) + gcurnt1;
305 gnext1 = __SSAT(gnext1, 16);
310 /* If the (filter length -1) is not a multiple of 4, compute the remaining filter taps */
311 stageCnt = (numStages - 1u) % 0x4u;
317 /* save g value in state buffer */
318 *px++ = (q15_t) gnext4;
320 /* Process four samples for last three taps here */
321 fnext1 = (q31_t) ((gcurnt1 * (*pk)) >> 15u) + fcurnt1;
322 fnext1 = __SSAT(fnext1, 16);
323 fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15u) + fcurnt2;
324 fnext2 = __SSAT(fnext2, 16);
326 fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15u) + fcurnt3;
327 fnext3 = __SSAT(fnext3, 16);
329 fnext4 = (q31_t) ((gnext3 * (*pk)) >> 15u) + fcurnt4;
330 fnext4 = __SSAT(fnext4, 16);
332 /* g1(n) = f0(n) * K1 + g0(n-1) */
333 gnext4 = (q31_t) ((fcurnt4 * (*pk)) >> 15u) + gnext3;
334 gnext4 = __SSAT(gnext4, 16);
335 gnext3 = (q31_t) ((fcurnt3 * (*pk)) >> 15u) + gnext2;
336 gnext3 = __SSAT(gnext3, 16);
337 gnext2 = (q31_t) ((fcurnt2 * (*pk)) >> 15u) + gnext1;
338 gnext2 = __SSAT(gnext2, 16);
339 gnext1 = (q31_t) ((fcurnt1 * (*pk++)) >> 15u) + gcurnt1;
340 gnext1 = __SSAT(gnext1, 16);
342 /* Update of f values for next coefficient set processing */
352 /* The results in the 4 accumulators, store in the destination buffer. */
355 #ifndef ARM_MATH_BIG_ENDIAN
357 *__SIMD32(pDst)++ = __PKHBT(fcurnt1, fcurnt2, 16);
358 *__SIMD32(pDst)++ = __PKHBT(fcurnt3, fcurnt4, 16);
362 *__SIMD32(pDst)++ = __PKHBT(fcurnt2, fcurnt1, 16);
363 *__SIMD32(pDst)++ = __PKHBT(fcurnt4, fcurnt3, 16);
365 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
370 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
371 ** No loop unrolling is used. */
372 blkCnt = blockSize % 0x4u;
379 /* Initialize coeff pointer */
382 /* Initialize state pointer */
385 /* read g2(n) from state buffer */
388 /* for sample 1 processing */
389 /* f1(n) = f0(n) + K1 * g0(n-1) */
390 fnext1 = (((q31_t) gcurnt1 * (*pk)) >> 15u) + fcurnt1;
391 fnext1 = __SSAT(fnext1, 16);
394 /* g1(n) = f0(n) * K1 + g0(n-1) */
395 gnext1 = (((q31_t) fcurnt1 * (*pk++)) >> 15u) + gcurnt1;
396 gnext1 = __SSAT(gnext1, 16);
398 /* save g1(n) in state buffer */
399 *px++ = (q15_t) fcurnt1;
401 /* f1(n) is saved in fcurnt1
402 for next stage processing */
405 stageCnt = (numStages - 1u);
410 /* read g2(n) from state buffer */
413 /* save g1(n) in state buffer */
414 *px++ = (q15_t) gnext1;
416 /* Sample processing for K2, K3.... */
417 /* f2(n) = f1(n) + K2 * g1(n-1) */
418 fnext1 = (((q31_t) gcurnt1 * (*pk)) >> 15u) + fcurnt1;
419 fnext1 = __SSAT(fnext1, 16);
421 /* g2(n) = f1(n) * K2 + g1(n-1) */
422 gnext1 = (((q31_t) fcurnt1 * (*pk++)) >> 15u) + gcurnt1;
423 gnext1 = __SSAT(gnext1, 16);
426 /* f1(n) is saved in fcurnt1
427 for next stage processing */
435 *pDst++ = __SSAT(fcurnt1, 16);
444 /* Run the below code for Cortex-M0 */
446 q31_t fcurnt, fnext, gcurnt, gnext; /* temporary variables */
447 uint32_t numStages = S->numStages; /* Length of the filter */
448 uint32_t blkCnt, stageCnt; /* temporary variables for counts */
450 pState = &S->pState[0];
459 /* Initialize coeff pointer */
462 /* Initialize state pointer */
465 /* read g0(n-1) from state buffer */
468 /* for sample 1 processing */
469 /* f1(n) = f0(n) + K1 * g0(n-1) */
470 fnext = ((gcurnt * (*pk)) >> 15u) + fcurnt;
471 fnext = __SSAT(fnext, 16);
474 /* g1(n) = f0(n) * K1 + g0(n-1) */
475 gnext = ((fcurnt * (*pk++)) >> 15u) + gcurnt;
476 gnext = __SSAT(gnext, 16);
478 /* save f0(n) in state buffer */
479 *px++ = (q15_t) fcurnt;
481 /* f1(n) is saved in fcurnt
482 for next stage processing */
485 stageCnt = (numStages - 1u);
490 /* read g1(n-1) from state buffer */
493 /* save g0(n-1) in state buffer */
494 *px++ = (q15_t) gnext;
496 /* Sample processing for K2, K3.... */
497 /* f2(n) = f1(n) + K2 * g1(n-1) */
498 fnext = ((gcurnt * (*pk)) >> 15u) + fcurnt;
499 fnext = __SSAT(fnext, 16);
501 /* g2(n) = f1(n) * K2 + g1(n-1) */
502 gnext = ((fcurnt * (*pk++)) >> 15u) + gcurnt;
503 gnext = __SSAT(gnext, 16);
506 /* f1(n) is saved in fcurnt
507 for next stage processing */
515 *pDst++ = __SSAT(fcurnt, 16);
522 #endif /* #ifndef ARM_MATH_CM0 */
527 * @} end of FIR_Lattice group