2006-04-13 20:47:06 +00:00
|
|
|
// Compatibility declarations for things which might not be present in
|
|
|
|
// certain build environments. It also levels the playing field caused
|
|
|
|
// by different platforms.
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#ifndef compat_h_
|
|
|
|
#define compat_h_
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-25 10:56:13 +00:00
|
|
|
#include "xs_Float.h"
|
2020-04-11 21:46:42 +00:00
|
|
|
#include "m_alloc.h"
|
2020-06-20 16:17:49 +00:00
|
|
|
#include "intvec.h"
|
2020-08-30 21:34:40 +00:00
|
|
|
#include "m_swap.h"
|
2020-01-25 10:56:13 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Compiler detection //////////
|
|
|
|
|
2014-10-25 10:17:35 +00:00
|
|
|
#ifdef __GNUC__
|
2014-11-24 08:19:50 +00:00
|
|
|
# define EDUKE32_GCC_PREREQ(major, minor) (major < __GNUC__ || (major == __GNUC__ && minor <= __GNUC_MINOR__))
|
2014-10-25 10:17:35 +00:00
|
|
|
#else
|
|
|
|
# define EDUKE32_GCC_PREREQ(major, minor) 0
|
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
|
2017-04-12 08:30:18 +00:00
|
|
|
////////// Language detection //////////
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Language and compiler feature polyfills //////////
|
|
|
|
|
|
|
|
# define EXTERNC
|
|
|
|
|
2017-02-25 08:15:13 +00:00
|
|
|
#ifndef UNREFERENCED_PARAMETER
|
|
|
|
# define UNREFERENCED_PARAMETER(x) (x) = (x)
|
2014-11-30 23:18:57 +00:00
|
|
|
#endif
|
2014-10-29 17:05:15 +00:00
|
|
|
|
2017-06-27 02:24:14 +00:00
|
|
|
#ifndef UNREFERENCED_CONST_PARAMETER
|
|
|
|
# ifdef _MSC_VER
|
2017-07-29 20:39:50 +00:00
|
|
|
# define UNREFERENCED_CONST_PARAMETER(x) ((void)(x))
|
2017-06-27 02:24:14 +00:00
|
|
|
# else
|
|
|
|
# define UNREFERENCED_CONST_PARAMETER(x)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2010-12-20 16:55:37 +00:00
|
|
|
#if defined __GNUC__ || defined __clang__
|
|
|
|
# define ATTRIBUTE(attrlist) __attribute__(attrlist)
|
|
|
|
#else
|
|
|
|
# define ATTRIBUTE(attrlist)
|
|
|
|
#endif
|
|
|
|
|
2014-11-24 08:19:50 +00:00
|
|
|
|
2018-12-23 23:23:50 +00:00
|
|
|
#ifndef MAY_ALIAS
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define MAY_ALIAS
|
|
|
|
# else
|
|
|
|
# define MAY_ALIAS __attribute__((may_alias))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifndef FORCE_INLINE
|
2017-02-25 08:15:53 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define FORCE_INLINE __forceinline
|
2015-01-25 12:16:10 +00:00
|
|
|
# else
|
2017-02-25 08:15:16 +00:00
|
|
|
# ifdef __GNUC__
|
2017-02-25 08:15:53 +00:00
|
|
|
# define FORCE_INLINE inline __attribute__((always_inline))
|
2017-02-25 08:15:16 +00:00
|
|
|
# else
|
2017-02-25 08:15:53 +00:00
|
|
|
# define FORCE_INLINE inline
|
2014-11-24 08:19:50 +00:00
|
|
|
# endif
|
2014-10-30 04:59:23 +00:00
|
|
|
# endif
|
2008-02-16 22:27:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-23 03:17:29 +00:00
|
|
|
# define fallthrough__ [[fallthrough]]
|
2012-03-18 08:50:41 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Architecture detection //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2006-04-13 20:47:06 +00:00
|
|
|
# define B_BIG_ENDIAN 1
|
2020-08-30 21:34:40 +00:00
|
|
|
#else
|
2006-04-13 20:47:06 +00:00
|
|
|
# define B_BIG_ENDIAN 0
|
|
|
|
#endif
|
|
|
|
|
2015-01-11 04:53:16 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Standard library headers //////////
|
|
|
|
|
|
|
|
#undef __USE_MINGW_ANSI_STDIO // Workaround for MinGW-w64.
|
2015-01-11 04:53:16 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
# define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
#ifndef __STDC_LIMIT_MACROS
|
|
|
|
# define __STDC_LIMIT_MACROS
|
2014-01-27 10:31:39 +00:00
|
|
|
#endif
|
2017-02-25 08:15:20 +00:00
|
|
|
|
2018-03-17 03:26:05 +00:00
|
|
|
#ifndef _USE_MATH_DEFINES
|
|
|
|
# define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
|
2019-04-18 17:24:22 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
2006-08-31 01:54:23 +00:00
|
|
|
|
2017-02-25 08:15:36 +00:00
|
|
|
#include <limits.h>
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <stdarg.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <stddef.h>
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <stdio.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <stdlib.h>
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <string.h>
|
2006-08-31 01:54:23 +00:00
|
|
|
|
2019-07-16 09:35:09 +00:00
|
|
|
#if !(defined _WIN32 && defined __clang__)
|
2017-02-25 08:15:36 +00:00
|
|
|
#include <float.h>
|
2019-07-16 09:35:09 +00:00
|
|
|
#endif
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <math.h>
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <time.h>
|
2015-05-03 07:03:12 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <assert.h>
|
2015-05-03 07:03:48 +00:00
|
|
|
|
2019-07-16 09:35:09 +00:00
|
|
|
# include <limits>
|
2018-10-16 06:09:54 +00:00
|
|
|
# include <algorithm>
|
|
|
|
# include <functional>
|
2017-04-12 08:30:29 +00:00
|
|
|
# include <type_traits>
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// Platform headers //////////
|
|
|
|
|
|
|
|
#if !defined __APPLE__ && (!defined EDUKE32_BSD || !__STDC__)
|
|
|
|
# include <malloc.h>
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-11 21:50:43 +00:00
|
|
|
#include "engineerrors.h"
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// DEPRECATED: Standard library prefixing //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
typedef intptr_t ssize_t;
|
2017-06-24 21:18:12 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
typedef size_t bsize_t;
|
|
|
|
typedef ssize_t bssize_t;
|
|
|
|
|
|
|
|
#define BMAX_PATH 256
|
|
|
|
|
2017-04-12 08:30:29 +00:00
|
|
|
////////// Metaprogramming structs //////////
|
|
|
|
|
|
|
|
using std::enable_if_t;
|
2020-08-30 21:34:40 +00:00
|
|
|
using native_t = intptr_t;
|
2017-04-12 08:30:32 +00:00
|
|
|
|
2018-12-23 23:23:50 +00:00
|
|
|
typedef struct MAY_ALIAS {
|
2014-12-27 18:36:43 +00:00
|
|
|
int32_t x, y;
|
|
|
|
} vec2_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float x, y;
|
|
|
|
} vec2f_t;
|
|
|
|
|
2019-03-02 23:21:52 +00:00
|
|
|
typedef struct {
|
|
|
|
double x, y;
|
|
|
|
} vec2d_t;
|
|
|
|
|
2019-06-25 11:28:04 +00:00
|
|
|
typedef struct MAY_ALIAS {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct { int32_t x, y, z; };
|
|
|
|
vec2_t vec2;
|
|
|
|
};
|
2019-06-25 11:28:04 +00:00
|
|
|
} vec3_t;
|
|
|
|
|
|
|
|
typedef struct MAY_ALIAS {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct { int16_t x, y, z; };
|
|
|
|
vec2_16_t vec2;
|
|
|
|
};
|
2019-06-25 11:28:04 +00:00
|
|
|
} vec3_16_t;
|
|
|
|
|
2014-12-27 18:36:43 +00:00
|
|
|
typedef struct {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
union { float x, d; };
|
|
|
|
union { float y, u; };
|
|
|
|
union { float z, v; };
|
|
|
|
};
|
|
|
|
vec2f_t vec2;
|
|
|
|
};
|
2014-12-27 18:36:43 +00:00
|
|
|
} vec3f_t;
|
|
|
|
|
2020-09-04 19:24:48 +00:00
|
|
|
static_assert(sizeof(vec3f_t) == sizeof(float) * 3);
|
2015-03-24 00:40:33 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union { double x; double d; };
|
|
|
|
union { double y; double u; };
|
|
|
|
union { double z; double v; };
|
|
|
|
} vec3d_t;
|
|
|
|
|
2020-09-04 19:24:48 +00:00
|
|
|
static_assert(sizeof(vec3d_t) == sizeof(double) * 3);
|
2014-12-27 18:36:43 +00:00
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:16:01 +00:00
|
|
|
////////// Language tricks that depend on size_t //////////
|
|
|
|
|
2019-12-29 15:35:51 +00:00
|
|
|
#include "basics.h"
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Pointer management //////////
|
|
|
|
|
|
|
|
#define DO_FREE_AND_NULL(var) do { \
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(var); (var) = NULL; \
|
2017-02-25 08:15:16 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
////////// Data serialization //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
inline int32_t B_LITTLE32(int32_t val) { return LittleLong(val); }
|
|
|
|
inline uint32_t B_LITTLE32(uint32_t val) { return LittleLong(val); }
|
|
|
|
inline int32_t B_LITTLE16(int16_t val) { return LittleShort(val); }
|
|
|
|
inline uint32_t B_LITTLE16(uint16_t val) { return LittleShort(val); }
|
2019-04-08 06:27:48 +00:00
|
|
|
|
2017-02-25 08:15:53 +00:00
|
|
|
static FORCE_INLINE void B_BUF32(void * const buf, uint32_t const x) { *(uint32_t *) buf = x; }
|
2020-09-04 19:24:48 +00:00
|
|
|
static FORCE_INLINE uint32_t B_UNBUF32(void const * const buf) { return *(uint32_t const *) buf; }
|
|
|
|
static FORCE_INLINE uint16_t B_UNBUF16(void const * const buf) { return *(uint16_t const *) buf; }
|
2020-08-30 21:34:40 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
////////// Abstract data operations //////////
|
|
|
|
|
2020-09-04 19:24:48 +00:00
|
|
|
template <typename T, typename X, typename Y> constexpr T clamp(T in, X min, Y max) { return in <= (T) min ? (T) min : (in >= (T) max ? (T) max : in); }
|
2018-10-16 06:09:54 +00:00
|
|
|
using std::min;
|
|
|
|
using std::max;
|
2017-02-25 08:15:16 +00:00
|
|
|
|
2019-05-19 03:52:54 +00:00
|
|
|
////////// Bitfield manipulation //////////
|
|
|
|
|
2020-09-04 19:24:48 +00:00
|
|
|
// This once was a static array, requiring a memory acces where a shift would suffice.
|
2019-11-02 09:45:41 +00:00
|
|
|
// Revert the above to a real bit shift through some C++ operator magic. That saves me from reverting all the code that uses this construct.
|
2020-09-04 19:24:48 +00:00
|
|
|
// Note: Only occurs 25 times in the code, should be removed for good.
|
2019-11-13 21:40:08 +00:00
|
|
|
static struct
|
2019-11-02 09:45:41 +00:00
|
|
|
{
|
|
|
|
constexpr uint8_t operator[](int index) const { return 1 << index; };
|
|
|
|
} pow2char;
|
2020-09-04 19:24:48 +00:00
|
|
|
|
2019-08-04 02:51:50 +00:00
|
|
|
|
2020-09-08 16:48:18 +00:00
|
|
|
static FORCE_INLINE void bitmap_set(uint8_t *const ptr, int const n) { ptr[n>>3] |= 1 << (n&7); }
|
|
|
|
static FORCE_INLINE char bitmap_test(uint8_t const *const ptr, int const n) { return ptr[n>>3] & (1 << (n&7)); }
|
2017-04-12 08:30:35 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Utility functions //////////
|
|
|
|
|
2019-05-19 03:52:54 +00:00
|
|
|
// breadth-first search helpers
|
|
|
|
template <typename T>
|
|
|
|
void bfirst_search_init(T *const list, uint8_t *const bitmap, T *const eltnumptr, int const maxelts, int const firstelt)
|
|
|
|
{
|
2020-09-08 16:48:18 +00:00
|
|
|
memset(bitmap, 0, (maxelts+7)>>3);
|
2019-05-19 03:52:54 +00:00
|
|
|
|
|
|
|
list[0] = firstelt;
|
|
|
|
bitmap_set(bitmap, firstelt);
|
|
|
|
*eltnumptr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void bfirst_search_try(T *const list, uint8_t *const bitmap, T *const eltnumptr, int const elt)
|
|
|
|
{
|
|
|
|
if (!bitmap_test(bitmap, elt))
|
|
|
|
{
|
|
|
|
bitmap_set(bitmap, elt);
|
|
|
|
list[(*eltnumptr)++] = elt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// PANICKING ALLOCATION WRAPPERS //////////
|
|
|
|
|
2014-05-30 00:02:16 +00:00
|
|
|
|
2019-12-26 13:43:44 +00:00
|
|
|
#define Xstrdup(s) (strdup(s))
|
2020-04-11 21:46:42 +00:00
|
|
|
#define Xmalloc(size) (M_Malloc(size))
|
|
|
|
#define Xcalloc(nmemb, size) (M_Calloc(nmemb, size))
|
|
|
|
#define Xrealloc(ptr, size) (M_Realloc(ptr, size))
|
|
|
|
#define Xfree(ptr) (M_Free(ptr))
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// Inlined external libraries //////////
|
|
|
|
|
|
|
|
/* End dependence on compat.o object. */
|
|
|
|
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#endif // compat_h_
|