Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/csound-iOS/headers/sysdep.h

raw symbols SCIP
1 /*
2 sysdep.h:
3
4 Copyright (C) 1991 Barry Vercoe, John ffitch
5
6 This file is part of Csound.
7
8 The Csound Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 Csound is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with Csound; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 02110-1301 USA
22 */
23
24 #ifndef CSOUND_SYSDEP_H
25 #define CSOUND_SYSDEP_H
26
27 /* check for the presence of a modern compiler (for use of certain features) */
28 #if defined(WIN32)
29 #if !defined(locale_t)
30 typedef void *locale_t;
31 #endif
32 #endif
33
34 #include <limits.h>
35 /* this checks for 64BIT builds */
36 #if defined(__MACH__) || defined(LINUX)
37 #if ( __WORDSIZE == 64 ) || defined(__x86_64__) || defined(__amd64__)
38 #define B64BIT
39 #endif
40 #endif
41
42 #if defined(WIN32)
43 #if _WIN64
44 #define B64BIT
45 #endif
46 #endif
47
48
49
50 #ifdef HAVE_GCC3
51 # undef HAVE_GCC3
52 #endif
53 #ifdef HAVE_C99
54 # undef HAVE_C99
55 #endif
56 #if (defined(__GNUC__) && (__GNUC__ >= 3))
57 # define HAVE_C99 1
58 # if defined(__BUILDING_LIBCSOUND) || defined(CSOUND_CSDL_H)
59 # ifndef _ISOC99_SOURCE
60 # define _ISOC99_SOURCE 1
61 # endif
62 # ifndef _ISOC9X_SOURCE
63 # define _ISOC9X_SOURCE 1
64 # endif
65 # endif
66 # if !(defined(__MACH__) && (__GNUC__ == 3) && (__GNUC_MINOR__ < 2))
67 # define HAVE_GCC3 1
68 # endif
69 #elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
70 # define HAVE_C99 1
71 #endif
72
73 #if defined(__GNUC__)
74 # if defined(__GNUC_PATCHLEVEL__)
75 # define __GNUC_VERSION__ (__GNUC__ * 10000 \
76 + __GNUC_MINOR__ * 100 \
77 + __GNUC_PATCHLEVEL__)
78 # else
79 # define __GNUC_VERSION__ (__GNUC__ * 10000 \
80 + __GNUC_MINOR__ * 100)
81 # endif
82 #endif
83
84 #ifndef CABBAGE
85 #ifdef MSVC
86 typedef __int32 int32;
87 typedef __int16 int16;
88 typedef unsigned __int32 uint32;
89 typedef unsigned __int16 uint16;
90 #else
91 #include <stdint.h>
92 typedef int_least32_t int32;
93 typedef int_least16_t int16;
94 typedef uint_least32_t uint32;
95 typedef uint_least16_t uint16;
96 #endif
97 #endif
98
99 #if defined(HAVE_PTHREAD_SPIN_LOCK)
100 #include <pthread.h>
101 #endif
102
103 #ifdef __MACH__
104 #include <AvailabilityMacros.h>
105 #endif
106
107 #if !defined(USE_DOUBLE)
108 #if !defined(_MSC_VER)
109 #include "float-version.h"
110 #else
111 #define USE_DOUBLE
112 #endif
113 #endif
114
115 #ifdef USE_DOUBLE
116 /* Defined here as Android does not have log2 functions */
117 #define MYRECIPLN2 1.442695040888963407359924681001892137426 /* 1.0/log(2) */
118 #define LOG2(a) (MYRECIPLN2*log(a)) /* floating point logarithm base 2 */
119
120 #define ACOS acos
121 #define ASIN asin
122 #define ATAN atan
123 #define ATAN2 atan2
124 #define COS cos
125 #define SIN sin
126 #define TAN tan
127 #define COSH cosh
128 #define SINH sinh
129 #define TANH tanh
130 #define ACOSH acosh
131 #define ASINH asinh
132 #define ATANH atanh
133 #define EXP exp
134 #define LOG log
135 #define LOG10 log10
136 /* #define LOG2 log2 */
137 #define POWER pow
138 #define SQRT sqrt
139 #define HYPOT hypot
140 #define FABS fabs
141 #define FLOOR floor
142 #define CEIL ceil
143 #define FMOD fmod
144 #define MODF modf
145 #else
146 /* Defined here as Android does not have log2 functions */
147 #define MYRECIPLN2 1.442695040888963407359924681001892137426 /* 1.0/log(2) */
148 #define LOG2(a) (MYRECIPLN2*logf(a)) /* floating point logarithm base 2 */
149
150 #define ACOS acosf
151 #define ASIN asinf
152 #define ATAN atanf
153 #define ATAN2 atan2f
154 #define COS cosf
155 #define SIN sinf
156 #define TAN tanf
157 #define COSH coshf
158 #define SINH sinhf
159 #define TANH tanhf
160 #define ACOSH acoshf
161 #define ASINH asinhf
162 #define ATANH atanhf
163 #define EXP expf
164 #define LOG logf
165 #define LOG10 log10f
166 /* #define LOG2 log2f */
167 #define POWER powf
168 #define SQRT sqrtf
169 #define HYPOT hypotf
170 #define FABS(x) fabsf(FL(x))
171 #define FLOOR floorf
172 #define CEIL ceilf
173 #define FMOD fmodf
174 #define MODF modff
175 #endif
176
177 #include <stdio.h>
178 #include <stdlib.h>
179 #include <math.h>
180 #include <string.h>
181 #if defined(HAVE_FCNTL_H) || defined(__unix) || defined(__unix__)
182 #include <fcntl.h>
183 #endif
184 #if defined(HAVE_UNISTD_H) || defined(__unix) || defined(__unix__)
185 #include <unistd.h>
186 #endif
187
188 /* Experiment with doubles or floats */
189
190 #ifndef __MYFLT_DEF
191 # define __MYFLT_DEF
192 # ifndef USE_DOUBLE
193 # define MYFLT float
194 # else
195 # define MYFLT double
196 # endif
197 #endif
198
199 /* Aligning to double boundaries, should work with MYFLT as float or double */
200 #define CS_FLOAT_ALIGN(x) ((int)(x + 7) & (~7))
201
202 #if defined(__BUILDING_LIBCSOUND) || defined(CSOUND_CSDL_H)
203
204 #define FL(x) ((MYFLT) (x))
205
206 /* find out operating system if not specified on the command line */
207
208 #if defined(_WIN32) || defined(__WIN32__)
209 # ifndef WIN32
210 # define WIN32 1
211 # endif
212 #elif (defined(linux) || defined(__linux)) && !defined(LINUX)
213 # define LINUX 1
214 #endif
215
216 #if defined(WIN32) && defined(_MSC_VER) && !defined(__GNUC__)
217 # ifndef MSVC
218 # define MSVC 1
219 # endif
220 #elif defined(MSVC)
221 # undef MSVC
222 #endif
223
224 /* inline keyword: always available in C++, C99, and GCC 3.x and above */
225 /* add any other compiler that supports 'inline' */
226
227 #if !(defined(__cplusplus) || defined(inline))
228 # if defined(HAVE_C99) || defined(HAVE_GCC3)
229 # if defined(__GNUC__) && defined(__STRICT_ANSI__)
230 # define inline __inline__
231 # endif
232 # elif defined(MSVC)
233 # define inline __inline
234 # else
235 # define inline
236 # endif
237 #endif
238
239 #define DIRSEP '/'
240 #ifdef WIN32
241 # undef DIRSEP
242 # define DIRSEP '\\'
243 # if !defined(O_NDELAY)
244 # define O_NDELAY (0)
245 # endif
246 # include <io.h>
247 #else
248 # ifdef DOSGCC
249 # if !defined(O_NDELAY)
250 # define O_NDELAY (0)
251 # endif
252 # endif
253 # ifdef HAVE_SYS_TYPES_H
254 # include <sys/types.h>
255 # endif
256 /* RWD for WIN32 on VC++ */
257 #endif
258 #ifndef MSVC
259 # include <sys/file.h>
260 #endif
261 #include <sys/stat.h>
262
263 #endif /* __BUILDING_LIBCSOUND || CSOUND_CSDL_H */
264
265 #ifdef WIN32
266 # define ENVSEP ';'
267 #else
268 # define ENVSEP ':'
269 #endif
270 /* standard integer types */
271
272 #if defined(USE_GUSI2)
273 /* When compiling with GUSI on MacOS 9 (for Python), */
274 /* all of the other integer types are already defined */
275 typedef int64_t int_least64_t;
276 typedef uint64_t uint_least64_t;
277 #elif defined(HAVE_STDINT_H) || defined(HAVE_C99)
278 # include <stdint.h>
279
280 # if defined(__CYGWIN__)
281 #define __int8 char
282 #define __int16 short
283 #define __int32 int
284 #define __int64 long long
285 # endif
286 #else
287 typedef signed char int8_t;
288 typedef unsigned char uint8_t;
289 typedef short int16_t;
290 typedef unsigned short uint16_t;
291 typedef int int32_t;
292 typedef unsigned int uint32_t;
293 # if defined(__GNUC__) || !defined(WIN32)
294 typedef long long int64_t;
295 typedef unsigned long long uint64_t;
296 typedef long long int_least64_t;
297 typedef unsigned long long uint_least64_t;
298 # else
299 typedef __int64 int64_t;
300 typedef unsigned __int64 uint64_t;
301 typedef __int64 int_least64_t;
302 typedef unsigned __int64 uint_least64_t;
303 # endif
304 #if !defined(_MSC_VER)
305 typedef long intptr_t;
306 typedef unsigned long uintptr_t;
307 #endif
308 #endif /* !(USE_GUSI2 || HAVE_STDINT_H || HAVE_C99) */
309
310
311
312 /* function attributes */
313
314 #if defined(HAVE_GCC3) && !defined(SWIG)
315 /* deprecated function, variable, or type that is to be removed eventually */
316 # define CS_DEPRECATED __attribute__ ((__deprecated__))
317 /* a function that should not be inlined */
318 # define CS_NOINLINE __attribute__ ((__noinline__))
319 /* a function that never returns (e.g. csoundDie()) */
320 # define CS_NORETURN __attribute__ ((__noreturn__))
321 /* printf-style function with first argument as format string */
322 # define CS_PRINTF1 __attribute__ ((__format__ (__printf__, 1, 2)))
323 /* printf-style function with second argument as format string */
324 # define CS_PRINTF2 __attribute__ ((__format__ (__printf__, 2, 3)))
325 /* printf-style function with third argument as format string */
326 # define CS_PRINTF3 __attribute__ ((__format__ (__printf__, 3, 4)))
327 /* a function with no side effects or dependencies on volatile data */
328 # define CS_PURE __attribute__ ((__pure__))
329 #else
330 # define CS_DEPRECATED
331 # define CS_NOINLINE
332 # define CS_NORETURN
333 # define CS_PRINTF1
334 # define CS_PRINTF2
335 # define CS_PRINTF3
336 # define CS_PURE
337 #endif
338 #if defined(__clang__) || defined(HAVE_GCC3)
339 # define LIKELY(x) __builtin_expect(!!(x),1)
340 # define UNLIKELY(x) __builtin_expect(!!(x),0)
341 #else
342 # define LIKELY(x) x
343 # define UNLIKELY(x) x
344 #endif
345
346 #if defined(__BUILDING_LIBCSOUND) || defined(CSOUND_CSDL_H)
347
348 /* macros for converting floats to integers */
349 /* MYFLT2LONG: converts with unspecified rounding */
350 /* MYFLT2LRND: rounds to nearest integer */
351
352 #ifdef USE_LRINT
353 # ifndef USE_DOUBLE
354 # define MYFLT2LONG(x) (x > LONG_MIN && x < LONG_MAX ? \
355 (int32) lrintf((float) (x)) : 0)
356 # define MYFLT2LRND(x) (x > LONG_MIN && x < LONG_MAX ? \
357 (int32) lrintf((float) (x)) : 0)
358 # else
359 # define MYFLT2LONG(x) (x > LONG_MIN && x < LONG_MAX ? \
360 (int32) lrint((double) (x)) : 0)
361 # define MYFLT2LRND(x) (x > LONG_MIN && x < LONG_MAX ? \
362 (int32) lrint((double) (x)) : 0)
363 # endif
364 #elif defined(MSVC)
365 #include <emmintrin.h>
366 # ifndef USE_DOUBLE
367 // From Agner Fog optimisation manuals p.144
368 static inline int MYFLT2LONG (float const x) {
369 return _mm_cvtss_si32 (_mm_load_ss (&x));
370 }
371
372 static inline int MYFLT2LRND (float const x) {
373 return _mm_cvtss_si32 (_mm_load_ss (&x));
374 }
375
376 # else
377 static inline int MYFLT2LONG (double const x) {
378 return _mm_cvtsd_si32 (_mm_load_sd (&x));
379 }
380
381 static inline int MYFLT2LRND (double const x) {
382 return _mm_cvtsd_si32 (_mm_load_sd (&x));
383 }
384 # endif
385 #else
386 # ifndef USE_DOUBLE
387 # define MYFLT2LONG(x) ((int32) (x))
388 # if defined(HAVE_GCC3) && defined(__i386__) && !defined(__ICC)
389 # define MYFLT2LRND(x) ((int32) lrintf((float) (x)))
390 # else
391 static inline int32 MYFLT2LRND(float fval)
392 {
393 return ((int32) (fval + (fval < 0.0f ? -0.5f : 0.5f)));
394 }
395 # endif
396 # else
397 # define MYFLT2LONG(x) ((int32) (x))
398 # if defined(HAVE_GCC3) && defined(__i386__) && !defined(__ICC)
399 # define MYFLT2LRND(x) ((int32) lrint((double) (x)))
400 # else
401 static inline int32 MYFLT2LRND(double fval)
402 {
403 return ((int32) (fval + (fval < 0.0 ? -0.5 : 0.5)));
404 }
405 # endif
406 # endif
407 #endif
408
409 /* inline functions and macros for clamping denormals to zero */
410
411 #if defined(__i386__) || defined(MSVC)
412 static inline float csoundUndenormalizeFloat(float x)
413 {
414 volatile float tmp = 1.0e-30f;
415 return ((x + 1.0e-30f) - tmp);
416 }
417
418 static inline double csoundUndenormalizeDouble(double x)
419 {
420 volatile double tmp = 1.0e-200;
421 return ((x + 1.0e-200) - tmp);
422 }
423 #else
424 # define csoundUndenormalizeFloat(x) x
425 # define csoundUndenormalizeDouble(x) x
426 #endif
427
428 #ifndef USE_DOUBLE
429 # define csoundUndenormalizeMYFLT csoundUndenormalizeFloat
430 #else
431 # define csoundUndenormalizeMYFLT csoundUndenormalizeDouble
432 #endif
433
434 #endif /* __BUILDING_LIBCSOUND || CSOUND_CSDL_H */
435
436 // This is wrong..... needs thought
437 /* #ifdef HAVE_SPRINTF_L */
438 /* # define CS_SPRINTF sprintf_l */
439 /* #elseif HAVE__SPRINT_L */
440 /* /\* this would be the case for the Windows locale aware function *\/ */
441 /* # define CS_SPRINTF _sprintf_l */
442 /* #else */
443 # define CS_SPRINTF cs_sprintf
444 # define CS_SSCANF cs_sscanf
445 /* #endif */
446
447 #if !defined(HAVE_STRLCAT) && !defined(strlcat)
448 size_t strlcat(char *dst, const char *src, size_t siz);
449 #endif
450 char *strNcpy(char *dst, const char *src, size_t siz);
451
452 /* atomics */
453 #if defined(MSVC)
454 #define ATOMIC_SET(var, val) InterlockedExchange(&var, val);
455 #elif defined(HAVE_ATOMIC_BUILTIN)
456 #define ATOMIC_SET(var, val) __atomic_test_and_set(&var, val);
457 #else
458 #define ATOMIC_SET(var, val) var = val;
459 #endif
460
461 #if defined(MSVC)
462 #define ATOMIC_SET8(var, val) InterlockedExchange8(&var, val);
463 #elif defined(HAVE_ATOMIC_BUILTIN)
464 #define ATOMIC_SET8(var, val) __atomic_test_and_set(&var, val);
465 #else
466 #define ATOMIC_SET8(var, val) var = val;
467 #endif
468
469 #ifdef MSVC
470 #define ATOMIC_GET(var) InterlockedExchangeAdd(&var, 0)
471 #elif defined(HAVE_ATOMIC_BUILTIN)
472 #define ATOMIC_GET(var) __atomic_load_n(&var, __ATOMIC_SEQ_CST)
473 #else
474 #define ATOMIC_GET(var) var
475 #endif
476
477 #ifdef MSVC
478 #define ATOMIC_GET8(var) InterlockedExchangeAdd8(&var, 0)
479 #elif defined(HAVE_ATOMIC_BUILTIN)
480 #define ATOMIC_GET8(var) __atomic_load_n(&var, __ATOMIC_SEQ_CST)
481 #else
482 #define ATOMIC_GET8(var) var
483 #endif
484
485 #ifdef MSVC
486 #define ATOMIC_DECR(var) InterlockedExchangeAdd(&var, -1)
487 #elif defined(HAVE_ATOMIC_BUILTIN)
488 #define ATOMIC_DECR(var) __atomic_sub_fetch(&var, 1, __ATOMIC_SEQ_CST)
489 #else
490 #define ATOMIC_DECR(var) var -= 1
491 #endif
492
493 #ifdef MSVC
494 #define ATOMIC_INCR(var) InterlockedExchangeAdd(&var, 1)
495 #elif defined(HAVE_ATOMIC_BUILTIN)
496 #define ATOMIC_INCR(var) __atomic_add_fetch(&var, 1, __ATOMIC_SEQ_CST)
497 #else
498 #define ATOMIC_INCR(var) var += 1
499 #endif
500
501 #ifdef MSVC
502 #define ATOMIC_SUB(var, val) InterlockedExchangeAdd(&var, -val)
503 #elif defined(HAVE_ATOMIC_BUILTIN)
504 #define ATOMIC_SUB(var, val) __atomic_sub_fetch(&var, val, __ATOMIC_SEQ_CST)
505 #else
506 #define ATOMIC_SUB(var, val) var -= val
507 #endif
508
509 #ifdef MSVC
510 #define ATOMIC_ADD(var, val) InterlockedExchangeAdd(&var, val)
511 #elif defined(HAVE_ATOMIC_BUILTIN)
512 #define ATOMIC_ADD(var, val) __atomic_add_fetch(&var, val, __ATOMIC_SEQ_CST)
513 #else
514 #define ATOMIC_ADD(var, val) var += val
515 #endif
516
517 #if defined(MSVC)
518 #define ATOMIC_CMP_XCH(val, newVal, oldVal) \
519 (InterlockedCompareExchange(val, newVal, oldVal) != oldVal)
520 #elif defined(HAVE_ATOMIC_BUILTIN)
521 #define ATOMIC_CMP_XCH(val, newVal, oldVal) \
522 !(__atomic_compare_exchange(val, (long *) &oldVal, &newVal, 0, \
523 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
524 #else /* FIXME: no atomics, what to do? */
525 #define ATOMIC_CMP_XCH(val, newVal, oldVal) (*val = newVal) != oldVal
526 #endif
527
528 #if defined(WIN32)
529 typedef int32_t spin_lock_t;
530 #define SPINLOCK_INIT 0
531 #elif defined(__GNUC__) && defined(HAVE_PTHREAD_SPIN_LOCK)
532 typedef pthread_spinlock_t spin_lock_t;
533 #define SPINLOCK_INIT PTHREAD_SPINLOCK_INITIALIZER
534 #elif defined(__GNUC__) && defined(HAVE_SYNC_LOCK_TEST_AND_SET)
535 typedef int32_t spin_lock_t;
536 #define SPINLOCK_INIT 0
537 #elif defined(MACOSX)
538 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
539 #include <os/lock.h>
540 typedef struct os_unfair_lock_s spin_lock_t;
541 #define SPINLOCK_INIT {0}
542 #else
543 #include <libkern/OSAtomic.h>
544 typedef int32_t spin_lock_t;
545 #define SPINLOCK_INIT 0
546 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
547 #else
548 typedef int32_t spin_lock_t;
549 #define SPINLOCK_INIT 0
550 #endif
551
552 /* The ignore_value() macro is taken from GNULIB ignore-value.h,
553 licensed under the terms of the LGPLv2+
554 Normally casting an expression to void discards its value, but GCC
555 versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
556 which may cause unwanted diagnostics in that case. Use __typeof__
557 and __extension__ to work around the problem, if the workaround is
558 known to be needed. */
559 #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
560 # define ignore_value(x) \
561 (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
562 #else
563 # define ignore_value(x) ((void) (x))
564 #endif
565
566
567 #endif /* CSOUND_SYSDEP_H */