all repos — mgba @ e6ea94d2296eae963a48a18d009217a38d92bf9b

mGBA Game Boy Advance Emulator

src/arm/macros.h (view raw)

 1#ifndef MACROS_H
 2#define MACROS_H
 3
 4#include "util/common.h"
 5
 6#if defined(__PPC__) || defined(__POWERPC__)
 7#define LOAD_32(DEST, ADDR, ARR) { \
 8	uint32_t _addr = (ADDR); \
 9	void* _ptr = (ARR); \
10	asm("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
11}
12
13#define LOAD_16(DEST, ADDR, ARR) { \
14	uint32_t _addr = (ADDR); \
15	void* _ptr = (ARR); \
16	asm("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
17}
18
19#define STORE_32(SRC, ADDR, ARR) { \
20	uint32_t _addr = (ADDR); \
21	void* _ptr = (ARR); \
22	asm("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
23}
24
25#define STORE_16(SRC, ADDR, ARR) { \
26	uint32_t _addr = (ADDR); \
27	void* _ptr = (ARR); \
28	asm("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
29}
30#else
31#define LOAD_32(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
32#define LOAD_16(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
33#define STORE_32(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
34#define STORE_16(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
35#endif
36
37#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
38#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
39#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
40#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
41#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
42#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
43
44#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
45
46#define DECL_BITS(TYPE, FIELD, START, SIZE) \
47	__attribute__((unused)) static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
48		return CHECK_BITS(src, (START), (START) + (SIZE)); \
49	} \
50	__attribute__((unused)) static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
51		return EXT_BITS(src, (START), (START) + (SIZE)); \
52	} \
53	__attribute__((unused)) static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
54		return CLEAR_BITS(src, (START), (START) + (SIZE)); \
55	} \
56	__attribute__((unused)) static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
57		return FILL_BITS(src, (START), (START) + (SIZE)); \
58	} \
59	__attribute__((unused)) static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
60		return INS_BITS(src, (START), (START) + (SIZE), bits); \
61	}
62
63#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
64
65#define LIKELY(X) __builtin_expect(!!(X), 1)
66#define UNLIKELY(X) __builtin_expect(!!(X), 0)
67#endif