all repos — mgba @ cd2533edce880ab07fcc1b206cefe4c4b20040f3

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#ifdef _MSC_VER
 23#ifdef _WIN64
 24typedef int64_t off_t;
 25typedef int64_t ssize_t;
 26#else
 27typedef int32_t off_t;
 28typedef int32_t ssize_t;
 29#endif
 30#define restrict __restrict
 31#define SSIZE_MAX ((ssize_t) SIZE_MAX)
 32#define strcasecmp _stricmp
 33#define strncasecmp _strnicmp
 34#define ftruncate _chsize
 35#else
 36#include <strings.h>
 37#include <unistd.h>
 38#endif
 39
 40#include "version.h"
 41
 42#define UNUSED(V) (void)(V)
 43
 44#ifndef M_PI
 45#define M_PI 3.141592654f
 46#endif
 47
 48#if defined(__PPC__) || defined(__POWERPC__)
 49#define LOAD_32LE(DEST, ADDR, ARR) { \
 50	uint32_t _addr = (ADDR); \
 51	void* _ptr = (ARR); \
 52	asm("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 53}
 54
 55#define LOAD_16LE(DEST, ADDR, ARR) { \
 56	uint32_t _addr = (ADDR); \
 57	void* _ptr = (ARR); \
 58	asm("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
 59}
 60
 61#define STORE_32LE(SRC, ADDR, ARR) { \
 62	uint32_t _addr = (ADDR); \
 63	void* _ptr = (ARR); \
 64	asm("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 65}
 66
 67#define STORE_16LE(SRC, ADDR, ARR) { \
 68	uint32_t _addr = (ADDR); \
 69	void* _ptr = (ARR); \
 70	asm("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
 71}
 72#else
 73#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
 74#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
 75#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
 76#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
 77#endif
 78
 79#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
 80#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
 81#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
 82#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
 83#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
 84#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
 85
 86#ifdef _MSC_VER
 87#define ATTRIBUTE_UNUSED
 88#define ATTRIBUTE_FORMAT(X, Y, Z)
 89#define ATTRIBUTE_PACKED
 90#else
 91#define ATTRIBUTE_UNUSED __attribute__((unused))
 92#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
 93#define ATTRIBUTE_PACKED __attribute__((packed))
 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