2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef SYSDEP_H_INCLUDED
22 #define SYSDEP_H_INCLUDED 1
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
39 #if defined(_M_IX86) || defined(__i386__) || defined(__i386) || \
40 defined(_X86_) || defined(__X86__) || defined(__I86__)
44 #if defined(_M_AMD64) || defined(__x86_64__) || defined(__amd64__)
48 #if defined(_M_IA64) || defined(__IA64__) || defined(__itanium__)
52 #if defined(_M_ARM) || defined(_M_ARMT) || defined(__arm__)
56 #if defined(__aarch64__)
62 #if (defined(__WIN32__) || defined(_WIN32) || defined(_WIN64)) && !defined(__W32__)
66 #if !defined(_AMD64_) && defined(AMD64CPU) && \
67 (((defined(WIN64) || defined(_WIN64))) || \
68 (defined(__GNUC__) && defined(__W32__)))
72 #if defined(__linux) && !defined(__linux__)
76 #if defined(__unix) && !defined(__unix__)
80 #if defined(sun) && !defined(SOLARIS)
84 /* Fastest calling conversion */
86 #if defined(IX86CPU) && (defined(_MSC_VER) || defined(__POCC__) || \
87 defined(__BORLANDC__) || defined(__WATCOMC__))
88 #define CALLINGCONV __fastcall
89 #elif defined(IX86CPU) && !defined(AMD64CPU) && defined(__GNUC__)
90 #define CALLINGCONV __attribute__((fastcall))
92 #define CALLINGCONV /**/
94 #endif /* !CALLINGCONV */
97 #define restrict /* not C99 */
98 #endif /* !restrict */
100 #ifndef TIMIDITY_FORCEINLINE
102 #define TIMIDITY_FORCEINLINE __attribute__((__always_inline__))
103 #elif defined(_MSC_VER)
104 #define TIMIDITY_FORCEINLINE __forceinline
106 #define TIMIDITY_FORCEINLINE inline
108 #endif /* TIMIDITY_FORCEINLINE */
110 /* The size of the internal buffer is 2^AUDIO_BUFFER_BITS samples.
111 This determines maximum number of samples ever computed in a row.
113 For Linux and FreeBSD users:
115 This also specifies the size of the buffer fragment. A smaller
116 fragment gives a faster response in interactive mode -- 10 or 11 is
117 probably a good number. Unfortunately some sound cards emit a click
118 when switching DMA buffers. If this happens to you, try increasing
119 this number to reduce the frequency of the clicks.
123 You should probably use a larger number for improved performance.
126 #ifndef DEFAULT_AUDIO_BUFFER_BITS
128 # define DEFAULT_AUDIO_BUFFER_BITS 12
130 # define DEFAULT_AUDIO_BUFFER_BITS 11
131 # endif /* __W32__ */
132 #endif /* !DEFAULT_AUDIO_BUFFER_BITS */
134 #ifndef DEFAULT_AUDIO_BUFFER_NUM
135 #define DEFAULT_AUDIO_BUFFER_NUM 32
136 #endif /* !DEFAULT_AUDIO_BUFFER_NUM */
138 #ifndef DEFAULT_COMPUTE_BUFFER_BITS
139 #define DEFAULT_COMPUTE_BUFFER_BITS (-1)
140 #endif /* !DEFAULT_COMPUTE_BUFFER_BITS */
143 #define SAMPLE_LENGTH_BITS 32 /* PAT/SF2:DWORD(unsined int 32bit */
146 # define VOLATILE_TOUCH(val) /* do nothing */
147 # define VOLATILE volatile
149 extern int volatile_touch(void *dmy);
150 # define VOLATILE_TOUCH(val) volatile_touch(&(val))
152 #endif /* NO_VOLATILE */
155 #ifdef WORDS_BIGENDIAN
157 # define BIG_ENDIAN 4321
159 # undef LITTLE_ENDIAN
162 # ifndef LITTLE_ENDIAN
163 # define LITTLE_ENDIAN 1234
167 /* Inline assembly (_asm and __asm__) */
168 #if defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DMC__) || \
169 (defined(__BORLANDC__) && (__BORLANDC__ >= 1380))
170 # define SUPPORT_ASM_INTEL 1
171 #elif defined(__GNUC__) && !defined(SUPPORT_ASM_INTEL)
172 # define SUPPORT_ASM_AT_AND_T 1
176 /* integer type definitions: ISO C now knows a better way */
177 #if defined(HAVE_STDINT_H) || \
178 (defined(_MSC_VER) && _MSC_VER >= 1600 /* VC2010 */) || \
179 (defined(__GNUC__) && __GNUC__ >= 3)
180 #include <stdint.h> // int types are defined here
182 typedef uint8_t uint8;
183 typedef int16_t int16;
184 typedef uint16_t uint16;
185 typedef int32_t int32;
186 typedef uint32_t uint32;
187 typedef int64_t int64;
188 typedef uint64_t uint64;
189 #define TIMIDITY_HAVE_INT64 1
194 typedef unsigned char uint8;
196 typedef unsigned short uint16;
198 typedef signed char int8;
199 typedef unsigned char uint8;
200 typedef signed short int16;
201 typedef unsigned short uint16;
203 /* DEC MMS has 64 bit long words */
204 /* Linux-Axp has also 64 bit long words */
205 #if defined(DEC) || defined(__alpha__) \
206 || defined(__ia64__) || defined(__x86_64__) \
207 || defined(__ppc64__) || defined(__s390x__) \
208 || defined(__mips64__) || defined(__LP64__) \
211 typedef unsigned int uint32;
213 typedef unsigned long uint64;
214 #define TIMIDITY_HAVE_INT64 1
216 #else /* 32bit architectures */
218 typedef unsigned long uint32;
221 /* gcc version<3 (gcc3 has c99 support) */
222 typedef long long int64;
223 typedef unsigned long long uint64;
224 #define TIMIDITY_HAVE_INT64 1
226 #elif defined(_MSC_VER)
227 /* VC++. or PellesC */
229 typedef __int64 int64;
230 typedef unsigned __int64 uint64;
232 typedef _int64 int64;
233 typedef unsigned _int64 uint64;
235 #define TIMIDITY_HAVE_INT64 1
237 #elif defined(__BORLANDC__) || defined(__WATCOMC__)
238 typedef __int64 int64;
239 typedef unsigned __int64 uint64;
240 #define TIMIDITY_HAVE_INT64 1
242 #elif defined(__MACOS__)
243 /* Mac's C compiler seems to have these types in common */
244 typedef SInt64 int64;
245 typedef UInt64 uint64;
246 #define TIMIDITY_HAVE_INT64 1
249 /* typedef long int64; */
250 /* typedef unsigned long uint64; */
251 #undef TIMIDITY_HAVE_INT64
253 #endif /* 64bit arch */
256 #if (defined(SIZEOF_LONG_DOUBLE) && SIZEOF_LONG_DOUBLE > 8) || \
257 (defined(__SIZEOF_LONG_DOUBLE__) && __SIZEOF_LONG_DOUBLE__ > 8) || \
258 (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) || \
259 defined(__BORLANDC__)
260 /* 80-bit (12~16 bytes) */
261 typedef long double LDBL_T;
262 #define HAVE_LONG_DOUBLE 1
264 /* 64-bit (8 bytes) */
265 typedef double LDBL_T;
266 #undef HAVE_LONG_DOUBLE
269 /* pointer size is not long in WIN64 and x86_64 */
270 #if (defined(SIZEOF_POINTER) && SIZEOF_POINTER == 8) || \
271 (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \
272 (defined(__W32__) && defined(_AMD64_))
274 typedef int64 ptr_size_t;
275 typedef uint64 u_ptr_size_t;
276 #elif defined(__linux__) || defined(__unix__)
278 typedef long ptr_size_t;
279 typedef unsigned long u_ptr_size_t;
280 #elif defined(_MSC_VER) && !defined(__clang__) && !defined(__POCC__)
281 typedef _w64 int32 ptr_size_t;
282 typedef _w64 uint32 u_ptr_size_t;
284 typedef int32 ptr_size_t;
285 typedef uint32 u_ptr_size_t;
288 /* type of file offset/size */
291 # if _MSC_VER >= 1300 // VC2003 toolkit // def 1400 /* VC2005 */
294 /* 64-bit offset/size (VCRT) */
295 # define fseeko _fseeki64
296 # define ftello _ftelli64
297 # define lseek _lseeki64
298 typedef int64 off_size_t;
299 # define HAVE_OFF_SIZE_T_64BIT 1
301 /* 32-bit offset/size (PSDKCRT) */
302 # define fseeko fseek
303 # define ftello ftell
304 # define lseek _lseek
305 typedef long off_size_t;
306 # endif /* PSDKCRT */
308 /* 32-bit offset/size (pseudo) */
309 # define fseeko fseek
310 # define ftello ftell
311 # define lseek _lseek
312 typedef off_t off_size_t; /* off_t is 32-bit signed */
313 # endif /* _MSC_VER >= 1400 */
314 #elif defined(__MINGW32__)
316 # define fseeko _fseeki64
317 # define ftello _ftelli64
318 # define lseek _lseeki64
319 typedef int64 off_size_t;
320 # define HAVE_OFF_SIZE_T_64BIT 1
321 #elif defined(_LARGEFILE_SOURCE) || \
322 (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || \
323 (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
324 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
325 /* 64-bit offset/size (o) */
326 typedef off_t off_size_t; /* off_t is 64-bit signed */
327 # define HAVE_OFF_SIZE_T_64BIT 1
328 #elif defined(HAVE_FSEEKO) && defined(HAVE_FTELLO)
329 /* 32-bit offset/size (o) */
330 typedef off_t off_size_t; /* off_t is 32-bit signed */
332 /* 32-bit offset/size (pseudo) */
333 # define fseeko fseek
334 # define ftello ftell
335 typedef off_t off_size_t; /* off_t is 32-bit signed */
338 /* type of audio data */
340 typedef double DATA_T;
343 #elif defined(DATA_T_FLOAT)
344 typedef float DATA_T;
346 # undef DATA_T_DOUBLE
348 typedef int32 DATA_T; // only 16bit 8bit sample
349 # ifndef DATA_T_INT32
350 # define DATA_T_INT32 1
353 # undef DATA_T_DOUBLE
356 /* type of floating point number */
357 #ifdef FLOAT_T_DOUBLE
358 typedef double FLOAT_T;
359 #else // FLOAT_T_FLOAT
360 typedef float FLOAT_T;
361 #define FLOAT_T_FLOAT
365 /* Instrument files are little-endian, MIDI files big-endian, so we
366 need to do some conversions. */
367 #define XCHG_SHORT_CONST(x) ((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF))
369 # define XCHG_SHORT(_x) ( \
371 unsigned short x = (_x); \
372 ((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF)); \
376 # define XCHG_SHORT(x) XCHG_SHORT_CONST(x)
379 #define XCHG_LONG_CONST(x) ((((x) & 0xFFL) << 24) | \
380 (((x) & 0xFF00L) << 8) | \
381 (((x) & 0xFF0000L) >> 8) | \
382 (((x) >> 24) & 0xFFL))
383 #if defined(__i486__) && !defined(__i386__)
384 # define XCHG_LONG(x) \
386 asm("bswap %1; movl %1,%0" : "=g" (__value) : "r" (x)); \
388 #elif defined(__GNUC__)
389 # define XCHG_LONG(_x) ( \
391 uint32 x = (_x); /* 32-bit */ \
392 ((((x) & 0xFFL) << 24) | \
393 (((x) & 0xFF00L) << 8) | \
394 (((x) & 0xFF0000L) >> 8) | \
395 (((x) >> 24) & 0xFFL)); \
399 # define XCHG_LONG(x) XCHG_LONG_CONST(x)
403 #define XCHG_LONGLONG_CONST(x)(\
404 (((x) & 0xFFL) << 56) | \
405 (((x) & 0xFF00L) << 40) | \
406 (((x) & 0xFF0000L) << 24) | \
407 (((x) & 0xFF000000L) << 8) | \
408 (((x) & 0xFF00000000LL) >> 8) | \
409 (((x) & 0xFF0000000000LL) >> 24) | \
410 (((x) & 0xFF000000000000LL) >> 40) | \
411 (((x) >> 56) & 0xFFL))
413 # define XCHG_LONGLONG(_x) ( \
415 unsigned long long x = (_x); \
417 (((x) & 0xFFL) << 56) | \
418 (((x) & 0xFF00L) << 40) | \
419 (((x) & 0xFF0000L) << 24) | \
420 (((x) & 0xFF000000L) << 8) | \
421 (((x) & 0xFF00000000LL) >> 8) | \
422 (((x) & 0xFF0000000000LL) >> 24) | \
423 (((x) & 0xFF000000000000LL) >> 40) | \
424 (((x) >> 56) & 0xFFL)); \
428 # define XCHG_LONGLONG(x) XCHG_LONGLONG_CONST(x)
433 # define LE_SHORT(x) (x)
434 # define LE_LONG(x) (x)
435 # define LE_LONGLONG(x) (x)
436 # define LE_SHORT_CONST(x) (x)
437 # define LE_LONG_CONST(x) (x)
438 # define LE_LONGLONG_CONST(x) (x)
439 # define BE_SHORT_CONST(x) XCHG_SHORT_CONST(x)
440 # define BE_LONG_CONST(x) XCHG_LONG_CONST(x)
441 # define BE_LONGLONG_CONST(x) XCHG_LONGLONG_CONST(x)
443 # include <osreldate.h>
444 # if __FreeBSD_version <= 500000
445 # define BE_SHORT(x) __byte_swap_word(x)
446 # define BE_LONG(x) __byte_swap_long(x)
447 # define BE_LONGLONG(x) XCHG_LONGLONG(x)
449 # if __FreeBSD_version <= 500028
450 # define BE_SHORT(x) __uint8_swap_uint16(x)
451 # define BE_LONG(x) __uint8_swap_uint32(x)
452 # define BE_LONGLONG(x) XCHG_LONGLONG(x)
454 # define BE_SHORT(x) __bswap16(x)
455 # define BE_LONG(x) __bswap32(x)
456 # define BE_LONGLONG(x) __bswap64(x)
460 # define BE_SHORT(x) XCHG_SHORT(x)
461 # define BE_LONG(x) XCHG_LONG(x)
462 # define BE_LONGLONG(x) XCHG_LONGLONG(x)
464 #else /* BIG_ENDIAN */
465 # define BE_SHORT(x) (x)
466 # define BE_LONG(x) (x)
467 # define BE_LONGLONG(x) (x)
468 # define BE_SHORT_CONST(x) (x)
469 # define BE_LONG_CONST(x) (x)
470 # define BE_LONGLONG_CONST(x) (x)
471 # define LE_SHORT_CONST(x) XCHG_SHORT_CONST(x)
472 # define LE_LONG_CONST(x) XCHG_LONG_CONST(x)
473 # define LE_LONGLONG_CONST(x) XCHG_LONGLONG_CONST(x)
475 # include <osreldate.h>
476 # if __FreeBSD_version <= 500000
477 # define LE_SHORT(x) __byte_swap_word(x)
478 # define LE_LONG(x) __byte_swap_long(x)
479 # define LE_LONGLONG(x) XCHG_LONGLONG(x)
481 # if __FreeBSD_version <= 500028
482 # define LE_SHORT(x) __uint8_swap_uint16(x)
483 # define LE_LONG(x) __uint8_swap_uint32(x)
484 # define LE_LONGLONG(x) XCHG_LONGLONG(x)
486 # define LE_SHORT(x) __bswap16(x)
487 # define LE_LONG(x) __bswap32(x)
488 # define LE_LONGLONG(x) __bswap64(x)
492 # define LE_SHORT(x) XCHG_SHORT(x)
493 # define LE_LONG(x) XCHG_LONG(x)
494 # define LE_LONGLONG(x) XCHG_LONGLONG(x)
495 # endif /* __FreeBSD__ */
496 #endif /* LITTLE_ENDIAN */
499 #define MAX_PORT (MAX_CHANNELS / 16)
500 #endif /* !MAX_PORT */
501 #if MAX_PORT > MAXMIDIPORT
504 #define MAX_CHANNELS (MAXMIDIPORT * 16)
505 #define MAX_PORT (MAXMIDIPORT)
508 #if ((MAX_CHANNELS) & 0xF) == 0
509 #define MIDIPORT_MASK(ch) ((ch) & (MAX_CHANNELS - 1u))
511 #define MIDIPORT_MASK(ch) ((ch) % MAX_CHANNELS)
515 /* max_channels is defined in "timidity.h" */
516 #if MAX_CHANNELS > 32
517 typedef struct _ChannelBitMask
519 uint32 b[8]; /* 256-bit bitvector */
521 #define CLEAR_CHANNELMASK(bits) \
522 memset((bits).b, 0, sizeof(ChannelBitMask))
523 #define FILL_CHANNELMASK(bits) \
524 memset((bits).b, 0xFF, sizeof(ChannelBitMask))
525 #define IS_SET_CHANNELMASK(bits, c) \
526 ((bits).b[((c) >> 5) & 0x7] & (1u << ((c) & 0x1F)))
527 #define SET_CHANNELMASK(bits, c) \
528 ((bits).b[((c) >> 5) & 0x7] |= (1u << ((c) & 0x1F)))
529 #define UNSET_CHANNELMASK(bits, c) \
530 ((bits).b[((c) >> 5) & 0x7] &= ~(1u << ((c) & 0x1F)))
531 #define TOGGLE_CHANNELMASK(bits, c) \
532 ((bits).b[((c) >> 5) & 0x7] ^= (1u << ((c) & 0x1F)))
533 #define COPY_CHANNELMASK(dest, src) \
534 memcpy(&(dest), &(src), sizeof(ChannelBitMask))
535 #define REVERSE_CHANNELMASK(bits, a) \
536 ((bits).b[a] = ~(bits).b[a])
537 #define COMPARE_CHANNELMASK(bitsA, bitsB) \
538 (memcmp((bitsA).b, (bitsB).b, sizeof ((bitsA).b)) == 0)
540 typedef struct _ChannelBitMask
542 uint32 b; /* 32-bit bitvector */
544 #define CLEAR_CHANNELMASK(bits) ((bits).b = 0)
545 #define FILL_CHANNELMASK(bits) ((bits).b = ~0)
546 #define IS_SET_CHANNELMASK(bits, c) ((bits).b & (1u << (c)))
547 #define SET_CHANNELMASK(bits, c) ((bits).b |= (1u << (c)))
548 #define UNSET_CHANNELMASK(bits, c) ((bits).b &= ~(1u << (c)))
549 #define TOGGLE_CHANNELMASK(bits, c) ((bits).b ^= (1u << (c)))
550 #define COPY_CHANNELMASK(dest, src) ((dest).b = (src).b)
551 #define REVERSE_CHANNELMASK(bits,a) ((bits).b = ~(bits).b) // not use a = NULL
552 #define COMPARE_CHANNELMASK(bitsA, bitsB) ((bitsA).b == (bitsB).b)
556 typedef int8 sample_t;
557 typedef uint8 final_volume_t;
558 # define MIXUP_SHIFT 5
560 typedef int16 sample_t;
561 #if defined(DATA_T_DOUBLE) || defined(DATA_T_FLOAT)
562 typedef FLOAT_T final_volume_t;
564 typedef int32 final_volume_t;
569 #if ((SAMPLE_LENGTH_BITS + FRACTION_BITS) >= 32) && defined(TIMIDITY_HAVE_INT64)
570 typedef int64 splen_t;
571 #define SPLEN_T_MAX (splen_t)((((uint64)1ULL) << 63) - 1)
572 #define INTEGER_BITS (64 - FRACTION_BITS) // 64bit
573 #define INTEGER_MASK (0xFFFFFFFFFFFFFFFF << FRACTION_BITS) // 64bit
575 typedef int32 splen_t;
576 #define SPLEN_T_MAX (splen_t)((((uint32)1UL) << 31) - 1)
577 #define INTEGER_BITS (32 - FRACTION_BITS) // 32bit
578 #define INTEGER_MASK (0xFFFFFFFF << FRACTION_BITS) // 32bit
579 #endif /* SAMPLE_LENGTH_BITS */
581 #define FRACTION_MASK (~ INTEGER_MASK)
584 # define TIM_FSCALE(a, b) ldexp((double)(a), (b))
585 # define TIM_FSCALENEG(a, b) ldexp((double)(a), -(b))
589 # define TIM_FSCALE(a, b) ((a) * (double)(1L << (b)))
590 # define TIM_FSCALENEG(a, b) ((a) * (1.0 / (double)(1L << (b))))
599 int usleep(unsigned int useconds); /* shut gcc warning up */
600 #elif defined(__SUNOS__)
602 #include <sys/stdtypes.h>
604 #define memcpy(x, y, n) bcopy(y, x, n)
609 #undef PATCH_EXT_LIST
610 #define PATCH_EXT_LIST { ".pat", 0 }
612 #define URL_DIR_CACHE_DISABLE 1
615 /* The path separator (D.M.) */
617 * Cygwin: both "\" and "/"
622 # define PATH_SEP '\\'
623 # define PATH_STRING "\\"
624 # define PATH_SEP2 '/'
625 #elif defined(__MACOS__)
626 # define PATH_SEP ':'
627 # define PATH_STRING ":"
629 # define PATH_SEP '/'
630 # define PATH_STRING "/"
634 #define IS_PATH_SEP(c) ((c) == PATH_SEP || (c) == PATH_SEP2)
636 #define IS_PATH_SEP(c) ((c) == PATH_SEP)
643 #else /* !__W32G__ */
649 // _MSC_VER < 1600 VC2010(VC10) stdint.h
\82ª
\82È
\82¢
\8fê
\8d\87
651 #define INT8_MAX 127i8
654 #define INT16_MAX 32767i16
655 #endif /*INT16_MAX */
657 #define INT32_MAX 2147483647i32
658 #endif /*INT32_MAX */
660 #define INT64_MAX 9223372036854775807i64
661 #endif /*INT64_MAX */
663 #define INT8_MIN (-127i8 - 1)
666 #define INT16_MIN (-32767i16 - 1)
667 #endif /*INT16_MIN */
669 #define INT32_MIN (-2147483647i32 - 1)
670 #endif /*INT32_MIN */
672 #define INT64_MIN (-9223372036854775807i64 - 1)
673 #endif /*INT64_MIN */
675 #define UINT8_MAX 0xffui8
676 #endif /*UINT8_MAX */
678 #define UINT16_MAX 0xffffui16
679 #endif /*UINT16_MAX */
681 #define UINT32_MAX 0xffffffffui32
682 #endif /*UINT32_MAX */
684 #define UINT64_MAX 0xffffffffffffffffui64
685 #endif /*UINT64_MAX */
688 #define M_E ((double)2.7182818284590452353602874713527)
691 #define M_PI ((double)3.1415926535897932384626433832795)
694 #define M_LN2 ((double)0.69314718055994530941723212145818) // log(2)
697 #define M_LN10 ((double)2.3025850929940456840179914546844) // log(10)
700 #define M_LOG2 ((double)0.30102999566398119521373889472449) // log10(2)
701 #define M_PI2 ((double)6.283185307179586476925286766559) // pi * 2
702 #define M_PI_DIV2 ((double)1.5707963267948966192313216916398) // pi / 2
705 #define M_1BIT (2) // 1U<<1
706 #define M_2BIT (4) // 1U<<2
707 #define M_3BIT (8) // 1U<<3
708 #define M_4BIT (16) // 1U<<4
709 #define M_5BIT (32) // 1U<<5
710 #define M_6BIT (64) // 1U<<6
711 #define M_7BIT (128) // 1U<<7
712 #define M_8BIT (256) // 1U<<8
713 #define M_9BIT (512) // 1U<<9
714 #define M_10BIT (1024) // 1U<<10
715 #define M_11BIT (2048) // 1U<<11
716 #define M_12BIT (4096) // 1U<<12
717 #define M_13BIT (8192) // 1U<<13
718 #define M_14BIT (16384) // 1U<<14
719 #define M_15BIT (32768L) // 1U<<15
720 #define M_16BIT (65536L) // 1U<<16
721 #define M_20BIT (1048576L) // 1U<<20
722 #define M_23BIT (8388608L) // 1U<<23
723 #define M_24BIT (16777216L) // 1U<<24
724 #define M_28BIT (268435456L) // 1U<<28
725 #define M_30BIT (1073741824UL) // 1U<<30
726 #define M_31BIT (2147483648UL) // 1U<<31
727 #define M_32BIT (4294967296LL) // 1U<<32
728 #define M_63BIT (9223372036854775808ULL) // 1U<<63
729 #define M_64BIT (18446744073709551616ULL) // 1U<<64
731 #define SQRT_2 ((double)1.4142135623730950488016887242097) // sqrt(2)
733 // float div to mult 1/X
734 #define DIV_2 ((double)0.5)
735 #define DIV_3 ((double)0.33333333333333333333333333333333)
736 #define DIV_4 ((double)0.25)
737 #define DIV_5 ((double)0.2)
738 #define DIV_6 ((double)0.16666666666666666666666666666667)
739 #define DIV_7 ((double)0.14285714285714285714285714285714)
740 #define DIV_8 ((double)0.125)
741 #define DIV_9 ((double)0.11111111111111111111111111111111)
742 #define DIV_10 ((double)0.1)
743 #define DIV_11 ((double)0.090909090909090909090909090909091)
744 #define DIV_12 ((double)0.083333333333333333333333333333333)
745 #define DIV_13 ((double)0.076923076923076923076923076923077)
746 #define DIV_14 ((double)0.071428571428571428571428571428571)
747 #define DIV_15 ((double)0.066666666666666666666666666666667) // 1/15
748 #define DIV_16 ((double)0.0625) // 1/16
749 #define DIV_19 ((double)0.052631578947368421052631578947368) // 1/19
750 #define DIV_20 ((double)0.05)
751 #define DIV_24 ((double)0.041666666666666666666666666666667) // 1/24
752 #define DIV_30 ((double)0.033333333333333333333333333333333) // 1/30
753 #define DIV_31 ((double)0.032258064516129032258064516129032) // 1/31
754 #define DIV_32 ((double)0.03125) // 1/32
755 #define DIV_36 ((double)0.027777777777777777777777777777778) // 1/36
756 #define DIV_40 ((double)0.025) // 1/40
757 #define DIV_48 ((double)0.020833333333333333333333333333333) // 1/48
758 #define DIV_50 ((double)0.02) // 1/50
759 #define DIV_60 ((double)0.016666666666666666666666666666667) // 1/60
760 #define DIV_63 ((double)0.015873015873015873015873015873016) // 1/63
761 #define DIV_64 ((double)0.015625) // 1/64
762 #define DIV_65 ((double)0.015384615384615384615384615384615) // 1/65
763 #define DIV_80 ((double)0.0125) // 1/80
764 #define DIV_96 ((double)0.010416666666666666666666666666) // 1/96
765 #define DIV_100 (double)(0.01) // 1/100
766 #define DIV_120 ((double)0.0083333333333333333333333333333333) // 1/120
767 #define DIV_127 (double)(0.007874015748031496062992125984252) // 1/127
768 #define DIV_128 (double)(0.0078125) // 1/128
769 #define DIV_140 ((double)0.0071428571428571428571428571428571) // 1/140
770 #define DIV_150 ((double)0.0066666666666666666666666666666667) // 1/150
771 #define DIV_160 ((double)0.00625) // 1/160
772 #define DIV_180 (double)(0.0055555555555555555555555555555556)
773 #define DIV_200 (double)(0.005) // 1/200
774 #define DIV_255 (double)(0.003921568627450980392156862745098)
775 #define DIV_256 (double)(0.00390625) // 1/256
776 #define DIV_360 (double)(0.0027777777777777777777777777777778)
777 #define DIV_440 (double)(0.0022727272727272727272727272727273)
778 #define DIV_600 (double)(0.0016666666666666666666666666666667) // 1/600
779 #define DIV_1000 (double)(0.001) // 1/1000
780 #define DIV_1023 (double)(9.7751710654936461388074291300098e-4) // 1/1023
781 #define DIV_1024 (double)(0.0009765625)
782 #define DIV_1200 (double)(8.3333333333333333333333333333333e-4) // 1/1200
783 #define DIV_1600 (double)(0.000625) // 1/1600
784 #define DIV_2400 (double)(4.1666666666666666666666666666667e-4) // 1/2400
785 #define DIV_4800 (double)(2.0833333333333333333333333333333e-4) // 1/4800
786 #define DIV_9600 (double)(1.0416666666666666666666666666667e-4) // 1/9600
787 #define DIV_10000 (double)(0.0001) // 1/10000
788 #define DIV_12288 (double)(8.1380208333333333333333333333333e-5) // 1/12288
789 #define DIV_22000 (double)(4.5454545454545454545454545454545e-5) // 1/22000
790 #define DIV_44100 (double)(2.2675736961451247165532879818594e-5) // 1/44100
791 #define DIV_1000000 (double)(0.000001) // 1/1000000
793 #define DIV_3_2 ((double)0.6666666666666666666666666666666) // 1/(3/2)
794 #define DIV_32_6 ((double)0.1875) // 1/(32/6)
796 #define DIV_1BIT ((double)0.5) // 1/(1<<1)
797 #define DIV_2BIT ((double)0.25) // 1/(1<<2)
798 #define DIV_3BIT ((double)0.125) // 1/(1<<3)
799 #define DIV_4BIT ((double)0.0625) // 1/(1<<4)
800 #define DIV_5BIT ((double)0.03125) // 1/(1<<5)
801 #define DIV_6BIT ((double)0.015625) // 1/(1<<6)
802 #define DIV_7BIT ((double)0.0078125) // 1/(1<<7)
803 #define DIV_8BIT ((double)0.00390625) // 1/(1<<8)
804 #define DIV_9BIT ((double)0.001953125) // 1/(1<<9)
805 #define DIV_10BIT ((double)0.0009765625) // 1/(1<<10)
806 #define DIV_11BIT ((double)0.00048828125) // 1/(1<<11)
807 #define DIV_12BIT ((double)0.000244140625) // 1/(1<<12)
808 #define DIV_13BIT ((double)0.0001220703125) // 1/(1<<13)
809 #define DIV_14BIT ((double)0.00006103515625) // 1/(1<<14)
810 #define DIV_15BIT ((double)0.000030517578125) // 1/(1<<15)
811 #define DIV_16BIT ((double)0.0000152587890625) // 1/(1<<16)
812 #define DIV_17BIT ((double)0.00000762939453125) // 1/(1<<17)
813 #define DIV_18BIT ((double)0.000003814697265625) // 1/(1<<18)
814 #define DIV_19BIT ((double)0.0000019073486328125) // 1/(1<<19)
815 #define DIV_20BIT ((double)0.00000095367431640625) // 1/(1<<20)
816 #define DIV_21BIT ((double)0.000000476837158203125) // 1/(1<<21)
817 #define DIV_22BIT ((double)0.0000002384185791015625) // 1/(1<<22)
818 #define DIV_23BIT ((double)0.00000011920928955078125) // 1/(1<<23)
819 #define DIV_24BIT ((double)0.000000059604644775390625) // 1/(1<<24)
820 #define DIV_25BIT ((double)0.0000000298023223876953125) // 1/(1<<25)
821 #define DIV_26BIT ((double)0.00000001490116119384765625) // 1/(1<<26)
822 #define DIV_27BIT ((double)0.000000007450580596923828125) // 1/(1<<27)
823 #define DIV_28BIT ((double)0.0000000037252902984619140625) // 1/(1<<28)
824 #define DIV_29BIT ((double)0.00000000186264514923095703125) // 1/(1<<29)
825 #define DIV_30BIT ((double)0.000000000931322574615478515625) // 1/(1<<30)
826 #define DIV_31BIT ((double)0.0000000004656612873077392578125) // 1/(1<<31)
827 #define DIV_32BIT ((double)0.00000000023283064365386962890625) // 1/(1<<32)
828 #define DIV_33BIT ((double)0.000000000116415321826934814453125) // 1/(1<<33)
829 #define DIV_34BIT ((double)0.0000000000582076609134674072265625) // 1/(1<<34)
830 #define DIV_35BIT ((double)(2.910383045673370361328125e-11)) // 1/(1<<35)
831 #define DIV_36BIT ((double)(1.4551915228366851806640625e-11)) // 1/(1<<36)
832 #define DIV_37BIT ((double)(7.2759576141834259033203125e-12)) // 1/(1<<37)
833 #define DIV_38BIT ((double)(3.63797880709171295166015625e-12)) // 1/(1<<38)
834 #define DIV_39BIT ((double)(1.818989403545856475830078125e-12)) // 1/(1<<39)
835 #define DIV_40BIT ((double)(9.094947017729282379150390625e-13)) // 1/(1<<40)
836 #define DIV_48BIT ((double)(3.5527136788005009293556213378906e-15)) // 1/(1<<48)
837 #define DIV_50BIT ((double)(8.8817841970012523233890533447266e-16)) // 1/(1<<50)
838 #define DIV_52BIT ((double)(2.2204460492503130808472633361816e-16)) // 1/(1<<52)
839 #define DIV_63BIT ((double)(1.0842021724855044340074528008699e-19)) // 1/(1<<63)
840 #define DIV_64BIT ((double)(5.4210108624275221700372640043497e-20)) // 1/(1<<64)
842 #define DIV_PI ((double)0.31830988618379067153776752674503) // 1/pi
843 #define DIV_PI2 ((double)0.15915494309189533576888376337251) // 1/(pi*2)
844 #define DIV_LN2 ((double)1.4426950408889634073599246810019) // 1/log(2)
845 #define DIV_LN10 ((double)0.43429448190325182765112891891661) // 1/log(10)
846 #define DIV_LOG2 ((double)3.3219280948873623478703194294894) // 1/log10(2)
847 #define DIV_SQRT2 ((double)0.70710678118654752440084436210485) // 1/sqrt(2)
850 #define USE_SHIFT_DIV 1 /* (n / 2) -> (n >> 1) */
851 #define USE_RECIP_DIV 1 /* (n / 2.0) -> (n * 0.5) */
853 #if -1L == (-1L >> 1)
854 #define USE_ARITHMETIC_SHIHT 1
858 #define divi_2(i) ((i) >> 1)
859 #define divi_4(i) ((i) >> 2)
860 #define divi_8(i) ((i) >> 3)
861 #define divi_16(i) ((i) >> 4)
862 #define divi_32(i) ((i) >> 5)
863 #define divi_64(i) ((i) >> 6)
864 #define divi_128(i) ((i) >> 7)
865 #define divi_256(i) ((i) >> 8)
866 #define divi_512(i) ((i) >> 9)
867 #define divi_1024(i) ((i) >> 10)
868 #define divi_2048(i) ((i) >> 11)
869 #define divi_4096(i) ((i) >> 12)
871 #define divi_2(i) ((i) / 2)
872 #define divi_4(i) ((i) / 4)
873 #define divi_8(i) ((i) / 8)
874 #define divi_16(i) ((i) / 16)
875 #define divi_32(i) ((i) / 32)
876 #define divi_64(i) ((i) / 64)
877 #define divi_128(i) ((i) / 128)
878 #define divi_256(i) ((i) / 256)
879 #define divi_512(i) ((i) / 512)
880 #define divi_1024(i) ((i) / 1024)
881 #define divi_2048(i) ((i) / 2048)
882 #define divi_4096(i) ((i) / 4096)
886 #define divf_2(f) ((f) * DIV_2)
887 #define divf_4(f) ((f) * DIV_4)
888 #define divf_8(f) ((f) * DIV_8)
889 #define divf_16(f) ((f) * DIV_16)
890 #define divf_32(f) ((f) * DIV_32)
891 #define divf_64(f) ((f) * DIV_64)
892 #define divf_128(f) ((f) * DIV_128)
893 #define divf_256(f) ((f) * DIV_256)
894 #define divf_512(f) ((f) * DIV_9BIT)
895 #define divf_1024(f) ((f) * DIV_10BIT)
896 #define divf_2048(f) ((f) * DIV_11BIT)
897 #define divf_4096(f) ((f) * DIV_12BIT)
899 #define divf_2(flt) ((flt) / 2.0f)
900 #define divf_4(flt) ((flt) / 4.0f)
901 #define divf_8(flt) ((flt) / 8.0f)
902 #define divf_16(flt) ((flt) / 16.0f)
903 #define divf_32(flt) ((flt) / 32.0f)
904 #define divf_64(flt) ((flt) / 64.0f)
905 #define divf_128(flt) ((flt) / 128.0f)
906 #define divf_256(flt) ((flt) / 256.0f)
907 #define divf_512(flt) ((flt) / 512.0f)
908 #define divf_1024(flt) ((flt) / 1024.0f)
909 #define divf_2048(flt) ((flt) / 2048.0f)
910 #define divf_4096(flt) ((flt) / 4096.0f)
913 #if defined(DATA_T_INT32) && defined(USE_ARITHMETIC_SHIHT)
914 #define divt_2(t) divi_2(t)
915 #define divt_4(t) divi_4(t)
916 #define divt_8(t) divi_8(t)
917 #define divt_16(t) divi_16(t)
918 #define divt_32(t) divi_32(t)
919 #define divt_64(t) divi_64(t)
920 #define divt_128(t) divi_128(t)
921 #define divt_256(t) divi_256(t)
922 #define divt_512(t) divi_512(t)
923 #define divt_1024(t) divi_1024(t)
924 #define divt_2048(t) divi_2048(t)
925 #define divt_4096(t) divi_4096(t)
926 #elif defined(DATA_T_INT32)
927 #define divt_2(i) ((i) / 2)
928 #define divt_4(i) ((i) / 4)
929 #define divt_8(i) ((i) / 8)
930 #define divt_16(i) ((i) / 16)
931 #define divt_32(i) ((i) / 32)
932 #define divt_64(i) ((i) / 64)
933 #define divt_128(i) ((i) / 128)
934 #define divt_256(i) ((i) / 256)
935 #define divt_512(i) ((i) / 512)
936 #define divt_1024(i) ((i) / 1024)
937 #define divt_2048(i) ((i) / 2048)
938 #define divt_4096(i) ((i) / 4096)
940 #define divt_2(t) divf_2(t)
941 #define divt_4(t) divf_4(t)
942 #define divt_8(t) divf_8(t)
943 #define divt_16(t) divf_16(t)
944 #define divt_32(t) divf_32(t)
945 #define divt_64(t) divf_64(t)
946 #define divt_128(t) divf_128(t)
947 #define divt_256(t) divf_256(t)
948 #define divt_512(t) divf_512(t)
949 #define divt_1024(t) divf_1024(t)
950 #define divt_2048(t) divf_2048(t)
951 #define divt_4096(t) divf_4096(t)
959 #define aligned_malloc __mingw_aligned_malloc
960 #define aligned_free __mingw_aligned_free
961 /* aligned_malloc is unsafe because s must be a multiple of a */
962 //#elif __STDC_VERSION__ >= 201112L
963 //#define aligned_malloc(s,a) aligned_malloc(a,s)
964 //#define aligned_free free
965 #elif defined(__GNUC__) && _POSIX_VERSION >= 200112L
966 #define aligned_malloc(s,a) ({void *ptr; if(!s || posix_memalign(&ptr,a,s)) ptr = NULL; ptr;})
967 #define aligned_free free
969 #define aligned_malloc _aligned_malloc
970 #define aligned_free _aligned_free
973 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__DMC__)
974 /* strncasecmp() -> strncmpi(char*, char*, size_t) */
975 //#define strncasecmp(a, b, c) strncmpi(a, b, c)
976 //#define strcasecmp(a, b) strcmpi(a, b)
978 #define strncasecmp(a, b, c) strnicmp(a, b, c)
981 #define strcasecmp(a, b) stricmp(a, b)
983 #endif /* __BORLANDC__ */
987 #define strncasecmp(a, b, c) _strnicmp((a), (b), (c))
990 #define strcasecmp(a, b) _stricmp((a), (b))
995 //#define write _write
996 //#define lseek _lseek
997 #define unlink _unlink
998 #if _MSC_VER < 1500 /* 1500(VC9) */
1000 #ifdef HAVE_VSNPRINTF
1001 #define vsnprintf _vsnprintf
1004 #pragma warning(4 : 4305 4244)
1012 #ifndef STDOUT_FILENO
1013 #define STDOUT_FILENO 1
1016 #define _MAX_PATH 260
1020 #endif /* _MSC_VER */
1022 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1026 #ifndef STDOUT_FILENO
1027 #define STDOUT_FILENO 1
1030 #define _MAX_PATH 260
1032 #endif /* __GNUC__ */
1034 #define SAFE_CONVERT_LENGTH(len) (6 * (len) + 1)
1037 #include "mac_com.h"
1041 # undef DECOMPRESSOR_LIST
1042 # undef PATCH_CONVERTERS
1046 # undef DECOMPRESSOR_LIST
1047 # undef PATCH_CONVERTERS
1052 typedef ptrdiff_t ssize_t;
1057 #define _set_errno(val) (errno = val)
1060 #endif /* SYSDEP_H_INCUDED */