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