all repos — mgba @ aead90a2c14949fbf063a8ef210867a949f9e437

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