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(__APPLE__) && TARGET_OS_IPHONE 83 // iOS on iPhone / iPad Touch, iPad OS, watchOS, or tvOS 84 #define XNN_PLATFORM_IOS 1 85 #else 86 #define XNN_PLATFORM_IOS 0 87 #endif 88 89 #if defined(__APPLE__) && TARGET_OS_MAC 90 #define XNN_PLATFORM_MAC 1 91 #else 92 #define XNN_PLATFORM_MAC 0 93 #endif 94 95 #if XNN_PLATFORM_ANDROID || XNN_PLATFORM_IOS 96 #define XNN_PLATFORM_MOBILE 1 97 #else 98 #define XNN_PLATFORM_MOBILE 0 99 #endif 100 101 #if defined(__EMSCRIPTEN__) || defined(__wasm__) 102 #define XNN_PLATFORM_WEB 1 103 #else 104 #define XNN_PLATFORM_WEB 0 105 #endif 106 107 #if defined(_WIN32) 108 #define XNN_PLATFORM_WINDOWS 1 109 #else 110 #define XNN_PLATFORM_WINDOWS 0 111 #endif 112 113 #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS 114 #define XNN_PLATFORM_JIT 1 115 #else 116 #define XNN_PLATFORM_JIT 0 117 #endif 118 119 // Define compile identification macros 120 121 #if defined(__clang__) 122 #define XNN_COMPILER_CLANG 1 123 #elif defined(__INTEL_COMPILER) 124 #define XNN_COMPILER_ICC 1 125 #elif defined(_MSC_VER) 126 #define XNN_COMPILER_MSVC 1 127 #elif defined(__GNUC__) 128 #define XNN_COMPILER_GCC 1 129 #endif 130 131 #ifndef XNN_COMPILER_CLANG 132 #define XNN_COMPILER_CLANG 0 133 #endif 134 135 #ifndef XNN_COMPILER_GCC 136 #define XNN_COMPILER_GCC 0 137 #endif 138 139 #ifndef XNN_COMPILER_MSVC 140 #define XNN_COMPILER_MSVC 0 141 #endif 142 143 #ifndef XNN_COMPILER_ICC 144 #define XNN_COMPILER_ICC 0 145 #endif 146 147 148 #ifndef XNN_TEST_MODE 149 #define XNN_TEST_MODE 0 150 #endif 151 152 #ifndef XNN_MAX_UARCH_TYPES 153 #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS 154 #define XNN_MAX_UARCH_TYPES 3 155 #else 156 #define XNN_MAX_UARCH_TYPES 1 157 #endif 158 #endif 159 160 #define XNN_UARCH_DEFAULT 0 161 162 #if defined(__has_builtin) 163 #define XNN_COMPILER_HAS_BUILTIN(builtin) __has_builtin(builtin) 164 #else 165 #define XNN_COMPILER_HAS_BUILTIN(builtin) 0 166 #endif 167 168 #if defined(__has_feature) 169 #define XNN_COMPILER_HAS_FEATURE(builtin) __has_feature(builtin) 170 #else 171 #define XNN_COMPILER_HAS_FEATURE(builtin) 0 172 #endif 173 174 #if defined(__GNUC__) 175 #if defined(__clang__) || (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 5) 176 #define XNN_UNREACHABLE do { __builtin_unreachable(); } while (0) 177 #else 178 #define XNN_UNREACHABLE do { __builtin_trap(); } while (0) 179 #endif 180 #elif defined(_MSC_VER) 181 #define XNN_UNREACHABLE __assume(0) 182 #else 183 #define XNN_UNREACHABLE do { } while (0) 184 #endif 185 186 #if defined(__GNUC__) 187 #define XNN_ALIGN(alignment) __attribute__((__aligned__(alignment))) 188 #elif defined(_MSC_VER) 189 #define XNN_ALIGN(alignment) __declspec(align(alignment)) 190 #else 191 #error "Platform-specific implementation of XNN_ALIGN required" 192 #endif 193 194 #if defined(__GNUC__) 195 #define XNN_UNALIGNED __attribute__((__aligned__(1))) 196 #elif defined(_MSC_VER) 197 #if defined(_M_IX86) 198 #define XNN_UNALIGNED 199 #else 200 #define XNN_UNALIGNED __unaligned 201 #endif 202 #else 203 #error "Platform-specific implementation of XNN_UNALIGNED required" 204 #endif 205 206 #define XNN_COUNT_OF(array) (sizeof(array) / sizeof(0[array])) 207 208 #if defined(__cplusplus) || XNN_COMPILER_MSVC 209 #define XNN_MIN_ELEMENTS(count) count 210 #else 211 #define XNN_MIN_ELEMENTS(count) static count 212 #endif 213 214 #if defined(__GNUC__) 215 #define XNN_LIKELY(condition) (__builtin_expect(!!(condition), 1)) 216 #define XNN_UNLIKELY(condition) (__builtin_expect(!!(condition), 0)) 217 #else 218 #define XNN_LIKELY(condition) (!!(condition)) 219 #define XNN_UNLIKELY(condition) (!!(condition)) 220 #endif 221 222 #if XNN_COMPILER_HAS_BUILTIN(__builtin_unpredictable) 223 #define XNN_UNPREDICTABLE(condition) (__builtin_unpredictable(!!(condition))) 224 #elif defined(__GNUC__) && (__GNUC__ >= 9) && !defined(__INTEL_COMPILER) 225 #define XNN_UNPREDICTABLE(condition) (__builtin_expect_with_probability(!!(condition), 0, 0.5)) 226 #else 227 #define XNN_UNPREDICTABLE(condition) (!!(condition)) 228 #endif 229 230 #if XNN_COMPILER_HAS_FEATURE(thread_sanitizer) 231 #define XNN_DISABLE_TSAN __attribute__((__no_sanitize__("thread"))) 232 #else 233 #define XNN_DISABLE_TSAN 234 #endif 235 236 #if XNN_COMPILER_HAS_FEATURE(memory_sanitizer) 237 #define XNN_DISABLE_MSAN __attribute__((__no_sanitize__("memory"))) 238 #else 239 #define XNN_DISABLE_MSAN 240 #endif 241 242 #define XNN_OOB_READS XNN_DISABLE_TSAN XNN_DISABLE_MSAN 243 244 #if defined(__GNUC__) 245 #define XNN_INTRINSIC inline __attribute__((__always_inline__, __artificial__)) 246 #elif defined(_MSC_VER) 247 #define XNN_INTRINSIC __forceinline 248 #else 249 #define XNN_INTRINSIC inline 250 #endif 251 252 #if defined(__GNUC__) 253 #define XNN_INLINE inline __attribute__((__always_inline__)) 254 #elif defined(_MSC_VER) 255 #define XNN_INLINE __forceinline 256 #else 257 #define XNN_INLINE inline 258 #endif 259 260 #ifndef XNN_INTERNAL 261 #if defined(__ELF__) 262 #define XNN_INTERNAL __attribute__((__visibility__("internal"))) 263 #elif defined(__MACH__) 264 #define XNN_INTERNAL __attribute__((__visibility__("hidden"))) 265 #else 266 #define XNN_INTERNAL 267 #endif 268 #endif 269 270 #ifndef XNN_PRIVATE 271 #if defined(__ELF__) 272 #define XNN_PRIVATE __attribute__((__visibility__("hidden"))) 273 #elif defined(__MACH__) 274 #define XNN_PRIVATE __attribute__((__visibility__("hidden"))) 275 #else 276 #define XNN_PRIVATE 277 #endif 278 #endif 279