1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_cmplx_mult_real_q31.c
4  * Description:  Q31 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   @addtogroup CmplxByRealMult
37   @{
38  */
39 
40 /**
41   @brief         Q31 complex-by-real multiplication.
42   @param[in]     pSrcCmplx   points to complex input vector
43   @param[in]     pSrcReal    points to real input vector
44   @param[out]    pCmplxDst   points to complex output vector
45   @param[in]     numSamples  number of samples in each vector
46 
47   @par           Scaling and Overflow Behavior
48                    The function uses saturating arithmetic.
49                    Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] are saturated.
50  */
51 
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_cmplx_mult_real_q31(const q31_t * pSrcCmplx,const q31_t * pSrcReal,q31_t * pCmplxDst,uint32_t numSamples)53 ARM_DSP_ATTRIBUTE void arm_cmplx_mult_real_q31(
54   const q31_t * pSrcCmplx,
55   const q31_t * pSrcReal,
56         q31_t * pCmplxDst,
57         uint32_t numSamples)
58 {
59 
60     static const uint32_t stride_cmplx_x_real_32[4] = {
61         0, 0, 1, 1
62     };
63     q31x4_t rVec;
64     q31x4_t cmplxVec;
65     q31x4_t dstVec;
66     uint32x4_t strideVec;
67     uint32_t blockSizeC = numSamples * CMPLX_DIM;   /* loop counters */
68     uint32_t blkCnt;
69     q31_t in;
70 
71     /*
72      * stride vector for pairs of real generation
73      */
74     strideVec = vld1q(stride_cmplx_x_real_32);
75 
76     /* Compute 4 complex outputs at a time */
77     blkCnt = blockSizeC >> 2;
78     while (blkCnt > 0U)
79     {
80         cmplxVec = vld1q(pSrcCmplx);
81         rVec = vldrwq_gather_shifted_offset_s32(pSrcReal, strideVec);
82         dstVec = vqdmulhq(cmplxVec, rVec);
83         vst1q(pCmplxDst, dstVec);
84 
85         pSrcReal += 2;
86         pSrcCmplx += 4;
87         pCmplxDst += 4;
88         blkCnt --;
89     }
90 
91     blkCnt = (blockSizeC & 3) >> 1;
92     while (blkCnt > 0U)
93     {
94       /* C[2 * i    ] = A[2 * i    ] * B[i]. */
95       /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
96 
97       in = *pSrcReal++;
98       /* store saturated result in 1.31 format to destination buffer */
99       *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
100       *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
101 
102       /* Decrement loop counter */
103       blkCnt--;
104     }
105 }
106 #else
arm_cmplx_mult_real_q31(const q31_t * pSrcCmplx,const q31_t * pSrcReal,q31_t * pCmplxDst,uint32_t numSamples)107 ARM_DSP_ATTRIBUTE void arm_cmplx_mult_real_q31(
108   const q31_t * pSrcCmplx,
109   const q31_t * pSrcReal,
110         q31_t * pCmplxDst,
111         uint32_t numSamples)
112 {
113         uint32_t blkCnt;                               /* Loop counter */
114         q31_t in;                                      /* Temporary variable */
115 
116 #if defined (ARM_MATH_LOOPUNROLL)
117 
118   /* Loop unrolling: Compute 4 outputs at a time */
119   blkCnt = numSamples >> 2U;
120 
121   while (blkCnt > 0U)
122   {
123     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
124     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
125 
126     in = *pSrcReal++;
127 #if defined (ARM_MATH_DSP)
128     /* store saturated result in 1.31 format to destination buffer */
129     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
130     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
131 #else
132     /* store result in destination buffer. */
133     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
134     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
135 #endif
136 
137     in = *pSrcReal++;
138 #if defined (ARM_MATH_DSP)
139     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
140     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
141 #else
142     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
143     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
144 #endif
145 
146     in = *pSrcReal++;
147 #if defined (ARM_MATH_DSP)
148     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
149     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
150 #else
151     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
152     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
153 #endif
154 
155     in = *pSrcReal++;
156 #if defined (ARM_MATH_DSP)
157     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
158     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
159 #else
160     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
161     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
162 #endif
163 
164     /* Decrement loop counter */
165     blkCnt--;
166   }
167 
168   /* Loop unrolling: Compute remaining outputs */
169   blkCnt = numSamples % 0x4U;
170 
171 #else
172 
173   /* Initialize blkCnt with number of samples */
174   blkCnt = numSamples;
175 
176 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
177 
178   while (blkCnt > 0U)
179   {
180     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
181     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
182 
183     in = *pSrcReal++;
184 #if defined (ARM_MATH_DSP)
185     /* store saturated result in 1.31 format to destination buffer */
186     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
187     *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
188 #else
189     /* store result in destination buffer. */
190     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
191     *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
192 #endif
193 
194     /* Decrement loop counter */
195     blkCnt--;
196   }
197 
198 }
199 #endif /* defined(ARM_MATH_MVEI) */
200 
201 /**
202   @} end of CmplxByRealMult group
203  */
204