all repos — mgba @ e94744d8c761139a6246ae48e9723825c5bd28f7

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#include <ctype.h>
 10#include <fcntl.h>
 11#include <inttypes.h>
 12#include <limits.h>
 13#include <math.h>
 14#include <stdarg.h>
 15#include <stdbool.h>
 16#include <stddef.h>
 17#include <stdint.h>
 18#include <stdio.h>
 19#include <stdlib.h>
 20#include <string.h>
 21
 22#include "version.h"
 23
 24#ifdef _MSC_VER
 25typedef intptr_t off_t;
 26typedef intptr_t ssize_t;
 27#define restrict __restrict
 28#define strcasecmp _stricmp
 29#define strncasecmp _strnicmp
 30#define ftruncate _chsize
 31#elif defined(__wii__)
 32typedef int32_t ssize_t;
 33#define SSIZE_MAX ((ssize_t) SIZE_MAX)
 34#else
 35#include <strings.h>
 36#include <unistd.h>
 37#endif
 38
 39#ifndef SSIZE_MAX
 40#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
 41#endif
 42
 43#define UNUSED(V) (void)(V)
 44
 45#ifndef M_PI
 46#define M_PI 3.141592654f
 47#endif
 48
 49#if defined(__PPC__) || defined(__POWERPC__)
 50#define LOAD_32LE(DEST, ADDR, ARR) { \
 51	uint32_t _addr = (ADDR); \
 52	void* _ptr = (ARR); \
 53	__asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 54}
 55
 56#define LOAD_16LE(DEST, ADDR, ARR) { \
 57	uint32_t _addr = (ADDR); \
 58	void* _ptr = (ARR); \
 59	__asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 60}
 61
 62#define STORE_32LE(SRC, ADDR, ARR) { \
 63	uint32_t _addr = (ADDR); \
 64	void* _ptr = (ARR); \
 65	__asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 66}
 67
 68#define STORE_16LE(SRC, ADDR, ARR) { \
 69	uint32_t _addr = (ADDR); \
 70	void* _ptr = (ARR); \
 71	__asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 72}
 73#else
 74#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
 75#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
 76#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
 77#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
 78#endif
 79
 80#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
 81#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
 82#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
 83#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
 84#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
 85#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
 86
 87#ifdef _MSC_VER
 88#define ATTRIBUTE_UNUSED
 89#define ATTRIBUTE_FORMAT(X, Y, Z)
 90#else
 91#define ATTRIBUTE_UNUSED __attribute__((unused))
 92#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
 93#endif
 94
 95#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
 96
 97#define DECL_BITS(TYPE, FIELD, START, SIZE) \
 98	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
 99		return CHECK_BITS(src, (START), (START) + (SIZE)); \
100	} \
101	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
102		return EXT_BITS(src, (START), (START) + (SIZE)); \
103	} \
104	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
105		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
106	} \
107	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
108		return FILL_BITS(src, (START), (START) + (SIZE)); \
109	} \
110	ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
111		return INS_BITS(src, (START), (START) + (SIZE), bits); \
112	}
113
114#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
115
116#ifndef _MSC_VER
117#define LIKELY(X) __builtin_expect(!!(X), 1)
118#define UNLIKELY(X) __builtin_expect(!!(X), 0)
119#else
120#define LIKELY(X) (!!(X))
121#define UNLIKELY(X) (!!(X))
122#endif
123
124#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
125
126#endif