1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_abs_q7.c
4  * Description:  Q7 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         Q7 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           Conditions for optimum performance
47                    Input and output buffers should be aligned by 32-bit
48   @par           Scaling and Overflow Behavior
49                    The function uses saturating arithmetic.
50                    The Q7 value -1 (0x80) will be saturated to the maximum allowable positive value 0x7F.
51  */
52 
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
54 
55 #include "arm_helium_utils.h"
56 
arm_abs_q7(const q7_t * pSrc,q7_t * pDst,uint32_t blockSize)57 void arm_abs_q7(
58     const q7_t * pSrc,
59     q7_t * pDst,
60     uint32_t blockSize)
61 {
62     uint32_t  blkCnt;           /* loop counters */
63     q7x16_t vecSrc;
64 
65     /* Compute 16 outputs at a time */
66     blkCnt = blockSize >> 4;
67     while (blkCnt > 0U)
68     {
69         /*
70          * C = |A|
71          * Calculate absolute and then store the results in the destination buffer.
72          */
73         vecSrc = vld1q(pSrc);
74         vst1q(pDst, vqabsq(vecSrc));
75         /*
76          * Decrement the blockSize loop counter
77          */
78         blkCnt--;
79         /*
80          * advance vector source and destination pointers
81          */
82         pSrc += 16;
83         pDst += 16;
84     }
85     /*
86      * tail
87      */
88     blkCnt = blockSize & 0xF;
89     if (blkCnt > 0U)
90     {
91         mve_pred16_t p0 = vctp8q(blkCnt);
92         vecSrc = vld1q(pSrc);
93         vstrbq_p(pDst, vqabsq(vecSrc), p0);
94     }
95 }
96 
97 #else
arm_abs_q7(const q7_t * pSrc,q7_t * pDst,uint32_t blockSize)98 void arm_abs_q7(
99   const q7_t * pSrc,
100         q7_t * pDst,
101         uint32_t blockSize)
102 {
103         uint32_t blkCnt;                               /* Loop counter */
104         q7_t in;                                       /* Temporary input variable */
105 
106 #if defined (ARM_MATH_LOOPUNROLL)
107 
108   /* Loop unrolling: Compute 4 outputs at a time */
109   blkCnt = blockSize >> 2U;
110 
111   while (blkCnt > 0U)
112   {
113     /* C = |A| */
114 
115     /* Calculate absolute of input (if -1 then saturated to 0x7f) and store result in destination buffer. */
116     in = *pSrc++;
117 #if defined (ARM_MATH_DSP)
118     *pDst++ = (in > 0) ? in : (q7_t)__QSUB8(0, in);
119 #else
120     *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? (q7_t) 0x7f : -in);
121 #endif
122 
123     in = *pSrc++;
124 #if defined (ARM_MATH_DSP)
125     *pDst++ = (in > 0) ? in : (q7_t)__QSUB8(0, in);
126 #else
127     *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? (q7_t) 0x7f : -in);
128 #endif
129 
130     in = *pSrc++;
131 #if defined (ARM_MATH_DSP)
132     *pDst++ = (in > 0) ? in : (q7_t)__QSUB8(0, in);
133 #else
134     *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? (q7_t) 0x7f : -in);
135 #endif
136 
137     in = *pSrc++;
138 #if defined (ARM_MATH_DSP)
139     *pDst++ = (in > 0) ? in : (q7_t)__QSUB8(0, in);
140 #else
141     *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? (q7_t) 0x7f : -in);
142 #endif
143 
144     /* Decrement loop counter */
145     blkCnt--;
146   }
147 
148   /* Loop unrolling: Compute remaining outputs */
149   blkCnt = blockSize % 0x4U;
150 
151 #else
152 
153   /* Initialize blkCnt with number of samples */
154   blkCnt = blockSize;
155 
156 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
157 
158   while (blkCnt > 0U)
159   {
160     /* C = |A| */
161 
162     /* Calculate absolute of input (if -1 then saturated to 0x7f) and store result in destination buffer. */
163     in = *pSrc++;
164 #if defined (ARM_MATH_DSP)
165     *pDst++ = (in > 0) ? in : (q7_t) __QSUB8(0, in);
166 #else
167     *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? (q7_t) 0x7f : -in);
168 #endif
169 
170     /* Decrement loop counter */
171     blkCnt--;
172   }
173 
174 }
175 #endif /* defined(ARM_MATH_MVEI) */
176 
177 /**
178   @} end of BasicAbs group
179  */
180