1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_absmax_q31.c
4  * Description:  Maximum value of absolute values of a Q31 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 Q31 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 
48 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
49 
50 #include "arm_helium_utils.h"
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)51 void arm_absmax_q31(
52   const q31_t * pSrc,
53         uint32_t blockSize,
54         q31_t * pResult,
55         uint32_t * pIndex)
56 {
57     int32_t         blkCnt;     /* loop counters */
58     q31x4_t         extremValVec = vdupq_n_s32(Q31_ABSMIN);
59     q31_t           maxValue = Q31_ABSMIN;
60     uint32x4_t      indexVec;
61     uint32x4_t      extremIdxVec;
62     mve_pred16_t    p0;
63     uint32_t        extremIdxArr[4];
64 
65     indexVec = vidupq_u32(0U, 1);
66 
67     blkCnt = blockSize;
68     do {
69         mve_pred16_t    p = vctp32q(blkCnt);
70         q31x4_t         extremIdxVal = vld1q_z_s32(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_u32(extremIdxArr, indexVec, p0);
82 
83         indexVec += 4;
84         pSrc += 4;
85         blkCnt -= 4;
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_u32(extremIdxArr);
96 
97     indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
98     *pIndex = vminvq(blockSize - 1, indexVec);
99     *pResult = maxValue;
100 }
101 #else
102 #if defined(ARM_MATH_DSP)
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)103 void arm_absmax_q31(
104   const q31_t * pSrc,
105         uint32_t blockSize,
106         q31_t * pResult,
107         uint32_t * pIndex)
108 {
109         q31_t cur_absmax, out;                     /* Temporary variables to store the output value. */\
110         uint32_t blkCnt, outIndex;                     /* Loop counter */                                   \
111         uint32_t index;                                /* index of maximum value */                         \
112                                                                                                             \
113   /* Initialize index value to zero. */                                                                     \
114   outIndex = 0U;                                                                                            \
115   /* Load first input value that act as reference value for comparision */                                  \
116   out = *pSrc++;                                                                                            \
117   out = (out > 0) ? out : (q31_t)__QSUB(0, out);                                                                           \
118   /* Initialize index of extrema value. */                                                                  \
119   index = 0U;                                                                                               \
120                                                                                                             \
121   /* Loop unrolling: Compute 4 outputs at a time */                                                         \
122   blkCnt = (blockSize - 1U) >> 2U;                                                                          \
123                                                                                                             \
124   while (blkCnt > 0U)                                                                                       \
125   {                                                                                                         \
126     /* Initialize cur_absmax to next consecutive values one by one */                                         \
127     cur_absmax = *pSrc++;                                                                                     \
128     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
129     /* compare for the extrema value */                                                                     \
130     if (cur_absmax > out)                                                                         \
131     {                                                                                                       \
132       /* Update the extrema value and it's index */                                                         \
133       out = cur_absmax;                                                                                       \
134       outIndex = index + 1U;                                                                                \
135     }                                                                                                       \
136                                                                                                             \
137     cur_absmax = *pSrc++;                                                                                     \
138     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
139     if (cur_absmax > out)                                                                         \
140     {                                                                                                       \
141       out = cur_absmax;                                                                                       \
142       outIndex = index + 2U;                                                                                \
143     }                                                                                                       \
144                                                                                                             \
145     cur_absmax = *pSrc++;                                                                                     \
146     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
147     if (cur_absmax > out)                                                                          \
148     {                                                                                                       \
149       out = cur_absmax;                                                                                       \
150       outIndex = index + 3U;                                                                                \
151     }                                                                                                       \
152                                                                                                             \
153     cur_absmax = *pSrc++;                                                                                     \
154     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                 \
155     if (cur_absmax > out)                                                                          \
156     {                                                                                                       \
157       out = cur_absmax;                                                                                       \
158       outIndex = index + 4U;                                                                                \
159     }                                                                                                       \
160                                                                                                             \
161     index += 4U;                                                                                            \
162                                                                                                             \
163     /* Decrement loop counter */                                                                            \
164     blkCnt--;                                                                                               \
165   }                                                                                                         \
166                                                                                                             \
167   /* Loop unrolling: Compute remaining outputs */                                                           \
168   blkCnt = (blockSize - 1U) % 4U;                                                                           \
169                                                                                                             \
170                                                                                                             \
171   while (blkCnt > 0U)                                                                                       \
172   {                                                                                                         \
173     cur_absmax = *pSrc++;                                                                                     \
174     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                 \
175     if (cur_absmax > out)                                                                         \
176     {                                                                                                       \
177       out = cur_absmax;                                                                                       \
178       outIndex = blockSize - blkCnt;                                                                        \
179     }                                                                                                       \
180                                                                                                             \
181     /* Decrement loop counter */                                                                            \
182     blkCnt--;                                                                                               \
183   }                                                                                                         \
184                                                                                                             \
185   /* Store the extrema value and it's index into destination pointers */                                    \
186   *pResult = out;                                                                                           \
187   *pIndex = outIndex;
188 }
189 #else
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)190 void arm_absmax_q31(
191   const q31_t * pSrc,
192         uint32_t blockSize,
193         q31_t * pResult,
194         uint32_t * pIndex)
195 {
196         q31_t maxVal, out;                             /* Temporary variables to store the output value. */
197         uint32_t blkCnt, outIndex;                     /* Loop counter */
198 
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 == INT32_MIN) ? INT32_MAX : -*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 == INT32_MIN) ? INT32_MAX : -*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