1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mult_f32.c
4  * Description:  Floating-point vector 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/basic_math_functions.h"
30 
31 /**
32   @ingroup groupMath
33  */
34 
35 /**
36   @defgroup BasicMult Vector Multiplication
37 
38   Element-by-element multiplication of two vectors.
39 
40   <pre>
41       pDst[n] = pSrcA[n] * pSrcB[n],   0 <= n < blockSize.
42   </pre>
43 
44   There are separate functions for floating-point, Q7, Q15, and Q31 data types.
45  */
46 
47 /**
48   @addtogroup BasicMult
49   @{
50  */
51 
52 /**
53   @brief         Floating-point vector multiplication.
54   @param[in]     pSrcA      points to the first input vector.
55   @param[in]     pSrcB      points to the second input vector.
56   @param[out]    pDst       points to the output vector.
57   @param[in]     blockSize  number of samples in each vector.
58  */
59 
60 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
61 
62 #include "arm_helium_utils.h"
63 
arm_mult_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)64 void arm_mult_f32(
65   const float32_t * pSrcA,
66   const float32_t * pSrcB,
67         float32_t * pDst,
68         uint32_t blockSize)
69 {
70     uint32_t blkCnt;                               /* Loop counter */
71 
72     f32x4_t vec1;
73     f32x4_t vec2;
74     f32x4_t res;
75 
76     /* Compute 4 outputs at a time */
77     blkCnt = blockSize >> 2U;
78     while (blkCnt > 0U)
79     {
80         /* C = A + B */
81 
82       /* Add and then store the results in the destination buffer. */
83         vec1 = vld1q(pSrcA);
84         vec2 = vld1q(pSrcB);
85         res = vmulq(vec1, vec2);
86         vst1q(pDst, res);
87 
88         /* Increment pointers */
89         pSrcA += 4;
90         pSrcB += 4;
91         pDst += 4;
92 
93         /* Decrement the loop counter */
94         blkCnt--;
95     }
96 
97     /* Tail */
98     blkCnt = blockSize & 0x3;
99     if (blkCnt > 0U)
100     {
101       /* C = A + B */
102       mve_pred16_t p0 = vctp32q(blkCnt);
103       vec1 = vld1q(pSrcA);
104       vec2 = vld1q(pSrcB);
105       vstrwq_p(pDst, vmulq(vec1,vec2), p0);
106     }
107 
108 }
109 
110 #else
arm_mult_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)111 void arm_mult_f32(
112   const float32_t * pSrcA,
113   const float32_t * pSrcB,
114         float32_t * pDst,
115         uint32_t blockSize)
116 {
117     uint32_t blkCnt;                               /* Loop counter */
118 
119 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
120     f32x4_t vec1;
121     f32x4_t vec2;
122     f32x4_t res;
123 
124     /* Compute 4 outputs at a time */
125     blkCnt = blockSize >> 2U;
126 
127     while (blkCnt > 0U)
128     {
129         /* C = A * B */
130 
131     	/* Multiply the inputs and then store the results in the destination buffer. */
132         vec1 = vld1q_f32(pSrcA);
133         vec2 = vld1q_f32(pSrcB);
134         res = vmulq_f32(vec1, vec2);
135         vst1q_f32(pDst, res);
136 
137         /* Increment pointers */
138         pSrcA += 4;
139         pSrcB += 4;
140         pDst += 4;
141 
142         /* Decrement the loop counter */
143         blkCnt--;
144     }
145 
146     /* Tail */
147     blkCnt = blockSize & 0x3;
148 
149 #else
150 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
151 
152   /* Loop unrolling: Compute 4 outputs at a time */
153   blkCnt = blockSize >> 2U;
154 
155   while (blkCnt > 0U)
156   {
157     /* C = A * B */
158 
159     /* Multiply inputs and store result in destination buffer. */
160     *pDst++ = (*pSrcA++) * (*pSrcB++);
161 
162     *pDst++ = (*pSrcA++) * (*pSrcB++);
163 
164     *pDst++ = (*pSrcA++) * (*pSrcB++);
165 
166     *pDst++ = (*pSrcA++) * (*pSrcB++);
167 
168     /* Decrement loop counter */
169     blkCnt--;
170   }
171 
172   /* Loop unrolling: Compute remaining outputs */
173   blkCnt = blockSize % 0x4U;
174 
175 #else
176 
177   /* Initialize blkCnt with number of samples */
178   blkCnt = blockSize;
179 
180 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
181 #endif /* #if defined(ARM_MATH_NEON) */
182 
183   while (blkCnt > 0U)
184   {
185     /* C = A * B */
186 
187     /* Multiply input and store result in destination buffer. */
188     *pDst++ = (*pSrcA++) * (*pSrcB++);
189 
190     /* Decrement loop counter */
191     blkCnt--;
192   }
193 
194 }
195 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
196 
197 /**
198   @} end of BasicMult group
199  */
200