blob: 0fc701185e751700452fbbfa7576753a3fe48ca5 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// 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
12#if defined(__GNUC__)
13 #if defined(__clang__) || (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 5)
14 #define XNN_UNREACHABLE do { __builtin_unreachable(); } while (0)
15 #else
16 #define XNN_UNREACHABLE do { __builtin_trap(); } while (0)
17 #endif
18#elif defined(_MSC_VER)
19 #define XNN_UNREACHABLE __assume(0)
20#else
21 #define XNN_UNREACHABLE do { } while (0)
22#endif
23
24#define XNN_ALIGN(alignment) __attribute__((__aligned__(alignment)))
25
26#define XNN_COUNT_OF(array) (sizeof(array) / sizeof(0[array]))
27
28#if defined(__GNUC__)
29 #define XNN_LIKELY(condition) (__builtin_expect(!!(condition), 1))
30 #define XNN_UNLIKELY(condition) (__builtin_expect(!!(condition), 0))
31#else
32 #define XNN_LIKELY(condition) (!!(condition))
33 #define XNN_UNLIKELY(condition) (!!(condition))
34#endif
35
36// TODO - __builtin_expect_with_probability for GCC 9+
37#if defined(__clang__) && (__has_builtin(__builtin_unpredictable))
38 #define XNN_UNPREDICTABLE(condition) (__builtin_unpredictable(!!(condition)))
39#else
40 #define XNN_UNPREDICTABLE(condition) (!!(condition))
41#endif
42
43#if defined(__GNUC__)
44 #define XNN_INLINE inline __attribute__((__always_inline__))
45#else
46 #define XNN_INLINE inline
47#endif
48
49#ifndef XNN_INTERNAL
50 #if defined(__ELF__)
51 #define XNN_INTERNAL __attribute__((__visibility__("internal")))
52 #elif defined(__MACH__)
53 #define XNN_INTERNAL __attribute__((__visibility__("hidden")))
54 #else
55 #define XNN_INTERNAL
56 #endif
57#endif
58
59#ifndef XNN_PRIVATE
60 #if defined(__ELF__)
61 #define XNN_PRIVATE __attribute__((__visibility__("hidden")))
62 #elif defined(__MACH__)
63 #define XNN_PRIVATE __attribute__((__visibility__("hidden")))
64 #else
65 #define XNN_PRIVATE
66 #endif
67#endif