xref: /aosp_15_r20/external/XNNPACK/src/xnnpack/common.h (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8 
9 #pragma once
10 
11 #if defined(__APPLE__)
12   #include <TargetConditionals.h>
13 #endif
14 
15 
16 // Define architecture identification macros
17 
18 #if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86)
19   #define XNN_ARCH_X86 1
20 #else
21   #define XNN_ARCH_X86 0
22 #endif
23 
24 #if defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
25   #define XNN_ARCH_X86_64 1
26 #else
27   #define XNN_ARCH_X86_64 0
28 #endif
29 
30 #if defined(__arm__) || defined(_M_ARM)
31   #define XNN_ARCH_ARM 1
32 #else
33   #define XNN_ARCH_ARM 0
34 #endif
35 
36 #if defined(__aarch64__) || defined(_M_ARM64)
37   #define XNN_ARCH_ARM64 1
38 #else
39   #define XNN_ARCH_ARM64 0
40 #endif
41 
42 #if defined(__PPC64__) || defined(__ppc64__) || defined(__powerpc64__) || defined(_ARCH_PPC64)
43   #define XNN_ARCH_PPC64 1
44 #else
45   #define XNN_ARCH_PPC64 0
46 #endif
47 
48 #if defined(__riscv) || defined(__riscv__)
49   #define XNN_ARCH_RISCV 1
50 #else
51   #define XNN_ARCH_RISCV 0
52 #endif
53 
54 #if defined(__wasm__)
55   #if defined(__wasm_relaxed_simd__)
56     #define XNN_ARCH_WASM 0
57     #define XNN_ARCH_WASMSIMD 0
58     #define XNN_ARCH_WASMRELAXEDSIMD 1
59   #elif defined(__wasm_simd128__)
60     #define XNN_ARCH_WASM 0
61     #define XNN_ARCH_WASMSIMD 1
62     #define XNN_ARCH_WASMRELAXEDSIMD 0
63   #else
64     #define XNN_ARCH_WASM 1
65     #define XNN_ARCH_WASMSIMD 0
66     #define XNN_ARCH_WASMRELAXEDSIMD 0
67   #endif
68 #else
69   #define XNN_ARCH_WASM 0
70   #define XNN_ARCH_WASMSIMD 0
71   #define XNN_ARCH_WASMRELAXEDSIMD 0
72 #endif
73 
74 // Define platform identification macros
75 
76 #if defined(__ANDROID__)
77   #define XNN_PLATFORM_ANDROID 1
78 #else
79   #define XNN_PLATFORM_ANDROID 0
80 #endif
81 
82 #if defined(__linux__)
83   #define XNN_PLATFORM_LINUX 1
84 #else
85   #define XNN_PLATFORM_LINUX 0
86 #endif
87 
88 #if defined(__APPLE__) && TARGET_OS_IPHONE
89   // iOS on iPhone / iPad Touch, iPad OS, watchOS, or tvOS
90   #define XNN_PLATFORM_IOS 1
91 #else
92   #define XNN_PLATFORM_IOS 0
93 #endif
94 
95 #if defined(__APPLE__) && TARGET_OS_MAC
96   #define XNN_PLATFORM_MAC 1
97 #else
98   #define XNN_PLATFORM_MAC 0
99 #endif
100 
101 #if XNN_PLATFORM_ANDROID || XNN_PLATFORM_IOS
102   #define XNN_PLATFORM_MOBILE 1
103 #else
104   #define XNN_PLATFORM_MOBILE 0
105 #endif
106 
107 #if defined(__EMSCRIPTEN__) || defined(__wasm__)
108   #define XNN_PLATFORM_WEB 1
109 #else
110   #define XNN_PLATFORM_WEB 0
111 #endif
112 
113 #if defined(_WIN32)
114   #define XNN_PLATFORM_WINDOWS 1
115 #else
116   #define XNN_PLATFORM_WINDOWS 0
117 #endif
118 
119 #if defined(__Fuchsia__)
120   #define XNN_PLATFORM_FUCHSIA 1
121 #else
122   #define XNN_PLATFORM_FUCHSIA 0
123 #endif
124 
125 #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS && !XNN_PLATFORM_FUCHSIA
126   #define XNN_PLATFORM_JIT 1
127 #else
128   #define XNN_PLATFORM_JIT 0
129 #endif
130 
131 // Define compile identification macros
132 
133 #if defined(__clang__)
134   #define XNN_COMPILER_CLANG 1
135 #elif defined(__INTEL_COMPILER)
136   #define XNN_COMPILER_ICC 1
137 #elif defined(_MSC_VER)
138   #define XNN_COMPILER_MSVC 1
139 #elif defined(__GNUC__)
140   #define XNN_COMPILER_GCC 1
141 #endif
142 
143 #ifndef XNN_COMPILER_CLANG
144   #define XNN_COMPILER_CLANG 0
145 #endif
146 
147 #ifndef XNN_COMPILER_GCC
148   #define XNN_COMPILER_GCC 0
149 #endif
150 
151 #ifndef XNN_COMPILER_MSVC
152   #define XNN_COMPILER_MSVC 0
153 #endif
154 
155 #ifndef XNN_COMPILER_ICC
156   #define XNN_COMPILER_ICC 0
157 #endif
158 
159 
160 #ifndef XNN_TEST_MODE
161   #define XNN_TEST_MODE 0
162 #endif
163 
164 #ifndef XNN_MAX_UARCH_TYPES
165   #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS
166     #define XNN_MAX_UARCH_TYPES 3
167   #else
168     #define XNN_MAX_UARCH_TYPES 1
169   #endif
170 #endif
171 
172 #define XNN_UARCH_DEFAULT 0
173 
174 #if defined(__has_builtin)
175   #define XNN_COMPILER_HAS_BUILTIN(builtin) __has_builtin(builtin)
176 #else
177   #define XNN_COMPILER_HAS_BUILTIN(builtin) 0
178 #endif
179 
180 #if defined(__has_feature)
181   #define XNN_COMPILER_HAS_FEATURE(builtin) __has_feature(builtin)
182 #else
183   #define XNN_COMPILER_HAS_FEATURE(builtin) 0
184 #endif
185 
186 #if defined(__GNUC__)
187   #if defined(__clang__) || (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 5)
188     #define XNN_UNREACHABLE do { __builtin_unreachable(); } while (0)
189   #else
190     #define XNN_UNREACHABLE do { __builtin_trap(); } while (0)
191   #endif
192 #elif defined(_MSC_VER)
193   #define XNN_UNREACHABLE __assume(0)
194 #else
195   #define XNN_UNREACHABLE do { } while (0)
196 #endif
197 
198 #if defined(__GNUC__)
199   #define XNN_ALIGN(alignment) __attribute__((__aligned__(alignment)))
200 #elif defined(_MSC_VER)
201   #define XNN_ALIGN(alignment) __declspec(align(alignment))
202 #else
203   #error "Platform-specific implementation of XNN_ALIGN required"
204 #endif
205 
206 #if defined(__GNUC__)
207   #define XNN_UNALIGNED __attribute__((__aligned__(1)))
208 #elif defined(_MSC_VER)
209   #if defined(_M_IX86)
210     #define XNN_UNALIGNED
211   #else
212     #define XNN_UNALIGNED __unaligned
213   #endif
214 #else
215   #error "Platform-specific implementation of XNN_UNALIGNED required"
216 #endif
217 
218 #define XNN_COUNT_OF(array) (sizeof(array) / sizeof(0[array]))
219 
220 #if defined(__cplusplus) || XNN_COMPILER_MSVC
221   #define XNN_MIN_ELEMENTS(count) count
222 #else
223   #define XNN_MIN_ELEMENTS(count) static count
224 #endif
225 
226 #if defined(__GNUC__)
227   #define XNN_LIKELY(condition) (__builtin_expect(!!(condition), 1))
228   #define XNN_UNLIKELY(condition) (__builtin_expect(!!(condition), 0))
229 #else
230   #define XNN_LIKELY(condition) (!!(condition))
231   #define XNN_UNLIKELY(condition) (!!(condition))
232 #endif
233 
234 #if XNN_COMPILER_HAS_BUILTIN(__builtin_unpredictable)
235   #define XNN_UNPREDICTABLE(condition) (__builtin_unpredictable(!!(condition)))
236 #elif defined(__GNUC__) && (__GNUC__ >= 9) && !defined(__INTEL_COMPILER)
237   #define XNN_UNPREDICTABLE(condition) (__builtin_expect_with_probability(!!(condition), 0, 0.5))
238 #else
239   #define XNN_UNPREDICTABLE(condition) (!!(condition))
240 #endif
241 
242 #if XNN_COMPILER_HAS_FEATURE(thread_sanitizer)
243   #define XNN_DISABLE_TSAN __attribute__((__no_sanitize__("thread")))
244 #else
245   #define XNN_DISABLE_TSAN
246 #endif
247 
248 #if XNN_COMPILER_HAS_FEATURE(memory_sanitizer)
249   #define XNN_DISABLE_MSAN __attribute__((__no_sanitize__("memory")))
250 #else
251   #define XNN_DISABLE_MSAN
252 #endif
253 
254 #if XNN_COMPILER_HAS_FEATURE(hwaddress_sanitizer)
255   #define XNN_DISABLE_HWASAN __attribute__((__no_sanitize__("hwaddress")))
256 #else
257   #define XNN_DISABLE_HWASAN
258 #endif
259 
260 #define XNN_OOB_READS XNN_DISABLE_TSAN XNN_DISABLE_MSAN XNN_DISABLE_HWASAN
261 
262 #if defined(__GNUC__)
263   #define XNN_INTRINSIC inline __attribute__((__always_inline__, __artificial__))
264 #elif defined(_MSC_VER)
265   #define XNN_INTRINSIC __forceinline
266 #else
267   #define XNN_INTRINSIC inline
268 #endif
269 
270 #if defined(__GNUC__)
271   #define XNN_INLINE inline __attribute__((__always_inline__))
272 #elif defined(_MSC_VER)
273   #define XNN_INLINE __forceinline
274 #else
275   #define XNN_INLINE inline
276 #endif
277 
278 #ifndef XNN_INTERNAL
279   #if defined(__ELF__)
280     #define XNN_INTERNAL __attribute__((__visibility__("internal")))
281   #elif defined(__MACH__)
282     #define XNN_INTERNAL __attribute__((__visibility__("hidden")))
283   #else
284     #define XNN_INTERNAL
285   #endif
286 #endif
287 
288 #ifndef XNN_PRIVATE
289   #if defined(__ELF__)
290     #define XNN_PRIVATE __attribute__((__visibility__("hidden")))
291   #elif defined(__MACH__)
292     #define XNN_PRIVATE __attribute__((__visibility__("hidden")))
293   #else
294     #define XNN_PRIVATE
295   #endif
296 #endif
297