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