1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_dot_prod_f32.c
4  * Description:  Floating-point dot product
5  *
6  * $Date:        05 October 2021
7  * $Revision:    V1.9.1
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 BasicDotProd Vector Dot Product
37 
38   Computes the dot product of two vectors.
39   The vectors are multiplied element-by-element and then summed.
40 
41   <pre>
42       sum = pSrcA[0]*pSrcB[0] + pSrcA[1]*pSrcB[1] + ... + pSrcA[blockSize-1]*pSrcB[blockSize-1]
43   </pre>
44 
45   There are separate functions for floating-point, Q7, Q15, and Q31 data types.
46  */
47 
48 /**
49   @addtogroup BasicDotProd
50   @{
51  */
52 
53 /**
54   @brief         Dot product of floating-point vectors.
55   @param[in]     pSrcA      points to the first input vector.
56   @param[in]     pSrcB      points to the second input vector.
57   @param[in]     blockSize  number of samples in each vector.
58   @param[out]    result     output result returned here.
59  */
60 
61 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
62 
63 #include "arm_helium_utils.h"
64 
65 
arm_dot_prod_f32(const float32_t * pSrcA,const float32_t * pSrcB,uint32_t blockSize,float32_t * result)66 ARM_DSP_ATTRIBUTE void arm_dot_prod_f32(
67     const float32_t * pSrcA,
68     const float32_t * pSrcB,
69     uint32_t    blockSize,
70     float32_t * result)
71 {
72     f32x4_t vecA, vecB;
73     f32x4_t vecSum;
74     uint32_t blkCnt;
75     float32_t sum = 0.0f;
76     vecSum = vdupq_n_f32(0.0f);
77 
78     /* Compute 4 outputs at a time */
79     blkCnt = blockSize >> 2U;
80     while (blkCnt > 0U)
81     {
82         /*
83          * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
84          * Calculate dot product and then store the result in a temporary buffer.
85          * and advance vector source and destination pointers
86          */
87         vecA = vld1q(pSrcA);
88         pSrcA += 4;
89 
90         vecB = vld1q(pSrcB);
91         pSrcB += 4;
92 
93         vecSum = vfmaq(vecSum, vecA, vecB);
94         /*
95          * Decrement the blockSize loop counter
96          */
97         blkCnt --;
98     }
99 
100 
101     blkCnt = blockSize & 3;
102     if (blkCnt > 0U)
103     {
104         /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
105 
106         mve_pred16_t p0 = vctp32q(blkCnt);
107         vecA = vld1q(pSrcA);
108         vecB = vld1q(pSrcB);
109         vecSum = vfmaq_m(vecSum, vecA, vecB, p0);
110     }
111 
112     sum = vecAddAcrossF32Mve(vecSum);
113 
114     /* Store result in destination buffer */
115     *result = sum;
116 
117 }
118 
119 #else
120 
arm_dot_prod_f32(const float32_t * pSrcA,const float32_t * pSrcB,uint32_t blockSize,float32_t * result)121 ARM_DSP_ATTRIBUTE void arm_dot_prod_f32(
122   const float32_t * pSrcA,
123   const float32_t * pSrcB,
124         uint32_t blockSize,
125         float32_t * result)
126 {
127         uint32_t blkCnt;                               /* Loop counter */
128         float32_t sum = 0.0f;                          /* Temporary return variable */
129 
130 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
131     f32x4_t vec1;
132     f32x4_t vec2;
133     f32x4_t accum = vdupq_n_f32(0);
134 #if !defined(__aarch64__)
135     f32x2_t tmp = vdup_n_f32(0);
136 #endif
137 
138     /* Compute 4 outputs at a time */
139     blkCnt = blockSize >> 2U;
140 
141     vec1 = vld1q_f32(pSrcA);
142     vec2 = vld1q_f32(pSrcB);
143 
144     while (blkCnt > 0U)
145     {
146         /* C = A[0]*B[0] + A[1]*B[1] + A[2]*B[2] + ... + A[blockSize-1]*B[blockSize-1] */
147         /* Calculate dot product and then store the result in a temporary buffer. */
148 
149 	      accum = vmlaq_f32(accum, vec1, vec2);
150 
151         /* Increment pointers */
152         pSrcA += 4;
153         pSrcB += 4;
154 
155         vec1 = vld1q_f32(pSrcA);
156         vec2 = vld1q_f32(pSrcB);
157 
158         /* Decrement the loop counter */
159         blkCnt--;
160     }
161 
162 #if defined(__aarch64__)
163     sum = vpadds_f32(vpadd_f32(vget_low_f32(accum), vget_high_f32(accum)));
164 #else
165     tmp = vpadd_f32(vget_low_f32(accum), vget_high_f32(accum));
166     sum = vget_lane_f32(tmp, 0) + vget_lane_f32(tmp, 1);
167 
168 #endif
169 
170     /* Tail */
171     blkCnt = blockSize & 0x3;
172 
173 #else
174 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
175 
176   /* Loop unrolling: Compute 4 outputs at a time */
177   blkCnt = blockSize >> 2U;
178 
179   /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
180    ** a second loop below computes the remaining 1 to 3 samples. */
181   while (blkCnt > 0U)
182   {
183     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
184 
185     /* Calculate dot product and store result in a temporary buffer. */
186     sum += (*pSrcA++) * (*pSrcB++);
187 
188     sum += (*pSrcA++) * (*pSrcB++);
189 
190     sum += (*pSrcA++) * (*pSrcB++);
191 
192     sum += (*pSrcA++) * (*pSrcB++);
193 
194     /* Decrement loop counter */
195     blkCnt--;
196   }
197 
198   /* Loop unrolling: Compute remaining outputs */
199   blkCnt = blockSize % 0x4U;
200 
201 #else
202 
203   /* Initialize blkCnt with number of samples */
204   blkCnt = blockSize;
205 
206 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
207 #endif /* #if defined(ARM_MATH_NEON) */
208 
209   while (blkCnt > 0U)
210   {
211     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
212 
213     /* Calculate dot product and store result in a temporary buffer. */
214     sum += (*pSrcA++) * (*pSrcB++);
215 
216     /* Decrement loop counter */
217     blkCnt--;
218   }
219 
220   /* Store result in destination buffer */
221   *result = sum;
222 }
223 
224 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
225 /**
226   @} end of BasicDotProd group
227  */
228