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