Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.2KB

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