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_64BE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
130#define LOAD_32BE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
131#define LOAD_16BE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
132#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
133#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
134#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
135#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
136#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
137#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
138#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
139#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap64(SRC)
140#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap32(SRC)
141#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap16(SRC)
142#elif defined(__PPC__) || defined(__POWERPC__)
143#define LOAD_32LE(DEST, ADDR, ARR) { \
144 size_t _addr = (ADDR); \
145 const void* _ptr = (ARR); \
146 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
147}
148
149#define LOAD_16LE(DEST, ADDR, ARR) { \
150 size_t _addr = (ADDR); \
151 const void* _ptr = (ARR); \
152 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
153}
154
155#define STORE_32LE(SRC, ADDR, ARR) { \
156 size_t _addr = (ADDR); \
157 void* _ptr = (ARR); \
158 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
159}
160
161#define STORE_16LE(SRC, ADDR, ARR) { \
162 size_t _addr = (ADDR); \
163 void* _ptr = (ARR); \
164 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
165}
166
167#ifndef _ARCH_PWR7
168#define LOAD_64LE(DEST, ADDR, ARR) { \
169 size_t _addr = (ADDR); \
170 union { \
171 struct { \
172 uint32_t hi; \
173 uint32_t lo; \
174 }; \
175 uint64_t b64; \
176 } bswap; \
177 const void* _ptr = (ARR); \
178 __asm__( \
179 "lwbrx %0, %2, %3 \n" \
180 "lwbrx %1, %2, %4 \n" \
181 : "=&r"(bswap.lo), "=&r"(bswap.hi) : "b"(_ptr), "r"(_addr), "r"(_addr + 4)) ; \
182 DEST = bswap.b64; \
183}
184
185#define STORE_64LE(SRC, ADDR, ARR) { \
186 size_t _addr = (ADDR); \
187 union { \
188 struct { \
189 uint32_t hi; \
190 uint32_t lo; \
191 }; \
192 uint64_t b64; \
193 } bswap = { .b64 = SRC }; \
194 const void* _ptr = (ARR); \
195 __asm__( \
196 "stwbrx %0, %2, %3 \n" \
197 "stwbrx %1, %2, %4 \n" \
198 : : "r"(bswap.hi), "r"(bswap.lo), "b"(_ptr), "r"(_addr), "r"(_addr + 4) : "memory"); \
199}
200#else
201#define LOAD_64LE(DEST, ADDR, ARR) { \
202 size_t _addr = (ADDR); \
203 const void* _ptr = (ARR); \
204 __asm__("ldbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
205}
206
207#define STORE_64LE(SRC, ADDR, ARR) { \
208 size_t _addr = (ADDR); \
209 void* _ptr = (ARR); \
210 __asm__("stdbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr) : "memory"); \
211}
212#endif
213#else
214#error Big endian build not supported on this platform.
215#endif
216#else
217#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
218#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
219#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
220#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
221#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
222#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
223#ifdef _MSC_VER
224#define LOAD_64BE(DEST, ADDR, ARR) DEST = _byteswap_uint64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
225#define LOAD_32BE(DEST, ADDR, ARR) DEST = _byteswap_ulong(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
226#define LOAD_16BE(DEST, ADDR, ARR) DEST = _byteswap_ushort(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
227#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_uint64(SRC)
228#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_ulong(SRC)
229#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = _byteswap_ushort(SRC)
230#else
231#define LOAD_64BE(DEST, ADDR, ARR) DEST = __builtin_bswap64(*(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
232#define LOAD_32BE(DEST, ADDR, ARR) DEST = __builtin_bswap32(*(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
233#define LOAD_16BE(DEST, ADDR, ARR) DEST = __builtin_bswap16(*(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)))
234#define STORE_64BE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap64(SRC)
235#define STORE_32BE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap32(SRC)
236#define STORE_16BE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = __builtin_bswap16(SRC)
237#endif
238#endif
239
240#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
241#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
242#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
243#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
244#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
245#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
246#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
247
248#ifdef _MSC_VER
249#pragma section(".CRT$XCU",read)
250#define ATTRIBUTE_UNUSED
251#define ATTRIBUTE_FORMAT(X, Y, Z)
252#define ATTRIBUTE_NOINLINE
253// Adapted from https://stackoverflow.com/a/2390626
254#define _CONSTRUCTOR(FN, PRE) \
255 static void FN(void); \
256 __declspec(allocate(".CRT$XCU")) void (*_CONSTRUCTOR_ ## FN)(void) = FN; \
257 static void FN(void)
258#ifdef _WIN64
259#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "")
260#else
261#define CONSTRUCTOR(FN) _CONSTRUCTOR(FN, "_")
262#endif
263#else
264#define ATTRIBUTE_UNUSED __attribute__((unused))
265#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
266#define ATTRIBUTE_NOINLINE __attribute__((noinline))
267#define CONSTRUCTOR(FN) static __attribute__((constructor)) void FN(void)
268#endif
269
270#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
271
272#define DECL_BITS(TYPE, FIELD, START, SIZE) \
273 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
274 return CHECK_BITS(src, (START), (START) + (SIZE)); \
275 } \
276 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
277 return EXT_BITS(src, (START), (START) + (SIZE)); \
278 } \
279 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
280 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
281 } \
282 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
283 return FILL_BITS(src, (START), (START) + (SIZE)); \
284 } \
285 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
286 return INS_BITS(src, (START), (START) + (SIZE), bits); \
287 } \
288 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
289 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
290 }
291
292#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
293
294#ifndef _MSC_VER
295#define LIKELY(X) __builtin_expect(!!(X), 1)
296#define UNLIKELY(X) __builtin_expect(!!(X), 0)
297#else
298#define LIKELY(X) (!!(X))
299#define UNLIKELY(X) (!!(X))
300#endif
301
302#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
303
304CXX_GUARD_END
305
306#endif