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(__wasm__) 49 #if defined(__wasm_simd128__) 50 #define XNN_ARCH_WASMSIMD 1 51 #define XNN_ARCH_WASM 0 52 #else 53 #define XNN_ARCH_WASM 1 54 #define XNN_ARCH_WASMSIMD 0 55 #endif 56 #else 57 #define XNN_ARCH_WASM 0 58 #define XNN_ARCH_WASMSIMD 0 59 #endif 60 61 // Define platform identification macros 62 63 #if defined(__ANDROID__) 64 #define XNN_PLATFORM_ANDROID 1 65 #else 66 #define XNN_PLATFORM_ANDROID 0 67 #endif 68 69 #if defined(__APPLE__) && TARGET_OS_IPHONE 70 // iOS on iPhone / iPad Touch, iPad OS, watchOS, or tvOS 71 #define XNN_PLATFORM_IOS 1 72 #else 73 #define XNN_PLATFORM_IOS 0 74 #endif 75 76 #if defined(__APPLE__) && TARGET_OS_MAC 77 #define XNN_PLATFORM_MAC 1 78 #else 79 #define XNN_PLATFORM_MAC 0 80 #endif 81 82 #if XNN_PLATFORM_ANDROID || XNN_PLATFORM_IOS 83 #define XNN_PLATFORM_MOBILE 1 84 #else 85 #define XNN_PLATFORM_MOBILE 0 86 #endif 87 88 #if defined(__EMSCRIPTEN__) || defined(__wasm__) 89 #define XNN_PLATFORM_WEB 1 90 #else 91 #define XNN_PLATFORM_WEB 0 92 #endif 93 94 // Define compile identification macros 95 96 #if defined(__clang__) 97 #define XNN_COMPILER_CLANG 1 98 #elif defined(__INTEL_COMPILER) 99 #define XNN_COMPILER_ICC 1 100 #elif defined(_MSC_VER) 101 #define XNN_COMPILER_MSVC 1 102 #elif defined(__GNUC__) 103 #define XNN_COMPILER_GCC 1 104 #endif 105 106 #ifndef XNN_COMPILER_CLANG 107 #define XNN_COMPILER_CLANG 0 108 #endif 109 110 #ifndef XNN_COMPILER_GCC 111 #define XNN_COMPILER_GCC 0 112 #endif 113 114 #ifndef XNN_COMPILER_MSVC 115 #define XNN_COMPILER_MSVC 0 116 #endif 117 118 #ifndef XNN_COMPILER_ICC 119 #define XNN_COMPILER_ICC 0 120 #endif 121 122 123 #ifndef XNN_TEST_MODE 124 #define XNN_TEST_MODE 0 125 #endif 126 127 #ifndef XNN_MAX_UARCH_TYPES 128 #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS 129 #define XNN_MAX_UARCH_TYPES 3 130 #else 131 #define XNN_MAX_UARCH_TYPES 1 132 #endif 133 #endif 134 135 #define XNN_UARCH_DEFAULT 0 136 137 #if defined(__has_builtin) 138 #define XNN_COMPILER_HAS_BUILTIN(builtin) __has_builtin(builtin) 139 #else 140 #define XNN_COMPILER_HAS_BUILTIN(builtin) 0 141 #endif 142 143 #if defined(__has_feature) 144 #define XNN_COMPILER_HAS_FEATURE(builtin) __has_feature(builtin) 145 #else 146 #define XNN_COMPILER_HAS_FEATURE(builtin) 0 147 #endif 148 149 #if defined(__GNUC__) 150 #if defined(__clang__) || (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 5) 151 #define XNN_UNREACHABLE do { __builtin_unreachable(); } while (0) 152 #else 153 #define XNN_UNREACHABLE do { __builtin_trap(); } while (0) 154 #endif 155 #elif defined(_MSC_VER) 156 #define XNN_UNREACHABLE __assume(0) 157 #else 158 #define XNN_UNREACHABLE do { } while (0) 159 #endif 160 161 #if defined(__GNUC__) 162 #define XNN_ALIGN(alignment) __attribute__((__aligned__(alignment))) 163 #elif defined(_MSC_VER) 164 #define XNN_ALIGN(alignment) __declspec(align(alignment)) 165 #else 166 #error "Platform-specific implementation of XNN_ALIGN required" 167 #endif 168 169 #define XNN_COUNT_OF(array) (sizeof(array) / sizeof(0[array])) 170 171 #if defined(__cplusplus) || XNN_COMPILER_MSVC 172 #define XNN_MIN_ELEMENTS(count) count 173 #else 174 #define XNN_MIN_ELEMENTS(count) static count 175 #endif 176 177 #if defined(__GNUC__) 178 #define XNN_LIKELY(condition) (__builtin_expect(!!(condition), 1)) 179 #define XNN_UNLIKELY(condition) (__builtin_expect(!!(condition), 0)) 180 #else 181 #define XNN_LIKELY(condition) (!!(condition)) 182 #define XNN_UNLIKELY(condition) (!!(condition)) 183 #endif 184 185 #if XNN_COMPILER_HAS_BUILTIN(__builtin_unpredictable) 186 #define XNN_UNPREDICTABLE(condition) (__builtin_unpredictable(!!(condition))) 187 #elif defined(__GNUC__) && (__GNUC__ >= 9) && !defined(__INTEL_COMPILER) 188 #define XNN_UNPREDICTABLE(condition) (__builtin_expect_with_probability(!!(condition), 0, 0.5)) 189 #else 190 #define XNN_UNPREDICTABLE(condition) (!!(condition)) 191 #endif 192 193 #if XNN_COMPILER_HAS_FEATURE(thread_sanitizer) 194 #define XNN_DISABLE_TSAN __attribute__((__no_sanitize__("thread"))) 195 #else 196 #define XNN_DISABLE_TSAN 197 #endif 198 199 #if defined(__GNUC__) 200 #define XNN_INTRINSIC inline __attribute__((__always_inline__, __artificial__)) 201 #elif defined(_MSC_VER) 202 #define XNN_INTRINSIC __forceinline 203 #else 204 #define XNN_INTRINSIC inline 205 #endif 206 207 #if defined(__GNUC__) 208 #define XNN_INLINE inline __attribute__((__always_inline__)) 209 #elif defined(_MSC_VER) 210 #define XNN_INLINE __forceinline 211 #else 212 #define XNN_INLINE inline 213 #endif 214 215 #ifndef XNN_INTERNAL 216 #if defined(__ELF__) 217 #define XNN_INTERNAL __attribute__((__visibility__("internal"))) 218 #elif defined(__MACH__) 219 #define XNN_INTERNAL __attribute__((__visibility__("hidden"))) 220 #else 221 #define XNN_INTERNAL 222 #endif 223 #endif 224 225 #ifndef XNN_PRIVATE 226 #if defined(__ELF__) 227 #define XNN_PRIVATE __attribute__((__visibility__("hidden"))) 228 #elif defined(__MACH__) 229 #define XNN_PRIVATE __attribute__((__visibility__("hidden"))) 230 #else 231 #define XNN_PRIVATE 232 #endif 233 #endif 234