all repos — mgba @ 87313041c0fe8c6ea6531962aa14ea3668567c56

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#include <strings.h>
22#include <unistd.h>
23
24#include "version.h"
25
26#define UNUSED(V) (void)(V)
27
28#ifndef M_PI
29#define M_PI 3.141592654f
30#endif
31
32#if defined(__PPC__) || defined(__POWERPC__)
33#define LOAD_32LE(DEST, ADDR, ARR) { \
34	uint32_t _addr = (ADDR); \
35	void* _ptr = (ARR); \
36	asm("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
37}
38
39#define LOAD_16LE(DEST, ADDR, ARR) { \
40	uint32_t _addr = (ADDR); \
41	void* _ptr = (ARR); \
42	asm("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
43}
44
45#define STORE_32LE(SRC, ADDR, ARR) { \
46	uint32_t _addr = (ADDR); \
47	void* _ptr = (ARR); \
48	asm("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
49}
50
51#define STORE_16LE(SRC, ADDR, ARR) { \
52	uint32_t _addr = (ADDR); \
53	void* _ptr = (ARR); \
54	asm("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
55}
56#else
57#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
58#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
59#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
60#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
61#endif
62
63#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
64#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
65#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
66#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
67#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
68#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
69
70#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
71
72#define DECL_BITS(TYPE, FIELD, START, SIZE) \
73	__attribute__((unused)) static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
74		return CHECK_BITS(src, (START), (START) + (SIZE)); \
75	} \
76	__attribute__((unused)) static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
77		return EXT_BITS(src, (START), (START) + (SIZE)); \
78	} \
79	__attribute__((unused)) static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
80		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
81	} \
82	__attribute__((unused)) static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
83		return FILL_BITS(src, (START), (START) + (SIZE)); \
84	} \
85	__attribute__((unused)) static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
86		return INS_BITS(src, (START), (START) + (SIZE), bits); \
87	}
88
89#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
90
91#define LIKELY(X) __builtin_expect(!!(X), 1)
92#define UNLIKELY(X) __builtin_expect(!!(X), 0)
93
94#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
95
96#endif