1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_max_q15.c
4  * Description:  Maximum value 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 Max
37   @{
38  */
39 
40 /**
41   @brief         Maximum value 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_max_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)51 void arm_max_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_MIN);
59     q15_t           maxValue = Q15_MIN;
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          * 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_u16(extremIdxArr, indexVec, p0);
80 
81         indexVec += 8;
82         pSrc += 8;
83         blkCnt -= 8;
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_u16(extremIdxArr);
94 
95     indexVec = vpselq(extremIdxVec, vdupq_n_u16(blockSize - 1), p0);
96     *pIndex = vminvq(blockSize - 1, indexVec);
97     *pResult = maxValue;
98 }
99 
100 #else
arm_max_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)101 void arm_max_q15(
102   const q15_t * pSrc,
103         uint32_t blockSize,
104         q15_t * pResult,
105         uint32_t * pIndex)
106 {
107         q15_t maxVal, out;                             /* Temporary variables to store the output value. */
108         uint32_t blkCnt, outIndex;                     /* Loop counter */
109 
110 #if defined (ARM_MATH_LOOPUNROLL)
111         uint32_t index;                                /* index of maximum value */
112 #endif
113 
114   /* Initialise index value to zero. */
115   outIndex = 0U;
116   /* Load first input value that act as reference value for comparision */
117   out = *pSrc++;
118 
119 #if defined (ARM_MATH_LOOPUNROLL)
120   /* Initialise index of maximum 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 maxVal to next consecutive values one by one */
129     maxVal = *pSrc++;
130 
131     /* compare for the maximum value */
132     if (out < maxVal)
133     {
134       /* Update the maximum value and it's index */
135       out = maxVal;
136       outIndex = index + 1U;
137     }
138 
139     maxVal = *pSrc++;
140     if (out < maxVal)
141     {
142       out = maxVal;
143       outIndex = index + 2U;
144     }
145 
146     maxVal = *pSrc++;
147     if (out < maxVal)
148     {
149       out = maxVal;
150       outIndex = index + 3U;
151     }
152 
153     maxVal = *pSrc++;
154     if (out < maxVal)
155     {
156       out = maxVal;
157       outIndex = index + 4U;
158     }
159 
160     index += 4U;
161 
162     /* Decrement loop counter */
163     blkCnt--;
164   }
165 
166   /* Loop unrolling: Compute remaining outputs */
167   blkCnt = (blockSize - 1U) % 4U;
168 
169 #else
170 
171   /* Initialize blkCnt with number of samples */
172   blkCnt = (blockSize - 1U);
173 
174 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
175 
176   while (blkCnt > 0U)
177   {
178     /* Initialize maxVal to the next consecutive values one by one */
179     maxVal = *pSrc++;
180 
181     /* compare for the maximum value */
182     if (out < maxVal)
183     {
184       /* Update the maximum value and it's index */
185       out = maxVal;
186       outIndex = blockSize - blkCnt;
187     }
188 
189     /* Decrement loop counter */
190     blkCnt--;
191   }
192 
193   /* Store the maximum value and it's index into destination pointers */
194   *pResult = out;
195   *pIndex = outIndex;
196 }
197 #endif /* defined(ARM_MATH_MVEI) */
198 /**
199   @} end of Max group
200  */
201