all repos — mgba @ b486223532adbc5bb55d9306cf31709d226e4c79

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#ifndef SSIZE_MAX
 60#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
 61#endif
 62
 63#ifndef UNUSED
 64#define UNUSED(V) (void)(V)
 65#endif
 66
 67#ifndef M_PI
 68#define M_PI 3.141592654f
 69#endif
 70
 71#ifndef _MSC_VER
 72#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
 73#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
 74#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
 75#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
 76#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
 77#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
 78#else
 79// TODO
 80#define ATOMIC_STORE(DST, SRC) DST = SRC
 81#define ATOMIC_LOAD(DST, SRC) DST = SRC
 82#define ATOMIC_ADD(DST, OP) DST += OP
 83#define ATOMIC_OR(DST, OP) DST |= OP
 84#define ATOMIC_AND(DST, OP) DST &= OP
 85#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
 86#endif
 87
 88#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
 89// newlib doesn't support %z properly by default
 90#define PRIz ""
 91#elif defined(_WIN64)
 92#define PRIz "ll"
 93#elif defined(_WIN32)
 94#define PRIz ""
 95#else
 96#define PRIz "z"
 97#endif
 98
 99#if defined(__PPC__) || defined(__POWERPC__)
100#define LOAD_32LE(DEST, ADDR, ARR) { \
101	uint32_t _addr = (ADDR); \
102	const void* _ptr = (ARR); \
103	__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
104}
105
106#define LOAD_16LE(DEST, ADDR, ARR) { \
107	uint32_t _addr = (ADDR); \
108	const void* _ptr = (ARR); \
109	__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
110}
111
112#define STORE_32LE(SRC, ADDR, ARR) { \
113	uint32_t _addr = (ADDR); \
114	void* _ptr = (ARR); \
115	__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
116}
117
118#define STORE_16LE(SRC, ADDR, ARR) { \
119	uint32_t _addr = (ADDR); \
120	void* _ptr = (ARR); \
121	__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
122}
123
124#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
125#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
126#elif defined __BIG_ENDIAN__
127#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
128#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
129#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
130#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
131#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
132#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
133#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
134#else
135#error Big endian build not supported on this platform.
136#endif
137#else
138#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
139#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
140#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
141#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
142#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
143#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
144#endif
145
146#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
147#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
148#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
149#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
150#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
151#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
152#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
153
154#ifdef _MSC_VER
155#define ATTRIBUTE_UNUSED
156#define ATTRIBUTE_FORMAT(X, Y, Z)
157#else
158#define ATTRIBUTE_UNUSED __attribute__((unused))
159#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
160#endif
161
162#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
163
164#define DECL_BITS(TYPE, FIELD, START, SIZE) \
165	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
166		return CHECK_BITS(src, (START), (START) + (SIZE)); \
167	} \
168	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
169		return EXT_BITS(src, (START), (START) + (SIZE)); \
170	} \
171	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
172		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
173	} \
174	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
175		return FILL_BITS(src, (START), (START) + (SIZE)); \
176	} \
177	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
178		return INS_BITS(src, (START), (START) + (SIZE), bits); \
179	} \
180	ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
181		return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
182	}
183
184#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
185
186#ifndef _MSC_VER
187#define LIKELY(X) __builtin_expect(!!(X), 1)
188#define UNLIKELY(X) __builtin_expect(!!(X), 0)
189#else
190#define LIKELY(X) (!!(X))
191#define UNLIKELY(X) (!!(X))
192#endif
193
194#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
195
196CXX_GUARD_END
197
198#endif