1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sub_f32.c
4 * Description: Floating-point vector subtraction
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 BasicSub Vector Subtraction
37
38 Element-by-element subtraction 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 BasicSub
49 @{
50 */
51
52 /**
53 @brief Floating-point vector subtraction.
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 @return none
59 */
60
61 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
62
63 #include "arm_helium_utils.h"
64
arm_sub_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)65 void arm_sub_f32(
66 const float32_t * pSrcA,
67 const float32_t * pSrcB,
68 float32_t * pDst,
69 uint32_t blockSize)
70 {
71 uint32_t blkCnt; /* Loop counter */
72
73 f32x4_t vec1;
74 f32x4_t vec2;
75 f32x4_t res;
76
77 /* Compute 4 outputs at a time */
78 blkCnt = blockSize >> 2U;
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 = vsubq(vec1, vec2);
88 vst1q(pDst, res);
89
90 /* Increment pointers */
91 pSrcA += 4;
92 pSrcB += 4;
93 pDst += 4;
94
95 /* Decrement the loop counter */
96 blkCnt--;
97 }
98
99 /* Tail */
100 blkCnt = blockSize & 0x3;
101
102 if (blkCnt > 0U)
103 {
104 /* C = A + B */
105 mve_pred16_t p0 = vctp32q(blkCnt);
106 vec1 = vld1q(pSrcA);
107 vec2 = vld1q(pSrcB);
108 vstrwq_p(pDst, vsubq(vec1,vec2), p0);
109 }
110
111 }
112
113 #else
arm_sub_f32(const float32_t * pSrcA,const float32_t * pSrcB,float32_t * pDst,uint32_t blockSize)114 void arm_sub_f32(
115 const float32_t * pSrcA,
116 const float32_t * pSrcB,
117 float32_t * pDst,
118 uint32_t blockSize)
119 {
120 uint32_t blkCnt; /* Loop counter */
121
122 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
123 f32x4_t vec1;
124 f32x4_t vec2;
125 f32x4_t res;
126
127 /* Compute 4 outputs at a time */
128 blkCnt = blockSize >> 2U;
129
130 while (blkCnt > 0U)
131 {
132 /* C = A - B */
133
134 /* Subtract and then store the results in the destination buffer. */
135 vec1 = vld1q_f32(pSrcA);
136 vec2 = vld1q_f32(pSrcB);
137 res = vsubq_f32(vec1, vec2);
138 vst1q_f32(pDst, res);
139
140 /* Increment pointers */
141 pSrcA += 4;
142 pSrcB += 4;
143 pDst += 4;
144
145 /* Decrement the loop counter */
146 blkCnt--;
147 }
148
149 /* Tail */
150 blkCnt = blockSize & 0x3;
151
152 #else
153 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
154
155 /* Loop unrolling: Compute 4 outputs at a time */
156 blkCnt = blockSize >> 2U;
157
158 while (blkCnt > 0U)
159 {
160 /* C = A - B */
161
162 /* Subtract and store result in destination buffer. */
163 *pDst++ = (*pSrcA++) - (*pSrcB++);
164
165 *pDst++ = (*pSrcA++) - (*pSrcB++);
166
167 *pDst++ = (*pSrcA++) - (*pSrcB++);
168
169 *pDst++ = (*pSrcA++) - (*pSrcB++);
170
171 /* Decrement loop counter */
172 blkCnt--;
173 }
174
175 /* Loop unrolling: Compute remaining outputs */
176 blkCnt = blockSize % 0x4U;
177
178 #else
179
180 /* Initialize blkCnt with number of samples */
181 blkCnt = blockSize;
182
183 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
184 #endif /* #if defined(ARM_MATH_NEON) */
185
186 while (blkCnt > 0U)
187 {
188 /* C = A - B */
189
190 /* Subtract and store result in destination buffer. */
191 *pDst++ = (*pSrcA++) - (*pSrcB++);
192
193 /* Decrement loop counter */
194 blkCnt--;
195 }
196
197 }
198 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
199
200 /**
201 @} end of BasicSub group
202 */
203