DISTRHO Plugin Framework
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.

295 lines
7.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_UTILS_HPP_INCLUDED
  17. #define DISTRHO_UTILS_HPP_INCLUDED
  18. #include "src/DistrhoDefines.h"
  19. #include <cstdarg>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <cmath>
  24. #include <limits>
  25. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  26. # include <cstdint>
  27. #else
  28. # include <stdint.h>
  29. #endif
  30. #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
  31. namespace std {
  32. inline float fmin(float __x, float __y)
  33. { return __builtin_fminf(__x, __y); }
  34. inline float fmax(float __x, float __y)
  35. { return __builtin_fmaxf(__x, __y); }
  36. inline float rint(float __x)
  37. { return __builtin_rintf(__x); }
  38. inline float round(float __x)
  39. { return __builtin_roundf(__x); }
  40. }
  41. #endif
  42. #ifndef M_PI
  43. # define M_PI 3.14159265358979323846
  44. #endif
  45. #define DISTRHO_MACRO_AS_STRING(MACRO) #MACRO
  46. // -----------------------------------------------------------------------
  47. // misc functions
  48. /*
  49. * Return a 64-bit number from 4 8-bit numbers.
  50. */
  51. static inline
  52. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  53. {
  54. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  55. }
  56. /*
  57. * Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
  58. */
  59. static inline
  60. uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
  61. {
  62. return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
  63. }
  64. /*
  65. * Dummy function.
  66. */
  67. static inline
  68. void d_pass() noexcept {}
  69. // -----------------------------------------------------------------------
  70. // string print functions
  71. /*
  72. * Print a string to stdout with newline (gray color).
  73. * Does nothing if DEBUG is not defined.
  74. */
  75. #ifndef DEBUG
  76. # define d_debug(...)
  77. #else
  78. static inline
  79. void d_debug(const char* const fmt, ...) noexcept
  80. {
  81. try {
  82. ::va_list args;
  83. ::va_start(args, fmt);
  84. std::fprintf(stdout, "\x1b[30;1m");
  85. std::vfprintf(stdout, fmt, args);
  86. std::fprintf(stdout, "\x1b[0m\n");
  87. ::va_end(args);
  88. } catch (...) {}
  89. }
  90. #endif
  91. /*
  92. * Print a string to stdout with newline.
  93. */
  94. static inline
  95. void d_stdout(const char* const fmt, ...) noexcept
  96. {
  97. try {
  98. ::va_list args;
  99. ::va_start(args, fmt);
  100. std::vfprintf(stdout, fmt, args);
  101. std::fprintf(stdout, "\n");
  102. ::va_end(args);
  103. } catch (...) {}
  104. }
  105. /*
  106. * Print a string to stderr with newline.
  107. */
  108. static inline
  109. void d_stderr(const char* const fmt, ...) noexcept
  110. {
  111. try {
  112. ::va_list args;
  113. ::va_start(args, fmt);
  114. std::vfprintf(stderr, fmt, args);
  115. std::fprintf(stderr, "\n");
  116. ::va_end(args);
  117. } catch (...) {}
  118. }
  119. /*
  120. * Print a string to stderr with newline (red color).
  121. */
  122. static inline
  123. void d_stderr2(const char* const fmt, ...) noexcept
  124. {
  125. try {
  126. ::va_list args;
  127. ::va_start(args, fmt);
  128. std::fprintf(stderr, "\x1b[31m");
  129. std::vfprintf(stderr, fmt, args);
  130. std::fprintf(stderr, "\x1b[0m\n");
  131. ::va_end(args);
  132. } catch (...) {}
  133. }
  134. /*
  135. * Print a safe assertion error message.
  136. */
  137. static inline
  138. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  139. {
  140. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  141. }
  142. /*
  143. * Print a safe assertion error message, with 1 extra signed integer value.
  144. */
  145. static inline
  146. void d_safe_assert_int(const char* const assertion, const char* const file,
  147. const int line, const int value) noexcept
  148. {
  149. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  150. }
  151. /*
  152. * Print a safe assertion error message, with 1 extra unsigned integer value.
  153. */
  154. static inline
  155. void d_safe_assert_uint(const char* const assertion, const char* const file,
  156. const int line, const uint value) noexcept
  157. {
  158. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  159. }
  160. /*
  161. * Print a safe assertion error message, with 2 extra signed integer values.
  162. */
  163. static inline
  164. void d_safe_assert_int2(const char* const assertion, const char* const file,
  165. const int line, const int v1, const int v2) noexcept
  166. {
  167. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  168. }
  169. /*
  170. * Print a safe assertion error message, with 2 extra unsigned integer values.
  171. */
  172. static inline
  173. void d_safe_assert_uint2(const char* const assertion, const char* const file,
  174. const int line, const uint v1, const uint v2) noexcept
  175. {
  176. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  177. }
  178. /*
  179. * Print a safe assertion error message, with a custom error message.
  180. */
  181. static inline
  182. void d_custom_safe_assert(const char* const message, const char* const assertion, const char* const file,
  183. const int line) noexcept
  184. {
  185. d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
  186. }
  187. /*
  188. * Print a safe exception error message.
  189. */
  190. static inline
  191. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  192. {
  193. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  194. }
  195. // -----------------------------------------------------------------------
  196. // math functions
  197. /*
  198. * Safely compare two floating point numbers.
  199. * Returns true if they match.
  200. */
  201. template<typename T>
  202. static inline
  203. bool d_isEqual(const T& v1, const T& v2)
  204. {
  205. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  206. }
  207. /*
  208. * Safely compare two floating point numbers.
  209. * Returns true if they don't match.
  210. */
  211. template<typename T>
  212. static inline
  213. bool d_isNotEqual(const T& v1, const T& v2)
  214. {
  215. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  216. }
  217. /*
  218. * Safely check if a floating point number is zero.
  219. */
  220. template<typename T>
  221. static inline
  222. bool d_isZero(const T& value)
  223. {
  224. return std::abs(value) < std::numeric_limits<T>::epsilon();
  225. }
  226. /*
  227. * Safely check if a floating point number is not zero.
  228. */
  229. template<typename T>
  230. static inline
  231. bool d_isNotZero(const T& value)
  232. {
  233. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  234. }
  235. /*
  236. * Get next power of 2.
  237. */
  238. static inline
  239. uint32_t d_nextPowerOf2(uint32_t size) noexcept
  240. {
  241. DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
  242. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  243. --size;
  244. size |= size >> 1;
  245. size |= size >> 2;
  246. size |= size >> 4;
  247. size |= size >> 8;
  248. size |= size >> 16;
  249. return ++size;
  250. }
  251. // -----------------------------------------------------------------------
  252. #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
  253. // If your code uses a lot of DISTRHO classes, then this will obviously save you
  254. // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
  255. namespace DISTRHO_NAMESPACE {}
  256. using namespace DISTRHO_NAMESPACE;
  257. #endif
  258. // -----------------------------------------------------------------------
  259. #endif // DISTRHO_UTILS_HPP_INCLUDED