all repos — mgba @ 2e43210eac6e6b798180d480cd3c876939b840fb

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#else
 36#include <strings.h>
 37#include <unistd.h>
 38#endif
 39
 40#ifndef SSIZE_MAX
 41#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
 42#endif
 43
 44#define UNUSED(V) (void)(V)
 45
 46#ifndef M_PI
 47#define M_PI 3.141592654f
 48#endif
 49
 50#if defined(__PPC__) || defined(__POWERPC__)
 51#define LOAD_32LE(DEST, ADDR, ARR) { \
 52	uint32_t _addr = (ADDR); \
 53	void* _ptr = (ARR); \
 54	__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 55}
 56
 57#define LOAD_16LE(DEST, ADDR, ARR) { \
 58	uint32_t _addr = (ADDR); \
 59	void* _ptr = (ARR); \
 60	__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 61}
 62
 63#define STORE_32LE(SRC, ADDR, ARR) { \
 64	uint32_t _addr = (ADDR); \
 65	void* _ptr = (ARR); \
 66	__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 67}
 68
 69#define STORE_16LE(SRC, ADDR, ARR) { \
 70	uint32_t _addr = (ADDR); \
 71	void* _ptr = (ARR); \
 72	__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 73}
 74#else
 75#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
 76#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
 77#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
 78#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
 79#endif
 80
 81#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
 82#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
 83#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
 84#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
 85#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
 86#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
 87
 88#ifdef _MSC_VER
 89#define ATTRIBUTE_UNUSED
 90#define ATTRIBUTE_FORMAT(X, Y, Z)
 91#else
 92#define ATTRIBUTE_UNUSED __attribute__((unused))
 93#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
 94#endif
 95
 96#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
 97
 98#define DECL_BITS(TYPE, FIELD, START, SIZE) \
 99	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
100		return CHECK_BITS(src, (START), (START) + (SIZE)); \
101	} \
102	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
103		return EXT_BITS(src, (START), (START) + (SIZE)); \
104	} \
105	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
106		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
107	} \
108	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
109		return FILL_BITS(src, (START), (START) + (SIZE)); \
110	} \
111	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
112		return INS_BITS(src, (START), (START) + (SIZE), bits); \
113	}
114
115#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
116
117#ifndef _MSC_VER
118#define LIKELY(X) __builtin_expect(!!(X), 1)
119#define UNLIKELY(X) __builtin_expect(!!(X), 0)
120#else
121#define LIKELY(X) (!!(X))
122#define UNLIKELY(X) (!!(X))
123#endif
124
125#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
126
127#endif