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) { \
21 uint32_t _addr = (ADDR); \
22 void* _ptr = (ARR); \
23 asm("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
24}
25
26#define LOAD_16(DEST, ADDR, ARR) { \
27 uint32_t _addr = (ADDR); \
28 void* _ptr = (ARR); \
29 asm("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
30}
31
32#define STORE_32(SRC, ADDR, ARR) { \
33 uint32_t _addr = (ADDR); \
34 void* _ptr = (ARR); \
35 asm("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
36}
37
38#define STORE_16(SRC, ADDR, ARR) { \
39 uint32_t _addr = (ADDR); \
40 void* _ptr = (ARR); \
41 asm("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
42}
43#else
44#define LOAD_32(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
45#define LOAD_16(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
46#define STORE_32(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
47#define STORE_16(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
48#endif
49
50#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
51#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
52#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
53#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
54#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
55#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
56
57#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
58
59#define DECL_BITS(TYPE, FIELD, START, SIZE) \
60 __attribute__((unused)) static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
61 return CHECK_BITS(src, (START), (START) + (SIZE)); \
62 } \
63 __attribute__((unused)) static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
64 return EXT_BITS(src, (START), (START) + (SIZE)); \
65 } \
66 __attribute__((unused)) static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
67 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
68 } \
69 __attribute__((unused)) static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
70 return FILL_BITS(src, (START), (START) + (SIZE)); \
71 } \
72 __attribute__((unused)) static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
73 return INS_BITS(src, (START), (START) + (SIZE), bits); \
74 }
75
76#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
77#endif