1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_min_q15.c
4  * Description:  Minimum 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 /**
37   @addtogroup Min
38   @{
39  */
40 
41 /**
42   @brief         Minimum value of a Q15 vector.
43   @param[in]     pSrc       points to the input vector
44   @param[in]     blockSize  number of samples in input vector
45   @param[out]    pResult    minimum value returned here
46   @param[out]    pIndex     index of minimum value returned here
47  */
48 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
49 
50 #include "arm_helium_utils.h"
51 
arm_min_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)52 ARM_DSP_ATTRIBUTE void arm_min_q15(
53   const q15_t * pSrc,
54         uint32_t blockSize,
55         q15_t * pResult,
56         uint32_t * pIndex)
57 {
58 
59     int32_t         blkCnt;     /* loop counters */
60     q15x8_t         extremValVec = vdupq_n_s16(Q15_MAX);
61     q15_t           minValue = Q15_MAX;
62     uint16x8_t      indexVec;
63     uint16x8_t      extremIdxVec;
64     mve_pred16_t    p0;
65     uint16_t        extremIdxArr[8];
66 
67     indexVec = vidupq_u16(0U, 1);
68 
69     blkCnt = blockSize;
70     do {
71         mve_pred16_t    p = vctp16q(blkCnt);
72         q15x8_t         extremIdxVal = vld1q_z_s16(pSrc, p);
73         /*
74          * Get current min per lane and current index per lane
75          * when a min is selected
76          */
77         p0 = vcmpleq_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     /* Get min value across the vector   */
90     minValue = vminvq(minValue, extremValVec);
91 
92     /* set index for lower values to min possible index   */
93     p0 = vcmpleq(extremValVec, minValue);
94     extremIdxVec = vld1q_u16(extremIdxArr);
95 
96     indexVec = vpselq(extremIdxVec, vdupq_n_u16(blockSize - 1), p0);
97     *pIndex = vminvq(blockSize - 1, indexVec);
98     *pResult = minValue;
99 
100 }
101 #else
arm_min_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult,uint32_t * pIndex)102 ARM_DSP_ATTRIBUTE void arm_min_q15(
103   const q15_t * pSrc,
104         uint32_t blockSize,
105         q15_t * pResult,
106         uint32_t * pIndex)
107 {
108         q15_t minVal, out;                             /* Temporary variables to store the output value. */
109         uint32_t blkCnt, outIndex;                     /* Loop counter */
110 
111 #if defined (ARM_MATH_LOOPUNROLL)
112         uint32_t index;                                /* index of maximum value */
113 #endif
114 
115   /* Initialise index value to zero. */
116   outIndex = 0U;
117   /* Load first input value that act as reference value for comparision */
118   out = *pSrc++;
119 
120 #if defined (ARM_MATH_LOOPUNROLL)
121   /* Initialise index of maximum value. */
122   index = 0U;
123 
124   /* Loop unrolling: Compute 4 outputs at a time */
125   blkCnt = (blockSize - 1U) >> 2U;
126 
127   while (blkCnt > 0U)
128   {
129     /* Initialize minVal to next consecutive values one by one */
130     minVal = *pSrc++;
131 
132     /* compare for the minimum value */
133     if (out > minVal)
134     {
135       /* Update the minimum value and it's index */
136       out = minVal;
137       outIndex = index + 1U;
138     }
139 
140     minVal = *pSrc++;
141     if (out > minVal)
142     {
143       out = minVal;
144       outIndex = index + 2U;
145     }
146 
147     minVal = *pSrc++;
148     if (out > minVal)
149     {
150       out = minVal;
151       outIndex = index + 3U;
152     }
153 
154     minVal = *pSrc++;
155     if (out > minVal)
156     {
157       out = minVal;
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 #else
171 
172   /* Initialize blkCnt with number of samples */
173   blkCnt = (blockSize - 1U);
174 
175 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
176 
177   while (blkCnt > 0U)
178   {
179     /* Initialize minVal to the next consecutive values one by one */
180     minVal = *pSrc++;
181 
182     /* compare for the minimum value */
183     if (out > minVal)
184     {
185       /* Update the minimum value and it's index */
186       out = minVal;
187       outIndex = blockSize - blkCnt;
188     }
189 
190     /* Decrement loop counter */
191     blkCnt--;
192   }
193 
194   /* Store the minimum value and it's index into destination pointers */
195   *pResult = out;
196   *pIndex = outIndex;
197 }
198 #endif /* defined(ARM_MATH_MVEI) */
199 
200 /**
201   @} end of Min group
202  */
203