Blender V5.3
defines.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* clang-format off */
6
7/* #define __forceinline triggers a bug in some clang-format versions, disable
8 * format for entire file to keep results consistent. */
9
10#pragma once
11
12/* Bitness */
13
14#if defined(__ppc64__) || defined(__PPC64__) || defined(__x86_64__) || defined(__ia64__) || \
15 defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64)
16# define __KERNEL_64_BIT__
17#endif
18
19/* Qualifiers for kernel code shared by CPU and GPU */
20
21#ifndef __KERNEL_GPU__
22
23/* Leave inlining decisions to compiler for these, the inline keyword here
24 * is not about performance but including function definitions in headers. */
25# define ccl_device static inline
26# define ccl_device_extern extern "C"
27# define ccl_device_noinline static inline
28# define ccl_device_noinline_cpu ccl_device_noinline
29
30/* Forced inlining. */
31# if defined(_WIN32) && !defined(FREE_WINDOWS)
32# define ccl_device_inline static __forceinline
33# define ccl_device_forceinline static __forceinline
34# define ccl_device_inline_method __forceinline
35# define ccl_device_template_spec template<> __forceinline
36# define ccl_align(...) __declspec(align(__VA_ARGS__))
37# ifdef __KERNEL_64_BIT__
38# define ccl_try_align(...) __declspec(align(__VA_ARGS__))
39# else /* __KERNEL_64_BIT__ */
40# undef __KERNEL_WITH_SSE_ALIGN__
41/* No support for function arguments (error C2719). */
42# define ccl_try_align(...)
43# endif /* __KERNEL_64_BIT__ */
44# define ccl_may_alias
45# define ccl_always_inline __forceinline
46# define ccl_never_inline __declspec(noinline)
47# else /* _WIN32 && !FREE_WINDOWS */
48# define ccl_device_inline static inline __attribute__((always_inline))
49# define ccl_device_forceinline static inline __attribute__((always_inline))
50# define ccl_device_inline_method __attribute__((always_inline))
51# define ccl_device_template_spec template<> inline __attribute__((always_inline))
52# define ccl_align(...) __attribute__((aligned(__VA_ARGS__)))
53# ifndef FREE_WINDOWS64
54# define __forceinline inline __attribute__((always_inline))
55# endif
56# define ccl_try_align(...) __attribute__((aligned(__VA_ARGS__)))
57# define ccl_may_alias __attribute__((__may_alias__))
58# define ccl_always_inline __attribute__((always_inline))
59# define ccl_never_inline __attribute__((noinline))
60# endif /* _WIN32 && !FREE_WINDOWS */
61
62/* Address spaces for GPU. */
63# define ccl_global
64# define ccl_inline_constant inline constexpr
65# define ccl_static_constexpr static constexpr
66# define ccl_constant const
67# define ccl_private
68# define ccl_ray_data ccl_private
69
70# define ccl_restrict __restrict
71# define ccl_optional_struct_init
72# define ccl_attr_maybe_unused [[maybe_unused]]
73# define __KERNEL_WITH_SSE_ALIGN__
74
75/* Use to suppress `-Wimplicit-fallthrough` (in place of `break`). */
76# ifndef ATTR_FALLTHROUGH
77# if defined(__GNUC__) && (__GNUC__ >= 7) /* gcc7.0+ only */
78# define ATTR_FALLTHROUGH __attribute__((fallthrough))
79# else
80# define ATTR_FALLTHROUGH ((void)0)
81# endif
82# endif
83#endif /* __KERNEL_GPU__ */
84
85/* Address sanitizer suppression. */
86
87#ifdef __KERNEL_GPU__
88# define ccl_ignore_integer_overflow
89#else
90# if defined(__clang__)
91# if __has_feature(address_sanitizer)
92# define ccl_ignore_integer_overflow [[clang::no_sanitize("signed-integer-overflow")]]
93# else
94# define ccl_ignore_integer_overflow
95# endif
96# elif defined(__GNUC__) && defined(__SANITIZE_ADDRESS__)
97# define ccl_ignore_integer_overflow [[gnu::no_sanitize("signed-integer-overflow")]]
98# else
99# define ccl_ignore_integer_overflow
100# endif
101#endif
102
103/* macros */
104
105/* hints for branch prediction, only use in code that runs a _lot_ */
106#if defined(__GNUC__) && !defined(__KERNEL_GPU__)
107# define LIKELY(x) __builtin_expect(!!(x), 1)
108# define UNLIKELY(x) __builtin_expect(!!(x), 0)
109#else
110# define LIKELY(x) (x)
111# define UNLIKELY(x) (x)
112#endif
113
114#ifndef __KERNEL_GPU__
115# include <cassert>
116# define util_assert(statement) assert(statement)
117#else
118# define util_assert(statement)
119#endif
120
121#if (defined __KERNEL_METAL__) && (__METAL_VERSION__ >= 320)
122# define __METAL_PRINTF__
123#endif
124
125/* Metal's logging works very similar to `printf()`, except for a few differences:
126 * - %s is not supported,
127 * - double doesn't exist, so no casting to double for %f,
128 * - no `\n` needed at the end of the format string.
129 * NOTE: To see the print in the console, environment variables `MTL_LOG_LEVEL` should be set to
130 * `MTLLogLevelDebug`, and `MTL_LOG_TO_STDERR` should be set to `1`.
131 * See https://developer.apple.com/documentation/metal/logging-shader-debug-messages */
132# ifdef __METAL_PRINTF__
133# define printf(...) metal::os_log_default.log_debug(__VA_ARGS__)
134# endif