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#ifdef _MSC_VER
38#include <Windows.h>
39#include <sys/types.h>
40typedef intptr_t ssize_t;
41#define PATH_MAX MAX_PATH
42#define restrict __restrict
43#define strcasecmp _stricmp
44#define strncasecmp _strnicmp
45#define ftruncate _chsize
46#define snprintf _snprintf
47#define strdup _strdup
48#define lseek _lseek
49#elif defined(__wii__)
50#include <sys/time.h>
51typedef intptr_t ssize_t;
52#else
53#include <strings.h>
54#include <unistd.h>
55#include <sys/time.h>
56#endif
57
58#ifndef SSIZE_MAX
59#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
60#endif
61
62#ifndef UNUSED
63#define UNUSED(V) (void)(V)
64#endif
65
66#ifndef M_PI
67#define M_PI 3.141592654f
68#endif
69
70#ifndef _MSC_VER
71#define ATOMIC_STORE(DST, SRC) __atomic_store_n(&DST, SRC, __ATOMIC_RELEASE)
72#define ATOMIC_LOAD(DST, SRC) DST = __atomic_load_n(&SRC, __ATOMIC_ACQUIRE)
73#define ATOMIC_ADD(DST, OP) __atomic_add_fetch(&DST, OP, __ATOMIC_RELEASE)
74#define ATOMIC_OR(DST, OP) __atomic_or_fetch(&DST, OP, __ATOMIC_RELEASE)
75#define ATOMIC_AND(DST, OP) __atomic_and_fetch(&DST, OP, __ATOMIC_RELEASE)
76#define ATOMIC_CMPXCHG(DST, EXPECTED, SRC) __atomic_compare_exchange_n(&DST, &EXPECTED, SRC, true,__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)
77#else
78// TODO
79#define ATOMIC_STORE(DST, SRC) DST = SRC
80#define ATOMIC_LOAD(DST, SRC) DST = SRC
81#define ATOMIC_ADD(DST, OP) DST += OP
82#define ATOMIC_OR(DST, OP) DST |= OP
83#define ATOMIC_AND(DST, OP) DST &= OP
84#define ATOMIC_CMPXCHG(DST, EXPECTED, OP) ((DST == EXPECTED) ? ((DST = OP), true) : false)
85#endif
86
87#if defined(_3DS) || defined(GEKKO) || defined(PSP2)
88// newlib doesn't support %z properly by default
89#define PRIz ""
90#elif defined(_WIN64)
91#define PRIz "ll"
92#elif defined(_WIN32)
93#define PRIz ""
94#else
95#define PRIz "z"
96#endif
97
98#if defined(__PPC__) || defined(__POWERPC__)
99#define LOAD_32LE(DEST, ADDR, ARR) { \
100 uint32_t _addr = (ADDR); \
101 const void* _ptr = (ARR); \
102 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
103}
104
105#define LOAD_16LE(DEST, ADDR, ARR) { \
106 uint32_t _addr = (ADDR); \
107 const void* _ptr = (ARR); \
108 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
109}
110
111#define STORE_32LE(SRC, ADDR, ARR) { \
112 uint32_t _addr = (ADDR); \
113 void* _ptr = (ARR); \
114 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
115}
116
117#define STORE_16LE(SRC, ADDR, ARR) { \
118 uint32_t _addr = (ADDR); \
119 void* _ptr = (ARR); \
120 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
121}
122
123#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
124#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
125#elif defined __BIG_ENDIAN__
126#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
127#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
128#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
129#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
130#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
131#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
132#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
133#else
134#error Big endian build not supported on this platform.
135#endif
136#else
137#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
138#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
139#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
140#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
141#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
142#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
143#endif
144
145#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
146#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
147#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
148#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
149#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
150#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
151#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
152
153#ifdef _MSC_VER
154#define ATTRIBUTE_UNUSED
155#define ATTRIBUTE_FORMAT(X, Y, Z)
156#else
157#define ATTRIBUTE_UNUSED __attribute__((unused))
158#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
159#endif
160
161#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
162
163#define DECL_BITS(TYPE, FIELD, START, SIZE) \
164 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
165 return CHECK_BITS(src, (START), (START) + (SIZE)); \
166 } \
167 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
168 return EXT_BITS(src, (START), (START) + (SIZE)); \
169 } \
170 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
171 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
172 } \
173 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
174 return FILL_BITS(src, (START), (START) + (SIZE)); \
175 } \
176 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
177 return INS_BITS(src, (START), (START) + (SIZE), bits); \
178 } \
179 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
180 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
181 }
182
183#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
184
185#ifndef _MSC_VER
186#define LIKELY(X) __builtin_expect(!!(X), 1)
187#define UNLIKELY(X) __builtin_expect(!!(X), 0)
188#else
189#define LIKELY(X) (!!(X))
190#define UNLIKELY(X) (!!(X))
191#endif
192
193#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
194
195CXX_GUARD_END
196
197#endif