1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_cmplx_mag_q15.c
4 * Description: Q15 complex magnitude
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/complex_math_functions.h"
30
31 /**
32 @ingroup groupCmplxMath
33 */
34
35 /**
36 @addtogroup cmplx_mag
37 @{
38 */
39
40 /**
41 @brief Q15 complex magnitude.
42 @param[in] pSrc points to input vector
43 @param[out] pDst points to output vector
44 @param[in] numSamples number of samples in each vector
45 @return none
46
47 @par Scaling and Overflow Behavior
48 The function implements 1.15 by 1.15 multiplications and finally output is converted into 2.14 format.
49 */
50
51 /* Sqrt q31 is used otherwise accuracy is not good enough
52 for small values and for some applications it is
53 an issue.
54 */
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
56
57 #include "arm_helium_utils.h"
58
arm_cmplx_mag_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)59 void arm_cmplx_mag_q15(
60 const q15_t * pSrc,
61 q15_t * pDst,
62 uint32_t numSamples)
63 {
64
65 int32_t blockSize = numSamples; /* loop counters */
66 uint32_t blkCnt; /* loop counters */
67 q15x8x2_t vecSrc;
68 q31x4_t prod0;
69 q31x4_t prod1;
70
71 q31_t in;
72 q31_t acc0;
73 q31x4_t acc0V;
74 q31x4_t acc1V;
75
76 q31_t res;
77 q15x8_t resV;
78
79 blkCnt = blockSize >> 3;
80 while (blkCnt > 0U)
81 {
82 vecSrc = vld2q(pSrc);
83 pSrc += 16;
84
85 acc0V = vdupq_n_s32(0);
86 acc1V = vdupq_n_s32(0);
87
88 prod0 = vmullbq_int_s16(vecSrc.val[0], vecSrc.val[0]);
89 acc0V = vqaddq_s32(acc0V,prod0);
90
91 prod0 = vmullbq_int_s16(vecSrc.val[1], vecSrc.val[1]);
92 acc0V = vqaddq_s32(acc0V,prod0);
93
94
95 prod1 = vmulltq_int_s16(vecSrc.val[0], vecSrc.val[0]);
96 acc1V = vqaddq_s32(acc1V,prod1);
97
98 prod1 = vmulltq_int_s16(vecSrc.val[1], vecSrc.val[1]);
99 acc1V = vqaddq_s32(acc1V,prod1);
100
101
102
103 acc0V = vshrq(acc0V, 1);
104 acc1V = vshrq(acc1V, 1);
105
106 acc0V = FAST_VSQRT_Q31(acc0V);
107 acc1V = FAST_VSQRT_Q31(acc1V);
108
109 resV = vdupq_n_s16(0);
110 resV = vqshrnbq_n_s32(resV,acc0V,16);
111 resV = vqshrntq_n_s32(resV,acc1V,16);
112
113 vst1q(pDst, resV);
114 pDst += 8;
115 /*
116 * Decrement the blockSize loop counter
117 */
118 blkCnt--;
119 }
120
121 /*
122 * tail
123 */
124 blkCnt = blockSize & 7;
125
126 while (blkCnt > 0U)
127 {
128 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
129
130 in = read_q15x2_ia ((q15_t **) &pSrc);
131 acc0 = __SMUAD(in, in);
132
133 /* store result in 2.14 format in destination buffer. */
134 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
135 *pDst++ = res >> 16;
136
137
138 /* Decrement loop counter */
139 blkCnt--;
140 }
141 }
142
143 #else
arm_cmplx_mag_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)144 void arm_cmplx_mag_q15(
145 const q15_t * pSrc,
146 q15_t * pDst,
147 uint32_t numSamples)
148 {
149 q31_t res; /* temporary result */
150 uint32_t blkCnt; /* Loop counter */
151
152 #if defined (ARM_MATH_DSP)
153 q31_t in;
154 q31_t acc0; /* Accumulators */
155 #else
156 q15_t real, imag; /* Temporary input variables */
157 q31_t acc0, acc1; /* Accumulators */
158 #endif
159
160 #if defined (ARM_MATH_LOOPUNROLL)
161
162 /* Loop unrolling: Compute 4 outputs at a time */
163 blkCnt = numSamples >> 2U;
164
165 while (blkCnt > 0U)
166 {
167 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
168
169 #if defined (ARM_MATH_DSP)
170 in = read_q15x2_ia (&pSrc);
171 acc0 = __SMUAD(in, in);
172 /* store result in 2.14 format in destination buffer. */
173 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
174 *pDst++ = res >> 16;
175
176 in = read_q15x2_ia (&pSrc);
177 acc0 = __SMUAD(in, in);
178 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
179 *pDst++ = res >> 16;
180
181 in = read_q15x2_ia (&pSrc);
182 acc0 = __SMUAD(in, in);
183 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
184 *pDst++ = res >> 16;
185
186 in = read_q15x2_ia (&pSrc);
187 acc0 = __SMUAD(in, in);
188 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
189 *pDst++ = res >> 16;
190 #else
191 real = *pSrc++;
192 imag = *pSrc++;
193 acc0 = ((q31_t) real * real);
194 acc1 = ((q31_t) imag * imag);
195
196 /* store result in 2.14 format in destination buffer. */
197 arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
198 *pDst++ = res >> 16;
199
200 real = *pSrc++;
201 imag = *pSrc++;
202 acc0 = ((q31_t) real * real);
203 acc1 = ((q31_t) imag * imag);
204 arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
205 *pDst++ = res >> 16;
206
207 real = *pSrc++;
208 imag = *pSrc++;
209 acc0 = ((q31_t) real * real);
210 acc1 = ((q31_t) imag * imag);
211 arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
212 *pDst++ = res >> 16;
213
214 real = *pSrc++;
215 imag = *pSrc++;
216 acc0 = ((q31_t) real * real);
217 acc1 = ((q31_t) imag * imag);
218 arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
219 *pDst++ = res >> 16;
220 #endif /* #if defined (ARM_MATH_DSP) */
221
222 /* Decrement loop counter */
223 blkCnt--;
224 }
225
226 /* Loop unrolling: Compute remaining outputs */
227 blkCnt = numSamples % 0x4U;
228
229 #else
230
231 /* Initialize blkCnt with number of samples */
232 blkCnt = numSamples;
233
234 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
235
236 while (blkCnt > 0U)
237 {
238 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
239
240 #if defined (ARM_MATH_DSP)
241 in = read_q15x2_ia (&pSrc);
242 acc0 = __SMUAD(in, in);
243 /* store result in 2.14 format in destination buffer. */
244 arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
245 *pDst++ = res >> 16;
246 #else
247 real = *pSrc++;
248 imag = *pSrc++;
249 acc0 = ((q31_t) real * real);
250 acc1 = ((q31_t) imag * imag);
251
252 /* store result in 2.14 format in destination buffer. */
253 arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
254 *pDst++ = res >> 16;
255
256 #endif
257
258 /* Decrement loop counter */
259 blkCnt--;
260 }
261
262 }
263 #endif /* defined(ARM_MATH_MVEI) */
264
265 /**
266 @} end of cmplx_mag group
267 */
268