-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakestuff.h
More file actions
161 lines (144 loc) · 4.25 KB
/
Copy pathmakestuff.h
File metadata and controls
161 lines (144 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2009-2012 Chris McClelland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAKESTUFF_H
#define MAKESTUFF_H
#include <stddef.h>
#ifndef __cplusplus
#ifdef WIN32
typedef char bool;
enum {
false = 0,
true = 1
};
#else
#include <stdbool.h>
#endif
#endif
#ifdef WIN32
#define WARN_UNUSED_RESULT
#define DLLEXPORT(t) __declspec(dllexport) t __stdcall
#define PFSZD "%Iu"
#ifdef _WIN64
#define PFSZH "%016IX"
#define WORD_LENGTH 64
#else
#define PFSZH "%08IX"
#define WORD_LENGTH 32
#endif
#else
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#define DLLEXPORT(t) t
#define PFSZD "%zu"
#ifdef __LP64__
#define PFSZH "%016zX"
#define WORD_LENGTH 64
#else
#define PFSZH "%08zX"
#define WORD_LENGTH 32
#endif
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
typedef unsigned char uint8;
typedef unsigned short uint16;
#ifndef __cplusplus
#ifndef SDCC
typedef unsigned long long uint64;
#endif
#endif
typedef signed char int8;
typedef signed short int16;
#if (defined __AVR__ && defined __GNUC__) || defined SDCC
// The embedded platforms have sizeof(int) = 2, so use long
typedef signed long int32;
typedef unsigned long uint32;
#else
// The i686 & x86_64 have sizeof(int) = 4
typedef signed int int32;
typedef unsigned int uint32;
#endif
#ifndef __cplusplus
#ifndef SDCC
typedef signed long long int64;
#endif
#endif
typedef unsigned int bitfield;
#if defined __GNUC__
#define swap32(x) __builtin_bswap32(x)
#elif defined WIN32
#ifdef __cplusplus
extern "C"
#endif
unsigned long __cdecl _byteswap_ulong(unsigned long);
#define swap32(x) _byteswap_ulong(x)
#ifndef __cplusplus
#define inline __inline
#endif
#endif
#define swap16(x) ((uint16)((((x) & 0x00FF) << 8) | (((x) >> 8) & 0x00FF)))
// The C standard requires this two-level indirection thing
#undef CONCAT
#define CONCAT_INTERNAL(x, y) x ## y
#define CONCAT(x, y) CONCAT_INTERNAL(x, y)
#define STR_INTERNAL(x) #x
#define STR(x) STR_INTERNAL(x)
// The VA_NARGS() macro - count the number of arguments in a C99 variadic macro
#define VA_EXPAND(x) x
#define VA_NARGS(...) VA_EXPAND(VA_NARGS_INTERNAL(__VA_ARGS__, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1))
#define VA_NARGS_INTERNAL(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, N, ...) N
#define FAIL(code, label) { retVal = code; goto label; }
// The CHECK_STATUS() macro - if condition is true, set a returnCode and jump to a label (exit,
// cleanup etc). If liberror is included you can also give an error message.
#define CHECK_INTERNAL3(condition, code, label) if ( condition ) { FAIL(code, label); }
#define CHECK_INTERNAL4(condition, code, label, prefix) LIBERROR_IS_REQUIRED
#define CHECK_INTERNAL5(condition, code, label, ...) LIBERROR_IS_REQUIRED
#define CHECK_STATUS(...) VA_EXPAND(CONCAT(CHECK_INTERNAL, VA_NARGS(__VA_ARGS__))(__VA_ARGS__))
#ifdef BYTE_ORDER
#if BYTE_ORDER == 1234
// Little-endian machines
static inline uint16 bigEndian16(uint16 x) {
return swap16(x);
}
static inline uint32 bigEndian32(uint32 x) {
return swap32(x);
}
static inline uint16 littleEndian16(uint16 x) {
return x;
}
static inline uint32 littleEndian32(uint32 x) {
return x;
}
#elif BYTE_ORDER == 4321
// Big-endian machines
static inline uint16 bigEndian16(uint16 x) {
return x;
}
static inline uint32 bigEndian32(uint32 x) {
return x;
}
static inline uint16 littleEndian16(uint16 x) {
return swap16(x);
}
static inline uint32 littleEndian32(uint32 x) {
return swap32(x);
}
#else
#error Unsupported BYTE_ORDER
#endif
#endif
#endif