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