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.

283 lines
7.4KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 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. // -----------------------------------------------------------------------
  46. // misc functions
  47. /*
  48. * Return a 64-bit number from 4 8-bit numbers.
  49. */
  50. static inline
  51. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  52. {
  53. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  54. }
  55. /*
  56. * Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
  57. */
  58. static inline
  59. uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
  60. {
  61. return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
  62. }
  63. /*
  64. * Dummy function.
  65. */
  66. static inline
  67. void d_pass() noexcept {}
  68. // -----------------------------------------------------------------------
  69. // string print functions
  70. /*
  71. * Print a string to stdout with newline (gray color).
  72. * Does nothing if DEBUG is not defined.
  73. */
  74. #ifndef DEBUG
  75. # define d_debug(...)
  76. #else
  77. static inline
  78. void d_debug(const char* const fmt, ...) noexcept
  79. {
  80. try {
  81. ::va_list args;
  82. ::va_start(args, fmt);
  83. std::fprintf(stdout, "\x1b[30;1m");
  84. std::vfprintf(stdout, fmt, args);
  85. std::fprintf(stdout, "\x1b[0m\n");
  86. ::va_end(args);
  87. } catch (...) {}
  88. }
  89. #endif
  90. /*
  91. * Print a string to stdout with newline.
  92. */
  93. static inline
  94. void d_stdout(const char* const fmt, ...) noexcept
  95. {
  96. try {
  97. ::va_list args;
  98. ::va_start(args, fmt);
  99. std::vfprintf(stdout, fmt, args);
  100. std::fprintf(stdout, "\n");
  101. ::va_end(args);
  102. } catch (...) {}
  103. }
  104. /*
  105. * Print a string to stderr with newline.
  106. */
  107. static inline
  108. void d_stderr(const char* const fmt, ...) noexcept
  109. {
  110. try {
  111. ::va_list args;
  112. ::va_start(args, fmt);
  113. std::vfprintf(stderr, fmt, args);
  114. std::fprintf(stderr, "\n");
  115. ::va_end(args);
  116. } catch (...) {}
  117. }
  118. /*
  119. * Print a string to stderr with newline (red color).
  120. */
  121. static inline
  122. void d_stderr2(const char* const fmt, ...) noexcept
  123. {
  124. try {
  125. ::va_list args;
  126. ::va_start(args, fmt);
  127. std::fprintf(stderr, "\x1b[31m");
  128. std::vfprintf(stderr, fmt, args);
  129. std::fprintf(stderr, "\x1b[0m\n");
  130. ::va_end(args);
  131. } catch (...) {}
  132. }
  133. /*
  134. * Print a safe assertion error message.
  135. */
  136. static inline
  137. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  138. {
  139. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  140. }
  141. /*
  142. * Print a safe assertion error message, with 1 extra signed integer value.
  143. */
  144. static inline
  145. void d_safe_assert_int(const char* const assertion, const char* const file,
  146. const int line, const int value) noexcept
  147. {
  148. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  149. }
  150. /*
  151. * Print a safe assertion error message, with 1 extra unsigned integer value.
  152. */
  153. static inline
  154. void d_safe_assert_uint(const char* const assertion, const char* const file,
  155. const int line, const uint value) noexcept
  156. {
  157. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  158. }
  159. /*
  160. * Print a safe assertion error message, with 2 extra signed integer values.
  161. */
  162. static inline
  163. void d_safe_assert_int2(const char* const assertion, const char* const file,
  164. const int line, const int v1, const int v2) noexcept
  165. {
  166. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  167. }
  168. /*
  169. * Print a safe assertion error message, with 2 extra unsigned integer values.
  170. */
  171. static inline
  172. void d_safe_assert_uint2(const char* const assertion, const char* const file,
  173. const int line, const uint v1, const uint v2) noexcept
  174. {
  175. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  176. }
  177. /*
  178. * Print a safe exception error message.
  179. */
  180. static inline
  181. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  182. {
  183. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  184. }
  185. // -----------------------------------------------------------------------
  186. // math functions
  187. /*
  188. * Safely compare two floating point numbers.
  189. * Returns true if they match.
  190. */
  191. template<typename T>
  192. static inline
  193. bool d_isEqual(const T& v1, const T& v2)
  194. {
  195. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  196. }
  197. /*
  198. * Safely compare two floating point numbers.
  199. * Returns true if they don't match.
  200. */
  201. template<typename T>
  202. static inline
  203. bool d_isNotEqual(const T& v1, const T& v2)
  204. {
  205. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  206. }
  207. /*
  208. * Safely check if a floating point number is zero.
  209. */
  210. template<typename T>
  211. static inline
  212. bool d_isZero(const T& value)
  213. {
  214. return std::abs(value) < std::numeric_limits<T>::epsilon();
  215. }
  216. /*
  217. * Safely check if a floating point number is not zero.
  218. */
  219. template<typename T>
  220. static inline
  221. bool d_isNotZero(const T& value)
  222. {
  223. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  224. }
  225. /*
  226. * Get next power of 2.
  227. */
  228. static inline
  229. uint32_t d_nextPowerOf2(uint32_t size) noexcept
  230. {
  231. DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
  232. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  233. --size;
  234. size |= size >> 1;
  235. size |= size >> 2;
  236. size |= size >> 4;
  237. size |= size >> 8;
  238. size |= size >> 16;
  239. return ++size;
  240. }
  241. // -----------------------------------------------------------------------
  242. #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
  243. // If your code uses a lot of DISTRHO classes, then this will obviously save you
  244. // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
  245. namespace DISTRHO_NAMESPACE {}
  246. using namespace DISTRHO_NAMESPACE;
  247. #endif
  248. // -----------------------------------------------------------------------
  249. #endif // DISTRHO_UTILS_HPP_INCLUDED