2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
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.
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.
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(CARLA_MATH_UTILS_HPP_INCLUDED) && ! 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)
61 /* ------------------------------------------------------------------------------------------------------------
65 @defgroup MiscellaneousFunctions Miscellaneous functions
71 Return a 32-bit number from 4 8-bit numbers.@n
72 The return type is a int64_t for better compatibility with plugin formats that use such numbers.
74 static inline constexpr
75 int64_t d_cconst(const uint8_t a
, const uint8_t b
, const uint8_t c
, const uint8_t d
) noexcept
77 return (a
<< 24) | (b
<< 16) | (c
<< 8) | (d
<< 0);
81 Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
83 static inline constexpr
84 uint32_t d_version(const uint8_t major
, const uint8_t minor
, const uint8_t micro
) noexcept
86 return uint32_t(major
<< 16) | uint32_t(minor
<< 8) | (micro
<< 0);
90 Dummy, no-op function.
93 void d_pass() noexcept
{}
97 /* ------------------------------------------------------------------------------------------------------------
98 * string print functions */
101 @defgroup StringPrintFunctions String print functions
107 Print a string to stdout with newline (gray color).
108 Does nothing if DEBUG is not defined.
111 # define d_debug(...)
114 void d_debug(const char* const fmt
, ...) noexcept
119 std::fprintf(stdout
, "\x1b[30;1m");
120 std::vfprintf(stdout
, fmt
, args
);
121 std::fprintf(stdout
, "\x1b[0m\n");
128 Print a string to stdout with newline.
131 void d_stdout(const char* const fmt
, ...) noexcept
136 std::vfprintf(stdout
, fmt
, args
);
137 std::fprintf(stdout
, "\n");
143 Print a string to stderr with newline.
146 void d_stderr(const char* const fmt
, ...) noexcept
151 std::vfprintf(stderr
, fmt
, args
);
152 std::fprintf(stderr
, "\n");
158 Print a string to stderr with newline (red color).
161 void d_stderr2(const char* const fmt
, ...) noexcept
166 std::fprintf(stderr
, "\x1b[31m");
167 std::vfprintf(stderr
, fmt
, args
);
168 std::fprintf(stderr
, "\x1b[0m\n");
174 Print a safe assertion error message.
177 void d_safe_assert(const char* const assertion
, const char* const file
, const int line
) noexcept
179 d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion
, file
, line
);
183 Print a safe assertion error message, with 1 extra signed integer value.
186 void d_safe_assert_int(const char* const assertion
, const char* const file
,
187 const int line
, const int value
) noexcept
189 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion
, file
, line
, value
);
193 Print a safe assertion error message, with 1 extra unsigned integer value.
196 void d_safe_assert_uint(const char* const assertion
, const char* const file
,
197 const int line
, const uint value
) noexcept
199 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion
, file
, line
, value
);
203 Print a safe assertion error message, with 2 extra signed integer values.
206 void d_safe_assert_int2(const char* const assertion
, const char* const file
,
207 const int line
, const int v1
, const int v2
) noexcept
209 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion
, file
, line
, v1
, v2
);
213 Print a safe assertion error message, with 2 extra unsigned integer values.
216 void d_safe_assert_uint2(const char* const assertion
, const char* const file
,
217 const int line
, const uint v1
, const uint v2
) noexcept
219 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion
, file
, line
, v1
, v2
);
223 Print a safe assertion error message, with a custom error message.
226 void d_custom_safe_assert(const char* const message
, const char* const assertion
, const char* const file
,
227 const int line
) noexcept
229 d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message
, assertion
, file
, line
);
233 Print a safe exception error message.
236 void d_safe_exception(const char* const exception
, const char* const file
, const int line
) noexcept
238 d_stderr2("exception caught: \"%s\" in file %s, line %i", exception
, file
, line
);
243 /* ------------------------------------------------------------------------------------------------------------
247 @defgroup MathFunctions Math related functions
253 Safely compare two floating point numbers.
254 Returns true if they match.
258 bool d_isEqual(const T
& v1
, const T
& v2
)
260 return std::abs(v1
-v2
) < std::numeric_limits
<T
>::epsilon();
264 Safely compare two floating point numbers.
265 Returns true if they don't match.
269 bool d_isNotEqual(const T
& v1
, const T
& v2
)
271 return std::abs(v1
-v2
) >= std::numeric_limits
<T
>::epsilon();
275 Safely check if a floating point number is zero.
279 bool d_isZero(const T
& value
)
281 return std::abs(value
) < std::numeric_limits
<T
>::epsilon();
285 Safely check if a floating point number is not zero.
289 bool d_isNotZero(const T
& value
)
291 return std::abs(value
) >= std::numeric_limits
<T
>::epsilon();
298 uint32_t d_nextPowerOf2(uint32_t size
) noexcept
300 DISTRHO_SAFE_ASSERT_RETURN(size
> 0, 0);
302 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
314 // -----------------------------------------------------------------------
316 #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
317 // If your code uses a lot of DISTRHO classes, then this will obviously save you
318 // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
319 namespace DISTRHO_NAMESPACE
{}
320 using namespace DISTRHO_NAMESPACE
;
323 // -----------------------------------------------------------------------
325 #endif // DISTRHO_UTILS_HPP_INCLUDED