-
Notifications
You must be signed in to change notification settings - Fork 4
/
harbol_common_defines.h
410 lines (366 loc) · 10.1 KB
/
harbol_common_defines.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#ifndef HARBOL_COMMON_DEFINES_INCLUDED
# define HARBOL_COMMON_DEFINES_INCLUDED
/** Check if Windows */
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
# ifndef OS_WINDOWS
# define OS_WINDOWS 1
# endif
/** Check if Linux/UNIX & FreeBSD */
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__) || defined(linux) || defined(__linux) || defined(__freeBSD__)
# ifndef OS_LINUX_UNIX
# define OS_LINUX_UNIX 1
# endif
/** Check if Android */
#elif defined(__ANDROID__)
# ifndef OS_ANDROID
# define OS_ANDROID 1
# endif
/** Check if Solaris/SunOS */
#elif defined(sun) || defined(__sun)
# if defined(__SVR4) || defined(__svr4__)
# ifndef OS_SOLARIS
# define OS_SOLARIS 1
# endif
# else
# ifndef OS_SUNOS
# define OS_SUNOS 1
# endif
# endif
/** Check if Macintosh/MacOS/iOS */
#elif defined(macintosh) || defined(Macintosh) || defined(__APPLE__)
# include "TargetConditionals.h"
# if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
# ifndef OS_IPHONE_SIM
# define OS_IPHONE_SIM 1
# endif
# ifndef OS_IPHONE
# define OS_IPHONE 1
# endif
# elif TARGET_OS_IPHONE
# ifndef OS_IPHONE
# define OS_IPHONE 1
# endif
# else
# ifndef OS_MAC
# define OS_MAC 1
# endif
# endif
#endif /** end OS checks */
/** check what compiler we got */
#if defined(__clang__)
# ifndef COMPILER_CLANG
# define COMPILER_CLANG
# endif
#elif defined(__GNUC__) || defined(__GNUG__)
# ifndef COMPILER_GCC
# define COMPILER_GCC
# endif
#elif defined(_MSC_VER)
# ifndef COMPILER_MSVC
# define COMPILER_MSVC
# endif
#elif defined(__INTEL_COMPILER)
# ifndef COMPILER_INTEL
# define COMPILER_INTEL
# endif
#endif /** end compiler check macros */
/** check arch platform. */
#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(_M_AMD64)
# ifndef PLATFORM_AMD64
# define PLATFORM_AMD64
# endif
# ifndef PLATFORM_x64
# define PLATFORM_x64
# endif
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__386)
# ifndef PLATFORM_IA32
# define PLATFORM_IA32
# endif
# ifndef PLATFORM_x86
# define PLATFORM_x86
# endif
#elif defined(__ia64__) || defined(_IA64) || defined(__IA64__) || defined(__ia64) || defined(_M_IA64) || defined(__itanium__)
# ifndef PLATFORM_IA64
# define PLATFORM_IA64
# endif
# ifndef PLATFORM_ITANIUM
# define PLATFORM_ITANIUM
# endif
#elif defined(__arm__) || defined(_ARM) || defined(_M_ARM) || defined(__arm)
# ifndef PLATFORM_ARM32
# define PLATFORM_ARM32
# endif
#elif defined(__aarch64__)
# ifndef PLATFORM_ARM64
# define PLATFORM_ARM64
# endif
#elif defined(__riscv) || defined(_riscv) || defined(__riscv__)
# ifndef PLATFORM_RISCV
# define PLATFORM_RISCV
# endif
# if defined(__riscv_xlen) && __riscv_xlen==32
# ifndef PLATFORM_RISCV32
# define PLATFORM_RISCV32
# endif
# elif defined(__riscv_xlen) && __riscv_xlen==64
# ifndef PLATFORM_RISCV64
# define PLATFORM_RISCV64
# endif
# endif
#endif /** end platform arch defines. */
/** set up the C standard macros! */
#ifdef __STDC__
# ifndef C89
# define C89
# endif
# ifdef __STDC_VERSION__
# ifndef C90
# define C90
# endif
# if (__STDC_VERSION__ >= 199409L)
# ifndef C94
# define C94
# endif
# endif
# if (__STDC_VERSION__ >= 199901L)
# ifndef C99
# define C99
# endif
# endif
# if (__STDC_VERSION__ >= 201112L)
# ifndef C11
# define C11
# endif
# endif
# if (__STDC_VERSION__ >= 201710L)
# ifndef C18
# define C18
# endif
# endif
# endif
#endif
#ifdef __cplusplus
# if( __cplusplus >= 199711L )
# ifndef CPP03
# define CPP03
# endif
# endif
# if( __cplusplus >= 201103L )
# ifndef CPP11
# define CPP11
# endif
# endif
# if( __cplusplus >= 201402L )
# ifndef CPP14
# define CPP14
# endif
# endif
# if( __cplusplus >= 201703L )
# ifndef CPP17
# define CPP17
# endif
# endif
# if( __cplusplus >= 202002L )
# ifndef CPP20
# define CPP20
# endif
# endif
#endif
/** setup RAII destructor macro if possibru to mark functions as cleaner-uppers. */
#ifndef RAII_DTOR
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define RAII_DTOR(func) __attribute__ ((cleanup((func))))
# else
# define RAII_DTOR(func)
# endif
#endif
/** setup macro to mark a param as "cannot or can never be NULL". */
#ifndef NEVER_NULL
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define NEVER_NULL(...) __attribute__( (nonnull(__VA_ARGS__)) )
# else
# define NEVER_NULL(...)
# endif
#endif
/** setup macro that declares all params to never be NULL. */
#ifndef NO_NULL
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define NO_NULL __attribute__((nonnull))
# else
# define NO_NULL
# endif
#endif
/** setup macro to mark a function as never returning a null pointer. */
#ifndef NONNULL_RET
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define NONNULL_RET __attribute__((returns_nonnull))
# else
# define NONNULL_RET
# endif
#endif
/** setup macro that marks a function as deprecated. */
#ifndef DEPRECATED
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define DEPRECATED(func) func __attribute__ ((deprecated))
# elif defined(COMPILER_MSVC)
# define DEPRECATED(func) __declspec(deprecated) func
# else
# define DEPRECATED(func)
# endif
#endif
/** setup macro that defines a var that it may be aliased by other data. */
#ifndef PTR_ALIAS
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define PTR_ALIAS __attribute__ ((__may_alias__))
# else
# define PTR_ALIAS
# endif
#endif
/** setup macro that marks a function as having hidden visibility. */
#ifndef VIS_HIDDEN
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define VIS_HIDDEN __attribute__ ((visibility ("hidden")))
# else
# define VIS_HIDDEN
# endif
#endif
/** setup macro that marks a function as having internal visibility. */
#ifndef VIS_INTERNAL
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define VIS_INTERNAL __attribute__ ((visibility ("internal")))
# else
# define VIS_INTERNAL
# endif
#endif
/** setup macro that marks a function as having protected visibility. */
#ifndef VIS_PROTECTED
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define VIS_PROTECTED __attribute__ ((visibility ("protected")))
# else
# define VIS_PROTECTED
# endif
#endif
/** setup macro that gives a warning if a function's result is unused. */
#ifndef WARN_UNUSED_RESULT
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
# else
# define WARN_UNUSED_RESULT
# endif
#endif
/** setup macro to mark a function or variable as unused and ignore unused warnings. */
#ifndef UNUSED
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define UNUSED __attribute__ ((unused))
# else
# define UNUSED
# endif
#endif
/** setup macro to mark a function as a hot spot, thus requiring aggressive optimizations. */
#ifndef HOT
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define HOT __attribute__ ((hot))
# else
# define HOT
# endif
#endif
/** setup macro to make vector types. Argument must be power of 2.
* Example:
typedef __attribute__ ((vector_size (32))) int int_vec32_t; which makes int_vec32_t as 32-bytes.
*
* All the basic integer types can be used as base types, both as signed and as unsigned: char, short, int, long, long long. In addition, float and double can be used to build floating-point vector types.
* +, -, *, /, unary minus, ^, |, &, ~, %. are the only operators allowed.
*
* Bigger Example:
typedef int v4si SIMD_VEC(16);
v4si a = {1,2,3,4};
v4si b = {3,2,1,4};
v4si c;
c = a > b; /// The result would be {0, 0,-1, 0}
c = a == b; /// The result would be {0,-1, 0,-1}
*/
#ifndef SIMD_VEC
# if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
# define SIMD_VEC(bytes) __attribute__ ((vector_size ((bytes))))
# else
# define SIMD_VEC(bytes)
# endif
#endif
#ifdef HARBOL_DLL
# ifndef HARBOL_LIB
# define HARBOL_EXPORT __declspec(dllimport)
# else
# define HARBOL_EXPORT __declspec(dllexport)
# endif
#else
# define HARBOL_EXPORT
#endif
#ifdef __cplusplus
# ifdef OS_WINDOWS
# ifndef restrict
# define restrict __restrict
# endif
# else
# ifndef restrict
# define restrict __restrict__
# endif
# endif
#endif
#ifdef COMPILER_MSVC
# ifndef inline
# define inline __inline
# endif
#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
# ifndef inline
# define inline __inline__
# endif
#endif
#ifdef C11
# ifndef IS_VAR_OF_TYPE
# define IS_VAR_OF_TYPE(n, T) _Generic((n), T:true, default:false)
# endif
# ifndef IS_SIGNED_INT_TYPE
# define IS_SIGNED_INT_TYPE(n) _Generic((n), _Bool: true, char: true, short: true, int:true, long: true, long long: true, default:false)
# endif
# ifndef IS_UNSIGNED_INT_TYPE
# define IS_UNSIGNED_INT_TYPE(n) _Generic((n), _Bool: true, unsigned char: true, unsigned short: true, unsigned int:true, unsigned long: true, unsigned long long: true, default:false)
# endif
# ifndef IS_FLOAT_TYPE
# define IS_FLOAT_TYPE(n) _Generic((n), float: true, double: true, long double: true, default:false)
# endif
# ifndef IS_NUMERIC_TYPE
# define IS_NUMERIC_TYPE(n) IS_FLOAT_TYPE(n) || IS_UNSIGNED_INT_TYPE(n) || IS_SIGNED_INT_TYPE(n)
# endif
# ifndef IS_SIGNED_TYPE
# define IS_SIGNED_TYPE(n) IS_FLOAT_TYPE(n) || IS_SIGNED_INT_TYPE(n)
# endif
#endif
#ifndef SIGN_EXTEND
# ifdef __cplusplus
template< typename T > static inline T _sign_extend(T val) {
size_t const sign_bit = 1 << ((sizeof val * CHAR_BIT) - 1);
return (val ^ sign_bit) - sign_bit;
}
# define SIGN_EXTEND _sign_extend
# else
# define SIGN_EXTEND(val) (( (val) ^ (1u << ((sizeof val * CHAR_BIT) - 1u)) ) - (1u << ((sizeof val * CHAR_BIT) - 1u)))
# endif
#endif
#ifndef TAIL_CALL
# if defined(COMPILER_GCC) || defined(COMPILER_CLANG)
# define TAIL_CALL __attribute__ ((musttail))
# else
# define TAIL_CALL
# endif
#endif
#ifndef REG_CALL
# if defined(PLATFORM_AMD64) && (defined(COMPILER_CLANG) || defined(COMPILER_INTEL))
# define REG_CALL __attribute__ ((regcall))
# else
# define REG_CALL
# endif
#endif
#ifndef HEADER_IMPL
# define HEADER_IMPL static inline
#endif
#endif /** HARBOL_COMMON_DEFINES_INCLUDED */