1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_max_q7.c
4  * Description:  Maximum value of a Q7 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 Max
37   @{
38  */
39 
40 /**
41   @brief         Maximum value of a Q7 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_small_blk_max_q7(const q7_t * pSrc,uint16_t blockSize,q7_t * pResult,uint32_t * pIndex)51 static void arm_small_blk_max_q7(
52     const q7_t * pSrc,
53     uint16_t blockSize,
54     q7_t * pResult,
55     uint32_t * pIndex)
56 {
57     int32_t        blkCnt;     /* loop counters */
58     q7x16_t        extremValVec = vdupq_n_s8(Q7_MIN);
59     q7_t           maxValue = Q7_MIN;
60     uint8x16_t     indexVec;
61     uint8x16_t     extremIdxVec;
62     mve_pred16_t   p0;
63     uint8_t        extremIdxArr[16];
64 
65     indexVec = vidupq_u8(0U, 1);
66 
67     blkCnt = blockSize;
68     do {
69         mve_pred16_t    p = vctp8q(blkCnt);
70         q7x16_t         extremIdxVal = vld1q_z_s8(pSrc, p);
71         /*
72          * Get current max per lane and current index per lane
73          * when a max is selected
74          */
75         p0 = vcmpgeq_m(extremIdxVal, extremValVec, p);
76 
77         extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
78         /* store per-lane extrema indexes */
79         vst1q_p_u8(extremIdxArr, indexVec, p0);
80 
81         indexVec += 16;
82         pSrc += 16;
83         blkCnt -= 16;
84     }
85     while (blkCnt > 0);
86 
87 
88     /* Get max value across the vector   */
89     maxValue = vmaxvq(maxValue, extremValVec);
90 
91     /* set index for lower values to max possible index   */
92     p0 = vcmpgeq(extremValVec, maxValue);
93     extremIdxVec = vld1q_u8(extremIdxArr);
94 
95     indexVec = vpselq(extremIdxVec, vdupq_n_u8(blockSize - 1), p0);
96     *pIndex = vminvq_u8(blockSize - 1, indexVec);
97     *pResult = maxValue;
98 }
99 
arm_max_q7(const q7_t * pSrc,uint32_t blockSize,q7_t * pResult,uint32_t * pIndex)100 ARM_DSP_ATTRIBUTE void arm_max_q7(
101   const q7_t * pSrc,
102         uint32_t blockSize,
103         q7_t * pResult,
104         uint32_t * pIndex)
105 {
106     int32_t   totalSize = blockSize;
107     const uint16_t sub_blk_sz = UINT8_MAX + 1;
108 
109     if (totalSize <= sub_blk_sz)
110     {
111         arm_small_blk_max_q7(pSrc, blockSize, pResult, pIndex);
112     }
113     else
114     {
115         uint32_t  curIdx = 0;
116         q7_t      curBlkExtr = Q7_MIN;
117         uint32_t  curBlkPos = 0;
118         uint32_t  curBlkIdx = 0;
119         /*
120          * process blocks of 255 elts
121          */
122         while (totalSize >= sub_blk_sz)
123         {
124             const q7_t     *curSrc = pSrc;
125 
126             arm_small_blk_max_q7(curSrc, sub_blk_sz, pResult, pIndex);
127             if (*pResult > curBlkExtr)
128             {
129                 /*
130                  * update partial extrema
131                  */
132                 curBlkExtr = *pResult;
133                 curBlkPos = *pIndex;
134                 curBlkIdx = curIdx;
135             }
136             curIdx++;
137             pSrc += sub_blk_sz;
138             totalSize -= sub_blk_sz;
139         }
140         /*
141          * remainder
142          */
143         arm_small_blk_max_q7(pSrc, totalSize, pResult, pIndex);
144         if (*pResult > curBlkExtr)
145         {
146             curBlkExtr = *pResult;
147             curBlkPos = *pIndex;
148             curBlkIdx = curIdx;
149         }
150         *pIndex = curBlkIdx * sub_blk_sz + curBlkPos;
151         *pResult = curBlkExtr;
152     }
153 }
154 #else
arm_max_q7(const q7_t * pSrc,uint32_t blockSize,q7_t * pResult,uint32_t * pIndex)155 ARM_DSP_ATTRIBUTE void arm_max_q7(
156   const q7_t * pSrc,
157         uint32_t blockSize,
158         q7_t * pResult,
159         uint32_t * pIndex)
160 {
161         q7_t maxVal, out;                              /* Temporary variables to store the output value. */
162         uint32_t blkCnt, outIndex;                     /* Loop counter */
163 
164 #if defined (ARM_MATH_LOOPUNROLL)
165         uint32_t index;                                /* index of maximum value */
166 #endif
167 
168   /* Initialise index value to zero. */
169   outIndex = 0U;
170   /* Load first input value that act as reference value for comparision */
171   out = *pSrc++;
172 
173 #if defined (ARM_MATH_LOOPUNROLL)
174   /* Initialise index of maximum value. */
175   index = 0U;
176 
177   /* Loop unrolling: Compute 4 outputs at a time */
178   blkCnt = (blockSize - 1U) >> 2U;
179 
180   while (blkCnt > 0U)
181   {
182     /* Initialize maxVal to next consecutive values one by one */
183     maxVal = *pSrc++;
184 
185     /* compare for the maximum value */
186     if (out < maxVal)
187     {
188       /* Update the maximum value and it's index */
189       out = maxVal;
190       outIndex = index + 1U;
191     }
192 
193     maxVal = *pSrc++;
194     if (out < maxVal)
195     {
196       out = maxVal;
197       outIndex = index + 2U;
198     }
199 
200     maxVal = *pSrc++;
201     if (out < maxVal)
202     {
203       out = maxVal;
204       outIndex = index + 3U;
205     }
206 
207     maxVal = *pSrc++;
208     if (out < maxVal)
209     {
210       out = maxVal;
211       outIndex = index + 4U;
212     }
213 
214     index += 4U;
215 
216     /* Decrement loop counter */
217     blkCnt--;
218   }
219 
220   /* Loop unrolling: Compute remaining outputs */
221   blkCnt = (blockSize - 1U) % 4U;
222 
223 #else
224 
225   /* Initialize blkCnt with number of samples */
226   blkCnt = (blockSize - 1U);
227 
228 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
229 
230   while (blkCnt > 0U)
231   {
232     /* Initialize maxVal to the next consecutive values one by one */
233     maxVal = *pSrc++;
234 
235     /* compare for the maximum value */
236     if (out < maxVal)
237     {
238       /* Update the maximum value and it's index */
239       out = maxVal;
240       outIndex = blockSize - blkCnt;
241     }
242 
243     /* Decrement loop counter */
244     blkCnt--;
245   }
246 
247   /* Store the maximum value and it's index into destination pointers */
248   *pResult = out;
249   *pIndex = outIndex;
250 }
251 #endif /* defined(ARM_MATH_MVEI) */
252 
253 /**
254   @} end of Max group
255  */
256