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