1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_abs_q15.c
4  * Description:  Q15 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         Q15 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           Scaling and Overflow Behavior
48                    The function uses saturating arithmetic.
49                    The Q15 value -1 (0x8000) will be saturated to the maximum allowable positive value 0x7FFF.
50  */
51 
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
53 
54 #include "arm_helium_utils.h"
55 
arm_abs_q15(const q15_t * pSrc,q15_t * pDst,uint32_t blockSize)56 void arm_abs_q15(
57     const q15_t * pSrc,
58     q15_t * pDst,
59     uint32_t blockSize)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q15x8_t vecSrc;
63 
64     /* Compute 8 outputs at a time */
65     blkCnt = blockSize >> 3;
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 += 8;
82         pDst += 8;
83     }
84     /*
85      * tail
86      */
87     blkCnt = blockSize & 7;
88     if (blkCnt > 0U)
89     {
90         mve_pred16_t p0 = vctp16q(blkCnt);
91         vecSrc = vld1q(pSrc);
92         vstrhq_p(pDst, vqabsq(vecSrc), p0);
93     }
94 }
95 
96 #else
arm_abs_q15(const q15_t * pSrc,q15_t * pDst,uint32_t blockSize)97 void arm_abs_q15(
98   const q15_t * pSrc,
99         q15_t * pDst,
100         uint32_t blockSize)
101 {
102         uint32_t blkCnt;                               /* Loop counter */
103         q15_t in;                                      /* Temporary input variable */
104 
105 #if defined (ARM_MATH_LOOPUNROLL)
106 
107   /* Loop unrolling: Compute 4 outputs at a time */
108   blkCnt = blockSize >> 2U;
109 
110   while (blkCnt > 0U)
111   {
112     /* C = |A| */
113 
114     /* Calculate absolute of input (if -1 then saturated to 0x7fff) and store result in destination buffer. */
115     in = *pSrc++;
116 #if defined (ARM_MATH_DSP)
117     *pDst++ = (in > 0) ? in : (q15_t)__QSUB16(0, in);
118 #else
119     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
120 #endif
121 
122     in = *pSrc++;
123 #if defined (ARM_MATH_DSP)
124     *pDst++ = (in > 0) ? in : (q15_t)__QSUB16(0, in);
125 #else
126     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
127 #endif
128 
129     in = *pSrc++;
130 #if defined (ARM_MATH_DSP)
131     *pDst++ = (in > 0) ? in : (q15_t)__QSUB16(0, in);
132 #else
133     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
134 #endif
135 
136     in = *pSrc++;
137 #if defined (ARM_MATH_DSP)
138     *pDst++ = (in > 0) ? in : (q15_t)__QSUB16(0, in);
139 #else
140     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
141 #endif
142 
143     /* Decrement loop counter */
144     blkCnt--;
145   }
146 
147   /* Loop unrolling: Compute remaining outputs */
148   blkCnt = blockSize % 0x4U;
149 
150 #else
151 
152   /* Initialize blkCnt with number of samples */
153   blkCnt = blockSize;
154 
155 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
156 
157   while (blkCnt > 0U)
158   {
159     /* C = |A| */
160 
161     /* Calculate absolute of input (if -1 then saturated to 0x7fff) and store result in destination buffer. */
162     in = *pSrc++;
163 #if defined (ARM_MATH_DSP)
164     *pDst++ = (in > 0) ? in : (q15_t)__QSUB16(0, in);
165 #else
166     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
167 #endif
168 
169     /* Decrement loop counter */
170     blkCnt--;
171   }
172 
173 }
174 #endif /* defined(ARM_MATH_MVEI) */
175 
176 /**
177   @} end of BasicAbs group
178  */
179