Elffuss BraidHub

kikocis/ncnn public

master · 8.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#ifndef NCNN_PLATFORM_H
16#define NCNN_PLATFORM_H
17
18#cmakedefine01 NCNN_STDIO
19#cmakedefine01 NCNN_STRING
20#cmakedefine01 NCNN_SIMPLEOCV
21#cmakedefine01 NCNN_SIMPLEOMP
22#cmakedefine01 NCNN_SIMPLESTL
23#cmakedefine01 NCNN_SIMPLEMATH
24#cmakedefine01 NCNN_THREADS
25#cmakedefine01 NCNN_BENCHMARK
26#cmakedefine01 NCNN_C_API
27#cmakedefine01 NCNN_PLATFORM_API
28#cmakedefine01 NCNN_PIXEL
29#cmakedefine01 NCNN_PIXEL_ROTATE
30#cmakedefine01 NCNN_PIXEL_AFFINE
31#cmakedefine01 NCNN_PIXEL_DRAWING
32#cmakedefine01 NCNN_VULKAN
33#cmakedefine01 NCNN_SIMPLEVK
34#cmakedefine01 NCNN_SYSTEM_GLSLANG
35#cmakedefine01 NCNN_RUNTIME_CPU
36#cmakedefine01 NCNN_GNU_INLINE_ASM
37#cmakedefine01 NCNN_AVX
38#cmakedefine01 NCNN_XOP
39#cmakedefine01 NCNN_FMA
40#cmakedefine01 NCNN_F16C
41#cmakedefine01 NCNN_AVX2
42#cmakedefine01 NCNN_AVXVNNI
43#cmakedefine01 NCNN_AVX512
44#cmakedefine01 NCNN_AVX512VNNI
45#cmakedefine01 NCNN_AVX512BF16
46#cmakedefine01 NCNN_AVX512FP16
47#cmakedefine01 NCNN_VFPV4
48#cmakedefine01 NCNN_ARM82
49#cmakedefine01 NCNN_ARM82DOT
50#cmakedefine01 NCNN_ARM82FP16FML
51#cmakedefine01 NCNN_ARM84BF16
52#cmakedefine01 NCNN_ARM84I8MM
53#cmakedefine01 NCNN_ARM86SVE
54#cmakedefine01 NCNN_ARM86SVE2
55#cmakedefine01 NCNN_ARM86SVEBF16
56#cmakedefine01 NCNN_ARM86SVEI8MM
57#cmakedefine01 NCNN_ARM86SVEF32MM
58#cmakedefine01 NCNN_MSA
59#cmakedefine01 NCNN_LSX
60#cmakedefine01 NCNN_MMI
61#cmakedefine01 NCNN_RVV
62#cmakedefine01 NCNN_INT8
63#cmakedefine01 NCNN_BF16
64#cmakedefine01 NCNN_FORCE_INLINE
65
66#cmakedefine NCNN_VERSION_STRING "@NCNN_VERSION_STRING@"
67
68#include "ncnn_export.h"
69
70#ifdef __cplusplus
71
72#if NCNN_THREADS
73#if defined _WIN32
74#define WIN32_LEAN_AND_MEAN
75#include <windows.h>
76#include <process.h>
77#else
78#include <pthread.h>
79#endif
80#endif // NCNN_THREADS
81
82#if __ANDROID_API__ >= 26
83#define VK_USE_PLATFORM_ANDROID_KHR
84#endif // __ANDROID_API__ >= 26
85
86namespace ncnn {
87
88#if NCNN_THREADS
89#if defined _WIN32
90class NCNN_EXPORT Mutex
91{
92public:
93 Mutex() { InitializeSRWLock(&srwlock); }
94 ~Mutex() {}
95 void lock() { AcquireSRWLockExclusive(&srwlock); }
96 void unlock() { ReleaseSRWLockExclusive(&srwlock); }
97private:
98 friend class ConditionVariable;
99 // NOTE SRWLock is available from windows vista
100 SRWLOCK srwlock;
101};
102
103class NCNN_EXPORT ConditionVariable
104{
105public:
106 ConditionVariable() { InitializeConditionVariable(&condvar); }
107 ~ConditionVariable() {}
108 void wait(Mutex& mutex) { SleepConditionVariableSRW(&condvar, &mutex.srwlock, INFINITE, 0); }
109 void broadcast() { WakeAllConditionVariable(&condvar); }
110 void signal() { WakeConditionVariable(&condvar); }
111private:
112 CONDITION_VARIABLE condvar;
113};
114
115static unsigned __stdcall start_wrapper(void* args);
116class NCNN_EXPORT Thread
117{
118public:
119 Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); }
120 ~Thread() {}
121 void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); }
122private:
123 friend unsigned __stdcall start_wrapper(void* args)
124 {
125 Thread* t = (Thread*)args;
126 t->_start(t->_args);
127 return 0;
128 }
129 HANDLE handle;
130 void* (*_start)(void*);
131 void* _args;
132};
133
134class NCNN_EXPORT ThreadLocalStorage
135{
136public:
137 ThreadLocalStorage() { key = TlsAlloc(); }
138 ~ThreadLocalStorage() { TlsFree(key); }
139 void set(void* value) { TlsSetValue(key, (LPVOID)value); }
140 void* get() { return (void*)TlsGetValue(key); }
141private:
142 DWORD key;
143};
144#else // defined _WIN32
145class NCNN_EXPORT Mutex
146{
147public:
148 Mutex() { pthread_mutex_init(&mutex, 0); }
149 ~Mutex() { pthread_mutex_destroy(&mutex); }
150 void lock() { pthread_mutex_lock(&mutex); }
151 void unlock() { pthread_mutex_unlock(&mutex); }
152private:
153 friend class ConditionVariable;
154 pthread_mutex_t mutex;
155};
156
157class NCNN_EXPORT ConditionVariable
158{
159public:
160 ConditionVariable() { pthread_cond_init(&cond, 0); }
161 ~ConditionVariable() { pthread_cond_destroy(&cond); }
162 void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); }
163 void broadcast() { pthread_cond_broadcast(&cond); }
164 void signal() { pthread_cond_signal(&cond); }
165private:
166 pthread_cond_t cond;
167};
168
169class NCNN_EXPORT Thread
170{
171public:
172 Thread(void* (*start)(void*), void* args = 0) { pthread_create(&t, 0, start, args); }
173 ~Thread() {}
174 void join() { pthread_join(t, 0); }
175private:
176 pthread_t t;
177};
178
179class NCNN_EXPORT ThreadLocalStorage
180{
181public:
182 ThreadLocalStorage() { pthread_key_create(&key, 0); }
183 ~ThreadLocalStorage() { pthread_key_delete(key); }
184 void set(void* value) { pthread_setspecific(key, value); }
185 void* get() { return pthread_getspecific(key); }
186private:
187 pthread_key_t key;
188};
189#endif // defined _WIN32
190#else // NCNN_THREADS
191class NCNN_EXPORT Mutex
192{
193public:
194 Mutex() {}
195 ~Mutex() {}
196 void lock() {}
197 void unlock() {}
198};
199
200class NCNN_EXPORT ConditionVariable
201{
202public:
203 ConditionVariable() {}
204 ~ConditionVariable() {}
205 void wait(Mutex& /*mutex*/) {}
206 void broadcast() {}
207 void signal() {}
208};
209
210class NCNN_EXPORT Thread
211{
212public:
213 Thread(void* (*/*start*/)(void*), void* /*args*/ = 0) {}
214 ~Thread() {}
215 void join() {}
216};
217
218class NCNN_EXPORT ThreadLocalStorage
219{
220public:
221 ThreadLocalStorage() { data = 0; }
222 ~ThreadLocalStorage() {}
223 void set(void* value) { data = value; }
224 void* get() { return data; }
225private:
226 void* data;
227};
228#endif // NCNN_THREADS
229
230class NCNN_EXPORT MutexLockGuard
231{
232public:
233 MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); }
234 ~MutexLockGuard() { mutex.unlock(); }
235private:
236 Mutex& mutex;
237};
238
239static inline void swap_endianness_16(void* x)
240{
241 unsigned char* xx = (unsigned char*)x;
242 unsigned char x0 = xx[0];
243 unsigned char x1 = xx[1];
244 xx[0] = x1;
245 xx[1] = x0;
246}
247
248static inline void swap_endianness_32(void* x)
249{
250 unsigned char* xx = (unsigned char*)x;
251 unsigned char x0 = xx[0];
252 unsigned char x1 = xx[1];
253 unsigned char x2 = xx[2];
254 unsigned char x3 = xx[3];
255 xx[0] = x3;
256 xx[1] = x2;
257 xx[2] = x1;
258 xx[3] = x0;
259}
260
261} // namespace ncnn
262
263#if NCNN_SIMPLESTL
264#include "simplestl.h"
265#else
266#include <algorithm>
267#include <list>
268#include <vector>
269#include <string>
270#endif
271
272// simplemath
273#if NCNN_SIMPLEMATH
274#include "simplemath.h"
275#else
276#include <math.h>
277#include <fenv.h>
278#endif
279
280#if NCNN_VULKAN
281#if NCNN_SIMPLEVK
282#include "simplevk.h"
283#else
284#include <vulkan/vulkan.h>
285#endif
286#include "vulkan_header_fix.h"
287#endif // NCNN_VULKAN
288
289#endif // __cplusplus
290
291#if NCNN_STDIO
292#if NCNN_PLATFORM_API && __ANDROID_API__ >= 8
293#include <android/log.h>
294#define NCNN_LOGE(...) do { \
295 fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); \
296 __android_log_print(ANDROID_LOG_WARN, "ncnn", ##__VA_ARGS__); } while(0)
297#else // NCNN_PLATFORM_API && __ANDROID_API__ >= 8
298#include <stdio.h>
299#define NCNN_LOGE(...) do { \
300 fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); } while(0)
301#endif // NCNN_PLATFORM_API && __ANDROID_API__ >= 8
302#else
303#define NCNN_LOGE(...)
304#endif
305
306
307#if NCNN_FORCE_INLINE
308#ifdef _MSC_VER
309 #define NCNN_FORCEINLINE __forceinline
310#elif defined(__GNUC__)
311 #define NCNN_FORCEINLINE inline __attribute__((__always_inline__))
312#elif defined(__CLANG__)
313 #if __has_attribute(__always_inline__)
314 #define NCNN_FORCEINLINE inline __attribute__((__always_inline__))
315 #else
316 #define NCNN_FORCEINLINE inline
317 #endif
318#else
319 #define NCNN_FORCEINLINE inline
320#endif
321#else
322 #define NCNN_FORCEINLINE inline
323#endif
324
325#endif // NCNN_PLATFORM_H