all repos — mgba @ 1844c26388885986ecd7cb7cdbd02453a342a0f8

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