all repos — mgba @ f0f929665e0e2497bed27b2d7300973c4122bb96

mGBA Game Boy Advance Emulator

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