1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_absmax_q31.c
4 * Description: Maximum value of absolute values 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 @addtogroup AbsMax
37 @{
38 */
39
40 /**
41 @brief Maximum value of absolute values of a Q31 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 @return none
47 */
48
49 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
50
51 #include "arm_helium_utils.h"
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)52 void arm_absmax_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_ABSMIN);
60 q31_t maxValue = Q31_ABSMIN;
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 extremIdxVal = vabsq(extremIdxVal);
74 /*
75 * Get current max per lane and current index per lane
76 * when a max is selected
77 */
78 p0 = vcmpgeq_m(extremIdxVal, extremValVec, p);
79
80 extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
81 /* store per-lane extrema indexes */
82 vst1q_p_u32(extremIdxArr, indexVec, p0);
83
84 indexVec += 4;
85 pSrc += 4;
86 blkCnt -= 4;
87 }
88 while (blkCnt > 0);
89
90
91 /* Get max value across the vector */
92 maxValue = vmaxvq(maxValue, extremValVec);
93
94 /* set index for lower values to max possible index */
95 p0 = vcmpgeq(extremValVec, maxValue);
96 extremIdxVec = vld1q_u32(extremIdxArr);
97
98 indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
99 *pIndex = vminvq(blockSize - 1, indexVec);
100 *pResult = maxValue;
101 }
102 #else
103 #if defined(ARM_MATH_DSP)
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)104 void arm_absmax_q31(
105 const q31_t * pSrc,
106 uint32_t blockSize,
107 q31_t * pResult,
108 uint32_t * pIndex)
109 {
110 q31_t cur_absmax, out; /* Temporary variables to store the output value. */\
111 uint32_t blkCnt, outIndex; /* Loop counter */ \
112 uint32_t index; /* index of maximum value */ \
113 \
114 /* Initialize index value to zero. */ \
115 outIndex = 0U; \
116 /* Load first input value that act as reference value for comparision */ \
117 out = *pSrc++; \
118 out = (out > 0) ? out : (q31_t)__QSUB(0, out); \
119 /* Initialize index of extrema value. */ \
120 index = 0U; \
121 \
122 /* Loop unrolling: Compute 4 outputs at a time */ \
123 blkCnt = (blockSize - 1U) >> 2U; \
124 \
125 while (blkCnt > 0U) \
126 { \
127 /* Initialize cur_absmax to next consecutive values one by one */ \
128 cur_absmax = *pSrc++; \
129 cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
130 /* compare for the extrema value */ \
131 if (cur_absmax > out) \
132 { \
133 /* Update the extrema value and it's index */ \
134 out = cur_absmax; \
135 outIndex = index + 1U; \
136 } \
137 \
138 cur_absmax = *pSrc++; \
139 cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
140 if (cur_absmax > out) \
141 { \
142 out = cur_absmax; \
143 outIndex = index + 2U; \
144 } \
145 \
146 cur_absmax = *pSrc++; \
147 cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
148 if (cur_absmax > out) \
149 { \
150 out = cur_absmax; \
151 outIndex = index + 3U; \
152 } \
153 \
154 cur_absmax = *pSrc++; \
155 cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
156 if (cur_absmax > out) \
157 { \
158 out = cur_absmax; \
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 \
172 while (blkCnt > 0U) \
173 { \
174 cur_absmax = *pSrc++; \
175 cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
176 if (cur_absmax > out) \
177 { \
178 out = cur_absmax; \
179 outIndex = blockSize - blkCnt; \
180 } \
181 \
182 /* Decrement loop counter */ \
183 blkCnt--; \
184 } \
185 \
186 /* Store the extrema value and it's index into destination pointers */ \
187 *pResult = out; \
188 *pIndex = outIndex;
189 }
190 #else
arm_absmax_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult,uint32_t * pIndex)191 void arm_absmax_q31(
192 const q31_t * pSrc,
193 uint32_t blockSize,
194 q31_t * pResult,
195 uint32_t * pIndex)
196 {
197 q31_t maxVal, out; /* Temporary variables to store the output value. */
198 uint32_t blkCnt, outIndex; /* Loop counter */
199
200
201 /* Initialise index value to zero. */
202 outIndex = 0U;
203 /* Load first input value that act as reference value for comparision */
204 out = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
205 pSrc++;
206
207 /* Initialize blkCnt with number of samples */
208 blkCnt = (blockSize - 1U);
209
210 while (blkCnt > 0U)
211 {
212 /* Initialize maxVal to the next consecutive values one by one */
213 maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
214 pSrc++;
215
216 /* compare for the maximum value */
217 if (out < maxVal)
218 {
219 /* Update the maximum value and it's index */
220 out = maxVal;
221 outIndex = blockSize - blkCnt;
222 }
223
224 /* Decrement loop counter */
225 blkCnt--;
226 }
227
228 /* Store the maximum value and it's index into destination pointers */
229 *pResult = out;
230 *pIndex = outIndex;
231 }
232 #endif /* defined(ARM_MATH_DSP) */
233 #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
234 /**
235 @} end of AbsMax group
236 */
237