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