all repos — mgba @ 4f04c30f708f30f51f5b90aff2e320cd607955ea

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