1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_absmax_no_idx_q31.c
4  * Description:  Maximum value of absolute values of a Q31 vector
5  *
6  * $Date:        16 November 2021
7  * $Revision:    V1.10.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  */
46 
47 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
48 
49 #include "arm_helium_utils.h"
arm_absmax_no_idx_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)50 ARM_DSP_ATTRIBUTE void arm_absmax_no_idx_q31(
51   const q31_t * pSrc,
52         uint32_t blockSize,
53         q31_t * pResult)
54 {
55     int32_t  blkCnt;           /* loop counters */
56     q31x4_t       vecSrc;
57     q31_t   const *pSrcVec;
58     uint32x4_t    curExtremValVec = vdupq_n_u32(Q31_ABSMIN);
59     uint32_t           maxValue = Q31_ABSMIN;
60 
61 
62     pSrcVec = (q31_t const *) pSrc;
63     blkCnt = blockSize ;
64     while (blkCnt > 0)
65     {
66         mve_pred16_t    p = vctp32q(blkCnt);
67         vecSrc = vldrwq_z_s32(pSrcVec,p);
68         pSrcVec += 4;
69         /*
70          * update per-lane max.
71          */
72         curExtremValVec = vmaxaq_m(curExtremValVec, vecSrc,p);
73         /*
74          * Decrement the blockSize loop counter
75          */
76         blkCnt -= 4;
77     }
78     /*
79      * Get max value across the vector
80      */
81     maxValue = vmaxvq(maxValue, curExtremValVec);
82     *pResult = clip_q63_to_q31((q63_t)maxValue);
83 }
84 #else
85 #if defined(ARM_MATH_DSP)
arm_absmax_no_idx_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)86 ARM_DSP_ATTRIBUTE void arm_absmax_no_idx_q31(
87   const q31_t * pSrc,
88         uint32_t blockSize,
89         q31_t * pResult)
90 {
91         q31_t cur_absmax, out;                     /* Temporary variables to store the output value. */\
92         uint32_t blkCnt;                     /* Loop counter */                                   \
93                                                                                                             \
94                                                                                            \
95   /* Load first input value that act as reference value for comparision */                                  \
96   out = *pSrc++;                                                                                            \
97   out = (out > 0) ? out : (q31_t)__QSUB(0, out);                                                                           \
98                                                                                               \
99                                                                                                             \
100   /* Loop unrolling: Compute 4 outputs at a time */                                                         \
101   blkCnt = (blockSize - 1U) >> 2U;                                                                          \
102                                                                                                             \
103   while (blkCnt > 0U)                                                                                       \
104   {                                                                                                         \
105     /* Initialize cur_absmax to next consecutive values one by one */                                         \
106     cur_absmax = *pSrc++;                                                                                     \
107     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
108     /* compare for the extrema value */                                                                     \
109     if (cur_absmax > out)                                                                         \
110     {                                                                                                       \
111       /* Update the extrema value and it's index */                                                         \
112       out = cur_absmax;                                                                                       \
113     }                                                                                                       \
114                                                                                                             \
115     cur_absmax = *pSrc++;                                                                                     \
116     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
117     if (cur_absmax > out)                                                                         \
118     {                                                                                                       \
119       out = cur_absmax;                                                                                       \
120     }                                                                                                       \
121                                                                                                             \
122     cur_absmax = *pSrc++;                                                                                     \
123     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                \
124     if (cur_absmax > out)                                                                          \
125     {                                                                                                       \
126       out = cur_absmax;                                                                                       \
127     }                                                                                                       \
128                                                                                                             \
129     cur_absmax = *pSrc++;                                                                                     \
130     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                 \
131     if (cur_absmax > out)                                                                          \
132     {                                                                                                       \
133       out = cur_absmax;                                                                                       \
134     }                                                                                                       \
135                                                                                                             \
136                                                                                                             \
137     /* Decrement loop counter */                                                                            \
138     blkCnt--;                                                                                               \
139   }                                                                                                         \
140                                                                                                             \
141   /* Loop unrolling: Compute remaining outputs */                                                           \
142   blkCnt = (blockSize - 1U) % 4U;                                                                           \
143                                                                                                             \
144                                                                                                             \
145   while (blkCnt > 0U)                                                                                       \
146   {                                                                                                         \
147     cur_absmax = *pSrc++;                                                                                     \
148     cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax);                                                                 \
149     if (cur_absmax > out)                                                                         \
150     {                                                                                                       \
151       out = cur_absmax;                                                                                       \
152     }                                                                                                       \
153                                                                                                             \
154     /* Decrement loop counter */                                                                            \
155     blkCnt--;                                                                                               \
156   }                                                                                                         \
157                                                                                                             \
158   /* Store the extrema value and it's index into destination pointers */                                    \
159   *pResult = out;                                                                                           \
160 }
161 #else
arm_absmax_no_idx_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)162 ARM_DSP_ATTRIBUTE void arm_absmax_no_idx_q31(
163   const q31_t * pSrc,
164         uint32_t blockSize,
165         q31_t * pResult)
166 {
167         q31_t maxVal, out;                             /* Temporary variables to store the output value. */
168         uint32_t blkCnt;                     /* Loop counter */
169 
170 
171 
172   /* Load first input value that act as reference value for comparision */
173   out = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
174   pSrc++;
175 
176   /* Initialize blkCnt with number of samples */
177   blkCnt = (blockSize - 1U);
178 
179   while (blkCnt > 0U)
180   {
181     /* Initialize maxVal to the next consecutive values one by one */
182     maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
183     pSrc++;
184 
185     /* compare for the maximum value */
186     if (out < maxVal)
187     {
188       /* Update the maximum value and it's index */
189       out = maxVal;
190     }
191 
192     /* Decrement loop counter */
193     blkCnt--;
194   }
195 
196   /* Store the maximum value and it's index into destination pointers */
197   *pResult = out;
198 }
199 #endif /* defined(ARM_MATH_DSP) */
200 #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
201 /**
202   @} end of AbsMax group
203  */
204