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#if defined(__PPC__) || defined(__POWERPC__)
56#define LOAD_32LE(DEST, ADDR, ARR) { \
57 uint32_t _addr = (ADDR); \
58 const void* _ptr = (ARR); \
59 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
60}
61
62#define LOAD_16LE(DEST, ADDR, ARR) { \
63 uint32_t _addr = (ADDR); \
64 const void* _ptr = (ARR); \
65 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
66}
67
68#define STORE_32LE(SRC, ADDR, ARR) { \
69 uint32_t _addr = (ADDR); \
70 void* _ptr = (ARR); \
71 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
72}
73
74#define STORE_16LE(SRC, ADDR, ARR) { \
75 uint32_t _addr = (ADDR); \
76 void* _ptr = (ARR); \
77 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
78}
79
80#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
81#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
82#elif defined __BIG_ENDIAN__
83#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
84#define LOAD_64LE(DEST, ADDR, ARR) DEST = __builtin_bswap64(((uint64_t*) ARR)[(ADDR) >> 3])
85#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
86#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
87#define STORE_64LE(SRC, ADDR, ARR) ((uint64_t*) ARR)[(ADDR) >> 3] = __builtin_bswap64(SRC)
88#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
89#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
90#else
91#error Big endian build not supported on this platform.
92#endif
93#else
94#define LOAD_64LE(DEST, ADDR, ARR) DEST = *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
95#define LOAD_32LE(DEST, ADDR, ARR) DEST = *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
96#define LOAD_16LE(DEST, ADDR, ARR) DEST = *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR))
97#define STORE_64LE(SRC, ADDR, ARR) *(uint64_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
98#define STORE_32LE(SRC, ADDR, ARR) *(uint32_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
99#define STORE_16LE(SRC, ADDR, ARR) *(uint16_t*) ((uintptr_t) (ARR) + (size_t) (ADDR)) = SRC
100#endif
101
102#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
103#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
104#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
105#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
106#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
107#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
108#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
109
110#ifdef _MSC_VER
111#define ATTRIBUTE_UNUSED
112#define ATTRIBUTE_FORMAT(X, Y, Z)
113#else
114#define ATTRIBUTE_UNUSED __attribute__((unused))
115#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
116#endif
117
118#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
119
120#define DECL_BITS(TYPE, FIELD, START, SIZE) \
121 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
122 return CHECK_BITS(src, (START), (START) + (SIZE)); \
123 } \
124 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
125 return EXT_BITS(src, (START), (START) + (SIZE)); \
126 } \
127 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
128 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
129 } \
130 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
131 return FILL_BITS(src, (START), (START) + (SIZE)); \
132 } \
133 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
134 return INS_BITS(src, (START), (START) + (SIZE), bits); \
135 } \
136 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
137 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
138 }
139
140#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
141
142#ifndef _MSC_VER
143#define LIKELY(X) __builtin_expect(!!(X), 1)
144#define UNLIKELY(X) __builtin_expect(!!(X), 0)
145#else
146#define LIKELY(X) (!!(X))
147#define UNLIKELY(X) (!!(X))
148#endif
149
150#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
151
152#endif