Debian Codegraph @

Xtree-house/bindings/vendor/src/portable/endian.h

raw
1 // "License": Public Domain
2 // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
3 // In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
4 // be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
5 // an example on how to get the endian conversion functions on different platforms.
6
7 // updates from https://github.com/mikepb/endian.h/issues/4
8
9 #ifndef ENDIAN_H
10 #define ENDIAN_H
11
12 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
13
14 # define __WINDOWS__
15
16 #endif
17
18 #if defined(HAVE_ENDIAN_H) || \
19 defined(__linux__) || \
20 defined(__GNU__) || \
21 defined(__HAIKU__) || \
22 defined(__illumos__) || \
23 defined(__redox__) || \
24 defined(__NetBSD__) || \
25 defined(__OpenBSD__) || \
26 defined(__CYGWIN__) || \
27 defined(__MSYS__) || \
28 defined(__EMSCRIPTEN__) || \
29 defined(__wasi__) || \
30 defined(__wasm__)
31
32 #if defined(__NetBSD__)
33 #define _NETBSD_SOURCE 1
34 #endif
35
36 # include <endian.h>
37
38 #elif defined(HAVE_SYS_ENDIAN_H) || \
39 defined(__FreeBSD__) || \
40 defined(__DragonFly__)
41
42 # include <sys/endian.h>
43
44 #elif defined(__APPLE__)
45 # define __BYTE_ORDER BYTE_ORDER
46 # define __BIG_ENDIAN BIG_ENDIAN
47 # define __LITTLE_ENDIAN LITTLE_ENDIAN
48 # define __PDP_ENDIAN PDP_ENDIAN
49
50 # if !defined(_POSIX_C_SOURCE)
51 # include <libkern/OSByteOrder.h>
52
53 # define htobe16(x) OSSwapHostToBigInt16(x)
54 # define htole16(x) OSSwapHostToLittleInt16(x)
55 # define be16toh(x) OSSwapBigToHostInt16(x)
56 # define le16toh(x) OSSwapLittleToHostInt16(x)
57
58 # define htobe32(x) OSSwapHostToBigInt32(x)
59 # define htole32(x) OSSwapHostToLittleInt32(x)
60 # define be32toh(x) OSSwapBigToHostInt32(x)
61 # define le32toh(x) OSSwapLittleToHostInt32(x)
62
63 # define htobe64(x) OSSwapHostToBigInt64(x)
64 # define htole64(x) OSSwapHostToLittleInt64(x)
65 # define be64toh(x) OSSwapBigToHostInt64(x)
66 # define le64toh(x) OSSwapLittleToHostInt64(x)
67 # else
68 # if BYTE_ORDER == LITTLE_ENDIAN
69 # define htobe16(x) __builtin_bswap16(x)
70 # define htole16(x) (x)
71 # define be16toh(x) __builtin_bswap16(x)
72 # define le16toh(x) (x)
73
74 # define htobe32(x) __builtin_bswap32(x)
75 # define htole32(x) (x)
76 # define be32toh(x) __builtin_bswap32(x)
77 # define le32toh(x) (x)
78
79 # define htobe64(x) __builtin_bswap64(x)
80 # define htole64(x) (x)
81 # define be64toh(x) __builtin_bswap64(x)
82 # define le64toh(x) (x)
83 # elif BYTE_ORDER == BIG_ENDIAN
84 # define htobe16(x) (x)
85 # define htole16(x) __builtin_bswap16(x)
86 # define be16toh(x) (x)
87 # define le16toh(x) __builtin_bswap16(x)
88
89 # define htobe32(x) (x)
90 # define htole32(x) __builtin_bswap32(x)
91 # define be32toh(x) (x)
92 # define le32toh(x) __builtin_bswap32(x)
93
94 # define htobe64(x) (x)
95 # define htole64(x) __builtin_bswap64(x)
96 # define be64toh(x) (x)
97 # define le64toh(x) __builtin_bswap64(x)
98 # else
99 # error byte order not supported
100 # endif
101 # endif
102
103 #elif defined(__WINDOWS__)
104
105 # if defined(_MSC_VER) && !defined(__clang__)
106 # include <stdlib.h>
107 # define B_SWAP_16(x) _byteswap_ushort(x)
108 # define B_SWAP_32(x) _byteswap_ulong(x)
109 # define B_SWAP_64(x) _byteswap_uint64(x)
110 # else
111 # define B_SWAP_16(x) __builtin_bswap16(x)
112 # define B_SWAP_32(x) __builtin_bswap32(x)
113 # define B_SWAP_64(x) __builtin_bswap64(x)
114 # endif
115
116 # if defined(__MINGW32__) || defined(HAVE_SYS_PARAM_H)
117 # include <sys/param.h>
118 # endif
119
120 # ifndef BIG_ENDIAN
121 # ifdef __BIG_ENDIAN
122 # define BIG_ENDIAN __BIG_ENDIAN
123 # elif defined(__ORDER_BIG_ENDIAN__)
124 # define BIG_ENDIAN __ORDER_BIG_ENDIAN__
125 # else
126 # define BIG_ENDIAN 4321
127 # endif
128 # endif
129
130 # ifndef LITTLE_ENDIAN
131 # ifdef __LITTLE_ENDIAN
132 # define LITTLE_ENDIAN __LITTLE_ENDIAN
133 # elif defined(__ORDER_LITTLE_ENDIAN__)
134 # define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
135 # else
136 # define LITTLE_ENDIAN 1234
137 # endif
138 # endif
139
140 # ifndef BYTE_ORDER
141 # ifdef __BYTE_ORDER
142 # define BYTE_ORDER __BYTE_ORDER
143 # elif defined(__BYTE_ORDER__)
144 # define BYTE_ORDER __BYTE_ORDER__
145 # else
146 /* assume LE on Windows if nothing was defined */
147 # define BYTE_ORDER LITTLE_ENDIAN
148 # endif
149 # endif
150
151 # if BYTE_ORDER == LITTLE_ENDIAN
152
153 # define htobe16(x) B_SWAP_16(x)
154 # define htole16(x) (x)
155 # define be16toh(x) B_SWAP_16(x)
156 # define le16toh(x) (x)
157
158 # define htobe32(x) B_SWAP_32(x)
159 # define htole32(x) (x)
160 # define be32toh(x) B_SWAP_32(x)
161 # define le32toh(x) (x)
162
163 # define htobe64(x) B_SWAP_64(x)
164 # define htole64(x) (x)
165 # define be64toh(x) B_SWAP_64(x)
166 # define le64toh(x) (x)
167
168 # elif BYTE_ORDER == BIG_ENDIAN
169
170 # define htobe16(x) (x)
171 # define htole16(x) B_SWAP_16(x)
172 # define be16toh(x) (x)
173 # define le16toh(x) B_SWAP_16(x)
174
175 # define htobe32(x) (x)
176 # define htole32(x) B_SWAP_32(x)
177 # define be32toh(x) (x)
178 # define le32toh(x) B_SWAP_32(x)
179
180 # define htobe64(x) (x)
181 # define htole64(x) B_SWAP_64(x)
182 # define be64toh(x) (x)
183 # define le64toh(x) B_SWAP_64(x)
184
185 # else
186
187 # error byte order not supported
188
189 # endif
190
191 #elif defined(__QNXNTO__)
192
193 # include <gulliver.h>
194
195 # define __LITTLE_ENDIAN 1234
196 # define __BIG_ENDIAN 4321
197 # define __PDP_ENDIAN 3412
198
199 # if defined(__BIGENDIAN__)
200
201 # define __BYTE_ORDER __BIG_ENDIAN
202
203 # define htobe16(x) (x)
204 # define htobe32(x) (x)
205 # define htobe64(x) (x)
206
207 # define htole16(x) ENDIAN_SWAP16(x)
208 # define htole32(x) ENDIAN_SWAP32(x)
209 # define htole64(x) ENDIAN_SWAP64(x)
210
211 # elif defined(__LITTLEENDIAN__)
212
213 # define __BYTE_ORDER __LITTLE_ENDIAN
214
215 # define htole16(x) (x)
216 # define htole32(x) (x)
217 # define htole64(x) (x)
218
219 # define htobe16(x) ENDIAN_SWAP16(x)
220 # define htobe32(x) ENDIAN_SWAP32(x)
221 # define htobe64(x) ENDIAN_SWAP64(x)
222
223 # else
224
225 # error byte order not supported
226
227 # endif
228
229 # define be16toh(x) ENDIAN_BE16(x)
230 # define be32toh(x) ENDIAN_BE32(x)
231 # define be64toh(x) ENDIAN_BE64(x)
232 # define le16toh(x) ENDIAN_LE16(x)
233 # define le32toh(x) ENDIAN_LE32(x)
234 # define le64toh(x) ENDIAN_LE64(x)
235
236 #else
237
238 # error platform not supported
239
240 #endif
241
242 #endif