1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_min_f16.c
4  * Description:  Minimum value of a floating-point 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_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 
34 #if (defined(ARM_MATH_NEON) || defined(ARM_MATH_MVEF)) && !defined(ARM_MATH_AUTOVECTORIZE)
35 #include <limits.h>
36 #endif
37 
38 
39 /**
40   @ingroup groupStats
41  */
42 
43 /**
44   @addtogroup Min
45   @{
46  */
47 
48 /**
49   @brief         Minimum value of a floating-point vector.
50   @param[in]     pSrc       points to the input vector
51   @param[in]     blockSize  number of samples in input vector
52   @param[out]    pResult    minimum value returned here
53   @param[out]    pIndex     index of minimum value returned here
54  */
55 
56 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
57 
arm_min_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult,uint32_t * pIndex)58 ARM_DSP_ATTRIBUTE void arm_min_f16(
59   const float16_t * pSrc,
60   uint32_t blockSize,
61   float16_t * pResult,
62   uint32_t * pIndex)
63 {
64     int32_t  blkCnt;           /* loop counters */
65     f16x8_t vecSrc;
66     float16_t const *pSrcVec;
67     f16x8_t curExtremValVec = vdupq_n_f16(F16_MAX);
68     float16_t minValue = F16_MAX;
69     uint32_t  idx = blockSize;
70     uint16x8_t indexVec;
71     uint16x8_t curExtremIdxVec;
72     mve_pred16_t p0;
73 
74     indexVec = vidupq_u16((uint32_t)0, 1);
75     curExtremIdxVec = vdupq_n_u16(0);
76 
77     pSrcVec = (float16_t const *) pSrc;
78     blkCnt = blockSize >> 3;
79     while (blkCnt > 0)
80     {
81         vecSrc = vldrhq_f16(pSrcVec);  pSrcVec += 8;
82         /*
83          * Get current min per lane and current index per lane
84          * when a min is selected
85          */
86         p0 = vcmpleq(vecSrc, curExtremValVec);
87         curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
88         curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
89 
90         indexVec = indexVec + 8;
91         /*
92          * Decrement the blockSize loop counter
93          */
94         blkCnt--;
95     }
96     /*
97      * tail
98      * (will be merged thru tail predication)
99      */
100     blkCnt = blockSize & 7;
101     if (blkCnt > 0)
102     {
103         vecSrc = vldrhq_f16(pSrcVec);  pSrcVec += 8;
104         p0 = vctp16q(blkCnt);
105         /*
106          * Get current min per lane and current index per lane
107          * when a min is selected
108          */
109         p0 = vcmpleq_m(vecSrc, curExtremValVec, p0);
110         curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
111         curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
112     }
113     /*
114      * Get min value across the vector
115      */
116     minValue = vminnmvq(minValue, curExtremValVec);
117     /*
118      * set index for lower values to min possible index
119      */
120     p0 = vcmpleq(curExtremValVec, minValue);
121     indexVec = vpselq(curExtremIdxVec, vdupq_n_u16(blockSize), p0);
122     /*
123      * Get min index which is thus for a min value
124      */
125     idx = vminvq(idx, indexVec);
126     /*
127      * Save result
128      */
129     *pIndex = idx;
130     *pResult = minValue;
131 }
132 
133 #else
134 
arm_min_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult,uint32_t * pIndex)135 ARM_DSP_ATTRIBUTE void arm_min_f16(
136   const float16_t * pSrc,
137         uint32_t blockSize,
138         float16_t * pResult,
139         uint32_t * pIndex)
140 {
141         float16_t minVal, out;                         /* Temporary variables to store the output value. */
142         uint32_t blkCnt, outIndex;                     /* Loop counter */
143 
144 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
145         uint32_t index;                                /* index of maximum value */
146 #endif
147 
148   /* Initialise index value to zero. */
149   outIndex = 0U;
150 
151   /* Load first input value that act as reference value for comparision */
152   out = *pSrc++;
153 
154 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
155   /* Initialise index of maximum value. */
156   index = 0U;
157 
158   /* Loop unrolling: Compute 4 outputs at a time */
159   blkCnt = (blockSize - 1U) >> 2U;
160 
161   while (blkCnt > 0U)
162   {
163     /* Initialize minVal to next consecutive values one by one */
164     minVal = *pSrc++;
165 
166     /* compare for the minimum value */
167     if ((_Float16)out > (_Float16)minVal)
168     {
169       /* Update the minimum value and it's index */
170       out = minVal;
171       outIndex = index + 1U;
172     }
173 
174     minVal = *pSrc++;
175     if ((_Float16)out > (_Float16)minVal)
176     {
177       out = minVal;
178       outIndex = index + 2U;
179     }
180 
181     minVal = *pSrc++;
182     if ((_Float16)out > (_Float16)minVal)
183     {
184       out = minVal;
185       outIndex = index + 3U;
186     }
187 
188     minVal = *pSrc++;
189     if ((_Float16)out > (_Float16)minVal)
190     {
191       out = minVal;
192       outIndex = index + 4U;
193     }
194 
195     index += 4U;
196 
197     /* Decrement loop counter */
198     blkCnt--;
199   }
200 
201   /* Loop unrolling: Compute remaining outputs */
202   blkCnt = (blockSize - 1U) % 4U;
203 
204 #else
205 
206   /* Initialize blkCnt with number of samples */
207   blkCnt = (blockSize - 1U);
208 
209 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
210 
211   while (blkCnt > 0U)
212   {
213     /* Initialize minVal to the next consecutive values one by one */
214     minVal = *pSrc++;
215 
216     /* compare for the minimum value */
217     if ((_Float16)out > (_Float16)minVal)
218     {
219       /* Update the minimum value and it's index */
220       out = minVal;
221       outIndex = blockSize - blkCnt;
222     }
223 
224     /* Decrement loop counter */
225     blkCnt--;
226   }
227 
228   /* Store the minimum value and it's index into destination pointers */
229   *pResult = out;
230   *pIndex = outIndex;
231 }
232 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
233 
234 /**
235   @} end of Min group
236  */
237 
238 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
239 
240