/ Drivers / CMSIS / DSP / Source / SupportFunctions / arm_q31_to_q15.c
arm_q31_to_q15.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_q31_to_q15.c
  4   * Description:  Converts the elements of the Q31 vector to Q15 vector
  5   *
  6   * $Date:        23 April 2021
  7   * $Revision:    V1.9.0
  8   *
  9   * Target Processor: Cortex-M and Cortex-A cores
 10   * -------------------------------------------------------------------- */
 11  /*
 12   * Copyright (C) 2010-2021 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 "dsp/support_functions.h"
 30  
 31  /**
 32    @ingroup groupSupport
 33   */
 34  
 35  /**
 36    @addtogroup q31_to_x
 37    @{
 38   */
 39  
 40  /**
 41    @brief         Converts the elements of the Q31 vector to Q15 vector.
 42    @param[in]     pSrc       points to the Q31 input vector
 43    @param[out]    pDst       points to the Q15 output vector
 44    @param[in]     blockSize  number of samples in each vector
 45    @return        none
 46  
 47    @par           Details
 48                     The equation used for the conversion process is:
 49    <pre>
 50        pDst[n] = (q15_t) pSrc[n] >> 16;   0 <= n < blockSize.
 51    </pre>
 52   */
 53  #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
 54  void arm_q31_to_q15(
 55    const q31_t * pSrc,
 56          q15_t * pDst,
 57          uint32_t blockSize)
 58  {
 59      uint32_t  blkCnt;           /* loop counters */
 60      q31x4x2_t tmp;
 61      q15x8_t vecDst;
 62      q31_t const *pSrcVec;
 63  
 64  
 65      pSrcVec = (q31_t const *) pSrc;
 66      blkCnt = blockSize >> 3;
 67      while (blkCnt > 0U)
 68      {
 69          /* C = (q15_t) A >> 16 */
 70          /* convert from q31 to q15 and then store the results in the destination buffer */
 71          tmp = vld2q(pSrcVec);   
 72          pSrcVec += 8;
 73          vecDst = vshrnbq_n_s32(vecDst, tmp.val[0], 16);
 74          vecDst = vshrntq_n_s32(vecDst, tmp.val[1], 16);
 75          vst1q(pDst, vecDst);    
 76          pDst += 8;
 77          /*
 78           * Decrement the blockSize loop counter
 79           */
 80          blkCnt--;
 81      }
 82  
 83      /*
 84       * tail
 85       */
 86      blkCnt = blockSize & 7;
 87      while (blkCnt > 0U)
 88      {
 89        /* C = (q15_t) (A >> 16) */
 90    
 91        /* Convert from q31 to q15 and store result in destination buffer */
 92        *pDst++ = (q15_t) (*pSrcVec++ >> 16);
 93    
 94        /* Decrement loop counter */
 95        blkCnt--;
 96      }
 97  }
 98  
 99  #else
100  void arm_q31_to_q15(
101    const q31_t * pSrc,
102          q15_t * pDst,
103          uint32_t blockSize)
104  {
105          uint32_t blkCnt;                               /* Loop counter */
106    const q31_t *pIn = pSrc;                             /* Source pointer */
107  
108  #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
109          q31_t in1, in2, in3, in4;
110          q31_t out1, out2;
111  #endif
112  
113  #if defined (ARM_MATH_LOOPUNROLL)
114  
115    /* Loop unrolling: Compute 4 outputs at a time */
116    blkCnt = blockSize >> 2U;
117  
118    while (blkCnt > 0U)
119    {
120      /* C = (q15_t) (A >> 16) */
121  
122      /* Convert from q31 to q15 and store result in destination buffer */
123  #if defined (ARM_MATH_DSP)
124  
125      in1 = *pIn++;
126      in2 = *pIn++;
127      in3 = *pIn++;
128      in4 = *pIn++;
129  
130      /* pack two higher 16-bit values from two 32-bit values */
131  #ifndef ARM_MATH_BIG_ENDIAN
132      out1 = __PKHTB(in2, in1, 16);
133      out2 = __PKHTB(in4, in3, 16);
134  #else
135      out1 = __PKHTB(in1, in2, 16);
136      out2 = __PKHTB(in3, in4, 16);
137  #endif /* #ifdef ARM_MATH_BIG_ENDIAN */
138  
139      write_q15x2_ia (&pDst, out1);
140      write_q15x2_ia (&pDst, out2);
141  
142  #else
143  
144      *pDst++ = (q15_t) (*pIn++ >> 16);
145      *pDst++ = (q15_t) (*pIn++ >> 16);
146      *pDst++ = (q15_t) (*pIn++ >> 16);
147      *pDst++ = (q15_t) (*pIn++ >> 16);
148  
149  #endif /* #if defined (ARM_MATH_DSP) */
150  
151      /* Decrement loop counter */
152      blkCnt--;
153    }
154  
155    /* Loop unrolling: Compute remaining outputs */
156    blkCnt = blockSize % 0x4U;
157  
158  #else
159  
160    /* Initialize blkCnt with number of samples */
161    blkCnt = blockSize;
162  
163  #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
164  
165    while (blkCnt > 0U)
166    {
167      /* C = (q15_t) (A >> 16) */
168  
169      /* Convert from q31 to q15 and store result in destination buffer */
170      *pDst++ = (q15_t) (*pIn++ >> 16);
171  
172      /* Decrement loop counter */
173      blkCnt--;
174    }
175  
176  }
177  #endif /* defined(ARM_MATH_MVEI) */
178  
179  /**
180    @} end of q31_to_x group
181   */