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