DISTRHO Plugin Framework
 All Classes Functions Variables Modules Pages
DistrhoUtils.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2015 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 
17 #ifndef DISTRHO_UTILS_HPP_INCLUDED
18 #define DISTRHO_UTILS_HPP_INCLUDED
19 
20 #include "src/DistrhoDefines.h"
21 
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 
27 #include <cmath>
28 #include <limits>
29 
30 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
31 # include <cstdint>
32 #else
33 # include <stdint.h>
34 #endif
35 
36 #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC)
37 namespace std {
38 inline float fmin(float __x, float __y)
39  { return __builtin_fminf(__x, __y); }
40 inline float fmax(float __x, float __y)
41  { return __builtin_fmaxf(__x, __y); }
42 inline float rint(float __x)
43  { return __builtin_rintf(__x); }
44 inline float round(float __x)
45  { return __builtin_roundf(__x); }
46 }
47 #endif
48 
49 #ifndef M_PI
50 # define M_PI 3.14159265358979323846
51 #endif
52 
53 // -----------------------------------------------------------------------
54 // misc functions
55 
56 /*
57  * Return a 64-bit number from 4 8-bit numbers.
58  */
59 static inline
60 int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
61 {
62  return (a << 24) | (b << 16) | (c << 8) | (d << 0);
63 }
64 
65 /*
66  * Dummy function.
67  */
68 static inline
69 void d_pass() noexcept {}
70 
71 // -----------------------------------------------------------------------
72 // string print functions
73 
74 /*
75  * Print a string to stdout with newline (gray color).
76  * Does nothing if DEBUG is not defined.
77  */
78 #ifndef DEBUG
79 # define d_debug(...)
80 #else
81 static inline
82 void d_debug(const char* const fmt, ...) noexcept
83 {
84  try {
85  ::va_list args;
86  ::va_start(args, fmt);
87  std::fprintf(stdout, "\x1b[30;1m");
88  std::vfprintf(stdout, fmt, args);
89  std::fprintf(stdout, "\x1b[0m\n");
90  ::va_end(args);
91  } catch (...) {}
92 }
93 #endif
94 
95 /*
96  * Print a string to stdout with newline.
97  */
98 static inline
99 void d_stdout(const char* const fmt, ...) noexcept
100 {
101  try {
102  ::va_list args;
103  ::va_start(args, fmt);
104  std::vfprintf(stdout, fmt, args);
105  std::fprintf(stdout, "\n");
106  ::va_end(args);
107  } catch (...) {}
108 }
109 
110 /*
111  * Print a string to stderr with newline.
112  */
113 static inline
114 void d_stderr(const char* const fmt, ...) noexcept
115 {
116  try {
117  ::va_list args;
118  ::va_start(args, fmt);
119  std::vfprintf(stderr, fmt, args);
120  std::fprintf(stderr, "\n");
121  ::va_end(args);
122  } catch (...) {}
123 }
124 
125 /*
126  * Print a string to stderr with newline (red color).
127  */
128 static inline
129 void d_stderr2(const char* const fmt, ...) noexcept
130 {
131  try {
132  ::va_list args;
133  ::va_start(args, fmt);
134  std::fprintf(stderr, "\x1b[31m");
135  std::vfprintf(stderr, fmt, args);
136  std::fprintf(stderr, "\x1b[0m\n");
137  ::va_end(args);
138  } catch (...) {}
139 }
140 
141 /*
142  * Print a safe assertion error message.
143  */
144 static inline
145 void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
146 {
147  d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
148 }
149 
150 /*
151  * Print a safe exception error message.
152  */
153 static inline
154 void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
155 {
156  d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
157 }
158 
159 // -----------------------------------------------------------------------
160 // math functions
161 
162 /*
163  * Safely compare two floating point numbers.
164  * Returns true if they match.
165  */
166 template<typename T>
167 static inline
168 bool d_isEqual(const T& v1, const T& v2)
169 {
170  return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
171 }
172 
173 /*
174  * Safely compare two floating point numbers.
175  * Returns true if they don't match.
176  */
177 template<typename T>
178 static inline
179 bool d_isNotEqual(const T& v1, const T& v2)
180 {
181  return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
182 }
183 
184 /*
185  * Safely check if a floating point number is zero.
186  */
187 template<typename T>
188 static inline
189 bool d_isZero(const T& value)
190 {
191  return std::abs(value) < std::numeric_limits<T>::epsilon();
192 }
193 
194 /*
195  * Safely check if a floating point number is not zero.
196  */
197 template<typename T>
198 static inline
199 bool d_isNotZero(const T& value)
200 {
201  return std::abs(value) >= std::numeric_limits<T>::epsilon();
202 }
203 
204 // -----------------------------------------------------------------------
205 
206 #endif // DISTRHO_UTILS_HPP_INCLUDED