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