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