1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_cmplx_mult_real_q15.c
4  * Description:  Q15 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         Q15 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 Q15 range [0x8000 0x7FFF] are saturated.
50  */
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
arm_cmplx_mult_real_q15(const q15_t * pSrcCmplx,const q15_t * pSrcReal,q15_t * pCmplxDst,uint32_t numSamples)53 void arm_cmplx_mult_real_q15(
54   const q15_t * pSrcCmplx,
55   const q15_t * pSrcReal,
56         q15_t * pCmplxDst,
57         uint32_t numSamples)
58 {
59   static const uint16_t stride_cmplx_x_real_16[8] = {
60       0, 0, 1, 1, 2, 2, 3, 3
61       };
62   q15x8_t rVec;
63   q15x8_t cmplxVec;
64   q15x8_t dstVec;
65   uint16x8_t strideVec;
66   uint32_t blockSizeC = numSamples * CMPLX_DIM;   /* loop counters */
67   uint32_t blkCnt;
68   q15_t in;
69 
70   /*
71   * stride vector for pairs of real generation
72   */
73   strideVec = vld1q(stride_cmplx_x_real_16);
74 
75   blkCnt = blockSizeC >> 3;
76 
77   while (blkCnt > 0U)
78   {
79     cmplxVec = vld1q(pSrcCmplx);
80     rVec = vldrhq_gather_shifted_offset_s16(pSrcReal, strideVec);
81     dstVec = vqdmulhq(cmplxVec, rVec);
82     vst1q(pCmplxDst, dstVec);
83 
84     pSrcReal += 4;
85     pSrcCmplx += 8;
86     pCmplxDst += 8;
87     blkCnt --;
88   }
89 
90   /* Tail */
91   blkCnt = (blockSizeC & 7) >> 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 the result in the destination buffer. */
99     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
100     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
101 
102     /* Decrement loop counter */
103     blkCnt--;
104   }
105 }
106 #else
arm_cmplx_mult_real_q15(const q15_t * pSrcCmplx,const q15_t * pSrcReal,q15_t * pCmplxDst,uint32_t numSamples)107 void arm_cmplx_mult_real_q15(
108   const q15_t * pSrcCmplx,
109   const q15_t * pSrcReal,
110         q15_t * pCmplxDst,
111         uint32_t numSamples)
112 {
113         uint32_t blkCnt;                               /* Loop counter */
114         q15_t in;                                      /* Temporary variable */
115 
116 #if defined (ARM_MATH_LOOPUNROLL)
117 
118 #if defined (ARM_MATH_DSP)
119         q31_t inA1, inA2;                              /* Temporary variables to hold input data */
120         q31_t inB1;                                    /* Temporary variables to hold input data */
121         q15_t out1, out2, out3, out4;                  /* Temporary variables to hold output data */
122         q31_t mul1, mul2, mul3, mul4;                  /* Temporary variables to hold intermediate data */
123 #endif
124 
125   /* Loop unrolling: Compute 4 outputs at a time */
126   blkCnt = numSamples >> 2U;
127 
128   while (blkCnt > 0U)
129   {
130     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
131     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
132 
133 #if defined (ARM_MATH_DSP)
134     /* read 2 complex numbers both real and imaginary from complex input buffer */
135     inA1 = read_q15x2_ia (&pSrcCmplx);
136     inA2 = read_q15x2_ia (&pSrcCmplx);
137     /* read 2 real values at a time from real input buffer */
138     inB1 = read_q15x2_ia (&pSrcReal);
139 
140     /* multiply complex number with real numbers */
141 #ifndef ARM_MATH_BIG_ENDIAN
142     mul1 = (q31_t) ((q15_t) (inA1)       * (q15_t) (inB1));
143     mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
144     mul3 = (q31_t) ((q15_t) (inA2)       * (q15_t) (inB1 >> 16));
145     mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
146 #else
147     mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
148     mul1 = (q31_t) ((q15_t) inA1         * (q15_t) (inB1 >> 16));
149     mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
150     mul3 = (q31_t) ((q15_t) inA2         * (q15_t) inB1);
151 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
152 
153     /* saturate the result */
154     out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
155     out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
156     out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
157     out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
158 
159     /* pack real and imaginary outputs and store them to destination */
160     write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
161     write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
162 
163     inA1 = read_q15x2_ia (&pSrcCmplx);
164     inA2 = read_q15x2_ia (&pSrcCmplx);
165     inB1 = read_q15x2_ia (&pSrcReal);
166 
167 #ifndef ARM_MATH_BIG_ENDIAN
168     mul1 = (q31_t) ((q15_t) (inA1)       * (q15_t) (inB1));
169     mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
170     mul3 = (q31_t) ((q15_t) (inA2)       * (q15_t) (inB1 >> 16));
171     mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
172 #else
173     mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
174     mul1 = (q31_t) ((q15_t) inA1         * (q15_t) (inB1 >> 16));
175     mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
176     mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
177 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
178 
179     out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
180     out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
181     out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
182     out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
183 
184     write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
185     write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
186 #else
187     in = *pSrcReal++;
188     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
189     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
190 
191     in = *pSrcReal++;
192     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
193     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
194 
195     in = *pSrcReal++;
196     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
197     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
198 
199     in = *pSrcReal++;
200     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
201     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
202 #endif
203 
204     /* Decrement loop counter */
205     blkCnt--;
206   }
207 
208   /* Loop unrolling: Compute remaining outputs */
209   blkCnt = numSamples % 0x4U;
210 
211 #else
212 
213   /* Initialize blkCnt with number of samples */
214   blkCnt = numSamples;
215 
216 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
217 
218   while (blkCnt > 0U)
219   {
220     /* C[2 * i    ] = A[2 * i    ] * B[i]. */
221     /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
222 
223     in = *pSrcReal++;
224     /* store the result in the destination buffer. */
225     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
226     *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
227 
228     /* Decrement loop counter */
229     blkCnt--;
230   }
231 
232 }
233 #endif /* defined(ARM_MATH_MVEI) */
234 
235 /**
236   @} end of CmplxByRealMult group
237  */
238