chromium/third_party/xnnpack/src/src/xnnpack/common.h

// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#pragma once

#if defined(__APPLE__)
  #include <TargetConditionals.h>
#endif


// Define architecture identification macros

#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86)
  #define XNN_ARCH_X86
#else
  #define XNN_ARCH_X86
#endif

#if defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) && !defined(_M_ARM64EC)
  #define XNN_ARCH_X86_64
#else
  #define XNN_ARCH_X86_64
#endif

#if defined(__arm__) || defined(_M_ARM)
  #define XNN_ARCH_ARM
#else
  #define XNN_ARCH_ARM
#endif

#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
  #define XNN_ARCH_ARM64
#else
  #define XNN_ARCH_ARM64
#endif

#if defined(__PPC64__) || defined(__ppc64__) || defined(__powerpc64__) || defined(_ARCH_PPC64)
  #define XNN_ARCH_PPC64
#else
  #define XNN_ARCH_PPC64
#endif

#if defined(__riscv) || defined(__riscv__)
  #define XNN_ARCH_RISCV
#else
  #define XNN_ARCH_RISCV
#endif

#if defined(__hexagon__)
  #define XNN_ARCH_HEXAGON
#else
  #define XNN_ARCH_HEXAGON
#endif

#if defined(__wasm__)
  #if defined(__wasm_relaxed_simd__)
    #define XNN_ARCH_WASM
    #define XNN_ARCH_WASMSIMD
    #define XNN_ARCH_WASMRELAXEDSIMD
  #elif defined(__wasm_simd128__)
    #define XNN_ARCH_WASM
    #define XNN_ARCH_WASMSIMD
    #define XNN_ARCH_WASMRELAXEDSIMD
  #else
    #define XNN_ARCH_WASM
    #define XNN_ARCH_WASMSIMD
    #define XNN_ARCH_WASMRELAXEDSIMD
  #endif
#else
  #define XNN_ARCH_WASM
  #define XNN_ARCH_WASMSIMD
  #define XNN_ARCH_WASMRELAXEDSIMD
#endif

// Define platform identification macros

#if defined(__ANDROID__)
  #define XNN_PLATFORM_ANDROID
#else
  #define XNN_PLATFORM_ANDROID
#endif

#if defined(__linux__)
  #define XNN_PLATFORM_LINUX
#else
  #define XNN_PLATFORM_LINUX
#endif

#if defined(__APPLE__) && TARGET_OS_IPHONE
  // iOS on iPhone / iPad Touch, iPad OS, watchOS, or tvOS
  #define XNN_PLATFORM_IOS
#else
  #define XNN_PLATFORM_IOS
#endif

#if defined(__APPLE__) && TARGET_OS_MAC
  #define XNN_PLATFORM_MAC
#else
  #define XNN_PLATFORM_MAC
#endif

// Mobile build x86 versions for debugging
#if XNN_PLATFORM_ANDROID || XNN_PLATFORM_IOS
  #define XNN_PLATFORM_MOBILE
#else
  #define XNN_PLATFORM_MOBILE
#endif

#if defined(__EMSCRIPTEN__) || defined(__wasm__)
  #define XNN_PLATFORM_WEB
#else
  #define XNN_PLATFORM_WEB
#endif

#if defined(_WIN32)
  #define XNN_PLATFORM_WINDOWS
#else
  #define XNN_PLATFORM_WINDOWS
#endif

#if defined(__Fuchsia__)
  #define XNN_PLATFORM_FUCHSIA
#else
  #define XNN_PLATFORM_FUCHSIA
#endif

#if defined(__hexagon__) && !defined(__linux__)
  #define XNN_PLATFORM_QURT
#else
  #define XNN_PLATFORM_QURT
#endif

#ifndef XNN_PLATFORM_JIT
  #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS && !XNN_PLATFORM_FUCHSIA || XNN_PLATFORM_WEB
    #define XNN_PLATFORM_JIT
  #else
    #define XNN_PLATFORM_JIT
  #endif
#endif  // XNN_PLATFORM_JIT

// Define compile identification macros

#if defined(__clang__)
  #define XNN_COMPILER_CLANG
#elif defined(__INTEL_COMPILER)
  #define XNN_COMPILER_ICC
#elif defined(_MSC_VER)
  #define XNN_COMPILER_MSVC
#elif defined(__GNUC__)
  #define XNN_COMPILER_GCC
#endif

#ifndef XNN_COMPILER_CLANG
  #define XNN_COMPILER_CLANG
#endif

#ifndef XNN_COMPILER_GCC
  #define XNN_COMPILER_GCC
#endif

#ifndef XNN_COMPILER_MSVC
  #define XNN_COMPILER_MSVC
#endif

#ifndef XNN_COMPILER_ICC
  #define XNN_COMPILER_ICC
#endif

#ifndef XNN_MAX_UARCH_TYPES
  #if (XNN_ARCH_ARM || XNN_ARCH_ARM64) && !XNN_PLATFORM_IOS
    #define XNN_MAX_UARCH_TYPES
  #else
    #define XNN_MAX_UARCH_TYPES
  #endif
#endif

#define XNN_UARCH_DEFAULT

#if defined(__has_builtin)
  #define XNN_COMPILER_HAS_BUILTIN(builtin)
#else
  #define XNN_COMPILER_HAS_BUILTIN
#endif

#if defined(__has_feature)
  #define XNN_COMPILER_HAS_FEATURE(builtin)
#else
  #define XNN_COMPILER_HAS_FEATURE
#endif

#if defined(__GNUC__)
  #if defined(__clang__) || (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 5)
    #define XNN_UNREACHABLE
  #else
    #define XNN_UNREACHABLE
  #endif
#elif defined(_MSC_VER)
  #define XNN_UNREACHABLE
#else
  #define XNN_UNREACHABLE
#endif

#if defined(__GNUC__)
  #define XNN_ALIGN(alignment)
#elif defined(_MSC_VER)
  #define XNN_ALIGN
#else
  #error "Platform-specific implementation of XNN_ALIGN required"
#endif

#if defined(__GNUC__)
  #define XNN_UNALIGNED
#elif defined(_MSC_VER)
  #if defined(_M_IX86)
    #define XNN_UNALIGNED
  #else
    #define XNN_UNALIGNED
  #endif
#else
  #error "Platform-specific implementation of XNN_UNALIGNED required"
#endif

#define XNN_COUNT_OF(array)

#if defined(__cplusplus) || XNN_COMPILER_MSVC || XNN_COMPILER_CLANG
  // static as array indices in function parameter declaration is a C99 feature, not supported in C++.
  // MSVC does not support this feature, even in C mode.
  // Clang generates suboptimal code, see https://github.com/llvm/llvm-project/issues/59120
  #define XNN_MIN_ELEMENTS(count)
#else
  #define XNN_MIN_ELEMENTS
#endif

#if defined(__cplusplus) || XNN_COMPILER_MSVC
  #define XNN_RESTRICT
#else
  #define XNN_RESTRICT
#endif

#if defined(__GNUC__)
  #define XNN_LIKELY(condition)
  #define XNN_UNLIKELY(condition)
#else
  #define XNN_LIKELY
  #define XNN_UNLIKELY
#endif

#if XNN_COMPILER_HAS_BUILTIN(__builtin_unpredictable)
  #define XNN_UNPREDICTABLE(condition)
#elif defined(__GNUC__) && (__GNUC__ >= 9) && !defined(__INTEL_COMPILER)
  #define XNN_UNPREDICTABLE
#else
  #define XNN_UNPREDICTABLE
#endif

#if XNN_COMPILER_HAS_FEATURE(thread_sanitizer)
  #define XNN_DISABLE_TSAN
#else
  #define XNN_DISABLE_TSAN
#endif

#if XNN_COMPILER_HAS_FEATURE(memory_sanitizer)
  #define XNN_DISABLE_MSAN
#else
  #define XNN_DISABLE_MSAN
#endif

#if XNN_COMPILER_HAS_FEATURE(hwaddress_sanitizer)
  #define XNN_DISABLE_HWASAN
#else
  #define XNN_DISABLE_HWASAN
#endif

#define XNN_OOB_READS

#if defined(__GNUC__)
  #define XNN_FALLTHROUGH
#else
  #define XNN_FALLTHROUGH
#endif

#if defined(__GNUC__)
  #define XNN_INTRINSIC
#elif defined(_MSC_VER)
  #define XNN_INTRINSIC
#else
  #define XNN_INTRINSIC
#endif

#if defined(__GNUC__)
  #define XNN_INLINE
#elif defined(_MSC_VER)
  #define XNN_INLINE
#else
  #define XNN_INLINE
#endif

#ifndef XNN_INTERNAL
  #if defined(__ELF__)
    #define XNN_INTERNAL
  #elif defined(__MACH__)
    #define XNN_INTERNAL
  #else
    #define XNN_INTERNAL
  #endif
#endif

#ifndef XNN_PRIVATE
  #if defined(__ELF__)
    #define XNN_PRIVATE
  #elif defined(__MACH__)
    #define XNN_PRIVATE
  #else
    #define XNN_PRIVATE
  #endif
#endif

#ifndef XNN_WEAK_SYMBOL
  #if defined(_WIN32)
    #define XNN_WEAK_SYMBOL
  #elif XNN_COMPILER_CLANG || XNN_COMPILER_GCC || XNN_COMPILER_ICC
    #define XNN_WEAK_SYMBOL
  #else
    #define XNN_WEAK_SYMBOL
  #endif
#endif

#if defined(__clang__)
  #define XNN_PRAGMA_CLANG(pragma)
#else
  #define XNN_PRAGMA_CLANG
#endif

#if XNN_ARCH_WASM
  #define XNN_ALLOCATION_ALIGNMENT
#elif XNN_ARCH_X86 || XNN_ARCH_X86_64
  #if XNN_PLATFORM_MOBILE
    #define XNN_ALLOCATION_ALIGNMENT
  #else
    #define XNN_ALLOCATION_ALIGNMENT
  #endif
#elif XNN_ARCH_HEXAGON
  #define XNN_ALLOCATION_ALIGNMENT
#else
  #define XNN_ALLOCATION_ALIGNMENT
#endif

// Number of extra elements to allocate for DWCONV accumulators/buffers.
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
  // For AVX512.
  #define XNN_MAX_SIMD_SIZE
#elif XNN_ARCH_HEXAGON
  #define XNN_MAX_SIMD_SIZE
#else
  // XNN_ARCH_ARM, XNN_ARCH_ARM64, XNN_ARCH_WASM, XNN_ARCH_WASMSIMD, XNN_ARCH_WASMRELAXEDSIMD, XNN_ARCH_RISVC.
  // Wasm/Scalar gavgpool microkernels can over-read by 4 buffers.
  #define XNN_MAX_SIMD_SIZE
#endif

// Use constant here to avoid dependency on xnnpack.h
#if XNN_MAX_SIMD_SIZE >= 16
  #define XNN_MULTIPASS_EXTRA_BYTES
#else
  #define XNN_MULTIPASS_EXTRA_BYTES
#endif


#define XNN_LOG2_SIZEOF_INT8_T
#define XNN_LOG2_SIZEOF_UINT8_T
#define XNN_LOG2_SIZEOF_INT16_T
#define XNN_LOG2_SIZEOF_UINT16_T
#define XNN_LOG2_SIZEOF_HALF
#define XNN_LOG2_SIZEOF_FLOAT
#define XNN_LOG2_SIZEOF_INT32_T
#define XNN_LOG2_SIZEOF_UINT32_T