src/arm/common.h (view raw)
1#ifndef COMMON_H
2#define COMMON_H
3
4#include <ctype.h>
5#include <fcntl.h>
6#include <limits.h>
7#include <math.h>
8#include <stdarg.h>
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
17#define UNUSED(V) (void)(V)
18
19#if defined(__PPC__) || defined(__POWERPC__)
20#define LOAD_32(DEST, ADDR, ARR) asm("lwbrx %0, %1, %2" : "=r"(DEST) : "r"(ADDR), "r"(ARR))
21#define LOAD_16(DEST, ADDR, ARR) asm("lhbrx %0, %1, %2" : "=r"(DEST) : "r"(ADDR), "r"(ARR))
22#define STORE_32(SRC, ADDR, ARR) asm("stwbrx %0, %1, %2" : : "r"(SRC), "r"(ADDR), "r"(ARR))
23#define STORE_16(SRC, ADDR, ARR) asm("sthbrx %0, %1, %2" : : "r"(SRC), "r"(ADDR), "r"(ARR))
24#else
25#define LOAD_32(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
26#define LOAD_16(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
27#define STORE_32(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
28#define STORE_16(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
29#endif
30
31#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
32#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
33#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
34#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
35#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
36#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
37
38#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
39
40#define DECL_BITS(TYPE, FIELD, START, SIZE) \
41 static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
42 return CHECK_BITS(src, (START), (START) + (SIZE)); \
43 } \
44 static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
45 return EXT_BITS(src, (START), (START) + (SIZE)); \
46 } \
47 static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
48 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
49 } \
50 static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
51 return FILL_BITS(src, (START), (START) + (SIZE)); \
52 } \
53 static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
54 return INS_BITS(src, (START), (START) + (SIZE), bits); \
55 }
56
57#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
58#endif