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