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 _MSC_VER
27#include <sys/types.h>
28typedef intptr_t ssize_t;
29#define inline __inline
30#define restrict __restrict
31#define strcasecmp _stricmp
32#define strncasecmp _strnicmp
33#define ftruncate _chsize
34#define snprintf _snprintf
35#elif defined(__wii__)
36typedef intptr_t ssize_t;
37#else
38#include <strings.h>
39#include <unistd.h>
40#endif
41
42#ifndef SSIZE_MAX
43#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
44#endif
45
46#define UNUSED(V) (void)(V)
47
48#ifndef M_PI
49#define M_PI 3.141592654f
50#endif
51
52#if defined(__PPC__) || defined(__POWERPC__)
53#define LOAD_32LE(DEST, ADDR, ARR) { \
54 uint32_t _addr = (ADDR); \
55 const void* _ptr = (ARR); \
56 __asm__("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
57}
58
59#define LOAD_16LE(DEST, ADDR, ARR) { \
60 uint32_t _addr = (ADDR); \
61 const void* _ptr = (ARR); \
62 __asm__("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
63}
64
65#define STORE_32LE(SRC, ADDR, ARR) { \
66 uint32_t _addr = (ADDR); \
67 void* _ptr = (ARR); \
68 __asm__("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
69}
70
71#define STORE_16LE(SRC, ADDR, ARR) { \
72 uint32_t _addr = (ADDR); \
73 void* _ptr = (ARR); \
74 __asm__("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
75}
76#elif defined __BIG_ENDIAN__
77#if defined(__llvm__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
78#define LOAD_32LE(DEST, ADDR, ARR) DEST = __builtin_bswap32(((uint32_t*) ARR)[(ADDR) >> 2])
79#define LOAD_16LE(DEST, ADDR, ARR) DEST = __builtin_bswap16(((uint16_t*) ARR)[(ADDR) >> 1])
80#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = __builtin_bswap32(SRC)
81#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = __builtin_bswap16(SRC)
82#else
83#error Big endian build not supported on this platform.
84#endif
85#else
86#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
87#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
88#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
89#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
90#endif
91
92#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
93#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
94#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
95#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
96#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
97#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
98#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
99
100#ifdef _MSC_VER
101#define ATTRIBUTE_UNUSED
102#define ATTRIBUTE_FORMAT(X, Y, Z)
103#else
104#define ATTRIBUTE_UNUSED __attribute__((unused))
105#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
106#endif
107
108#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
109
110#define DECL_BITS(TYPE, FIELD, START, SIZE) \
111 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
112 return CHECK_BITS(src, (START), (START) + (SIZE)); \
113 } \
114 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
115 return EXT_BITS(src, (START), (START) + (SIZE)); \
116 } \
117 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
118 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
119 } \
120 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
121 return FILL_BITS(src, (START), (START) + (SIZE)); \
122 } \
123 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
124 return INS_BITS(src, (START), (START) + (SIZE), bits); \
125 } \
126 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
127 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
128 }
129
130#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
131
132#ifndef _MSC_VER
133#define LIKELY(X) __builtin_expect(!!(X), 1)
134#define UNLIKELY(X) __builtin_expect(!!(X), 0)
135#else
136#define LIKELY(X) (!!(X))
137#define UNLIKELY(X) (!!(X))
138#endif
139
140#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
141
142#endif