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