nndeploy C++ API  0.2.0
nndeploy C++ API
macro.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_BASE_MACRO_H_
3 #define _NNDEPLOY_BASE_MACRO_H_
4 
6 
11 #if defined _WIN32 || defined __CYGWIN__
12 #ifdef ENABLE_NNDEPLOY_BUILDING_DLL
13 #ifdef __GNUC__
14 #define NNDEPLOY_CC_API __attribute__((dllexport))
15 #else // __GNUC__
16 #define NNDEPLOY_CC_API __declspec(dllexport)
17 #endif // __GNUC__
18 #else // NNDEPLOY_BUILDING_DLL
19 #ifdef __GNUC__
20 #define NNDEPLOY_CC_API __attribute__((dllimport))
21 #else
22 #define NNDEPLOY_CC_API __declspec(dllimport)
23 #endif // __GNUC__
24 #endif // NNDEPLOY_BUILDING_DLL
25 #else // _WIN32 || __CYGWIN__
26 #if __GNUC__ >= 4
27 #define NNDEPLOY_CC_API __attribute__((visibility("default")))
28 #else
29 #define NNDEPLOY_CC_API
30 #endif
31 #endif
32 
33 #ifdef __cplusplus
34 #define NNDEPLOY_C_API extern "C" NNDEPLOY_CC_API
35 #endif
36 
41 #ifdef _MSC_VER
42 #define NNDEPLOY_PRAGMA(X) __pragma(X)
43 #else
44 #define NNDEPLOY_PRAGMA(X) _Pragma(#X)
45 #endif
46 
51 #define NNDEPLOY_FORCE_LOAD_LIB_SYMBOL(TYPE) \
52  TYPE *forceLoad(TYPE *ptr) { \
53  if (ptr == nullptr) return ptr; \
54  return ptr; \
55  }
56 
61 #if defined(__GNUC__) || defined(__clang__)
62 #define NNDEPLOY_DEPRECATED(msg) __attribute__((deprecated(msg)))
63 #elif defined(_MSC_VER)
64 #define NNDEPLOY_DEPRECATED(msg) __declspec(deprecated(msg))
65 #else
66 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
67 #define NNDEPLOY_DEPRECATED
68 #endif
69 
74 #define NNDEPLOY_DEFAULT_STR "nndeploy_default_str"
75 #define NNDEPLOY_TO_STR(x) #x
76 #define NNDEPLOY_NAMESPACE_PLUS_TO_STR(x) nndeploy_namespace##x
77 #define NNDEPLOY_GENERATE_DEFAULT_STR() \
78  std::string file = __FILE__; \
79  std::string function = __FUNCTION__; \
80  std::string line = std::to_string(__LINE__); \
81  return NNDEPLOY_DEFAULT_STR + "_" file + "_" + function + "_" + line;
82 
87 #ifndef NNDEPLOY_UP_DIV
88 #define NNDEPLOY_UP_DIV(x, y) \
89  ((static_cast<int>(x) + static_cast<int>(y) - (1)) / static_cast<int>(y))
90 #endif
91 #ifndef NNDEPLOY_ROUND_UP
92 #define NNDEPLOY_ROUND_UP(x, y) \
93  ((static_cast<int>(x) + static_cast<int>(y) - (1)) / static_cast<int>(y) * \
94  static_cast<int>(y))
95 #endif
96 #ifndef NNDEPLOY_ALIGN_UP4
97 #define NNDEPLOY_ALIGN_UP4(x) NNDEPLOY_ROUND_UP((x), 4)
98 #endif
99 #ifndef NNDEPLOY_ALIGN_UP8
100 #define NNDEPLOY_ALIGN_UP8(x) NNDEPLOY_ROUND_UP((x), 8)
101 #endif
102 #ifndef NNDEPLOY_ALIGN_PTR
103 #define NNDEPLOY_ALIGN_PTR(x, y) \
104  (void *)(x & ~static_cast<size_t>(NNDEPLOY_ABS(y) - 1))
105 #endif
106 #ifndef NNDEPLOY_MIN
107 #define NNDEPLOY_MIN(x, y) ((x) < (y) ? (x) : (y))
108 #endif
109 #ifndef NNDEPLOY_MAX
110 #define NNDEPLOY_MAX(x, y) ((x) > (y) ? (x) : (y))
111 #endif
112 #ifndef NNDEPLOY_ABS
113 #define NNDEPLOY_ABS(x) ((x) > (0) ? (x) : (-(x)))
114 #endif
115 
116 #ifdef NNDEPLOY_XADD
117 // allow to use user-defined macro
118 #elif defined __GNUC__ || defined __clang__
119 #if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && \
120  !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && \
121  !defined __INTEL_COMPILER
122 #ifdef __ATOMIC_ACQ_REL
123 #define NNDEPLOY_XADD(addr, delta) \
124  __c11_atomic_fetch_add((_Atomic(int) *)(addr), delta, __ATOMIC_ACQ_REL)
125 #else
126 #define NNDEPLOY_XADD(addr, delta) \
127  __atomic_fetch_add((_Atomic(int) *)(addr), delta, 4)
128 #endif
129 #else
130 #if defined __ATOMIC_ACQ_REL && !defined __clang__
131 // version for gcc >= 4.7
132 #define NNDEPLOY_XADD(addr, delta) \
133  (int)__atomic_fetch_add((unsigned *)(addr), (unsigned)(delta), \
134  __ATOMIC_ACQ_REL)
135 #else
136 #define NNDEPLOY_XADD(addr, delta) \
137  (int)__sync_fetch_and_add((unsigned *)(addr), (unsigned)(delta))
138 #endif
139 #endif
140 #elif defined _MSC_VER && !defined RC_INVOKED
141 #include <intrin.h>
142 #define NNDEPLOY_XADD(addr, delta) \
143  (int)_InterlockedExchangeAdd((long volatile *)addr, delta)
144 #else
145 #ifdef NNDEPLOY_FORCE_UNSAFE_XADD
146 static inline int NNDEPLOY_XADD(int *addr, int delta) {
147  int tmp = *addr;
148  *addr += delta;
149  return tmp;
150 }
151 #else
152 #error \
153  "NNDEPLOY: can't define safe NNDEPLOY_XADD macro for current platform (unsupported). Define NNDEPLOY_XADD macro through custom port header"
154 #endif
155 #endif
156 
157 #define NNDEPLOY_OS_LINUX 0
158 #define NNDEPLOY_OS_ANDROID 0
159 #define NNDEPLOY_OS_DARWIN 0
160 #define NNDEPLOY_OS_IOS 0
161 #define NNDEPLOY_OS_WINDOWS 0
162 #define NNDEPLOY_OS_UNIX 0
163 #define NNDEPLOY_OS_UNKNOWN 0
164 
165 #if (defined __linux)
166 #undef NNDEPLOY_OS_LINUX
167 #define NNDEPLOY_OS_LINUX 1
168 #elif (defined __linux__)
169 #undef NNDEPLOY_OS_LINUX
170 #define NNDEPLOY_OS_LINUX 1
171 #else
172 #endif
173 
174 #if (defined __android__)
175 #undef NNDEPLOY_OS_ANDROID
176 #define NNDEPLOY_OS_ANDROID 1
177 #endif
178 
179 #if (defined __APPLE__)
180 #include <TargetConditionals.h>
181 #if defined(TARGET_OS_MAC)
182 #undef NNDEPLOY_OS_DARWIN
183 #define NNDEPLOY_OS_DARWIN 1
184 #elif defined(TARGET_OS_IPHONE)
185 #undef NNDEPLOY_OS_IOS
186 #define NNDEPLOY_OS_IOS 1
187 #endif
188 #endif
189 
190 #ifdef _WIN32
191 #undef NNDEPLOY_OS_WINDOWS
192 #define NNDEPLOY_OS_WINDOWS 1
193 #endif
194 
195 #ifdef __CYGWIN__
196 #undef NNDEPLOY_OS_WINDOWS
197 #define NNDEPLOY_OS_WINDOWS 1
198 #endif
199 
200 #if (1 != NNDEPLOY_OS_LINUX + NNDEPLOY_OS_ANDROID + NNDEPLOY_OS_DARWIN + \
201  NNDEPLOY_OS_IOS + NNDEPLOY_OS_WINDOWS)
202 #define NNDEPLOY_OS_UNKNOWN 1
203 #endif
204 
205 #if NNDEPLOY_OS_LINUX || NNDEPLOY_OS_ANDROID || NNDEPLOY_OS_DARWIN || \
206  NNDEPLOY_OS_IOS
207 #undef NNDEPLOY_OS_UNIX
208 #define NNDEPLOY_OS_UNIX 1
209 #endif
210 
211 // ARCHITECTURE
212 #define NNDEPLOY_ARCHITECTURE_X86 0
213 #define NNDEPLOY_ARCHITECTURE_ARM 0
214 #define NNDEPLOY_ARCHITECTURE_CPU 1
215 
216 #if (defined ENABLE_NNDEPLOY_DEVICE_X86)
217 #undef NNDEPLOY_ARCHITECTURE_X86
218 #define NNDEPLOY_ARCHITECTURE_X86 1
219 #endif
220 
221 #if (defined ENABLE_NNDEPLOY_DEVICE_ARM)
222 #undef NNDEPLOY_ARCHITECTURE_ARM
223 #define NNDEPLOY_ARCHITECTURE_ARM 1
224 #endif
225 
226 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
227 #define NNDEPLOY_LIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 1))
228 #define NNDEPLOY_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0))
229 #else
230 #define NNDEPLOY_LIKELY(expr) (expr)
231 #define NNDEPLOY_UNLIKELY(expr) (expr)
232 #endif
233 
234 #endif // _NNDEPLOY_BASE_MACRO_H_