Blender  V2.93
util_defines.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2011-2017 Blender Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* clang-format off */
19 
20 /* #define __forceinline triggers a bug in some clang-format versions, disable
21  * format for entire file to keep results consistent. */
22 
23 #ifndef __UTIL_DEFINES_H__
24 #define __UTIL_DEFINES_H__
25 
26 /* Bitness */
27 
28 #if defined(__ppc64__) || defined(__PPC64__) || defined(__x86_64__) || defined(__ia64__) || \
29  defined(_M_X64) || defined(__aarch64__)
30 # define __KERNEL_64_BIT__
31 #endif
32 
33 /* Qualifiers for kernel code shared by CPU and GPU */
34 
35 #ifndef __KERNEL_GPU__
36 # define ccl_device static inline
37 # define ccl_device_noinline static
38 # define ccl_device_noinline_cpu ccl_device_noinline
39 # define ccl_global
40 # define ccl_static_constant static const
41 # define ccl_constant const
42 # define ccl_local
43 # define ccl_local_param
44 # define ccl_private
45 # define ccl_restrict __restrict
46 # define ccl_ref &
47 # define ccl_optional_struct_init
48 # define ccl_loop_no_unroll
49 # define __KERNEL_WITH_SSE_ALIGN__
50 
51 # if defined(_WIN32) && !defined(FREE_WINDOWS)
52 # define ccl_device_inline static __forceinline
53 # define ccl_device_forceinline static __forceinline
54 # define ccl_align(...) __declspec(align(__VA_ARGS__))
55 # ifdef __KERNEL_64_BIT__
56 # define ccl_try_align(...) __declspec(align(__VA_ARGS__))
57 # else /* __KERNEL_64_BIT__ */
58 # undef __KERNEL_WITH_SSE_ALIGN__
59 /* No support for function arguments (error C2719). */
60 # define ccl_try_align(...)
61 # endif /* __KERNEL_64_BIT__ */
62 # define ccl_may_alias
63 # define ccl_always_inline __forceinline
64 # define ccl_never_inline __declspec(noinline)
65 # define ccl_maybe_unused
66 # else /* _WIN32 && !FREE_WINDOWS */
67 # define ccl_device_inline static inline __attribute__((always_inline))
68 # define ccl_device_forceinline static inline __attribute__((always_inline))
69 # define ccl_align(...) __attribute__((aligned(__VA_ARGS__)))
70 # ifndef FREE_WINDOWS64
71 # define __forceinline inline __attribute__((always_inline))
72 # endif
73 # define ccl_try_align(...) __attribute__((aligned(__VA_ARGS__)))
74 # define ccl_may_alias __attribute__((__may_alias__))
75 # define ccl_always_inline __attribute__((always_inline))
76 # define ccl_never_inline __attribute__((noinline))
77 # define ccl_maybe_unused __attribute__((used))
78 # endif /* _WIN32 && !FREE_WINDOWS */
79 
80 /* Use to suppress '-Wimplicit-fallthrough' (in place of 'break'). */
81 # ifndef ATTR_FALLTHROUGH
82 # if defined(__GNUC__) && (__GNUC__ >= 7) /* gcc7.0+ only */
83 # define ATTR_FALLTHROUGH __attribute__((fallthrough))
84 # else
85 # define ATTR_FALLTHROUGH ((void)0)
86 # endif
87 # endif
88 #endif /* __KERNEL_GPU__ */
89 
90 /* macros */
91 
92 /* hints for branch prediction, only use in code that runs a _lot_ */
93 #if defined(__GNUC__) && defined(__KERNEL_CPU__)
94 # define LIKELY(x) __builtin_expect(!!(x), 1)
95 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
96 #else
97 # define LIKELY(x) (x)
98 # define UNLIKELY(x) (x)
99 #endif
100 
101 #if defined(__GNUC__) || defined(__clang__)
102 # if defined(__cplusplus)
103 /* Some magic to be sure we don't have reference in the type. */
104 template<typename T> static inline T decltype_helper(T x)
105 {
106  return x;
107 }
108 # define TYPEOF(x) decltype(decltype_helper(x))
109 # else
110 # define TYPEOF(x) typeof(x)
111 # endif
112 #endif
113 
114 /* Causes warning:
115  * incompatible types when assigning to type 'Foo' from type 'Bar'
116  * ... the compiler optimizes away the temp var */
117 #ifdef __GNUC__
118 # define CHECK_TYPE(var, type) \
119  { \
120  TYPEOF(var) * __tmp; \
121  __tmp = (type *)NULL; \
122  (void)__tmp; \
123  } \
124  (void)0
125 
126 # define CHECK_TYPE_PAIR(var_a, var_b) \
127  { \
128  TYPEOF(var_a) * __tmp; \
129  __tmp = (typeof(var_b) *)NULL; \
130  (void)__tmp; \
131  } \
132  (void)0
133 #else
134 # define CHECK_TYPE(var, type)
135 # define CHECK_TYPE_PAIR(var_a, var_b)
136 #endif
137 
138 /* can be used in simple macros */
139 #define CHECK_TYPE_INLINE(val, type) ((void)(((type)0) != (val)))
140 
141 #ifndef __KERNEL_GPU__
142 # include <cassert>
143 # define util_assert(statement) assert(statement)
144 #else
145 # define util_assert(statement)
146 #endif
147 
148 #endif /* __UTIL_DEFINES_H__ */
#define T