1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_absmax_q15.c
4  * Description:  Maximum value of absolute values of a Q15 vector
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/statistics_functions.h"
30 
31 /**
32   @ingroup groupStats
33  */
34 
35 /**
36   @addtogroup AbsMax
37   @{
38  */
39 
40 /**
41   @brief         Maximum value of absolute values of a Q15 vector.
42   @param[in]     pSrc       points to the input vector
43   @param[in]     blockSize  number of samples in input vector
44   @param[out]    pResult    maximum value returned here
45   @param[out]    pIndex     index of maximum value returned here
46  */
47 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
48 
49 #include "arm_helium_utils.h"
50 
arm_absmax_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)51 ARM_DSP_ATTRIBUTE void arm_absmax_q15(
52   const q15_t * pSrc,
53         uint32_t blockSize,
54         q15_t * pResult,
55         uint32_t * pIndex)
56 {
57       int32_t         blkCnt;     /* loop counters */
58     q15x8_t         extremValVec = vdupq_n_s16(Q15_ABSMIN);
59     q15_t           maxValue = Q15_ABSMIN;
60     uint16x8_t      indexVec;
61     uint16x8_t      extremIdxVec;
62     mve_pred16_t    p0;
63     uint16_t        extremIdxArr[8];
64 
65     indexVec = vidupq_u16(0U, 1);
66 
67     blkCnt = blockSize;
68     do {
69         mve_pred16_t    p = vctp16q(blkCnt);
70         q15x8_t         extremIdxVal = vld1q_z_s16(pSrc, p);
71 
72         extremIdxVal = vqabsq(extremIdxVal);
73         /*
74          * Get current max per lane and current index per lane
75          * when a max is selected
76          */
77         p0 = vcmpgtq_m(extremIdxVal, extremValVec, p);
78 
79         extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
80         /* store per-lane extrema indexes */
81         vst1q_p_u16(extremIdxArr, indexVec, p0);
82 
83         indexVec += 8;
84         pSrc += 8;
85         blkCnt -= 8;
86     }
87     while (blkCnt > 0);
88 
89 
90     /* Get max value across the vector   */
91     maxValue = vmaxvq(maxValue, extremValVec);
92 
93     /* set index for lower values to max possible index   */
94     p0 = vcmpgeq(extremValVec, maxValue);
95     extremIdxVec = vld1q_u16(extremIdxArr);
96 
97     indexVec = vpselq(extremIdxVec, vdupq_n_u16(blockSize - 1), p0);
98     *pIndex = vminvq(blockSize - 1, indexVec);
99     *pResult = maxValue;
100 }
101 
102 #else
103 #if defined(ARM_MATH_DSP)
arm_absmax_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)104 ARM_DSP_ATTRIBUTE void arm_absmax_q15(
105   const q15_t * pSrc,
106         uint32_t blockSize,
107         q15_t * pResult,
108         uint32_t * pIndex)
109 {
110         q15_t cur_absmax, out;                     /* Temporary variables to store the output value. */\
111         uint32_t blkCnt, outIndex;                     /* Loop counter */                                   \
112         uint32_t index;                                /* index of maximum value */                         \
113                                                                                                             \
114   /* Initialize index value to zero. */                                                                     \
115   outIndex = 0U;                                                                                            \
116   /* Load first input value that act as reference value for comparision */                                  \
117   out = *pSrc++;                                                                                            \
118   out = (out > 0) ? out : (q15_t)__QSUB16(0, out);                                                                           \
119   /* Initialize index of extrema value. */                                                                  \
120   index = 0U;                                                                                               \
121                                                                                                             \
122   /* Loop unrolling: Compute 4 outputs at a time */                                                         \
123   blkCnt = (blockSize - 1U) >> 2U;                                                                          \
124                                                                                                             \
125   while (blkCnt > 0U)                                                                                       \
126   {                                                                                                         \
127     /* Initialize cur_absmax to next consecutive values one by one */                                         \
128     cur_absmax = *pSrc++;                                                                                     \
129     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q15_t)__QSUB16(0, cur_absmax);                                                                \
130     /* compare for the extrema value */                                                                     \
131     if (cur_absmax > out)                                                                         \
132     {                                                                                                       \
133       /* Update the extrema value and it's index */                                                         \
134       out = cur_absmax;                                                                                       \
135       outIndex = index + 1U;                                                                                \
136     }                                                                                                       \
137                                                                                                             \
138     cur_absmax = *pSrc++;                                                                                     \
139     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q15_t)__QSUB16(0, cur_absmax);                                                                \
140     if (cur_absmax > out)                                                                         \
141     {                                                                                                       \
142       out = cur_absmax;                                                                                       \
143       outIndex = index + 2U;                                                                                \
144     }                                                                                                       \
145                                                                                                             \
146     cur_absmax = *pSrc++;                                                                                     \
147     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q15_t)__QSUB16(0, cur_absmax);                                                                \
148     if (cur_absmax > out)                                                                          \
149     {                                                                                                       \
150       out = cur_absmax;                                                                                       \
151       outIndex = index + 3U;                                                                                \
152     }                                                                                                       \
153                                                                                                             \
154     cur_absmax = *pSrc++;                                                                                     \
155     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q15_t)__QSUB16(0, cur_absmax);                                                                 \
156     if (cur_absmax > out)                                                                          \
157     {                                                                                                       \
158       out = cur_absmax;                                                                                       \
159       outIndex = index + 4U;                                                                                \
160     }                                                                                                       \
161                                                                                                             \
162     index += 4U;                                                                                            \
163                                                                                                             \
164     /* Decrement loop counter */                                                                            \
165     blkCnt--;                                                                                               \
166   }                                                                                                         \
167                                                                                                             \
168   /* Loop unrolling: Compute remaining outputs */                                                           \
169   blkCnt = (blockSize - 1U) % 4U;                                                                           \
170                                                                                                             \
171                                                                                                             \
172   while (blkCnt > 0U)                                                                                       \
173   {                                                                                                         \
174     cur_absmax = *pSrc++;                                                                                     \
175     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q15_t)__QSUB16(0, cur_absmax);                                                                 \
176     if (cur_absmax > out)                                                                         \
177     {                                                                                                       \
178       out = cur_absmax;                                                                                       \
179       outIndex = blockSize - blkCnt;                                                                        \
180     }                                                                                                       \
181                                                                                                             \
182     /* Decrement loop counter */                                                                            \
183     blkCnt--;                                                                                               \
184   }                                                                                                         \
185                                                                                                             \
186   /* Store the extrema value and it's index into destination pointers */                                    \
187   *pResult = out;                                                                                           \
188   *pIndex = outIndex;
189 }
190 #else
arm_absmax_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)191 ARM_DSP_ATTRIBUTE void arm_absmax_q15(
192   const q15_t * pSrc,
193         uint32_t blockSize,
194         q15_t * pResult,
195         uint32_t * pIndex)
196 {
197        q15_t maxVal, out;                             /* Temporary variables to store the output value. */
198         uint32_t blkCnt, outIndex;                     /* Loop counter */
199 
200   /* Initialise index value to zero. */
201   outIndex = 0U;
202   /* Load first input value that act as reference value for comparision */
203   out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
204   pSrc++;
205 
206   /* Initialize blkCnt with number of samples */
207   blkCnt = (blockSize - 1U);
208 
209   while (blkCnt > 0U)
210   {
211     /* Initialize maxVal to the next consecutive values one by one */
212     maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
213     pSrc++;
214 
215     /* compare for the maximum value */
216     if (out < maxVal)
217     {
218       /* Update the maximum value and it's index */
219       out = maxVal;
220       outIndex = blockSize - blkCnt;
221     }
222 
223     /* Decrement loop counter */
224     blkCnt--;
225   }
226 
227   /* Store the maximum value and it's index into destination pointers */
228   *pResult = out;
229   *pIndex = outIndex;
230 }
231 #endif /* defined(ARM_MATH_DSP) */
232 #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
233 /**
234   @} end of AbsMax group
235  */
236