Blender V5.0
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(__SANITIZE_ADDRESS__) && (defined(__GNUC__) || defined(__clang__))
91# define ccl_ignore_integer_overflow [[gnu::no_sanitize("signed-integer-overflow")]]
92# else
93# define ccl_ignore_integer_overflow
94# endif
95#endif
96
97/* macros */
98
99/* hints for branch prediction, only use in code that runs a _lot_ */
100#if defined(__GNUC__) && !defined(__KERNEL_GPU__)
101# define LIKELY(x) __builtin_expect(!!(x), 1)
102# define UNLIKELY(x) __builtin_expect(!!(x), 0)
103#else
104# define LIKELY(x) (x)
105# define UNLIKELY(x) (x)
106#endif
107
108#ifndef __KERNEL_GPU__
109# include <cassert>
110# define util_assert(statement) assert(statement)
111#else
112# define util_assert(statement)
113#endif
114
115#define CONCAT_HELPER(a, ...) a##__VA_ARGS__
116#define CONCAT(a, ...) CONCAT_HELPER(a, __VA_ARGS__)
117
118#if (defined __KERNEL_METAL__) && (__METAL_VERSION__ >= 320)
119# define __METAL_PRINTF__
120#endif
121
122/* Metal's logging works very similar to `printf()`, except for a few differences:
123 * - %s is not supported,
124 * - double doesn't exist, so no casting to double for %f,
125 * - no `\n` needed at the end of the format string.
126 * NOTE: To see the print in the console, environment variables `MTL_LOG_LEVEL` should be set to
127 * `MTLLogLevelDebug`, and `MTL_LOG_TO_STDERR` should be set to `1`.
128 * See https://developer.apple.com/documentation/metal/logging-shader-debug-messages */
129# ifdef __METAL_PRINTF__
130# define printf(...) metal::os_log_default.log_debug(__VA_ARGS__)
131# endif