1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_absmin_no_idx_q15.c
4  * Description:  Minimum value of absolute values of a Q15 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 /**
37   @addtogroup AbsMin
38   @{
39  */
40 
41 /**
42   @brief         Minimum value of absolute values 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  */
47 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
48 
49 #include "arm_helium_utils.h"
50 
arm_absmin_no_idx_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)51 void arm_absmin_no_idx_q15(
52   const q15_t * pSrc,
53         uint32_t blockSize,
54         q15_t * pResult)
55 {
56     uint16_t        blkCnt;           /* loop counters */
57     q15x8_t       vecSrc;
58     q15_t   const *pSrcVec;
59     uint16x8_t    curExtremValVec = vdupq_n_s16(Q15_ABSMAX);
60     q15_t           minValue = Q15_ABSMAX;
61     mve_pred16_t    p0;
62 
63 
64     pSrcVec = (q15_t const *) pSrc;
65     blkCnt = blockSize >> 3;
66     while (blkCnt > 0)
67     {
68         vecSrc = vld1q(pSrcVec);
69         pSrcVec += 8;
70         /*
71          * update per-lane min.
72          */
73         curExtremValVec = vminaq(curExtremValVec, vecSrc);
74         /*
75          * Decrement the blockSize loop counter
76          */
77         blkCnt--;
78     }
79     /*
80      * tail
81      * (will be merged thru tail predication)
82      */
83     blkCnt = blockSize & 7;
84     if (blkCnt > 0)
85     {
86         vecSrc = vld1q(pSrcVec);
87         pSrcVec += 8;
88         p0 = vctp16q(blkCnt);
89         /*
90          * Get current min per lane and current index per lane
91          * when a min is selected
92          */
93          curExtremValVec = vminaq_m(curExtremValVec, vecSrc, p0);
94     }
95     /*
96      * Get min value across the vector
97      */
98     minValue = vminavq(minValue, (q15x8_t)curExtremValVec);
99     *pResult = minValue;
100 }
101 
102 #else
103 #if defined(ARM_MATH_DSP)
arm_absmin_no_idx_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)104 void arm_absmin_no_idx_q15(
105   const q15_t * pSrc,
106         uint32_t blockSize,
107         q15_t * pResult)
108 {
109         q15_t cur_absmin, out;                     /* Temporary variables to store the output value. */\
110         uint32_t blkCnt;                     /* Loop counter */                                   \
111                                                                                                             \
112                                                                                            \
113   /* Load first input value that act as reference value for comparision */                                  \
114   out = *pSrc++;                                                                                            \
115   out = (out > 0) ? out : (q15_t)__QSUB16(0, out);                                                                           \
116                                                                                              \
117                                                                                                             \
118   /* Loop unrolling: Compute 4 outputs at a time */                                                         \
119   blkCnt = (blockSize - 1U) >> 2U;                                                                          \
120                                                                                                             \
121   while (blkCnt > 0U)                                                                                       \
122   {                                                                                                         \
123     /* Initialize cur_absmin to next consecutive values one by one */                                         \
124     cur_absmin = *pSrc++;                                                                                     \
125     cur_absmin = (cur_absmin > 0) ? cur_absmin : (q15_t)__QSUB16(0, cur_absmin);                                                                \
126     /* compare for the extrema value */                                                                     \
127     if (cur_absmin < out)                                                                         \
128     {                                                                                                       \
129       /* Update the extrema value and it's index */                                                         \
130       out = cur_absmin;                                                                                       \
131     }                                                                                                       \
132                                                                                                             \
133     cur_absmin = *pSrc++;                                                                                     \
134     cur_absmin = (cur_absmin > 0) ? cur_absmin : (q15_t)__QSUB16(0, cur_absmin);                                                                \
135     if (cur_absmin < out)                                                                         \
136     {                                                                                                       \
137       out = cur_absmin;                                                                                       \
138     }                                                                                                       \
139                                                                                                             \
140     cur_absmin = *pSrc++;                                                                                     \
141     cur_absmin = (cur_absmin > 0) ? cur_absmin : (q15_t)__QSUB16(0, cur_absmin);                                                                \
142     if (cur_absmin < out)                                                                          \
143     {                                                                                                       \
144       out = cur_absmin;                                                                                       \
145     }                                                                                                       \
146                                                                                                             \
147     cur_absmin = *pSrc++;                                                                                     \
148     cur_absmin = (cur_absmin > 0) ? cur_absmin : (q15_t)__QSUB16(0, cur_absmin);                                                                 \
149     if (cur_absmin < out)                                                                          \
150     {                                                                                                       \
151       out = cur_absmin;                                                                                       \
152     }                                                                                                       \
153                                                                                                             \
154                                                                                                             \
155     /* Decrement loop counter */                                                                            \
156     blkCnt--;                                                                                               \
157   }                                                                                                         \
158                                                                                                             \
159   /* Loop unrolling: Compute remaining outputs */                                                           \
160   blkCnt = (blockSize - 1U) % 4U;                                                                           \
161                                                                                                             \
162                                                                                                             \
163   while (blkCnt > 0U)                                                                                       \
164   {                                                                                                         \
165     cur_absmin = *pSrc++;                                                                                     \
166     cur_absmin = (cur_absmin > 0) ? cur_absmin : (q15_t)__QSUB16(0, cur_absmin);                                                                 \
167     if (cur_absmin < out)                                                                         \
168     {                                                                                                       \
169       out = cur_absmin;                                                                                       \
170     }                                                                                                       \
171                                                                                                             \
172     /* Decrement loop counter */                                                                            \
173     blkCnt--;                                                                                               \
174   }                                                                                                         \
175                                                                                                             \
176   /* Store the extrema value and it's index into destination pointers */                                    \
177   *pResult = out;                                                                                           \
178 }
179 #else
arm_absmin_no_idx_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)180 void arm_absmin_no_idx_q15(
181   const q15_t * pSrc,
182         uint32_t blockSize,
183         q15_t * pResult)
184 {
185        q15_t minVal, out;                             /* Temporary variables to store the output value. */
186         uint32_t blkCnt;                     /* Loop counter */
187 
188 
189 
190   /* Load first input value that act as reference value for comparision */
191   out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
192   pSrc++;
193 
194   /* Initialize blkCnt with number of samples */
195   blkCnt = (blockSize - 1U);
196 
197   while (blkCnt > 0U)
198   {
199     /* Initialize minVal to the next consecutive values one by one */
200     minVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
201     pSrc++;
202 
203     /* compare for the minimum value */
204     if (out > minVal)
205     {
206       /* Update the minimum value and it's index */
207       out = minVal;
208     }
209 
210     /* Decrement loop counter */
211     blkCnt--;
212   }
213 
214   /* Store the minimum value and it's index into destination pointers */
215   *pResult = out;
216 }
217 #endif /* defined(ARM_MATH_DSP) */
218 #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
219 /**
220   @} end of AbsMin group
221  */
222