1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_cmplx_mult_real_f32.c
4  * Description:  Floating-point complex by real multiplication
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   @defgroup CmplxByRealMult Complex-by-Real Multiplication
37 
38   Multiplies a complex vector by a real vector and generates a complex result.
39   The data in the complex arrays is stored in an interleaved fashion
40   (real, imag, real, imag, ...).
41   The parameter <code>numSamples</code> represents the number of complex
42   samples processed.  The complex arrays have a total of <code>2*numSamples</code>
43   real values while the real array has a total of <code>numSamples</code>
44   real values.
45 
46   The underlying algorithm is used:
47 
48   <pre>
49   for (n = 0; n < numSamples; n++) {
50       pCmplxDst[(2*n)+0] = pSrcCmplx[(2*n)+0] * pSrcReal[n];
51       pCmplxDst[(2*n)+1] = pSrcCmplx[(2*n)+1] * pSrcReal[n];
52   }
53   </pre>
54 
55   There are separate functions for floating-point, Q15, and Q31 data types.
56  */
57 
58 /**
59   @addtogroup CmplxByRealMult
60   @{
61  */
62 
63 /**
64   @brief         Floating-point complex-by-real multiplication.
65   @param[in]     pSrcCmplx   points to complex input vector
66   @param[in]     pSrcReal    points to real input vector
67   @param[out]    pCmplxDst   points to complex output vector
68   @param[in]     numSamples  number of samples in each vector
69  */
70 
71 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
72 
arm_cmplx_mult_real_f32(const float32_t * pSrcCmplx,const float32_t * pSrcReal,float32_t * pCmplxDst,uint32_t numSamples)73 ARM_DSP_ATTRIBUTE void arm_cmplx_mult_real_f32(
74   const float32_t * pSrcCmplx,
75   const float32_t * pSrcReal,
76         float32_t * pCmplxDst,
77         uint32_t numSamples)
78 {
79     static const uint32_t stride_cmplx_x_real_32[4] = { 0, 0, 1, 1 };
80 
81     uint32_t blockSizeC = numSamples * CMPLX_DIM;   /* loop counters */
82     uint32_t blkCnt;
83     f32x4_t rVec;
84     f32x4_t cmplxVec;
85     f32x4_t dstVec;
86     uint32x4_t strideVec;
87     float32_t in;
88 
89 
90     /* stride vector for pairs of real generation */
91     strideVec = vld1q(stride_cmplx_x_real_32);
92 
93     /* Compute 4 complex outputs at a time */
94     blkCnt = blockSizeC >> 2;
95     while (blkCnt > 0U)
96     {
97         cmplxVec = vld1q(pSrcCmplx);
98         rVec = vldrwq_gather_shifted_offset_f32(pSrcReal, strideVec);
99         dstVec = vmulq(cmplxVec, rVec);
100         vst1q(pCmplxDst, dstVec);
101 
102         pSrcReal += 2;
103         pSrcCmplx += 4;
104         pCmplxDst += 4;
105         blkCnt--;
106     }
107 
108     blkCnt = (blockSizeC & 3) >> 1;
109     while (blkCnt > 0U)
110     {
111       /* C[2 * i    ] = A[2 * i    ] * B[i]. */
112       /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
113 
114       in = *pSrcReal++;
115       /* store result in destination buffer. */
116       *pCmplxDst++ = *pSrcCmplx++ * in;
117       *pCmplxDst++ = *pSrcCmplx++ * in;
118 
119       /* Decrement loop counter */
120       blkCnt--;
121     }
122 }
123 
124 #else
arm_cmplx_mult_real_f32(const float32_t * pSrcCmplx,const float32_t * pSrcReal,float32_t * pCmplxDst,uint32_t numSamples)125 ARM_DSP_ATTRIBUTE void arm_cmplx_mult_real_f32(
126   const float32_t * pSrcCmplx,
127   const float32_t * pSrcReal,
128         float32_t * pCmplxDst,
129         uint32_t numSamples)
130 {
131         uint32_t blkCnt;                               /* Loop counter */
132         float32_t in;                                  /* Temporary variable */
133 
134 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
135     float32x4_t r;
136     float32x4x2_t ab,outCplx;
137 
138     /* Compute 4 outputs at a time */
139     blkCnt = numSamples >> 2U;
140 
141     while (blkCnt > 0U)
142     {
143         ab = vld2q_f32(pSrcCmplx);  // load & separate real/imag pSrcA (de-interleave 2)
144         r = vld1q_f32(pSrcReal);  // load & separate real/imag pSrcB
145 
146 	/* Increment pointers */
147         pSrcCmplx += 8;
148         pSrcReal += 4;
149 
150         outCplx.val[0] = vmulq_f32(ab.val[0], r);
151         outCplx.val[1] = vmulq_f32(ab.val[1], r);
152 
153         vst2q_f32(pCmplxDst, outCplx);
154         pCmplxDst += 8;
155 
156         blkCnt--;
157     }
158 
159     /* Tail */
160     blkCnt = numSamples & 3;
161 #else
162 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
163 
164   /* Loop unrolling: Compute 4 outputs at a time */
165   blkCnt = numSamples >> 2U;
166 
167   while (blkCnt > 0U)
168   {
169     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
170     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
171 
172     in = *pSrcReal++;
173     /* store result in destination buffer. */
174     *pCmplxDst++ = *pSrcCmplx++ * in;
175     *pCmplxDst++ = *pSrcCmplx++ * in;
176 
177     in = *pSrcReal++;
178     *pCmplxDst++ = *pSrcCmplx++ * in;
179     *pCmplxDst++ = *pSrcCmplx++ * in;
180 
181     in = *pSrcReal++;
182     *pCmplxDst++ = *pSrcCmplx++ * in;
183     *pCmplxDst++ = *pSrcCmplx++ * in;
184 
185     in = *pSrcReal++;
186     *pCmplxDst++ = *pSrcCmplx++* in;
187     *pCmplxDst++ = *pSrcCmplx++ * in;
188 
189     /* Decrement loop counter */
190     blkCnt--;
191   }
192 
193   /* Loop unrolling: Compute remaining outputs */
194   blkCnt = numSamples % 0x4U;
195 
196 #else
197 
198   /* Initialize blkCnt with number of samples */
199   blkCnt = numSamples;
200 
201 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
202 #endif /* #if defined(ARM_MATH_NEON) */
203 
204   while (blkCnt > 0U)
205   {
206     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
207     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
208 
209     in = *pSrcReal++;
210     /* store result in destination buffer. */
211     *pCmplxDst++ = *pSrcCmplx++ * in;
212     *pCmplxDst++ = *pSrcCmplx++ * in;
213 
214     /* Decrement loop counter */
215     blkCnt--;
216   }
217 
218 }
219 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
220 
221 /**
222   @} end of CmplxByRealMult group
223  */
224