Subversion Repositories canSerial

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_fir_q15.c
  4.  * Description:  Q15 FIR filter processing function
  5.  *
  6.  * $Date:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13.  *
  14.  * SPDX-License-Identifier: Apache-2.0
  15.  *
  16.  * Licensed under the Apache License, Version 2.0 (the License); you may
  17.  * not use this file except in compliance with the License.
  18.  * You may obtain a copy of the License at
  19.  *
  20.  * www.apache.org/licenses/LICENSE-2.0
  21.  *
  22.  * Unless required by applicable law or agreed to in writing, software
  23.  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25.  * See the License for the specific language governing permissions and
  26.  * limitations under the License.
  27.  */
  28.  
  29. #include "arm_math.h"
  30.  
  31. /**
  32.  * @ingroup groupFilters
  33.  */
  34.  
  35. /**
  36.  * @addtogroup FIR
  37.  * @{
  38.  */
  39.  
  40. /**
  41.  * @brief Processing function for the Q15 FIR filter.
  42.  * @param[in] *S points to an instance of the Q15 FIR structure.
  43.  * @param[in] *pSrc points to the block of input data.
  44.  * @param[out] *pDst points to the block of output data.
  45.  * @param[in]  blockSize number of samples to process per call.
  46.  * @return none.
  47.  *
  48.  *
  49.  * \par Restrictions
  50.  *  If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
  51.  *      In this case input, output, state buffers should be aligned by 32-bit
  52.  *
  53.  * <b>Scaling and Overflow Behavior:</b>
  54.  * \par
  55.  * The function is implemented using a 64-bit internal accumulator.
  56.  * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
  57.  * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  58.  * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
  59.  * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
  60.  * Lastly, the accumulator is saturated to yield a result in 1.15 format.
  61.  *
  62.  * \par
  63.  * Refer to the function <code>arm_fir_fast_q15()</code> for a faster but less precise implementation of this function.
  64.  */
  65.  
  66. #if defined (ARM_MATH_DSP)
  67.  
  68. /* Run the below code for Cortex-M4 and Cortex-M3 */
  69.  
  70. #ifndef UNALIGNED_SUPPORT_DISABLE
  71.  
  72.  
  73. void arm_fir_q15(
  74.   const arm_fir_instance_q15 * S,
  75.   q15_t * pSrc,
  76.   q15_t * pDst,
  77.   uint32_t blockSize)
  78. {
  79.   q15_t *pState = S->pState;                     /* State pointer */
  80.   q15_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
  81.   q15_t *pStateCurnt;                            /* Points to the current sample of the state */
  82.   q15_t *px1;                                    /* Temporary q15 pointer for state buffer */
  83.   q15_t *pb;                                     /* Temporary pointer for coefficient buffer */
  84.   q31_t x0, x1, x2, x3, c0;                      /* Temporary variables to hold SIMD state and coefficient values */
  85.   q63_t acc0, acc1, acc2, acc3;                  /* Accumulators */
  86.   uint32_t numTaps = S->numTaps;                 /* Number of taps in the filter */
  87.   uint32_t tapCnt, blkCnt;                       /* Loop counters */
  88.  
  89.  
  90.   /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  91.   /* pStateCurnt points to the location where the new input data should be written */
  92.   pStateCurnt = &(S->pState[(numTaps - 1U)]);
  93.  
  94.   /* Apply loop unrolling and compute 4 output values simultaneously.
  95.    * The variables acc0 ... acc3 hold output values that are being computed:
  96.    *
  97.    *    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]
  98.    *    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]
  99.    *    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]
  100.    *    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]
  101.    */
  102.  
  103.   blkCnt = blockSize >> 2;
  104.  
  105.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  106.    ** a second loop below computes the remaining 1 to 3 samples. */
  107.   while (blkCnt > 0U)
  108.   {
  109.     /* Copy four new input samples into the state buffer.
  110.      ** Use 32-bit SIMD to move the 16-bit data.  Only requires two copies. */
  111.     *__SIMD32(pStateCurnt)++ = *__SIMD32(pSrc)++;
  112.     *__SIMD32(pStateCurnt)++ = *__SIMD32(pSrc)++;
  113.  
  114.     /* Set all accumulators to zero */
  115.     acc0 = 0;
  116.     acc1 = 0;
  117.     acc2 = 0;
  118.     acc3 = 0;
  119.  
  120.     /* Initialize state pointer of type q15 */
  121.     px1 = pState;
  122.  
  123.     /* Initialize coeff pointer of type q31 */
  124.     pb = pCoeffs;
  125.  
  126.     /* Read the first two samples from the state buffer:  x[n-N], x[n-N-1] */
  127.     x0 = _SIMD32_OFFSET(px1);
  128.  
  129.     /* Read the third and forth samples from the state buffer: x[n-N-1], x[n-N-2] */
  130.     x1 = _SIMD32_OFFSET(px1 + 1U);
  131.  
  132.     px1 += 2U;
  133.  
  134.     /* Loop over the number of taps.  Unroll by a factor of 4.
  135.      ** Repeat until we've computed numTaps-4 coefficients. */
  136.     tapCnt = numTaps >> 2;
  137.  
  138.     while (tapCnt > 0U)
  139.     {
  140.       /* Read the first two coefficients using SIMD:  b[N] and b[N-1] coefficients */
  141.       c0 = *__SIMD32(pb)++;
  142.  
  143.       /* acc0 +=  b[N] * x[n-N] + b[N-1] * x[n-N-1] */
  144.       acc0 = __SMLALD(x0, c0, acc0);
  145.  
  146.       /* acc1 +=  b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
  147.       acc1 = __SMLALD(x1, c0, acc1);
  148.  
  149.       /* Read state x[n-N-2], x[n-N-3] */
  150.       x2 = _SIMD32_OFFSET(px1);
  151.  
  152.       /* Read state x[n-N-3], x[n-N-4] */
  153.       x3 = _SIMD32_OFFSET(px1 + 1U);
  154.  
  155.       /* acc2 +=  b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
  156.       acc2 = __SMLALD(x2, c0, acc2);
  157.  
  158.       /* acc3 +=  b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
  159.       acc3 = __SMLALD(x3, c0, acc3);
  160.  
  161.       /* Read coefficients b[N-2], b[N-3] */
  162.       c0 = *__SIMD32(pb)++;
  163.  
  164.       /* acc0 +=  b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
  165.       acc0 = __SMLALD(x2, c0, acc0);
  166.  
  167.       /* acc1 +=  b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
  168.       acc1 = __SMLALD(x3, c0, acc1);
  169.  
  170.       /* Read state x[n-N-4], x[n-N-5] */
  171.       x0 = _SIMD32_OFFSET(px1 + 2U);
  172.  
  173.       /* Read state x[n-N-5], x[n-N-6] */
  174.       x1 = _SIMD32_OFFSET(px1 + 3U);
  175.  
  176.       /* acc2 +=  b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
  177.       acc2 = __SMLALD(x0, c0, acc2);
  178.  
  179.       /* acc3 +=  b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
  180.       acc3 = __SMLALD(x1, c0, acc3);
  181.  
  182.       px1 += 4U;
  183.  
  184.       tapCnt--;
  185.  
  186.     }
  187.  
  188.  
  189.     /* If the filter length is not a multiple of 4, compute the remaining filter taps.
  190.      ** This is always be 2 taps since the filter length is even. */
  191.     if ((numTaps & 0x3U) != 0U)
  192.     {
  193.       /* Read 2 coefficients */
  194.       c0 = *__SIMD32(pb)++;
  195.  
  196.       /* Fetch 4 state variables */
  197.       x2 = _SIMD32_OFFSET(px1);
  198.  
  199.       x3 = _SIMD32_OFFSET(px1 + 1U);
  200.  
  201.       /* Perform the multiply-accumulates */
  202.       acc0 = __SMLALD(x0, c0, acc0);
  203.  
  204.       px1 += 2U;
  205.  
  206.       acc1 = __SMLALD(x1, c0, acc1);
  207.       acc2 = __SMLALD(x2, c0, acc2);
  208.       acc3 = __SMLALD(x3, c0, acc3);
  209.     }
  210.  
  211.     /* The results in the 4 accumulators are in 2.30 format.  Convert to 1.15 with saturation.
  212.      ** Then store the 4 outputs in the destination buffer. */
  213.  
  214. #ifndef ARM_MATH_BIG_ENDIAN
  215.  
  216.     *__SIMD32(pDst)++ =
  217.       __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
  218.     *__SIMD32(pDst)++ =
  219.       __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
  220.  
  221. #else
  222.  
  223.     *__SIMD32(pDst)++ =
  224.       __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
  225.     *__SIMD32(pDst)++ =
  226.       __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
  227.  
  228. #endif /*      #ifndef ARM_MATH_BIG_ENDIAN       */
  229.  
  230.  
  231.  
  232.     /* Advance the state pointer by 4 to process the next group of 4 samples */
  233.     pState = pState + 4;
  234.  
  235.     /* Decrement the loop counter */
  236.     blkCnt--;
  237.   }
  238.  
  239.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  240.    ** No loop unrolling is used. */
  241.   blkCnt = blockSize % 0x4U;
  242.   while (blkCnt > 0U)
  243.   {
  244.     /* Copy two samples into state buffer */
  245.     *pStateCurnt++ = *pSrc++;
  246.  
  247.     /* Set the accumulator to zero */
  248.     acc0 = 0;
  249.  
  250.     /* Initialize state pointer of type q15 */
  251.     px1 = pState;
  252.  
  253.     /* Initialize coeff pointer of type q31 */
  254.     pb = pCoeffs;
  255.  
  256.     tapCnt = numTaps >> 1;
  257.  
  258.     do
  259.     {
  260.  
  261.       c0 = *__SIMD32(pb)++;
  262.       x0 = *__SIMD32(px1)++;
  263.  
  264.       acc0 = __SMLALD(x0, c0, acc0);
  265.       tapCnt--;
  266.     }
  267.     while (tapCnt > 0U);
  268.  
  269.     /* The result is in 2.30 format.  Convert to 1.15 with saturation.
  270.      ** Then store the output in the destination buffer. */
  271.     *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16));
  272.  
  273.     /* Advance state pointer by 1 for the next sample */
  274.     pState = pState + 1;
  275.  
  276.     /* Decrement the loop counter */
  277.     blkCnt--;
  278.   }
  279.  
  280.   /* Processing is complete.
  281.    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  282.    ** This prepares the state buffer for the next function call. */
  283.  
  284.   /* Points to the start of the state buffer */
  285.   pStateCurnt = S->pState;
  286.  
  287.   /* Calculation of count for copying integer writes */
  288.   tapCnt = (numTaps - 1U) >> 2;
  289.  
  290.   while (tapCnt > 0U)
  291.   {
  292.  
  293.     /* Copy state values to start of state buffer */
  294.     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
  295.     *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
  296.  
  297.     tapCnt--;
  298.  
  299.   }
  300.  
  301.   /* Calculation of count for remaining q15_t data */
  302.   tapCnt = (numTaps - 1U) % 0x4U;
  303.  
  304.   /* copy remaining data */
  305.   while (tapCnt > 0U)
  306.   {
  307.     *pStateCurnt++ = *pState++;
  308.  
  309.     /* Decrement the loop counter */
  310.     tapCnt--;
  311.   }
  312. }
  313.  
  314. #else /* UNALIGNED_SUPPORT_DISABLE */
  315.  
  316. void arm_fir_q15(
  317.   const arm_fir_instance_q15 * S,
  318.   q15_t * pSrc,
  319.   q15_t * pDst,
  320.   uint32_t blockSize)
  321. {
  322.   q15_t *pState = S->pState;                     /* State pointer */
  323.   q15_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
  324.   q15_t *pStateCurnt;                            /* Points to the current sample of the state */
  325.   q63_t acc0, acc1, acc2, acc3;                  /* Accumulators */
  326.   q15_t *pb;                                     /* Temporary pointer for coefficient buffer */
  327.   q15_t *px;                                     /* Temporary q31 pointer for SIMD state buffer accesses */
  328.   q31_t x0, x1, x2, c0;                          /* Temporary variables to hold SIMD state and coefficient values */
  329.   uint32_t numTaps = S->numTaps;                 /* Number of taps in the filter */
  330.   uint32_t tapCnt, blkCnt;                       /* Loop counters */
  331.  
  332.  
  333.   /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
  334.   /* pStateCurnt points to the location where the new input data should be written */
  335.   pStateCurnt = &(S->pState[(numTaps - 1U)]);
  336.  
  337.   /* Apply loop unrolling and compute 4 output values simultaneously.
  338.    * The variables acc0 ... acc3 hold output values that are being computed:
  339.    *
  340.    *    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]
  341.    *    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]
  342.    *    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]
  343.    *    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]
  344.    */
  345.  
  346.   blkCnt = blockSize >> 2;
  347.  
  348.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  349.    ** a second loop below computes the remaining 1 to 3 samples. */
  350.   while (blkCnt > 0U)
  351.   {
  352.     /* Copy four new input samples into the state buffer.
  353.      ** Use 32-bit SIMD to move the 16-bit data.  Only requires two copies. */
  354.     *pStateCurnt++ = *pSrc++;
  355.     *pStateCurnt++ = *pSrc++;
  356.     *pStateCurnt++ = *pSrc++;
  357.     *pStateCurnt++ = *pSrc++;
  358.  
  359.  
  360.     /* Set all accumulators to zero */
  361.     acc0 = 0;
  362.     acc1 = 0;
  363.     acc2 = 0;
  364.     acc3 = 0;
  365.  
  366.     /* Typecast q15_t pointer to q31_t pointer for state reading in q31_t */
  367.     px = pState;
  368.  
  369.     /* Typecast q15_t pointer to q31_t pointer for coefficient reading in q31_t */
  370.     pb = pCoeffs;
  371.  
  372.     /* Read the first two samples from the state buffer:  x[n-N], x[n-N-1] */
  373.     x0 = *__SIMD32(px)++;
  374.  
  375.     /* Read the third and forth samples from the state buffer: x[n-N-2], x[n-N-3] */
  376.     x2 = *__SIMD32(px)++;
  377.  
  378.     /* Loop over the number of taps.  Unroll by a factor of 4.
  379.      ** Repeat until we've computed numTaps-(numTaps%4) coefficients. */
  380.     tapCnt = numTaps >> 2;
  381.  
  382.     while (tapCnt > 0)
  383.     {
  384.       /* Read the first two coefficients using SIMD:  b[N] and b[N-1] coefficients */
  385.       c0 = *__SIMD32(pb)++;
  386.  
  387.       /* acc0 +=  b[N] * x[n-N] + b[N-1] * x[n-N-1] */
  388.       acc0 = __SMLALD(x0, c0, acc0);
  389.  
  390.       /* acc2 +=  b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */
  391.       acc2 = __SMLALD(x2, c0, acc2);
  392.  
  393.       /* pack  x[n-N-1] and x[n-N-2] */
  394. #ifndef ARM_MATH_BIG_ENDIAN
  395.       x1 = __PKHBT(x2, x0, 0);
  396. #else
  397.       x1 = __PKHBT(x0, x2, 0);
  398. #endif
  399.  
  400.       /* Read state x[n-N-4], x[n-N-5] */
  401.       x0 = _SIMD32_OFFSET(px);
  402.  
  403.       /* acc1 +=  b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */
  404.       acc1 = __SMLALDX(x1, c0, acc1);
  405.  
  406.       /* pack  x[n-N-3] and x[n-N-4] */
  407. #ifndef ARM_MATH_BIG_ENDIAN
  408.       x1 = __PKHBT(x0, x2, 0);
  409. #else
  410.       x1 = __PKHBT(x2, x0, 0);
  411. #endif
  412.  
  413.       /* acc3 +=  b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */
  414.       acc3 = __SMLALDX(x1, c0, acc3);
  415.  
  416.       /* Read coefficients b[N-2], b[N-3] */
  417.       c0 = *__SIMD32(pb)++;
  418.  
  419.       /* acc0 +=  b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */
  420.       acc0 = __SMLALD(x2, c0, acc0);
  421.  
  422.       /* Read state x[n-N-6], x[n-N-7] with offset */
  423.       x2 = _SIMD32_OFFSET(px + 2U);
  424.  
  425.       /* acc2 +=  b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */
  426.       acc2 = __SMLALD(x0, c0, acc2);
  427.  
  428.       /* acc1 +=  b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */
  429.       acc1 = __SMLALDX(x1, c0, acc1);
  430.  
  431.       /* pack  x[n-N-5] and x[n-N-6] */
  432. #ifndef ARM_MATH_BIG_ENDIAN
  433.       x1 = __PKHBT(x2, x0, 0);
  434. #else
  435.       x1 = __PKHBT(x0, x2, 0);
  436. #endif
  437.  
  438.       /* acc3 +=  b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */
  439.       acc3 = __SMLALDX(x1, c0, acc3);
  440.  
  441.       /* Update state pointer for next state reading */
  442.       px += 4U;
  443.  
  444.       /* Decrement tap count */
  445.       tapCnt--;
  446.  
  447.     }
  448.  
  449.     /* If the filter length is not a multiple of 4, compute the remaining filter taps.
  450.      ** This is always be 2 taps since the filter length is even. */
  451.     if ((numTaps & 0x3U) != 0U)
  452.     {
  453.  
  454.       /* Read last two coefficients */
  455.       c0 = *__SIMD32(pb)++;
  456.  
  457.       /* Perform the multiply-accumulates */
  458.       acc0 = __SMLALD(x0, c0, acc0);
  459.       acc2 = __SMLALD(x2, c0, acc2);
  460.  
  461.       /* pack state variables */
  462. #ifndef ARM_MATH_BIG_ENDIAN
  463.       x1 = __PKHBT(x2, x0, 0);
  464. #else
  465.       x1 = __PKHBT(x0, x2, 0);
  466. #endif
  467.  
  468.       /* Read last state variables */
  469.       x0 = *__SIMD32(px);
  470.  
  471.       /* Perform the multiply-accumulates */
  472.       acc1 = __SMLALDX(x1, c0, acc1);
  473.  
  474.       /* pack state variables */
  475. #ifndef ARM_MATH_BIG_ENDIAN
  476.       x1 = __PKHBT(x0, x2, 0);
  477. #else
  478.       x1 = __PKHBT(x2, x0, 0);
  479. #endif
  480.  
  481.       /* Perform the multiply-accumulates */
  482.       acc3 = __SMLALDX(x1, c0, acc3);
  483.     }
  484.  
  485.     /* The results in the 4 accumulators are in 2.30 format.  Convert to 1.15 with saturation.
  486.      ** Then store the 4 outputs in the destination buffer. */
  487.  
  488. #ifndef ARM_MATH_BIG_ENDIAN
  489.  
  490.     *__SIMD32(pDst)++ =
  491.       __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16);
  492.  
  493.     *__SIMD32(pDst)++ =
  494.       __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16);
  495.  
  496. #else
  497.  
  498.     *__SIMD32(pDst)++ =
  499.       __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16);
  500.  
  501.     *__SIMD32(pDst)++ =
  502.       __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16);
  503.  
  504. #endif /*      #ifndef ARM_MATH_BIG_ENDIAN       */
  505.  
  506.     /* Advance the state pointer by 4 to process the next group of 4 samples */
  507.     pState = pState + 4;
  508.  
  509.     /* Decrement the loop counter */
  510.     blkCnt--;
  511.   }
  512.  
  513.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  514.    ** No loop unrolling is used. */
  515.   blkCnt = blockSize % 0x4U;
  516.   while (blkCnt > 0U)
  517.   {
  518.     /* Copy two samples into state buffer */
  519.     *pStateCurnt++ = *pSrc++;
  520.  
  521.     /* Set the accumulator to zero */
  522.     acc0 = 0;
  523.  
  524.     /* Use SIMD to hold states and coefficients */
  525.     px = pState;
  526.     pb = pCoeffs;
  527.  
  528.     tapCnt = numTaps >> 1U;
  529.  
  530.     do
  531.     {
  532.       acc0 += (q31_t) * px++ * *pb++;
  533.           acc0 += (q31_t) * px++ * *pb++;
  534.       tapCnt--;
  535.     }
  536.     while (tapCnt > 0U);
  537.  
  538.     /* The result is in 2.30 format.  Convert to 1.15 with saturation.
  539.      ** Then store the output in the destination buffer. */
  540.     *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16));
  541.  
  542.     /* Advance state pointer by 1 for the next sample */
  543.     pState = pState + 1U;
  544.  
  545.     /* Decrement the loop counter */
  546.     blkCnt--;
  547.   }
  548.  
  549.   /* Processing is complete.
  550.    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  551.    ** This prepares the state buffer for the next function call. */
  552.  
  553.   /* Points to the start of the state buffer */
  554.   pStateCurnt = S->pState;
  555.  
  556.   /* Calculation of count for copying integer writes */
  557.   tapCnt = (numTaps - 1U) >> 2;
  558.  
  559.   while (tapCnt > 0U)
  560.   {
  561.     *pStateCurnt++ = *pState++;
  562.     *pStateCurnt++ = *pState++;
  563.     *pStateCurnt++ = *pState++;
  564.     *pStateCurnt++ = *pState++;
  565.  
  566.     tapCnt--;
  567.  
  568.   }
  569.  
  570.   /* Calculation of count for remaining q15_t data */
  571.   tapCnt = (numTaps - 1U) % 0x4U;
  572.  
  573.   /* copy remaining data */
  574.   while (tapCnt > 0U)
  575.   {
  576.     *pStateCurnt++ = *pState++;
  577.  
  578.     /* Decrement the loop counter */
  579.     tapCnt--;
  580.   }
  581. }
  582.  
  583.  
  584. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  585.  
  586. #else /* ARM_MATH_CM0_FAMILY */
  587.  
  588.  
  589. /* Run the below code for Cortex-M0 */
  590.  
  591. void arm_fir_q15(
  592.   const arm_fir_instance_q15 * S,
  593.   q15_t * pSrc,
  594.   q15_t * pDst,
  595.   uint32_t blockSize)
  596. {
  597.   q15_t *pState = S->pState;                     /* State pointer */
  598.   q15_t *pCoeffs = S->pCoeffs;                   /* Coefficient pointer */
  599.   q15_t *pStateCurnt;                            /* Points to the current sample of the state */
  600.  
  601.  
  602.  
  603.   q15_t *px;                                     /* Temporary pointer for state buffer */
  604.   q15_t *pb;                                     /* Temporary pointer for coefficient buffer */
  605.   q63_t acc;                                     /* Accumulator */
  606.   uint32_t numTaps = S->numTaps;                 /* Number of nTaps in the filter */
  607.   uint32_t tapCnt, blkCnt;                       /* Loop counters */
  608.  
  609.   /* S->pState buffer contains previous frame (numTaps - 1) samples */
  610.   /* pStateCurnt points to the location where the new input data should be written */
  611.   pStateCurnt = &(S->pState[(numTaps - 1U)]);
  612.  
  613.   /* Initialize blkCnt with blockSize */
  614.   blkCnt = blockSize;
  615.  
  616.   while (blkCnt > 0U)
  617.   {
  618.     /* Copy one sample at a time into state buffer */
  619.     *pStateCurnt++ = *pSrc++;
  620.  
  621.     /* Set the accumulator to zero */
  622.     acc = 0;
  623.  
  624.     /* Initialize state pointer */
  625.     px = pState;
  626.  
  627.     /* Initialize Coefficient pointer */
  628.     pb = pCoeffs;
  629.  
  630.     tapCnt = numTaps;
  631.  
  632.     /* Perform the multiply-accumulates */
  633.     do
  634.     {
  635.       /* 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] */
  636.       acc += (q31_t) * px++ * *pb++;
  637.       tapCnt--;
  638.     } while (tapCnt > 0U);
  639.  
  640.     /* The result is in 2.30 format.  Convert to 1.15
  641.      ** Then store the output in the destination buffer. */
  642.     *pDst++ = (q15_t) __SSAT((acc >> 15U), 16);
  643.  
  644.     /* Advance state pointer by 1 for the next sample */
  645.     pState = pState + 1;
  646.  
  647.     /* Decrement the samples loop counter */
  648.     blkCnt--;
  649.   }
  650.  
  651.   /* Processing is complete.
  652.    ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
  653.    ** This prepares the state buffer for the next function call. */
  654.  
  655.   /* Points to the start of the state buffer */
  656.   pStateCurnt = S->pState;
  657.  
  658.   /* Copy numTaps number of values */
  659.   tapCnt = (numTaps - 1U);
  660.  
  661.   /* copy data */
  662.   while (tapCnt > 0U)
  663.   {
  664.     *pStateCurnt++ = *pState++;
  665.  
  666.     /* Decrement the loop counter */
  667.     tapCnt--;
  668.   }
  669.  
  670. }
  671.  
  672. #endif /* #if defined (ARM_MATH_DSP) */
  673.  
  674.  
  675.  
  676.  
  677. /**
  678.  * @} end of FIR group
  679.  */
  680.