1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_q15_to_q7.c
4 * Description: Converts the elements of the Q15 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 q15_to_x
37 @{
38 */
39
40 /**
41 @brief Converts the elements of the Q15 vector to Q7 vector.
42 @param[in] pSrc points to the Q15 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] >> 8; 0 <= n < blockSize.
50 </pre>
51 */
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_q15_to_q7(const q15_t * pSrc,q7_t * pDst,uint32_t blockSize)53 ARM_DSP_ATTRIBUTE void arm_q15_to_q7(
54 const q15_t * pSrc,
55 q7_t * pDst,
56 uint32_t blockSize)
57 {
58
59 uint32_t blkCnt; /* loop counters */
60 q15x8x2_t tmp;
61 q15_t const *pSrcVec;
62 q7x16_t vecDst = { 0 };
63
64
65 pSrcVec = (q15_t const *) pSrc;
66 blkCnt = blockSize >> 4;
67 while (blkCnt > 0U)
68 {
69 /* C = (q7_t) A >> 8 */
70 /* convert from q15 to q7 and then store the results in the destination buffer */
71 tmp = vld2q(pSrcVec);
72 pSrcVec += 16;
73 vecDst = vqshrnbq_n_s16(vecDst, tmp.val[0], 8);
74 vecDst = vqshrntq_n_s16(vecDst, tmp.val[1], 8);
75 vst1q(pDst, vecDst);
76 pDst += 16;
77 /*
78 * Decrement the blockSize loop counter
79 */
80 blkCnt--;
81 }
82
83 blkCnt = blockSize & 0xF;
84 while (blkCnt > 0U)
85 {
86 /* C = (q7_t) A >> 8 */
87
88 /* Convert from q15 to q7 and store result in destination buffer */
89 *pDst++ = (q7_t) (*pSrcVec++ >> 8);
90
91 /* Decrement loop counter */
92 blkCnt--;
93 }
94 }
95 #else
arm_q15_to_q7(const q15_t * pSrc,q7_t * pDst,uint32_t blockSize)96 ARM_DSP_ATTRIBUTE void arm_q15_to_q7(
97 const q15_t * pSrc,
98 q7_t * pDst,
99 uint32_t blockSize)
100 {
101 uint32_t blkCnt; /* Loop counter */
102 const q15_t *pIn = pSrc; /* Source pointer */
103
104 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
105 q31_t in1, in2;
106 q31_t out1, out2;
107 #endif
108
109 #if defined (ARM_MATH_LOOPUNROLL)
110
111 /* Loop unrolling: Compute 4 outputs at a time */
112 blkCnt = blockSize >> 2U;
113
114 while (blkCnt > 0U)
115 {
116 /* C = (q7_t) A >> 8 */
117
118 /* Convert from q15 to q7 and store result in destination buffer */
119 #if defined (ARM_MATH_DSP)
120
121 in1 = read_q15x2_ia (&pIn);
122 in2 = read_q15x2_ia (&pIn);
123
124 #ifndef ARM_MATH_BIG_ENDIAN
125
126 out1 = __PKHTB(in2, in1, 16);
127 out2 = __PKHBT(in2, in1, 16);
128
129 #else
130
131 out1 = __PKHTB(in1, in2, 16);
132 out2 = __PKHBT(in1, in2, 16);
133
134 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
135
136 /* rotate packed value by 24 */
137 out2 = ((uint32_t) out2 << 8) | ((uint32_t) out2 >> 24);
138
139 /* anding with 0xff00ff00 to get two 8 bit values */
140 out1 = out1 & 0xFF00FF00;
141 /* anding with 0x00ff00ff to get two 8 bit values */
142 out2 = out2 & 0x00FF00FF;
143
144 /* oring two values(contains two 8 bit values) to get four packed 8 bit values */
145 out1 = out1 | out2;
146
147 /* store 4 samples at a time to destiantion buffer */
148 write_q7x4_ia (&pDst, out1);
149
150 #else
151
152 *pDst++ = (q7_t) (*pIn++ >> 8);
153 *pDst++ = (q7_t) (*pIn++ >> 8);
154 *pDst++ = (q7_t) (*pIn++ >> 8);
155 *pDst++ = (q7_t) (*pIn++ >> 8);
156
157 #endif /* #if defined (ARM_MATH_DSP) */
158
159 /* Decrement loop counter */
160 blkCnt--;
161 }
162
163 /* Loop unrolling: Compute remaining outputs */
164 blkCnt = blockSize % 0x4U;
165
166 #else
167
168 /* Initialize blkCnt with number of samples */
169 blkCnt = blockSize;
170
171 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
172
173 while (blkCnt > 0U)
174 {
175 /* C = (q7_t) A >> 8 */
176
177 /* Convert from q15 to q7 and store result in destination buffer */
178 *pDst++ = (q7_t) (*pIn++ >> 8);
179
180 /* Decrement loop counter */
181 blkCnt--;
182 }
183
184 }
185 #endif /* defined(ARM_MATH_MVEI) */
186
187 /**
188 @} end of q15_to_x group
189 */
190