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