Subversion Repositories eduke32

Rev

Rev 4989 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 Plagman 1
// Compatibility declarations for things which might not be present in
2
// certain build environments. It also levels the playing field caused
3
// by different platforms.
4
 
4747 terminx 5
#ifndef compat_h_
6
#define compat_h_
5 Plagman 7
 
4685 hendricks2 8
#ifdef __GNUC__
4762 hendricks2 9
# define EDUKE32_GCC_PREREQ(major, minor) (major < __GNUC__ || (major == __GNUC__ && minor <= __GNUC_MINOR__))
4685 hendricks2 10
#else
11
# define EDUKE32_GCC_PREREQ(major, minor) 0
12
#endif
13
 
14
#ifdef __clang__
4762 hendricks2 15
# define EDUKE32_CLANG_PREREQ(major, minor) (major < __clang_major__ || (major == __clang_major__ && minor <= __clang_minor__))
4685 hendricks2 16
#else
17
# define EDUKE32_CLANG_PREREQ(major, minor) 0
18
#endif
19
#ifndef __has_builtin
20
# define __has_builtin(x) 0  // Compatibility with non-clang compilers.
21
#endif
22
#ifndef __has_feature
23
# define __has_feature(x) 0  // Compatibility with non-clang compilers.
24
#endif
25
#ifndef __has_extension
26
# define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
27
#endif
28
 
4785 helixhorne 29
#if !defined(_MSC_VER) || _MSC_FULL_VER < 180031101
4697 terminx 30
#ifdef UNREFERENCED_PARAMETER
31
#undef UNREFERENCED_PARAMETER
654 terminx 32
#endif
33
 
4697 terminx 34
#define UNREFERENCED_PARAMETER(x) x = x
4783 terminx 35
#endif
4697 terminx 36
 
1743 helixhorne 37
#if defined __GNUC__ || defined __clang__
38
# define ATTRIBUTE(attrlist) __attribute__(attrlist)
39
#else
40
# define ATTRIBUTE(attrlist)
41
#endif
42
 
3341 helixhorne 43
#if !defined __clang__ && !defined USING_LTO
44
# define ATTRIBUTE_OPTIMIZE(str) ATTRIBUTE((optimize(str)))
45
#else
46
# define ATTRIBUTE_OPTIMIZE(str)
47
#endif
48
 
4685 hendricks2 49
#if defined __GNUC__ || __has_builtin(__builtin_expect)
4680 terminx 50
#define EDUKE32_PREDICT_TRUE(x)       __builtin_expect(!!(x),1)
51
#define EDUKE32_PREDICT_FALSE(x)     __builtin_expect(!!(x),0)
4685 hendricks2 52
#else
53
#define EDUKE32_PREDICT_TRUE(x) (x)
54
#define EDUKE32_PREDICT_FALSE(x) (x)
55
#endif
56
 
57
#if EDUKE32_GCC_PREREQ(4,5)  || __has_builtin(__builtin_unreachable)
4680 terminx 58
#define EDUKE32_UNREACHABLE_SECTION(...)   __builtin_unreachable()
59
#elif _MSC_VER
60
#define EDUKE32_UNREACHABLE_SECTION(...)   __assume(0)
4685 hendricks2 61
#else
4680 terminx 62
#define EDUKE32_UNREACHABLE_SECTION(...) __VA_ARGS__
63
#endif
64
 
3180 helixhorne 65
#ifndef min
66
#define min(x,y) ((x) < (y) ? (x) : (y))
67
#endif
68
#ifndef max
69
#define max(x,y) ((x) > (y) ? (x) : (y))
70
#endif
71
 
4762 hendricks2 72
#ifndef __APPLE__
73
# include <malloc.h>
74
#endif
75
 
76
#ifdef _WIN32
77
# define WIN32_LEAN_AND_MEAN
78
# include <windows.h>
79
#endif
80
 
81
#ifdef __APPLE__
82
# include <TargetConditionals.h>
4942 hendricks2 83
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
84
#  define EDUKE32_IOS
85
# else
86
#  define EDUKE32_OSX
4762 hendricks2 87
#  if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_3
88
#   include <CoreFoundation/CoreFoundation.h>
89
#  endif
90
#  include <CoreServices/CoreServices.h>
91
# endif
92
#endif
93
 
4942 hendricks2 94
#if defined __ANDROID__ || defined EDUKE32_IOS
4852 hendricks2 95
# define EDUKE32_TOUCH_DEVICES
4989 terminx 96
# define EDUKE32_GLES
4852 hendricks2 97
#endif
98
 
618 terminx 99
// This gives us access to 'intptr_t' and 'uintptr_t', which are
100
// abstractions to the size of a pointer on a given platform
101
// (ie, they're guaranteed to be the same size as a pointer)
3116 hendricks2 102
 
4078 hendricks2 103
#undef __USE_MINGW_ANSI_STDIO // Workaround for MinGW-w64.
104
 
4762 hendricks2 105
#ifndef __STDC_FORMAT_MACROS
3116 hendricks2 106
#define __STDC_FORMAT_MACROS
4762 hendricks2 107
#endif
4540 hendricks2 108
#ifndef __STDC_LIMIT_MACROS
4708 helixhorne 109
# define __STDC_LIMIT_MACROS
4540 hendricks2 110
#endif
4619 terminx 111
#if defined(HAVE_INTTYPES) || defined(__cplusplus)
4708 helixhorne 112
# include <stdint.h>
113
# include <inttypes.h>
3496 hendricks2 114
 
115
// Ghetto. Blame devkitPPC's faulty headers.
4708 helixhorne 116
# ifdef GEKKO
3496 hendricks2 117
#  undef PRIdPTR
118
#  define PRIdPTR "d"
119
#  undef PRIxPTR
120
#  define PRIxPTR "x"
121
#  undef SCNx32
122
#  define SCNx32 "x"
4708 helixhorne 123
# endif
3496 hendricks2 124
 
795 terminx 125
#elif defined(_MSC_VER)
4708 helixhorne 126
# include "msvc/inttypes.h" // from http://code.google.com/p/msinttypes/
618 terminx 127
#endif
128
 
1435 terminx 129
#ifndef _MSC_VER
3278 hendricks2 130
#  ifndef __fastcall
131
#    if defined(__GNUC__) && defined(__i386__)
2992 helixhorne 132
#      define __fastcall __attribute__((fastcall))
3278 hendricks2 133
#    else
134
#      define __fastcall
2992 helixhorne 135
#    endif
136
#  endif
1435 terminx 137
#endif
138
 
1346 terminx 139
#ifndef TRUE
2992 helixhorne 140
#  define TRUE 1
1346 terminx 141
#endif
142
 
143
#ifndef FALSE
2992 helixhorne 144
#  define FALSE 0
1346 terminx 145
#endif
146
 
1592 terminx 147
#define WITHKPLIB
148
 
4658 terminx 149
#include "libdivide.h"
150
 
5 Plagman 151
// Define this to rewrite all 'B' versions to library functions. This
152
// is for platforms which give us a standard sort of C library so we
153
// link directly. Platforms like PalmOS which don't have a standard C
154
// library will need to wrap these functions with suitable emulations.
4747 terminx 155
#define compat_h_macrodef__
5 Plagman 156
 
4766 hendricks2 157
#include <stdarg.h>
158
#include <stddef.h>
5 Plagman 159
 
4747 terminx 160
#ifdef compat_h_macrodef__
4766 hendricks2 161
# include <stdio.h>
162
# include <string.h>
163
# include <stdlib.h>
164
# include <time.h>
5 Plagman 165
# include <fcntl.h>
166
# include <ctype.h>
167
# include <sys/types.h>
168
# include <sys/stat.h>
169
# include <errno.h>
170
# if defined(_WIN32)
171
#  include <io.h>
172
# else
173
#  include <unistd.h>
174
# endif
175
#endif
176
 
2481 helixhorne 177
#include <assert.h>
178
 
5 Plagman 179
#ifdef EFENCE
180
# include <efence.h>
2254 helixhorne 181
#elif defined DMALLOC
182
# include <dmalloc.h>
5 Plagman 183
#endif
184
 
1454 terminx 185
#if defined(_MSC_VER)
4708 helixhorne 186
# include <direct.h>
5 Plagman 187
# define longlong(x) x##i64
4708 helixhorne 188
# if _MSC_VER < 1800
189
#  define inline __inline
190
# endif
5 Plagman 191
#else
192
# define longlong(x) x##ll
193
#endif
194
 
4606 terminx 195
#if defined(__arm__)
4989 terminx 196
# define Bsqrt __builtin_sqrt
197
# define Bsqrtf __builtin_sqrtf
4606 terminx 198
#else
4708 helixhorne 199
# define Bsqrt sqrt
200
# define Bsqrtf sqrtf
4606 terminx 201
#endif
202
 
5 Plagman 203
#ifndef NULL
204
# define NULL ((void *)0)
205
#endif
206
 
2270 helixhorne 207
#if DEBUGGINGAIDS>=2
208
# define DEBUG_MAIN_ARRAYS
209
#endif
210
 
4893 terminx 211
#ifndef DISABLE_INLINING
212
# define EXTERN_INLINE static inline
213
# define EXTERN_INLINE_HEADER static inline
3116 hendricks2 214
#else
4247 hendricks2 215
# define EXTERN_INLINE __fastcall
216
# define EXTERN_INLINE_HEADER extern __fastcall
3116 hendricks2 217
#endif
218
 
4937 helixhorne 219
#ifndef FORCE_INLINE
220
# ifdef _MSC_VER    // Visual Studio
221
#  define FORCE_INLINE static __forceinline
222
# else
223
#  ifdef __GNUC__
224
#    define FORCE_INLINE static inline __attribute__((always_inline))
225
#  else
226
#    define FORCE_INLINE static inline
227
#  endif
228
# endif
229
#endif
230
 
2495 hendricks2 231
#if !defined DEBUG_MAIN_ARRAYS
232
# define HAVE_CLIPSHAPE_FEATURE
233
#endif
234
 
1935 helixhorne 235
// redefined for apple/ppc, which chokes on stderr when linking...
236
#define ERRprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
237
 
5 Plagman 238
#if defined(__linux)
239
# include <endian.h>
240
# if __BYTE_ORDER == __LITTLE_ENDIAN
241
#  define B_LITTLE_ENDIAN 1
242
#  define B_BIG_ENDIAN    0
243
# elif __BYTE_ORDER == __BIG_ENDIAN
244
#  define B_LITTLE_ENDIAN 0
245
#  define B_BIG_ENDIAN    1
246
# endif
247
# define B_ENDIAN_C_INLINE 1
248
 
2916 terminx 249
#elif defined(GEKKO) || defined(__ANDROID__)
2630 helixhorne 250
# define B_LITTLE_ENDIAN 0
251
# define B_BIG_ENDIAN 1
252
# define B_ENDIAN_C_INLINE 1
253
 
2916 terminx 254
#elif defined(__OpenBSD__)
255
# include <machine/endian.h>
256
# if _BYTE_ORDER == _LITTLE_ENDIAN
257
#  define B_LITTLE_ENDIAN 1
258
#  define B_BIG_ENDIAN    0
259
# elif _BYTE_ORDER == _BIG_ENDIAN
260
#  define B_LITTLE_ENDIAN 0
261
#  define B_BIG_ENDIAN    1
262
# endif
263
# define B_SWAP64(x) __swap64(x)
264
# define B_SWAP32(x) __swap32(x)
265
# define B_SWAP16(x) __swap16(x)
266
 
267
#elif defined(__FreeBSD__) || defined(__NetBSD__)
5 Plagman 268
# include <sys/endian.h>
269
# if _BYTE_ORDER == _LITTLE_ENDIAN
270
#  define B_LITTLE_ENDIAN 1
271
#  define B_BIG_ENDIAN    0
272
# elif _BYTE_ORDER == _BIG_ENDIAN
273
#  define B_LITTLE_ENDIAN 0
274
#  define B_BIG_ENDIAN    1
275
# endif
276
# define B_SWAP64(x) __bswap64(x)
277
# define B_SWAP32(x) __bswap32(x)
278
# define B_SWAP16(x) __bswap16(x)
279
 
280
#elif defined(__APPLE__)
4708 helixhorne 281
# if !defined __x86_64__ && defined __GNUC__
1935 helixhorne 282
// PK 20110617: is*() crashes for me in x86 code compiled from 64-bit, and gives link errors on ppc
1905 helixhorne 283
//              This hack patches all occurences.
1907 helixhorne 284
#  define isdigit(ch) ({ int32_t c__dontuse_=ch; c__dontuse_>='0' && c__dontuse_<='9'; })
285
#  define isalpha(ch) ({ int32_t c__dontuse2_=ch; (c__dontuse2_>='A' && c__dontuse2_<='Z') || (c__dontuse2_>='a' && c__dontuse2_<='z'); })
286
#  define isalnum(ch2)  ({ int32_t c2__dontuse_=ch2; isalpha(c2__dontuse_) || isdigit(c2__dontuse_); })
1935 helixhorne 287
#  if defined __BIG_ENDIAN__
288
#    define isspace(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_==' ' || c__dontuse_=='\t' || c__dontuse_=='\n' || c__dontuse_=='\v' || c__dontuse_=='\f' || c__dontuse_=='\r'); })
289
#    define isprint(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_>=0x20 && c__dontuse_<0x7f); })
290
#    undef ERRprintf
291
#    define ERRprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
292
#  endif
1905 helixhorne 293
# endif
5 Plagman 294
# if defined(__LITTLE_ENDIAN__)
295
#  define B_LITTLE_ENDIAN 1
296
#  define B_BIG_ENDIAN    0
297
# elif defined(__BIG_ENDIAN__)
298
#  define B_LITTLE_ENDIAN 0
299
#  define B_BIG_ENDIAN    1
300
# endif
301
# include <libkern/OSByteOrder.h>
302
# define B_SWAP64(x) OSSwapConstInt64(x)
303
# define B_SWAP32(x) OSSwapConstInt32(x)
304
# define B_SWAP16(x) OSSwapConstInt16(x)
305
 
306
#elif defined(__BEOS__)
307
# include <posix/endian.h>
308
# if LITTLE_ENDIAN != 0
309
#  define B_LITTLE_ENDIAN 1
310
#  define B_BIG_ENDIAN    0
311
# elif BIG_ENDIAN != 0
312
#  define B_LITTLE_ENDIAN 0
313
#  define B_BIG_ENDIAN    1
314
# endif
315
# define B_ENDIAN_C_INLINE 1
316
 
317
#elif defined(__QNX__)
318
# if defined __LITTLEENDIAN__
319
#  define B_LITTLE_ENDIAN 1
320
#  define B_BIG_ENDIAN    0
321
# elif defined __BIGENDIAN__
322
#  define B_LITTLE_ENDIAN 0
323
#  define B_BIG_ENDIAN    1
324
# endif
325
# define B_ENDIAN_C_INLINE 1
326
 
327
#elif defined(__sun)
328
# if defined _LITTLE_ENDIAN
329
#  define B_LITTLE_ENDIAN 1
330
#  define B_BIG_ENDIAN    0
331
# elif defined _BIG_ENDIAN
332
#  define B_LITTLE_ENDIAN 0
333
#  define B_BIG_ENDIAN    1
334
# endif
335
# define B_ENDIAN_C_INLINE 1
336
 
337
#elif defined(_WIN32) || defined(SKYOS) || defined(__SYLLABLE__)
338
# define B_LITTLE_ENDIAN 1
339
# define B_BIG_ENDIAN    0
340
# define B_ENDIAN_C_INLINE 1
341
#endif
342
 
343
#if !defined(B_LITTLE_ENDIAN) || !defined(B_BIG_ENDIAN)
344
# error Unknown endianness
345
#endif
346
 
4893 terminx 347
#if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ ||       \
348
__WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 ||                   \
349
defined __x86_64__ || defined __amd64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__
350
 
4274 hendricks2 351
# define BITNESS64
4893 terminx 352
 
4274 hendricks2 353
#endif
354
 
4766 hendricks2 355
#ifdef __cplusplus
285 terminx 356
 
357
# ifndef SCREWED_UP_CPP
1124 terminx 358
//   using namespace std;
285 terminx 359
# endif
360
 
5 Plagman 361
extern "C" {
362
#endif
363
 
364
#if defined B_ENDIAN_X86_INLINE
365
# if defined(_MSC_VER)
366
        // inline asm using bswap/xchg
367
# elif defined(__GNUC__)
368
        // inline asm using bswap/xchg
369
# endif
370
#elif defined B_ENDIAN_C_INLINE
4743 terminx 371
FORCE_INLINE uint16_t B_SWAP16(uint16_t s) { return (s >> 8) | (s << 8); }
372
FORCE_INLINE uint32_t B_SWAP32(uint32_t l)
4716 terminx 373
{
374
    return ((l >> 8) & 0xff00) | ((l & 0xff00) << 8) | (l << 24) | (l >> 24);
375
}
4743 terminx 376
FORCE_INLINE uint64_t B_SWAP64(uint64_t l)
4716 terminx 377
{
378
    return (l >> 56) | ((l >> 40) & 0xff00) | ((l >> 24) & 0xff0000) | ((l >> 8) & 0xff000000) |
379
        ((l & 255) << 56) | ((l & 0xff00) << 40) | ((l & 0xff0000) << 24) | ((l & 0xff000000) << 8);
380
}
5 Plagman 381
#endif
382
 
4743 terminx 383
FORCE_INLINE void B_BUF16(uint8_t *buf, uint16_t x)
4322 hendricks2 384
{
385
    buf[0] = (x & 0x00FF);
386
    buf[1] = (x & 0xFF00) >> 8;
387
}
4743 terminx 388
FORCE_INLINE void B_BUF32(uint8_t *buf, uint32_t x)
4322 hendricks2 389
{
390
    buf[0] = (x & 0x000000FF);
391
    buf[1] = (x & 0x0000FF00) >> 8;
392
    buf[2] = (x & 0x00FF0000) >> 16;
393
    buf[3] = (x & 0xFF000000) >> 24;
394
}
4768 hendricks2 395
#if 0
396
// i686-apple-darwin11-llvm-gcc-4.2 complains "integer constant is too large for 'long' type"
4743 terminx 397
FORCE_INLINE void B_BUF64(uint8_t *buf, uint64_t x)
4322 hendricks2 398
{
399
    buf[0] = (x & 0x00000000000000FF);
400
    buf[1] = (x & 0x000000000000FF00) >> 8;
401
    buf[2] = (x & 0x0000000000FF0000) >> 16;
402
    buf[3] = (x & 0x00000000FF000000) >> 24;
403
    buf[4] = (x & 0x000000FF00000000) >> 32;
404
    buf[5] = (x & 0x0000FF0000000000) >> 40;
405
    buf[6] = (x & 0x00FF000000000000) >> 48;
406
    buf[7] = (x & 0xFF00000000000000) >> 56;
407
}
4768 hendricks2 408
#endif
4322 hendricks2 409
 
4743 terminx 410
FORCE_INLINE uint16_t B_UNBUF16(const uint8_t *buf) { return (buf[1] << 8) | (buf[0]); }
411
FORCE_INLINE uint32_t B_UNBUF32(const uint8_t *buf)
4716 terminx 412
{
413
    return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
414
}
4743 terminx 415
FORCE_INLINE uint64_t B_UNBUF64(const uint8_t *buf)
4716 terminx 416
{
417
    return ((uint64_t)buf[7] << 56) | ((uint64_t)buf[6] << 48) | ((uint64_t)buf[5] << 40) |
418
        ((uint64_t)buf[4] << 32) | (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
419
}
4322 hendricks2 420
 
5005 hendricks2 421
#if defined BITNESS64 && defined __SSE2__
4658 terminx 422
#include <emmintrin.h>
4743 terminx 423
FORCE_INLINE int32_t Blrintf(const float x)
1454 terminx 424
{
4658 terminx 425
    __m128 xx = _mm_load_ss(&x);
426
    return _mm_cvtss_si32(xx);
1454 terminx 427
}
4658 terminx 428
#elif defined (_MSC_VER)
4743 terminx 429
FORCE_INLINE int32_t Blrintf(const float x)
1454 terminx 430
{
4658 terminx 431
    int n;
432
    __asm fld x;
433
    __asm fistp n;
434
    return n;
1454 terminx 435
}
436
#else
4685 hendricks2 437
#include <math.h>
4658 terminx 438
#define Blrintf lrintf
1454 terminx 439
#endif
440
 
5 Plagman 441
#if B_LITTLE_ENDIAN == 1
442
# define B_LITTLE64(x) (x)
443
# define B_BIG64(x)    B_SWAP64(x)
444
# define B_LITTLE32(x) (x)
445
# define B_BIG32(x)    B_SWAP32(x)
446
# define B_LITTLE16(x) (x)
447
# define B_BIG16(x)    B_SWAP16(x)
448
#elif B_BIG_ENDIAN == 1
449
# define B_LITTLE64(x) B_SWAP64(x)
450
# define B_BIG64(x)    (x)
451
# define B_LITTLE32(x) B_SWAP32(x)
452
# define B_BIG32(x)    (x)
453
# define B_LITTLE16(x) B_SWAP16(x)
454
# define B_BIG16(x)    (x)
455
#endif
456
 
457
#ifndef FP_OFF
714 qbix79 458
# define FP_OFF(__p) ((uintptr_t)(__p))
5 Plagman 459
#endif
460
 
4747 terminx 461
#ifdef compat_h_macrodef__
5 Plagman 462
 
463
# ifndef O_BINARY
464
#  define O_BINARY 0
465
# endif
466
# ifndef O_TEXT
467
#  define O_TEXT 0
468
# endif
469
 
470
# ifndef F_OK
471
#  define F_OK 0
472
# endif
473
 
474
# define BO_BINARY O_BINARY
475
# define BO_TEXT   O_TEXT
476
# define BO_RDONLY O_RDONLY
477
# define BO_WRONLY O_WRONLY
478
# define BO_RDWR   O_RDWR
479
# define BO_APPEND O_APPEND
480
# define BO_CREAT  O_CREAT
481
# define BO_TRUNC  O_TRUNC
482
# define BS_IRGRP  S_IRGRP
483
# define BS_IWGRP  S_IWGRP
484
# define BS_IEXEC  S_IEXEC
2916 terminx 485
# ifdef __ANDROID__
486
#  define BS_IWRITE S_IWUSR
487
#  define BS_IREAD  S_IRUSR
488
# else
489
#  define BS_IWRITE S_IWRITE
490
#  define BS_IREAD  S_IREAD
491
# endif
5 Plagman 492
# define BS_IFIFO  S_IFIFO
493
# define BS_IFCHR  S_IFCHR
494
# define BS_IFBLK  S_IFBLK
495
# define BS_IFDIR  S_IFDIR
496
# define BS_IFREG  S_IFREG
497
# define BSEEK_SET SEEK_SET
498
# define BSEEK_CUR SEEK_CUR
499
# define BSEEK_END SEEK_END
500
#else
501
# define BO_BINARY 0
502
# define BO_TEXT   1
503
# define BO_RDONLY 2
504
# define BO_WRONLY 4
505
# define BO_RDWR   6
506
# define BO_APPEND 8
507
# define BO_CREAT  16
508
# define BO_TRUNC  32
509
# define BS_IRGRP  0
510
# define BS_IWGRP  0
511
# define BS_IEXEC  1
512
# define BS_IWRITE 2
513
# define BS_IREAD  4
514
# define BS_IFIFO  0x1000
515
# define BS_IFCHR  0x2000
516
# define BS_IFBLK  0x3000
517
# define BS_IFDIR  0x4000
518
# define BS_IFREG  0x8000
519
# define BSEEK_SET 0
520
# define BSEEK_CUR 1
521
# define BSEEK_END 2
522
#endif
523
 
524
#ifdef UNDERSCORES
525
# define ASMSYM(x) "_" x
526
#else
527
# define ASMSYM(x) x
528
#endif
529
 
530
#ifndef min
4716 terminx 531
# define min(a, b) (((a) < (b)) ? (a) : (b))
5 Plagman 532
#endif
533
 
534
#ifndef max
4716 terminx 535
# define max(a, b) (((a) > (b)) ? (a) : (b))
5 Plagman 536
#endif
537
 
1854 helixhorne 538
#if __GNUC__ >= 4
4743 terminx 539
# define CLAMP_DECL FORCE_INLINE __attribute__((warn_unused_result))
1854 helixhorne 540
#else
4743 terminx 541
# define CLAMP_DECL FORCE_INLINE
1854 helixhorne 542
#endif
3780 helixhorne 543
 
544
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
4893 terminx 545
CLAMP_DECL int32_t clamp(int32_t in, int32_t min, int32_t max) { return in <= min ? min : (in >= max ? max : in); }
5 Plagman 546
 
3780 helixhorne 547
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
4893 terminx 548
CLAMP_DECL int32_t clamp2(int32_t in, int32_t min, int32_t max) { return in >= max ? max : (in <= min ? min : in); }
3780 helixhorne 549
 
4431 terminx 550
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
4893 terminx 551
CLAMP_DECL float fclamp(float in, float min, float max) { return in <= min ? min : (in >= max ? max : in); }
4431 terminx 552
 
553
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
4893 terminx 554
CLAMP_DECL float fclamp2(float in, float min, float max) { return in >= max ? max : (in <= min ? min : in); }
4431 terminx 555
 
1457 terminx 556
#define BMAX_PATH 256
5 Plagman 557
 
558
 
4716 terminx 559
struct Bdirent
560
{
561
    uint16_t namlen;
562
    char *name;
563
    uint32_t mode;
564
    uint32_t size;
565
    uint32_t mtime;
5 Plagman 566
};
567
typedef void BDIR;
568
 
4716 terminx 569
BDIR *Bopendir(const char *name);
570
struct Bdirent *Breaddir(BDIR *dir);
571
int32_t Bclosedir(BDIR *dir);
5 Plagman 572
 
3411 terminx 573
#ifdef _MSC_VER
574
typedef intptr_t ssize_t;
575
#endif
5 Plagman 576
 
4747 terminx 577
#ifdef compat_h_macrodef__
285 terminx 578
  typedef FILE BFILE;
5 Plagman 579
# define bsize_t size_t
580
# define bssize_t ssize_t
581
#else
285 terminx 582
  typedef void          BFILE;
1205 terminx 583
  typedef uint32_t bsize_t;
1346 terminx 584
  typedef int32_t bssize_t;
5 Plagman 585
#endif
586
 
4853 hendricks2 587
 
588
typedef struct {
589
    int32_t x, y;
590
} vec2_t;
591
 
592
typedef struct {
593
    int32_t x, y, z;
594
} vec3_t;
595
 
596
typedef struct {
597
    float x, y;
598
} vec2f_t;
599
 
600
typedef struct {
601
    float x, y, z;
602
} vec3f_t;
603
 
604
 
2105 helixhorne 605
#if RAND_MAX == 32767
4743 terminx 606
FORCE_INLINE uint16_t system_15bit_rand(void) { return (uint16_t)rand(); }
2105 helixhorne 607
#else  // RAND_MAX > 32767, assumed to be of the form 2^k - 1
4743 terminx 608
FORCE_INLINE uint16_t system_15bit_rand(void) { return ((uint16_t)rand())&0x7fff; }
2105 helixhorne 609
#endif
5 Plagman 610
 
2394 helixhorne 611
#if defined(_MSC_VER)
612
// XXX: non-__compat_h_macrodef__ version?
613
#define strtoll _strtoi64
614
#endif
615
 
4747 terminx 616
#ifdef compat_h_macrodef__
2546 helixhorne 617
# define Bassert assert
5 Plagman 618
# define Brand rand
619
# define Balloca alloca
4708 helixhorne 620
# define Bmalloc malloc
621
# define Bcalloc calloc
622
# define Brealloc realloc
623
# define Bfree free
624
# if defined(__cplusplus) && defined(_MSC_VER)
4697 terminx 625
#  define Bstrdup _strdup
4708 helixhorne 626
#  define Bchdir _chdir
627
#  define Bgetcwd _getcwd
628
# else
2105 helixhorne 629
#  define Bstrdup strdup
4708 helixhorne 630
#  define Bchdir chdir
631
#  define Bgetcwd getcwd
632
# endif
5 Plagman 633
# define Bopen open
634
# define Bclose close
635
# define Bwrite write
636
# define Bread read
637
# define Blseek lseek
638
# if defined(__GNUC__)
639
#  define Btell(h) lseek(h,0,SEEK_CUR)
640
# else
641
#  define Btell tell
642
# endif
3168 helixhorne 643
# ifdef _MSC_VER
4708 helixhorne 644
#  define Bstat stat
645
#  define Bfstat fstat
3168 helixhorne 646
# else
4708 helixhorne 647
#  define Bstat stat
648
#  define Bfstat fstat
3168 helixhorne 649
# endif
3163 hendricks2 650
# define Bfileno fileno
651
# define Bferror ferror
5 Plagman 652
# define Bfopen fopen
653
# define Bfclose fclose
2725 hendricks2 654
# define Bfflush fflush
5 Plagman 655
# define Bfeof feof
656
# define Bfgetc fgetc
657
# define Brewind rewind
658
# define Bfgets fgets
659
# define Bfputc fputc
660
# define Bfputs fputs
661
# define Bfread fread
662
# define Bfwrite fwrite
663
# define Bfprintf fprintf
2493 hendricks2 664
# define Bfscanf fscanf
665
# define Bfseek fseek
2521 hendricks2 666
# define Bftell ftell
667
# define Bputs puts
5 Plagman 668
# define Bstrcpy strcpy
669
# define Bstrncpy strncpy
670
# define Bstrcmp strcmp
671
# define Bstrncmp strncmp
4802 hendricks2 672
# if defined(_MSC_VER)
1454 terminx 673
#  define Bstrcasecmp _stricmp
674
#  define Bstrncasecmp _strnicmp
4708 helixhorne 675
# elif defined(__QNX__)
5 Plagman 676
#  define Bstrcasecmp stricmp
677
#  define Bstrncasecmp strnicmp
678
# else
679
#  define Bstrcasecmp strcasecmp
680
#  define Bstrncasecmp strncasecmp
681
# endif
682
# if defined(_WIN32)
683
#  define Bstrlwr strlwr
684
#  define Bstrupr strupr
685
#  define Bmkdir(s,x) mkdir(s)
686
# else
687
#  define Bmkdir mkdir
688
# endif
689
# define Bstrcat strcat
690
# define Bstrncat strncat
691
# define Bstrlen strlen
692
# define Bstrchr strchr
693
# define Bstrrchr strrchr
2412 helixhorne 694
// XXX: different across 32- and 64-bit archs (e.g.
695
// parsing the decimal representation of 0xffffffff,
696
// 4294967295 -- long is signed, so strtol would
697
// return LONG_MAX (== 0x7fffffff on 32-bit archs))
2374 helixhorne 698
# define Batoi(str) ((int32_t)strtol(str, NULL, 10))
699
# define Batol(str) (strtol(str, NULL, 10))
2521 hendricks2 700
# define Batof(str) (strtod(str, NULL))
5 Plagman 701
# define Bstrtol strtol
702
# define Bstrtoul strtoul
703
# define Bstrtod strtod
1763 terminx 704
# define Bstrstr strstr
3833 hendricks2 705
# define Bislower islower
706
# define Bisupper isupper
707
# define Bisdigit isdigit
5 Plagman 708
# define Btoupper toupper
709
# define Btolower tolower
710
# define Bmemcpy memcpy
711
# define Bmemmove memmove
712
# define Bmemchr memchr
713
# define Bmemset memset
714
# define Bmemcmp memcmp
2521 hendricks2 715
# define Bscanf scanf
5 Plagman 716
# define Bprintf printf
2521 hendricks2 717
# define Bsscanf sscanf
5 Plagman 718
# define Bsprintf sprintf
719
# ifdef _MSC_VER
720
#  define Bsnprintf _snprintf
721
#  define Bvsnprintf _vsnprintf
722
# else
723
#  define Bsnprintf snprintf
724
#  define Bvsnprintf vsnprintf
725
# endif
726
# define Bvfprintf vfprintf
727
# define Bgetenv getenv
728
# define Btime() time(NULL)
3293 hendricks2 729
# define Butime utime
5 Plagman 730
 
731
#else
732
 
2546 helixhorne 733
void Bassert(int);
1205 terminx 734
int32_t Brand(void);
5 Plagman 735
void *Bmalloc(bsize_t size);
736
void Bfree(void *ptr);
1205 terminx 737
int32_t Bopen(const char *pathname, int32_t flags, unsigned mode);
738
int32_t Bclose(int32_t fd);
739
bssize_t Bwrite(int32_t fd, const void *buf, bsize_t count);
740
bssize_t Bread(int32_t fd, void *buf, bsize_t count);
741
int32_t Blseek(int32_t fildes, int32_t offset, int32_t whence);
5 Plagman 742
BFILE *Bfopen(const char *path, const char *mode);
1205 terminx 743
int32_t Bfclose(BFILE *stream);
744
int32_t Bfeof(BFILE *stream);
745
int32_t Bfgetc(BFILE *stream);
5 Plagman 746
void Brewind(BFILE *stream);
1205 terminx 747
char *Bfgets(char *s, int32_t size, BFILE *stream);
748
int32_t Bfputc(int32_t c, BFILE *stream);
749
int32_t Bfputs(const char *s, BFILE *stream);
5 Plagman 750
bsize_t Bfread(void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
751
bsize_t Bfwrite(const void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
752
char *Bstrdup(const char *s);
753
char *Bstrcpy(char *dest, const char *src);
754
char *Bstrncpy(char *dest, const char *src, bsize_t n);
1205 terminx 755
int32_t Bstrcmp(const char *s1, const char *s2);
756
int32_t Bstrncmp(const char *s1, const char *s2, bsize_t n);
757
int32_t Bstrcasecmp(const char *s1, const char *s2);
758
int32_t Bstrncasecmp(const char *s1, const char *s2, bsize_t n);
5 Plagman 759
char *Bstrcat(char *dest, const char *src);
760
char *Bstrncat(char *dest, const char *src, bsize_t n);
761
bsize_t Bstrlen(const char *s);
1205 terminx 762
char *Bstrchr(const char *s, int32_t c);
763
char *Bstrrchr(const char *s, int32_t c);
764
int32_t Batoi(const char *nptr);
765
int32_t Batol(const char *nptr);
1743 helixhorne 766
int32_t Bstrtol(const char *nptr, char **endptr, int32_t base);
767
uint32_t Bstrtoul(const char *nptr, char **endptr, int32_t base);
5 Plagman 768
void *Bmemcpy(void *dest, const void *src, bsize_t n);
769
void *Bmemmove(void *dest, const void *src, bsize_t n);
1205 terminx 770
void *Bmemchr(const void *s, int32_t c, bsize_t n);
771
void *Bmemset(void *s, int32_t c, bsize_t n);
772
int32_t Bmemcmp(const void *s1, const void *s2, bsize_t n);
4716 terminx 773
int32_t Bprintf(const char *format, ...) ATTRIBUTE((format(printf, 1, 2)));
774
int32_t Bsprintf(char *str, const char *format, ...) ATTRIBUTE((format(printf, 2, 3)));
775
int32_t Bsnprintf(char *str, bsize_t size, const char *format, ...) ATTRIBUTE((format(printf, 3, 4)));
1205 terminx 776
int32_t Bvsnprintf(char *str, bsize_t size, const char *format, va_list ap);
5 Plagman 777
char *Bgetcwd(char *buf, bsize_t size);
778
char *Bgetenv(const char *name);
779
#endif
780
 
781
char *Bgethomedir(void);
4801 hendricks2 782
char *Bgetappdir(void);
1205 terminx 783
uint32_t Bgetsysmemsize(void);
784
int32_t Bcorrectfilename(char *filename, int32_t removefn);
785
int32_t Bcanonicalisefilename(char *filename, int32_t removefn);
5 Plagman 786
char *Bgetsystemdrives(void);
1205 terminx 787
int32_t Bfilelength(int32_t fd);
4893 terminx 788
char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop);
2458 hendricks2 789
char *Bstrtolower(char *str);
4658 terminx 790
#define Bwildmatch wildmatch
5 Plagman 791
 
792
#if !defined(_WIN32)
793
char *Bstrlwr(char *);
794
char *Bstrupr(char *);
795
#endif
796
 
3735 helixhorne 797
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
4743 terminx 798
FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
3213 terminx 799
{
800
    Bstrncpy(dst, src, n);
801
    dst[n-1] = 0;
802
    return dst;
803
}
804
 
3735 helixhorne 805
// Append extension when <outbuf> contains no dot.
3876 helixhorne 806
// <ext> can be like ".mhk" or like "_crash.map", no need to start with a dot.
3735 helixhorne 807
// The ugly name is deliberate: we should be checking the sizes of all buffers!
808
static inline void append_ext_UNSAFE(char *outbuf, const char *ext)
809
{
810
    char *p = Bstrrchr(outbuf,'.');
811
 
812
    if (!p)
813
        Bstrcat(outbuf, ext);
814
    else
3876 helixhorne 815
        Bstrcpy(p, ext);
3735 helixhorne 816
}
817
 
4490 helixhorne 818
#ifdef DEBUGGINGAIDS
819
extern void xalloc_set_location(int32_t line, const char *file, const char *func);
820
#endif
821
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
4619 terminx 822
void handle_memerr(void);
4490 helixhorne 823
 
4743 terminx 824
FORCE_INLINE char *xstrdup(const char *s)
4619 terminx 825
{
826
    char *ptr = Bstrdup(s);
827
    if (ptr == NULL) handle_memerr();
828
    return ptr;
829
}
830
 
4743 terminx 831
FORCE_INLINE void *xmalloc(const bsize_t size)
4619 terminx 832
{
833
    void *ptr = Bmalloc(size);
834
    if (ptr == NULL) handle_memerr();
835
    return ptr;
836
}
837
 
4743 terminx 838
FORCE_INLINE void *xcalloc(const bsize_t nmemb, const bsize_t size)
4619 terminx 839
{
840
    void *ptr = Bcalloc(nmemb, size);
841
    if (ptr == NULL) handle_memerr();
842
    return ptr;
843
}
844
 
4743 terminx 845
FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size)
4619 terminx 846
{
847
    void *newptr = Brealloc(ptr, size);
848
 
849
    // According to the C Standard,
850
    //  - ptr == NULL makes realloc() behave like malloc()
851
    //  - size == 0 make it behave like free() if ptr != NULL
852
    // Since we want to catch an out-of-mem in the first case, this leaves:
853
    if (newptr == NULL && size != 0)
854
        handle_memerr();
855
 
856
    return newptr;
857
}
858
 
4743 terminx 859
FORCE_INLINE void *xaligned_malloc(const bsize_t alignment, const bsize_t size)
4695 terminx 860
{
861
#ifdef _WIN32
862
    void *ptr = _aligned_malloc(size, alignment);
4762 hendricks2 863
#elif defined __APPLE__
864
    void *ptr = NULL;
865
    posix_memalign(&ptr, alignment, size);
4695 terminx 866
#else
867
    void *ptr = memalign(alignment, size);
868
#endif
4619 terminx 869
 
4695 terminx 870
    if (ptr == NULL) handle_memerr();
871
    return ptr;
872
}
873
 
874
 
4766 hendricks2 875
#ifdef __cplusplus
5 Plagman 876
}
877
#endif
878
 
4442 terminx 879
#define DO_FREE_AND_NULL(var) do { \
880
    if (var != NULL) { Bfree(var); var = NULL; } \
881
} while (0)
882
 
4695 terminx 883
#define ALIGNED_FREE_AND_NULL(var) do { \
884
    if (var != NULL) { Baligned_free(var); var = NULL; } \
885
} while (0)
886
 
2995 helixhorne 887
#define MAYBE_FCLOSE_AND_NULL(fileptr) do { \
888
    if (fileptr) { Bfclose(fileptr); fileptr=NULL; } \
889
} while (0)
890
 
3644 helixhorne 891
/* Static assertions, based on source found in LuaJIT's src/lj_def.h. */
892
#define EDUKE32_ASSERT_NAME2(name, line) name ## line
893
#define EDUKE32_ASSERT_NAME(line) EDUKE32_ASSERT_NAME2(eduke32_assert_, line)
894
#ifdef __COUNTER__
895
# define EDUKE32_STATIC_ASSERT(cond) \
896
    extern void EDUKE32_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
897
#else
898
# define EDUKE32_STATIC_ASSERT(cond) \
899
    extern void EDUKE32_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
900
#endif
901
 
4199 helixhorne 902
#define ARRAY_SIZE(Ar) (sizeof(Ar)/sizeof((Ar)[0]))
903
#define ARRAY_SSIZE(Ar) (bssize_t)ARRAY_SIZE(Ar)
904
 
4490 helixhorne 905
////////// PANICKING ALLOCATION MACROS (wrapping the functions) //////////
906
#ifdef DEBUGGINGAIDS
907
// Detection of __func__ or equivalent functionality, found in SDL_assert.h
908
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
909
#  define EDUKE32_FUNCTION __func__
910
# elif ((__GNUC__ >= 2) || defined(_MSC_VER))
911
#  define EDUKE32_FUNCTION __FUNCTION__
912
# else
913
#  define EDUKE32_FUNCTION "???"
914
# endif
915
 
916
# define EDUKE32_PRE_XALLLOC xalloc_set_location(__LINE__, __FILE__, EDUKE32_FUNCTION)
917
# define Xstrdup(s) (EDUKE32_PRE_XALLLOC, xstrdup(s))
918
# define Xmalloc(size) (EDUKE32_PRE_XALLLOC, xmalloc(size))
919
# define Xcalloc(nmemb, size) (EDUKE32_PRE_XALLLOC, xcalloc(nmemb, size))
920
# define Xrealloc(ptr, size) (EDUKE32_PRE_XALLLOC, xrealloc(ptr, size))
4695 terminx 921
# define Xaligned_alloc(size, alignment) (EDUKE32_PRE_XALLLOC, xaligned_malloc(size, alignment))
4502 hendricks2 922
# define Bexit(status) do { initprintf("exit(%d) at %s:%d in %s()\n", status, __FILE__, __LINE__, EDUKE32_FUNCTION); exit(status); } while (0)
4490 helixhorne 923
#else
924
# define Xstrdup xstrdup
925
# define Xmalloc xmalloc
926
# define Xcalloc xcalloc
927
# define Xrealloc xrealloc
4736 helixhorne 928
# define Xaligned_alloc xaligned_malloc
4502 hendricks2 929
# define Bexit exit
4490 helixhorne 930
#endif
4695 terminx 931
 
932
#ifdef _WIN32
933
# define Baligned_free(ptr) _aligned_free(ptr)
934
#else
935
# define Baligned_free(ptr) Bfree(ptr)
936
#endif
937
 
4490 helixhorne 938
//////////
939
 
4747 terminx 940
#endif // compat_h_