1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_min_q31.c
4  * Description:  Minimum value 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 /**
37   @addtogroup Min
38   @{
39  */
40 
41 /**
42   @brief         Minimum value of a Q31 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_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)52 ARM_DSP_ATTRIBUTE void arm_min_q31(
53   const q31_t * pSrc,
54         uint32_t blockSize,
55         q31_t * pResult,
56         uint32_t * pIndex)
57 {
58     int32_t         blkCnt;     /* loop counters */
59     q31x4_t         extremValVec = vdupq_n_s32(Q31_MAX);
60     q31_t           minValue = Q31_MAX;
61     uint32x4_t      indexVec;
62     uint32x4_t      extremIdxVec;
63     mve_pred16_t    p0;
64     uint32_t        extremIdxArr[4];
65 
66     indexVec = vidupq_u32(0U, 1);
67 
68     blkCnt = blockSize;
69     do {
70         mve_pred16_t    p = vctp32q(blkCnt);
71         q31x4_t         extremIdxVal = vld1q_z_s32(pSrc, p);
72         /*
73          * Get current min per lane and current index per lane
74          * when a min is selected
75          */
76         p0 = vcmpleq_m(extremIdxVal, extremValVec, p);
77 
78         extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
79         /* store per-lane extrema indexes */
80         vst1q_p_u32(extremIdxArr, indexVec, p0);
81 
82         indexVec += 4;
83         pSrc += 4;
84         blkCnt -= 4;
85     }
86     while (blkCnt > 0);
87 
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_u32(extremIdxArr);
95 
96     indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
97     *pIndex = vminvq(blockSize - 1, indexVec);
98     *pResult = minValue;
99 }
100 
101 #else
arm_min_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)102 ARM_DSP_ATTRIBUTE void arm_min_q31(
103   const q31_t * pSrc,
104         uint32_t blockSize,
105         q31_t * pResult,
106         uint32_t * pIndex)
107 {
108         q31_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