1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_add_f16.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_f16.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   @return        none
59  */
60 
61 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
62 
63 #include "arm_helium_utils.h"
64 
arm_add_f16(const float16_t * pSrcA,const float16_t * pSrcB,float16_t * pDst,uint32_t blockSize)65 void arm_add_f16(
66   const float16_t * pSrcA,
67   const float16_t * pSrcB,
68         float16_t * pDst,
69         uint32_t blockSize)
70 {
71     uint32_t blkCnt;                               /* Loop counter */
72 
73     f16x8_t vec1;
74     f16x8_t vec2;
75     f16x8_t res;
76 
77     /* Compute 4 outputs at a time */
78     blkCnt = blockSize >> 3U;
79 
80     while (blkCnt > 0U)
81     {
82         /* C = A + B */
83 
84         /* Add and then store the results in the destination buffer. */
85         vec1 = vld1q(pSrcA);
86         vec2 = vld1q(pSrcB);
87         res = vaddq(vec1, vec2);
88         vst1q(pDst, res);
89 
90         /* Increment pointers */
91         pSrcA += 8;
92         pSrcB += 8;
93         pDst += 8;
94 
95         /* Decrement the loop counter */
96         blkCnt--;
97     }
98 
99     /* Tail */
100     blkCnt = blockSize & 0x7;
101 
102     if (blkCnt > 0U)
103     {
104       /* C = A + B */
105       mve_pred16_t p0 = vctp16q(blkCnt);
106       vec1 = vld1q(pSrcA);
107       vec2 = vld1q(pSrcB);
108       vstrhq_p(pDst, vaddq(vec1,vec2), p0);
109     }
110 
111 }
112 
113 #else
114 #if defined(ARM_FLOAT16_SUPPORTED)
arm_add_f16(const float16_t * pSrcA,const float16_t * pSrcB,float16_t * pDst,uint32_t blockSize)115 void arm_add_f16(
116   const float16_t * pSrcA,
117   const float16_t * pSrcB,
118         float16_t * pDst,
119         uint32_t blockSize)
120 {
121         uint32_t blkCnt;                               /* Loop counter */
122 
123 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
124 
125   /* Loop unrolling: Compute 4 outputs at a time */
126   blkCnt = blockSize >> 2U;
127 
128   while (blkCnt > 0U)
129   {
130     /* C = A + B */
131 
132     /* Add and store result in destination buffer. */
133     *pDst++ = (*pSrcA++) + (*pSrcB++);
134     *pDst++ = (*pSrcA++) + (*pSrcB++);
135     *pDst++ = (*pSrcA++) + (*pSrcB++);
136     *pDst++ = (*pSrcA++) + (*pSrcB++);
137 
138     /* Decrement loop counter */
139     blkCnt--;
140   }
141 
142   /* Loop unrolling: Compute remaining outputs */
143   blkCnt = blockSize % 0x4U;
144 
145 #else
146 
147   /* Initialize blkCnt with number of samples */
148   blkCnt = blockSize;
149 
150 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
151 
152   while (blkCnt > 0U)
153   {
154     /* C = A + B */
155 
156     /* Add and store result in destination buffer. */
157     *pDst++ = (*pSrcA++) + (*pSrcB++);
158 
159     /* Decrement loop counter */
160     blkCnt--;
161   }
162 
163 }
164 #endif /* defined(ARM_FLOAT16_SUPPORTED) */
165 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
166 
167 /**
168   @} end of BasicAdd group
169  */
170