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