include/mgba-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#ifdef __cplusplus
10#define CXX_GUARD_START extern "C" {
11#define CXX_GUARD_END }
12#else
13#define CXX_GUARD_START
14#define CXX_GUARD_END
15#endif
16
17CXX_GUARD_START
18
19#include <ctype.h>
20#include <fcntl.h>
21#include <inttypes.h>
22#include <limits.h>
23#include <math.h>
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stddef.h>
27#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#ifdef _WIN32
33// WinSock2 gets very angry if it's included too late
34#include <winsock2.h>
35#endif
36#ifdef _MSC_VER
37#include <Windows.h>
38#include <sys/types.h>
39#include <time.h>
40typedef intptr_t ssize_t;
41#define PATH_MAX MAX_PATH
42#define restrict __restrict
43#define strcasecmp _stricmp
44#define strncasecmp _strnicmp
45#define ftruncate _chsize
46#define snprintf _snprintf
47#define strdup _strdup
48#define lseek _lseek
49#elif defined(__wii__)
50typedef intptr_t ssize_t;
51#else
52#include <strings.h>
53#include <unistd.h>
54#include <sys/time.h>
55#endif
56
57#ifndef SSIZE_MAX
58#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
59#endif
60
61#ifndef UNUSED
62#define UNUSED(V) (void)(V)
63#endif
64
65#ifndef M_PI
66#define M_PI 3.141592654f
67#endif
68
69#ifndef _MSC_VER
70#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
71#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
72#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
73#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
74#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
75#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
76#else
77// TODO
78#define ATOMIC_STORE(DST, SRC) DST = SRC
79#define ATOMIC_LOAD(DST, SRC) DST = SRC
80#define ATOMIC_ADD(DST, OP) DST += OP
81#define ATOMIC_OR(DST, OP) DST |= OP
82#define ATOMIC_AND(DST, OP) DST &= OP
83#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
84#endif
85
86#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
87// newlib doesn't support %z properly by default
88#define PRIz ""
89#elif defined(_WIN64)
90#define PRIz "ll"
91#elif defined(_WIN32)
92#define PRIz ""
93#else
94#define PRIz "z"
95#endif
96
97#if defined(__PPC__) || defined(__POWERPC__)
98#define LOAD_32LE(DEST, ADDR, ARR) { \
99 uint32_t _addr = (ADDR); \
100 const void* _ptr = (ARR); \
101 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
102}
103
104#define LOAD_16LE(DEST, ADDR, ARR) { \
105 uint32_t _addr = (ADDR); \
106 const void* _ptr = (ARR); \
107 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
108}
109
110#define STORE_32LE(SRC, ADDR, ARR) { \
111 uint32_t _addr = (ADDR); \
112 void* _ptr = (ARR); \
113 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
114}
115
116#define STORE_16LE(SRC, ADDR, ARR) { \
117 uint32_t _addr = (ADDR); \
118 void* _ptr = (ARR); \
119 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
120}
121
122#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
123#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
124#elif defined __BIG_ENDIAN__
125#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
126#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
127#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
128#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
129#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
130#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
131#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
132#else
133#error Big endian build not supported on this platform.
134#endif
135#else
136#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
137#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
138#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
139#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
140#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
141#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
142#endif
143
144#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
145#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
146#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
147#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
148#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
149#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
150#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
151
152#ifdef _MSC_VER
153#define ATTRIBUTE_UNUSED
154#define ATTRIBUTE_FORMAT(X, Y, Z)
155#else
156#define ATTRIBUTE_UNUSED __attribute__((unused))
157#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
158#endif
159
160#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
161
162#define DECL_BITS(TYPE, FIELD, START, SIZE) \
163 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
164 return CHECK_BITS(src, (START), (START) + (SIZE)); \
165 } \
166 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
167 return EXT_BITS(src, (START), (START) + (SIZE)); \
168 } \
169 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
170 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
171 } \
172 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
173 return FILL_BITS(src, (START), (START) + (SIZE)); \
174 } \
175 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
176 return INS_BITS(src, (START), (START) + (SIZE), bits); \
177 } \
178 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
179 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
180 }
181
182#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
183
184#ifndef _MSC_VER
185#define LIKELY(X) __builtin_expect(!!(X), 1)
186#define UNLIKELY(X) __builtin_expect(!!(X), 0)
187#else
188#define LIKELY(X) (!!(X))
189#define UNLIKELY(X) (!!(X))
190#endif
191
192#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
193
194CXX_GUARD_END
195
196#endif