1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_cmplx_mag_squared_q15.c
4 * Description: Q15 complex magnitude squared
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_squared
37 @{
38 */
39
40 /**
41 @brief Q15 complex magnitude squared.
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 3.13 format.
49 */
50
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52
arm_cmplx_mag_squared_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)53 void arm_cmplx_mag_squared_q15(
54 const q15_t * pSrc,
55 q15_t * pDst,
56 uint32_t numSamples)
57 {
58 int32_t blockSize = numSamples; /* loop counters */
59 uint32_t blkCnt; /* loop counters */
60 q31_t in;
61 q31_t acc0; /* Accumulators */
62 q15x8x2_t vecSrc;
63 q15x8_t vReal, vImag;
64 q15x8_t vMagSq;
65
66
67 blkCnt = blockSize >> 3;
68 while (blkCnt > 0U)
69 {
70 vecSrc = vld2q(pSrc);
71 vReal = vmulhq(vecSrc.val[0], vecSrc.val[0]);
72 vImag = vmulhq(vecSrc.val[1], vecSrc.val[1]);
73 vMagSq = vqaddq(vReal, vImag);
74 vMagSq = vshrq(vMagSq, 1);
75
76 vst1q(pDst, vMagSq);
77
78 pSrc += 16;
79 pDst += 8;
80 /*
81 * Decrement the blkCnt loop counter
82 * Advance vector source and destination pointers
83 */
84 blkCnt --;
85 }
86
87 /*
88 * tail
89 */
90 blkCnt = blockSize & 7;
91 while (blkCnt > 0U)
92 {
93 /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
94
95 in = read_q15x2_ia ((q15_t **) &pSrc);
96 acc0 = __SMUAD(in, in);
97
98 /* store result in 3.13 format in destination buffer. */
99 *pDst++ = (q15_t) (acc0 >> 17);
100
101
102 /* Decrement loop counter */
103 blkCnt--;
104 }
105
106 }
107
108 #else
arm_cmplx_mag_squared_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)109 void arm_cmplx_mag_squared_q15(
110 const q15_t * pSrc,
111 q15_t * pDst,
112 uint32_t numSamples)
113 {
114 uint32_t blkCnt; /* Loop counter */
115
116 #if defined (ARM_MATH_DSP)
117 q31_t in;
118 q31_t acc0; /* Accumulators */
119 #else
120 q15_t real, imag; /* Temporary input variables */
121 q31_t acc0, acc1; /* Accumulators */
122 #endif
123
124 #if defined (ARM_MATH_LOOPUNROLL)
125
126 /* Loop unrolling: Compute 4 outputs at a time */
127 blkCnt = numSamples >> 2U;
128
129 while (blkCnt > 0U)
130 {
131 /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
132
133 #if defined (ARM_MATH_DSP)
134 in = read_q15x2_ia ((q15_t **) &pSrc);
135 acc0 = __SMUAD(in, in);
136 /* store result in 3.13 format in destination buffer. */
137 *pDst++ = (q15_t) (acc0 >> 17);
138
139 in = read_q15x2_ia ((q15_t **) &pSrc);
140 acc0 = __SMUAD(in, in);
141 *pDst++ = (q15_t) (acc0 >> 17);
142
143 in = read_q15x2_ia ((q15_t **) &pSrc);
144 acc0 = __SMUAD(in, in);
145 *pDst++ = (q15_t) (acc0 >> 17);
146
147 in = read_q15x2_ia ((q15_t **) &pSrc);
148 acc0 = __SMUAD(in, in);
149 *pDst++ = (q15_t) (acc0 >> 17);
150 #else
151 real = *pSrc++;
152 imag = *pSrc++;
153 acc0 = ((q31_t) real * real);
154 acc1 = ((q31_t) imag * imag);
155 /* store result in 3.13 format in destination buffer. */
156 *pDst++ = (q15_t) (((q63_t) acc0 + acc1) >> 17);
157
158 real = *pSrc++;
159 imag = *pSrc++;
160 acc0 = ((q31_t) real * real);
161 acc1 = ((q31_t) imag * imag);
162 *pDst++ = (q15_t) (((q63_t) acc0 + acc1) >> 17);
163
164 real = *pSrc++;
165 imag = *pSrc++;
166 acc0 = ((q31_t) real * real);
167 acc1 = ((q31_t) imag * imag);
168 *pDst++ = (q15_t) (((q63_t) acc0 + acc1) >> 17);
169
170 real = *pSrc++;
171 imag = *pSrc++;
172 acc0 = ((q31_t) real * real);
173 acc1 = ((q31_t) imag * imag);
174 *pDst++ = (q15_t) (((q63_t) acc0 + acc1) >> 17);
175 #endif /* #if defined (ARM_MATH_DSP) */
176
177 /* Decrement loop counter */
178 blkCnt--;
179 }
180
181 /* Loop unrolling: Compute remaining outputs */
182 blkCnt = numSamples % 0x4U;
183
184 #else
185
186 /* Initialize blkCnt with number of samples */
187 blkCnt = numSamples;
188
189 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
190
191 while (blkCnt > 0U)
192 {
193 /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
194
195 #if defined (ARM_MATH_DSP)
196 in = read_q15x2_ia ((q15_t **) &pSrc);
197 acc0 = __SMUAD(in, in);
198
199 /* store result in 3.13 format in destination buffer. */
200 *pDst++ = (q15_t) (acc0 >> 17);
201 #else
202 real = *pSrc++;
203 imag = *pSrc++;
204 acc0 = ((q31_t) real * real);
205 acc1 = ((q31_t) imag * imag);
206
207 /* store result in 3.13 format in destination buffer. */
208 *pDst++ = (q15_t) (((q63_t) acc0 + acc1) >> 17);
209 #endif
210
211 /* Decrement loop counter */
212 blkCnt--;
213 }
214
215 }
216
217 #endif /* defined(ARM_MATH_MVEI) */
218
219 /**
220 @} end of cmplx_mag_squared group
221 */
222