-
Notifications
You must be signed in to change notification settings - Fork 0
/
cenv.h
433 lines (371 loc) · 12.2 KB
/
cenv.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* @file cenv.h
* @author Jens Munk Hansen <jens.munk.hansen@gmail.com>
* @date Sat Nov 29 17:48:45 2014
*
* @brief Environment macros introduced for portability
* This file must be kept C compliant
*
*
*/
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef _MSC_VER
/* Microsoft compiler does not understand __restrict */
# define __restrict
#endif
#ifdef __cplusplus
# include <cstdarg>
#else
# include <stdarg.h>
#endif
// TODO: Add other OS'es
#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
# define SPS_OS_WIN
#endif
#ifdef __GNUC__
# include <features.h>
#endif
/*
MSVC++ 15.0 _MSC_VER == 2000 (Visual Studio 2017)
MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio 2003)
MSVC++ 7.0 _MSC_VER == 1300
MSVC++ 6.0 _MSC_VER == 1200
MSVC++ 5.0 _MSC_VER == 1100
*/
#if defined(__STDC__)
# define C89 /* ANSI X3.159-1989 */
# if defined(__STDC_VERSION__)
# define C90 /* ISO/IEC 9899:1990 */
# if (__STDC_VERSION__ >= 199409L)
# define C94 /* ISO/IEC 9899-1:1994 */
# endif
# if (__STDC_VERSION__ >= 199901L)
# define C99 /* ISO/IEC 9899:1999 */
# endif
# if (__STDC_VERSION__ >= 201112L)
# define C11 /* ISO/IEC 9899:2011 */
# endif
# elif defined(__cplusplus)
# ifdef __USE_ISOC99
# define C99
# endif
# endif
#endif
#ifdef C99
# define SPS_FCOMPLEX float _Complex
#elif defined(_MSC_VER)
# define _CRT_USE_C_COMPLEX_H 1
# define SPS_FCOMPLEX _Fcomplex
#endif
#if defined(__cplusplus)
# if (__cplusplus >= 201103L)
# define CXX11 11
# endif
# if defined(_MSC_VER) && (_MSC_VER >= 1900)
# define CXX11 11
# endif
# if defined(_MSC_VER) && (_MSC_VER <= 1900)
# define SPS_NOEXCEPT
# define SPS_ALIGNAS(x) __declspec(align(32))
# else
# define SPS_NOEXCEPT noexcept
# define SPS_ALIGNAS(x) alignas(x)
# endif
# if CXX11
// Deprecated in C++11.
# define SPS_REGISTER
# define SPS_OVERRIDE override
# else
# define SPS_REGISTER #register
# define SPS_OVERRIDE
# endif
// TODO: Do this correctly for Microsucks
# if (__cplusplus >= 201402L) || (_MSC_VER >= 1900)
# define CXX14 14
# endif
# if (__cplusplus >= 201703L)
# define CXX17 17
# endif
#endif
/*
C++98 __cplusplus = 199711L ISO/IEC 14882:1998
C++11 __cplusplus = 201103L ISO/IEC 14882:2011
C++14 __cplusplus = 201402L ISO/IEC 14882:2014
C++/CLI __cplusplus_cli = 200406L ECMA-372
DSP-C ISO/IEC JTC1/SC22 WG14/N854
EC++ __embedded_cplusplus Embedded C++
*/
// Alignment, TODO: Use alignas if present
/*
TODO: Consider using a construction like
template <class T>
struct __attribute__((aligned(4*sizeof(T)))) Data
{
T a;
};
*/
#if (defined(_MSC_VER) && defined(_WIN32))
# define ALIGN16_BEGIN __declspec(align(16))
# define ALIGN16_END
# define ALIGN32_BEGIN __declspec(align(32))
# define ALIGN32_END
#elif defined(__GNUC__)
# define ALIGN16_BEGIN
# define ALIGN16_END __attribute__((aligned(16)))
# define ALIGN32_BEGIN
# define ALIGN32_END __attribute__((aligned(32)))
#endif
// Static inlines
#if (defined(_MSC_VER) && defined(_WIN32))
// Note when used inside a namespace, the static is superfluous
# define STATIC_INLINE_BEGIN static inline
# define STATIC_INLINE_END
#elif (defined(__GNUC__))
# define STATIC_INLINE_BEGIN static inline
# if defined(__CYGWIN__)
# define STATIC_INLINE_END
# else
# define STATIC_INLINE_END /* Work on this */ //__attribute__ ((always_inline))
# endif
#endif
#ifdef __GNUG__
# define SPS_LIKELY(x) __builtin_expect((x),1)
# define SPS_UNLIKELY(x) __builtin_expect((x),0)
# define SPS_ASS_ALIGNED(x,y) __builtin_assume_aligned((x),(y))
#else
# define SPS_LIKELY(x) (x)
# define SPS_UNLIKELY(x) (x)
# define SPS_ASS_ALIGNED(x,y) (x)
#endif
#ifdef __GNUG__
# define SPS_RELAXED_MEMSET_BEGIN \
_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"")
# define SPS_RELAXED_MEMSET_END \
_Pragma("GCC diagnostic pop")
#elif defined(__clang__)
# define SPS_RELAXED_MEMSET_BEGIN \
#pragma clang diagnostic push \
#pragma clang diagnostic ignored "-Wclass-memaccess"
# define SPS_RELAXED_MEMSET_END \
#pragma clang diagnostic pop
#else
# define SPS_RELAXED_MEMSET_BEGIN
# define SPS_RELAXED_MEMSET_END
#endif
#define __BEGIN__ {
#define __END__ goto exit; exit: ; }
// Thread-local storage
#if defined(_WIN32) && defined(_MSC_VER)
# define __THREAD __declspec(thread)
#elif defined(__clang__)
# define __THREAD thread_local
#elif defined(__GNUC__)
# define __THREAD __thread
#endif
#ifdef __GNUG__
# define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#endif
// TODO: Make file with helper macros
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
/* count arguments */
#define NUMARGS(...) \
SELECT_5TH(__VA_ARGS__, 4, 3, 2, 1, 0, throwaway, throwaway)
#define SELECT_5TH(a1,a2,a3,a4,a5, ...) a5
/* expands to the first arguments */
#define RETURN_FIRST(...) RETURN_FIRST_HELPER(__VA_ARGS__, throwaway)
#define RETURN_FIRST_HELPER(first, ...) return first;
#define FIRST(...) FIRST_HELPER(__VA_ARGS__, throwaway)
#define FIRST_HELPER(first, ...) first
/*
* if there's only one argument, expands to nothing. if there is more
* than one argument, expands to a comma followed by everything but
* the first argument. only supports up to 9 arguments but can be
* trivially expanded.
*/
#define REST(...) REST_HELPER(NNUM(__VA_ARGS__), __VA_ARGS__)
#define REST_HELPER(qty, ...) REST_HELPER2(qty, __VA_ARGS__)
#define REST_HELPER2(qty, ...) REST_HELPER_##qty(__VA_ARGS__)
#define REST_HELPER_ONE(first)
#define REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__
#define NNUM(...) \
SELECT_10TH(__VA_ARGS__, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway)
#define SELECT_10TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, ...) a10
#ifdef _WIN32
// Does not respect C99
# define EXPAND(x) x
# define FOR_EACH_1(what, x, ...) what(x)
# define FOR_EACH_2(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_1(what, __VA_ARGS__))
# define FOR_EACH_3(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_2(what, __VA_ARGS__))
# define FOR_EACH_4(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_3(what, __VA_ARGS__))
# define FOR_EACH_5(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_4(what, __VA_ARGS__))
# define FOR_EACH_6(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_5(what, __VA_ARGS__))
# define FOR_EACH_7(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_6(what, __VA_ARGS__))
# define FOR_EACH_8(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_7(what, __VA_ARGS__))
# define FOR_EACH_9(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_8(what, __VA_ARGS__))
# define FOR_EACH_10(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_9(what, __VA_ARGS__))
# define FOR_EACH_11(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_10(what, __VA_ARGS__))
# define FOR_EACH_12(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_11(what, __VA_ARGS__))
# define FOR_EACH_13(what, x, ...)\
what(x);\
EXPAND(FOR_EACH_12(what, __VA_ARGS__))
# define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
# define FOR_EACH_NARG_(...) EXPAND(FOR_EACH_ARG_N(__VA_ARGS__))
# define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
# define FOR_EACH_RSEQ_N() 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
# define CONCATENATE(x,y) x##y
# define FOR_EACH_(N, what, ...) EXPAND(CONCATENATE(FOR_EACH_, N)(what, __VA_ARGS__))
# define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
#else
// Respects ISO C99
# define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
# define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
# define CONCATENATE2(arg1, arg2) arg1##arg2
# define FOR_EACH_1(what, x) \
what(x)
# define FOR_EACH_2(what, x, ...) \
what(x); \
FOR_EACH_1(what, __VA_ARGS__);
# define FOR_EACH_3(what, x, ...) \
what(x); \
FOR_EACH_2(what, __VA_ARGS__);
# define FOR_EACH_4(what, x, ...) \
what(x); \
FOR_EACH_3(what, __VA_ARGS__);
# define FOR_EACH_5(what, x, ...) \
what(x); \
FOR_EACH_4(what, __VA_ARGS__);
# define FOR_EACH_6(what, x, ...) \
what(x); \
FOR_EACH_5(what, __VA_ARGS__);
# define FOR_EACH_7(what, x, ...) \
what(x); \
FOR_EACH_6(what, __VA_ARGS__);
# define FOR_EACH_8(what, x, ...) \
what(x); \
FOR_EACH_7(what, __VA_ARGS__);
# define FOR_EACH_9(what, x, ...) \
what(x); \
FOR_EACH_8(what, __VA_ARGS__);
# define FOR_EACH_10(what, x, ...) \
what(x); \
FOR_EACH_9(what, __VA_ARGS__);
# define FOR_EACH_11(what, x, ...) \
what(x); \
FOR_EACH_10(what, __VA_ARGS__);
# define FOR_EACH_12(what, x, ...) \
what(x); \
FOR_EACH_11(what, __VA_ARGS__);
# define FOR_EACH_13(what, x, ...) \
what(x); \
FOR_EACH_12(what, __VA_ARGS__);
# define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
# define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
# define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
# define FOR_EACH_RSEQ_N() 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
# define FOR_EACH_(N, what, ...) CONCATENATE(FOR_EACH_, N)(what, __VA_ARGS__)
# define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
#endif
#ifndef SPS_UNREFERENCED_PARAMETER
# define SPS_UNREFERENCED_PARAMETER(x) ((void)(x))
#endif
#ifndef SPS_UNREFERENCED_PARAMETERS
# define SPS_UNREFERENCED_PARAMETERS(...) FOR_EACH( SPS_UNREFERENCED_PARAMETER, __VA_ARGS__)
#endif
#ifdef _MSC_VER
# ifndef SPS_ATTR_DESTRUCTOR
# define SPS_ATTR_DESTRUCTOR
# endif
#else
# ifndef SPS_ATTR_DESTRUCTOR
# define SPS_ATTR_DESTRUCTOR __attribute__((destructor(101)))
# endif
#endif
#if CXX14
# ifndef SPS_DEPRECATED
# define SPS_DEPRECATED(since) [[deprecated("Since " #since)]]
# define SPS_DEPRECATED_FOR(since, replacement) \
[[deprecated("Since " #since "; use " #replacement)]]
# endif
# define SPS_ALIAS_TEMPLATE_FUNCTION(highLevelF, lowLevelF) \
template<typename... Args> \
inline auto highLevelF(Args&&... args)->decltype(lowLevelF(std::forward<Args>(args)...)) { \
return lowLevelF(std::forward<Args>(args)...); \
}
# define SPS_ALIAS_FUNCTION(highLevelF, lowLevelF) \
const auto highLevelF = lowLevelF;
// Alternatively, you could say auto& highLevelF = lowLevelF;
#else
# ifdef __GNUC__
# ifndef SPS_DEPRECATED
# define SPS_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
# define SPS_DEPRECATED_FOR(since, replacement) \
__attribute__((__deprecated__("Since " #since "; use " #replacement)))
# endif
# elif defined(_MSC_VER)
# ifndef SPS_DEPRECATED
# if (_MSC_VER >= 1900)
# define SPS_DEPRECATED(since) __declspec(deprecated)
# define SPS_DEPRECATED_FOR(since, replacement) \
__declspec(deprecated)
# else
# define SPS_DEPRECATED(since) __declspec(deprecated("Since " # since))
# define SPS_DEPRECATED_FOR(since, replacement) \
__declspec(deprecated("Since " #since "; use " #replacement))
# endif
# endif
# endif
#endif
/* Local variables: */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */