1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 25 мая 2017 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef DSP_ARCH_NATIVE_SEARCH_H_
23 #define DSP_ARCH_NATIVE_SEARCH_H_
24 
25 #ifndef __DSP_NATIVE_IMPL
26     #error "This header should not be included directly"
27 #endif /* __DSP_NATIVE_IMPL */
28 
29 namespace native
30 {
min(const float * src,size_t count)31     float min(const float *src, size_t count)
32     {
33         if (count == 0)
34             return 0.0f;
35 
36         float min = src[0];
37         for (size_t i=1; i<count; ++i)
38         {
39             float tmp = src[i];
40             if (tmp < min)
41                 min = tmp;
42         }
43         return min;
44     }
45 
max(const float * src,size_t count)46     float max(const float *src, size_t count)
47     {
48         if (count == 0)
49             return 0.0f;
50 
51         float max = src[0];
52         for (size_t i=1; i<count; ++i)
53         {
54             float tmp = src[i];
55             if (tmp > max)
56                 max = tmp;
57         }
58         return max;
59     }
60 
abs_min(const float * src,size_t count)61     float abs_min(const float *src, size_t count)
62     {
63         if (count == 0)
64             return 0.0f;
65 
66         float min = fabs(src[0]);
67         for (size_t i=1; i<count; ++i)
68         {
69             float tmp = fabs(src[i]);
70             if (tmp < min)
71                 min     = tmp;
72         }
73         return min;
74     }
75 
abs_max(const float * src,size_t count)76     float abs_max(const float *src, size_t count)
77     {
78         if (count == 0)
79             return 0.0f;
80 
81         float max = fabs(src[0]);
82         for (size_t i=1; i<count; ++i)
83         {
84             float tmp = fabs(src[i]);
85             if (tmp > max)
86                 max     = tmp;
87         }
88         return max;
89     }
90 
minmax(const float * src,size_t count,float * min,float * max)91     void minmax(const float *src, size_t count, float *min, float *max)
92     {
93         if (count == 0)
94         {
95             *min    = 0.0f;
96             *max    = 0.0f;
97             return;
98         }
99 
100         float a_min = src[0], a_max = src[0];
101         for (size_t i=1; i<count; ++i)
102         {
103             float tmp   = src[i];
104             if (tmp < a_min)
105                 a_min       = tmp;
106             if (tmp > a_max)
107                 a_max       = tmp;
108         }
109         *min    = a_min;
110         *max    = a_max;
111     }
112 
abs_minmax(const float * src,size_t count,float * min,float * max)113     void abs_minmax(const float *src, size_t count, float *min, float *max)
114     {
115         if (count == 0)
116         {
117             *min    = 0.0f;
118             *max    = 0.0f;
119             return;
120         }
121 
122         float a_min = fabs(src[0]), a_max = fabs(src[0]);
123 
124         for (size_t i=1; i<count; ++i)
125         {
126             float tmp   = fabs(src[i]);
127             if (tmp < a_min)
128                 a_min       = tmp;
129             if (tmp > a_max)
130                 a_max       = tmp;
131         }
132         *min    = a_min;
133         *max    = a_max;
134     }
135 
min_index(const float * src,size_t count)136     size_t min_index(const float *src, size_t count)
137     {
138         if (count <= 0)
139             return 0;
140 
141         size_t index = 0;
142         float vmin = src[0];
143         for (size_t i=1; i<count; ++i)
144         {
145             float v = src[i];
146             if (v < vmin)
147             {
148                 index   = i;
149                 vmin    = v;
150             }
151         }
152         return index;
153     }
154 
max_index(const float * src,size_t count)155     size_t max_index(const float *src, size_t count)
156     {
157         if (count <= 0)
158             return 0;
159 
160         size_t index = 0;
161         float vmax = src[0];
162         for (size_t i=1; i<count; ++i)
163         {
164             float v = src[i];
165             if (v > vmax)
166             {
167                 index   = i;
168                 vmax    = v;
169             }
170         }
171         return index;
172     }
173 
abs_min_index(const float * src,size_t count)174     size_t abs_min_index(const float *src, size_t count)
175     {
176         if (count <= 0)
177             return 0;
178 
179         size_t index = 0;
180         float s = fabs(src[0]);
181         for (size_t i=1; i<count; ++i)
182         {
183             float d = fabs(src[i]);
184             if (d < s)
185             {
186                 index   = i;
187                 s       = d;
188             }
189         }
190         return index;
191     }
192 
abs_max_index(const float * src,size_t count)193     size_t abs_max_index(const float *src, size_t count)
194     {
195         if (count <= 0)
196             return 0;
197 
198         size_t index = 0;
199         float s = fabs(src[0]);
200         for (size_t i=1; i<count; ++i)
201         {
202             float d = fabs(src[i]);
203             if (d > s)
204             {
205                 index   = i;
206                 s       = d;
207             }
208         }
209         return index;
210     }
211 
minmax_index(const float * src,size_t count,size_t * min,size_t * max)212     void minmax_index(const float *src, size_t count, size_t *min, size_t *max)
213     {
214         size_t imin = 0, imax = 0;
215         if (count > 0)
216         {
217             float vmin = src[0];
218             float vmax = vmin;
219 
220             for (size_t i=1; i<count; ++i)
221             {
222                 float v = src[i];
223                 if (v < vmin)
224                 {
225                     imin    = i;
226                     vmin    = v;
227                 }
228                 if (v > vmax)
229                 {
230                     imax    = i;
231                     vmax    = v;
232                 }
233             }
234         }
235 
236         *min = imin;
237         *max = imax;
238     }
239 
abs_minmax_index(const float * src,size_t count,size_t * min,size_t * max)240     void abs_minmax_index(const float *src, size_t count, size_t *min, size_t *max)
241     {
242         size_t imin = 0, imax = 0;
243         if (count > 0)
244         {
245             float vmin = fabs(src[0]);
246             float vmax = vmin;
247 
248             for (size_t i=1; i<count; ++i)
249             {
250                 float v = fabs(src[i]);
251                 if (v < vmin)
252                 {
253                     imin    = i;
254                     vmin    = v;
255                 }
256                 if (v > vmax)
257                 {
258                     imax    = i;
259                     vmax    = v;
260                 }
261             }
262         }
263 
264         *min = imin;
265         *max = imax;
266     }
267 }
268 
269 #endif /* DSP_ARCH_NATIVE_SEARCH_H_ */
270