all repos — mgba @ 396d097db44c732eb3188b181a475abdae60c137

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