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
90#ifdef _MSC_VER
91#define ATTRIBUTE_UNUSED
92#define ATTRIBUTE_FORMAT(X, Y, Z)
93#else
94#define ATTRIBUTE_UNUSED __attribute__((unused))
95#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
96#endif
97
98#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
99
100#define DECL_BITS(TYPE, FIELD, START, SIZE) \
101 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
102 return CHECK_BITS(src, (START), (START) + (SIZE)); \
103 } \
104 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
105 return EXT_BITS(src, (START), (START) + (SIZE)); \
106 } \
107 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
108 return CLEAR_BITS(src, (START), (START) + (SIZE)); \
109 } \
110 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
111 return FILL_BITS(src, (START), (START) + (SIZE)); \
112 } \
113 ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
114 return INS_BITS(src, (START), (START) + (SIZE), bits); \
115 }
116
117#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
118
119#ifndef _MSC_VER
120#define LIKELY(X) __builtin_expect(!!(X), 1)
121#define UNLIKELY(X) __builtin_expect(!!(X), 0)
122#else
123#define LIKELY(X) (!!(X))
124#define UNLIKELY(X) (!!(X))
125#endif
126
127#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
128
129#endif