all repos — mgba @ 346e436b52422a189b12d65c2036a738a3a86f53

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