2014-12-03 08:39:06 +00:00
|
|
|
/* Copyright (c) 2013-2014 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-10-12 01:18:47 +00:00
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#define CXX_GUARD_START extern "C" {
|
|
|
|
#define CXX_GUARD_END }
|
|
|
|
#else
|
|
|
|
#define CXX_GUARD_START
|
|
|
|
#define CXX_GUARD_END
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CXX_GUARD_START
|
|
|
|
|
2014-10-12 01:18:47 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
2014-11-22 08:33:41 +00:00
|
|
|
#include <inttypes.h>
|
2014-10-12 01:18:47 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-01-19 07:53:07 +00:00
|
|
|
#include <time.h>
|
2015-06-07 04:08:13 +00:00
|
|
|
|
2016-04-18 01:03:13 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// WinSock2 gets very angry if it's included too late
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
2019-03-17 02:37:42 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) || defined(__cplusplus)
|
|
|
|
#define restrict __restrict
|
|
|
|
#endif
|
|
|
|
|
2015-06-07 04:08:13 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-04-18 01:03:13 +00:00
|
|
|
#include <Windows.h>
|
2015-07-01 02:08:13 +00:00
|
|
|
#include <sys/types.h>
|
2015-06-18 08:19:33 +00:00
|
|
|
typedef intptr_t ssize_t;
|
2016-04-18 01:03:13 +00:00
|
|
|
#define PATH_MAX MAX_PATH
|
2015-06-07 04:08:13 +00:00
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define ftruncate _chsize
|
2015-07-01 02:08:13 +00:00
|
|
|
#define snprintf _snprintf
|
2016-04-18 01:03:13 +00:00
|
|
|
#define strdup _strdup
|
|
|
|
#define lseek _lseek
|
2017-03-16 19:33:30 +00:00
|
|
|
#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
|
2015-06-16 06:02:58 +00:00
|
|
|
#elif defined(__wii__)
|
2017-01-19 07:57:08 +00:00
|
|
|
#include <sys/time.h>
|
2015-06-18 09:47:41 +00:00
|
|
|
typedef intptr_t ssize_t;
|
2015-06-07 04:08:13 +00:00
|
|
|
#else
|
2015-03-23 09:47:10 +00:00
|
|
|
#include <strings.h>
|
2014-10-12 01:18:47 +00:00
|
|
|
#include <unistd.h>
|
2017-01-18 23:22:41 +00:00
|
|
|
#include <sys/time.h>
|
2015-06-07 04:08:13 +00:00
|
|
|
#endif
|
2015-02-10 10:46:12 +00:00
|
|
|
|
2017-04-17 08:12:23 +00:00
|
|
|
#ifdef PSP2
|
|
|
|
// For PATH_MAX on modern toolchains
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#endif
|
|
|
|
|
2019-06-24 15:30:28 +00:00
|
|
|
#ifndef MGBA_STANDALONE
|
|
|
|
#include <mgba-util/dllexports.h>
|
|
|
|
#else
|
|
|
|
#define MGBA_EXPORT
|
|
|
|
#define MGBA_NO_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2015-06-18 08:19:33 +00:00
|
|
|
#ifndef SSIZE_MAX
|
|
|
|
#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
|
|
|
|
#endif
|
2015-05-15 15:37:08 +00:00
|
|
|
|
2016-12-27 05:02:57 +00:00
|
|
|
#ifndef UNUSED
|
2014-10-12 01:18:47 +00:00
|
|
|
#define UNUSED(V) (void)(V)
|
2016-12-27 05:02:57 +00:00
|
|
|
#endif
|
2014-10-12 01:18:47 +00:00
|
|
|
|
2015-04-21 09:27:54 +00:00
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.141592654f
|
|
|
|
#endif
|
|
|
|
|
2017-12-23 23:37:57 +00:00
|
|
|
#if !defined(_MSC_VER) && (defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
|
2016-07-20 08:45:21 +00:00
|
|
|
#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
|
|
|
|
#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
|
|
|
|
#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
|
2018-12-17 06:14:21 +00:00
|
|
|
#define ATOMIC_SUB(DST, OP) __atomic_sub_fetch(&DST, OP, __ATOMIC_RELEASE)
|
2016-07-20 08:45:21 +00:00
|
|
|
#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
|
|
|
|
#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
|
|
|
|
#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
|
2018-12-17 18:58:35 +00:00
|
|
|
#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
|
|
|
|
#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
#define ATOMIC_STORE(DST, SRC) InterlockedExchange(&DST, SRC)
|
|
|
|
#define ATOMIC_LOAD(DST, SRC) DST = InterlockedOrAcquire(&SRC, 0)
|
|
|
|
#define ATOMIC_ADD(DST, OP) InterlockedAddRelease(&DST, OP)
|
|
|
|
#define ATOMIC_SUB(DST, OP) InterlockedAddRelease(&DST, -OP)
|
|
|
|
#define ATOMIC_OR(DST, OP) InterlockedOrRelease(&DST, OP)
|
|
|
|
#define ATOMIC_AND(DST, OP) InterlockedAndRelease(&DST, OP)
|
|
|
|
#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) (InterlockedCompareExchange(&DST, SRC, EXPECTED) == EXPECTED)
|
2019-06-24 21:02:56 +00:00
|
|
|
#define ATOMIC_STORE_PTR(DST, SRC) InterlockedExchangePointer(&DST, SRC)
|
|
|
|
#define ATOMIC_LOAD_PTR(DST, SRC) DST = InterlockedCompareExchangePointer(&SRC, 0, 0)
|
2016-07-20 08:45:21 +00:00
|
|
|
#else
|
|
|
|
// TODO
|
|
|
|
#define ATOMIC_STORE(DST, SRC) DST = SRC
|
|
|
|
#define ATOMIC_LOAD(DST, SRC) DST = SRC
|
|
|
|
#define ATOMIC_ADD(DST, OP) DST += OP
|
2018-12-17 06:14:21 +00:00
|
|
|
#define ATOMIC_SUB(DST, OP) DST -= OP
|
2016-07-20 08:45:21 +00:00
|
|
|
#define ATOMIC_OR(DST, OP) DST |= OP
|
|
|
|
#define ATOMIC_AND(DST, OP) DST &= OP
|
|
|
|
#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
|
2018-12-17 18:58:35 +00:00
|
|
|
#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
|
|
|
|
#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
|
2016-07-20 08:45:21 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 02:09:20 +00:00
|
|
|
#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
|
2016-09-15 17:25:23 +00:00
|
|
|
// newlib doesn't support %z properly by default
|
|
|
|
#define PRIz ""
|
|
|
|
#elif defined(_WIN64)
|
2020-02-28 05:33:15 +00:00
|
|
|
#define PRIz "I64"
|
2016-09-15 17:25:23 +00:00
|
|
|
#elif defined(_WIN32)
|
2016-09-09 02:09:20 +00:00
|
|
|
#define PRIz ""
|
|
|
|
#else
|
|
|
|
#define PRIz "z"
|
|
|
|
#endif
|
|
|
|
|
2017-12-23 23:37:57 +00:00
|
|
|
#if defined __BIG_ENDIAN__
|
|
|
|
#define LOAD_32BE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
|
2015-05-29 05:50:44 +00:00
|
|
|
#if defined(__PPC__) || defined(__POWERPC__)
|
|
|
|
#define LOAD_32LE(DEST, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
2015-10-15 05:00:36 +00:00
|
|
|
const void* _ptr = (ARR); \
|
2015-06-10 04:22:32 +00:00
|
|
|
__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
|
2015-05-29 05:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_16LE(DEST, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
2015-10-15 05:00:36 +00:00
|
|
|
const void* _ptr = (ARR); \
|
2015-06-10 04:22:32 +00:00
|
|
|
__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
|
2015-05-29 05:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_32LE(SRC, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
2018-05-26 00:33:59 +00:00
|
|
|
__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
|
2015-05-29 05:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_16LE(SRC, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
2018-05-26 00:33:59 +00:00
|
|
|
__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
|
2015-05-29 05:50:44 +00:00
|
|
|
}
|
2015-12-28 08:26:17 +00:00
|
|
|
|
2017-12-23 23:37:57 +00:00
|
|
|
#define LOAD_64LE(DEST, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
union { \
|
|
|
|
struct { \
|
|
|
|
uint32_t hi; \
|
|
|
|
uint32_t lo; \
|
|
|
|
}; \
|
|
|
|
uint64_t b64; \
|
2018-05-26 20:09:13 +00:00
|
|
|
} bswap; \
|
2017-12-23 23:37:57 +00:00
|
|
|
const void* _ptr = (ARR); \
|
|
|
|
__asm__( \
|
|
|
|
"lwbrx %0, %2, %3 \n" \
|
|
|
|
"lwbrx %1, %2, %4 \n" \
|
2018-05-26 20:09:13 +00:00
|
|
|
: "=&r"(bswap.lo), "=&r"(bswap.hi) : "b"(_ptr), "r"(_addr), "r"(_addr + 4)) ; \
|
|
|
|
DEST = bswap.b64; \
|
2017-12-23 23:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_64LE(SRC, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
union { \
|
|
|
|
struct { \
|
|
|
|
uint32_t hi; \
|
|
|
|
uint32_t lo; \
|
|
|
|
}; \
|
|
|
|
uint64_t b64; \
|
2018-05-26 20:09:13 +00:00
|
|
|
} bswap = { .b64 = SRC }; \
|
2017-12-23 23:37:57 +00:00
|
|
|
const void* _ptr = (ARR); \
|
|
|
|
__asm__( \
|
|
|
|
"stwbrx %0, %2, %3 \n" \
|
|
|
|
"stwbrx %1, %2, %4 \n" \
|
2018-05-26 20:09:13 +00:00
|
|
|
: : "r"(bswap.hi), "r"(bswap.lo), "b"(_ptr), "r"(_addr), "r"(_addr + 4) : "memory"); \
|
2017-12-23 23:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
|
2015-12-28 08:26:17 +00:00
|
|
|
#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
|
2015-10-20 06:49:35 +00:00
|
|
|
#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
|
|
|
|
#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
|
2015-12-28 08:26:17 +00:00
|
|
|
#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
|
2015-10-20 06:49:35 +00:00
|
|
|
#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
|
|
|
|
#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
|
|
|
|
#else
|
|
|
|
#error Big endian build not supported on this platform.
|
|
|
|
#endif
|
2015-05-29 05:50:44 +00:00
|
|
|
#else
|
2016-07-30 07:51:45 +00:00
|
|
|
#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
|
|
|
|
#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
|
|
|
|
#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
|
|
|
|
#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
|
|
|
|
#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
|
|
|
|
#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
|
2018-10-02 03:25:55 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define LOAD_32BE(DEST, ADDR, ARR) DEST = _byteswap_ulong(((uint32_t*) ARR)[(ADDR) >> 2])
|
|
|
|
#else
|
2017-12-23 23:37:57 +00:00
|
|
|
#define LOAD_32BE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
|
2015-05-29 05:50:44 +00:00
|
|
|
#endif
|
2018-10-02 03:25:55 +00:00
|
|
|
#endif
|
2015-05-29 05:50:44 +00:00
|
|
|
|
|
|
|
#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
|
|
|
|
#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
|
|
|
|
#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
|
|
|
|
#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
|
|
|
|
#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
|
|
|
|
#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
|
2015-10-15 05:52:03 +00:00
|
|
|
#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
|
2015-05-29 05:50:44 +00:00
|
|
|
|
2015-06-07 04:08:13 +00:00
|
|
|
#ifdef _MSC_VER
|
2019-01-21 05:09:34 +00:00
|
|
|
#pragma section(".CRT$XCU",read)
|
2015-06-07 04:08:13 +00:00
|
|
|
#define ATTRIBUTE_UNUSED
|
|
|
|
#define ATTRIBUTE_FORMAT(X, Y, Z)
|
2018-02-16 06:15:45 +00:00
|
|
|
#define ATTRIBUTE_NOINLINE
|
2019-01-21 05:09:34 +00:00
|
|
|
// Adapted from https://stackoverflow.com/a/2390626
|
|
|
|
#define _CONSTRUCTOR(FN, PRE) \
|
|
|
|
static void FN(void); \
|
|
|
|
__declspec(allocate(".CRT$XCU")) void (*_CONSTRUCTOR_ ## FN)(void) = FN; \
|
|
|
|
static void FN(void)
|
|
|
|
#ifdef _WIN64
|
|
|
|
#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "")
|
|
|
|
#else
|
|
|
|
#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "_")
|
|
|
|
#endif
|
2015-06-07 04:08:13 +00:00
|
|
|
#else
|
|
|
|
#define ATTRIBUTE_UNUSED __attribute__((unused))
|
|
|
|
#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
|
2018-02-16 06:15:45 +00:00
|
|
|
#define ATTRIBUTE_NOINLINE __attribute__((noinline))
|
2019-01-21 05:09:34 +00:00
|
|
|
#define CONSTRUCTOR(FN) static __attribute__((constructor)) void FN(void)
|
2015-06-07 04:08:13 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-29 05:50:44 +00:00
|
|
|
#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
|
|
|
|
|
|
|
|
#define DECL_BITS(TYPE, FIELD, START, SIZE) \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return CHECK_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return EXT_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return CLEAR_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return FILL_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return INS_BITS(src, (START), (START) + (SIZE), bits); \
|
2015-10-15 05:52:03 +00:00
|
|
|
} \
|
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
|
|
|
|
return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
|
2015-05-29 05:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
|
|
|
|
|
2015-06-08 04:20:54 +00:00
|
|
|
#ifndef _MSC_VER
|
2015-05-29 05:50:44 +00:00
|
|
|
#define LIKELY(X) __builtin_expect(!!(X), 1)
|
|
|
|
#define UNLIKELY(X) __builtin_expect(!!(X), 0)
|
2015-06-07 04:08:13 +00:00
|
|
|
#else
|
|
|
|
#define LIKELY(X) (!!(X))
|
|
|
|
#define UNLIKELY(X) (!!(X))
|
|
|
|
#endif
|
2015-05-29 05:50:44 +00:00
|
|
|
|
|
|
|
#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
|
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
CXX_GUARD_END
|
|
|
|
|
2014-10-12 01:18:47 +00:00
|
|
|
#endif
|