all repos — mgba @ 24c0893cf5ea1f234dee7bcbeaa829e921333049

mGBA Game Boy Advance Emulator

src/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#ifndef PSP2
 10#include <ctype.h>
 11#endif
 12#include <fcntl.h>
 13#include <inttypes.h>
 14#include <limits.h>
 15#include <math.h>
 16#include <stdarg.h>
 17#include <stdbool.h>
 18#include <stddef.h>
 19#include <stdint.h>
 20#include <stdio.h>
 21#include <stdlib.h>
 22#include <string.h>
 23
 24#ifdef _WIN32
 25// WinSock2 gets very angry if it's included too late
 26#include <winsock2.h>
 27#endif
 28#ifdef _MSC_VER
 29#include <Windows.h>
 30#include <sys/types.h>
 31typedef intptr_t ssize_t;
 32#define PATH_MAX MAX_PATH
 33#define restrict __restrict
 34#define strcasecmp _stricmp
 35#define strncasecmp _strnicmp
 36#define ftruncate _chsize
 37#define snprintf _snprintf
 38#define strdup _strdup
 39#define lseek _lseek
 40#elif defined(__wii__)
 41typedef intptr_t ssize_t;
 42#else
 43#include <strings.h>
 44#include <unistd.h>
 45#endif
 46
 47#ifndef SSIZE_MAX
 48#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
 49#endif
 50
 51#define UNUSED(V) (void)(V)
 52
 53#ifndef M_PI
 54#define M_PI 3.141592654f
 55#endif
 56
 57#if defined(__PPC__) || defined(__POWERPC__)
 58#define LOAD_32LE(DEST, ADDR, ARR) { \
 59	uint32_t _addr = (ADDR); \
 60	const void* _ptr = (ARR); \
 61	__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 62}
 63
 64#define LOAD_16LE(DEST, ADDR, ARR) { \
 65	uint32_t _addr = (ADDR); \
 66	const void* _ptr = (ARR); \
 67	__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 68}
 69
 70#define STORE_32LE(SRC, ADDR, ARR) { \
 71	uint32_t _addr = (ADDR); \
 72	void* _ptr = (ARR); \
 73	__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 74}
 75
 76#define STORE_16LE(SRC, ADDR, ARR) { \
 77	uint32_t _addr = (ADDR); \
 78	void* _ptr = (ARR); \
 79	__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 80}
 81
 82#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
 83#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
 84#elif defined __BIG_ENDIAN__
 85#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
 86#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
 87#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
 88#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
 89#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
 90#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
 91#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
 92#else
 93#error Big endian build not supported on this platform.
 94#endif
 95#else
 96#define LOAD_64LE(DEST, ADDR, ARR) DEST = ((uint64_t*) ARR)[(ADDR) >> 3]
 97#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
 98#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
 99#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = SRC
100#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
101#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
102#endif
103
104#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
105#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
106#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
107#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
108#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
109#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
110#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
111
112#ifdef _MSC_VER
113#define ATTRIBUTE_UNUSED
114#define ATTRIBUTE_FORMAT(X, Y, Z)
115#else
116#define ATTRIBUTE_UNUSED __attribute__((unused))
117#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
118#endif
119
120#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
121
122#define DECL_BITS(TYPE, FIELD, START, SIZE) \
123	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
124		return CHECK_BITS(src, (START), (START) + (SIZE)); \
125	} \
126	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
127		return EXT_BITS(src, (START), (START) + (SIZE)); \
128	} \
129	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
130		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
131	} \
132	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
133		return FILL_BITS(src, (START), (START) + (SIZE)); \
134	} \
135	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
136		return INS_BITS(src, (START), (START) + (SIZE), bits); \
137	} \
138	ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
139		return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
140	}
141
142#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
143
144#ifndef _MSC_VER
145#define LIKELY(X) __builtin_expect(!!(X), 1)
146#define UNLIKELY(X) __builtin_expect(!!(X), 0)
147#else
148#define LIKELY(X) (!!(X))
149#define UNLIKELY(X) (!!(X))
150#endif
151
152#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
153
154#endif