1 /*
2 * Carla math utils
3 * Copyright (C) 2011-2021 Filipe Coelho <falktx@falktx.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16 */
17
18 #ifndef CARLA_MATH_UTILS_HPP_INCLUDED
19 #define CARLA_MATH_UTILS_HPP_INCLUDED
20
21 #include "CarlaUtils.hpp"
22
23 #include <cmath>
24 #include <limits>
25
26 // --------------------------------------------------------------------------------------------------------------------
27 // math functions (base)
28
29 /*
30 * Return the lower of 2 values, with 'min' as the minimum possible value (ie, constrain).
31 */
32 template<typename T>
33 static inline
carla_minConstrained(const T & v1,const T & v2,const T & min)34 const T& carla_minConstrained(const T& v1, const T& v2, const T& min) noexcept
35 {
36 return (v1 <= min || v2 <= min) ? min : (v1 < v2 ? v1 : v2);
37 }
38
39 /*
40 * Return the lower positive of 2 values.
41 * If one of the values is zero, returns zero.
42 * If one value is negative but the other positive, returns the positive.
43 * Returned value is guaranteed to be >= 0.
44 */
45 template<typename T>
46 static inline
carla_minPositive(const T & v1,const T & v2)47 T carla_minPositive(const T& v1, const T& v2) noexcept
48 {
49 if (v1 == 0 || v2 == 0)
50 return 0;
51 if (v1 < 0)
52 return (v2 > 0) ? v2 : 0;
53 if (v2 < 0)
54 return (v1 > 0) ? v1 : 0;
55 return (v1 < v2) ? v1 : v2;
56 }
57
58 /*
59 * Return the higher of 2 values, with 'max' as the maximum possible value (ie, limit).
60 */
61 template<typename T>
62 static inline
carla_maxLimited(const T & v1,const T & v2,const T & max)63 const T& carla_maxLimited(const T& v1, const T& v2, const T& max) noexcept
64 {
65 return (v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2);
66 }
67
68 /*
69 * Return the higher negative of 2 values.
70 * If one of the values is zero, returns zero.
71 * If one value is positive but the other negative, returns the negative.
72 * Returned value is guaranteed to be <= 0.
73 */
74 template<typename T>
75 static inline
carla_maxNegative(const T & v1,const T & v2)76 T carla_maxNegative(const T& v1, const T& v2) noexcept
77 {
78 if (v1 == 0 || v2 == 0)
79 return 0;
80 if (v1 > 0)
81 return (v2 < 0) ? v2 : 0;
82 if (v2 > 0)
83 return (v1 < 0) ? v1 : 0;
84 return (v1 > v2) ? v1 : v2;
85 }
86
87 /*
88 * Return a value between 'min' and 'max'.
89 */
90 template<typename T>
91 static inline
carla_fixedValue(const T & min,const T & max,const T & value)92 const T& carla_fixedValue(const T& min, const T& max, const T& value) noexcept
93 {
94 CARLA_SAFE_ASSERT_RETURN(max > min, min);
95
96 if (value <= min)
97 return min;
98 if (value >= max)
99 return max;
100 return value;
101 }
102
103 /*
104 * Get next power of 2.
105 */
106 static inline
carla_nextPowerOf2(uint32_t size)107 uint32_t carla_nextPowerOf2(uint32_t size) noexcept
108 {
109 CARLA_SAFE_ASSERT_RETURN(size > 0, 0);
110
111 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
112 --size;
113 size |= size >> 1;
114 size |= size >> 2;
115 size |= size >> 4;
116 size |= size >> 8;
117 size |= size >> 16;
118 return ++size;
119 }
120
121 /*
122 * Safely check if 2 numbers are equal.
123 */
124 template<typename T>
125 static inline
carla_isEqual(const T & v1,const T & v2)126 bool carla_isEqual(const T& v1, const T& v2)
127 {
128 return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
129 }
130
131 /*
132 * Safely check if 2 numbers are not equal.
133 */
134 template<typename T>
135 static inline
carla_isNotEqual(const T & v1,const T & v2)136 bool carla_isNotEqual(const T& v1, const T& v2)
137 {
138 return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
139 }
140
141 /*
142 * Safely check if a number is zero.
143 */
144 template<typename T>
145 static inline
carla_isZero(const T & value)146 bool carla_isZero(const T& value)
147 {
148 return std::abs(value) < std::numeric_limits<T>::epsilon();
149 }
150
151 /*
152 * Safely check if a number is not zero.
153 */
154 template<typename T>
155 static inline
carla_isNotZero(const T & value)156 bool carla_isNotZero(const T& value)
157 {
158 return std::abs(value) >= std::numeric_limits<T>::epsilon();
159 }
160
161 // --------------------------------------------------------------------------------------------------------------------
162 // math functions (extended)
163
164 /*
165 * Add float array values to another float array.
166 */
167 static inline
carla_addFloats(float dest[],const float src[],const std::size_t count)168 void carla_addFloats(float dest[], const float src[], const std::size_t count) noexcept
169 {
170 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
171 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
172 CARLA_SAFE_ASSERT_RETURN(count > 0,);
173
174 for (std::size_t i=0; i<count; ++i)
175 *dest++ += *src++;
176 }
177
178 /*
179 * Copy float array values to another float array.
180 */
181 static inline
carla_copyFloats(float dest[],const float src[],const std::size_t count)182 void carla_copyFloats(float dest[], const float src[], const std::size_t count) noexcept
183 {
184 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
185 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
186 CARLA_SAFE_ASSERT_RETURN(count > 0,);
187
188 std::memcpy(dest, src, count*sizeof(float));
189 }
190
191 /*
192 * Fill a float array with a single float value.
193 */
194 static inline
carla_fillFloatsWithSingleValue(float data[],const float & value,const std::size_t count)195 void carla_fillFloatsWithSingleValue(float data[], const float& value, const std::size_t count) noexcept
196 {
197 CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
198 CARLA_SAFE_ASSERT_RETURN(count > 0,);
199
200 if (carla_isZero(value))
201 {
202 std::memset(data, 0, count*sizeof(float));
203 }
204 else
205 {
206 for (std::size_t i=0; i<count; ++i)
207 *data++ = value;
208 }
209 }
210
211 /*
212 * Clear a float array.
213 */
214 static inline
carla_zeroFloats(float floats[],const std::size_t count)215 void carla_zeroFloats(float floats[], const std::size_t count) noexcept
216 {
217 CARLA_SAFE_ASSERT_RETURN(floats != nullptr,);
218 CARLA_SAFE_ASSERT_RETURN(count > 0,);
219
220 std::memset(floats, 0, count*sizeof(float));
221 }
222
223 // --------------------------------------------------------------------------------------------------------------------
224
225 /*
226 * Fill an array with a fixed value, floating-point version.
227 */
228 template <>
229 inline
carla_fill(float data[],const float & value,const std::size_t count)230 void carla_fill<float>(float data[], const float& value, const std::size_t count) noexcept
231 {
232 carla_fillFloatsWithSingleValue(data, value, count);
233 }
234
235 /*
236 * Find the highest absolute and normalized value within a float array.
237 */
238 static inline
carla_findMaxNormalizedFloat(const float floats[],const std::size_t count)239 float carla_findMaxNormalizedFloat(const float floats[], const std::size_t count)
240 {
241 CARLA_SAFE_ASSERT_RETURN(floats != nullptr, 0.0f);
242 CARLA_SAFE_ASSERT_RETURN(count > 0, 0.0f);
243
244 static const float kEmptyFloats[8192] = { 0.0f };
245
246 if (count <= 8192 && std::memcmp(floats, kEmptyFloats, count) == 0)
247 return 0.0f;
248
249 float tmp, maxf2 = std::abs(floats[0]);
250
251 for (std::size_t i=1; i<count; ++i)
252 {
253 tmp = std::abs(*floats++);
254
255 if (tmp > maxf2)
256 maxf2 = tmp;
257 }
258
259 if (maxf2 > 1.0f)
260 maxf2 = 1.0f;
261
262 return maxf2;
263 }
264
265 /*
266 * Multiply an array with a fixed value, float-specific version.
267 */
268 static inline
carla_multiply(float data[],const float & multiplier,const std::size_t count)269 void carla_multiply(float data[], const float& multiplier, const std::size_t count) noexcept
270 {
271 CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
272 CARLA_SAFE_ASSERT_RETURN(count > 0,);
273
274 if (carla_isZero(multiplier))
275 {
276 std::memset(data, 0, count*sizeof(float));
277 }
278 else
279 {
280 for (std::size_t i=0; i<count; ++i)
281 *data++ *= multiplier;
282 }
283 }
284
285 // --------------------------------------------------------------------------------------------------------------------
286 // Missing functions in old OSX versions.
287
288 #if ! defined(DISTRHO_UTILS_HPP_INCLUDED) && ! defined(CARLA_PROPER_CPP11_SUPPORT)
289 namespace std {
fmin(float __x,float __y)290 inline float fmin(float __x, float __y)
291 { return __builtin_fminf(__x, __y); }
fmax(float __x,float __y)292 inline float fmax(float __x, float __y)
293 { return __builtin_fmaxf(__x, __y); }
rint(float __x)294 inline float rint(float __x)
295 { return __builtin_rintf(__x); }
round(float __x)296 inline float round(float __x)
297 { return __builtin_roundf(__x); }
298 }
299 #endif
300
301 // --------------------------------------------------------------------------------------------------------------------
302
303 #endif // CARLA_MATH_UTILS_HPP_INCLUDED
304