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#else
77#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
78#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
79#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
80#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
81#endif
82
83#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
84#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
85#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
86#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
87#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
88#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
89#define TEST_FILL_BITS(SRC, START, END, TEST) ((TEST) ? (FILL_BITS(SRC, START, END)) : (CLEAR_BITS(SRC, START, END)))
90
91#ifdef _MSC_VER
92#define ATTRIBUTE_UNUSED
93#define ATTRIBUTE_FORMAT(X, Y, Z)
94#else
95#define ATTRIBUTE_UNUSED __attribute__((unused))
96#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
97#endif
98
99#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
100
101#define DECL_BITS(TYPE, FIELD, START, SIZE) \
102 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
103 return CHECK_BITS(src, (START), (START) + (SIZE)); \
104 } \
105 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
106 return EXT_BITS(src, (START), (START) + (SIZE)); \
107 } \
108 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
109 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
110 } \
111 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
112 return FILL_BITS(src, (START), (START) + (SIZE)); \
113 } \
114 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
115 return INS_BITS(src, (START), (START) + (SIZE), bits); \
116 } \
117 ATTRIBUTE_UNUSED static inline TYPE TYPE ## TestFill ## FIELD (TYPE src, bool test) { \
118 return TEST_FILL_BITS(src, (START), (START) + (SIZE), test); \
119 }
120
121#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
122
123#ifndef _MSC_VER
124#define LIKELY(X) __builtin_expect(!!(X), 1)
125#define UNLIKELY(X) __builtin_expect(!!(X), 0)
126#else
127#define LIKELY(X) (!!(X))
128#define UNLIKELY(X) (!!(X))
129#endif
130
131#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
132
133#endif