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