17 #ifndef DISTRHO_UTILS_HPP_INCLUDED
18 #define DISTRHO_UTILS_HPP_INCLUDED
20 #include "src/DistrhoDefines.h"
30 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
36 #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
38 typedef SSIZE_T ssize_t;
41 #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
43 inline float fmin(
float __x,
float __y)
44 {
return __builtin_fminf(__x, __y); }
45 inline float fmax(
float __x,
float __y)
46 {
return __builtin_fmaxf(__x, __y); }
47 inline float rint(
float __x)
48 {
return __builtin_rintf(__x); }
49 inline float round(
float __x)
50 {
return __builtin_roundf(__x); }
55 # define M_PI 3.14159265358979323846
58 #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
59 #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
67 static inline constexpr
68 int64_t d_cconst(
const uint8_t a,
const uint8_t b,
const uint8_t c,
const uint8_t d) noexcept
70 return (a << 24) | (b << 16) | (c << 8) | (d << 0);
76 static inline constexpr
77 uint32_t d_version(
const uint8_t major,
const uint8_t minor,
const uint8_t micro) noexcept
79 return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
86 void d_pass() noexcept {}
99 void d_debug(
const char*
const fmt, ...) noexcept
104 std::fprintf(stdout,
"\x1b[30;1m");
105 std::vfprintf(stdout, fmt, args);
106 std::fprintf(stdout,
"\x1b[0m\n");
116 void d_stdout(
const char*
const fmt, ...) noexcept
121 std::vfprintf(stdout, fmt, args);
122 std::fprintf(stdout,
"\n");
131 void d_stderr(
const char*
const fmt, ...) noexcept
136 std::vfprintf(stderr, fmt, args);
137 std::fprintf(stderr,
"\n");
146 void d_stderr2(
const char*
const fmt, ...) noexcept
151 std::fprintf(stderr,
"\x1b[31m");
152 std::vfprintf(stderr, fmt, args);
153 std::fprintf(stderr,
"\x1b[0m\n");
162 void d_safe_assert(
const char*
const assertion,
const char*
const file,
const int line) noexcept
164 d_stderr2(
"assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
171 void d_safe_assert_int(
const char*
const assertion,
const char*
const file,
172 const int line,
const int value) noexcept
174 d_stderr2(
"assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
181 void d_safe_assert_uint(
const char*
const assertion,
const char*
const file,
182 const int line,
const uint value) noexcept
184 d_stderr2(
"assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
191 void d_safe_assert_int2(
const char*
const assertion,
const char*
const file,
192 const int line,
const int v1,
const int v2) noexcept
194 d_stderr2(
"assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
201 void d_safe_assert_uint2(
const char*
const assertion,
const char*
const file,
202 const int line,
const uint v1,
const uint v2) noexcept
204 d_stderr2(
"assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
211 void d_custom_safe_assert(
const char*
const message,
const char*
const assertion,
const char*
const file,
212 const int line) noexcept
214 d_stderr2(
"assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
221 void d_safe_exception(
const char*
const exception,
const char*
const file,
const int line) noexcept
223 d_stderr2(
"exception caught: \"%s\" in file %s, line %i", exception, file, line);
235 bool d_isEqual(
const T& v1,
const T& v2)
237 return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
246 bool d_isNotEqual(
const T& v1,
const T& v2)
248 return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
256 bool d_isZero(
const T& value)
258 return std::abs(value) < std::numeric_limits<T>::epsilon();
266 bool d_isNotZero(
const T& value)
268 return std::abs(value) >= std::numeric_limits<T>::epsilon();
275 uint32_t d_nextPowerOf2(uint32_t size) noexcept
277 DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
291 #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
294 namespace DISTRHO_NAMESPACE {}
295 using namespace DISTRHO_NAMESPACE;
300 #endif // DISTRHO_UTILS_HPP_INCLUDED