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"
22 #if defined(DISTRHO_OS_WINDOWS) && !defined(_MSC_VER)
36 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
42 #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
44 typedef SSIZE_T ssize_t
;
47 #if ! defined(CARLA_MATH_UTILS_HPP_INCLUDED) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
49 inline float fmin(float __x
, float __y
)
50 { return __builtin_fminf(__x
, __y
); }
51 inline float fmax(float __x
, float __y
)
52 { return __builtin_fmaxf(__x
, __y
); }
53 inline float rint(float __x
)
54 { return __builtin_rintf(__x
); }
55 inline float round(float __x
)
56 { return __builtin_roundf(__x
); }
61 # define M_PI 3.14159265358979323846
64 #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
65 #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
67 /* ------------------------------------------------------------------------------------------------------------
71 @defgroup MiscellaneousFunctions Miscellaneous functions
77 Return a 32-bit number from 4 8-bit numbers.@n
78 The return type is a int64_t for better compatibility with plugin formats that use such numbers.
80 static inline constexpr
81 int64_t d_cconst(const uint8_t a
, const uint8_t b
, const uint8_t c
, const uint8_t d
) noexcept
83 return (a
<< 24) | (b
<< 16) | (c
<< 8) | (d
<< 0);
87 Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
89 static inline constexpr
90 uint32_t d_version(const uint8_t major
, const uint8_t minor
, const uint8_t micro
) noexcept
92 return uint32_t(major
<< 16) | uint32_t(minor
<< 8) | (micro
<< 0);
96 Dummy, no-op function.
99 void d_pass() noexcept
{}
103 /* ------------------------------------------------------------------------------------------------------------
104 * string print functions */
107 @defgroup StringPrintFunctions String print functions
113 Print a string to stdout with newline (gray color).
114 Does nothing if DEBUG is not defined.
117 # define d_debug(...)
120 void d_debug(const char* const fmt
, ...) noexcept
125 std::fprintf(stdout
, "\x1b[30;1m");
126 std::vfprintf(stdout
, fmt
, args
);
127 std::fprintf(stdout
, "\x1b[0m\n");
134 Print a string to stdout with newline.
137 void d_stdout(const char* const fmt
, ...) noexcept
142 std::vfprintf(stdout
, fmt
, args
);
143 std::fprintf(stdout
, "\n");
149 Print a string to stderr with newline.
152 void d_stderr(const char* const fmt
, ...) noexcept
157 std::vfprintf(stderr
, fmt
, args
);
158 std::fprintf(stderr
, "\n");
164 Print a string to stderr with newline (red color).
167 void d_stderr2(const char* const fmt
, ...) noexcept
172 std::fprintf(stderr
, "\x1b[31m");
173 std::vfprintf(stderr
, fmt
, args
);
174 std::fprintf(stderr
, "\x1b[0m\n");
180 Print a safe assertion error message.
183 void d_safe_assert(const char* const assertion
, const char* const file
, const int line
) noexcept
185 d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion
, file
, line
);
189 Print a safe assertion error message, with 1 extra signed integer value.
192 void d_safe_assert_int(const char* const assertion
, const char* const file
,
193 const int line
, const int value
) noexcept
195 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion
, file
, line
, value
);
199 Print a safe assertion error message, with 1 extra unsigned integer value.
202 void d_safe_assert_uint(const char* const assertion
, const char* const file
,
203 const int line
, const uint value
) noexcept
205 d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion
, file
, line
, value
);
209 Print a safe assertion error message, with 2 extra signed integer values.
212 void d_safe_assert_int2(const char* const assertion
, const char* const file
,
213 const int line
, const int v1
, const int v2
) noexcept
215 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion
, file
, line
, v1
, v2
);
219 Print a safe assertion error message, with 2 extra unsigned integer values.
222 void d_safe_assert_uint2(const char* const assertion
, const char* const file
,
223 const int line
, const uint v1
, const uint v2
) noexcept
225 d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion
, file
, line
, v1
, v2
);
229 Print a safe assertion error message, with a custom error message.
232 void d_custom_safe_assert(const char* const message
, const char* const assertion
, const char* const file
,
233 const int line
) noexcept
235 d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message
, assertion
, file
, line
);
239 Print a safe exception error message.
242 void d_safe_exception(const char* const exception
, const char* const file
, const int line
) noexcept
244 d_stderr2("exception caught: \"%s\" in file %s, line %i", exception
, file
, line
);
249 /* ------------------------------------------------------------------------------------------------------------
253 @defgroup MathFunctions Math related functions
259 Safely compare two floating point numbers.
260 Returns true if they match.
264 bool d_isEqual(const T
& v1
, const T
& v2
)
266 return std::abs(v1
-v2
) < std::numeric_limits
<T
>::epsilon();
270 Safely compare two floating point numbers.
271 Returns true if they don't match.
275 bool d_isNotEqual(const T
& v1
, const T
& v2
)
277 return std::abs(v1
-v2
) >= std::numeric_limits
<T
>::epsilon();
281 Safely check if a floating point number is zero.
285 bool d_isZero(const T
& value
)
287 return std::abs(value
) < std::numeric_limits
<T
>::epsilon();
291 Safely check if a floating point number is not zero.
295 bool d_isNotZero(const T
& value
)
297 return std::abs(value
) >= std::numeric_limits
<T
>::epsilon();
304 uint32_t d_nextPowerOf2(uint32_t size
) noexcept
306 DISTRHO_SAFE_ASSERT_RETURN(size
> 0, 0);
308 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
320 // -----------------------------------------------------------------------
322 #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
323 // If your code uses a lot of DISTRHO classes, then this will obviously save you
324 // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
325 namespace DISTRHO_NAMESPACE
{}
326 using namespace DISTRHO_NAMESPACE
;
329 // -----------------------------------------------------------------------
331 #endif // DISTRHO_UTILS_HPP_INCLUDED