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