1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_add_f32.c
4  * Description:  Floating-point vector addition
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 BasicAdd Vector Addition
37 
38   Element-by-element addition 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 BasicAdd
49   @{
50  */
51 
52 /**
53   @brief         Floating-point vector addition.
54   @param[in]     pSrcA      points to first input vector
55   @param[in]     pSrcB      points to second input vector
56   @param[out]    pDst       points to 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_add_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)64 void arm_add_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 
79     while (blkCnt > 0U)
80     {
81         /* C = A + B */
82 
83         /* Add and then store the results in the destination buffer. */
84         vec1 = vld1q(pSrcA);
85         vec2 = vld1q(pSrcB);
86         res = vaddq(vec1, vec2);
87         vst1q(pDst, res);
88 
89         /* Increment pointers */
90         pSrcA += 4;
91         pSrcB += 4;
92         pDst += 4;
93 
94         /* Decrement the loop counter */
95         blkCnt--;
96     }
97 
98     /* Tail */
99     blkCnt = blockSize & 0x3;
100 
101     if (blkCnt > 0U)
102     {
103       /* C = A + B */
104       mve_pred16_t p0 = vctp32q(blkCnt);
105       vec1 = vld1q(pSrcA);
106       vec2 = vld1q(pSrcB);
107       vstrwq_p(pDst, vaddq(vec1,vec2), p0);
108     }
109 
110 }
111 
112 #else
arm_add_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)113 void arm_add_f32(
114   const float32_t * pSrcA,
115   const float32_t * pSrcB,
116         float32_t * pDst,
117         uint32_t blockSize)
118 {
119         uint32_t blkCnt;                               /* Loop counter */
120 
121 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
122     f32x4_t vec1;
123     f32x4_t vec2;
124     f32x4_t res;
125 
126     /* Compute 4 outputs at a time */
127     blkCnt = blockSize >> 2U;
128 
129     while (blkCnt > 0U)
130     {
131         /* C = A + B */
132 
133     	/* Add and then store the results in the destination buffer. */
134         vec1 = vld1q_f32(pSrcA);
135         vec2 = vld1q_f32(pSrcB);
136         res = vaddq_f32(vec1, vec2);
137         vst1q_f32(pDst, res);
138 
139         /* Increment pointers */
140         pSrcA += 4;
141         pSrcB += 4;
142         pDst += 4;
143 
144         /* Decrement the loop counter */
145         blkCnt--;
146     }
147 
148     /* Tail */
149     blkCnt = blockSize & 0x3;
150 
151 #else
152 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
153 
154   /* Loop unrolling: Compute 4 outputs at a time */
155   blkCnt = blockSize >> 2U;
156 
157   while (blkCnt > 0U)
158   {
159     /* C = A + B */
160 
161     /* Add and store result in destination buffer. */
162     *pDst++ = (*pSrcA++) + (*pSrcB++);
163     *pDst++ = (*pSrcA++) + (*pSrcB++);
164     *pDst++ = (*pSrcA++) + (*pSrcB++);
165     *pDst++ = (*pSrcA++) + (*pSrcB++);
166 
167     /* Decrement loop counter */
168     blkCnt--;
169   }
170 
171   /* Loop unrolling: Compute remaining outputs */
172   blkCnt = blockSize % 0x4U;
173 
174 #else
175 
176   /* Initialize blkCnt with number of samples */
177   blkCnt = blockSize;
178 
179 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
180 #endif /* #if defined(ARM_MATH_NEON) */
181 
182   while (blkCnt > 0U)
183   {
184     /* C = A + B */
185 
186     /* Add and store result in destination buffer. */
187     *pDst++ = (*pSrcA++) + (*pSrcB++);
188 
189     /* Decrement loop counter */
190     blkCnt--;
191   }
192 
193 }
194 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
195 
196 /**
197   @} end of BasicAdd group
198  */
199