1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_abs_q31.c
4  * Description:  Q31 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 
31 /**
32   @ingroup groupMath
33  */
34 
35 /**
36   @addtogroup BasicAbs
37   @{
38  */
39 
40 /**
41   @brief         Q31 vector absolute value.
42   @param[in]     pSrc       points to the input vector
43   @param[out]    pDst       points to the output vector
44   @param[in]     blockSize  number of samples in each vector
45 
46   @par           Scaling and Overflow Behavior
47                    The function uses saturating arithmetic.
48                    The Q31 value -1 (0x80000000) will be saturated to the maximum allowable positive value 0x7FFFFFFF.
49  */
50 
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_abs_q31(const q31_t * pSrc,q31_t * pDst,uint32_t blockSize)55 void arm_abs_q31(
56     const q31_t * pSrc,
57     q31_t * pDst,
58     uint32_t blockSize)
59 {
60     uint32_t  blkCnt;           /* Loop counters */
61     q31x4_t vecSrc;
62 
63     /* Compute 4 outputs at a time */
64     blkCnt = blockSize >> 2;
65 
66     while (blkCnt > 0U)
67     {
68         /*
69          * C = |A|
70          * Calculate absolute and then store the results in the destination buffer.
71          */
72         vecSrc = vld1q(pSrc);
73         vst1q(pDst, vqabsq(vecSrc));
74         /*
75          * Decrement the blockSize loop counter
76          */
77         blkCnt--;
78         /*
79          * Advance vector source and destination pointers
80          */
81         pSrc += 4;
82         pDst += 4;
83     }
84     /*
85      * Tail
86      */
87     blkCnt = blockSize & 3;
88 
89     if (blkCnt > 0U)
90     {
91         mve_pred16_t p0 = vctp32q(blkCnt);
92         vecSrc = vld1q(pSrc);
93         vstrwq_p(pDst, vqabsq(vecSrc), p0);
94     }
95 }
96 
97 #else
arm_abs_q31(const q31_t * pSrc,q31_t * pDst,uint32_t blockSize)98 void arm_abs_q31(
99   const q31_t * pSrc,
100         q31_t * pDst,
101         uint32_t blockSize)
102 {
103         uint32_t blkCnt;                               /* Loop counter */
104         q31_t in;                                      /* Temporary variable */
105 
106 #if defined(ARM_MATH_NEON)
107     int32x4_t vec1;
108     int32x4_t res;
109 
110     /* Compute 4 outputs at a time */
111     blkCnt = blockSize >> 2U;
112 
113     while (blkCnt > 0U)
114     {
115         /* C = |A| */
116         /* Calculate absolute and then store the results in the destination buffer. */
117 
118         vec1 = vld1q_s32(pSrc);
119         res = vqabsq_s32(vec1);
120         vst1q_s32(pDst, res);
121 
122         /* Increment pointers */
123         pSrc += 4;
124         pDst += 4;
125 
126         /* Decrement the blockSize loop counter */
127         blkCnt--;
128     }
129 
130     /* Tail */
131     blkCnt = blockSize & 0x3;
132 
133 #else
134 #if defined (ARM_MATH_LOOPUNROLL)
135 
136   /* Loop unrolling: Compute 4 outputs at a time */
137   blkCnt = blockSize >> 2U;
138 
139   while (blkCnt > 0U)
140   {
141     /* C = |A| */
142 
143     /* Calculate absolute of input (if -1 then saturated to 0x7fffffff) and store result in destination buffer. */
144     in = *pSrc++;
145 #if defined (ARM_MATH_DSP)
146     *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
147 #else
148     *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
149 #endif
150 
151     in = *pSrc++;
152 #if defined (ARM_MATH_DSP)
153     *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
154 #else
155     *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
156 #endif
157 
158     in = *pSrc++;
159 #if defined (ARM_MATH_DSP)
160     *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
161 #else
162     *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
163 #endif
164 
165     in = *pSrc++;
166 #if defined (ARM_MATH_DSP)
167     *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
168 #else
169     *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
170 #endif
171 
172     /* Decrement loop counter */
173     blkCnt--;
174   }
175 
176   /* Loop unrolling: Compute remaining outputs */
177   blkCnt = blockSize % 0x4U;
178 
179 #else
180 
181   /* Initialize blkCnt with number of samples */
182   blkCnt = blockSize;
183 
184 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
185 #endif /* #if defined (ARM_MATH_NEON) */
186 
187   while (blkCnt > 0U)
188   {
189     /* C = |A| */
190 
191     /* Calculate absolute of input (if -1 then saturated to 0x7fffffff) and store result in destination buffer. */
192     in = *pSrc++;
193 #if defined (ARM_MATH_DSP)
194     *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
195 #else
196     *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
197 #endif
198 
199     /* Decrement loop counter */
200     blkCnt--;
201   }
202 
203 }
204 #endif /* #if defined (ARM_MATH_MVEI) */
205 /**
206   @} end of BasicAbs group
207  */
208