- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# arm_fir_fast_q15.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------
    00002 * Copyright (C) 2010 ARM Limited. All rights reserved.
    00003 *
    00004 * $Date:        15. July 2011
    00005 * $Revision:    V1.0.10
    00006 *
    00007 * Project:      CMSIS DSP Library
    00008 * Title:        arm_fir_fast_q15.c
    00009 *
    00010 * Description:  Q15 Fast FIR filter processing function.
    00011 *
    00012 * Target Processor: Cortex-M4/Cortex-M3
    00013 *
    00014 * Version 1.0.10 2011/7/15
    00015 *    Big Endian support added and Merged M0 and M3/M4 Source code.
    00016 *
    00017 * Version 1.0.3 2010/11/29
    00018 *    Re-organized the CMSIS folders and updated documentation.
    00019 *
    00020 * Version 1.0.2 2010/11/11
    00021 *    Documentation updated.
    00022 *
    00023 * Version 1.0.1 2010/10/05
    00024 *    Production release and review comments incorporated.
    00025 *
    00026 * Version 1.0.0 2010/09/20
    00027 *    Production release and review comments incorporated.
    00028 *
    00029 * Version 0.0.9  2010/08/16
    00030 *    Initial version
    00031 *
    00032 * -------------------------------------------------------------------- */
    00033
    00034 #include "arm_math.h"
    00035
    00065 void arm_fir_fast_q15(
    00066   const arm_fir_instance_q15 * S,
    00067   q15_t * pSrc,
    00068   q15_t * pDst,
    00069   uint32_t blockSize)
    00070 {
    00071   q15_t *pState = S->pState;                     /* State pointer */
    00072   q15_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
    00073   q15_t *pStateCurnt;                            /* Points to the current sample of the state */
    00074   q15_t *px1;                                    /* Temporary q15 pointer for state buffer */
    00075   q31_t *pb;                                     /* Temporary pointer for coefficient buffer */
    00076   q31_t *px2;                                    /* Temporary q31 pointer for SIMD state buffer accesses */
    00077   q31_t x0, x1, x2, x3, c0;                      /* Temporary variables to hold SIMD state and coefficient values */
    00078   q31_t acc0, acc1, acc2, acc3;                  /* Accumulators */
    00079   uint32_t numTaps = S->numTaps;                 /* Number of taps in the filter */
    00080   uint32_t tapCnt, blkCnt;                       /* Loop counters */
    00081
    00082   /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
    00083   /* pStateCurnt points to the location where the new input data should be written */
    00084   pStateCurnt = &(S->pState[(numTaps - 1u)]);
    00085
    00086   /* Apply loop unrolling and compute 4 output values simultaneously.
    00087    * The variables acc0 ... acc3 hold output values that are being computed:
    00088    *
    00089    *    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]
    00090    *    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]
    00091    *    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]
    00092    *    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]
    00093    */
    00094   blkCnt = blockSize >> 2;
    00095
    00096   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
    00097    ** a second loop below computes the remaining 1 to 3 samples. */
    00098   while(blkCnt > 0u)
    00099   {
    00100     /* Copy four new input samples into the state buffer.
    00101      ** Use 32-bit SIMD to move the 16-bit data.  Only requires two copies. */
    00102     *__SIMD32(pStateCurnt)++ = *__SIMD32(pSrc)++;
    00103     *__SIMD32(pStateCurnt)++ = *__SIMD32(pSrc)++;
    00104
    00105     /* Set all accumulators to zero */
    00106     acc0 = 0;
    00107     acc1 = 0;
    00108     acc2 = 0;
    00109     acc3 = 0;
    00110
    00111     /* Initialize state pointer of type q15 */
    00112     px1 = pState;
    00113
    00114     /* Initialize coeff pointer of type q31 */
    00115     pb = (q31_t *) (pCoeffs);
    00116
    00117     /* Read the first two samples from the state buffer:  x[n-N], x[n-N-1] */
    00118     x0 = *(q31_t *) (px1++);
    00119
    00120     /* Read the third and forth samples from the state buffer: x[n-N-1], x[n-N-2] */
    00121     x1 = *(q31_t *) (px1++);
    00122
    00123     /* Loop over the number of taps.  Unroll by a factor of 4.
    00124      ** Repeat until we've computed numTaps-4 coefficients. */
    00125     tapCnt = numTaps >> 2;
    00126     do
    00127     {
    00128       /* Read the first two coefficients using SIMD:  b[N] and b[N-1] coefficients */
    00129       c0 = *(pb++);
    00130
    00131       /* acc0 +=  b[N] * x[n-N] + b[N-1] * x[n-N-1] */
    00132       acc0 = __SMLAD(x0, c0, acc0);
    00133
    00134       /* acc1 +=  b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
    00135       acc1 = __SMLAD(x1, c0, acc1);
    00136
    00137       /* Read state x[n-N-2], x[n-N-3] */
    00138       x2 = *(q31_t *) (px1++);
    00139
    00140       /* Read state x[n-N-3], x[n-N-4] */
    00141       x3 = *(q31_t *) (px1++);
    00142
    00143       /* acc2 +=  b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
    00144       acc2 = __SMLAD(x2, c0, acc2);
    00145
    00146       /* acc3 +=  b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
    00147       acc3 = __SMLAD(x3, c0, acc3);
    00148
    00149       /* Read coefficients b[N-2], b[N-3] */
    00150       c0 = *(pb++);
    00151
    00152       /* acc0 +=  b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
    00153       acc0 = __SMLAD(x2, c0, acc0);
    00154
    00155       /* acc1 +=  b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
    00156       acc1 = __SMLAD(x3, c0, acc1);
    00157
    00158       /* Read state x[n-N-4], x[n-N-5] */
    00159       x0 = *(q31_t *) (px1++);
    00160
    00161       /* Read state x[n-N-5], x[n-N-6] */
    00162       x1 = *(q31_t *) (px1++);
    00163
    00164       /* acc2 +=  b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
    00165       acc2 = __SMLAD(x0, c0, acc2);
    00166
    00167       /* acc3 +=  b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
    00168       acc3 = __SMLAD(x1, c0, acc3);
    00169       tapCnt--;
    00170
    00171     }
    00172     while(tapCnt > 0u);
    00173
    00174     /* If the filter length is not a multiple of 4, compute the remaining filter taps.
    00175      ** This is always 2 taps since the filter length is always even. */
    00176     if((numTaps & 0x3u) != 0u)
    00177     {
    00178       /* Read 2 coefficients */
    00179       c0 = *(pb++);
    00180       /* Fetch 4 state variables */
    00181       x2 = *(q31_t *) (px1++);
    00182       x3 = *(q31_t *) (px1++);
    00183
    00184       /* Perform the multiply-accumulates */
    00185       acc0 = __SMLAD(x0, c0, acc0);
    00186       acc1 = __SMLAD(x1, c0, acc1);
    00187       acc2 = __SMLAD(x2, c0, acc2);
    00188       acc3 = __SMLAD(x3, c0, acc3);
    00189     }
    00190
    00191     /* The results in the 4 accumulators are in 2.30 format.  Convert to 1.15 with saturation.
    00192      ** Then store the 4 outputs in the destination buffer. */
    00193
    00194 #ifndef ARM_MATH_BIG_ENDIAN
    00195
    00196     *__SIMD32(pDst)++ = __PKHBT((acc0 >> 15), (acc1 >> 15), 16u);
    00197     *__SIMD32(pDst)++ = __PKHBT((acc2 >> 15), (acc3 >> 15), 16u);
    00198
    00199 #else
    00200
    00201     *__SIMD32(pDst)++ = __PKHBT((acc1 >> 15), (acc0 >> 15), 16u);
    00202     *__SIMD32(pDst)++ = __PKHBT((acc3 >> 15), (acc2 >> 15), 16u);
    00203
    00204 #endif /*      #ifndef ARM_MATH_BIG_ENDIAN     */
    00205
    00206     /* Advance the state pointer by 4 to process the next group of 4 samples */
    00207     pState = pState + 4;
    00208
    00209     /* Decrement the loop counter */
    00210     blkCnt--;
    00211   }
    00212
    00213   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
    00214    ** No loop unrolling is used. */
    00215   blkCnt = blockSize % 0x4u;
    00216   while(blkCnt > 0u)
    00217   {
    00218     /* Copy two samples into state buffer */
    00219     *pStateCurnt++ = *pSrc++;
    00220
    00221     /* Set the accumulator to zero */
    00222     acc0 = 0;
    00223
    00224     /* Use SIMD to hold states and coefficients */
    00225     px2 = (q31_t *) pState;
    00226     pb = (q31_t *) (pCoeffs);
    00227     tapCnt = numTaps >> 1;
    00228
    00229     do
    00230     {
    00231       acc0 = __SMLAD(*px2++, *(pb++), acc0);
    00232       tapCnt--;
    00233     }
    00234     while(tapCnt > 0u);
    00235
    00236     /* The result is in 2.30 format.  Convert to 1.15 with saturation.
    00237      ** Then store the output in the destination buffer. */
    00238     *pDst++ = (q15_t) ((acc0 >> 15));
    00239
    00240     /* Advance state pointer by 1 for the next sample */
    00241     pState = pState + 1;
    00242
    00243     /* Decrement the loop counter */
    00244     blkCnt--;
    00245   }
    00246
    00247   /* Processing is complete.
    00248    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
    00249    ** This prepares the state buffer for the next function call. */
    00250
    00251   /* Points to the start of the state buffer */
    00252   pStateCurnt = S->pState;
    00253   /* Calculation of count for copying integer writes */
    00254   tapCnt = (numTaps - 1u) >> 2;
    00255
    00256   while(tapCnt > 0u)
    00257   {
    00258     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
    00259     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
    00260
    00261     tapCnt--;
    00262   }
    00263
    00264   /* Calculation of count for remaining q15_t data */
    00265   tapCnt = (numTaps - 1u) % 0x4u;
    00266
    00267   /* copy remaining data */
    00268   while(tapCnt > 0u)
    00269   {
    00270     *pStateCurnt++ = *pState++;
    00271
    00272     /* Decrement the loop counter */
    00273     tapCnt--;
    00274   }
    00275 }
    00276

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines