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