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
17#ifdef __MINGW32__
18#define __USE_MINGW_ANSI_STDIO 1
19#endif
20
21CXX_GUARD_START
22
23#include <ctype.h>
24#include <fcntl.h>
25#include <inttypes.h>
26#include <limits.h>
27#include <math.h>
28#include <stdarg.h>
29#include <stdbool.h>
30#include <stddef.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <time.h>
36
37#ifdef _WIN32
38// WinSock2 gets very angry if it's included too late
39#include <winsock2.h>
40#endif
41
42#if defined(_MSC_VER) || defined(__cplusplus)
43#define restrict __restrict
44#endif
45
46#ifdef _MSC_VER
47#include <Windows.h>
48#include <sys/types.h>
49typedef intptr_t ssize_t;
50#define PATH_MAX MAX_PATH
51#define strcasecmp _stricmp
52#define strncasecmp _strnicmp
53#define ftruncate _chsize
54#define snprintf _snprintf
55#define strdup _strdup
56#define lseek _lseek
57#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
58#elif defined(__wii__)
59#include <sys/time.h>
60typedef intptr_t ssize_t;
61#else
62#include <strings.h>
63#include <unistd.h>
64#include <sys/time.h>
65#endif
66
67#ifdef PSP2
68// For PATH_MAX on modern toolchains
69#include <sys/syslimits.h>
70#endif
71
72#include <mgba-util/dllexports.h>
73
74#ifndef SSIZE_MAX
75#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
76#endif
77
78#ifndef UNUSED
79#define UNUSED(V) (void)(V)
80#endif
81
82#ifndef M_PI
83#define M_PI 3.141592654f
84#endif
85
86#if !defined(_MSC_VER) && (defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
87#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
88#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
89#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
90#define ATOMIC_SUB(DST, OP) __atomic_sub_fetch(&DST, OP, __ATOMIC_RELEASE)
91#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
92#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
93#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
94#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
95#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
96#elif defined _MSC_VER
97#define ATOMIC_STORE(DST, SRC) InterlockedExchange(&DST, SRC)
98#define ATOMIC_LOAD(DST, SRC) DST = InterlockedOrAcquire(&SRC, 0)
99#define ATOMIC_ADD(DST, OP) InterlockedAddRelease(&DST, OP)
100#define ATOMIC_SUB(DST, OP) InterlockedAddRelease(&DST, -OP)
101#define ATOMIC_OR(DST, OP) InterlockedOrRelease(&DST, OP)
102#define ATOMIC_AND(DST, OP) InterlockedAndRelease(&DST, OP)
103#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) (InterlockedCompareExchange(&DST, SRC, EXPECTED) == EXPECTED)
104#define ATOMIC_STORE_PTR(DST, SRC) InterlockedExchangePointer(&DST, SRC)
105#define ATOMIC_LOAD_PTR(DST, SRC) DST = InterlockedCompareExchangePointer(&SRC, 0, 0)
106#else
107// TODO
108#define ATOMIC_STORE(DST, SRC) DST = SRC
109#define ATOMIC_LOAD(DST, SRC) DST = SRC
110#define ATOMIC_ADD(DST, OP) DST += OP
111#define ATOMIC_SUB(DST, OP) DST -= OP
112#define ATOMIC_OR(DST, OP) DST |= OP
113#define ATOMIC_AND(DST, OP) DST &= OP
114#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
115#define ATOMIC_STORE_PTR(DST, SRC) ATOMIC_STORE(DST, SRC)
116#define ATOMIC_LOAD_PTR(DST, SRC) ATOMIC_LOAD(DST, SRC)
117#endif
118
119#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
120// newlib doesn't support %z properly by default
121#define PRIz ""
122#elif defined(_MSC_VER)
123#define PRIz "I"
124#else
125#define PRIz "z"
126#endif
127
128#if defined __BIG_ENDIAN__
129#define LOAD_32BE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
130#if defined(__PPC__) || defined(__POWERPC__)
131#define LOAD_32LE(DEST, ADDR, ARR) { \
132 off_t _addr = (ADDR); \
133 const void* _ptr = (ARR); \
134 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
135}
136
137#define LOAD_16LE(DEST, ADDR, ARR) { \
138 off_t _addr = (ADDR); \
139 const void* _ptr = (ARR); \
140 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
141}
142
143#define STORE_32LE(SRC, ADDR, ARR) { \
144 off_t _addr = (ADDR); \
145 void* _ptr = (ARR); \
146 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
147}
148
149#define STORE_16LE(SRC, ADDR, ARR) { \
150 off_t _addr = (ADDR); \
151 void* _ptr = (ARR); \
152 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
153}
154
155#ifndef _ARCH_PWR7
156#define LOAD_64LE(DEST, ADDR, ARR) { \
157 off_t _addr = (ADDR); \
158 union { \
159 struct { \
160 uint32_t hi; \
161 uint32_t lo; \
162 }; \
163 uint64_t b64; \
164 } bswap; \
165 const void* _ptr = (ARR); \
166 __asm__( \
167 "lwbrx %0, %2, %3 \n" \
168 "lwbrx %1, %2, %4 \n" \
169 : "=&r"(bswap.lo), "=&r"(bswap.hi) : "b"(_ptr), "r"(_addr), "r"(_addr + 4)) ; \
170 DEST = bswap.b64; \
171}
172
173#define STORE_64LE(SRC, ADDR, ARR) { \
174 off_t _addr = (ADDR); \
175 union { \
176 struct { \
177 uint32_t hi; \
178 uint32_t lo; \
179 }; \
180 uint64_t b64; \
181 } bswap = { .b64 = SRC }; \
182 const void* _ptr = (ARR); \
183 __asm__( \
184 "stwbrx %0, %2, %3 \n" \
185 "stwbrx %1, %2, %4 \n" \
186 : : "r"(bswap.hi), "r"(bswap.lo), "b"(_ptr), "r"(_addr), "r"(_addr + 4) : "memory"); \
187}
188#else
189#define LOAD_64LE(DEST, ADDR, ARR) { \
190 off_t _addr = (ADDR); \
191 const void* _ptr = (ARR); \
192 __asm__("ldbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
193}
194
195#define STORE_64LE(SRC, ADDR, ARR) { \
196 off_t _addr = (ADDR); \
197 void* _ptr = (ARR); \
198 __asm__("stdbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
199}
200#endif
201
202#elif defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
203#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
204#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
205#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
206#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
207#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
208#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
209#else
210#error Big endian build not supported on this platform.
211#endif
212#else
213#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
214#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
215#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
216#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
217#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
218#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
219#ifdef _MSC_VER
220#define LOAD_32BE(DEST, ADDR, ARR) DEST = _byteswap_ulong(((uint32_t*) ARR)[(ADDR) >> 2])
221#else
222#define LOAD_32BE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
223#endif
224#endif
225
226#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
227#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
228#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
229#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
230#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
231#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
232#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
233
234#ifdef _MSC_VER
235#pragma section(".CRT$XCU",read)
236#define ATTRIBUTE_UNUSED
237#define ATTRIBUTE_FORMAT(X, Y, Z)
238#define ATTRIBUTE_NOINLINE
239// Adapted from https://stackoverflow.com/a/2390626
240#define _CONSTRUCTOR(FN, PRE) \
241 static void FN(void); \
242 __declspec(allocate(".CRT$XCU")) void (*_CONSTRUCTOR_ ## FN)(void) = FN; \
243 static void FN(void)
244#ifdef _WIN64
245#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "")
246#else
247#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "_")
248#endif
249#else
250#define ATTRIBUTE_UNUSED __attribute__((unused))
251#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
252#define ATTRIBUTE_NOINLINE __attribute__((noinline))
253#define CONSTRUCTOR(FN) static __attribute__((constructor)) void FN(void)
254#endif
255
256#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
257
258#define DECL_BITS(TYPE, FIELD, START, SIZE) \
259 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
260 return CHECK_BITS(src, (START), (START) + (SIZE)); \
261 } \
262 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
263 return EXT_BITS(src, (START), (START) + (SIZE)); \
264 } \
265 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
266 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
267 } \
268 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
269 return FILL_BITS(src, (START), (START) + (SIZE)); \
270 } \
271 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
272 return INS_BITS(src, (START), (START) + (SIZE), bits); \
273 } \
274 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
275 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
276 }
277
278#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
279
280#ifndef _MSC_VER
281#define LIKELY(X) __builtin_expect(!!(X), 1)
282#define UNLIKELY(X) __builtin_expect(!!(X), 0)
283#else
284#define LIKELY(X) (!!(X))
285#define UNLIKELY(X) (!!(X))
286#endif
287
288#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
289
290CXX_GUARD_END
291
292#endif