Cleanup
[carla.git] / source / modules / distrho / DistrhoUtils.hpp
blobc7c810ef28ff892731edf3b652ca375ab694ef14
1 /*
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)
23 #include <winsock2.h>
24 #undef max
25 #undef min
26 #endif
28 #include <cstdarg>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstring>
33 #include <cmath>
34 #include <limits>
36 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
37 # include <cstdint>
38 #else
39 # include <stdint.h>
40 #endif
42 #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
43 #include <basetsd.h>
44 typedef SSIZE_T ssize_t;
45 #endif
47 #if ! defined(CARLA_MATH_UTILS_HPP_INCLUDED) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
48 namespace std {
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); }
58 #endif
60 #ifndef M_PI
61 # define M_PI 3.14159265358979323846
62 #endif
64 #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
65 #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
67 /* ------------------------------------------------------------------------------------------------------------
68 * misc functions */
70 /**
71 @defgroup MiscellaneousFunctions Miscellaneous functions
76 /**
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);
86 /**
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);
95 /**
96 Dummy, no-op function.
98 static inline
99 void d_pass() noexcept {}
101 /** @} */
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.
116 #ifndef DEBUG
117 # define d_debug(...)
118 #else
119 static inline
120 void d_debug(const char* const fmt, ...) noexcept
122 try {
123 va_list args;
124 va_start(args, fmt);
125 std::fprintf(stdout, "\x1b[30;1m");
126 std::vfprintf(stdout, fmt, args);
127 std::fprintf(stdout, "\x1b[0m\n");
128 va_end(args);
129 } catch (...) {}
131 #endif
134 Print a string to stdout with newline.
136 static inline
137 void d_stdout(const char* const fmt, ...) noexcept
139 try {
140 va_list args;
141 va_start(args, fmt);
142 std::vfprintf(stdout, fmt, args);
143 std::fprintf(stdout, "\n");
144 va_end(args);
145 } catch (...) {}
149 Print a string to stderr with newline.
151 static inline
152 void d_stderr(const char* const fmt, ...) noexcept
154 try {
155 va_list args;
156 va_start(args, fmt);
157 std::vfprintf(stderr, fmt, args);
158 std::fprintf(stderr, "\n");
159 va_end(args);
160 } catch (...) {}
164 Print a string to stderr with newline (red color).
166 static inline
167 void d_stderr2(const char* const fmt, ...) noexcept
169 try {
170 va_list args;
171 va_start(args, fmt);
172 std::fprintf(stderr, "\x1b[31m");
173 std::vfprintf(stderr, fmt, args);
174 std::fprintf(stderr, "\x1b[0m\n");
175 va_end(args);
176 } catch (...) {}
180 Print a safe assertion error message.
182 static inline
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.
191 static inline
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.
201 static inline
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.
211 static inline
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.
221 static inline
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.
231 static inline
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.
241 static inline
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);
247 /** @} */
249 /* ------------------------------------------------------------------------------------------------------------
250 * math functions */
253 @defgroup MathFunctions Math related functions
259 Safely compare two floating point numbers.
260 Returns true if they match.
262 template<typename T>
263 static inline
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.
273 template<typename T>
274 static inline
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.
283 template<typename T>
284 static inline
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.
293 template<typename T>
294 static inline
295 bool d_isNotZero(const T& value)
297 return std::abs(value) >= std::numeric_limits<T>::epsilon();
301 Get next power of 2.
303 static inline
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
309 --size;
310 size |= size >> 1;
311 size |= size >> 2;
312 size |= size >> 4;
313 size |= size >> 8;
314 size |= size >> 16;
315 return ++size;
318 /** @} */
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;
327 #endif
329 // -----------------------------------------------------------------------
331 #endif // DISTRHO_UTILS_HPP_INCLUDED