all repos — mgba @ c8028e1a60749fe27fea50ae4fc0a2fc7a6b6c2d

mGBA Game Boy Advance Emulator

include/mgba-util/common.h (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#ifndef COMMON_H
  7#define COMMON_H
  8
  9#ifdef __cplusplus
 10#define CXX_GUARD_START extern "C" {
 11#define CXX_GUARD_END }
 12#else
 13#define CXX_GUARD_START
 14#define CXX_GUARD_END
 15#endif
 16
 17#ifdef __MINGW32__
 18#define __USE_MINGW_ANSI_STDIO 1
 19#endif
 20
 21CXX_GUARD_START
 22
 23#include <ctype.h>
 24#include <fcntl.h>
 25#include <inttypes.h>
 26#include <limits.h>
 27#include <math.h>
 28#include <stdarg.h>
 29#include <stdbool.h>
 30#include <stddef.h>
 31#include <stdint.h>
 32#include <stdio.h>
 33#include <stdlib.h>
 34#include <string.h>
 35#include <time.h>
 36
 37#ifdef _WIN32
 38// WinSock2 gets very angry if it's included too late
 39#include <winsock2.h>
 40#endif
 41
 42#if defined(_MSC_VER) || defined(__cplusplus)
 43#define restrict __restrict
 44#endif
 45
 46#ifdef _MSC_VER
 47#include <Windows.h>
 48#include <sys/types.h>
 49typedef intptr_t ssize_t;
 50#define PATH_MAX MAX_PATH
 51#define strcasecmp _stricmp
 52#define strncasecmp _strnicmp
 53#define ftruncate _chsize
 54#define snprintf _snprintf
 55#define strdup _strdup
 56#define lseek _lseek
 57#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
 58#elif defined(__wii__)
 59#include <sys/time.h>
 60typedef intptr_t ssize_t;
 61#else
 62#include <strings.h>
 63#include <unistd.h>
 64#include <sys/time.h>
 65#endif
 66
 67#ifdef PSP2
 68// For PATH_MAX on modern toolchains
 69#include <sys/syslimits.h>
 70#endif
 71
 72#include <mgba-util/dllexports.h>
 73
 74#ifndef SSIZE_MAX
 75#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
 76#endif
 77
 78#ifndef UNUSED
 79#define UNUSED(V) (void)(V)
 80#endif
 81
 82#ifndef M_PI
 83#define M_PI 3.141592654f
 84#endif
 85
 86#if !defined(_MSC_VER) && (defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
 87#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
 88#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
 89#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
 90#define ATOMIC_SUB(DST, OP) __atomic_sub_fetch(&DST, OP, __ATOMIC_RELEASE)
 91#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
 92#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
 93#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
 94#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
 95#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
 96#elif defined _MSC_VER
 97#define ATOMIC_STORE(DST, SRC) InterlockedExchange(&DST, SRC)
 98#define ATOMIC_LOAD(DST, SRC) DST = InterlockedOrAcquire(&SRC, 0)
 99#define ATOMIC_ADD(DST, OP) InterlockedAddRelease(&DST, OP)
100#define ATOMIC_SUB(DST, OP) InterlockedAddRelease(&DST, -OP)
101#define ATOMIC_OR(DST, OP) InterlockedOrRelease(&DST, OP)
102#define ATOMIC_AND(DST, OP) InterlockedAndRelease(&DST, OP)
103#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) (InterlockedCompareExchange(&DST, SRC, EXPECTED) == EXPECTED)
104#define ATOMIC_STORE_PTR(DST, SRC) InterlockedExchangePointer(&DST, SRC)
105#define ATOMIC_LOAD_PTR(DST, SRC) DST = InterlockedCompareExchangePointer(&SRC, 0, 0)
106#else
107// TODO
108#define ATOMIC_STORE(DST, SRC) DST = SRC
109#define ATOMIC_LOAD(DST, SRC) DST = SRC
110#define ATOMIC_ADD(DST, OP) DST += OP
111#define ATOMIC_SUB(DST, OP) DST -= OP
112#define ATOMIC_OR(DST, OP) DST |= OP
113#define ATOMIC_AND(DST, OP) DST &= OP
114#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
115#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
116#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
117#endif
118
119#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
120// newlib doesn't support %z properly by default
121#define PRIz ""
122#elif defined(_MSC_VER)
123#define PRIz "I"
124#else
125#define PRIz "z"
126#endif
127
128#if defined __BIG_ENDIAN__
129#define LOAD_64BE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
130#define LOAD_32BE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
131#define LOAD_16BE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
132#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
133#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
134#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
135#if defined(__PPC__) || defined(__POWERPC__)
136#define LOAD_32LE(DEST, ADDR, ARR) { \
137	size_t _addr = (ADDR); \
138	const void* _ptr = (ARR); \
139	__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
140}
141
142#define LOAD_16LE(DEST, ADDR, ARR) { \
143	size_t _addr = (ADDR); \
144	const void* _ptr = (ARR); \
145	__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
146}
147
148#define STORE_32LE(SRC, ADDR, ARR) { \
149	size_t _addr = (ADDR); \
150	void* _ptr = (ARR); \
151	__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
152}
153
154#define STORE_16LE(SRC, ADDR, ARR) { \
155	size_t _addr = (ADDR); \
156	void* _ptr = (ARR); \
157	__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
158}
159
160#ifndef _ARCH_PWR7
161#define LOAD_64LE(DEST, ADDR, ARR) { \
162	size_t _addr = (ADDR); \
163	union { \
164		struct { \
165			uint32_t hi; \
166			uint32_t lo; \
167		}; \
168		uint64_t b64; \
169	} bswap; \
170	const void* _ptr = (ARR); \
171	__asm__( \
172		"lwbrx %0, %2, %3 \n" \
173		"lwbrx %1, %2, %4 \n" \
174		: "=&r"(bswap.lo), "=&r"(bswap.hi) : "b"(_ptr), "r"(_addr), "r"(_addr + 4)) ; \
175	DEST = bswap.b64; \
176}
177
178#define STORE_64LE(SRC, ADDR, ARR) { \
179	size_t _addr = (ADDR); \
180	union { \
181		struct { \
182			uint32_t hi; \
183			uint32_t lo; \
184		}; \
185		uint64_t b64; \
186	} bswap = { .b64 = SRC }; \
187	const void* _ptr = (ARR); \
188	__asm__( \
189		"stwbrx %0, %2, %3 \n" \
190		"stwbrx %1, %2, %4 \n" \
191		: : "r"(bswap.hi), "r"(bswap.lo), "b"(_ptr), "r"(_addr), "r"(_addr + 4) : "memory"); \
192}
193#else
194#define LOAD_64LE(DEST, ADDR, ARR) { \
195	size_t _addr = (ADDR); \
196	const void* _ptr = (ARR); \
197	__asm__("ldbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
198}
199
200#define STORE_64LE(SRC, ADDR, ARR) { \
201	size_t _addr = (ADDR); \
202	void* _ptr = (ARR); \
203	__asm__("stdbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
204}
205#endif
206
207#elif defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
208#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
209#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
210#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
211#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap64(SRC)
212#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap32(SRC)
213#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap16(SRC)
214#else
215#error Big endian build not supported on this platform.
216#endif
217#else
218#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
219#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
220#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
221#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
222#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
223#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
224#ifdef _MSC_VER
225#define LOAD_64BE(DEST, ADDR, ARR) DEST = _byteswap_uint64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
226#define LOAD_32BE(DEST, ADDR, ARR) DEST = _byteswap_ulong(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
227#define LOAD_16BE(DEST, ADDR, ARR) DEST = _byteswap_ushort(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
228#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_uint64(SRC)
229#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_ulong(SRC)
230#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_ushort(SRC)
231#else
232#define LOAD_64BE(DEST, ADDR, ARR) DEST = __builtin_bswap64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
233#define LOAD_32BE(DEST, ADDR, ARR) DEST = __builtin_bswap32(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
234#define LOAD_16BE(DEST, ADDR, ARR) DEST = __builtin_bswap16(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
235#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap64(SRC)
236#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap32(SRC)
237#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap16(SRC)
238#endif
239#endif
240
241#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
242#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
243#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
244#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
245#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
246#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
247#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
248
249#ifdef _MSC_VER
250#pragma section(".CRT$XCU",read)
251#define ATTRIBUTE_UNUSED
252#define ATTRIBUTE_FORMAT(X, Y, Z)
253#define ATTRIBUTE_NOINLINE
254// Adapted from https://stackoverflow.com/a/2390626
255#define _CONSTRUCTOR(FN, PRE) \
256    static void FN(void); \
257    __declspec(allocate(".CRT$XCU")) void (*_CONSTRUCTOR_ ## FN)(void) = FN; \
258    static void FN(void)
259#ifdef _WIN64
260#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "")
261#else
262#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "_")
263#endif
264#else
265#define ATTRIBUTE_UNUSED __attribute__((unused))
266#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
267#define ATTRIBUTE_NOINLINE __attribute__((noinline))
268#define CONSTRUCTOR(FN) static __attribute__((constructor)) void FN(void)
269#endif
270
271#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
272
273#define DECL_BITS(TYPE, FIELD, START, SIZE) \
274	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
275		return CHECK_BITS(src, (START), (START) + (SIZE)); \
276	} \
277	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
278		return EXT_BITS(src, (START), (START) + (SIZE)); \
279	} \
280	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
281		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
282	} \
283	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
284		return FILL_BITS(src, (START), (START) + (SIZE)); \
285	} \
286	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
287		return INS_BITS(src, (START), (START) + (SIZE), bits); \
288	} \
289	ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
290		return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
291	}
292
293#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
294
295#ifndef _MSC_VER
296#define LIKELY(X) __builtin_expect(!!(X), 1)
297#define UNLIKELY(X) __builtin_expect(!!(X), 0)
298#else
299#define LIKELY(X) (!!(X))
300#define UNLIKELY(X) (!!(X))
301#endif
302
303#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
304
305CXX_GUARD_END
306
307#endif