• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_q31_to_q7.c
4  * Description:  Converts the elements of the Q31 vector to Q7 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 Q7 vector.
42   @param[in]     pSrc       points to the Q31 input vector
43   @param[out]    pDst       points to the Q7 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] = (q7_t) pSrc[n] >> 24;   0 <= n < blockSize.
51   </pre>
52  */
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_q31_to_q7(const q31_t * pSrc,q7_t * pDst,uint32_t blockSize)54 void arm_q31_to_q7(
55   const q31_t * pSrc,
56         q7_t * pDst,
57         uint32_t blockSize)
58 {
59     uint32_t  blkCnt;           /* loop counters */
60     q31x4x4_t tmp;
61     q15x8_t evVec, oddVec;
62     q7x16_t  vecDst;
63     q31_t const *pSrcVec;
64 
65     pSrcVec = (q31_t const *) pSrc;
66     blkCnt = blockSize >> 4;
67     while (blkCnt > 0U)
68     {
69         tmp = vld4q(pSrcVec);
70         pSrcVec += 16;
71         /* C = (q7_t) A >> 24 */
72         /* convert from q31 to q7 and then store the results in the destination buffer */
73         /*
74          * narrow and pack evens
75          */
76         evVec = vshrnbq_n_s32(evVec, tmp.val[0], 16);
77         evVec = vshrntq_n_s32(evVec, tmp.val[2], 16);
78         /*
79          * narrow and pack odds
80          */
81         oddVec = vshrnbq_n_s32(oddVec, tmp.val[1], 16);
82         oddVec = vshrntq_n_s32(oddVec, tmp.val[3], 16);
83         /*
84          * narrow & merge
85          */
86         vecDst = vshrnbq_n_s16(vecDst, evVec, 8);
87         vecDst = vshrntq_n_s16(vecDst, oddVec, 8);
88 
89         vst1q(pDst, vecDst);
90         pDst += 16;
91         /*
92          * Decrement the blockSize loop counter
93          */
94         blkCnt--;
95     }
96     /*
97      * tail
98      */
99     blkCnt = blockSize & 0xF;
100     while (blkCnt > 0U)
101     {
102       /* C = (q7_t) (A >> 24) */
103 
104       /* Convert from q31 to q7 and store result in destination buffer */
105       *pDst++ = (q7_t) (*pSrcVec++ >> 24);
106 
107       /* Decrement loop counter */
108       blkCnt--;
109     }
110 }
111 #else
arm_q31_to_q7(const q31_t * pSrc,q7_t * pDst,uint32_t blockSize)112 void arm_q31_to_q7(
113   const q31_t * pSrc,
114         q7_t * pDst,
115         uint32_t blockSize)
116 {
117         uint32_t blkCnt;                               /* Loop counter */
118   const q31_t *pIn = pSrc;                             /* Source pointer */
119 
120 #if defined (ARM_MATH_LOOPUNROLL)
121 
122   q7_t out1, out2, out3, out4;
123 
124   /* Loop unrolling: Compute 4 outputs at a time */
125   blkCnt = blockSize >> 2U;
126 
127   while (blkCnt > 0U)
128   {
129     /* C = (q7_t) (A >> 24) */
130 
131     /* Convert from q31 to q7 and store result in destination buffer */
132 
133     out1 = (q7_t) (*pIn++ >> 24);
134     out2 = (q7_t) (*pIn++ >> 24);
135     out3 = (q7_t) (*pIn++ >> 24);
136     out4 = (q7_t) (*pIn++ >> 24);
137     write_q7x4_ia (&pDst, __PACKq7(out1, out2, out3, out4));
138 
139     /* Decrement loop counter */
140     blkCnt--;
141   }
142 
143   /* Loop unrolling: Compute remaining outputs */
144   blkCnt = blockSize % 0x4U;
145 
146 #else
147 
148   /* Initialize blkCnt with number of samples */
149   blkCnt = blockSize;
150 
151 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
152 
153   while (blkCnt > 0U)
154   {
155     /* C = (q7_t) (A >> 24) */
156 
157     /* Convert from q31 to q7 and store result in destination buffer */
158     *pDst++ = (q7_t) (*pIn++ >> 24);
159 
160     /* Decrement loop counter */
161     blkCnt--;
162   }
163 
164 }
165 #endif /* defined(ARM_MATH_MVEI) */
166 
167 /**
168   @} end of q31_to_x group
169  */
170