]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_fir_q15.c
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
10 * Description: Q15 FIR filter processing function.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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
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.
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 * -------------------------------------------------------------------- */
44 * @ingroup groupFilters
53 * @brief Processing function for the Q15 FIR filter.
54 * @param[in] *S points to an instance of the Q15 FIR structure.
55 * @param[in] *pSrc points to the block of input data.
56 * @param[out] *pDst points to the block of output data.
57 * @param[in] blockSize number of samples to process per call.
62 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
63 * In this case input, output, state buffers should be aligned by 32-bit
65 * <b>Scaling and Overflow Behavior:</b>
67 * The function is implemented using a 64-bit internal accumulator.
68 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
69 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
70 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
71 * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
72 * Lastly, the accumulator is saturated to yield a result in 1.15 format.
75 * Refer to the function <code>arm_fir_fast_q15()</code> for a faster but less precise implementation of this function.
78 #ifndef ARM_MATH_CM0_FAMILY
80 /* Run the below code for Cortex-M4 and Cortex-M3 */
82 #ifndef UNALIGNED_SUPPORT_DISABLE
86 const arm_fir_instance_q15
* S
,
91 q15_t
*pState
= S
->pState
; /* State pointer */
92 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
93 q15_t
*pStateCurnt
; /* Points to the current sample of the state */
94 q15_t
*px1
; /* Temporary q15 pointer for state buffer */
95 q15_t
*pb
; /* Temporary pointer for coefficient buffer */
96 q31_t x0
, x1
, x2
, x3
, c0
; /* Temporary variables to hold SIMD state and coefficient values */
97 q63_t acc0
, acc1
, acc2
, acc3
; /* Accumulators */
98 uint32_t numTaps
= S
->numTaps
; /* Number of taps in the filter */
99 uint32_t tapCnt
, blkCnt
; /* Loop counters */
102 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
103 /* pStateCurnt points to the location where the new input data should be written */
104 pStateCurnt
= &(S
->pState
[(numTaps
- 1u)]);
106 /* Apply loop unrolling and compute 4 output values simultaneously.
107 * The variables acc0 ... acc3 hold output values that are being computed:
109 * 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]
110 * 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]
111 * 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]
112 * 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]
115 blkCnt
= blockSize
>> 2;
117 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
118 ** a second loop below computes the remaining 1 to 3 samples. */
121 /* Copy four new input samples into the state buffer.
122 ** Use 32-bit SIMD to move the 16-bit data. Only requires two copies. */
123 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pSrc
)++;
124 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pSrc
)++;
126 /* Set all accumulators to zero */
132 /* Initialize state pointer of type q15 */
135 /* Initialize coeff pointer of type q31 */
138 /* Read the first two samples from the state buffer: x[n-N], x[n-N-1] */
139 x0
= _SIMD32_OFFSET(px1
);
141 /* Read the third and forth samples from the state buffer: x[n-N-1], x[n-N-2] */
142 x1
= _SIMD32_OFFSET(px1
+ 1u);
146 /* Loop over the number of taps. Unroll by a factor of 4.
147 ** Repeat until we've computed numTaps-4 coefficients. */
148 tapCnt
= numTaps
>> 2;
152 /* Read the first two coefficients using SIMD: b[N] and b[N-1] coefficients */
153 c0
= *__SIMD32(pb
)++;
155 /* acc0 += b[N] * x[n-N] + b[N-1] * x[n-N-1] */
156 acc0
= __SMLALD(x0
, c0
, acc0
);
158 /* acc1 += b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
159 acc1
= __SMLALD(x1
, c0
, acc1
);
161 /* Read state x[n-N-2], x[n-N-3] */
162 x2
= _SIMD32_OFFSET(px1
);
164 /* Read state x[n-N-3], x[n-N-4] */
165 x3
= _SIMD32_OFFSET(px1
+ 1u);
167 /* acc2 += b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
168 acc2
= __SMLALD(x2
, c0
, acc2
);
170 /* acc3 += b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
171 acc3
= __SMLALD(x3
, c0
, acc3
);
173 /* Read coefficients b[N-2], b[N-3] */
174 c0
= *__SIMD32(pb
)++;
176 /* acc0 += b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
177 acc0
= __SMLALD(x2
, c0
, acc0
);
179 /* acc1 += b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
180 acc1
= __SMLALD(x3
, c0
, acc1
);
182 /* Read state x[n-N-4], x[n-N-5] */
183 x0
= _SIMD32_OFFSET(px1
+ 2u);
185 /* Read state x[n-N-5], x[n-N-6] */
186 x1
= _SIMD32_OFFSET(px1
+ 3u);
188 /* acc2 += b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
189 acc2
= __SMLALD(x0
, c0
, acc2
);
191 /* acc3 += b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
192 acc3
= __SMLALD(x1
, c0
, acc3
);
201 /* If the filter length is not a multiple of 4, compute the remaining filter taps.
202 ** This is always be 2 taps since the filter length is even. */
203 if((numTaps
& 0x3u
) != 0u)
205 /* Read 2 coefficients */
206 c0
= *__SIMD32(pb
)++;
208 /* Fetch 4 state variables */
209 x2
= _SIMD32_OFFSET(px1
);
211 x3
= _SIMD32_OFFSET(px1
+ 1u);
213 /* Perform the multiply-accumulates */
214 acc0
= __SMLALD(x0
, c0
, acc0
);
218 acc1
= __SMLALD(x1
, c0
, acc1
);
219 acc2
= __SMLALD(x2
, c0
, acc2
);
220 acc3
= __SMLALD(x3
, c0
, acc3
);
223 /* The results in the 4 accumulators are in 2.30 format. Convert to 1.15 with saturation.
224 ** Then store the 4 outputs in the destination buffer. */
226 #ifndef ARM_MATH_BIG_ENDIAN
229 __PKHBT(__SSAT((acc0
>> 15), 16), __SSAT((acc1
>> 15), 16), 16);
231 __PKHBT(__SSAT((acc2
>> 15), 16), __SSAT((acc3
>> 15), 16), 16);
236 __PKHBT(__SSAT((acc1
>> 15), 16), __SSAT((acc0
>> 15), 16), 16);
238 __PKHBT(__SSAT((acc3
>> 15), 16), __SSAT((acc2
>> 15), 16), 16);
240 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
244 /* Advance the state pointer by 4 to process the next group of 4 samples */
247 /* Decrement the loop counter */
251 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
252 ** No loop unrolling is used. */
253 blkCnt
= blockSize
% 0x4u
;
256 /* Copy two samples into state buffer */
257 *pStateCurnt
++ = *pSrc
++;
259 /* Set the accumulator to zero */
262 /* Initialize state pointer of type q15 */
265 /* Initialize coeff pointer of type q31 */
268 tapCnt
= numTaps
>> 1;
273 c0
= *__SIMD32(pb
)++;
274 x0
= *__SIMD32(px1
)++;
276 acc0
= __SMLALD(x0
, c0
, acc0
);
281 /* The result is in 2.30 format. Convert to 1.15 with saturation.
282 ** Then store the output in the destination buffer. */
283 *pDst
++ = (q15_t
) (__SSAT((acc0
>> 15), 16));
285 /* Advance state pointer by 1 for the next sample */
288 /* Decrement the loop counter */
292 /* Processing is complete.
293 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
294 ** This prepares the state buffer for the next function call. */
296 /* Points to the start of the state buffer */
297 pStateCurnt
= S
->pState
;
299 /* Calculation of count for copying integer writes */
300 tapCnt
= (numTaps
- 1u) >> 2;
305 /* Copy state values to start of state buffer */
306 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pState
)++;
307 *__SIMD32(pStateCurnt
)++ = *__SIMD32(pState
)++;
313 /* Calculation of count for remaining q15_t data */
314 tapCnt
= (numTaps
- 1u) % 0x4u
;
316 /* copy remaining data */
319 *pStateCurnt
++ = *pState
++;
321 /* Decrement the loop counter */
326 #else /* UNALIGNED_SUPPORT_DISABLE */
329 const arm_fir_instance_q15
* S
,
334 q15_t
*pState
= S
->pState
; /* State pointer */
335 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
336 q15_t
*pStateCurnt
; /* Points to the current sample of the state */
337 q63_t acc0
, acc1
, acc2
, acc3
; /* Accumulators */
338 q15_t
*pb
; /* Temporary pointer for coefficient buffer */
339 q15_t
*px
; /* Temporary q31 pointer for SIMD state buffer accesses */
340 q31_t x0
, x1
, x2
, c0
; /* Temporary variables to hold SIMD state and coefficient values */
341 uint32_t numTaps
= S
->numTaps
; /* Number of taps in the filter */
342 uint32_t tapCnt
, blkCnt
; /* Loop counters */
345 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
346 /* pStateCurnt points to the location where the new input data should be written */
347 pStateCurnt
= &(S
->pState
[(numTaps
- 1u)]);
349 /* Apply loop unrolling and compute 4 output values simultaneously.
350 * The variables acc0 ... acc3 hold output values that are being computed:
352 * 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]
353 * 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]
354 * 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]
355 * 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]
358 blkCnt
= blockSize
>> 2;
360 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
361 ** a second loop below computes the remaining 1 to 3 samples. */
364 /* Copy four new input samples into the state buffer.
365 ** Use 32-bit SIMD to move the 16-bit data. Only requires two copies. */
366 *pStateCurnt
++ = *pSrc
++;
367 *pStateCurnt
++ = *pSrc
++;
368 *pStateCurnt
++ = *pSrc
++;
369 *pStateCurnt
++ = *pSrc
++;
372 /* Set all accumulators to zero */
378 /* Typecast q15_t pointer to q31_t pointer for state reading in q31_t */
381 /* Typecast q15_t pointer to q31_t pointer for coefficient reading in q31_t */
384 /* Read the first two samples from the state buffer: x[n-N], x[n-N-1] */
385 x0
= *__SIMD32(px
)++;
387 /* Read the third and forth samples from the state buffer: x[n-N-2], x[n-N-3] */
388 x2
= *__SIMD32(px
)++;
390 /* Loop over the number of taps. Unroll by a factor of 4.
391 ** Repeat until we've computed numTaps-(numTaps%4) coefficients. */
392 tapCnt
= numTaps
>> 2;
396 /* Read the first two coefficients using SIMD: b[N] and b[N-1] coefficients */
397 c0
= *__SIMD32(pb
)++;
399 /* acc0 += b[N] * x[n-N] + b[N-1] * x[n-N-1] */
400 acc0
= __SMLALD(x0
, c0
, acc0
);
402 /* acc2 += b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
403 acc2
= __SMLALD(x2
, c0
, acc2
);
405 /* pack x[n-N-1] and x[n-N-2] */
406 #ifndef ARM_MATH_BIG_ENDIAN
407 x1
= __PKHBT(x2
, x0
, 0);
409 x1
= __PKHBT(x0
, x2
, 0);
412 /* Read state x[n-N-4], x[n-N-5] */
413 x0
= _SIMD32_OFFSET(px
);
415 /* acc1 += b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
416 acc1
= __SMLALDX(x1
, c0
, acc1
);
418 /* pack x[n-N-3] and x[n-N-4] */
419 #ifndef ARM_MATH_BIG_ENDIAN
420 x1
= __PKHBT(x0
, x2
, 0);
422 x1
= __PKHBT(x2
, x0
, 0);
425 /* acc3 += b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
426 acc3
= __SMLALDX(x1
, c0
, acc3
);
428 /* Read coefficients b[N-2], b[N-3] */
429 c0
= *__SIMD32(pb
)++;
431 /* acc0 += b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
432 acc0
= __SMLALD(x2
, c0
, acc0
);
434 /* Read state x[n-N-6], x[n-N-7] with offset */
435 x2
= _SIMD32_OFFSET(px
+ 2u);
437 /* acc2 += b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
438 acc2
= __SMLALD(x0
, c0
, acc2
);
440 /* acc1 += b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
441 acc1
= __SMLALDX(x1
, c0
, acc1
);
443 /* pack x[n-N-5] and x[n-N-6] */
444 #ifndef ARM_MATH_BIG_ENDIAN
445 x1
= __PKHBT(x2
, x0
, 0);
447 x1
= __PKHBT(x0
, x2
, 0);
450 /* acc3 += b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
451 acc3
= __SMLALDX(x1
, c0
, acc3
);
453 /* Update state pointer for next state reading */
456 /* Decrement tap count */
461 /* If the filter length is not a multiple of 4, compute the remaining filter taps.
462 ** This is always be 2 taps since the filter length is even. */
463 if((numTaps
& 0x3u
) != 0u)
466 /* Read last two coefficients */
467 c0
= *__SIMD32(pb
)++;
469 /* Perform the multiply-accumulates */
470 acc0
= __SMLALD(x0
, c0
, acc0
);
471 acc2
= __SMLALD(x2
, c0
, acc2
);
473 /* pack state variables */
474 #ifndef ARM_MATH_BIG_ENDIAN
475 x1
= __PKHBT(x2
, x0
, 0);
477 x1
= __PKHBT(x0
, x2
, 0);
480 /* Read last state variables */
483 /* Perform the multiply-accumulates */
484 acc1
= __SMLALDX(x1
, c0
, acc1
);
486 /* pack state variables */
487 #ifndef ARM_MATH_BIG_ENDIAN
488 x1
= __PKHBT(x0
, x2
, 0);
490 x1
= __PKHBT(x2
, x0
, 0);
493 /* Perform the multiply-accumulates */
494 acc3
= __SMLALDX(x1
, c0
, acc3
);
497 /* The results in the 4 accumulators are in 2.30 format. Convert to 1.15 with saturation.
498 ** Then store the 4 outputs in the destination buffer. */
500 #ifndef ARM_MATH_BIG_ENDIAN
503 __PKHBT(__SSAT((acc0
>> 15), 16), __SSAT((acc1
>> 15), 16), 16);
506 __PKHBT(__SSAT((acc2
>> 15), 16), __SSAT((acc3
>> 15), 16), 16);
511 __PKHBT(__SSAT((acc1
>> 15), 16), __SSAT((acc0
>> 15), 16), 16);
514 __PKHBT(__SSAT((acc3
>> 15), 16), __SSAT((acc2
>> 15), 16), 16);
516 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
518 /* Advance the state pointer by 4 to process the next group of 4 samples */
521 /* Decrement the loop counter */
525 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
526 ** No loop unrolling is used. */
527 blkCnt
= blockSize
% 0x4u
;
530 /* Copy two samples into state buffer */
531 *pStateCurnt
++ = *pSrc
++;
533 /* Set the accumulator to zero */
536 /* Use SIMD to hold states and coefficients */
540 tapCnt
= numTaps
>> 1u;
544 acc0
+= (q31_t
) * px
++ * *pb
++;
545 acc0
+= (q31_t
) * px
++ * *pb
++;
550 /* The result is in 2.30 format. Convert to 1.15 with saturation.
551 ** Then store the output in the destination buffer. */
552 *pDst
++ = (q15_t
) (__SSAT((acc0
>> 15), 16));
554 /* Advance state pointer by 1 for the next sample */
555 pState
= pState
+ 1u;
557 /* Decrement the loop counter */
561 /* Processing is complete.
562 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
563 ** This prepares the state buffer for the next function call. */
565 /* Points to the start of the state buffer */
566 pStateCurnt
= S
->pState
;
568 /* Calculation of count for copying integer writes */
569 tapCnt
= (numTaps
- 1u) >> 2;
573 *pStateCurnt
++ = *pState
++;
574 *pStateCurnt
++ = *pState
++;
575 *pStateCurnt
++ = *pState
++;
576 *pStateCurnt
++ = *pState
++;
582 /* Calculation of count for remaining q15_t data */
583 tapCnt
= (numTaps
- 1u) % 0x4u
;
585 /* copy remaining data */
588 *pStateCurnt
++ = *pState
++;
590 /* Decrement the loop counter */
596 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
598 #else /* ARM_MATH_CM0_FAMILY */
601 /* Run the below code for Cortex-M0 */
604 const arm_fir_instance_q15
* S
,
609 q15_t
*pState
= S
->pState
; /* State pointer */
610 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
611 q15_t
*pStateCurnt
; /* Points to the current sample of the state */
615 q15_t
*px
; /* Temporary pointer for state buffer */
616 q15_t
*pb
; /* Temporary pointer for coefficient buffer */
617 q63_t acc
; /* Accumulator */
618 uint32_t numTaps
= S
->numTaps
; /* Number of nTaps in the filter */
619 uint32_t tapCnt
, blkCnt
; /* Loop counters */
621 /* S->pState buffer contains previous frame (numTaps - 1) samples */
622 /* pStateCurnt points to the location where the new input data should be written */
623 pStateCurnt
= &(S
->pState
[(numTaps
- 1u)]);
625 /* Initialize blkCnt with blockSize */
630 /* Copy one sample at a time into state buffer */
631 *pStateCurnt
++ = *pSrc
++;
633 /* Set the accumulator to zero */
636 /* Initialize state pointer */
639 /* Initialize Coefficient pointer */
644 /* Perform the multiply-accumulates */
647 /* acc = 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] */
648 acc
+= (q31_t
) * px
++ * *pb
++;
650 } while(tapCnt
> 0u);
652 /* The result is in 2.30 format. Convert to 1.15
653 ** Then store the output in the destination buffer. */
654 *pDst
++ = (q15_t
) __SSAT((acc
>> 15u), 16);
656 /* Advance state pointer by 1 for the next sample */
659 /* Decrement the samples loop counter */
663 /* Processing is complete.
664 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
665 ** This prepares the state buffer for the next function call. */
667 /* Points to the start of the state buffer */
668 pStateCurnt
= S
->pState
;
670 /* Copy numTaps number of values */
671 tapCnt
= (numTaps
- 1u);
676 *pStateCurnt
++ = *pState
++;
678 /* Decrement the loop counter */
684 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
690 * @} end of FIR group