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