OpenVDB 13.0.1
Loading...
Searching...
No Matches
Platform.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3///
4/// @file Platform.h
5
6#ifndef OPENVDB_PLATFORM_HAS_BEEN_INCLUDED
7#define OPENVDB_PLATFORM_HAS_BEEN_INCLUDED
8
9#define PRAGMA(x) _Pragma(#x)
10
11/// @name Utilities
12/// @{
13/// @cond OPENVDB_DOCS_INTERNAL
14#define OPENVDB_PREPROC_STRINGIFY_(x) #x
15/// @endcond
16/// @brief Return @a x as a string literal. If @a x is a macro,
17/// return its value as a string literal.
18/// @hideinitializer
19#define OPENVDB_PREPROC_STRINGIFY(x) OPENVDB_PREPROC_STRINGIFY_(x)
20
21/// @cond OPENVDB_DOCS_INTERNAL
22#define OPENVDB_PREPROC_CONCAT_(x, y) x ## y
23/// @endcond
24/// @brief Form a new token by concatenating two existing tokens.
25/// If either token is a macro, concatenate its value.
26/// @hideinitializer
27#define OPENVDB_PREPROC_CONCAT(x, y) OPENVDB_PREPROC_CONCAT_(x, y)
28/// @}
29
30/// Macro for determining if GCC version is >= than X.Y
31#if defined(__GNUC__)
32 #define OPENVDB_CHECK_GCC(MAJOR, MINOR) \
33 (__GNUC__ > MAJOR || (__GNUC__ == MAJOR && __GNUC_MINOR__ >= MINOR))
34#else
35 #define OPENVDB_CHECK_GCC(MAJOR, MINOR) 0
36#endif
37
38/// OpenVDB now requires C++17
39#define OPENVDB_HAS_CXX11 1 // kept for backward compatibility
40
41#if __cplusplus >= 202002L
42 #define OPENVDB_HAS_CXX20
43#endif
44
45/// Windows defines
46#ifdef _WIN32
47 ///Disable the non-portable Windows definitions of min() and max() macros
48 #ifndef NOMINMAX
49 #define NOMINMAX
50 #endif
51
52 // By default, assume we're building OpenVDB as a DLL if we're dynamically
53 // linking in the CRT, unless OPENVDB_STATICLIB is defined.
54 #if defined(_DLL) && !defined(OPENVDB_STATICLIB) && !defined(OPENVDB_DLL)
55 #define OPENVDB_DLL
56 #endif
57#endif
58
59/// Macros to suppress undefined behaviour sanitizer warnings. Should be used
60/// sparingly, primarily to suppress issues in upstream dependencies.
61#if defined(__clang__)
62#define OPENVDB_UBSAN_SUPPRESS(X) __attribute__((no_sanitize(X)))
63#else
64#define OPENVDB_UBSAN_SUPPRESS(X)
65#endif
66
67/// Macros to alias to compiler builtins which hint at critical edge selection
68/// during conditional statements.
69#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
70#ifdef __cplusplus
71#define OPENVDB_LIKELY(x) (__builtin_expect(static_cast<bool>(x), true))
72#define OPENVDB_UNLIKELY(x) (__builtin_expect(static_cast<bool>(x), false))
73#else
74#define OPENVDB_LIKELY(x) (__builtin_expect((x), 1))
75#define OPENVDB_UNLIKELY(x) (__builtin_expect((x), 0))
76#endif
77#else
78#define OPENVDB_LIKELY(x) (x)
79#define OPENVDB_UNLIKELY(x) (x)
80#endif
81
82/// Macros for assume builtins. Note that we currently don't simply apply these
83/// in place of asserts (when asserts are disabled) - they should be only be
84/// applied with an assert once profiled
85#ifdef __has_cpp_attribute
86 #if __has_cpp_attribute(assume) >= 202207L
87 #define OPENVDB_ASSUME(...) [[assume(__VA_ARGS__)]]
88 #endif
89#endif
90#ifndef OPENVDB_ASSUME
91 #if defined(__clang__)
92 #define OPENVDB_ASSUME(...) __builtin_assume(__VA_ARGS__);
93 #elif defined(_MSC_VER)
94 #define OPENVDB_ASSUME(...) __assume(__VA_ARGS__);
95 #elif defined(__GNUC__)
96 #if __GNUC__ >= 13
97 #define OPENVDB_ASSUME(...) __attribute__((__assume__(__VA_ARGS__)))
98 #endif
99 #endif
100#endif
101#ifndef OPENVDB_ASSUME
102 #define OPENVDB_ASSUME(...)
103#endif
104
105/// Force inline function macros. These macros do not necessary guarantee that
106/// the decorated function will be inlined, but provide the strongest vendor
107/// annotations to that end.
108#if defined(__GNUC__)
109#define OPENVDB_FORCE_INLINE __attribute__((always_inline)) inline
110#elif defined(_MSC_VER)
111#define OPENVDB_FORCE_INLINE __forceinline
112#else
113#define OPENVDB_FORCE_INLINE inline
114#endif
115
116/// Bracket code with OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN/_END,
117/// as in the following example, to inhibit ICC remarks about unreachable code:
118/// @code
119/// template<typename NodeType>
120/// void processNode(NodeType& node)
121/// {
122/// OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
123/// if (NodeType::LEVEL == 0) return; // ignore leaf nodes
124/// int i = 0;
125/// ...
126/// OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
127/// }
128/// @endcode
129/// In the above, <tt>NodeType::LEVEL == 0</tt> is a compile-time constant expression,
130/// so for some template instantiations, the line below it is unreachable.
131#if defined(__INTEL_COMPILER)
132 // Disable ICC remarks 111 ("statement is unreachable"), 128 ("loop is not reachable"),
133 // 185 ("dynamic initialization in unreachable code"), and 280 ("selector expression
134 // is constant").
135 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN \
136 _Pragma("warning (push)") \
137 _Pragma("warning (disable:111)") \
138 _Pragma("warning (disable:128)") \
139 _Pragma("warning (disable:185)") \
140 _Pragma("warning (disable:280)")
141 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END \
142 _Pragma("warning (pop)")
143#elif defined(__clang__)
144 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN \
145 PRAGMA(clang diagnostic push) \
146 PRAGMA(clang diagnostic ignored "-Wunreachable-code")
147 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END \
148 PRAGMA(clang diagnostic pop)
149#else
150 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
151 #define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
152#endif
153
154/// Deprecation macros. Define OPENVDB_NO_DEPRECATION_WARNINGS to disable all
155/// deprecation warnings in OpenVDB.
156#ifndef OPENVDB_NO_DEPRECATION_WARNINGS
157#define OPENVDB_DEPRECATED [[deprecated]]
158#define OPENVDB_DEPRECATED_MESSAGE(msg) [[deprecated(msg)]]
159#else
160#define OPENVDB_DEPRECATED
161#define OPENVDB_DEPRECATED_MESSAGE(msg)
162#endif
163
164/// @brief Bracket code with OPENVDB_NO_DEPRECATION_WARNING_BEGIN/_END,
165/// to inhibit warnings about deprecated code.
166/// @note Only intended to be used internally whilst parent code is being
167/// deprecated
168/// @details Example:
169/// @code
170/// OPENVDB_DEPRECATED void myDeprecatedFunction() {}
171///
172/// {
173/// OPENVDB_NO_DEPRECATION_WARNING_BEGIN
174/// myDeprecatedFunction();
175/// OPENVDB_NO_DEPRECATION_WARNING_END
176/// }
177/// @endcode
178#if defined __INTEL_COMPILER
179 #define OPENVDB_NO_DEPRECATION_WARNING_BEGIN \
180 _Pragma("warning (push)") \
181 _Pragma("warning (disable:1478)")
182 #define OPENVDB_NO_DEPRECATION_WARNING_END \
183 _Pragma("warning (pop)")
184#elif defined __clang__
185 #define OPENVDB_NO_DEPRECATION_WARNING_BEGIN \
186 _Pragma("clang diagnostic push") \
187 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
188 // note: no #pragma message, since Clang treats them as warnings
189 #define OPENVDB_NO_DEPRECATION_WARNING_END \
190 _Pragma("clang diagnostic pop")
191#elif defined __GNUC__
192 #define OPENVDB_NO_DEPRECATION_WARNING_BEGIN \
193 _Pragma("GCC diagnostic push") \
194 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
195 #define OPENVDB_NO_DEPRECATION_WARNING_END \
196 _Pragma("GCC diagnostic pop")
197#elif defined _MSC_VER
198 #define OPENVDB_NO_DEPRECATION_WARNING_BEGIN \
199 __pragma(warning(push)) \
200 __pragma(warning(disable : 4996))
201 #define OPENVDB_NO_DEPRECATION_WARNING_END \
202 __pragma(warning(pop))
203#else
204 #define OPENVDB_NO_DEPRECATION_WARNING_BEGIN
205 #define OPENVDB_NO_DEPRECATION_WARNING_END
206#endif
207
208
209/// @brief Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END,
210/// to inhibit warnings about type conversion.
211/// @note Use this sparingly. Use static casts and explicit type conversion if at all possible.
212/// @details Example:
213/// @code
214/// float value = 0.1f;
215/// OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
216/// int valueAsInt = value;
217/// OPENVDB_NO_TYPE_CONVERSION_WARNING_END
218/// @endcode
219#if defined __INTEL_COMPILER
220 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
221 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
222#elif defined __GNUC__
223 // -Wfloat-conversion was only introduced in GCC 4.9
224 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN \
225 _Pragma("GCC diagnostic push") \
226 _Pragma("GCC diagnostic ignored \"-Wconversion\"") \
227 _Pragma("GCC diagnostic ignored \"-Wfloat-conversion\"")
228 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_END \
229 _Pragma("GCC diagnostic pop")
230#else
231 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
232 #define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
233#endif
234
235/// Helper macros for defining library symbol visibility
236#ifdef OPENVDB_EXPORT
237#undef OPENVDB_EXPORT
238#endif
239#ifdef OPENVDB_IMPORT
240#undef OPENVDB_IMPORT
241#endif
242#ifdef _WIN32
243 #ifdef OPENVDB_DLL
244 #define OPENVDB_EXPORT __declspec(dllexport)
245 #define OPENVDB_IMPORT __declspec(dllimport)
246 #else
247 #define OPENVDB_EXPORT
248 #define OPENVDB_IMPORT
249 #endif
250#elif defined(__GNUC__)
251 #define OPENVDB_EXPORT __attribute__((visibility("default")))
252 #define OPENVDB_IMPORT __attribute__((visibility("default")))
253#endif
254
255/// Helper macros for explicit template instantiation
256#if defined(_WIN32) && defined(OPENVDB_DLL)
257 #ifdef OPENVDB_PRIVATE
258 #define OPENVDB_TEMPLATE_EXPORT OPENVDB_EXPORT
259 #define OPENVDB_TEMPLATE_IMPORT
260 #else
261 #define OPENVDB_TEMPLATE_EXPORT
262 #define OPENVDB_TEMPLATE_IMPORT OPENVDB_IMPORT
263 #endif
264#else
265 #define OPENVDB_TEMPLATE_IMPORT
266 #define OPENVDB_TEMPLATE_EXPORT
267#endif
268
269/// All classes and public free standing functions must be explicitly marked
270/// as <lib>_API to be exported. The <lib>_PRIVATE macros are defined when
271/// building that particular library.
272#ifdef OPENVDB_API
273#undef OPENVDB_API
274#endif
275#ifdef OPENVDB_PRIVATE
276 #define OPENVDB_API OPENVDB_EXPORT
277#else
278 #define OPENVDB_API OPENVDB_IMPORT
279#endif
280#ifdef OPENVDB_HOUDINI_API
281#undef OPENVDB_HOUDINI_API
282#endif
283#ifdef OPENVDB_HOUDINI_PRIVATE
284 #define OPENVDB_HOUDINI_API OPENVDB_EXPORT
285#else
286 #define OPENVDB_HOUDINI_API OPENVDB_IMPORT
287#endif
288
289#ifdef OPENVDB_AX_DLL
290#ifdef OPENVDB_AX_API
291#undef OPENVDB_AX_API
292#endif
293#ifdef OPENVDB_AX_PRIVATE
294 #define OPENVDB_AX_API OPENVDB_EXPORT
295#else
296 #define OPENVDB_AX_API OPENVDB_IMPORT
297#endif
298#else
299#define OPENVDB_AX_API
300#endif // OPENVDB_AX_DLL
301
302#if defined(__ICC)
303
304// Use these defines to bracket a region of code that has safe static accesses.
305// Keep the region as small as possible.
306#define OPENVDB_START_THREADSAFE_STATIC_REFERENCE __pragma(warning(disable:1710))
307#define OPENVDB_FINISH_THREADSAFE_STATIC_REFERENCE __pragma(warning(default:1710))
308#define OPENVDB_START_THREADSAFE_STATIC_WRITE __pragma(warning(disable:1711))
309#define OPENVDB_FINISH_THREADSAFE_STATIC_WRITE __pragma(warning(default:1711))
310#define OPENVDB_START_THREADSAFE_STATIC_ADDRESS __pragma(warning(disable:1712))
311#define OPENVDB_FINISH_THREADSAFE_STATIC_ADDRESS __pragma(warning(default:1712))
312
313// Use these defines to bracket a region of code that has unsafe static accesses.
314// Keep the region as small as possible.
315#define OPENVDB_START_NON_THREADSAFE_STATIC_REFERENCE __pragma(warning(disable:1710))
316#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_REFERENCE __pragma(warning(default:1710))
317#define OPENVDB_START_NON_THREADSAFE_STATIC_WRITE __pragma(warning(disable:1711))
318#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_WRITE __pragma(warning(default:1711))
319#define OPENVDB_START_NON_THREADSAFE_STATIC_ADDRESS __pragma(warning(disable:1712))
320#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_ADDRESS __pragma(warning(default:1712))
321
322// Simpler version for one-line cases
323#define OPENVDB_THREADSAFE_STATIC_REFERENCE(CODE) \
324 __pragma(warning(disable:1710)); CODE; __pragma(warning(default:1710))
325#define OPENVDB_THREADSAFE_STATIC_WRITE(CODE) \
326 __pragma(warning(disable:1711)); CODE; __pragma(warning(default:1711))
327#define OPENVDB_THREADSAFE_STATIC_ADDRESS(CODE) \
328 __pragma(warning(disable:1712)); CODE; __pragma(warning(default:1712))
329
330#else // GCC does not support these compiler warnings
331
332#define OPENVDB_START_THREADSAFE_STATIC_REFERENCE
333#define OPENVDB_FINISH_THREADSAFE_STATIC_REFERENCE
334#define OPENVDB_START_THREADSAFE_STATIC_WRITE
335#define OPENVDB_FINISH_THREADSAFE_STATIC_WRITE
336#define OPENVDB_START_THREADSAFE_STATIC_ADDRESS
337#define OPENVDB_FINISH_THREADSAFE_STATIC_ADDRESS
338
339#define OPENVDB_START_NON_THREADSAFE_STATIC_REFERENCE
340#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_REFERENCE
341#define OPENVDB_START_NON_THREADSAFE_STATIC_WRITE
342#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_WRITE
343#define OPENVDB_START_NON_THREADSAFE_STATIC_ADDRESS
344#define OPENVDB_FINISH_NON_THREADSAFE_STATIC_ADDRESS
345
346#define OPENVDB_THREADSAFE_STATIC_REFERENCE(CODE) CODE
347#define OPENVDB_THREADSAFE_STATIC_WRITE(CODE) CODE
348#define OPENVDB_THREADSAFE_STATIC_ADDRESS(CODE) CODE
349
350#endif // defined(__ICC)
351
352#endif // OPENVDB_PLATFORM_HAS_BEEN_INCLUDED